From a1161d56343b9c91f1f5659f4ca4a06c48c4078a Mon Sep 17 00:00:00 2001 From: Michael Wiebusch Date: Mon, 16 Dec 2013 19:58:09 +0100 Subject: [PATCH] progress ... interrupt based uart receiver working, can control first switches from fpga --- firmware/src/main.c | 275 +++++++++++++++++++++++++++++------- firmware/src/periph_conf.c | 23 ++- firmware/src/periph_conf.h | 7 +- firmware/src/stm32f10x_it.c | 4 +- 4 files changed, 249 insertions(+), 60 deletions(-) diff --git a/firmware/src/main.c b/firmware/src/main.c index fde842d..4fc5aa7 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -10,11 +10,12 @@ #include "stm32f10x_conf.h" // contains all std periph includes #include "core_cm3.h" #include "stm32f10x_it.h" -#include "stm32_eval.h" +#include "stm32_eval.h" // remove this file !!! #include #include "newlib_stubs.c" -#define UC_NO_REGS 14 + +#define UC_NO_REGS 17 GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; @@ -38,6 +39,14 @@ void spi_dma_shovel(void); void init_CB_GPIO_Outputs(void); +/* function prototypes */ +void RCC_Configuration(void); +void GPIO_Configuration(void); +void NVIC_Configuration(void); +void USART1_IRQHandler(void); +void init_USART1_w_interrupt(void); +void decode_uC_reg(uint8_t addr); + // MAIN --------------------------------------------------------------------- int main(int argc, char *argv[]) { @@ -57,28 +66,28 @@ int main(int argc, char *argv[]) { // UART init the StdPeriphLib way - USART_InitStructure.USART_BaudRate = 9600; -// USART_InitStructure.USART_BaudRate = 19200; - USART_InitStructure.USART_WordLength = USART_WordLength_8b; - USART_InitStructure.USART_StopBits = USART_StopBits_1; - USART_InitStructure.USART_Parity = USART_Parity_No; - USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; - USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; - - STM_EVAL_COMInit(COM1, &USART_InitStructure); -// STM_EVAL_COMInit(COM2, &USART_InitStructure); - - USART_SendData(EVAL_COM1, (uint8_t) 0); - USART_SendData(EVAL_COM1, (uint8_t) 1); - USART_SendData(EVAL_COM1, (uint8_t) 2); -// USART_SendData(EVAL_COM2, (uint8_t) 'B'); +// USART_InitStructure.USART_BaudRate = 9600; +// // USART_InitStructure.USART_BaudRate = 19200; +// USART_InitStructure.USART_WordLength = USART_WordLength_8b; +// USART_InitStructure.USART_StopBits = USART_StopBits_1; +// USART_InitStructure.USART_Parity = USART_Parity_No; +// USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; +// USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; +// +// STM_EVAL_COMInit(COM1, &USART_InitStructure); +// // STM_EVAL_COMInit(COM2, &USART_InitStructure); +// +// USART_SendData(EVAL_COM1, (uint8_t) 0); +// USART_SendData(EVAL_COM1, (uint8_t) 1); +// USART_SendData(EVAL_COM1, (uint8_t) 2); +// // USART_SendData(EVAL_COM2, (uint8_t) 'B'); init_SPI2(); SPIBuffer[0] = 0x1234; SPIBuffer[1] = 0x5678; - + init_USART1_w_interrupt(); @@ -93,15 +102,16 @@ void spi_dma_shovel(void) { - DMA_Cmd(DMA1_Channel4, DISABLE); +// DMA_Cmd(DMA1_Channel4, DISABLE); DMA_Cmd(DMA1_Channel5, DISABLE); - DMA_SetCurrDataCounter(DMA1_Channel4, 2); +// DMA_SetCurrDataCounter(DMA1_Channel4, 2); DMA_SetCurrDataCounter(DMA1_Channel5, 2); // Chip Select Low - GPIO_WriteBit(GPIOB, GPIO_Pin_12, RESET); +// GPIO_WriteBit(GPIOB, GPIO_Pin_12, RESET); + CB_GPIO_Out_Lo(UC_CS); // spi nCS -> idle Hi - DMA_Cmd(DMA1_Channel4, ENABLE); +// DMA_Cmd(DMA1_Channel4, ENABLE); DMA_Cmd(DMA1_Channel5, ENABLE); } @@ -120,17 +130,18 @@ void init_SPI2(void) { 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); + // // MISO, not used +// 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); + // use different CS +// 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; @@ -145,31 +156,35 @@ void init_SPI2(void) { SPI_Init(SPI2, &SPI_InitStructure); SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE); - SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE); + // // MISO not used +// 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); + // // MISO not used +// // 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_MemoryBaseAddr = (uint32_t)SPIBuffer; + 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; @@ -235,14 +250,14 @@ void SysTick_Handler(void) static uint8_t spi_addr = 0; - USART_SendData(EVAL_COM1, (uint8_t) 0); +// USART_SendData(EVAL_COM1, (uint8_t) 0); SPIBuffer[0] = (uint16_t) spi_addr; // address - SPIBuffer[1] = (uint16_t) spi_addr; // data content + SPIBuffer[1] = (uint16_t) uC_regs[spi_addr]; // data content // SPIBuffer[1] = 0x5678; // data content spi_dma_shovel(); - spi_addr++; + spi_addr=(spi_addr+1)%16; } @@ -284,6 +299,7 @@ void init_CB_GPIO_Outputs(void) { CB_GPIO_Out_Init(ADC_CONV0); CB_GPIO_Out_Init(ADC_CONV1); CB_GPIO_Out_Init(DAC_CS); + CB_GPIO_Out_Init(UC_CS); CB_GPIO_Out_Hi(LED1); @@ -312,8 +328,163 @@ void init_CB_GPIO_Outputs(void) { CB_GPIO_Out_Hi(ADC_CONV0); // spi nCS -> idle Hi CB_GPIO_Out_Hi(ADC_CONV1); // spi nCS -> idle Hi CB_GPIO_Out_Hi(DAC_CS); // spi nCS -> idle Hi + CB_GPIO_Out_Hi(UC_CS); // spi nCS -> idle Hi } +void init_USART1_w_interrupt(void) { + /* structure contains data for USART configuration */ + USART_InitTypeDef USART_InitStructure; + + /* HW configuration */ + RCC_Configuration(); + GPIO_Configuration(); + NVIC_Configuration(); + + /* USARTx configuration ---------------------------------------------------*/ + USART_InitStructure.USART_BaudRate = 9600; + USART_InitStructure.USART_WordLength = USART_WordLength_8b; + USART_InitStructure.USART_StopBits = USART_StopBits_1; + USART_InitStructure.USART_Parity = USART_Parity_No ; + USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + + /* Configurating and enabling USART1 */ + USART_Init(USART1, &USART_InitStructure); + USART_Cmd(USART1, ENABLE); + + /* Enabling interrupt from USART1 */ + USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); + + while (1); +} + +/* clock sources configuration */ +void RCC_Configuration(void) +{ + /* error status */ + ErrorStatus HSEStartUpStatus; + + RCC_DeInit(); + + RCC_HSEConfig(RCC_HSE_ON); + HSEStartUpStatus = RCC_WaitForHSEStartUp(); + + if (HSEStartUpStatus == SUCCESS) { + FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); + FLASH_SetLatency(FLASH_Latency_2); + + RCC_HCLKConfig(RCC_SYSCLK_Div1); + RCC_PCLK2Config(RCC_HCLK_Div1); + RCC_PCLK1Config(RCC_HCLK_Div2); + + RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); + RCC_PLLCmd(ENABLE); + while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); + + RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); + while (RCC_GetSYSCLKSource() != 0x08); + } + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); +} + + +/* I/O ports configuration */ +void GPIO_Configuration(void) +{ + GPIO_InitTypeDef GPIO_InitStructure; + + /* Configuring USART1_Tx as 'alternate function push-pull' */ + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_Init(GPIOA, &GPIO_InitStructure); + + /* Configuring USART1_Rx as 'input floating' */ + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_Init(GPIOA, &GPIO_InitStructure); +} + + +/* NVIC module configuration */ +void NVIC_Configuration(void) +{ + NVIC_InitTypeDef NVIC_InitStructure; + + /* Place the vector table into FLASH */ + NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); + + /* Enabling interrupt from USART1 */ + NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); +} + + + +/* Handles interrupt from USART1 */ +void USART1_IRQHandler(void) +{ + static uint8_t byte_counter = 0; + static uint8_t addr = 0; + static uint8_t byte1 = 0; + static uint8_t byte0 = 0; + + u8 data; +// char buf[9]; + + /* Pick up the character, prepare its binary form */ + data = USART_ReceiveData(USART1); + byte_counter++; + switch( byte_counter ){ + case 1: + addr = data; + break; + case 2: + byte1 = data; + break; + case 3: + byte0 = data; + uC_regs[addr] = ((uint16_t) byte1)<<8 | (uint16_t) byte0; + byte_counter = 0; + decode_uC_reg(addr); + break; + } +} + + + + +void decode_uC_reg(uint8_t addr) { + + switch (addr){ + case 0: + uC_regs[uC_regs[addr]]++; + USART_SendData(USART1, addr); + while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); + USART_SendData(USART1, (uint8_t) (uC_regs[addr]>>8)); // upper nibble + while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); + USART_SendData(USART1, (uint8_t) (uC_regs[addr] & 0x00FF)); // lower nibble + while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); + + break; + case 1: + CB_GPIO_Out(ENAA0,uC_regs[addr] & 1<<5); + CB_GPIO_Out(DISA0,uC_regs[addr] & 1<<4); + CB_GPIO_Out(ENAD0,uC_regs[addr] & 1<<3); + CB_GPIO_Out(DISD0,uC_regs[addr] & 1<<2); + CB_GPIO_Out(SENSOREN0,uC_regs[addr] & 1<<1); + CB_GPIO_Out(JTAGEN0,uC_regs[addr] & 1<<0); + break; + + } + + +} \ No newline at end of file diff --git a/firmware/src/periph_conf.c b/firmware/src/periph_conf.c index a52c34d..13f4e94 100644 --- a/firmware/src/periph_conf.c +++ b/firmware/src/periph_conf.c @@ -33,7 +33,7 @@ // by micha, -GPIO_TypeDef* CB_GPIO_PORT[26] = +GPIO_TypeDef* CB_GPIO_PORT[27] = { LED1_GPIO_PORT, LED2_GPIO_PORT, @@ -60,11 +60,12 @@ GPIO_TypeDef* CB_GPIO_PORT[26] = ZEROCALIB_GPIO_PORT, ADC_CONV0_GPIO_PORT, ADC_CONV1_GPIO_PORT, - DAC_CS_GPIO_PORT + DAC_CS_GPIO_PORT, + UC_CS_GPIO_PORT }; -const uint16_t CB_GPIO_PIN[26] = +const uint16_t CB_GPIO_PIN[27] = { LED1_PIN, LED2_PIN, @@ -91,11 +92,12 @@ const uint16_t CB_GPIO_PIN[26] = ZEROCALIB_PIN, ADC_CONV0_PIN, ADC_CONV1_PIN, - DAC_CS_PIN + DAC_CS_PIN, + UC_CS_PIN }; -const uint32_t CB_GPIO_CLK[26] = +const uint32_t CB_GPIO_CLK[27] = { LED1_GPIO_CLK, LED2_GPIO_CLK, @@ -122,7 +124,8 @@ const uint32_t CB_GPIO_CLK[26] = ZEROCALIB_GPIO_CLK, ADC_CONV0_GPIO_CLK, ADC_CONV1_GPIO_CLK, - DAC_CS_GPIO_CLK + DAC_CS_GPIO_CLK, + UC_CS_GPIO_CLK }; @@ -274,6 +277,14 @@ void CB_GPIO_Out_Lo(CB_GPIO_Pin_TypeDef Pin) CB_GPIO_PORT[Pin]->BRR = CB_GPIO_PIN[Pin]; } +void CB_GPIO_Out(CB_GPIO_Pin_TypeDef Pin,uint16_t state){ + if(state){ + CB_GPIO_Out_Hi(Pin); + } else { + CB_GPIO_Out_Lo(Pin); + } +} + /** * @brief Toggles the selected LED. * @param Led: Specifies the Led to be toggled. diff --git a/firmware/src/periph_conf.h b/firmware/src/periph_conf.h index 783ffa4..0cb66df 100644 --- a/firmware/src/periph_conf.h +++ b/firmware/src/periph_conf.h @@ -181,6 +181,9 @@ #define DAC_CS_GPIO_PORT GPIOC #define DAC_CS_GPIO_CLK RCC_APB2Periph_GPIOC +#define UC_CS_PIN GPIO_Pin_14 +#define UC_CS_GPIO_PORT GPIOB +#define UC_CS_GPIO_CLK RCC_APB2Periph_GPIOB // give me just the line names: (bash) @@ -214,7 +217,8 @@ typedef enum ZEROCALIB = 22, ADC_CONV0 = 23, ADC_CONV1 = 24, - DAC_CS = 25 + DAC_CS = 25, + UC_CS = 26 } CB_GPIO_Pin_TypeDef; @@ -503,6 +507,7 @@ void CB_GPIO_Out_Init(CB_GPIO_Pin_TypeDef Pin); void CB_GPIO_Out_Hi(CB_GPIO_Pin_TypeDef Pin); void CB_GPIO_Out_Lo(CB_GPIO_Pin_TypeDef Pin); void CB_GPIO_Out_Toggle(CB_GPIO_Pin_TypeDef Pin); +void CB_GPIO_Out(CB_GPIO_Pin_TypeDef Pin,uint16_t state); void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode); uint32_t STM_EVAL_PBGetState(Button_TypeDef Button); diff --git a/firmware/src/stm32f10x_it.c b/firmware/src/stm32f10x_it.c index 114db16..0d7f1f8 100644 --- a/firmware/src/stm32f10x_it.c +++ b/firmware/src/stm32f10x_it.c @@ -23,6 +23,7 @@ /* Includes ------------------------------------------------------------------*/ #include "stm32f10x_it.h" +#include "stm32_eval.h" //remove this file ... #include "stm32f10x_conf.h" /*include directives for StdPeriphLib */ /* custom includes for this project */ @@ -57,7 +58,8 @@ void spi_handleDMA1Ch4Interrupt(void){ // Chip Select High - GPIO_WriteBit(GPIOB, GPIO_Pin_12, SET); +// GPIO_WriteBit(GPIOB, GPIO_Pin_12, SET); + CB_GPIO_Out_Hi(UC_CS); // spi nCS -> idle Hi } -- 2.43.0