#include "spi1.h"
-
+extern uint16_t spiDataBuffer[];
uint16_t uC_regs[UC_NO_REGS];
void uart_word_to_dac(uint16_t word){
void report_register(uint8_t addr) {
+ uint16_t *thisRegister = &uC_regs[addr];
// just for debug, counts number of read back operations
uC_regs[0x13]++;
+
+ switch (addr){
+ case 0x05: //OvCurStatus Chip 0
+ *thisRegister = CB_GPIO_In_State(OVCA_C0)<<1 | CB_GPIO_In_State(OVCD_C0);
+ break;
+ case 0x0a: //OvCurStatus Chip 1
+ *thisRegister = CB_GPIO_In_State(OVCA_C1)<<1 | CB_GPIO_In_State(OVCD_C1);
+ break;
+ }
+
uart_byte_to_fpga(addr);
uart_byte_to_fpga((uint8_t) (uC_regs[addr]>>8));
}
+
+
+
+
void decode_register(uint8_t addr) {
- uint16_t thisRegister = uC_regs[addr];
+ uint16_t *thisRegister = &uC_regs[addr];
switch (addr){
case 0x0:
- if (thisRegister & 0xFF00) {
- uC_regs[0] &= 0x00FF;
+ if (*thisRegister & 0xFF00) {
+ *thisRegister &= 0x00FF;
report_all_registers();
} else {
- report_register( (uint8_t) (thisRegister & 0x00FF) );
+ report_register( (uint8_t) (*thisRegister & 0x00FF) );
}
break;
case 0x1: // Switches0
- CB_GPIO_Out(ENAA0,thisRegister & 1<<5);
- CB_GPIO_Out(DISA0,thisRegister & 1<<4);
- CB_GPIO_Out(ENAD0,thisRegister & 1<<3);
- CB_GPIO_Out(DISD0,thisRegister & 1<<2);
- CB_GPIO_Out(SENSOREN0,thisRegister & 1<<1);
- CB_GPIO_Out(JTAGEN0,thisRegister & 1<<0);
+ CB_GPIO_Out(ENAA0,*thisRegister & 1<<5);
+ CB_GPIO_Out(DISA0,*thisRegister & 1<<4);
+ CB_GPIO_Out(ENAD0,*thisRegister & 1<<3);
+ CB_GPIO_Out(DISD0,*thisRegister & 1<<2);
+ CB_GPIO_Out(SENSOREN0,*thisRegister & 1<<1);
+ CB_GPIO_Out(JTAGEN0,*thisRegister & 1<<0);
break;
case 0x2: //DAC CurLimA 0
- uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (0<<12));
+ uart_word_to_dac(((*thisRegister&0xFFFF)>>4) | (0<<12));
case 0x3: //DAC CurLimD 0
- uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (1<<12));
+ uart_word_to_dac(((*thisRegister&0xFFFF)>>4) | (1<<12));
case 0x4: //DAC VClp 0
- uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (2<<12));
+ uart_word_to_dac(((*thisRegister&0xFFFF)>>4) | (2<<12));
case 0x6: // Switches1
- CB_GPIO_Out(ENAA1,thisRegister & 1<<5);
- CB_GPIO_Out(DISA1,thisRegister & 1<<4);
- CB_GPIO_Out(ENAD1,thisRegister & 1<<3);
- CB_GPIO_Out(DISD1,thisRegister & 1<<2);
- CB_GPIO_Out(SENSOREN1,thisRegister & 1<<1);
- CB_GPIO_Out(JTAGEN1,thisRegister & 1<<0);
+ CB_GPIO_Out(ENAA1,*thisRegister & 1<<5);
+ CB_GPIO_Out(DISA1,*thisRegister & 1<<4);
+ CB_GPIO_Out(ENAD1,*thisRegister & 1<<3);
+ CB_GPIO_Out(DISD1,*thisRegister & 1<<2);
+ CB_GPIO_Out(SENSOREN1,*thisRegister & 1<<1);
+ CB_GPIO_Out(JTAGEN1,*thisRegister & 1<<0);
break;
case 0x7: //DAC CurLimA 1
- uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (4<<12));
+ uart_word_to_dac(((*thisRegister&0xFFFF)>>4) | (4<<12));
case 0x8: //DAC CurLimD 1
- uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (5<<12));
+ uart_word_to_dac(((*thisRegister&0xFFFF)>>4) | (5<<12));
case 0x9: //DAC VClp 1
- uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (6<<12));
+ uart_word_to_dac(((*thisRegister&0xFFFF)>>4) | (6<<12));
case 0xF: //SPI Debug
-// uart_word_to_dac( (uint16_t) (thisRegister&0xFFFF));
-// CB_GPIO_Out_Lo(ADC_CONV0);
-// spi_writeTwoBytes(((thisRegister&0xFF00)>>8),(thisRegister&0xFF));
- spi_writeWord(thisRegister);
+// uart_word_to_dac( (uint16_t) (*thisRegister&0xFFFF));
+ //CB_GPIO_Out_Hi(ADC_CONV0); // start conversion
+
+ ADC_CONV0_GPIO_PORT->BSRR = ADC_CONV0_PIN; // make a ...
+ ADC_CONV0_GPIO_PORT->BRR = ADC_CONV0_PIN; // ... short activation pulse
+
+ 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);
+
break;
case 0x11: // LEDs
- if(thisRegister & 1<<7) {
- CB_GPIO_Out(LED4,thisRegister & 1<<3);
+ if(*thisRegister & 1<<7) {
+ CB_GPIO_Out(LED4,*thisRegister & 1<<3);
}
- if(thisRegister & 1<<6) {
- CB_GPIO_Out(LED3,thisRegister & 1<<2);
+ if(*thisRegister & 1<<6) {
+ CB_GPIO_Out(LED3,*thisRegister & 1<<2);
}
- if(thisRegister & 1<<5) {
- CB_GPIO_Out(LED2,thisRegister & 1<<1);
+ if(*thisRegister & 1<<5) {
+ CB_GPIO_Out(LED2,*thisRegister & 1<<1);
}
- if(thisRegister & 1<<4) {
- CB_GPIO_Out(LED1,thisRegister & 1<<0);
+ if(*thisRegister & 1<<4) {
+ CB_GPIO_Out(LED1,*thisRegister & 1<<0);
}
break;
case 0x12:
// send single byte back to fpga!
- uart_byte_to_fpga( (uint8_t) (thisRegister&0x00FF));
+ uart_byte_to_fpga( (uint8_t) (*thisRegister&0x00FF));
break;
case 0x13:
CB_GPIO_Out_Lo(MUXADDR0);
CB_GPIO_Out_Lo(MUXADDR1);
CB_GPIO_Out_Lo(ZEROCALIB);
- CB_GPIO_Out_Hi(ADC_CONV0); // spi nCS -> idle Hi
+ CB_GPIO_Out_Lo(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 spi_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); // spi nCS -> idle Hi
}
void spi_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); // spi nCS -> idle Hi
}
void spi_writeTwoBytes(uint8_t byte1, uint8_t byte0){
NVIC_Init(&NVIC_InitStructure);
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
+// 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;