]> jspc29.x-matter.uni-frankfurt.de Git - mvd_firmware.git/commitdiff
quick save ... adc readout working, but data is sent to fpga just once, uC crashes...
authorMichael Wiebusch <antiquark@gmx.net>
Wed, 8 Jan 2014 16:01:36 +0000 (17:01 +0100)
committerMichael Wiebusch <antiquark@gmx.net>
Wed, 8 Jan 2014 16:01:36 +0000 (17:01 +0100)
firmware/src/CB_functions.c
firmware/src/main.c
firmware/src/spi1.c
firmware/src/spi1.h
firmware/src/spi2.c
firmware/src/spi2.h
firmware/src/stm32f10x_it.c

index 352bb2115b153872698edbf132d4fa951cdf6d1b..c28c5e861d46e5cd499e211799cbc9fe8f40fe11 100644 (file)
 #include "stm32f10x_conf.h"
 #include "misc_utils.h"
 #include "spi1.h"
+#include "spi2.h"
 #include "dac.h"
 
 
-extern uint16_t spiDataBuffer[];
+extern uint16_t spi1DataBuffer[];
+extern uint16_t SPI_DMA_Buffer_In[];
 uint16_t uC_regs[UC_NO_REGS];
 
 
@@ -55,6 +57,40 @@ void report_all_registers(void) {
 }
 
 
+void adc0_read(void){
+  
+  uint8_t c = 0;
+  
+  spi1_writeWord(CH3VSGND | UNIPOLAR); // VDDDCUR
+  // ignore received data
+  spi1_writeWord(CH4VSGND | UNIPOLAR); // VDDACUR
+  SPI_DMA_Buffer_In[c++] = VDDDCUR;
+  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  spi1_writeWord(CH5VSCH7 | UNIPOLAR); // VDDD
+  SPI_DMA_Buffer_In[c++] = VDDACUR;
+  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  spi1_writeWord(CH6VSCH7 | UNIPOLAR); // VDDA
+  SPI_DMA_Buffer_In[c++] = VDDD;
+  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  spi1_writeWord(CH7VSGND | UNIPOLAR); // GNDSENSE
+  SPI_DMA_Buffer_In[c++] = VDDA;
+  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  spi1_writeWord(CH0VSCH7 | UNIPOLAR); // TEMP
+  SPI_DMA_Buffer_In[c++] = GNDSENSE;
+  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  spi1_writeWord(CH2VSGND | UNIPOLAR); // ZEROSING
+  SPI_DMA_Buffer_In[c++] = TEMP;
+  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  spi1_writeWord(CH1VSGND | UNIPOLAR); // ZERODIFF
+  SPI_DMA_Buffer_In[c++] = ZEROSING;
+  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  spi1_writeWord(CH1VSGND | UNIPOLAR); // dummy
+  SPI_DMA_Buffer_In[c++] = ZERODIFF;
+  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  
+  spi_dma_transfer(c);
+
+}
 
 
 
