#include "periph_conf.h"
#include "misc_utils.h"
#include "version.h"
+#include "exti.h"
+//#define MAX_CURRENT 39720 //16 bit value for 2000mV -> 200mA // REF=3300mV
+#define MAX_CURRENT 65535
+#define MAX_CURRENT_A 36739 //185mA for analog part
+#define MAX_CURRENT_D 23830 //120mA for digital part
extern uint16_t SPI_DMA_Buffer[];
extern uint16_t SPI_DMA_Buffer_In[];
extern uint16_t uC_regs[];
+extern uint8_t irq_stateD0;
+extern uint8_t irq_stateA0;
+extern uint8_t irq_stateD1;
+extern uint8_t irq_stateA1;
-
-
-// prototypes for functions that are included in main.c
-
-// void SysTick_Handler(void);
-
+uint16_t system_start_cnt = 0;
+uint8_t system_start = 0;
+//uint16_t cycle_cnt = 0;
/* function prototypes */
SystemInit();
disable_JTAG(); //disable JTAG to free GPIO ports
init_CB_GPIO_Outputs();
-
-// configure timer interval
-// SysTick_Config(16777215); // standard
-// SysTick_Config(7200000); // 10 Hz interrupt
-// SysTick_Config(720000); // 100 hz
-// SysTick_Config(72000); // 1000 hz
-
+ init_GPIO_EXTI();
init_SPI1();
init_SPI2();
init_USART1();
init_USART3();
- delay_ms(1000);
- CB_GPIO_Out_Hi(LED4);
+ delay_ms(500);
+
+ //set maximum current value for discriminators
+ usart3_word_to_dac(((MAX_CURRENT&0xFFFF)>>4) | (0<<12));
+ usart3_word_to_dac(((MAX_CURRENT&0xFFFF)>>4) | (1<<12));
+ usart3_word_to_dac(((MAX_CURRENT&0xFFFF)>>4) | (4<<12));
+ usart3_word_to_dac(((MAX_CURRENT&0xFFFF)>>4) | (5<<12));
+
+ delay_ms(500);
+
+ //CB_GPIO_Out_Hi(LED4);
uC_regs[0x0b] |= (1<<0) | (0<<1) | (1<<2); // adc readout and converter board status is reported
// by default, but the readout of VDiscRef lines is disabled.
report_all_registers();
while(1) {
-
+
+ if (system_start == 1) system_start_cnt++;
+
if(BufferOut(&UcRegAddr)){
decode_register(UcRegAddr);
+ system_start = 1;
}else{
if(uC_regs[0x0b] & (1<<0)){ // ReadPwr bit set?
adc0_read_pwr();
if(uC_regs[0x0b] & (1<<2)){ // ReadStatus bit set?
SensorControlStatus();
}
- }
-
- }
-
- return 0;
-}
-
-
-
+ if (system_start_cnt >= 9000){ //wait for end of the JTAG programming (above 10 seconds)
+ system_start = 0;
+ system_start_cnt = 0;
+ CB_GPIO_Out_Hi(LED4);
+ //set proper thresholds
+ usart3_word_to_dac(((MAX_CURRENT_A&0xFFFF)>>4) | (0<<12));
+ usart3_word_to_dac(((MAX_CURRENT_D&0xFFFF)>>4) | (1<<12));
+ usart3_word_to_dac(((MAX_CURRENT_A&0xFFFF)>>4) | (4<<12));
+ usart3_word_to_dac(((MAX_CURRENT_D&0xFFFF)>>4) | (5<<12));
+ }
+ }
+
+ //chip_0
+ if ((CB_GPIO_In_State(OVCA_C0)==0) && (CB_GPIO_In_State(OVCD_C0)==0) && (irq_stateD0 == 1 || irq_stateA0 == 1)) {
+ system_start_cnt = 0;
+ system_start = 0;
+ CB_GPIO_Out_Lo(LED4);
+ usart3_word_to_dac(((MAX_CURRENT&0xFFFF)>>4) | (0<<12));
+ usart3_word_to_dac(((MAX_CURRENT&0xFFFF)>>4) | (1<<12));
+ CB_GPIO_Out_Hi(SENSOREN0);
+ CB_GPIO_Out_Hi(JTAGEN0);
+ irq_stateD0 = 0;
+ irq_stateA0 = 0;
+ }
-
-
-/*
-void SysTick_Handler(void)
-{
- //do something
-
+ //chip_1
+ if ((CB_GPIO_In_State(OVCA_C1)==0) && (CB_GPIO_In_State(OVCD_C1)==0) && (irq_stateD1 == 1 || irq_stateA1 == 1)) {
+ system_start_cnt = 0;
+ system_start = 0;
+ CB_GPIO_Out_Lo(LED4);
+ usart3_word_to_dac(((MAX_CURRENT&0xFFFF)>>4) | (4<<12));
+ usart3_word_to_dac(((MAX_CURRENT&0xFFFF)>>4) | (5<<12));
+ CB_GPIO_Out_Hi(SENSOREN1);
+ CB_GPIO_Out_Hi(JTAGEN1);
+ irq_stateD1 = 0;
+ irq_stateA1 = 0;
+ }
+ } //end of while loop
+ return 0;
}
-*/
-
-
-
-
-
-
-
-
-
-
/* Handles interrupt from USART1 */
void USART1_IRQHandler(void)