// Author : Jiri Kubias DCE CVUT
//
//
-// TODO : VBAT watch | warning messages
-// ms counter for led blink
////////////////////////////////////////////////////////////////////////////////
#include <lpc21xx.h> /* LPC21xx definitions */
#include <string.h>
#include <deb_led.h>
+#include <system_def.h>
#include <periph/can.h>
+#include <can_ids.h>
#include "pwrstep.h"
#define CAN_SPEED 1000000
#define CAN_ISR 0
#define ADC_ISR 1
+#define TIME_ISR 2
-// shoul use canid.h
-#define CAN_PWR 0x40
-#define CAN_PWR_ADC1 0x41
-#define CAN_PWR_ADC2 0x42
+#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
+#define BAT_CNT 10
+#define BAT_STAT_LOW 120000
+#define BAT_STAT_MAIN 110000
+#define BAT_STAT_BYE 105000
+#define V33_MIN 30000
+#define V50_MIN 45000
+#define V80_MIN 75000
+
+#define CAN_TIMEOUT 10
//extern unsigned int adc_val[4];
-#define TEST
+//#define TEST
+
+can_msg_t msg;// = {.flags = 0, .dlc = 1};
+
+unsigned int time_blink = 0;
+unsigned int time_send_can = 0;
+unsigned int time_alert = 0;
+
+unsigned int time_timeout = 0;
+
+unsigned int can_send_cnt = 0;
+
+void led_blik()
+{
+ if (time_blink == 0) time_blink = time_ms + 500;
+
+ if (time_ms > time_blink)
+ {
+ deb_led_change(LEDG);
+ time_blink = time_ms + 500;
+ }
-void dumb_wait(void)
+}
+
+void send_alert(unsigned char type )
{
- unsigned int i =2000000;
- while(--i);
+
+
+ msg.id = CAN_PWR_ALERT;
+ msg.flags = 0;
+ msg.dlc = 1;
+ msg.data[0] = type;
+
+ while (can_tx_msg(&msg));
+
+}
+
+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 (adc_val[0] < BAT_STAT_BYE) // bat < 9,5V
+ {
+ deb_led_on(LEDR);
+ send_alert(ALERT_BYE);
+ pwr_50(PWR_OFF);
+ //pwr_80(PWR_OFF);
+ pwr_33(PWR_OFF);
+
+
+ }
+ else if (adc_val[0] < BAT_STAT_MAIN) // bat < 12V
+ {
+ deb_led_on(LEDB);
+ ++cnt_10V;
+ if (cnt_10V > BAT_CNT)
+ {
+ send_alert(ALERT_MAIN);
+ pwr_50(PWR_OFF);
+ //pwr_80(PWR_OFF);
+ }
+
+
+ }
+ else if (adc_val[0] < BAT_STAT_LOW) // bat < 12V
+ {
+ deb_led_on(LEDY);
+ ++cnt_12V;
+ if (cnt_12V > BAT_CNT)
+ send_alert(ALERT_LOW);
+ }
+ else
+ deb_led_off(LEDY);
+
+ if (cnt_10V < BAT_CNT)
+ {
+ if (adc_val[3] < V80_MIN)
+ {
+ send_alert(ALERT_80V);
+ }
+
+ if (adc_val[2] < V50_MIN)
+ {
+ send_alert(ALERT_50V);
+ }
+
+ if (adc_val[1] < V33_MIN)
+ {
+ send_alert(ALERT_33V);
+ }
+ }
+ time_alert = time_ms + 500;
+ }
+}
+
+void send_can()
+{
+ if (time_send_can == 0) time_send_can = time_ms + 200;
+
+ if (time_ms > time_send_can)
+ {
+ deb_led_on(LEDB);
+
+ 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);
+
+ time_timeout = time_ms + CAN_TIMEOUT;
+ //while(can_tx_msg(&msg) & (time_timeout > time_ms))
+
+ while (can_tx_msg(&msg));
+
+
+ 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);
+
+ time_timeout = time_ms + CAN_TIMEOUT;
+ //while(can_tx_msg(&msg) & (time_timeout > time_ms))
+
+ while (can_tx_msg(&msg));
+ deb_led_off(LEDB);
+ time_send_can = time_ms + 500;
+ }
+
}
+
+
void can_rx(can_msg_t *msg) {
can_msg_t rx_msg;
}
+
void init_perip(void) // inicializace periferii mikroprocesoru
{
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_OFF);
#endif
-
-
+ pwr_33(PWR_ON);
+ pwr_50(PWR_ON);
+ pwr_80(PWR_ON);
}
+unsigned int time_delay = 0;
-can_msg_t msg;// = {.flags = 0, .dlc = 1};
int main (void) {
init_perip(); // sys init MCU
-
+ time_delay = time_ms + 1000;
+
+ while(time_ms < time_delay);
+
while(1)
{
- __deb_led_on(LEDG);
- dumb_wait();
- __deb_led_off(LEDG);
-
- dumb_wait();
-
-
-
-
-#ifndef TEST
-
- __deb_led_on(LEDB);
-
- 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);
-
-
-
- while(can_tx_msg(&msg));
-
-
- 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(&msg));
-
-
- __deb_led_off(LEDB);
-#endif
-
+ led_blik();
+ send_can(); //FIXME
+ power_alert();
}
}
#include <lpc21xx.h> // LPC21XX Peripheral Registers\r
#include <types.h> \r
+#include <deb_led.h>\r
+#include <system_def.h>\r
#include "pwrstep.h"\r
-
+\r
\r
#define PWR33 (1<<22) \r
#define PWR50 (1<<24)\r
#define ADCCH2 26\r
#define ADCCH3 28\r
\r
+#define TIME1MS ((CPU_APB_HZ) / 1000)\r
+\r
// tohla me byt definovano nekde jinde FIXME\r
\r
\r
\r
\r
\r
- adc_val[chan] = val * ADC_CON_CONST;\r
+ adc_val[chan] = (((val * ADC_CON_CONST + ADC_OFFSET) + adc_val[chan]) >> 1) ;\r
\r
ADCR &= ~(ADC_CR_START_OFF);\r
\r
void init_adc(unsigned rx_isr_vect)\r
{\r
\r
- PINSEL1 |= ((GPIO_SEL_1 << ADCCH0) | (GPIO_SEL_1 << ADCCH1) | (GPIO_SEL_1 << ADCCH2) | (GPIO_SEL_1 << ADCCH3)); \r
+ PINSEL1 |= ((PINSEL_1 << ADCCH0) | (PINSEL_1 << ADCCH1) | (PINSEL_1 << ADCCH2) | (PINSEL_1 << ADCCH3)); \r
\r
((uint32_t*)&VICVectCntl0)[rx_isr_vect] = 0x32;\r
((uint32_t*)&VICVectAddr0)[rx_isr_vect] = (unsigned) adc_isr;\r
\r
\r
\r
+void tc1 (void) __attribute__ ((interrupt));\r
+\r
+void tc1 (void) {\r
+ \r
+ time_ms +=1;\r
+ \r
+ \r
+\r
+ if (T1IR != 4)\r
+ { \r
+ __deb_led_on(LEDR); \r
+ } \r
+\r
+ T1IR = 4; // Vynulovani priznaku preruseni\r
+ VICVectAddr = 0; // Potvrzeni o obsluze preruseni\r
+}\r
+\r
+\r
+/* Setup the Timer Counter 1 Interrupt */\r
+void init_time (unsigned rx_isr_vect)\r
+{\r
+\r
+ \r
+\r
+\r
+\r
+ T1PR = 0;\r
+ T1MR2 = TIME1MS;\r
+ T1MCR = (3<<6); // interrupt on MR1\r
+\r
+ T1TCR = 1; // Starts Timer 1 \r
+\r
+ ((uint32_t*)&VICVectAddr0)[rx_isr_vect] = (unsigned long)tc1; // Nastaveni adresy vektotu preruseni\r
+ ((uint32_t*)&VICVectCntl0)[rx_isr_vect] = 0x20 | 0x5; // vyber casovece pro preruseni\r
+ VICIntEnable = (1<<5); // Povoli obsluhu preruseni\r
+}\r
+\r
+\r
+\r
+\r
\r
\r