Simple Exmple
Dependencies: ST_401_84MHZ mbed
http://www.geocities.jp/micro_diys/
Revision 1:530d6fb59a93, committed 2014-05-24
- Comitter:
- p_igmon
- Date:
- Sat May 24 12:07:17 2014 +0000
- Parent:
- 0:0093b4cc5297
- Commit message:
- Nucleo F401RE I2S Transfer Example.
; DMA Dubble buffer mode.
; IRQ not yet.
Changed in this revision
diff -r 0093b4cc5297 -r 530d6fb59a93 ST_401_84MHZ.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ST_401_84MHZ.lib Sat May 24 12:07:17 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/dreschpe/code/ST_401_84MHZ/#b9343c8b85ec
diff -r 0093b4cc5297 -r 530d6fb59a93 main.cpp --- a/main.cpp Sat May 17 04:58:58 2014 +0000 +++ b/main.cpp Sat May 24 12:07:17 2014 +0000 @@ -1,51 +1,65 @@ +/* + Nucleo F401RE Board + DMA Transmit sample + + I2S Master Transmit (Enable MCK out) + Phillips Format 16bit + Dubble DMA Buffer + + + + +*/ + #include "mbed.h" #include <math.h> #include "cmsis.h" #include "pinmap.h" #include "PinNames.h" #include "error.h" +#include "stm32f401xe.h" #include "stm32f4xx.h" #include "stm32f4xx_hal.h" #include "stm32f4xx_hal_dma_ex.h" +#include "stm32f4xx_hal_dma.h" #include "stm32f4xx_hal_i2s.h" #include "sine_wave.h" #include "saw_wave.h" +#include "ST_F401_84MHZ.h" Serial pc(SERIAL_TX, SERIAL_RX); - DigitalOut myled(LED1); -#define I2S_BUFFERSIZE 256 -uint16_t TxBuff[I2S_BUFFERSIZE]; -uint32_t dmabuffer[2][256]; -//#define STM_PIN_DATA(MODE, FUNC) (((MODE) << 8) | (FUNC)) +#define I2S_BUFFERSIZE 256 +#define DMA_TRANSFERCOUNT (I2S_BUFFERSIZE<<1) +int16_t dmabuffer[2][DMA_TRANSFERCOUNT];//DMA Double Buffer static const PinMap PinMap_I2S_MCK[] = { {PC_6, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, - {PC_7, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)}, +// {PC_7, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)}, {NC, NC, 0} }; static const PinMap PinMap_I2S_CK[] = { {PB_10, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, {PB_13, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, - {PD_3, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, - {PC_10, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)}, +// {PD_3, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, +// {PC_10, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)}, {NC, NC, 0} }; static const PinMap PinMap_I2S_WS[] = { {PB_12, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, {PB_9, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, - {PA_15, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)}, +// {PA_15, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)}, {NC, NC, 0} }; static const PinMap PinMap_I2S_SD[] = { {PC_3, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, {PB_15, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, - {PC_12, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)}, - {PD_6, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI3)}, +// {PC_12, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)}, +// {PD_6, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI3)}, {NC, NC, 0} }; @@ -55,11 +69,11 @@ DMA_MEMORY_TO_PERIPH, DMA_PINC_DISABLE, DMA_MINC_ENABLE, - DMA_PDATAALIGN_WORD, + DMA_PDATAALIGN_HALFWORD, DMA_MDATAALIGN_WORD, - DMA_CIRCULAR, + DMA_CIRCULAR,//DMA_PFCTRL,// DMA_PRIORITY_HIGH, - DMA_FIFOMODE_ENABLE, + DMA_FIFOMODE_DISABLE,//DMA_FIFOMODE_ENABLE, DMA_FIFO_THRESHOLD_HALFFULL, DMA_MBURST_SINGLE, DMA_PBURST_SINGLE @@ -69,7 +83,7 @@ DMA1_Stream4, DMA_InitType, HAL_UNLOCKED, - HAL_DMA_STATE_RESET, + HAL_DMA_STATE_RESET,//HAL_DMA_STATE_READY NULL, NULL, NULL, @@ -80,7 +94,7 @@ I2S_InitTypeDef my_I2S_InitType ={ I2S_MODE_MASTER_TX, - I2S_STANDARD_MSB, + I2S_STANDARD_PHILLIPS, I2S_DATAFORMAT_16B, I2S_MCLKOUTPUT_ENABLE, I2S_AUDIOFREQ_44K, @@ -100,7 +114,7 @@ I2S_HandleTypeDef my_I2S_HandleTypeDef = { SPI2, my_I2S_InitType, - &TxBuff[0], + (uint16_t*)&dmabuffer[0][0], I2S_BUFFERSIZE, NULL, NULL, @@ -114,40 +128,65 @@ }; void init_dmabuffer(void){ - uint32_t temp; - for (int i =0;i<256;i++){ - temp = (uint32_t)((saw_wave[i]<<16) | sine_wave[i]); - dmabuffer[0][i] = temp; - dmabuffer[1][i] = temp; + for (int i =0;i < I2S_BUFFERSIZE;i++){ + dmabuffer[0][i*2] = sine_wave[i];// 1st Buffer Lch + dmabuffer[0][i*2+1] = saw_wave[i];// 1nd Buffer Rch + dmabuffer[1][i*2] = sine_wave[i];// 2nd Buffer Lch + dmabuffer[1][i*2+1] = saw_wave[i];// 2nd Buffer Rch } } + +#define PLLI2S_N 271 +#define PLLI2S_R 2 int main() { - pc.printf("Hello World !\n"); + F401_init84 myinit(0); + pc.baud(9600); + pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); init_dmabuffer(); - /// I2S_init(); - HAL_I2S_Init(&my_I2S_HandleTypeDef); - // Configure the I2S pins - pinmap_pinout(PC_6, PinMap_MCK); - pinmap_pinout(PB_10, PinMap_CK); - pinmap_pinout(PB_12, PinMap_WS); - pinmap_pinout(PC_3, PinMap_SD); + + /* Configure the I2S PLL */ + RCC->PLLI2SCFGR = (PLLI2S_N << 6) | (PLLI2S_R << 28); + /* Enable the I2S PLL */ + RCC->CR |= RCC_CR_PLLI2SON; + /* Wait until the I2S PLL is ready */ + while (!(RCC->CR & RCC_CR_PLLI2SRDY)); + + __SPI2_CLK_ENABLE(); + __DMA1_CLK_ENABLE(); + + pinmap_pinout(PC_6, PinMap_I2S_MCK); + pinmap_pinout(PB_13, PinMap_I2S_CK); + pinmap_pinout(PB_12, PinMap_I2S_WS); + pinmap_pinout(PB_15, PinMap_I2S_SD); + pin_mode(PC_6, PullUp); - pin_mode(PB_10, PullUp); + pin_mode(PB_13, PullUp); pin_mode(PB_12, PullUp); - pin_mode(PC_3, PullUp); + pin_mode(PB_15, PullUp); + + if(HAL_DMA_Init(&DMA_HandleType)!= HAL_OK) pc.printf("Eror in HAL_DMA_Init \n\r"); + + if (HAL_I2S_Init(&my_I2S_HandleTypeDef)!= HAL_OK) pc.printf("Eror in HAL_I2S_Init \n\r"); + + volatile uint32_t *I2S_DR = &SPI2->DR; + if(HAL_DMAEx_MultiBufferStart_IT(&DMA_HandleType ,(uint32_t)&dmabuffer[0][0] ,(uint32_t )I2S_DR ,(uint32_t)&dmabuffer[1][0] ,DMA_TRANSFERCOUNT)!=HAL_OK) pc.printf("Eror in HAL_DMAEx_MultiBufferStart_IT \n\r"); - // HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s); - // HAL_I2S_IRQHandler(&my_I2S_HandleType); -/// HAL_I2S_Transmit_DMA(&my_I2S_HandleTypeDef, &TxBuff[0], I2S_BUFFERSIZE); - - HAL_DMA_Init(&DMA_HandleType); - HAL_DMAEx_MultiBufferStart_IT(&DMA_HandleType ,(uint32_t)&dmabuffer[0][0] ,SPI2->DR ,(uint32_t)&dmabuffer[1][0] ,256); - //HAL_DMA_IRQHandler(&my_DMA_HamdleTypeDef); - //HAL_DMA_Start_IT(&my_DMA_HamdleTypeDef,NULL,NULL,256); + SPI2->I2SCFGR |= SPI_I2SCFGR_I2SE; + /* Enable Tx DMA Request */ + SPI2->CR2 |= SPI_CR2_TXDMAEN; + pc.printf("DMA M0AR =%4x M1AR =%4x PAR = %4x \n\r" ,DMA1_Stream4->M0AR ,DMA1_Stream4->M1AR ,DMA1_Stream4->PAR); + pc.printf("I2S CR2 = %4x \n\r" ,SPI2->CR2); + pc.printf("I2S SR = %4x \n\r" ,SPI2->SR); + pc.printf("I2S I2SCFGR = %4x \n\r" ,SPI2->I2SCFGR); + pc.printf("I2S I2SPR = %4x \n\r" ,SPI2->I2SPR); + pc.printf("RCC->PLLI2SCFGR = %4x \n\r" ,RCC->PLLI2SCFGR); + + HAL_RCC_MCOConfig(uint32_t RCC_MCO2, RCC_MCO2SOURCE_PLLI2SCLK, RCC_MCODIV_5); + while(1) { - myled = !myled; + wait_ms(500); } } \ No newline at end of file
diff -r 0093b4cc5297 -r 530d6fb59a93 mbed.bld --- a/mbed.bld Sat May 17 04:58:58 2014 +0000 +++ b/mbed.bld Sat May 24 12:07:17 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/ed8466a608b4 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877 \ No newline at end of file