]> jspc29.x-matter.uni-frankfurt.de Git - mvd_firmware.git/commitdiff
added SensorControlStatus register that is updated via SPI transfer
authorMichael Wiebusch <antiquark@gmx.net>
Thu, 16 Jan 2014 14:45:00 +0000 (15:45 +0100)
committerMichael Wiebusch <antiquark@gmx.net>
Thu, 16 Jan 2014 14:45:00 +0000 (15:45 +0100)
firmware/src/CB_functions.c
firmware/src/CB_functions.h
firmware/src/main.c

index e7c5c9701f9f7481bb047cf8a2c3016f704a174a..67a1b47d90538a4d6df2e9c82e42ce03dacb45d8 100644 (file)
@@ -84,6 +84,21 @@ uint16_t readAdc0(uint16_t cmd) {
   return res;
   }
 
+uint16_t readAdc1(uint16_t cmd) {
+  static uint32_t accu = 0;
+  spi3_writeWord(cmd);
+  accu += spi3_receivedWord();
+  uint16_t res = accu/4;
+  accu = 0;
+  spi3_writeWord(cmd);
+  accu += spi3_receivedWord();
+  spi3_writeWord(cmd);
+  accu += spi3_receivedWord();
+  spi3_writeWord(cmd);
+  accu += spi3_receivedWord();
+  return res;
+  }
+
 
 void adc0_read(void){
   
@@ -98,102 +113,71 @@ void adc0_read(void){
   CB_GPIO_Out_Lo(MUXADDR0); 
   CB_GPIO_Out_Lo(MUXADDR1); 
   
-  spi1_writeWord(CH3VSGND | UNIPOLAR); // VDDDCUR
-  // ignore received data
-  
-  spi1_writeWord(CH4VSGND | UNIPOLAR); // VDDACUR
-  
+  readAdc0(CH3VSGND | UNIPOLAR);  // ignore received data // VDDDCUR
   SPI_DMA_Buffer_In[c++] = VDDDCUR;
-
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-//   SPI_DMA_Buffer_In[c++] = readAdc0(CH5VSGND | UNIPOLAR);
-  //   spi1_writeWord(CH5VSCH7 | UNIPOLAR); // VDDD
-  spi1_writeWord(CH5VSGND | UNIPOLAR); // VDDD vs AGND
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH4VSGND | UNIPOLAR); // VDDACUR
   SPI_DMA_Buffer_In[c++] = VDDACUR;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-//   spi1_writeWord(CH6VSCH7 | UNIPOLAR); // VDDA
-  spi1_writeWord(CH6VSGND | UNIPOLAR); // VDDA vs AGND
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH5VSGND | UNIPOLAR); // VDDD vs AGND
   SPI_DMA_Buffer_In[c++] = VDDD;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  spi1_writeWord(CH7VSGND | UNIPOLAR); // GNDSENSE
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH6VSGND | UNIPOLAR); // VDDA vs AGND
   SPI_DMA_Buffer_In[c++] = VDDA;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  spi1_writeWord(CH0VSCH7 | UNIPOLAR); // TEMP
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH7VSGND | UNIPOLAR); // GNDSENSE
   SPI_DMA_Buffer_In[c++] = GNDSENSE;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  spi1_writeWord(CH2VSGND | UNIPOLAR); // ZEROSING
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH0VSCH7 | UNIPOLAR); // TEMP
   SPI_DMA_Buffer_In[c++] = TEMP;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  spi1_writeWord(CH1VSGND | UNIPOLAR); // ZERODIFF
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH2VSGND | UNIPOLAR); // ZEROSING
   SPI_DMA_Buffer_In[c++] = ZEROSING;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH1VSGND | UNIPOLAR); // ZERODIFF
+  
   // set external multiplexers to read VDISCREFA/VDISCREF2A
   CB_GPIO_Out_Lo(ZEROCALIB); 
   CB_GPIO_Out_Hi(MUXADDR0); 
   CB_GPIO_Out_Hi(MUXADDR1); 
   
-  spi1_writeWord(CH2VSGND | UNIPOLAR); // VDISCREF2A
   SPI_DMA_Buffer_In[c++] = ZERODIFF;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  spi1_writeWord(CH1VSGND | UNIPOLAR); // VDISCREFA
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH2VSGND | UNIPOLAR); // VDISCREF2A
   SPI_DMA_Buffer_In[c++] = VDISCREF2A;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH1VSGND | UNIPOLAR); // VDISCREFA
   
   // set external multiplexers to read VDISCREFB/VDISCREF2B
   CB_GPIO_Out_Lo(ZEROCALIB); 
   CB_GPIO_Out_Lo(MUXADDR0); 
   CB_GPIO_Out_Lo(MUXADDR1);
   
