Forum: Mikrocontroller und Digitale Elektronik STM32F4 Discovery Audio CS43L22 I2C Problem


von BK (Gast)


Lesenswert?

Hallo,

Ich versuche den CS43L22 Audio chip meines STM32F4 Discovery Audio mit 
Hilfe des internen Beep Generators Töne ausgeben zu lassen.
Die I2C Kommunikation habe ich bereits implementiert. I2S werde ich noch 
hinzufügen.
Bei der Kommunikation über I2C treten Fehler auf. Im meinem Code main.c 
hängt sich das Programm bei dem zweiten Schreiben eines Registers auf. 
Bei Zeile 130:
1
 while(HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000) != HAL_OK){}; // write power register 1

Wenn ich debugge und schrittweise durchgehe funktioniert der Code.
Beim Debuggen und einem run von Breakpoint zu Breakpoint tritt der 
Fehler wiederrum auf. Ich konne das Hängen auf die Zeilen 5084 bis 5122 
in stm32f4xx_hal_i2c.c eingrenzen:
1
static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
2
{
3
  while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
4
  {
5
    if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
6
    {
7
      /* Generate Stop */
8
      hi2c->Instance->CR1 |= I2C_CR1_STOP;
9
10
      /* Clear AF Flag */
11
      __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
12
13
      hi2c->ErrorCode = HAL_I2C_ERROR_AF;
14
      hi2c->PreviousState = I2C_STATE_NONE;
15
      hi2c->State= HAL_I2C_STATE_READY;
16
17
      /* Process Unlocked */
18
      __HAL_UNLOCK(hi2c);
19
20
      return HAL_ERROR;
21
    }
22
23
    /* Check for the Timeout */
24
    if(Timeout != HAL_MAX_DELAY)
25
    {
26
      if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
27
      {
28
        hi2c->PreviousState = I2C_STATE_NONE;
29
        hi2c->State= HAL_I2C_STATE_READY;
30
31
        /* Process Unlocked */
32
        __HAL_UNLOCK(hi2c);
33
34
        return HAL_TIMEOUT;
35
      }
36
    }
37
  }
38
  return HAL_OK;
39
}

Ich benutze Keil µVision 5.15 und würde gerne die HAL Treiber benutzen, 
da ich den Code später in einem RTOS einsetzen möchte. Nach vielen 
Versuchen den Chip zum laufen zu bringen bin ich nun etwas am 
verzweifeln und bin über jede Hilfe dankbar. Falls noch Informationen 
fehlen stelle ich die gerne zur Verfügung.

Hat jemand eine Idee worin das komische Verhalten begründet ist und wie 
man es vielleicht behebt?

Mein main.c:
1
// main.c
2
3
#include "stm32f4xx.h"
4
#include "my_header.h"
5
6
7
int main(void){
8
  
9
  I2C_HandleTypeDef I2C_Params; // declare Parameter Handle
10
  HAL_StatusTypeDef I2C_State; // declare State handle
11
  GPIO_InitTypeDef GPIOD_Params; // Declares the structure handle for the parameters of the reset pin on GPIOE
12
  GPIO_InitTypeDef GPIOB_Params; // Declares the structure handle for the parameters of SCL (PB6) and SDA (PB9) on GPIOB
13
  uint8_t send_data; // declare send data variable
14
  uint8_t send_data_array[2]; // declare send data variable
15
  uint8_t received_data; // declare received data variable
16
  
17
  HAL_Init(); // Initialise  Hal driver
18
  
19
  
20
  // configure System Clock
21
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
22
  RCC_OscInitTypeDef RCC_OscInitStruct;
23
24
  /* Enable Power Control clock */
25
  __PWR_CLK_ENABLE();
26
  
27
  /* The voltage scaling allows optimizing the power consumption when the device is 
28
     clocked below the maximum system frequency, to update the voltage scaling value 
29
     regarding system frequency refer to product datasheet.  */
30
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
31
  
32
  /* Enable HSE Oscillator and activate PLL with HSE as source */
33
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
34
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
35
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
36
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
37
  RCC_OscInitStruct.PLL.PLLM = 8;
38
  RCC_OscInitStruct.PLL.PLLN = 336;
39
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
40
  RCC_OscInitStruct.PLL.PLLQ = 7;
41
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
42
  
43
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
44
     clocks dividers */
45
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
46
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
47
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
48
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
49
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
50
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
51
  
52
  
53
  
54
  // Configure GPIOD for reset pin on CS43L22
55
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; //Enable the clock for GPIOD
56
  GPIOD_Params.Pin = GPIO_PIN_4; // Select pin 4
57
  GPIOD_Params.Mode = GPIO_MODE_OUTPUT_PP; //Selects normal output push-pull mode
58
  GPIOD_Params.Speed = GPIO_SPEED_FAST; //Selects fast speed
59
  GPIOD_Params.Pull = GPIO_PULLDOWN; //Selects pull-down activation
60
  
61
  HAL_GPIO_Init(GPIOD, &GPIOD_Params); // Sets GPIOD into the modes specified in GPIOE_Params
62
63
  
64
  // Configure GPIOB for SCL (PB6) and SDA (PB9) on I2C1
65
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; //Enable the clock for GPIOA
66
  GPIOB_Params.Pin = GPIO_PIN_6 | GPIO_PIN_9; // Selects pins 6 and 9
67
  GPIOB_Params.Alternate = GPIO_AF4_I2C1; //Selects alternate function (I2C1)
68
  GPIOB_Params.Mode = GPIO_MODE_AF_OD; //Selects alternate function open drain mode
69
  GPIOB_Params.Speed = GPIO_SPEED_FAST; //Selects fast speed
70
  //GPIOB_Params.Speed = GPIO_SPEED_FREQ_VERY_HIGH; //Selects fast speed
71
  GPIOB_Params.Pull = GPIO_NOPULL; //Selects no pull-up or pull-down activation
72
  //GPIOB_Params.Pull = GPIO_PULLUP; //Selects no pull-up activation
73
  
74
  HAL_GPIO_Init(GPIOB, &GPIOB_Params); // Sets GPIOB into the modes specified in GPIOA_Params
75
  
76
  LED_Init(); // initialise LEDs
77
  Set_State_Rd_LED(1); // turn on red LED
78
  
79
  // Configure I2C
80
  RCC->APB1ENR |= RCC_APB1ENR_I2C1EN  ; // Enables the clock for I2C1
81
  
82
  I2C_Params.Instance = I2C1;
83
  I2C_Params.Init.ClockSpeed = 100000; // Set clock speed to 50 kHz
84
  I2C_Params.Init.DutyCycle = I2C_DUTYCYCLE_2; // Set duty cycle
85
  I2C_Params.Init.OwnAddress1 = 0x33; // set own adress 1  
86
  // I2C_Params.Init.OwnAddress1 = 0x00; // set own adress 1  
87
  I2C_Params.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; // set to 7-Bit adresses
88
  I2C_Params.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; // Disabel dual address mode
89
  I2C_Params.Init.OwnAddress2 = 0; // set own adress 2
90
  I2C_Params.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; // General call disabled
91
  // I2C_Params.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; // No Stretch disabled
92
  I2C_Params.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE; // No Stretch enabled
93
  // I2C_Params.Mode = HAL_I2C_MODE_MASTER; // set I2C to master
94
  
95
  // HAL_I2C_MspInit(&I2C_Params); // configure I2C
96
  I2C_State = HAL_I2C_Init(&I2C_Params); // configure I2C
97
  
98
  __HAL_I2C_ENABLE(&I2C_Params); //Enable the SPI
99
  
100
  GPIOD->BSRR |= GPIO_PIN_4; //Sets the reset pin of CS43L22 high
101
  
102
  I2C_State = HAL_I2C_IsDeviceReady(&I2C_Params, 0x0094, 10, 1000); //
103
  
104
  if(I2C_State == HAL_OK){
105
    Set_State_Gn_LED(1); // turn on green LED
106
  }
107
  else{
108
    Set_State_Gn_LED(0); // turn off green LED
109
  }
110
  
111
  // send new value
112
  send_data_array[0] = 0x1E; // address of Beep register
113
  send_data_array[1] = 0xC0; // value for Beep register (contious beep)
114
  while(HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000) != HAL_OK){}; // write Beep register
