1 | /* Definitions for the I2S2 ADC Input Port
|
2 | * for Port C from setup.h */
|
3 | #define I2S2_MCLK_PIN GPIO_Pin_6
|
4 | #define I2S2_MCLK_SOURCE GPIO_PinSource6
|
5 | #define I2S2_MCLK_PORT GPIOC
|
6 | /* for Port B */
|
7 | #define I2S2_WS_PIN GPIO_Pin_12
|
8 | #define I2S2_WS_SOURCE GPIO_PinSource12
|
9 | #define I2S2_SCK_PIN GPIO_Pin_13
|
10 | #define I2S2_SCK_SOURCE GPIO_PinSource13
|
11 | #define I2S2_SD_PIN GPIO_Pin_15
|
12 | #define I2S2_SD_SOURCE GPIO_PinSource15
|
13 | #define I2S2_DATA_GPIO_PORT GPIOB
|
14 |
|
15 | #define I2SDIV 0x0103; // Korrekt value for 48kHz * 256 MCLK
|
16 | // now in main.c
|
17 | void MS_I2S2_Init(void) {
|
18 | /* Setup the ADC Input I2S2 for 48 kHz */
|
19 | GPIO_InitTypeDef GPIO_InitStructure;
|
20 | I2S_InitTypeDef I2S_InitStructure;
|
21 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
22 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
23 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
|
24 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
|
25 | SPI_I2S_DeInit(SPI2);
|
26 | /* Connect pins to I2S peripheral */
|
27 | GPIO_PinAFConfig(I2S2_DATA_GPIO_PORT, I2S2_SCK_SOURCE, GPIO_AF_SPI2);
|
28 | GPIO_PinAFConfig(I2S2_DATA_GPIO_PORT, I2S2_SD_SOURCE, GPIO_AF_SPI2);
|
29 | GPIO_PinAFConfig(I2S2_DATA_GPIO_PORT, I2S2_WS_SOURCE, GPIO_AF_SPI2);
|
30 | GPIO_PinAFConfig(I2S2_MCLK_PORT, I2S2_MCLK_SOURCE, GPIO_AF_SPI2);
|
31 | /* Configure I2S Pins */
|
32 | /* CODEC_I2S2 pins configuration: WS, SCK and SD pins */
|
33 | GPIO_InitStructure.GPIO_Pin = I2S2_WS_PIN | I2S2_SCK_PIN;
|
34 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
35 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
36 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
37 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
38 | GPIO_Init(I2S2_DATA_GPIO_PORT, &GPIO_InitStructure);
|
39 | /* Configure MCLK on diff. Port */
|
40 | GPIO_InitStructure.GPIO_Pin = I2S2_MCLK_PIN ;
|
41 | GPIO_Init(I2S2_MCLK_PORT, &GPIO_InitStructure);
|
42 | /* Configure SD as Input */
|
43 | GPIO_InitStructure.GPIO_Pin = I2S2_SD_PIN;
|
44 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
45 | GPIO_Init(I2S2_DATA_GPIO_PORT, &GPIO_InitStructure);
|
46 | SPI_I2S_DeInit(SPI2);
|
47 | /* now set the parameters for the I2S Interface itself */
|
48 | I2S_StructInit(&I2S_InitStructure);
|
49 | I2S_InitStructure.I2S_Mode = I2S_Mode_MasterRx;
|
50 | I2S_InitStructure.I2S_Standard = I2S_STANDARD;
|
51 | I2S_InitStructure.I2S_MCLKOutput = ENABLE;
|
52 | I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_48k;
|
53 | I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
|
54 | I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
|
55 | I2S_Init(SPI2,&I2S_InitStructure);
|
56 | // set the MCLK Output enable bit and prescaler , cause the initfunction is bugged
|
57 | SPI2->I2SPR = I2S_MCLKOutput_Enable | I2SDIV;
|
58 | // Configure the I2S Rx not empty IRQ
|
59 | NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
|
60 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0b;
|
61 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
62 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
63 | NVIC_Init(&NVIC_InitStructure);
|
64 | SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE,ENABLE);
|
65 | I2S_Cmd(SPI2,ENABLE);
|
66 | }
|