@@ -102,20 +138,16 @@ void decode_register(uint8_t addr) {
       usart3_word_to_dac(((*thisRegister&0xFFFF)>>4) | (6<<12));
      
     case 0xF: //SPI Debug
-//       usart3_word_to_dac( (uint16_t) (*thisRegister&0xFFFF));
-      //CB_GPIO_Out_Hi(ADC_CONV0); // start conversion
       
-      adc_conv0_pulse();
+//       // ADC0 Test block
+//       adc_conv0_pulse();
+//       delay_us(4); // wait conversion time
+//       spi1_writeWord(*thisRegister);
+//       delay_ms(2);
+//       uC_regs[0x10]=spi1DataBuffer[0];
+//       report_register(0x10);
       
-      delay_us(4); // wait conversion time
-      //CB_GPIO_Out_Lo(ADC_CONV0); // now the data is ready
-//       spi_writeTwoBytes(((*thisRegister&0xFF00)>>8),(*thisRegister&0xFF));
-      spi_writeWord(*thisRegister);
-//       delay_us(1500);
-//       CB_GPIO_Out_Hi(ADC_CONV0);
-      delay_ms(2);
-      uC_regs[0x10]=spiDataBuffer[0];
-      report_register(0x10);
+      adc0_read();
       
       break;
       
index 2146131f5acfbf70161c0549e675ca3465c7ff8f..20539dda1848a36c76018ecc23909b8388658f03 100644 (file)
@@ -25,7 +25,8 @@
 
 
 
-extern uint16_t SPIBuffer[];
+extern uint16_t SPI_DMA_Buffer[];
+extern uint16_t SPI_DMA_Buffer_In[];
 extern uint16_t uC_regs[];
 
 // GPIO_InitTypeDef GPIO_InitStructure;
@@ -102,9 +103,9 @@ void SysTick_Handler(void)
   static uint8_t spi_addr = 0;
   
   
-  SPIBuffer[0] = (uint16_t) spi_addr; // address
-  SPIBuffer[1] = (uint16_t) uC_regs[spi_addr]; // data content
-//   SPIBuffer[1] = 0x5678; // data content
+  SPI_DMA_Buffer[0] = (uint16_t) spi_addr; // address
+  SPI_DMA_Buffer[1] = (uint16_t) uC_regs[spi_addr]; // data content
+//   SPI_DMA_Buffer[1] = 0x5678; // data content
   spi_dma_shovel();
   
   spi_addr=(spi_addr+1)%18;
index 63f2ccac772e7d4198f69507d96acf37df811010..8453bc315777810c864d0f657f8a73dff564ba6c 100644 (file)
@@ -1,17 +1,21 @@
 #include "spi1.h"
 #include "CB_functions.h"
 #include "periph_conf.h"
+#include "misc_utils.h"
  
-#define BUFFER_SIZE 2
+#define SPI1_BUFFER_SIZE 2
+
+
+
  
 SPI_TypeDef * SPI_Module;
 // GPIO_TypeDef * CS_GPIO;
 // uint16_t CS_GPIO_Pin;
  
-uint8_t spiRxCounter;
-uint8_t spiTxCounter;
-uint8_t spiBusyFlag;
-uint16_t spiDataBuffer[BUFFER_SIZE];
+uint8_t spi1RxCounter;
+uint8_t spi1TxCounter;
+uint8_t spi1BusyFlag;
+uint16_t spi1DataBuffer[SPI1_BUFFER_SIZE];
 
 
 GPIO_InitTypeDef GPIO_InitStructure;
@@ -25,63 +29,83 @@ SPI_InitTypeDef SPI_InitStructure;
 //   spiBusyFlag = 0;
 // }
  
-void spi_enableTxInterrupt(void){
+void spi1_enableTxInterrupt(void){
   SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);
 }
  
-void spi_disableTxInterrupt(void){
+void spi1_disableTxInterrupt(void){
   SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);
 }
  
-void spi_chipSelect(void){
+void spi1_chipSelect(void){
 //   GPIO_WriteBit(CS_GPIO, CS_GPIO_Pin, RESET);
-//     CB_GPIO_Out_Lo(ADC_CONV0); // spi nCS -> idle Hi
+//     CB_GPIO_Out_Lo(ADC_CONV0); // spi1 nCS -> idle Hi
 }
  
-void spi_chipDeselect(void){
+void spi1_chipDeselect(void){
 //   GPIO_WriteBit(CS_GPIO, CS_GPIO_Pin, SET);
-//     CB_GPIO_Out_Hi(ADC_CONV0); // spi nCS -> idle Hi
+//     CB_GPIO_Out_Hi(ADC_CONV0); // spi1 nCS -> idle Hi
 }
  
-void spi_writeTwoBytes(uint8_t byte1, uint8_t byte0){
-  while(spiBusyFlag){}
-  spiTxCounter = 2;
-  spiRxCounter = 2;
-  spiBusyFlag = 1;
-  spiDataBuffer[0] = byte0;
-  spiDataBuffer[1] = byte1;
-  spi_chipSelect();
-  spi_enableTxInterrupt();
+void spi1_writeTwoBytes(uint8_t byte1, uint8_t byte0){
+  while(spi1BusyFlag){}
+  spi1TxCounter = 2;
+  spi1RxCounter = 2;
+  spi1BusyFlag = 1;
+  spi1DataBuffer[0] = byte0;
+  spi1DataBuffer[1] = byte1;
+  spi1_chipSelect();
+  spi1_enableTxInterrupt();
 }
 
-void spi_writeWord(uint16_t word){
-  while(spiBusyFlag){}
-  spiTxCounter = 1;
-  spiRxCounter = 1;
-  spiBusyFlag = 1;
-  spiDataBuffer[0] = word;
-  spi_chipSelect();
-  spi_enableTxInterrupt();
+void spi1_writeWord(uint16_t word){
+  while(spi1BusyFlag){}
+  adc_conv0_pulse();
+  delay_us(4); // wait conversion time
+  spi1TxCounter = 1;
+  spi1RxCounter = 1;
+  spi1BusyFlag = 1;
+  spi1DataBuffer[0] = word;
+//   spi1_chipSelect();
+  spi1_enableTxInterrupt();
+  
+}
+
+uint16_t spi1_receivedWord(void){
+  while(spi1BusyFlag){}
+  return spi1DataBuffer[0];
+}
+
+void spi1_communicate(uint8_t rounds){
+  while(spi1BusyFlag){}
+  spi1TxCounter = rounds;
+  spi1RxCounter = rounds;
+  spi1BusyFlag = 1;
+//   spi1_chipSelect();
+  spi1_enableTxInterrupt();
   
 }
  
-void spi_handleSPI1Interrupt(void){
+void spi1_handleSPI1Interrupt(void){
   if(SPI_I2S_GetFlagStatus(SPI_Module, SPI_I2S_FLAG_RXNE) == SET){
     // Receive Buffer Not Empty
-    spiRxCounter--;
-    spiDataBuffer[spiRxCounter] = SPI_I2S_ReceiveData(SPI_Module);
+    spi1RxCounter--;
+    spi1DataBuffer[spi1RxCounter] = SPI_I2S_ReceiveData(SPI_Module);
  
-    if(spiRxCounter == 0){
-      spi_chipDeselect();
-      spiBusyFlag = 0;
+    if(spi1RxCounter == 0){
+//       spi1_chipDeselect();
+      spi1BusyFlag = 0;
     }
   }else if(SPI_I2S_GetFlagStatus(SPI_Module, SPI_I2S_FLAG_TXE) == SET){
     // Transmit Buffer Empty
-    if(spiTxCounter != 0){
-      SPI_I2S_SendData(SPI_Module, spiDataBuffer[spiTxCounter - 1]);
-      spiTxCounter--;
+    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]);
+      spi1TxCounter--;
     }else{
-      spi_disableTxInterrupt();
+      spi1_disableTxInterrupt();
     }
   }
 }
@@ -150,7 +174,7 @@ void init_SPI1(void){
   SPI_Module = SPI1;
 //   CS_GPIO = GPIOA;
 //   CS_GPIO_Pin = CS_GPIO_Pin_x;
-  spiBusyFlag = 0;
+  spi1BusyFlag = 0;
   
 }
 
@@ -160,3 +184,36 @@ inline void adc_conv0_pulse(void){
   ADC_CONV0_GPIO_PORT->BRR = ADC_CONV0_PIN; // ... short activation pulse
 }
 
+
+
+
+
+void read_adc0_block0(void){
+  
+  // 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){
+  
+  // 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);
+}
+
index 1207f23e8d5372b1e48eab7ce6c843d7052641c1..738a6d493b744de7f0b1007763bbbb2912859eb5 100644 (file)
@@ -1,9 +1,79 @@
 #include "stm32f10x_conf.h"
  
+//     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 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 spi_handleSPI1Interrupt(void);
-void spi_writeTwoBytes(uint8_t byte1, uint8_t byte0);
-void spi_writeWord(uint16_t word);
+void spi1_handleSPI1Interrupt(void);
+void spi1_writeTwoBytes(uint8_t byte1, uint8_t byte0);
+void spi1_writeWord(uint16_t word);
+uint16_t spi1_receivedWord(void);
 
 void init_SPI1(void);
-void adc_conv0_pulse(void);
\ No newline at end of file
+void adc_conv0_pulse(void);
+
+void spi1_communicate(uint8_t rounds);
+void read_adc0_block0(void);
+void read_adc0_block1(void);
index ce28d55afd6d43d807d8aa878d3cf97728f4ac29..0255ddf6c1ce988b9a33014a62132cda35f4ea31 100644 (file)
@@ -9,8 +9,11 @@
 #include "spi2.h"\r
 #include "periph_conf.h"\r
 #include "stm32f10x_conf.h"\r
+#include <string.h>\r
 \r
-uint16_t SPIBuffer[] = {0xAAAA, 0xAAAA, 0xAAAA};\r
+uint16_t SPI_DMA_Buffer[32];\r
+uint16_t SPI_DMA_Buffer_In[32];\r
+uint8_t spi_dma_busyFlag;\r
 \r
 GPIO_InitTypeDef GPIO_InitStructure;\r
 NVIC_InitTypeDef NVIC_InitStructure;\r
@@ -67,7 +70,7 @@ void init_SPI2(void) {
 //   DMA_InitStructure.DMA_BufferSize = 0;\r
 //   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;\r
 //   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;\r
-//   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPIBuffer;\r
+//   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_DMA_Buffer;\r
 //   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;\r
 //   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;\r
 //   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;\r
@@ -83,9 +86,9 @@ void init_SPI2(void) {
   DMA_InitStructure.DMA_BufferSize = 0;\r
   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;\r
   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;\r
-//   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPIBuffer; \r
-  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPIBuffer; \r
-  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;\r
+//   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_DMA_Buffer; \r
+  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_DMA_Buffer; \r
+  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // means 16 bit\r
   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;\r
   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;\r
   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR;\r
@@ -99,23 +102,36 @@ void init_SPI2(void) {
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
   NVIC_Init(&NVIC_InitStructure);\r
+  \r
+  spi_dma_busyFlag = 0;\r
 }\r
 \r
 \r
 \r
-void spi_dma_shovel(void) {\r
-\r
+void spi_dma_transfer(uint8_t count) {\r
 \r
+  while(spi_dma_busyFlag){};\r
+  \r
+  memcpy(SPI_DMA_Buffer, SPI_DMA_Buffer_In, count );\r
   \r
 //   DMA_Cmd(DMA1_Channel4, DISABLE);\r
   DMA_Cmd(DMA1_Channel5, DISABLE);\r
 //   DMA_SetCurrDataCounter(DMA1_Channel4, 2);\r
-  DMA_SetCurrDataCounter(DMA1_Channel5, 2);\r
+  DMA_SetCurrDataCounter(DMA1_Channel5, count);\r
   \r
   // Chip Select Low\r
 //   GPIO_WriteBit(GPIOB, GPIO_Pin_12, RESET);\r
   CB_GPIO_Out_Lo(UC_CS);     // spi nCS -> idle Hi\r
   \r
 //   DMA_Cmd(DMA1_Channel4, ENABLE);\r
+  spi_dma_busyFlag = 1;\r
   DMA_Cmd(DMA1_Channel5, ENABLE);\r
 }\r
+\r
+\r
+void spi_handleDMA1Ch4Interrupt(void){\r
+  // Chip Select High\r
+//   GPIO_WriteBit(GPIOB, GPIO_Pin_12, SET);\r
+    CB_GPIO_Out_Hi(UC_CS);     // spi nCS -> idle Hi\r
+    spi_dma_busyFlag = 0;\r
+}
\ No newline at end of file
index 4d6b8274c2aca2fbd09c38bc1fb7351eababb8e0..8e45b5995ffbf9d5a601aae8246dee18b2ddb3cb 100644 (file)
@@ -14,4 +14,5 @@
 \r
 void init_SPI2(void);\r
 \r
-void spi_dma_shovel(void);
\ No newline at end of file
+void spi_dma_transfer(uint8_t count);\r
+void spi_handleDMA1Ch4Interrupt(void);
\ No newline at end of file
index 0044363a446c64c3cca8e6b11e2ef98c8737c457..6f3e5b04bd1ecf6ee9fdb7002db6eba80b55b701 100644 (file)
@@ -29,6 +29,7 @@
 /* custom includes for this project */\r
 // #include "serial.h"\r
 #include "spi1.h"\r
+#include "spi2.h"\r
 \r
 \r
 /** @addtogroup STM32F10x_StdPeriph_Template\r
 \r
 \r
 void SPI1_IRQHandler(void){\r
-  spi_handleSPI1Interrupt();\r
+  spi1_handleSPI1Interrupt();\r
 }\r
 \r
 \r
 \r
-void spi_handleDMA1Ch4Interrupt(void){\r
-  // Chip Select High\r
-//   GPIO_WriteBit(GPIOB, GPIO_Pin_12, SET);\r
-    CB_GPIO_Out_Hi(UC_CS);     // spi nCS -> idle Hi\r
-}\r
+\r
 \r
 \r
 \r