115
  
116
  
117
  // ceck value
118
  send_data = 0x1E; // address of Beep register
119
  while(HAL_I2C_Master_Transmit(&I2C_Params, 0x94, &send_data, 1, 1000) != HAL_OK){}; // Write Address to read
120
  
121
  while(HAL_I2C_Master_Receive(&I2C_Params, 0x94, &received_data, 1, 1000) != HAL_OK){}; // read Beep register
122
  
123
  if(received_data == 0xC0){
124
    Set_State_Bl_LED(1); // turn on blue LED
125
  }
126
  
127
  // send new value
128
  send_data_array[0] = 0x02; // adress of power register 1
129
  send_data_array[1] = 0x9E; // value for power register 1
130
  while(HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000) != HAL_OK){}; // write power register 1
131
132
  
133
  // ceck value
134
  send_data = 0x02; // adress of power register 1
135
  while(HAL_I2C_Master_Transmit(&I2C_Params, 0x94, &send_data, 1, 1000) != HAL_OK){}; // Write Address to read
136
  
137
  while(HAL_I2C_Master_Receive(&I2C_Params, 0x94, &received_data, 1, 1000) != HAL_OK){}; // read power register 1
138
  
139
  if(received_data == 0x9E){
140
    Set_State_Or_LED(1); // turn on orange LED
141
  }
142
};

von W.S. (Gast)


Lesenswert?

BK schrieb:
> und würde gerne die HAL Treiber benutzen,
> da ich den Code später in einem RTOS einsetzen möchte. Nach vielen
> Versuchen den Chip zum laufen zu bringen bin ich nun etwas am
> verzweifeln und bin über jede Hilfe dankbar.

Also mal ganz konkret:

Du benutzt dieses unsägliche Zeugs von ST, was nur dann zu funktinieren 
scheint, wenn du deine Aufrufe per Einzelschritt durchwinkst - und das 
alles NUR!, weil du irgend wann mal ein RTOS einsetzen willst.

Aha.

Du kämpfst derzeit vergeblich mit dem I2C-Core deines Chips und mit dem 
Teil der ST-Lib, der angeblich dafür zuständig ist.

Ich würde an deiner Stelle all diesen Kram erstmal komplett bleiben 
lassen, die beiden Pins für SCL und SDA als gewöhnliche Portpins mit 
Open-Collector Ausgang einrichten und die I2C-Signalspiele schlichtweg 
in Software erledigen - ist doch easy. Wenn da etwas nicht geht, dann 
weißt du wenigstens, an welcher Stelle es passiert. Ich hab die 
Erfahrung gemacht, daß die allermeisten I2C-Cores eher mistig 
konstruiert sind und Probleme mit ihrer eigenen Befindlichkeit machen.

Bevor du also dich um den eigentlichen CODEC kümmern kannst, mußt du 
dich um die Befindlichkeiten des I2C kümmern und davor auch noch um die 
Hakeligkeiten des I2C-Cores in deinem µC und in deinem Falle auch noch 
mit der ST-Lib. Prost Mahlzeit.

Also laß die letzteren weg, kümmere dich zu Fuß erstmal um den I2C und 
dann um deinen Audio-IC.

W.S.

von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

Du weisst aber schon, das der CS43L22 eine Initialisierung braucht, 
sonst piepst da nix.
Da sind auch noch ein paar undokumentierte Register, die man setzen 
muss. Hier mal frei nach Uwe B. eine brauchbare Initialisierung, 
allerdings in SPL:
1
/* first the I2C setup  */
2
uint16_t MS_Init_CS42L22(void) {
3
int16_t error = 0;
4
    GPIO_SetBits(DRIVE_GPIO_PORT,CS43_RST_PIN);
5
    Delay(50);  // lets settle the chip
6
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x02, 0x01); // power off
7
  if (error) return error;
8
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x00, 0x99); //  init sequence by datasheet
9
  if (error) return error;
10
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x47, 0x80); //  undocumented registers
11
  if (error) return error;
12
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x32, 0xBB); //  have to be written
13
  if (error) return error;
14
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x32, 0x3B); //
15
  if (error) return error;
16
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x00, 0x00); //  register 0 is not in the datasheet
17
  if (error) return error;
18
  return 0;
19
}
20
uint16_t MS_Control_CS42L22(void) {
21
int16_t error = 0;
22
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x02, 0x9E); // power on
23
  if (error) return error;
24
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x04, 0xFF); // SPK off , HP off
25
  if (error) return error;
26
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x05, 0x81); // autodetect clock
27
  if (error) return error;
28
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x06, CODEC_STANDARD); // codec used
29
  if (error) return error;
30
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x20, 0xFC); // master volume -2db
31
  if (error) return error;
32
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x21, 0xFC); // master volume -2db
33
  if (error) return error;
34
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x22, 0xF8); // HP volume -4db
35
  if (error) return error;
36
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x23, 0xF8); // HP volume -4db
37
  if (error) return error;
38
/* soft on and off ramp disable */
39
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x0A, 0x00); // analog soft ramp off
40
  if (error) return error;
41
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x0E, 0x04); // digital soft ramp off
42
  if (error) return error;
43
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x27, 0x00); // limiter attack off
44
  if (error) return error;
45
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x1e, 0x0B); // bass 100hz and treble 7khz tone control on
46
  if (error) return error;
47
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x1F, 0x88); // bass 0db and treble 0db
48
  if (error) return error;
49
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x1A, 0x00); // PCM volume A 0db
50
  if (error) return error;
51
  error = I2C_WriteByte(CODEC_I2C_ADRESS,0x1B, 0x00); // PCM volume B 0db
52
  if (error) return error;
53
54
return 0;
55
}
Nun ist der Chip sowohl für I2S als auch fürs Quäken bereit.

: Bearbeitet durch User
von BK (Gast)


Angehängte Dateien:

Lesenswert?

Vielen Dank für die Hinweise bis hierhin. Durch weitere Versuche hat 
sich gezeigt, dass der Fehler nicht im Zusammenhang mit I2C steht 
sondern anscheinend die Clock oder ein Stystick Interrupt nicht richtig 
funktioniert. Ich mache wohl einen Fehler bei der Initialisierung.

Wenn ich debugge hängt das Programm in startup_stm32f407xx.s bei der 
Zeile mit dem Inhalt "B" unter SysTick_Handler:
1
; Dummy Exception Handlers (infinite loops which can be modified)
2
3
NMI_Handler     PROC
4
                EXPORT  NMI_Handler                [WEAK]
5
                B       .
6
                ENDP
7
HardFault_Handler\
8
                PROC
9
                EXPORT  HardFault_Handler          [WEAK]
10
                B       .
11
                ENDP
12
MemManage_Handler\
13
                PROC
14
                EXPORT  MemManage_Handler          [WEAK]
15
                B       .
16
                ENDP
17
BusFault_Handler\
18
                PROC
19
                EXPORT  BusFault_Handler           [WEAK]
20
                B       .
21
                ENDP
22
UsageFault_Handler\
23
                PROC
24
                EXPORT  UsageFault_Handler         [WEAK]
25
                B       .
26
                ENDP
27
SVC_Handler     PROC
28
                EXPORT  SVC_Handler                [WEAK]
29
                B       .
30
                ENDP
31
DebugMon_Handler\
32
                PROC
33
                EXPORT  DebugMon_Handler           [WEAK]
34
                B       .
35
                ENDP
36
PendSV_Handler  PROC
37
                EXPORT  PendSV_Handler             [WEAK]
38
                B       .
39
                ENDP
40
SysTick_Handler PROC
41
                EXPORT  SysTick_Handler            [WEAK]
