This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

MCBSTM32F200 and CS42L52 Audio Init Issues

Hi Everyone!

I am trying to get the audio to work. Not trying to do anything too complicated. Just want it to beep. I went through the schematics and documentations and came up with some code. A lot borrowed from example projects provided. It doesn't seem to do anything right now. I am not sure about the RST signal and how to control it.

I started hard coding values to try to make it work.

If anyone has the time to scan through the code or give any tips, it would be greatly appreciated.

/*-----------------------------------------------------------------------------
 * Name:    Sound.c
 * Purpose: Low level sound functions
 * Note(s):
 *----------------------------------------------------------------------------*/

#include <stm32f2xx.h>                  /* STM32F2xx Definitions              */
#include "Sound.h"
#include "I2C.h"

#define IOES_I2C_ADDR 0x4A               /* IO expander I2C write address      */

/* Prototypes */
static uint32_t  IOE_Read  (uint8_t reg, uint8_t *val);
static uint32_t  IOE_Write (uint8_t reg, uint8_t  val);
static void              S_BEEP_SET_PITCH(uint32_t pitch);
static void S_VOL_INIT(uint32_t volume);
static void S_BEEP_SET_SINGLE();
static void S_BEEP_SET_MULTI();
static void S_BEEP_SET_CONT();
static void S_BEEP_SETB(uint32_t beep);
void S_Beep_Init (uint32_t pitch, uint32_t volume);

/*-----------------------------------------------------------------------------
 *       S_Init:  Initialize
 *----------------------------------------------------------------------------*/
void S_Init (void) {
        uint8_t temp_data;

        //Set PDN to 1
        IOE_Read  (0x02, &temp_data);
        temp_data |= 0x01;
  temp_data = IOE_Write (0x02, temp_data);               /* Set To Master Mode  */

  IOE_Write (0x00, 0x99);               /*       */
  IOE_Write (0x3E, 0xBA);               /*   */
  IOE_Write (0x47, 0x80);               /*    */
        IOE_Read  (0x32, &temp_data);
        temp_data |= 0x80;
  IOE_Write (0x32, temp_data);               /*    */
        temp_data &= ~0x80;
  IOE_Write (0x32, temp_data);               /*  */
  IOE_Write (0x00, 0x00);               /*   */

        //Set PDN to 0
        IOE_Read  (0x02, &temp_data);
        temp_data &= ~0x01;
  IOE_Write (0x02, temp_data);               /* Set To Master Mode      */

        //Set to Master Mode
        IOE_Read  (0x06, &temp_data);
        temp_data |= 0x80;
  IOE_Write (0x06, temp_data);               /* Set To Master Mode      */

        S_Beep_Init(1, 10);

}

void S_Beep_Init (uint32_t pitch, uint32_t volume){
        uint8_t temp_data;

        S_BEEP_SET_PITCH(pitch);

        S_VOL_INIT(volume);

        S_BEEP_SET_CONT();

}


void S_BEEP_SET_OFF(){
        S_BEEP_SETB(0);
}

void S_BEEP_SET_SINGLE(){
        S_BEEP_SETB(1);
}

void S_BEEP_SET_MULTI(){
        S_BEEP_SETB(2);
}

void S_BEEP_SET_CONT(){
        S_BEEP_SETB(3);
}

void S_BEEP_SETB(uint32_t beep){
                uint8_t temp_data;

        if(beep >=4){
                beep = 0;
        }
        //for(;;);
        //Set to Master Mode
        IOE_Read  (0x1E, &temp_data);
        temp_data &= ~0xC0;
        //temp_data |= 6UL << beep;

        temp_data |= 0xC0;
  IOE_Write (0x1E, temp_data);               /* Set To Master Mode      */
}


void S_BEEP_SET_PITCH(uint32_t pitch){
        uint8_t temp_data;

        if(pitch >=16){
                pitch = 0;
        }

        //Set to Master Mode
        IOE_Read  (0x1C, &temp_data);
        temp_data &= ~0xF0;
        //temp_data |= 4UL << pitch;
  IOE_Write (0x1C, temp_data);               /* Set To Master Mode      */
}

void S_VOL_INIT(uint32_t volume){
        uint8_t temp_data;

        if(volume >=32){
                volume = 10;
        }

        //Set to Master Mode
        IOE_Read  (0x1D, &temp_data);
        temp_data &= ~0x1F;
        //temp_data |= volume;
        temp_data = 0xFF;
  IOE_Write (0x1D, temp_data);               /* Set To Master Mode      */
}

/*-----------------------------------------------------------------------------
 *      IOE_Write:  Write value to the STMPE811 register
 *
 *  Parameter:  reg - register address
 *              val - value to be written
 *
 *  Return:   0 on success, nonzero on error
 *----------------------------------------------------------------------------*/
static uint32_t IOE_Write (uint8_t reg, uint8_t val) {
  return (I2C_WrData (IOES_I2C_ADDR, reg, &val, 1));
}


/*-----------------------------------------------------------------------------
 *      IOE_Read:  Read data from the STMPE811 register
 *
 *  Parameter:  reg - register address
 *              val - variable where value will be stored
 *
 *  Return: 0 on success, nonzero on error
 *----------------------------------------------------------------------------*/
static uint32_t IOE_Read (uint8_t reg, uint8_t *val) {
  return (I2C_RdData (IOES_I2C_ADDR, reg, val, 1));
}

/*-----------------------------------------------------------------------------
 * End of file
 *----------------------------------------------------------------------------*/