-  spi1_writeWord(CH2VSGND | UNIPOLAR); // VDISCREF2B
   SPI_DMA_Buffer_In[c++] = VDISCREFA;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  spi1_writeWord(CH1VSGND | UNIPOLAR); // VDISCREFB
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH2VSGND | UNIPOLAR); // VDISCREF2B
   SPI_DMA_Buffer_In[c++] = VDISCREF2B;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH1VSGND | UNIPOLAR); // VDISCREFB
   
   // set external multiplexers to read VDISCREFC/VDISCREF2C
   CB_GPIO_Out_Lo(ZEROCALIB); 
   CB_GPIO_Out_Hi(MUXADDR0); 
-  CB_GPIO_Out_Lo(MUXADDR1); 
+  CB_GPIO_Out_Lo(MUXADDR1);
   
-  spi1_writeWord(CH2VSGND | UNIPOLAR); // VDISCREF2C
   SPI_DMA_Buffer_In[c++] = VDISCREFB;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  spi1_writeWord(CH1VSGND | UNIPOLAR); // VDISCREFC
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH2VSGND | UNIPOLAR); // VDISCREF2C
   SPI_DMA_Buffer_In[c++] = VDISCREF2C;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH1VSGND | UNIPOLAR); // VDISCREFC
   
   // set external multiplexers to read VDISCREFD/VDISCREF2D
   CB_GPIO_Out_Lo(ZEROCALIB); 
   CB_GPIO_Out_Lo(MUXADDR0); 
   CB_GPIO_Out_Hi(MUXADDR1); 
   
-  spi1_writeWord(CH2VSGND | UNIPOLAR); // VDISCREF2D
   SPI_DMA_Buffer_In[c++] = VDISCREFC;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-  spi1_writeWord(CH1VSGND | UNIPOLAR); // VDISCREFD
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH2VSGND | UNIPOLAR); // VDISCREF2D
   SPI_DMA_Buffer_In[c++] = VDISCREF2D;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH1VSGND | UNIPOLAR); // VDISCREFD
   
-  spi1_writeWord(CH3VSGND | UNIPOLAR); // dummy command
   SPI_DMA_Buffer_In[c++] = VDISCREFD;
-  SPI_DMA_Buffer_In[c++] = spi1_receivedWord();
-
-
-//   SPI_DMA_Buffer[c++] = d;
-//   SPI_DMA_Buffer[c++] = d;
-//   
-//   
-  debug_out(c);
+  // the one extra with some dummy in it
+  SPI_DMA_Buffer_In[c++] = readAdc0(CH1VSGND | UNIPOLAR); // VDISCREFD
 
   spi_dma_transfer(c);
 
 } 
 
+
 void adc1_read(void){
   
   uint8_t c = 0; // counter for spi buffer fill
@@ -207,97 +191,69 @@ void adc1_read(void){
   CB_GPIO_Out_Lo(MUXADDR0); 
   CB_GPIO_Out_Lo(MUXADDR1); 
   
-  spi3_writeWord(CH3VSGND | UNIPOLAR); // VDDDCUR
-  // ignore received data
-  spi3_writeWord(CH4VSGND | UNIPOLAR); // VDDACUR
+  readAdc1(CH3VSGND | UNIPOLAR);  // ignore received data // VDDDCUR
   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++] = readAdc1(CH4VSGND | UNIPOLAR); // VDDACUR
   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++] = readAdc1(CH5VSGND | UNIPOLAR); // VDDD 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++] = readAdc1(CH6VSGND | UNIPOLAR); // VDDA vs AGND
   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++] = readAdc1(CH7VSGND | UNIPOLAR); // GNDSENSE
   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++] = readAdc1(CH0VSCH7 | UNIPOLAR); // TEMP
   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++] = readAdc1(CH2VSGND | UNIPOLAR); // ZEROSING
   SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + ZEROSING;