42
                B       .
43
                ENDP

Aber anscheinend funktioniert der ganze Aufbau kurzfristig (deshalb 
konnte ich auch über I2C übertragen). Ein neues Minimalbeispiel ist 
hier:
1
// main.c
2
3
#include "stm32f4xx.h"
4
5
6
int main(void){
7
  
8
  uint32_t ii;  
9
  
10
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
11
  RCC_OscInitTypeDef RCC_OscInitStruct;
12
  
13
  // SystemInit(); // tried with and without no change
14
  
15
  // HAL_Init(); // tried with and without no change
16
  
17
  /**
18
  * @brief  System Clock Configuration
19
  *         The system Clock is configured as follow : 
20
  *            System Clock source            = PLL (HSE)
21
  *            SYSCLK(Hz)                     = 168000000
22
  *            HCLK(Hz)                       = 168000000
23
  *            AHB Prescaler                  = 1
24
  *            APB1 Prescaler                 = 4
25
  *            APB2 Prescaler                 = 2
26
  *            HSE Frequency(Hz)              = 8000000
27
  *            PLL_M                          = 8
28
  *            PLL_N                          = 336
29
  *            PLL_P                          = 2
30
  *            PLL_Q                          = 7
31
  *            VDD(V)                         = 3.3
32
  *            Main regulator output voltage  = Scale1 mode
33
  *            Flash Latency(WS)              = 5
34
  * @param  None
35
  * @retval None
36
  */
37
  
38
  /* Enable Power Control clock */
39
  __PWR_CLK_ENABLE();
40
  
41
  /* The voltage scaling allows optimizing the power consumption when the device is 
42
     clocked below the maximum system frequency, to update the voltage scaling value 
43
     regarding system frequency refer to product datasheet.  */
44
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
45
  
46
  /* Enable HSE Oscillator and activate PLL with HSE as source */
47
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
48
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
49
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
50
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
51
  RCC_OscInitStruct.PLL.PLLM = 8;
52
  RCC_OscInitStruct.PLL.PLLN = 336;
53
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
54
  RCC_OscInitStruct.PLL.PLLQ = 7;
55
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
56
  
57
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
58
     clocks dividers */
59
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
60
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
61
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
62
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
63
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
64
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
65
66
67
  // initialise LEDs
68
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; // Enable Port D clock
69
  GPIOD->MODER |= GPIO_MODER_MODER12_0; // Port D.12 output - green LED
70
  
71
  for(ii=0; ii<260000; ii++){} // wait
72
  //for(ii=0; ii<260; ii++){} // wait
73
  
74
  GPIOD->BSRR = 1<<12; // Turn on the green LED
75
    
76
};

bei diesem leuchtet die grüne LED für ein wait von 260 Schritten und 
leuchtet nicht für 260000 Schritte (Programm hängt).

Als system_stm32f4xx.c benutze ich die von Keil automatisch generierte 
Datei wenn man im "Manage Run-Time Environment" unter Device --> 
STM32Cube Framwork (API) --> Classic wählt. Diese Datei findet ihr im 
Anhang.

Für vorherige Versuche (z.B. kommunikation mit dem Beschleunigungssensor 
auf dem Board über HAL_SPI) hat eine initalisierung der Clocks in 
system_stm32f4xx.c ausgereicht. hierbei wurde folgender Code nach Zeile 
192 eingefügt:
1
  
2
  // Sets PLL to give 168MHz when using HSI
3
RCC->PLLCFGR = RCC->PLLCFGR & ~0x00007FC0; /*Un-sets all PLLN bits */
4
RCC->PLLCFGR = RCC->PLLCFGR + (336<<6); /* Sets PLLN to 336 */
5
RCC->PLLCFGR = RCC->PLLCFGR & ~0x00030000; /*Un-sets all PLLP bits */
6
RCC->PLLCFGR = RCC->PLLCFGR + (1<<16); /* Sets PLLP to 4 */
7
RCC->PLLCFGR = RCC->PLLCFGR & ~0x0000003F; /*Un-sets all PLLM bits */
8
RCC->PLLCFGR = RCC->PLLCFGR + 8; /* Sets PLLM to 8 or can use 8 instead of hex */
9
// Enables the HSE clock
10
RCC->CR |= RCC_CR_HSEON;
11
// Waits for the HSE clock to become stable and ready to use
12
do
13
{
14
} while(((RCC->CR & RCC_CR_HSERDY) == 0));
15
/* Enables high performance mode, System frequency up to 168 MHz */
16
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
17
PWR->CR |= PWR_CR_PMODE;
18
//RCC->PLLCFGR |= RCC_PLLCFGR_PLLSRC_HSI; // Selects the HSI (internal oscillator) as the PLL source
19
RCC->PLLCFGR |= RCC_PLLCFGR_PLLSRC_HSE; // Selects the HSE (external oscillator) as the PLL source
20
21
/* Enables the main PLL */
22
RCC->CR |= RCC_CR_PLLON;
23
/* Waits until the main PLL is ready */
24
while((RCC->CR & RCC_CR_PLLRDY) == 0)
25
{
26
}
27
/* Configure Flash prefetch, Instruction cache, Data cache and wait state */
28
FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
29
/* Selects the main PLL as the system clock source */
30
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
31
RCC->CFGR |= RCC_CFGR_SW_PLL;
32
/* Waits until the main PLL is used as the system clock source */
33
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
34
{
35
}
36
RCC->CFGR |= RCC_CFGR_PPRE2_DIV2; // Sets the prescaler for the APB2 high speed clock to 2, which gives an APB1 clock frequency of 84MHz if the system clock is 168MHz
37
RCC->CFGR |= RCC_CFGR_PPRE1_DIV4; // Sets the prescaler for the APB1 low speed clock to 4, which gives an APB1 clock frequency of 42MHz if the system clock is 168MHz

Mit diesem Code funktionierten jedoch die I2C Funktionen nicht.

Nun die Frage was mache ich falsch bei der Initalisierung?

Sollte ich lieber in system_stm32f4xx.c initalisieren? Wenn ja was 
fehlt, dass die I2C Funktionen ausgeführt werden?

Was ist falsch bei der Initalisierung mit den HAL_Funktionen?

Vielen dank für eure Hilfe schon im Vorraus

von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

Also, bei HAL bin ich erstmal raus. Ich empfehle dir, mal Beispiel Code 
von STM anzuschauen, um die Initialisierung der Systemtakte und des 
SysTickers hinzukriegen.
Bei der SPL reicht es, system_stm32f4xx.c auf die gewünschten PLL Werte 
einzustellen und dann in main() einmal SystemInit() aufzurufen.
Der Systicker wird danach mit z.B.
1
  if (SysTick_Config(SystemCoreClock / 1000))
2
  {
3
    /* Capture error */
4
    while (1);
5
  }
aufgerufen - in diesem Beispiel für eine Granularität von 1 ms.
Meine Werte für die PLL:
1
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
2
#define PLL_M      4
3
#define PLL_N      168
4
5
/* SYSCLK = PLL_VCO / PLL_P */
6
#define PLL_P      2
7
8
/* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */
9
#define PLL_Q      7
Die I2S Clock konfiguriere ich dann mit PLL_N = 192 und PLL_R = 3.

: Bearbeitet durch User
von Bülent C. (mirki)


Lesenswert?

1
/**
2
  ******************************************************************************
3
  * File Name          : main.c
4
  * Description        : Main program body
5
  ******************************************************************************
6
  *
7
  * COPYRIGHT(c) 2016 STMicroelectronics
8
  *
9
  * Redistribution and use in source and binary forms, with or without modification,
10
  * are permitted provided that the following conditions are met:
11
  *   1. Redistributions of source code must retain the above copyright notice,
12
  *      this list of conditions and the following disclaimer.
13
  *   2. Redistributions in binary form must reproduce the above copyright notice,
14
  *      this list of conditions and the following disclaimer in the documentation
15
  *      and/or other materials provided with the distribution.
16
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
17
  *      may be used to endorse or promote products derived from this software
18
  *      without specific prior written permission.
19
  *
20
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
  *
31
  ******************************************************************************
32
  */
