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){
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
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);
-}
+}
}
+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);