cancel
Showing results for 
Search instead for 
Did you mean: 

How do I initial CS43L22 on STM32F407G?

BC.1
Associate

Hi,

I tried couple to run my code, but I can't just hear any noise or beep. I am very new to this.

One issue that I found is SPI3->DR is not receiving anything during debugging.

Here is my code and correct me,

#include <mbed.h>

#include<I2C.h>

#include <stm32f4xx_hal.h>

I2C i2c(PB_9,PB_6);

int main() {

    

    //SETTING UP FOR SPI3

    RCC->APB1ENR |= RCC_APB1ENR_SPI3EN; //Enable SPI3 clock

    RCC->APB1RSTR |= (1<<15);//Reset the SPI3

    RCC->APB1RSTR &= ~(1<<15);//Clear the SPI3

    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;//Enable GPIO clock

    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;//Enable GPIO clock

    

    int mclk_pinc=7,sclk_pinc=10,sd_pinc=12,fs_pina=4;//mclk_pin=7,sclk_pin=10,sd_pin=12,fs_pin=4;

    const int slave_addr=0x94;

    char sound_data[2];

    // Configure I2S MCK, SCK, SD pins.

    GPIOC->MODER &= ~(3UL<<(mclk_pinc*2));

    GPIOC->MODER |= 2UL<<(mclk_pinc*2);

    GPIOC->MODER &= ~(3UL<<(sclk_pinc*2));

    GPIOC->MODER |= 2UL<<(sclk_pinc*2);

    GPIOC->MODER &= ~(3UL<<(sd_pinc*2)); //sd_pin

    GPIOC->MODER |= 2UL<<(sd_pinc*2);

    GPIOA->MODER &= ~(3UL<<(fs_pina*2)); // lr clk

    GPIOA->MODER |= 2UL<<(fs_pina*2);

    GPIOE->OTYPER &=~(1<<mclk_pinc);

    GPIOE->OTYPER &=~(1<<sclk_pinc);

    GPIOE->OTYPER &=~(1<<sd_pinc);

    GPIOA->OTYPER &=~(1<<fs_pina);

    GPIOC->OSPEEDR |= (3<<(mclk_pinc*2));//High speed

    GPIOC->OSPEEDR |= (3<<(sclk_pinc*2));//High speed

    GPIOC->OSPEEDR |= (3<<(sd_pinc*2));//High speed

    GPIOA->OSPEEDR |= (3<<(fs_pina*2));//High speed

    GPIOC->PUPDR &=~(3UL<<(mclk_pinc*2));

    GPIOC->PUPDR &=~(3UL<<(sclk_pinc*2));

    GPIOC->PUPDR &=~(3UL<<(sd_pinc*2));

    GPIOA->PUPDR &=~(3UL<<(fs_pina*2));

    /* (appendix I of book) */

    GPIOC->AFR[1] &= ~0xffffffff;//SPI3_SCK Serial Clock (mapped on the SCK pin)

    GPIOC->AFR[1] |=  0x00000600;

    GPIOC->AFR[1] &= ~0xffffffff;//SPI3_MOSI SD: Serial Data (mapped on the MOSI pin)

    GPIOC->AFR[1] |=  0x00060000;

    GPIOC->AFR[0] &= ~0xffffffff;//SPI3_MOSI SD: Serial Data (mapped on the MOSI pin)

    GPIOC->AFR[0] |=  0x60000000;

    GPIOA->AFR[0] &= ~0xffffffff;//SPI3_NSS WS: Word Select (mapped on the NSS pin)

    GPIOA->AFR[0] |=  0x00060000;

    // Configure CS43L22.

    sound_data[0] =0x02;

    sound_data[1] =0x01;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0] =0x04;

    sound_data[1] =0xaf;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0] =0x05;

    sound_data[1] =0x81;

    i2c.write(slave_addr,sound_data,2);

    

    sound_data[0] =0x06;

    sound_data[1] =0x04;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0]=0x20;

    sound_data[1]=90+0x19;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0]=0x21;

    sound_data[1]=90+0x19;

    i2c.write(slave_addr,sound_data,2);

    // Power on the code.

    sound_data[0] =0x02;

    sound_data[1] =0x9e;

    i2c.write(slave_addr,sound_data,2);

    // Configure codec for fast shutdown.

    sound_data[0] =0x0a;

    sound_data[1] =0x00;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0] =0x0e;

    sound_data[1] =0x04;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0] =0x27;

    sound_data[1] =0x00;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0] =0x1f;

    sound_data[1] =0x0f;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0] =0x1a;

    sound_data[1] =0x0a;

    i2c.write(slave_addr,sound_data,2);

    sound_data[0] =0x1b;

    sound_data[1] =0x0a;

    i2c.write(slave_addr,sound_data,2);

    

    SPI3->I2SCFGR=0;// Disable I2S for configuration

    // I2S clock configuration

    RCC->CFGR&=~RCC_CFGR_I2SSRC; // PLLI2S clock used as I2S clock source.

    RCC->PLLI2SCFGR=(50<<28)|(5<<6);

    // Enable PLLI2S and wait until it is ready.

    RCC->CR|=RCC_CR_PLLI2SON;

    while(!(RCC->CR&RCC_CR_PLLI2SRDY));

    // Configure I2S.

    SPI3->I2SPR=1|(0<<8)|SPI_I2SPR_MCKOE;

    SPI3->I2SCFGR=SPI_I2SCFGR_I2SMOD|SPI_I2SCFGR_I2SCFG_1|SPI_I2SCFGR_I2SE; // Master transmitter, Phillips mode, 16 bit values, clock polarity low, enable.

    sound_data[0] =0x02;

    sound_data[1] =0x9e;

    i2c.write(slave_addr,sound_data,2);//Power on

while(1)

    {

        if((SPI3->SR & SPI_SR_TXE))

        {

            SPI3->DR = 0xffff; // This DR is not receiving anything.

        }

    }

}

1 REPLY 1
Ozone
Lead II

If you ignore Cube/HAL anyway, I suggest to download the "old", SPL-based F4 Discovery firmware package, which contains drivers and example applications for the DAC.