33
/* Includes ------------------------------------------------------------------*/
34
#include "main.h"
35
#include "stm32f4xx_hal.h"
36
37
/* USER CODE BEGIN Includes */
38
39
#include "WaveData.h"
40
41
42
43
#define CORE_I2C_ADDRESS 0x33
44
#define CODEC_I2C_ADDRESS 0x94
45
46
#define CODEC_MAPBYTE_INC 0x80
47
48
//register map bytes for CS42L22 (see page 35)
49
#define CODEC_MAP_CHIP_ID 0x01
50
#define CODEC_MAP_PWR_CTRL1 0x02
51
#define CODEC_MAP_PWR_CTRL2 0x04
52
#define CODEC_MAP_CLK_CTRL  0x05
53
#define CODEC_MAP_IF_CTRL1  0x06
54
#define CODEC_MAP_IF_CTRL2  0x07
55
#define CODEC_MAP_PASSTHROUGH_A_SELECT 0x08
56
#define CODEC_MAP_PASSTHROUGH_B_SELECT 0x09
57
#define CODEC_MAP_ANALOG_SET 0x0A
58
#define CODEC_MAP_PASSTHROUGH_GANG_CTRL 0x0C
59
#define CODEC_MAP_PLAYBACK_CTRL1 0x0D
60
#define CODEC_MAP_MISC_CTRL 0x0E
61
#define CODEC_MAP_PLAYBACK_CTRL2 0x0F
62
#define CODEC_MAP_PASSTHROUGH_A_VOL 0x14
63
#define CODEC_MAP_PASSTHROUGH_B_VOL 0x15
64
#define CODEC_MAP_PCMA_VOL 0x1A
65
#define CODEC_MAP_PCMB_VOL 0x1B
66
#define CODEC_MAP_BEEP_FREQ_ONTIME 0x1C
67
#define CODEC_MAP_BEEP_VOL_OFFTIME 0x1D
68
#define CODEC_MAP_BEEP_TONE_CFG 0x1E
69
#define CODEC_MAP_TONE_CTRL 0x1F
70
#define CODEC_MAP_MASTER_A_VOL 0x20
71
#define CODEC_MAP_MASTER_B_VOL 0x21
72
#define CODEC_MAP_HP_A_VOL 0x22
73
#define CODEC_MAP_HP_B_VOL 0x23
74
#define CODEC_MAP_SPEAK_A_VOL 0x24
75
#define CODEC_MAP_SPEAK_B_VOL 0x25
76
#define CODEC_MAP_CH_MIX_SWAP 0x26
77
#define CODEC_MAP_LIMIT_CTRL1 0x27
78
#define CODEC_MAP_LIMIT_CTRL2 0x28
79
#define CODEC_MAP_LIMIT_ATTACK 0x29
80
#define CODEC_MAP_OVFL_CLK_STATUS 0x2E
81
#define CODEC_MAP_BATT_COMP 0x2F
82
#define CODEC_MAP_VP_BATT_LEVEL 0x30
83
#define CODEC_MAP_SPEAK_STATUS 0x31
84
#define CODEC_MAP_CHARGE_PUMP_FREQ 0x34
85
86
/* USER CODE END Includes */
87
88
/* Private variables ---------------------------------------------------------*/
89
I2C_HandleTypeDef hi2c1;
90
91
I2S_HandleTypeDef hi2s3;
92
DMA_HandleTypeDef hdma_spi3_tx;
93
94
/* USER CODE BEGIN PV */
95
/* Private variables ---------------------------------------------------------*/
96
97
/* USER CODE END PV */
98
99
/* Private function prototypes -----------------------------------------------*/
100
void SystemClock_Config(void);
101
void Error_Handler(void);
102
static void MX_GPIO_Init(void);
103
static void MX_DMA_Init(void);
104
static void MX_I2C1_Init(void);
105
static void MX_I2S3_Init(void);
106
107
/* USER CODE BEGIN PFP */
108
/* Private function prototypes -----------------------------------------------*/
109
110
/* USER CODE END PFP */
111
112
/* USER CODE BEGIN 0 */
113
114
void send_codec_ctrl(uint8_t *werte, uint8_t laenge)     
115
{
116
  while( HAL_I2C_Master_Transmit(&hi2c1, CODEC_I2C_ADDRESS, werte,  laenge,  1000) !=HAL_OK){}
117
  
118
}
119
120
uint8_t read_codec_register(uint8_t regValue)
121
{
122
  uint8_t RxBuffer[1];
123
  HAL_I2C_Mem_Read(&hi2c1, CODEC_I2C_ADDRESS, regValue, I2C_MEMADD_SIZE_8BIT, RxBuffer, 1, 10000);
124
  return RxBuffer[0];
125
}
126
127
void codec_ctrl_init()
128
{
129
  
130
  uint8_t CodecCommandBuffer[5];
131
132
  uint8_t regValue = 0xFF;
133
  HAL_GPIO_WritePin(GPIOD, LD4_Pin|Audio_RST_Pin, GPIO_PIN_SET);
134
    
135
  HAL_Delay(1000);
136
  
137
  //keep codec OFF
138
    CodecCommandBuffer[0] = CODEC_MAP_PLAYBACK_CTRL1;
139
  CodecCommandBuffer[1] = 0x01;
140
  send_codec_ctrl(CodecCommandBuffer, 2);
141
142
  //begin initialization sequence (p. 32)
143
  CodecCommandBuffer[0] = 0x00;
144
  CodecCommandBuffer[1] = 0x99;
145
  send_codec_ctrl(CodecCommandBuffer, 2);
146
147
  CodecCommandBuffer[0] = 0x47;
148
  CodecCommandBuffer[1] = 0x80;
149
  send_codec_ctrl(CodecCommandBuffer, 2);
150
151
  regValue = read_codec_register(0x32);
152
153
  CodecCommandBuffer[0] = 0x32;
154
  CodecCommandBuffer[1] = regValue | 0x80;
155
  send_codec_ctrl(CodecCommandBuffer, 2);
156
157
  regValue = read_codec_register(0x32);
158
159
  CodecCommandBuffer[0] = 0x32;
160
  CodecCommandBuffer[1] = regValue & (~0x80);
161
  send_codec_ctrl(CodecCommandBuffer, 2);
162
163
  CodecCommandBuffer[0] = 0x00;
164
  CodecCommandBuffer[1] = 0x00;
165
  send_codec_ctrl(CodecCommandBuffer, 2);
166
  //end of initialization sequence
167
168
  CodecCommandBuffer[0] = CODEC_MAP_PWR_CTRL2;
169
  CodecCommandBuffer[1] = 0xAF;
170
  send_codec_ctrl(CodecCommandBuffer, 2);
171
172
  CodecCommandBuffer[0] = CODEC_MAP_PLAYBACK_CTRL1;
173
  CodecCommandBuffer[1] = 0x70;
174
  send_codec_ctrl(CodecCommandBuffer, 2);
175
176
  CodecCommandBuffer[0] = CODEC_MAP_CLK_CTRL;
177
  CodecCommandBuffer[1] = 0x81; //auto detect clock
178
  send_codec_ctrl(CodecCommandBuffer, 2);
179
180
  CodecCommandBuffer[0] = CODEC_MAP_IF_CTRL1;
181
  CodecCommandBuffer[1] = 0x07;
182
  send_codec_ctrl(CodecCommandBuffer, 2);
183
184
  CodecCommandBuffer[0] = 0x0A;
185
  CodecCommandBuffer[1] = 0x00;
186
  send_codec_ctrl(CodecCommandBuffer, 2);
187
188
  CodecCommandBuffer[0] = 0x27;
189
  CodecCommandBuffer[1] = 0x00;
190
  send_codec_ctrl(CodecCommandBuffer, 2);
191
192
  CodecCommandBuffer[0] = 0x1A | CODEC_MAPBYTE_INC;
193
  CodecCommandBuffer[1] = 0x0A;
194
  CodecCommandBuffer[2] = 0x0A;
195
  send_codec_ctrl(CodecCommandBuffer, 3);
196
197
  CodecCommandBuffer[0] = 0x1F;
198
  CodecCommandBuffer[1] = 0x0F;
199
  send_codec_ctrl(CodecCommandBuffer, 2);
200
201
  CodecCommandBuffer[0] = CODEC_MAP_PWR_CTRL1;
202
  CodecCommandBuffer[1] = 0x9E;
203
  send_codec_ctrl(CodecCommandBuffer, 2);
204
  
205
  /*
206
  CodecCommandBuffer[0] = CODEC_MAP_HP_A_VOL;
207
  CodecCommandBuffer[1] = 0x34;
208
  send_codec_ctrl(CodecCommandBuffer, 2);
209
  
210
  CodecCommandBuffer[0] = CODEC_MAP_HP_B_VOL;
211
  CodecCommandBuffer[1] = 0x34;
212
  send_codec_ctrl(CodecCommandBuffer, 2);
213
  */
214
  HAL_GPIO_WritePin(GPIOD, LD4_Pin, GPIO_PIN_RESET);
215
}
216
217
/* USER CODE END 0 */
218
219
int main(void)
220
{
221
222
  /* USER CODE BEGIN 1 */
223
224
  /* USER CODE END 1 */
225
226
  /* MCU Configuration----------------------------------------------------------*/
227
228
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
229
  HAL_Init();
230
231
  /* Configure the system clock */
232
  SystemClock_Config();
233
234
  /* Initialize all configured peripherals */
235
  MX_GPIO_Init();
236
  MX_DMA_Init();
237
  MX_I2C1_Init();
238
  MX_I2S3_Init();
239
240
  /* USER CODE BEGIN 2 */
241
  codec_ctrl_init();
242
  
243
  
244
  /* USER CODE END 2 */
245
246
  /* Infinite loop */
247
  /* USER CODE BEGIN WHILE */
248
  while (1)
249
  {
250
    HAL_I2S_Transmit(&hi2s3,(uint16_t *)sound0,2048,100);
251
    //HAL_I2S_Transmit(&hi2s3,(uint16_t *)result_sound,2048,100);
252
    
253
    
254
    
255
  /* USER CODE END WHILE */
256
257
  /* USER CODE BEGIN 3 */
258
259
  }
260
  /* USER CODE END 3 */
261
262
}
263
264
/** System Clock Configuration
265
*/
266
void SystemClock_Config(void)
267
{
268
269
  RCC_OscInitTypeDef RCC_OscInitStruct;
270
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
271
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
272
273
    /**Configure the main internal regulator output voltage 
274
    */
275
  __HAL_RCC_PWR_CLK_ENABLE();
276
277
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
278
279
    /**Initializes the CPU, AHB and APB busses clocks 
280
    */
281
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
282
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
283
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
284
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
285
  RCC_OscInitStruct.PLL.PLLM = 8;
286
  RCC_OscInitStruct.PLL.PLLN = 336;
287
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
288
  RCC_OscInitStruct.PLL.PLLQ = 7;
289
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
290
  {
291
    Error_Handler();
292
  }
293
294
    /**Initializes the CPU, AHB and APB busses clocks 
295
    */
296
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
297
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
298
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
299
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
300
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
301
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
302
303
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
304
  {
305
    Error_Handler();
306
  }
307
308
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
309
  PeriphClkInitStruct.PLLI2S.PLLI2SN = 352;
310
  PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
311
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
312
  {
313
    Error_Handler();
314
  }
315
316
    /**Configure the Systick interrupt time 
317
    */
318
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
319
320
    /**Configure the Systick 
321
    */
322
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
323
324
  /* SysTick_IRQn interrupt configuration */
325
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
326
}
327
328
/* I2C1 init function */
329
static void MX_I2C1_Init(void)
330
{
331
332
  hi2c1.Instance = I2C1;
333
  hi2c1.Init.ClockSpeed = 100000;
334
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
335
  hi2c1.Init.OwnAddress1 = 0;
336
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
337
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
338
  hi2c1.Init.OwnAddress2 = 0;
339
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
340
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
341
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
342
  {
343
    Error_Handler();
344
  }
345
346
}
347
348
/* I2S3 init function */
349
static void MX_I2S3_Init(void)
350
{
351
352
  hi2s3.Instance = SPI3;
353
  hi2s3.Init.Mode = I2S_MODE_MASTER_TX;
354
  hi2s3.Init.Standard = I2S_STANDARD_PHILIPS;
355
  hi2s3.Init.DataFormat = I2S_DATAFORMAT_16B;
356
  hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
357
  hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_8K;
358
  hi2s3.Init.CPOL = I2S_CPOL_LOW;
359
  hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
360
  hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE;
361
  if (HAL_I2S_Init(&hi2s3) != HAL_OK)
362
  {
363
    Error_Handler();
364
  }
365
366
}
367
368
/** 
369
  * Enable DMA controller clock
370
  */
