#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 <stdio.h>
#include "newlib_stubs.c"
-#define UC_NO_REGS 14
+
+#define UC_NO_REGS 17
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
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[]) {
// 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();
- 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);
}
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;
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;
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;
}
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);
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