From d3881c1ed1156c7ec53598986a2c42418bf898ea Mon Sep 17 00:00:00 2001 From: Michael Wiebusch Date: Wed, 27 Nov 2013 11:30:23 +0100 Subject: [PATCH] gererating spi ram test data and sending it with dma --- china_devel_board/test001/src/main.c | 354 +++++++------------ china_devel_board/test001/src/stm32f10x_it.c | 17 +- china_devel_board/test001/src/stm32f10x_it.h | 4 + 3 files changed, 146 insertions(+), 229 deletions(-) diff --git a/china_devel_board/test001/src/main.c b/china_devel_board/test001/src/main.c index e4ec6e0..2d379d0 100644 --- a/china_devel_board/test001/src/main.c +++ b/china_devel_board/test001/src/main.c @@ -7,44 +7,32 @@ */ #include "stm32f10x.h" -// #include "stm32f10x_rcc.h" -// #include "stm32f10x_gpio.h" -// #include "stm32f10x_usart.h" -// #include "stm32f10x_tim.h" -#include "stm32f10x_conf.h" -// #include "serial.h" +#include "stm32f10x_conf.h" // contains all std periph includes #include "core_cm3.h" -// #include "spi.h" #include "stm32f10x_it.h" #include "stm32_eval.h" #include #include "newlib_stubs.c" -#include "spi_conf.h" GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; SPI_InitTypeDef SPI_InitStructure; USART_InitTypeDef USART_InitStructure; +DMA_InitTypeDef DMA_InitStructure; +uint16_t SPIBuffer[] = {0xAAAA, 0xAAAA, 0xAAAA}; -// /* Private function prototypes -----------------------------------------------*/ -// #ifdef __GNUC__ -// /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf -// set to 'Yes') calls __io_putchar() */ -// #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) -// #else -// #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) -// #endif /* __GNUC__ */ -void diller_init_spi1(void); + +// prototypes for functions that are included in main.c void SysTick_Handler(void); void disable_JTAG(void); -void RCC_Configuration(void); +void init_SPI2(void); -void GPIO_Configuration(uint16_t SPIy_Mode, uint16_t SPIz_Mode); +void spi_dma_shovel(void); // MAIN --------------------------------------------------------------------- @@ -62,14 +50,11 @@ int main(int argc, char *argv[]) { // uinit(31250); // midi baudrate // configure timer interval - SysTick_Config(16777215); // standard -// SysTick_Config(7200000); // 10 Hz interrupt -// SysTick_Config(720000); // 100 hz -// SysTick_Config(72000); // 1000 hz +// SysTick_Config(16777215); // standard +// SysTick_Config(7200000); // 10 Hz interrupt +// SysTick_Config(720000); // 100 hz + SysTick_Config(72000); // 1000 hz -// comm_puts("uart initialized\r\n"); - - // UART init the StdPeriphLib way @@ -85,50 +70,6 @@ int main(int argc, char *argv[]) { STM_EVAL_COMInit(COM1, &USART_InitStructure); STM_EVAL_COMInit(COM2, &USART_InitStructure); -// makes the uC freeze sadly ... -// printf("\n\r This has been printed by printf!\n\r"); - - - /* System clocks configuration ---------------------------------------------*/ - RCC_Configuration(); - - /* 1st phase: SPIy Master and SPIz Slave */ - /* GPIO configuration ------------------------------------------------------*/ - GPIO_Configuration(SPI_Mode_Master, SPI_Mode_Master); - - - /* SPIy Config -------------------------------------------------------------*/ - SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; - SPI_InitStructure.SPI_Mode = SPI_Mode_Master; - SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; - SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; - SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; -// SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; - SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; - SPI_InitStructure.SPI_CRCPolynomial = 7; - SPI_Init(SPIy, &SPI_InitStructure); - - /* SPIz Config -------------------------------------------------------------*/ -// SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; - // both master - SPI_Init(SPIz, &SPI_InitStructure); - - /* Enable SPIy */ - SPI_Cmd(SPIy, ENABLE); - /* Enable SPIz */ - SPI_Cmd(SPIz, ENABLE); - - - - -// diller_init_spi1(); - - -// spi_create(SPI1, GPIOA, GPIO_Pin_4); // (which spi?, which port, which softCS pin?) -//spi_writeTwoBytes(0x12, 0x34); - // try the LED gpio stuff from the evaluation boards @@ -146,27 +87,125 @@ int main(int argc, char *argv[]) { USART_SendData(EVAL_COM1, (uint8_t) 'B'); USART_SendData(EVAL_COM1, (uint8_t) 'C'); USART_SendData(EVAL_COM2, (uint8_t) 'B'); + + init_SPI2(); + SPIBuffer[0] = 0x1234; + SPIBuffer[1] = 0x5678; + - return 0; - - -} - - - + + + return 0; +} +void spi_dma_shovel(void) { + + DMA_Cmd(DMA1_Channel4, DISABLE); + DMA_Cmd(DMA1_Channel5, DISABLE); + DMA_SetCurrDataCounter(DMA1_Channel4, 2); + DMA_SetCurrDataCounter(DMA1_Channel5, 2); + + // Chip Select Low + GPIO_WriteBit(GPIOB, GPIO_Pin_12, RESET); + + DMA_Cmd(DMA1_Channel4, ENABLE); + DMA_Cmd(DMA1_Channel5, ENABLE); +} +void init_SPI2(void) { + // configure the spi with DMA + // code example from dillertech.de + + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOB, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOB, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOB, &GPIO_InitStructure); + + GPIO_WriteBit(GPIOB, GPIO_Pin_12, SET); + +// SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; + SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; + SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; + SPI_InitStructure.SPI_CRCPolynomial = 0; + SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; + SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; + SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; + SPI_InitStructure.SPI_Mode = SPI_Mode_Master; + SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; + SPI_Init(SPI2, &SPI_InitStructure); + + SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE); + SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE); + + SPI_Cmd(SPI2, ENABLE); + + // DMA Channel 4 - SPI RX + DMA_InitStructure.DMA_BufferSize = 0; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPIBuffer; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_Init(DMA1_Channel4, &DMA_InitStructure); + + DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); + + // DMA Channel 5 - SPI TX + DMA_InitStructure.DMA_BufferSize = 0; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPIBuffer; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_Init(DMA1_Channel5, &DMA_InitStructure); + + NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + + + + + +} @@ -184,11 +223,7 @@ void SysTick_Handler(void) STM_EVAL_LEDToggle(LED3); STM_EVAL_LEDToggle(LED4); - // send spi test data - - - SPI_I2S_SendData(SPIy, 0b10100101); - SPI_I2S_SendData(SPIz, 0b10100101); + uint8_t i; for( i = 0; i<10; i++){ USART_SendData(EVAL_COM1, character); @@ -201,6 +236,17 @@ void SysTick_Handler(void) if (character > 'Z') { character = 'A'; } + + static uint8_t spi_addr = 0; + + + + SPIBuffer[0] = (uint16_t) spi_addr; // address + SPIBuffer[1] = (uint16_t) spi_addr; // data content +// SPIBuffer[1] = 0x5678; // data content + spi_dma_shovel(); + + spi_addr++; } @@ -218,147 +264,3 @@ void disable_JTAG(void) { - - -void diller_init_spi1(void) { - - - -RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1, ENABLE); - -GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; -GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; -GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; -GPIO_Init(GPIOA, &GPIO_InitStructure); - -GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; -GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; -GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; -GPIO_Init(GPIOA, &GPIO_InitStructure); - -GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; -GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; -GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; -GPIO_Init(GPIOA, &GPIO_InitStructure); - -GPIO_WriteBit(GPIOA, GPIO_Pin_4, SET); - -NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn; -NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; -NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; -NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; -NVIC_Init(&NVIC_InitStructure); - -// SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; -SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; -SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; -SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; -SPI_InitStructure.SPI_CRCPolynomial = 0; -SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; -SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; -SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; -SPI_InitStructure.SPI_Mode = SPI_Mode_Master; -SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; -SPI_Init(SPI1, &SPI_InitStructure); - -SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE); - -SPI_Cmd(SPI1, ENABLE); - - - - -} - -void RCC_Configuration(void) -{ - /* PCLK2 = HCLK/2 */ -// RCC_PCLK2Config(RCC_HCLK_Div2); // bothers USART1, if deactivated makes spi faster - -/* Enable peripheral clocks --------------------------------------------------*/ -// #ifdef USE_STM3210C_EVAL -// /* Enable GPIO clock for SPIy and SPIz */ -// RCC_APB2PeriphClockCmd(SPIy_GPIO_CLK | SPIz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE); -// -// /* Enable SPIy Periph clock */ -// RCC_APB1PeriphClockCmd(SPIy_CLK, ENABLE); - -// #else - /* Enable SPIy clock and GPIO clock for SPIy and SPIz */ - RCC_APB2PeriphClockCmd(SPIy_GPIO_CLK | SPIz_GPIO_CLK | SPIy_CLK, ENABLE); -// #endif - /* Enable SPIz Periph clock */ - RCC_APB1PeriphClockCmd(SPIz_CLK, ENABLE); -} - - - -void GPIO_Configuration(uint16_t SPIy_Mode, uint16_t SPIz_Mode) -{ - GPIO_InitTypeDef GPIO_InitStructure; - -// #ifdef USE_STM3210C_EVAL -// /* Enable SPI3 Pins Software Remapping */ -// GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE); -// #endif - - // if SPI1 be remapped -// GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE); - - /* Configure SPIy pins: SCK, MISO and MOSI ---------------------------------*/ - GPIO_InitStructure.GPIO_Pin = SPIy_PIN_SCK | SPIy_PIN_MOSI; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - - if(SPIy_Mode == SPI_Mode_Master) - { - /* Configure SCK and MOSI pins as Alternate Function Push Pull */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - } - else - { - /* Configure SCK and MOSI pins as Input Floating */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - } - GPIO_Init(SPIy_GPIO, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = SPIy_PIN_MISO; - - if(SPIy_Mode == SPI_Mode_Master) - { - /* Configure MISO pin as Input Floating */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - } - else - { - /* Configure MISO pin as Alternate Function Push Pull */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - } - GPIO_Init(SPIy_GPIO, &GPIO_InitStructure); - - /* Configure SPIz pins: SCK, MISO and MOSI ---------------------------------*/ - GPIO_InitStructure.GPIO_Pin = SPIz_PIN_SCK | SPIz_PIN_MOSI; - - if(SPIz_Mode == SPI_Mode_Slave) - { - /* Configure SCK and MOSI pins as Input Floating */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - } - else - { - /* Configure SCK and MOSI pins as Alternate Function Push Pull */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - } - GPIO_Init(SPIz_GPIO, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = SPIz_PIN_MISO; - if(SPIz_Mode == SPI_Mode_Slave) - { - /* Configure MISO pin as Alternate Function Push Pull */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - } - else - { /* Configure MISO pin as Input Floating */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - } - GPIO_Init(SPIz_GPIO, &GPIO_InitStructure); -} \ No newline at end of file diff --git a/china_devel_board/test001/src/stm32f10x_it.c b/china_devel_board/test001/src/stm32f10x_it.c index 86927a9..114db16 100644 --- a/china_devel_board/test001/src/stm32f10x_it.c +++ b/china_devel_board/test001/src/stm32f10x_it.c @@ -27,7 +27,7 @@ /* custom includes for this project */ // #include "serial.h" -#include "spi.h" +// #include "spi.h" /** @addtogroup STM32F10x_StdPeriph_Template @@ -49,12 +49,23 @@ /* from diller-tech */ -void SPI1_IRQHandler(void){ - spi_handleSPI1Interrupt(); +// void SPI1_IRQHandler(void){ +// spi_handleSPI1Interrupt(); +// } + + + +void spi_handleDMA1Ch4Interrupt(void){ + // Chip Select High + GPIO_WriteBit(GPIOB, GPIO_Pin_12, SET); } +void DMA1_Channel4_IRQHandler(void){ + spi_handleDMA1Ch4Interrupt(); + DMA_ClearFlag(DMA1_FLAG_TC4); +} diff --git a/china_devel_board/test001/src/stm32f10x_it.h b/china_devel_board/test001/src/stm32f10x_it.h index 9935cf2..adb1f50 100644 --- a/china_devel_board/test001/src/stm32f10x_it.h +++ b/china_devel_board/test001/src/stm32f10x_it.h @@ -45,6 +45,10 @@ void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); +//for spi with dma +void spi_handleDMA1Ch4Interrupt(void); + + #ifdef __cplusplus } #endif -- 2.43.0