371
static void MX_DMA_Init(void) 
372
{
373
  /* DMA controller clock enable */
374
  __HAL_RCC_DMA1_CLK_ENABLE();
375
376
  /* DMA interrupt init */
377
  /* DMA1_Stream5_IRQn interrupt configuration */
378
  HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
379
  HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
380
381
}
382
383
/** Configure pins as 
384
        * Analog 
385
        * Input 
386
        * Output
387
        * EVENT_OUT
388
        * EXTI
389
     PC3   ------> I2S2_SD
390
     PA5   ------> SPI1_SCK
391
     PA6   ------> SPI1_MISO
392
     PA7   ------> SPI1_MOSI
393
     PB10   ------> I2S2_CK
394
     PA9   ------> USB_OTG_FS_VBUS
395
     PA10   ------> USB_OTG_FS_ID
396
     PA11   ------> USB_OTG_FS_DM
397
     PA12   ------> USB_OTG_FS_DP
398
*/
399
static void MX_GPIO_Init(void)
400
{
401
402
  GPIO_InitTypeDef GPIO_InitStruct;
403
404
  /* GPIO Ports Clock Enable */
405
  __HAL_RCC_GPIOE_CLK_ENABLE();
406
  __HAL_RCC_GPIOC_CLK_ENABLE();
407
  __HAL_RCC_GPIOH_CLK_ENABLE();
408
  __HAL_RCC_GPIOA_CLK_ENABLE();
409
  __HAL_RCC_GPIOB_CLK_ENABLE();
410
  __HAL_RCC_GPIOD_CLK_ENABLE();
411
412
  /*Configure GPIO pin Output Level */
413
  HAL_GPIO_WritePin(CS_I2C_SPI_GPIO_Port, CS_I2C_SPI_Pin, GPIO_PIN_RESET);
414
415
  /*Configure GPIO pin Output Level */
416
  HAL_GPIO_WritePin(OTG_FS_PowerSwitchOn_GPIO_Port, OTG_FS_PowerSwitchOn_Pin, GPIO_PIN_SET);
417
418
  /*Configure GPIO pin Output Level */
419
  HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin 
420
                          |Audio_RST_Pin, GPIO_PIN_RESET);
421
422
  /*Configure GPIO pin : CS_I2C_SPI_Pin */
423
  GPIO_InitStruct.Pin = CS_I2C_SPI_Pin;
424
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
425
  GPIO_InitStruct.Pull = GPIO_NOPULL;
426
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
427
  HAL_GPIO_Init(CS_I2C_SPI_GPIO_Port, &GPIO_InitStruct);
428
429
  /*Configure GPIO pin : OTG_FS_PowerSwitchOn_Pin */
430
  GPIO_InitStruct.Pin = OTG_FS_PowerSwitchOn_Pin;
