From ce14d1d679baae0137e593402b5debae04685e77 Mon Sep 17 00:00:00 2001 From: Michael Wiebusch Date: Tue, 14 Jan 2014 16:54:40 +0100 Subject: [PATCH] can now read out both ADCs, fixed bug with spi2 transmission (CS cleared too early) --- firmware/src/CB_functions.c | 111 +++++++++++++++++++- firmware/src/CB_functions.h | 55 +++++++++- firmware/src/Makefile | 1 + firmware/src/ltc1867.h | 34 +++++++ firmware/src/main.c | 3 + firmware/src/spi1.c | 49 ++------- firmware/src/spi1.h | 65 ------------ firmware/src/spi2.c | 5 +- firmware/src/spi3.c | 187 ++++++++++++++++++++++++++++++++++ firmware/src/spi3.h | 19 ++++ firmware/src/stm32f10x_conf.h | 16 +++ firmware/src/stm32f10x_it.c | 5 +- firmware/src/stm32f10x_it.h | 1 + 13 files changed, 437 insertions(+), 114 deletions(-) create mode 100644 firmware/src/ltc1867.h create mode 100644 firmware/src/spi3.c create mode 100644 firmware/src/spi3.h diff --git a/firmware/src/CB_functions.c b/firmware/src/CB_functions.c index ab45120..7aa19fc 100644 --- a/firmware/src/CB_functions.c +++ b/firmware/src/CB_functions.c @@ -12,7 +12,9 @@ #include "misc_utils.h" #include "spi1.h" #include "spi2.h" +#include "spi3.h" #include "dac.h" +#include "ltc1867.h" extern uint16_t spi1DataBuffer[]; @@ -70,9 +72,6 @@ void report_all_registers(void) { void adc0_read(void){ uint8_t c = 0; // counter for spi buffer fill - static uint8_t d = 0; - - c = 0; // the received data is always one transfer cycle // later than the corresponding commands requesting @@ -168,7 +167,111 @@ void adc0_read(void){ // SPI_DMA_Buffer[c++] = d; // SPI_DMA_Buffer[c++] = d; // - d = (d+1)%16; +// + debug_out(c); + + spi_dma_transfer(c); + +} + +void adc1_read(void){ + + uint8_t c = 0; // counter for spi buffer fill + + // the received data is always one transfer cycle + // later than the corresponding commands requesting + // for the data + + // set external multiplexers for zero calibration + CB_GPIO_Out_Hi(ZEROCALIB); + CB_GPIO_Out_Lo(MUXADDR0); + CB_GPIO_Out_Lo(MUXADDR1); + + spi3_writeWord(CH3VSGND | UNIPOLAR); // VDDDCUR + // ignore received data + spi3_writeWord(CH4VSGND | UNIPOLAR); // VDDACUR + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDDDCUR; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); +// spi3_writeWord(CH5VSCH7 | UNIPOLAR); // VDDD + spi3_writeWord(CH5VSGND | UNIPOLAR); // VDDD vs AGND + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDDACUR; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); +// spi3_writeWord(CH6VSCH7 | UNIPOLAR); // VDDA + spi3_writeWord(CH6VSGND | UNIPOLAR); // VDDA vs AGND + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDDD; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + spi3_writeWord(CH7VSGND | UNIPOLAR); // GNDSENSE + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDDA; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + spi3_writeWord(CH0VSCH7 | UNIPOLAR); // TEMP + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + GNDSENSE; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + spi3_writeWord(CH2VSGND | UNIPOLAR); // ZEROSING + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + TEMP; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + spi3_writeWord(CH1VSGND | UNIPOLAR); // ZERODIFF + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + ZEROSING; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + + + // set external multiplexers to read VDISCREFA/VDISCREF2A + CB_GPIO_Out_Lo(ZEROCALIB); + CB_GPIO_Out_Hi(MUXADDR0); + CB_GPIO_Out_Hi(MUXADDR1); + + spi3_writeWord(CH2VSGND | UNIPOLAR); // VDISCREF2A + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + ZERODIFF; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + spi3_writeWord(CH1VSGND | UNIPOLAR); // VDISCREFA + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREF2A; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + + + // set external multiplexers to read VDISCREFB/VDISCREF2B + CB_GPIO_Out_Lo(ZEROCALIB); + CB_GPIO_Out_Lo(MUXADDR0); + CB_GPIO_Out_Lo(MUXADDR1); + + spi3_writeWord(CH2VSGND | UNIPOLAR); // VDISCREF2B + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREFA; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + spi3_writeWord(CH1VSGND | UNIPOLAR); // VDISCREFB + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREF2B; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + + + // set external multiplexers to read VDISCREFC/VDISCREF2C + CB_GPIO_Out_Lo(ZEROCALIB); + CB_GPIO_Out_Hi(MUXADDR0); + CB_GPIO_Out_Lo(MUXADDR1); + + spi3_writeWord(CH2VSGND | UNIPOLAR); // VDISCREF2C + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREFB; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + spi3_writeWord(CH1VSGND | UNIPOLAR); // VDISCREFC + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREF2C; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + + // set external multiplexers to read VDISCREFD/VDISCREF2D + CB_GPIO_Out_Lo(ZEROCALIB); + CB_GPIO_Out_Lo(MUXADDR0); + CB_GPIO_Out_Hi(MUXADDR1); + + spi3_writeWord(CH2VSGND | UNIPOLAR); // VDISCREF2D + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREFC; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + spi3_writeWord(CH1VSGND | UNIPOLAR); // VDISCREFD + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREF2D; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + + spi3_writeWord(CH3VSGND | UNIPOLAR); // dummy command + SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREFD; + SPI_DMA_Buffer_In[c++] = spi3_receivedWord(); + + +// SPI_DMA_Buffer[c++] = d; +// SPI_DMA_Buffer[c++] = d; +// // debug_out(c); diff --git a/firmware/src/CB_functions.h b/firmware/src/CB_functions.h index a31fa16..ef9601e 100644 --- a/firmware/src/CB_functions.h +++ b/firmware/src/CB_functions.h @@ -9,6 +9,58 @@ #include "stm32f10x.h" +// addresses of ADC readings in SPI-Ram +// 0 digital current CH3 vs GND +// +// 1 analog current CH4 vs GND +// +// 2 digital voltage CH5 vs CH7 +// +// 3 analog voltage CH6 vs CH7 +// +// 4 GND voltage CH7 vs GND +// +// 5 Temperature CH0 vs CH7 +// +// 6 Zero calibration single ended CH2 vs GND +// +// 7 Zero calibration differential CH1 vs GND +// +// 8 - 11 DiscRef 2A-2D CH2 vs GND +// +// 12-15 DiscRef A-D differential CH1 vs GND + + + + + +#define VDDDCUR 0 +#define VDDACUR 1 +#define VDDD 2 +#define VDDA 3 +#define GNDSENSE 4 +#define TEMP 5 +#define ZEROSING 6 +#define ZERODIFF 7 + +#define VDISCREF2A 8 +#define VDISCREF2B 9 +#define VDISCREF2C 10 +#define VDISCREF2D 11 + +#define VDISCREFA 12 +#define VDISCREFB 13 +#define VDISCREFC 14 +#define VDISCREFD 15 + +#define ADC1_OFFSET 16 + + + + + + + #define UC_NO_REGS 20 void init_CB_GPIO_Outputs(void); @@ -19,4 +71,5 @@ void report_all_registers(void); void uart_byte_to_fpga(uint8_t byte); void debug_out(uint16_t debu); -void adc0_read(void); \ No newline at end of file +void adc0_read(void); +void adc1_read(void); \ No newline at end of file diff --git a/firmware/src/Makefile b/firmware/src/Makefile index 769a8a6..8aad90c 100644 --- a/firmware/src/Makefile +++ b/firmware/src/Makefile @@ -9,6 +9,7 @@ OBJS+=CB_functions.o OBJS+=usart1.o OBJS+=spi1.o OBJS+=spi2.o +OBJS+=spi3.o OBJS+=dac.o OBJS+=misc_utils.o diff --git a/firmware/src/ltc1867.h b/firmware/src/ltc1867.h new file mode 100644 index 0000000..90af87f --- /dev/null +++ b/firmware/src/ltc1867.h @@ -0,0 +1,34 @@ +/** + ******************************************************** + * + * LTC1867(ADC) Commands / other defines + * + ******************************************************** + */ + +#define UNIPOLAR 0b0000010000000000 +#define BIPOLAR 0b0000000000000000 + +#define CH0VSGND 0b1000000000000000 +#define CH1VSGND 0b1100000000000000 +#define CH2VSGND 0b1001000000000000 +#define CH3VSGND 0b1101000000000000 +#define CH4VSGND 0b1010000000000000 +#define CH5VSGND 0b1110000000000000 +#define CH6VSGND 0b1011000000000000 +#define CH7VSGND 0b1111000000000000 + +#define CH0VSCH7 0b1000100000000000 +#define CH1VSCH7 0b1100100000000000 +#define CH2VSCH7 0b1001100000000000 +#define CH3VSCH7 0b1101100000000000 +#define CH4VSCH7 0b1010100000000000 +#define CH5VSCH7 0b1110100000000000 +#define CH6VSCH7 0b1011100000000000 +#define CH7VSCH7 0b1111100000000000 + +#define CONV_TIME 4 +#define ACQ_DELAY 2 + +// example usage: +// spi1_writeWord(CH3VSGND | UNIPOLAR); \ No newline at end of file diff --git a/firmware/src/main.c b/firmware/src/main.c index a5dcecd..9780cac 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -17,6 +17,7 @@ #include "usart1.h" #include "spi1.h" #include "spi2.h" +#include "spi3.h" #include "dac.h" #include "periph_conf.h" #include "misc_utils.h" @@ -67,6 +68,7 @@ int main(int argc, char *argv[]) { init_SPI1(); init_SPI2(); + init_SPI3(); init_USART1(); @@ -84,6 +86,7 @@ int main(int argc, char *argv[]) { }else{ if(uC_regs[0x0b] & 1){ adc0_read(); + adc1_read(); } } diff --git a/firmware/src/spi1.c b/firmware/src/spi1.c index 2c7cd43..7d3dca9 100644 --- a/firmware/src/spi1.c +++ b/firmware/src/spi1.c @@ -2,13 +2,14 @@ #include "CB_functions.h" #include "periph_conf.h" #include "misc_utils.h" +#include "ltc1867.h" #define SPI1_BUFFER_SIZE 2 -SPI_TypeDef * SPI_Module; +// SPI_TypeDef * SPI_Module; // GPIO_TypeDef * CS_GPIO; // uint16_t CS_GPIO_Pin; @@ -61,7 +62,7 @@ void spi1_writeTwoBytes(uint8_t byte1, uint8_t byte0){ void spi1_writeWord(uint16_t word){ while(spi1BusyFlag){} adc_conv0_pulse(); - delay_us(4); // wait conversion time + delay_us(CONV_TIME); // wait conversion time spi1TxCounter = 1; spi1RxCounter = 1; spi1BusyFlag = 1; @@ -87,22 +88,22 @@ void spi1_communicate(uint8_t rounds){ } void spi1_handleSPI1Interrupt(void){ - if(SPI_I2S_GetFlagStatus(SPI_Module, SPI_I2S_FLAG_RXNE) == SET){ + if(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == SET){ // Receive Buffer Not Empty spi1RxCounter--; - spi1DataBuffer[spi1RxCounter] = SPI_I2S_ReceiveData(SPI_Module); + spi1DataBuffer[spi1RxCounter] = SPI_I2S_ReceiveData(SPI1); if(spi1RxCounter == 0){ // spi1_chipDeselect(); spi1BusyFlag = 0; } - }else if(SPI_I2S_GetFlagStatus(SPI_Module, SPI_I2S_FLAG_TXE) == SET){ + }else if(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == SET){ // Transmit Buffer Empty if(spi1TxCounter != 0){ // delay_us(ACQ_DELAY); // delay between acquisitions // adc_conv0_pulse(); // conversion pulse // delay_us(CONV_TIME); // wait a bit until conversion is finished - SPI_I2S_SendData(SPI_Module, spi1DataBuffer[spi1TxCounter - 1]); + SPI_I2S_SendData(SPI1, spi1DataBuffer[spi1TxCounter - 1]); spi1TxCounter--; }else{ spi1_disableTxInterrupt(); @@ -171,7 +172,7 @@ void init_SPI1(void){ // on top :D // spi_create(SPI1, GPIOA); - SPI_Module = SPI1; +// SPI_Module = SPI1; // CS_GPIO = GPIOA; // CS_GPIO_Pin = CS_GPIO_Pin_x; spi1BusyFlag = 0; @@ -183,37 +184,3 @@ inline void adc_conv0_pulse(void){ ADC_CONV0_GPIO_PORT->BSRR = ADC_CONV0_PIN; // make a ... ADC_CONV0_GPIO_PORT->BRR = ADC_CONV0_PIN; // ... short activation pulse } - - - - - -void read_adc0_block0(void){// do not use, remove soon - - // power supply voltages/currents, temperature + zero calib - - spi1DataBuffer[0] = (CH3VSGND | UNIPOLAR); // VDDDCUR - spi1DataBuffer[1] = (CH4VSGND | UNIPOLAR); // VDDACUR - spi1DataBuffer[2] = (CH5VSCH7 | UNIPOLAR); // VDDD - spi1DataBuffer[3] = (CH6VSCH7 | UNIPOLAR); // VDDA - spi1DataBuffer[4] = (CH7VSGND | UNIPOLAR); // GNDSENSE - spi1DataBuffer[5] = (CH0VSCH7 | UNIPOLAR); // TEMP - spi1DataBuffer[6] = (CH2VSGND | UNIPOLAR); // ZEROSING - spi1DataBuffer[7] = (CH1VSGND | UNIPOLAR); // ZERODIFF - - spi1DataBuffer[8] = (CH1VSGND | UNIPOLAR); // dummy command - - spi1_communicate(9); -} - - -void read_adc0_block1(void){ // do not use, remove soon - - // VDiscRef differential + VDiscRef2 single ended - - spi1DataBuffer[0] = (CH2VSGND | UNIPOLAR); // VDISCREF2[A-D] - spi1DataBuffer[1] = (CH1VSGND | BIPOLAR); // VDISCREF[A-D] - spi1DataBuffer[2] = (CH1VSGND | BIPOLAR); // dummy command - spi1_communicate(3); -} - diff --git a/firmware/src/spi1.h b/firmware/src/spi1.h index 03d4075..0646b32 100644 --- a/firmware/src/spi1.h +++ b/firmware/src/spi1.h @@ -2,70 +2,7 @@ - - -// 0 digital current CH3 vs GND -// -// 1 analog current CH4 vs GND -// -// 2 digital voltage CH5 vs CH7 -// -// 3 analog voltage CH6 vs CH7 -// -// 4 GND voltage CH7 vs GND -// -// 5 Temperature CH0 vs CH7 -// -// 6 Zero calibration single ended CH2 vs GND -// -// 7 Zero calibration differential CH1 vs GND -// -// 8 - 11 DiscRef 2A-2D CH2 vs GND -// -// 12-15 DiscRef A-D differential CH1 vs GND - -#define VDDDCUR 0 -#define VDDACUR 1 -#define VDDD 2 -#define VDDA 3 -#define GNDSENSE 4 -#define TEMP 5 -#define ZEROSING 6 -#define ZERODIFF 7 - -#define VDISCREF2A 8 -#define VDISCREF2B 9 -#define VDISCREF2C 10 -#define VDISCREF2D 11 - -#define VDISCREFA 12 -#define VDISCREFB 13 -#define VDISCREFC 14 -#define VDISCREFD 15 - -#define UNIPOLAR 0b0000010000000000 -#define BIPOLAR 0b0000000000000000 - -#define CH0VSGND 0b1000000000000000 -#define CH1VSGND 0b1100000000000000 -#define CH2VSGND 0b1001000000000000 -#define CH3VSGND 0b1101000000000000 -#define CH4VSGND 0b1010000000000000 -#define CH5VSGND 0b1110000000000000 -#define CH6VSGND 0b1011000000000000 -#define CH7VSGND 0b1111000000000000 -#define CH0VSCH7 0b1000100000000000 -#define CH5VSCH7 0b1110100000000000 -#define CH6VSCH7 0b1011100000000000 - -#define CONV_TIME 4 -#define ACQ_DELAY 2 - - - - - // void spi_create(SPI_TypeDef * SPIx, GPIO_TypeDef * CS_GPIOx); void spi1_handleSPI1Interrupt(void); @@ -77,5 +14,3 @@ void init_SPI1(void); void adc_conv0_pulse(void); void spi1_communicate(uint8_t rounds); -void read_adc0_block0(void); -void read_adc0_block1(void); diff --git a/firmware/src/spi2.c b/firmware/src/spi2.c index 06f1a43..56dcfe4 100644 --- a/firmware/src/spi2.c +++ b/firmware/src/spi2.c @@ -115,6 +115,7 @@ void init_SPI2(void) { void spi_dma_transfer(uint8_t count) { while(spi_dma_busyFlag){}; + CB_GPIO_Out_Hi(UC_CS); // spi nCS -> idle Hi memcpy(SPI_DMA_Buffer, SPI_DMA_Buffer_In, count*2 ); @@ -125,10 +126,10 @@ void spi_dma_transfer(uint8_t count) { // Chip Select Low // GPIO_WriteBit(GPIOB, GPIO_Pin_12, RESET); - CB_GPIO_Out_Lo(UC_CS); // spi nCS -> idle Hi // DMA_Cmd(DMA1_Channel4, ENABLE); spi_dma_busyFlag = 1; + CB_GPIO_Out_Lo(UC_CS); // spi nCS -> idle Hi DMA_Cmd(DMA1_Channel5, ENABLE); } @@ -136,6 +137,6 @@ void spi_dma_transfer(uint8_t count) { void spi_handleDMA1Ch5Interrupt(void){ // Chip Select High // GPIO_WriteBit(GPIOB, GPIO_Pin_12, SET); - CB_GPIO_Out_Hi(UC_CS); // spi nCS -> idle Hi +// CB_GPIO_Out_Hi(UC_CS); // spi nCS -> idle Hi spi_dma_busyFlag = 0; } \ No newline at end of file diff --git a/firmware/src/spi3.c b/firmware/src/spi3.c new file mode 100644 index 0000000..886c88f --- /dev/null +++ b/firmware/src/spi3.c @@ -0,0 +1,187 @@ +#include "spi3.h" +#include "CB_functions.h" +#include "periph_conf.h" +#include "misc_utils.h" +#include "ltc1867.h" + +#define SPI3_BUFFER_SIZE 2 + + + + +// SPI_TypeDef * SPI_Module; +// GPIO_TypeDef * CS_GPIO; +// uint16_t CS_GPIO_Pin; + +uint8_t spi3RxCounter; +uint8_t spi3TxCounter; +uint8_t spi3BusyFlag; +uint16_t spi3DataBuffer[SPI3_BUFFER_SIZE]; + + +GPIO_InitTypeDef GPIO_InitStructure; +NVIC_InitTypeDef NVIC_InitStructure; +SPI_InitTypeDef SPI_InitStructure; + +// void spi_create(SPI_TypeDef * SPIx, GPIO_TypeDef * CS_GPIOx){ +// SPI_Module = SPIx; +// CS_GPIO = CS_GPIOx; +// // CS_GPIO_Pin = CS_GPIO_Pin_x; +// spiBusyFlag = 0; +// } + +void spi3_enableTxInterrupt(void){ + SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, ENABLE); +} + +void spi3_disableTxInterrupt(void){ + SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, DISABLE); +} + +void spi3_chipSelect(void){ +// GPIO_WriteBit(CS_GPIO, CS_GPIO_Pin, RESET); +// CB_GPIO_Out_Lo(ADC_CONV0); // spi3 nCS -> idle Hi +} + +void spi3_chipDeselect(void){ +// GPIO_WriteBit(CS_GPIO, CS_GPIO_Pin, SET); +// CB_GPIO_Out_Hi(ADC_CONV0); // spi3 nCS -> idle Hi +} + +void spi3_writeTwoBytes(uint8_t byte1, uint8_t byte0){ + while(spi3BusyFlag){} + spi3TxCounter = 2; + spi3RxCounter = 2; + spi3BusyFlag = 1; + spi3DataBuffer[0] = byte0; + spi3DataBuffer[1] = byte1; + spi3_chipSelect(); + spi3_enableTxInterrupt(); +} + +void spi3_writeWord(uint16_t word){ + while(spi3BusyFlag){} + adc_conv1_pulse(); + delay_us(CONV_TIME); // wait conversion time + spi3TxCounter = 1; + spi3RxCounter = 1; + spi3BusyFlag = 1; + spi3DataBuffer[0] = word; +// spi3_chipSelect(); + spi3_enableTxInterrupt(); + +} + +uint16_t spi3_receivedWord(void){ + while(spi3BusyFlag){} + return spi3DataBuffer[0]; +} + +void spi3_communicate(uint8_t rounds){ + while(spi3BusyFlag){} + spi3TxCounter = rounds; + spi3RxCounter = rounds; + spi3BusyFlag = 1; +// spi3_chipSelect(); + spi3_enableTxInterrupt(); + +} + +void spi3_handleSPI3Interrupt(void){ + if(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == SET){ + // Receive Buffer Not Empty + spi3RxCounter--; + spi3DataBuffer[spi3RxCounter] = SPI_I2S_ReceiveData(SPI3); + + if(spi3RxCounter == 0){ +// spi3_chipDeselect(); + spi3BusyFlag = 0; + } + }else if(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == SET){ + // Transmit Buffer Empty + if(spi3TxCounter != 0){ +// delay_us(ACQ_DELAY); // delay between acquisitions +// adc_conv0_pulse(); // conversion pulse +// delay_us(CONV_TIME); // wait a bit until conversion is finished + SPI_I2S_SendData(SPI3, spi3DataBuffer[spi3TxCounter - 1]); + spi3TxCounter--; + }else{ + spi3_disableTxInterrupt(); + } + } +} + + + + + + + + + + + + + + +void init_SPI3(void){ + + RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); // separate clock commands ... + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);//... for separate clock sources + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_3; + 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_4; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOB, &GPIO_InitStructure); + + + // CS pin is already initialized by CB_functions +// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; +// 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_8, SET); + + NVIC_InitStructure.NVIC_IRQChannel = SPI3_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_CPHA = SPI_CPHA_2Edge; //CPHA=1 + SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //CPHA=0 +// SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; + 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(SPI3, &SPI_InitStructure); + + SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_RXNE, ENABLE); + + SPI_Cmd(SPI3, ENABLE); + + // on top :D +// spi_create(SPI3, GPIOA); +// SPI_Module = SPI3; +// CS_GPIO = GPIOA; +// CS_GPIO_Pin = CS_GPIO_Pin_x; + spi3BusyFlag = 0; + +} + + +inline void adc_conv1_pulse(void){ + ADC_CONV1_GPIO_PORT->BSRR = ADC_CONV1_PIN; // make a ... + ADC_CONV1_GPIO_PORT->BRR = ADC_CONV1_PIN; // ... short activation pulse +} diff --git a/firmware/src/spi3.h b/firmware/src/spi3.h new file mode 100644 index 0000000..7be0d8b --- /dev/null +++ b/firmware/src/spi3.h @@ -0,0 +1,19 @@ +#include "stm32f10x_conf.h" + + + + + + + + +// void spi_create(SPI_TypeDef * SPIx, GPIO_TypeDef * CS_GPIOx); +void spi3_handleSPI3Interrupt(void); +void spi3_writeTwoBytes(uint8_t byte1, uint8_t byte0); +void spi3_writeWord(uint16_t word); +uint16_t spi3_receivedWord(void); + +void init_SPI3(void); +void adc_conv1_pulse(void); + +void spi3_communicate(uint8_t rounds); diff --git a/firmware/src/stm32f10x_conf.h b/firmware/src/stm32f10x_conf.h index 794ff05..f55f45b 100644 --- a/firmware/src/stm32f10x_conf.h +++ b/firmware/src/stm32f10x_conf.h @@ -1,3 +1,19 @@ +// This project runs on a High Density STM32 uC + +//(still don't know who defines a device class by default, +// grepping didn't show anything) + +#undef STM32F10X_LD +#undef STM32F10X_LD_VL +#undef STM32F10X_MD +#undef STM32F10X_MD_VL +#undef STM32F10X_HD +#undef STM32F10X_HD_VL +#undef STM32F10X_XL +#undef STM32F10X_CL + +#define STM32F10X_HD + /* Includes -----------------------------------------------------------*/ /* Uncomment the line below to enable peripheral header file inclusion */ /* #include "stm32f10x_adc.h" */ diff --git a/firmware/src/stm32f10x_it.c b/firmware/src/stm32f10x_it.c index 351171d..8b3228a 100644 --- a/firmware/src/stm32f10x_it.c +++ b/firmware/src/stm32f10x_it.c @@ -30,6 +30,7 @@ // #include "serial.h" #include "spi1.h" #include "spi2.h" +#include "spi3.h" /** @addtogroup STM32F10x_StdPeriph_Template @@ -56,7 +57,9 @@ void SPI1_IRQHandler(void){ spi1_handleSPI1Interrupt(); } - +void SPI3_IRQHandler(void){ + spi3_handleSPI3Interrupt(); +} diff --git a/firmware/src/stm32f10x_it.h b/firmware/src/stm32f10x_it.h index 0f89944..06f705a 100644 --- a/firmware/src/stm32f10x_it.h +++ b/firmware/src/stm32f10x_it.h @@ -49,6 +49,7 @@ void SysTick_Handler(void); //for spi1 void SPI1_IRQHandler(void); +void SPI3_IRQHandler(void); // for spi2 void DMA1_Channel5_IRQHandler(void); -- 2.43.0