#include <periph/can.h>
#include <can_ids.h>
-// #include <can/canmsg.h>
-// #include <can/lpcan.h>
-// #include <can/lpcan_vca.h>
-
#include "pwrstep.h"
#include "uart.h"
#define CAN_SPEED 1000000
+
#define CAN_ISR 0
#define ADC_ISR 1
#define TIME_ISR 2
+#define TIME1MS ((CPU_APB_HZ) / 1000)
+
+
#define CAN_TRY 20
-#define ALERT_LOW 1
-#define ALERT_MAIN 2
-#define ALERT_BYE 3
-#define ALERT_33V 4
-#define ALERT_50V 5
-#define ALERT_80V 6
+enum {
+ ALERT_LOW = 1,
+ ALERT_MAIN,
+ ALERT_BYE,
+ ALERT_33V,
+ ALERT_50V,
+ ALERT_80V
+} alert_states;
+
+
+
+#define TIME_COUNT 4
+#define LEDG_TIME 500
+#define CAN_REPORT_TIME 500
+#define CAN_ALERT_TIME 200
+#define CAN_TIMEOUT_TIME 50
+enum {
+ TIM_CAN_REPORT = 0,
+ TIM_CAN_ALERT,
+ TIM_LEDG,
+ TIM_CAN_TIMEOUT
+}time;
+
+
#define BAT_CNT 10
#define BAT_STAT_LOW 120000
#define CAN_TIMEOUT 10
-//extern unsigned int adc_val[4];
+
+
//#define TEST
-can_msg_t msg;// = {.flags = 0, .dlc = 1};
+can_msg_t msg;
-unsigned int time_blink = 0;
-unsigned int time_send_can2 = 0;
-unsigned int time_send_can1 = 0;
-unsigned int time_alert = 0;
+volatile uint32_t dev_time[TIME_COUNT];
-unsigned int time_timeout = 0;
-
-unsigned int can_send_cnt = 0;
+unsigned int cnt_12V;
+unsigned int cnt_10V;
void led_blik()
{
- if (time_blink == 0) time_blink = time_ms + 500;
-
- if (time_ms > time_blink)
+
+ if (dev_time[TIM_LEDG] == 0)
{
deb_led_change(LEDG);
- time_blink = time_ms + 500;
+ dev_time[TIM_LEDG] = LEDG_TIME;
}
}
msg.dlc = 1;
msg.data[0] = type;
- time_timeout = time_ms + CAN_TIMEOUT;
- while(can_tx_msg(&msg) && (time_timeout < time_ms));
- //while (can_tx_msg(&msg));
+ dev_time[TIM_CAN_TIMEOUT] = CAN_TIMEOUT_TIME;
+ while(can_tx_msg(&msg) && (dev_time[TIM_CAN_TIMEOUT] != 0));
}
-
-unsigned int cnt_12V;
-unsigned int cnt_10V;
-
-
void power_alert()
{
- if (time_alert == 0) time_alert = time_ms + 200;
-
- if (time_ms > time_alert)
+ if (dev_time[TIM_CAN_ALERT] == 0)
{
if (adc_val[0] < BAT_STAT_BYE) // bat < 9,5V
send_alert(ALERT_33V);
}
}
- time_alert = time_ms + 500;
+ dev_time[TIM_CAN_ALERT] = CAN_ALERT_TIME;
}
}
-void send_can2(void);
-uint8_t release = 0;
-
-void send_can1()
+void send_can()
{
- if (time_send_can1 == 0) time_send_can1 = time_ms + 200;
-
- if (time_ms > time_send_can1)
+ if (dev_time[TIM_CAN_REPORT] == 0)
{
- uint32_t timeout = time_ms;
- can_msg_t msg1;
deb_led_on(LEDB);
- msg1.id = CAN_PWR_ADC1;
- msg1.flags = 0;
- msg1.dlc = 8;
- msg1.data[0] = (((adc_val[0]) >> 24) & 0xFF);
- msg1.data[1] = (((adc_val[0]) >> 16) & 0xFF);
- msg1.data[2] = (((adc_val[0]) >> 8) & 0xFF);
- msg1.data[3] = (((adc_val[0]) >> 0) & 0xFF);
- msg1.data[4] = (((adc_val[1]) >> 24) & 0xFF);
- msg1.data[5] = (((adc_val[1]) >> 16) & 0xFF);
- msg1.data[6] = (((adc_val[1]) >> 8) & 0xFF);
- msg1.data[7] = (((adc_val[1]) >> 0) & 0xFF);
-
-// msg1.data[0] = 0;
-// msg1.data[1] = 1;
-// msg1.data[2] = 2;
-// msg1.data[3] = 3;
-// msg1.data[4] = 4;
-// msg1.data[5] = 5;
-// msg1.data[6] = 6;
-// msg1.data[7] = 7;
-
- //while( & ( (timeout + 300) < time_ms));
- can_tx_msg(&msg1);
-
-
- time_send_can1 = time_ms + 500;
+ msg.id = CAN_PWR_ADC1;
+ msg.flags = 0;
+ msg.dlc = 8;
+ msg.data[0] = (((adc_val[0]) >> 24) & 0xFF);
+ msg.data[1] = (((adc_val[0]) >> 16) & 0xFF);
+ msg.data[2] = (((adc_val[0]) >> 8) & 0xFF);
+ msg.data[3] = (((adc_val[0]) >> 0) & 0xFF);
+ msg.data[4] = (((adc_val[1]) >> 24) & 0xFF);
+ msg.data[5] = (((adc_val[1]) >> 16) & 0xFF);
+ msg.data[6] = (((adc_val[1]) >> 8) & 0xFF);
+ msg.data[7] = (((adc_val[1]) >> 0) & 0xFF);
+
- deb_led_off(LEDB);
- release = 1;
- }
-}
-
-void send_can2(void)
-{
- if (time_send_can2 == 0) time_send_can2 = time_ms + 205;
-
- if (time_ms > time_send_can2)
- {
- uint32_t timeout = time_ms;
- deb_led_on(LEDB);
- can_msg_t msg2;
-
- msg2.id = CAN_PWR_ADC2;
- msg2.flags = 0;
- msg2.dlc = 8;
- msg2.data[0] = (((adc_val[2]) >> 24) & 0xFF);
- msg2.data[1] = (((adc_val[2]) >> 16) & 0xFF);
- msg2.data[2] = (((adc_val[2]) >> 8) & 0xFF);
- msg2.data[3] = (((adc_val[2]) >> 0) & 0xFF);
- msg2.data[4] = (((adc_val[3]) >> 24) & 0xFF);
- msg2.data[5] = (((adc_val[3]) >> 16) & 0xFF);
- msg2.data[6] = (((adc_val[3]) >> 8) & 0xFF);
- msg2.data[7] = (((adc_val[3]) >> 0) & 0xFF);
-/*
- msg2.data[0] = 8;
- msg2.data[1] = 9;
- msg2.data[2] = 10;
- msg2.data[3] = 11;
- msg2.data[4] = 12;
- msg2.data[5] = 13;
- msg2.data[6] = 14;
- msg2.data[7] = 15;*/
-
+ dev_time[TIM_CAN_TIMEOUT] = CAN_TIMEOUT_TIME;
+ while(can_tx_msg(&msg) && (dev_time[TIM_CAN_TIMEOUT] != 0));
+
+ msg.id = CAN_PWR_ADC2;
+ msg.flags = 0;
+ msg.dlc = 8;
+ msg.data[0] = (((adc_val[2]) >> 24) & 0xFF);
+ msg.data[1] = (((adc_val[2]) >> 16) & 0xFF);
+ msg.data[2] = (((adc_val[2]) >> 8) & 0xFF);
+ msg.data[3] = (((adc_val[2]) >> 0) & 0xFF);
+ msg.data[4] = (((adc_val[3]) >> 24) & 0xFF);
+ msg.data[5] = (((adc_val[3]) >> 16) & 0xFF);
+ msg.data[6] = (((adc_val[3]) >> 8) & 0xFF);
+ msg.data[7] = (((adc_val[3]) >> 0) & 0xFF);
- //while(can_tx_msg(&msg2) & ((timeout + 300) < time_ms));
+ dev_time[TIM_CAN_TIMEOUT] = CAN_TIMEOUT_TIME;
+ while(can_tx_msg(&msg) && (dev_time[TIM_CAN_TIMEOUT] != 0));
- can_tx_msg(&msg2);
deb_led_off(LEDB);
-
- time_send_can2 = time_ms + 510;
+ dev_time[TIM_CAN_REPORT] = CAN_REPORT_TIME;
}
}
-void init_perip(void) // inicializace periferii mikroprocesoru
+
+
+void tc1 (void) __attribute__ ((interrupt));
+void tc1 (void) {
+
+ uint32_t i;
+
+ for (i = 0; i < TIME_COUNT; i++)
+ {
+ if(dev_time[i] != 0)
+ --dev_time[i];
+ }
+
+ T1IR = 4; // Vynulovani priznaku preruseni
+ VICVectAddr = 0; // Potvrzeni o obsluze preruseni
+}
+
+
+/* Setup the Timer Counter 1 Interrupt */
+void init_time (unsigned rx_isr_vect)
{
+ T1PR = 0;
+ T1MR2 = TIME1MS;
+ T1MCR = (3<<6); // interrupt on MR1
+ T1TCR = 1; // Starts Timer 1
+
+ ((uint32_t*)&VICVectAddr0)[rx_isr_vect] = (unsigned long)tc1; // Nastaveni adresy vektotu preruseni
+ ((uint32_t*)&VICVectCntl0)[rx_isr_vect] = 0x20 | 0x5; // vyber casovece pro preruseni
+ VICIntEnable = (1<<5); // Povoli obsluhu preruseni
+}
+
+
+
+void init_perip(void) // inicializace periferii mikroprocesoru
+{
+ int i;
+ for (i = 0; i < TIME_COUNT; i++)
+ dev_time[i] = 0;
+
init_pwr();
can_init_baudrate(CAN_SPEED, CAN_ISR, can_rx);
init_adc(ADC_ISR);
init_time (TIME_ISR);
init_uart0((int)9600 ,UART_BITS_8, UART_STOP_BIT_1, UART_PARIT_OFF, 0 );
+
+#ifdef TEST
pwr_33(PWR_ON);
pwr_50(PWR_ON);
pwr_80(PWR_ON);
-}
-
-
-unsigned int time_delay = 0;
-
+#else
+ pwr_33(PWR_OFF);
+ pwr_50(PWR_OFF);
+ pwr_80(PWR_OFF);
+#endif
-int main (void) {
+ pwr_33(PWR_ON);
+ pwr_50(PWR_ON);
+ pwr_80(PWR_ON);
+}
+int main (void)
+{
init_perip(); // sys init MCU
- time_delay = time_ms + 1000;
+ dev_time[TIM_LEDG] = 1000;
+ while(dev_time[TIM_LEDG]);
- while(time_ms < time_delay);
while(1)
{
led_blik();
- send_can1(); //FIXME
- send_can2();
- power_alert(); //FIXME
+ send_can();
+ power_alert();
}
}
-
-
-