431
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
432
  GPIO_InitStruct.Pull = GPIO_NOPULL;
433
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
434
  HAL_GPIO_Init(OTG_FS_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
435
436
  /*Configure GPIO pin : PDM_OUT_Pin */
437
  GPIO_InitStruct.Pin = PDM_OUT_Pin;
438
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
439
  GPIO_InitStruct.Pull = GPIO_NOPULL;
440
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
441
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
442
  HAL_GPIO_Init(PDM_OUT_GPIO_Port, &GPIO_InitStruct);
443
444
  /*Configure GPIO pins : PA5 PA6 PA7 */
445
  GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
446
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
447
  GPIO_InitStruct.Pull = GPIO_NOPULL;
448
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
449
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
450
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
451
452
  /*Configure GPIO pin : BOOT1_Pin */
453
  GPIO_InitStruct.Pin = BOOT1_Pin;
454
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
455
  GPIO_InitStruct.Pull = GPIO_NOPULL;
456
  HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
457
458
  /*Configure GPIO pin : CLK_IN_Pin */
459
  GPIO_InitStruct.Pin = CLK_IN_Pin;
460
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
461
  GPIO_InitStruct.Pull = GPIO_NOPULL;
462
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
463
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
464
  HAL_GPIO_Init(CLK_IN_GPIO_Port, &GPIO_InitStruct);
465
466
  /*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin LD6_Pin 
467
                           Audio_RST_Pin */
468
  GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin 
469
                          |Audio_RST_Pin;
470
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
471
  GPIO_InitStruct.Pull = GPIO_NOPULL;
472
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
473
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
474
475
  /*Configure GPIO pin : VBUS_FS_Pin */
476
  GPIO_InitStruct.Pin = VBUS_FS_Pin;
477
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
478
  GPIO_InitStruct.Pull = GPIO_NOPULL;
479
  HAL_GPIO_Init(VBUS_FS_GPIO_Port, &GPIO_InitStruct);
480
481
  /*Configure GPIO pins : OTG_FS_ID_Pin OTG_FS_DM_Pin OTG_FS_DP_Pin */
482
  GPIO_InitStruct.Pin = OTG_FS_ID_Pin|OTG_FS_DM_Pin|OTG_FS_DP_Pin;
483
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
484
  GPIO_InitStruct.Pull = GPIO_NOPULL;
485
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
486
  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
487
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
488
489
  /*Configure GPIO pin : OTG_FS_OverCurrent_Pin */
490
  GPIO_InitStruct.Pin = OTG_FS_OverCurrent_Pin;
491
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
492
  GPIO_InitStruct.Pull = GPIO_NOPULL;
493
  HAL_GPIO_Init(OTG_FS_OverCurrent_GPIO_Port, &GPIO_InitStruct);
494
495
  /*Configure GPIO pin : MEMS_INT1_Pin */
496
  GPIO_InitStruct.Pin = MEMS_INT1_Pin;
497
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
498
  GPIO_InitStruct.Pull = GPIO_NOPULL;
499
  HAL_GPIO_Init(MEMS_INT1_GPIO_Port, &GPIO_InitStruct);
500
501
  /*Configure GPIO pin : MEMS_INT2_Pin */
502
  GPIO_InitStruct.Pin = MEMS_INT2_Pin;
503
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
504
  GPIO_InitStruct.Pull = GPIO_NOPULL;
505
  HAL_GPIO_Init(MEMS_INT2_GPIO_Port, &GPIO_InitStruct);
506
507
}
508
509
/* USER CODE BEGIN 4 */
510
511
/* USER CODE END 4 */
512
513
/**
514
  * @brief  This function is executed in case of error occurrence.
515
  * @param  None
516
  * @retval None
517
  */
518
void Error_Handler(void)
519
{
520
  /* USER CODE BEGIN Error_Handler */
521
  /* User can add his own implementation to report the HAL error return state */
522
  while(1) 
523
  {
524
  }
525
  /* USER CODE END Error_Handler */ 
526
}
527
528
#ifdef USE_FULL_ASSERT
529
530
/**
531
   * @brief Reports the name of the source file and the source line number
532
   * where the assert_param error has occurred.
533
   * @param file: pointer to the source file name
534
   * @param line: assert_param error line source number
535
   * @retval None
536
   */
537
void assert_failed(uint8_t* file, uint32_t line)
538
{
539
  /* USER CODE BEGIN 6 */
540
  /* User can add his own implementation to report the file name and line number,
541
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
542
  /* USER CODE END 6 */
543
544
}
545
546
#endif
547
548
/**
549
  * @}
550
  */ 
551
552
/**
553
  * @}
554
*/ 
555
556
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

von BK (Gast)


Lesenswert?

Vielen dank für die Hilfe.
Ich bin einen Schritt weiter. die Kommunikation via I2C funktioniert 
jetzt ohne Probleme.
Der Grund für den Fehler war das SysTick_Handler() nicht automatisch 
erstellt wurde. Ich habe diese Funktion jetzt selbst implementiert:
1
void SysTick_Handler(void){
2
  HAL_IncTick(); // increase Tick counter for HAL driver
3
};

Mein neues Programm intialisert den CS43L22 via I2C. So weit ich aber 
rausgefunden habe muss ich Etwas über I2S Übertragen damit der Beep 
Generator startet. Leider scheitert die Übertragung mit I2S und endet 
mit einem Timeout da die flag von __HAL_I2S_GET_FLAG auf Reset bleibt.

Ich habe den Code von Bülent C angeschaut. Soweit ich das gesehen hab 
nutze ich die gleichen Einstellungen. Nur bei den GPIO intialsiere ich 
weniger. Jedoch braucht man da auch nicht alle oder?

