Hi I'm currently working on STM32 and I want to make my speaker work depending on it's PWM generation. I made the clock/pinout configuration and some parts of codes as written below, but I don't get why this code doesn't works. It doesn't make any errors in the building process, but my speaker doesn't make any sounds. I'm really not familiar with this STM32 programming, so if you could explain my problem in detail it would be a big help. Thanks a lot!
pinout configuration[
^]
clock configuration[
^]
Below these are the codes that I've written,(main.c/it.c/user_codex.h/user_codex.c) and for main.c and it.c I included the codes partially. Please help me solve my problem!
What I have tried:
HAL_TIM_PWM_Start(&htim1,0);
HAL_TIM_Base_Start_IT(&htim2);
Codex_off();
Codex_on();
Codex_Speaker_on();
while (1)
{
freq++;
if(freq>=1500) freq = 500;
HAL_Delay(1);
}
}
extern TIM_HandleTypeDef htim2;
extern UART_HandleTypeDef huart1;
extern TIM_HandleTypeDef htim1;
extern uint16_t freq;
void TIM2_IRQHandler(void)
{
__HAL_TIM_SET_COMPARE(&htim1,0,(int)500*SinGenerator(freq) + 500);
HAL_TIM_IRQHandler(&htim2);
}
#ifndef INC_USER_CODEX_H_
#define INC_USER_CODEX_H_
#include "main.h"
#define CODEX_READ 0x03
#define CODEX_WRITE 0x02
#define CODEX_REG_MODE 0x00
#define CODEX_REG_WRAM 0x06
#define CODEX_REG_WRAMADDR 0x07
#define CODEX_MEM_DDR 0xc017
#define CODEX_MEM_ODATA 0xc019
void Codex_on();
void Codex_off();
void Codex_reg_read(uint8_t addr, uint16_t* data);
void Codex_reg_write(uint8_t addr, uint16_t data);
void Codex_Speaker_on();
void Codex_Speaker_off();
float SinGenerator(uint16_t freq);
#endif /* INC_USER_CODEX_H_ */
#include "user_codex.h"
extern SPI_HandleTypeDef hspi1;
void Codex_on()
{
HAL_GPIO_WritePin(VS_RST_GPIO_Port,VS_RST_Pin,GPIO_PIN_SET);
HAL_Delay(100);
}
void Codex_off()
{
HAL_GPIO_WritePin(VS_RST_GPIO_Port,VS_RST_Pin,GPIO_PIN_RESET);
HAL_Delay(100);
}
void Codex_reg_read(uint8_t addr, uint16_t* data)
{
uint8_t pTxData[4];
uint8_t pRxData[4];
pTxData[0] = CODEX_READ;
pTxData[1] = addr;
while(HAL_GPIO_ReadPin(VS_DERQ_GPIO_Port,VS_DERQ_Pin) == GPIO_PIN_RESET);
HAL_GPIO_WritePin(VS_XCS_GPIO_Port,VS_XCS_Pin,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1,pTxData,pRxData,4,100);
HAL_GPIO_WritePin(VS_XCS_GPIO_Port,VS_XCS_Pin,GPIO_PIN_SET);
*data = (pRxData[2]<<8) | pRxData[3];
}
void Codex_reg_write(uint8_t addr, uint16_t data)
{
uint8_t pTxData[4];
uint8_t pRxData[4];
pTxData[0] = CODEX_READ;
pTxData[1] = addr;
pTxData[2] = 0xff & (data >> 8);
pTxData[3] = 0xff & data;
while(HAL_GPIO_ReadPin(VS_DERQ_GPIO_Port,VS_DERQ_Pin) == GPIO_PIN_RESET);
HAL_GPIO_WritePin(VS_XCS_GPIO_Port,VS_XCS_Pin,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1,pTxData,pRxData,4,100);
HAL_GPIO_WritePin(VS_XCS_GPIO_Port,VS_XCS_Pin,GPIO_PIN_SET);
}
void Codex_Speaker_on()
{
Codex_reg_write(CODEX_REG_WRAMADDR, CODEX_MEM_DDR);
Codex_reg_write(CODEX_REG_WRAM, 0x10);
Codex_reg_write(CODEX_REG_WRAMADDR, CODEX_MEM_ODATA);
Codex_reg_write(CODEX_REG_WRAM, 0x10);
}
void Codex_Speaker_off()
{
Codex_reg_write(CODEX_REG_WRAMADDR, CODEX_MEM_DDR);
Codex_reg_write(CODEX_REG_WRAM, 0x10);
Codex_reg_write(CODEX_REG_WRAMADDR, CODEX_MEM_ODATA);
Codex_reg_write(CODEX_REG_WRAM, 0x00);
}
float SinGenerator(uint16_t freq)
{
static float phase = 0;
phase += 2.0*3.14*(float)freq / 8000.0;
return sin(phase);
}