]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
Moved eb_pwr from sysless-lpc21xx
authorMichal Sojka <sojkam1@fel.cvut.cz>
Mon, 28 Jul 2008 11:46:24 +0000 (13:46 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Mon, 28 Jul 2008 11:46:24 +0000 (13:46 +0200)
src/eb_pwr/main.c
src/eb_pwr/pwrstep.c
src/eb_pwr/pwrstep.h
src/eb_pwr/uart.c

index 81658692415b4b6b014d3f3a77ae60f3039512c7..dd171a0bc7e39dccfbf28460c2c27db031632853 100644 (file)
@@ -8,14 +8,14 @@
 // 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;
        
@@ -65,14 +223,17 @@ void can_rx(can_msg_t *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);
@@ -83,69 +244,29 @@ void init_perip(void)         // inicializace periferii mikroprocesoru
        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();
 
        } 
 }
index 75756eaaaebfbe45c99b92b3d3d7a0e84a440e0b..dce248f38435c3c554be62e1add144d030e681f1 100644 (file)
@@ -1,7 +1,9 @@
 #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
@@ -19,6 +21,8 @@
 #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
@@ -119,7 +123,7 @@ void adc_isr(void)
        \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
@@ -151,7 +155,7 @@ void adc_isr(void)
 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
@@ -179,5 +183,45 @@ void init_pwr(void)   // init power lines
 \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
index b0fe888d3bc71cea3620d84905688e2417790f18..0995a8693413d7cce41952c8a00cf48fe05d250c 100644 (file)
@@ -5,10 +5,11 @@
 #define PWR_OFF        0\r
 \r
 #define ADC_DIV 10\r
-#define ADC_CON_CONST 322
+#define ADC_CON_CONST 322\r
+#define ADC_OFFSET     2000\r
 \r
 \r
-       unsigned int adc_val[4];\r
+       volatile unsigned int adc_val[4];\r
 \r
 /**    pwr_33 Switch on/off 3,3V power line\r
  *  @param mode  -  PWR_ON/PWR_OFF \r
@@ -35,4 +36,13 @@ void init_pwr(void);
  */\r
 void init_adc(unsigned rx_isr_vect);\r
 \r
+/** gobal time\r
+ *  @note incremented twenty 20ms, overrun every 1194hours\r
+ */\r
+volatile unsigned int time_ms;\r
+\r
+/**    inicializes time counter (use ISR)\r
+ *  @param rx_isr_vect  ISR vector\r
+ */\r
+void init_time (unsigned rx_isr_vect);\r
 #endif\r
index 9b72f54d2546f7f6f60b1922028182d9d5476496..5b76f639d8fbabc841caae0a1a44643e7dff231d 100644 (file)
@@ -14,7 +14,7 @@ void init_uart0(int baudrate, char bits, char stopbit, char parit_en, char parit
 \r
        int pom , vala, valb;\r
 \r
-       PINSEL0 |= ( GPIO_SEL_1<< 0) | ( GPIO_SEL_1 << 2);     \r
+       PINSEL0 |= ( PINSEL_1<< 0) | ( PINSEL_1 << 2);     \r
 \r
        U0LCR =UART_DLAB | bits | stopbit | parit_en | parit_mode;        // nastaven� datov�ho slova na 8 bit� a jeden stop bit bez parity, nastaven� DLAB na log "1"\r
        \r
@@ -71,7 +71,7 @@ void init_uart1(int baudrate, char bits, char stopbit, char parit_en, char parit
 \r
        int pom , vala, valb;\r
 \r
-       PINSEL0 |= ( GPIO_SEL_1<< 16) | ( GPIO_SEL_1 << 18);     \r
+       PINSEL0 |= ( PINSEL_1<< 16) | ( PINSEL_1 << 18);     \r
 \r
        U1LCR =UART_DLAB | bits | stopbit | parit_en | parit_mode;        // nastaven� datov�ho slova na 8 bit� a jeden stop bit bez parity, nastaven� DLAB na log "1"\r
        \r