Anbei mein Code. Für Tips was ich noch falsch mache wäre ich sehr 
dankbar.
1
// main.c
2
3
#include "stm32f4xx.h"
4
#include "my_header.h"
5
6
7
8
int main(void){
9
  
10
  I2C_HandleTypeDef I2C_Params; // declare Parameter Handle
11
  I2S_HandleTypeDef I2S_Params; // declare Parameter Handle
12
  HAL_StatusTypeDef I2C_State; // declare State handle
13
  HAL_StatusTypeDef I2S_State; // declare State handle
14
  GPIO_InitTypeDef GPIOA_Params; // Declares the structure handle for the parameters of I2S on GPIOA
15
  GPIO_InitTypeDef GPIOD_Params; // Declares the structure handle for the parameters of the reset pin on GPIOE
16
  GPIO_InitTypeDef GPIOB_Params; // Declares the structure handle for the parameters of I2C on GPIOB
17
  GPIO_InitTypeDef GPIOC_Params; // Declares the structure handle for the parameters of I2S on GPIOC
18
  uint8_t send_data; // declare send data variable
19
  uint8_t send_data_array[2]; // declare send data variable
20
  uint8_t received_data; // declare received data variable
21
  RCC_ClkInitTypeDef RCC_ClkInitStruct; // configure System Clock
22
  uint16_t I2S_Data[10]; //
23
  
24
  I2S_Data[0] = 15000;
25
  I2S_Data[1] = 16498;
26
  I2S_Data[2] = 17980;
27
  I2S_Data[3] = 19433;
28
  I2S_Data[4] = 20841;
29
  I2S_Data[5] = 22191;
30
  I2S_Data[6] = 23470;
31
  I2S_Data[7] = 24663;
32
  I2S_Data[8] = 25760;
33
  I2S_Data[9] = 26750;
34
  
35
  HAL_Init(); // Initialise  Hal driver
36
  
37
  // configure the HCLK, PCLK1 and PCLK2 
38
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
39
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
40
  
41
  LED_Init(); // initialise LEDs
42
  Set_State_Rd_LED(1); // turn on red LED
43
  
44
  
45
  // Configure GPIOD for reset pin on CS43L22
46
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; //Enable the clock for GPIOD
47
  GPIOD_Params.Pin = GPIO_PIN_4; // Select pin 4
48
  GPIOD_Params.Mode = GPIO_MODE_OUTPUT_PP; //Selects normal output push-pull mode
49
  GPIOD_Params.Speed = GPIO_SPEED_FAST; //Selects fast speed
50
  GPIOD_Params.Pull = GPIO_PULLDOWN; //Selects pull-down activation
51
  HAL_GPIO_Init(GPIOD, &GPIOD_Params); // Sets GPIOD into the modes specified in GPIOE_Params
52
53
  
54
  // Configure GPIOB for SCL (PB6) and SDA (PB9) on I2C1
55
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; //Enable the clock for GPIOB
56
  GPIOB_Params.Pin = GPIO_PIN_6 | GPIO_PIN_9; // Selects pins 6 and 9
57
  GPIOB_Params.Alternate = GPIO_AF4_I2C1; //Selects alternate function (I2C1)
58
  GPIOB_Params.Mode = GPIO_MODE_AF_OD; //Selects alternate function open drain mode
59
  GPIOB_Params.Speed = GPIO_SPEED_FAST; //Selects fast speed
60
  //GPIOB_Params.Speed = GPIO_SPEED_FREQ_VERY_HIGH; //Selects fast speed
61
  GPIOB_Params.Pull = GPIO_NOPULL; //Selects no pull-up or pull-down activation
62
  //GPIOB_Params.Pull = GPIO_PULLUP; //Selects no pull-up activation
63
  HAL_GPIO_Init(GPIOB, &GPIOB_Params); // Sets GPIOB into the modes specified in GPIOA_Params
64
  
65
  
66
  RCC->APB1ENR |= RCC_APB1ENR_I2C1EN  ; // Enables the clock for I2C1
67
  RCC->APB1ENR |= RCC_APB1ENR_SPI3EN  ; // Enables the clock for SPI3 (I2S)
68
  
69
  
70
  // Configure GPIOC for 7(MCLK), 10(SCLK) and 12(SDIN) on I2S
71
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; //Enable the clock for GPIOB
72
  GPIOC_Params.Pin = GPIO_PIN_7 | GPIO_PIN_10 | GPIO_PIN_12; // Selects pins 7(MCLK), 10(SCLK) and 12(SDIN)
73
  GPIOC_Params.Alternate = GPIO_AF6_SPI3; //Selects alternate function (I2C1)
74
  GPIOC_Params.Mode = GPIO_MODE_AF_PP; //Selects alternate function open drain mode
75
  GPIOC_Params.Speed = GPIO_SPEED_FAST; //Selects fast speed
76
  GPIOC_Params.Pull = GPIO_NOPULL; //Selects no pull-up or pull-down activation
77
  HAL_GPIO_Init(GPIOC, &GPIOC_Params); // Sets GPIOB into the modes specified in GPIOA_Params
78
  
79
  // Configure GPIOA for 4(WS)on I2S
80
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; //Enable the clock for GPIOA
81
  GPIOA_Params.Pin = GPIO_PIN_4; // Selects pins 4(WS)
82
  GPIOA_Params.Alternate = GPIO_AF6_SPI3; //Selects alternate function (I2C1)
83
  GPIOC_Params.Mode = GPIO_MODE_AF_PP; //Selects alternate function open drain mode
84
  GPIOC_Params.Speed = GPIO_SPEED_FAST; //Selects fast speed
85
  GPIOC_Params.Pull = GPIO_NOPULL; //Selects no pull-up or pull-down activation
86
  HAL_GPIO_Init(GPIOA, &GPIOA_Params); // Sets GPIOA into the modes specified in GPIOA_Params
87
  
88
  
89
  // HAL_I2S_DeInit(&I2S_Params);
90
  I2S_Params.Instance = SPI3; 
91
  I2S_Params.Init.Mode = I2S_MODE_MASTER_TX;
92
  I2S_Params.Init.Standard = I2S_STANDARD_PHILIPS;
93
  I2S_Params.Init.DataFormat = I2S_DATAFORMAT_16B;
94
  I2S_Params.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
95
  I2S_Params.Init.AudioFreq = I2S_AUDIOFREQ_48K;
96
  // I2S_Params.Init.AudioFreq = I2S_AUDIOFREQ_8K;
97
  I2S_Params.Init.CPOL = I2S_CPOL_LOW;
98
  I2S_Params.Init.ClockSource = I2S_CLOCK_PLL;
99
  // I2S_Params.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
100
  I2S_Params.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE;
101
  HAL_I2S_Init(&I2S_Params);
102
  __HAL_I2S_ENABLE(&I2S_Params);
103
  
104
  // Configure I2C
105
  I2C_Params.Instance = I2C1;
106
  I2C_Params.Init.ClockSpeed = 100000; // Set clock speed to 50 kHz
107
  I2C_Params.Init.DutyCycle = I2C_DUTYCYCLE_2; // Set duty cycle
108
  I2C_Params.Init.OwnAddress1 = 0x33; // set own adress 1  
109
  // I2C_Params.Init.OwnAddress1 = 0x00; // set own adress 1  
110
  I2C_Params.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; // set to 7-Bit adresses
111
  I2C_Params.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; // Disabel dual address mode
112
  I2C_Params.Init.OwnAddress2 = 0; // set own adress 2
113
  I2C_Params.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; // General call disabled
114
  I2C_Params.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; // No Stretch disabled
115
  // I2C_Params.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE; // No Stretch enabled
116
  // I2C_Params.Mode = HAL_I2C_MODE_MASTER; // set I2C to master
117
  // HAL_I2C_MspInit(&I2C_Params); // configure I2C
118
  __HAL_I2C_ENABLE(&I2C_Params); // Enable the I2C1
119
  I2C_State = HAL_I2C_Init(&I2C_Params); // configure I2C
120
  
121
  
122
  GPIOD->BSRR |= GPIO_PIN_4; //Sets the reset pin of CS43L22 high
123
  
124
  HAL_Delay(1000); // wait 1s for startup of CS43L22 
125
  
126
  
127
  // Init prozedure from Datasheet of CS43L22 page 32
128
  send_data_array[0] = 0x00; // adress of register
129
  send_data_array[1] = 0x99; // value for register
130
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 10000); // set register
131
  
132
  send_data_array[0] = 0x47; // adress of register
133
  send_data_array[1] = 0x80; // value for register
134
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 10000); // set register
135
  
136
  send_data = 0x32; // adress of register to read
137
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, &send_data, 1, 1000); // Write Address to read
138
  I2C_State = HAL_I2C_Master_Receive(&I2C_Params, 0x95, &received_data, 1, 1000); // read register
139
  
140
  send_data_array[0] = 0x32; // adress of register
141
  send_data_array[1] = received_data | 0x80; // value for register => set MSB
142
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 10000); // set register
143
  
144
  send_data = 0x32; // adress of register to read
145
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, &send_data, 1, 1000); // Write Address to read
146
  I2C_State = HAL_I2C_Master_Receive(&I2C_Params, 0x95, &received_data, 1, 1000); // read register
147
  
148
  send_data_array[0] = 0x32; // adress of register
149
  send_data_array[1] = received_data & (~0x80); // value for register => reset MSB
150
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 10000); // set register
151
  
152
  send_data_array[0] = 0x00; // adress of register
153
  send_data_array[1] = 0x00; // value for register
154
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 10000); // set register
155
  
156
  //end of initialization sequence
157
158
  send_data_array[0] = 0x1E; // address of Beep register
159
  send_data_array[1] = 0xC0; // value for Beep register (contious beep)
160
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write Beep register
161
  
162
  send_data_array[0] = CODEC_MAP_PWR_CTRL2; // adress of register to write
163
  send_data_array[1] = 0xAF; // value for register
164
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
165
  
166
  send_data_array[0] = CODEC_MAP_PLAYBACK_CTRL1; // adress of register to write
167
  send_data_array[1] = 0x70; // value for register
168
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
169
  
