diff --git a/firmware/led.c b/firmware/led.c index 4ea1e0d..19b52fa 100644 --- a/firmware/led.c +++ b/firmware/led.c @@ -1,8 +1,39 @@ #include "config.h" #include "led.h" +int16_t __led_pattern; + +/* Debug LED */ +void TISR_HANDLER(6) +{ + timer_clear_txif(6); + + if (__led_pattern == -1) + return; + + __led_pattern <<= 1; + if (__led_pattern & 0x100) { + PORTBbits.RB13 = 0; /* on */ + __led_pattern |= 1; + } else { + PORTBbits.RB13 = 1; /* off */ + } + + __led_pattern &= 0xff; +} + void led_init(void) { TRISBbits.TRISB13 = 0; PORTBbits.RB13 = 1; + __led_pattern = -1; + + timer_setup_16bit(6, LED_BLINK_RATE, 1); + timer_set_priority(6, 1); /* low priority */ +} + +/* Set a pattern (8-bit binary pattern). */ +void led_pattern(int8_t pattern) +{ + __led_pattern = pattern; } diff --git a/firmware/led.h b/firmware/led.h index b282df8..f64d6ef 100644 --- a/firmware/led.h +++ b/firmware/led.h @@ -3,16 +3,24 @@ #include "config.h" +#define LED_BLINK_RATE 4 +extern int16_t __led_pattern; + /* Initialize LED */ void led_init(void); +/* Set a pattern (8-bit binary pattern). */ +void led_pattern(int8_t pattern); + static inline void led_on(void) { + __led_pattern = -1; PORTBbits.RB13 = 0; } static inline void led_off(void) { + __led_pattern = -1; PORTBbits.RB13 = 1; } diff --git a/firmware/mode_debug.c b/firmware/mode_debug.c index 5c8625e..70d502e 100644 --- a/firmware/mode_debug.c +++ b/firmware/mode_debug.c @@ -11,21 +11,6 @@ #include "mode.h" #include "zoom.h" -#define DEBUG_BLINK_RATE 4 - -/* Debug LED */ -void TISR_HANDLER(6) -{ - static int toggle = 0; - timer_clear_txif(6); - - toggle = !toggle; - if (toggle) - led_on(); - else - led_off(); -} - static uint16_t dac = 32768; #define TIMER_RATE 8000 @@ -86,8 +71,10 @@ void run_debug(void) char buf[4]; uart1_init(115200); - timer_setup_16bit(6, DEBUG_BLINK_RATE, 1); timer_setup_16bit(7, TIMER_RATE, 1); + timer_set_priority(7, 6); + + led_pattern(0x10101010); uart1_put_string("Zoom NILM Debug\r\n"); diff --git a/firmware/mode_normal.c b/firmware/mode_normal.c index 4a4f689..87a6eaf 100644 --- a/firmware/mode_normal.c +++ b/firmware/mode_normal.c @@ -89,6 +89,7 @@ void send_to_pc(uint16_t adc, uint16_t dac) void run_normal(void) { uart1_init(500000); + led_on(); /* Assume startup current is 0 */ dac_current = 0.0; @@ -97,6 +98,7 @@ void run_normal(void) degauss(); timer_setup_16bit(5, TIMER_RATE, 1); + timer_set_priority(5, 6); while(1) { if (send_data) { diff --git a/firmware/timer.h b/firmware/timer.h index 45ff427..9454712 100644 --- a/firmware/timer.h +++ b/firmware/timer.h @@ -14,17 +14,31 @@ int timer_setup_16bit(int timer, uint32_t freq, int ie); __attribute__((__interrupt__,auto_psv)) _T##x##Interrupt(void) /* Clear TxIF corresponding to a timer */ -#define timer_clear_txif(timer) do { \ - if ((timer) == 1) IFS0bits.T1IF = 0; \ - if ((timer) == 2) IFS0bits.T2IF = 0; \ - if ((timer) == 3) IFS0bits.T3IF = 0; \ - if ((timer) == 4) IFS1bits.T4IF = 0; \ - if ((timer) == 5) IFS1bits.T5IF = 0; \ - if ((timer) == 6) IFS2bits.T6IF = 0; \ - if ((timer) == 7) IFS3bits.T7IF = 0; \ - if ((timer) == 8) IFS3bits.T8IF = 0; \ - if ((timer) == 9) IFS3bits.T9IF = 0; \ -} while(0) +#define timer_clear_txif(timer) do { \ + if ((timer) == 1) IFS0bits.T1IF = 0; \ + if ((timer) == 2) IFS0bits.T2IF = 0; \ + if ((timer) == 3) IFS0bits.T3IF = 0; \ + if ((timer) == 4) IFS1bits.T4IF = 0; \ + if ((timer) == 5) IFS1bits.T5IF = 0; \ + if ((timer) == 6) IFS2bits.T6IF = 0; \ + if ((timer) == 7) IFS3bits.T7IF = 0; \ + if ((timer) == 8) IFS3bits.T8IF = 0; \ + if ((timer) == 9) IFS3bits.T9IF = 0; \ + } while(0) + + +/* Set timer interrupt priority, 1-7. Default is 4, 7 is highest */ +#define timer_set_priority(timer, pri) do { \ + if ((timer) == 1) IPC0bits.T1IP = (pri); \ + if ((timer) == 2) IPC1bits.T2IP = (pri); \ + if ((timer) == 3) IPC2bits.T3IP = (pri); \ + if ((timer) == 4) IPC6bits.T4IP = (pri); \ + if ((timer) == 5) IPC7bits.T5IP = (pri); \ + if ((timer) == 6) IPC11bits.T6IP = (pri); \ + if ((timer) == 7) IPC12bits.T7IP = (pri); \ + if ((timer) == 8) IPC12bits.T8IP = (pri); \ + if ((timer) == 9) IPC13bits.T9IP = (pri); \ + } while(0) /* sleep for between "ms" and "ms+1" milliseconds */ void msleep(int ms);