-  SPI_DMA_Buffer_In[c++] = spi3_receivedWord();
-
+  SPI_DMA_Buffer_In[c++] = readAdc1(CH1VSGND | UNIPOLAR); // ZERODIFF
+  
   // 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++] = readAdc1(CH2VSGND | UNIPOLAR); // VDISCREF2A
   SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREF2A;
-  SPI_DMA_Buffer_In[c++] = spi3_receivedWord();
-  
+  SPI_DMA_Buffer_In[c++] = readAdc1(CH1VSGND | UNIPOLAR); // VDISCREFA
   
   // 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++] = readAdc1(CH2VSGND | UNIPOLAR); // VDISCREF2B
   SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREF2B;
-  SPI_DMA_Buffer_In[c++] = spi3_receivedWord();
-  
+  SPI_DMA_Buffer_In[c++] = readAdc1(CH1VSGND | UNIPOLAR); // VDISCREFB
   
   // set external multiplexers to read VDISCREFC/VDISCREF2C
   CB_GPIO_Out_Lo(ZEROCALIB); 
   CB_GPIO_Out_Hi(MUXADDR0); 
-  CB_GPIO_Out_Lo(MUXADDR1); 
+  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++] = readAdc1(CH2VSGND | UNIPOLAR); // VDISCREF2C
   SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREF2C;
-  SPI_DMA_Buffer_In[c++] = spi3_receivedWord();
+  SPI_DMA_Buffer_In[c++] = readAdc1(CH1VSGND | UNIPOLAR); // VDISCREFC
   
   // 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++] = readAdc1(CH2VSGND | UNIPOLAR); // VDISCREF2D
   SPI_DMA_Buffer_In[c++] = ADC1_OFFSET + VDISCREF2D;
-  SPI_DMA_Buffer_In[c++] = spi3_receivedWord();
+  SPI_DMA_Buffer_In[c++] = readAdc1(CH1VSGND | UNIPOLAR); // VDISCREFD
   
-  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);
+  // the one extra with some dummy in it
+  SPI_DMA_Buffer_In[c++] = readAdc1(CH1VSGND | UNIPOLAR); // VDISCREFD
 
   spi_dma_transfer(c);
 
-}
+} 
 
 
 
@@ -389,7 +345,22 @@ void decode_register(uint8_t addr) {
   
 }
 
+void SensorControlStatus(void){
+  
+  uint8_t c = 0; // counter for spi buffer fill
+  
+  // get status of overcurrent discriminators
+  uC_regs[0x05] = CB_GPIO_In_State(OVCA_C0)<<1 | CB_GPIO_In_State(OVCD_C0);
+  uC_regs[0x0A] = CB_GPIO_In_State(OVCA_C1)<<1 | CB_GPIO_In_State(OVCD_C1);
+  // reports switch settings + overcurrent status
+  SPI_DMA_Buffer_In[c++] = 32;
+  SPI_DMA_Buffer_In[c++] = (uC_regs[0x01] & 0b111111) | (uC_regs[0x05]<<6);
+  SPI_DMA_Buffer_In[c++] = 33;
+  SPI_DMA_Buffer_In[c++] = (uC_regs[0x06] & 0b111111) | (uC_regs[0x0A]<<6);
 
+  spi_dma_transfer(c);
+}  
+  
 void init_CB_GPIO_Outputs(void) {
   
   CB_GPIO_Out_Init(LED1);
index ef9601e07e083cb8397da75fec9cb8a44214e19f..3a07f0bf79e7a55f063075fe642c32f9150a6ce8 100644 (file)
@@ -72,4 +72,9 @@ void report_all_registers(void);
 void uart_byte_to_fpga(uint8_t byte);
 void debug_out(uint16_t debu);
 void adc0_read(void);
-void adc1_read(void);
\ No newline at end of file
+void adc1_read(void);
+void SensorControlStatus(void);
+
+
+uint16_t readAdc0(uint16_t cmd);
+uint16_t readAdc1(uint16_t cmd);
\ No newline at end of file
index 9780caca4f747d33e4083711f990f539b2d9170a..7b979afb7b7c71a6c10e7ac318cf4364eae515fc 100644 (file)
@@ -84,9 +84,10 @@ int main(int argc, char *argv[]) {
     if(BufferOut(&UcRegAddr)){
       decode_register(UcRegAddr);
     }else{
-      if(uC_regs[0x0b] & 1){
+      if(uC_regs[0x0b] & 1){ // cycle ref bit set?
        adc0_read();
        adc1_read();
+       SensorControlStatus();
       }
     }