170
  send_data_array[0] = CODEC_MAP_CLK_CTRL; // adress of register to write
171
  send_data_array[1] = 0x81; // value for register => automatic detection of Clock
172
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
173
  
174
  send_data_array[0] = CODEC_MAP_IF_CTRL1; // adress of register to write
175
  send_data_array[1] = 0x07; // value for register
176
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
177
178
  send_data_array[0] = CODEC_MAP_ANALOG_SET; // adress of register to write
179
  send_data_array[1] = 0x00; // value for register => analog settings
180
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
181
  
182
  send_data_array[0] = CODEC_MAP_LIMIT_CTRL1; // adress of register to write
183
  send_data_array[1] = 0x00; // value for register
184
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
185
186
  send_data_array[0] = CODEC_MAP_PCMA_VOL; // adress of register to write
187
  send_data_array[1] = 0x0A; // value for register
188
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
189
190
  send_data_array[0] = CODEC_MAP_PCMB_VOL; // adress of register to write
191
  send_data_array[1] = 0x0A; // value for register
192
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
193
194
  send_data_array[0] = CODEC_MAP_TONE_CTRL; // adress of register to write
195
  send_data_array[1] = 0x0F; // value for register
196
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write power register 1
197
  
198
  /*
199
  send_data_array[0] = CODEC_MAP_BEEP_TONE_CFG; // adress of register to write
200
  send_data_array[1] = 0xC0; // value for register => continous beep
201
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write register
202
  */
203
  
204
  send_data_array[0] = CODEC_MAP_PWR_CTRL1; // adress of register to write
205
  send_data_array[1] = 0x9E; // value for register => power up
206
  I2C_State = HAL_I2C_Master_Transmit(&I2C_Params, 0x94, send_data_array, 2, 1000); // write register
207
  
208
  // GPIOD->BSRR |= (GPIO_PIN_4+16); // Sets the reset pin of CS43L22 low
209
  
210
  
211
  while(1){  
212
    I2S_State = HAL_I2S_Transmit(&I2S_Params, I2S_Data, 10, 1000);
213
    Set_State_Or_LED(1); // turn on orange LED
214
  }  
215
  
216
};

von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

Du musst noch die I2S PLL initialisieren.

von BK (Gast)


Angehängte Dateien:

Lesenswert?

Nach vielen Versuchen habe ich den Chip dazu gebracht Geräusche 
auszugeben. Jedoch gibt er keine sauberen Beep-Signale aus sondern eher 
Rauschen. Und beim ersten Ansteuern dauert es einige Sekunden bis ein 
Geräusch erzeugt wird. Anbei meine finale Version einer Ampelsteuerung 
im RTOS für Keil µVision. Hoffe es hilft jemandem.

von dasrotemopped (Gast)


Lesenswert?

das mit dem Quellcode posten geht auch kompakter:
http://www.dasrotemopped.de/dateien/STM32F407VGT-DISCO_BSP_SSD1963_20170417.zip

So macht ST das Initialisieren der Komponenten,
von mir in ein SW4STM32 Projekt gepackt, wen es interessiert.

Gruß,
dasrotemopped.

von pitschu (Gast)


Lesenswert?

An dem Ding habe ich auch lange rumgerätselt. Entscheidend war, dass der 
Chip immer einen Masterclkock braucht, selbst dann wenn er keinen Ton 
von sich geben soll. Ansonsten kommt Rauchen, Zirpen und Knacken. Auch 
der Beep-Generator benötigt den Clock. Auf dem STM4discovery, welches 
ich nutze, gebe ich dauerhaft eine leere DMA- Dauerschleife an das 
WAV-Interface aus, wenn Ruhe sein soll. Seit dem funktionieren sowohl 
WAV-Files als auch Beeps vollkommen OK.

pitschu

von chris (Gast)


Angehängte Dateien:

Lesenswert?

Auf dem STM32F4 Discovery ist das Mikrofon und DAC1 direkt an den Mixer 
des CS43L22 angeschlossen.
Als einfachster Test, um überhaupt einmal einen Ton zu bekommen, habe 
ich mal versucht, am DAC1 Anschluss ( PA4 ) ein Rechtecksignal zu 
erzeugen das Routing des CS43L22 so einzustellen, dass es durchkommt.

Leider hat es nicht funktioniert. Hat jemand eine Idee, wie man die 
Register einstellen muss?

von chris (Gast)


Lesenswert?

Hier hat sich zumindest schon mal jemand mit einem ähnlichen Problem 
herum geschlagen:

Beitrag "STM32F4 Discovery audio passtrough (MP45DT02 --> CS43L22)"

von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

BK schrieb:
> Und beim ersten Ansteuern dauert es einige Sekunden bis ein
> Geräusch erzeugt wird.

Das scheint immer so zu sein. Und obwohl ich mit dem Mixer noch nicht 
rumgespielt habe, erscheint es logisch, das zumindest die Masterclock 
anliegen muss, die intern anscheinend immer gebraucht wird.
Denkt dran, ohne die undokumentierten I2C Kommandos geht nix so richtig. 
Siehe in meinem  Beitrag von oben - dank Uwe B. und seiner Library.
Beitrag "Re: STM32F4 Discovery Audio CS43L22 I2C Problem"

Übrigens hatte SPL einen kleinen Bug und hat die Masterclock trotz 
richtigem Parameter nicht ausgegeben. Es lohnt sich evtl., HAL mal 
darauf abzuklopfen und auch mal zu messen, ob sie nun anliegt.

: Bearbeitet durch User
von Markus (Gast)


Lesenswert?

Danke, dass Du den Thread etwas weiter führst. Der Chip ist wirklich 
nicht einfach in den Griff zu kriegen.

von V12Vinnie (Gast)


Lesenswert?

Mich hat es jedefalls geholfen! Ich hatte bis jetzt noch keinen einzigen 
ton aus meinem CS43L22 gehört, mit deinem Projekt bin ich selber 
mindestens einen Schritt weiter. Ich werde jetzt probieren, mich nochmal 
mit diesem Beep-Generator auseinanderzusetzen.

Was aber schon zu bemerken ist, falls es noch jemanden gibt der sucht: 
ich musste in system_stm32f4xx.c dieses noch reinschreiben
1
const uint8_t APBPrescTable[8]  = {0, 0, 0, 0, 1, 2, 3, 4};

Sonst hat das ganze sich nicht kompiliert (ich hoffe, das sagt mann so 
auf Deutsch, es ist nicht meine muttersprache).

Ich hab's auf line 140, da da auch AHBPrescTable definiert wird, aber 
keine ahnung ob dass dahin gehört, gibt es jemanden hier der das besser 
erklären könnte?

von V12Vinnie (Gast)


Lesenswert?

BK schrieb:
> Nach vielen Versuchen habe ich den Chip dazu gebracht Geräusche
> auszugeben. Jedoch gibt er keine sauberen Beep-Signale aus sondern eher
> Rauschen. Und beim ersten Ansteuern dauert es einige Sekunden bis ein
> Geräusch erzeugt wird. Anbei meine finale Version einer Ampelsteuerung
> im RTOS für Keil µVision. Hoffe es hilft jemandem.

Meinen letzten Kommentar war als Antwort auf diesen Beitrag, das habe 
ich das erste mal irgendwie nicht hinbekommen.

von Test (Gast)


Lesenswert?

pitschu schrieb:
> Entscheidend war, dass der
> Chip immer einen Masterclkock braucht, selbst dann wenn er keinen Ton
> von sich geben soll. Ansonsten kommt Rauchen, Zirpen und Knacken. Auch
> der Beep-Generator benötigt den Clock.

Erscheint ja auch prinzipiell logisch, das Ding hat ja keine internen 
Taktgenerator und auch extern keinen Quarz o.Ä. Und ganz ohne Takt ist 
die Generierung von Audio irgendwie kompliziert (auch ein einfaches 
Beep). Nur wirklich erwähnt wird das nicht im Datenblatt ...

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.