From: Joseph Coffland Date: Sat, 24 Feb 2018 04:49:19 +0000 (-0800) Subject: - Fixed pin fault output. X-Git-Url: https://git.buildbotics.com/?a=commitdiff_plain;h=c53b7e2039d06a9e14c79213f6a462e9b1ee5a2a;p=bbctrl-firmware - Fixed pin fault output. - No longer using interupts for switch inputs. Debouncing on clock tick. - Updated DB25 M2 breakout diagram. - Enabled AVR watchdog. --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 52c0dbf..1fe4ecc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,15 @@ Buildbotics CNC Controller Firmware Change Log ============================================== +## v0.3.12 + - Updated DB25 M2 breakout diagram. + - Enabled AVR watchdog. + ## v0.3.11 - Supressed ``firmware rebooted`` warning. - Error on unexpected AVR reboot. - - Enabled switch input slew rate limiting. + - Fixed pin fault output. + - No longer using interupts for switch inputs. Debouncing on clock tick. ## v0.3.10 - Fixed "Flood" display, changed to "Load 1" and "Load 2". #108 diff --git a/package.json b/package.json index 18e229e..30d65d6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bbctrl", - "version": "0.3.11", + "version": "0.3.12", "homepage": "http://buildbotics.com/", "repository": "https://github.com/buildbotics/bbctrl-firmware", "license": "GPL-3.0+", diff --git a/src/avr/src/command.c b/src/avr/src/command.c index 8688abd..d11beab 100644 --- a/src/avr/src/command.c +++ b/src/avr/src/command.c @@ -43,7 +43,6 @@ #include "cpp_magic.h" #ifdef __AVR__ -#include #include #else #define ATOMIC_BLOCK(x) diff --git a/src/avr/src/config.h b/src/avr/src/config.h index 0687490..edc2584 100644 --- a/src/avr/src/config.h +++ b/src/avr/src/config.h @@ -100,7 +100,7 @@ enum { // Switch settings. See switch.c -#define SWITCH_INTLVL PORT_INT0LVL_MED_gc +#define SWITCH_DEBOUNCE 5 // ms // Motor ISRs diff --git a/src/avr/src/hardware.c b/src/avr/src/hardware.c index 54cfdc6..af919d7 100644 --- a/src/avr/src/hardware.c +++ b/src/avr/src/hardware.c @@ -127,8 +127,6 @@ void hardware_init() { void hw_request_hard_reset() {hw.hard_reset = true;} -/// Hard reset using watchdog timer -/// software hard reset using the watchdog timer void hw_hard_reset() { usart_flush(); cli(); @@ -148,21 +146,4 @@ void hw_reset_handler() { } -uint8_t hw_disable_watchdog() { - uint8_t state = WDT.CTRL; - wdt_disable(); - return state; -} - - -void hw_restore_watchdog(uint8_t state) { - cli(); - CCP = CCP_IOREG_gc; - WDT.CTRL = state | WDT_CEN_bm; - sei(); -} - - -const char *get_hw_id() { - return hw.id; -} +const char *get_hw_id() {return hw.id;} diff --git a/src/avr/src/hardware.h b/src/avr/src/hardware.h index 3bdef89..27407cb 100644 --- a/src/avr/src/hardware.h +++ b/src/avr/src/hardware.h @@ -36,6 +36,3 @@ void hardware_init(); void hw_request_hard_reset(); void hw_hard_reset(); void hw_reset_handler(); - -uint8_t hw_disable_watchdog(); -void hw_restore_watchdog(uint8_t state); diff --git a/src/avr/src/main.c b/src/avr/src/main.c index 94b7948..7fcc497 100644 --- a/src/avr/src/main.c +++ b/src/avr/src/main.c @@ -51,7 +51,7 @@ int main() { - //wdt_enable(WDTO_250MS); TODO + wdt_enable(WDTO_250MS); // Init cli(); // disable interrupts @@ -81,7 +81,6 @@ int main() { state_callback(); // manage state command_callback(); // process next command report_callback(); // report changes - wdt_reset(); } return 0; diff --git a/src/avr/src/rtc.c b/src/avr/src/rtc.c index 00d8217..edf9779 100644 --- a/src/avr/src/rtc.c +++ b/src/avr/src/rtc.c @@ -34,6 +34,7 @@ #include #include +#include #include @@ -48,6 +49,7 @@ ISR(RTC_OVF_vect) { hy_rtc_callback(); if (!(ticks & 255)) motor_rtc_callback(); lcd_rtc_callback(); + wdt_reset(); } diff --git a/src/avr/src/switch.c b/src/avr/src/switch.c index aee9346..6da31c7 100644 --- a/src/avr/src/switch.c +++ b/src/avr/src/switch.c @@ -39,7 +39,7 @@ typedef struct { switch_callback_t cb; bool state; - bool triggered; + int8_t debounce; } switch_t; @@ -66,49 +66,11 @@ static switch_t switches[] = { const int num_switches = sizeof(switches) / sizeof (switch_t); -static bool _read_state(const switch_t *s) {return IN_PIN(s->pin);} - - -static void _switch_isr() { - for (int i = 0; i < num_switches; i++) { - switch_t *s = &switches[i]; - if (s->type == SW_DISABLED || s->triggered) continue; - s->triggered = _read_state(s) != s->state; - if (s->triggered) PORT(s->pin)->INT0MASK &= ~BM(s->pin); // Disable INT0 - } -} - - -// Switch interrupt handler vectors -ISR(PORTA_INT0_vect) {_switch_isr();} -ISR(PORTB_INT0_vect) {_switch_isr();} -ISR(PORTC_INT0_vect) {_switch_isr();} -ISR(PORTD_INT0_vect) {_switch_isr();} -ISR(PORTE_INT0_vect) {_switch_isr();} -ISR(PORTF_INT0_vect) {_switch_isr();} - - -void _switch_enable(switch_t *s, bool enable) { - if (enable) { - s->triggered = false; - s->state = _read_state(s); // Initialize state - PORT(s->pin)->INT0MASK |= BM(s->pin); // Enable INT0 - - } else PORT(s->pin)->INT0MASK &= ~BM(s->pin); // Disable INT0 -} - - void switch_init() { for (int i = 0; i < num_switches; i++) { switch_t *s = &switches[i]; - - // Pull up, trigger on both edges and enable slew rate limiting - PINCTRL_PIN(s->pin) = - PORT_OPC_PULLUP_gc | PORT_ISC_BOTHEDGES_gc;// | PORT_SRLEN_bm; + PINCTRL_PIN(s->pin) = PORT_OPC_PULLUP_gc; // Pull up DIRCLR_PIN(s->pin); // Input - PORT(s->pin)->INTCTRL |= SWITCH_INTLVL; // Set interrupt level - - _switch_enable(s, s->type != SW_DISABLED); } } @@ -118,14 +80,14 @@ void switch_rtc_callback() { for (int i = 0; i < num_switches; i++) { switch_t *s = &switches[i]; - if (s->type == SW_DISABLED || !s->triggered) continue; - - bool state = _read_state(s); - s->triggered = false; - PORT(s->pin)->INT0MASK |= BM(s->pin); // Reenable INT0 + if (s->type == SW_DISABLED) continue; - if (state != s->state) { + // Debounce switch + bool state = IN_PIN(s->pin); + if (state == s->state) s->debounce = 0; + else if (++s->debounce == SWITCH_DEBOUNCE) { s->state = state; + s->debounce = 0; if (s->cb) s->cb(i, switch_is_active(i)); } } @@ -159,8 +121,10 @@ void switch_set_type(int index, switch_type_t type) { switch_t *s = &switches[index]; if (s->type != type) { + bool wasActive = switch_is_active(index); s->type = type; - _switch_enable(s, type != SW_DISABLED); + bool isActive = switch_is_active(index); + if (wasActive != isActive && s->cb) s->cb(index, isActive); } } diff --git a/src/avr/src/vars.c b/src/avr/src/vars.c index 8db1a36..736fca1 100644 --- a/src/avr/src/vars.c +++ b/src/avr/src/vars.c @@ -159,9 +159,6 @@ void vars_init() { void vars_report(bool full) { - // Save and disable watchdog - uint8_t wd_state = hw_disable_watchdog(); - bool reported = false; #define VAR(NAME, CODE, TYPE, INDEX, ...) \ @@ -190,9 +187,6 @@ void vars_report(bool full) { #undef VAR if (reported) printf("}\n"); - - // Restore watchdog - hw_restore_watchdog(wd_state); } void vars_report_all(bool enable) { @@ -334,9 +328,6 @@ void vars_print_json() { "\"help\":\"%"PRPSTR"\""; static const char index_fmt[] PROGMEM = ",\"index\":\"%s\""; - // Save and disable watchdog - uint8_t wd_state = hw_disable_watchdog(); - #define VAR(NAME, CODE, TYPE, INDEX, ...) \ if (first) first = false; else putchar(','); \ printf_P(fmt, #CODE, NAME##_name, type_get_##TYPE##_name_pgm(), \ @@ -345,9 +336,6 @@ void vars_print_json() { putchar('}'); #include "vars.def" #undef VAR - - // Restore watchdog - hw_restore_watchdog(wd_state); } diff --git a/src/avr/test/hal.c b/src/avr/test/hal.c index 3c3b1b6..6a081b5 100644 --- a/src/avr/test/hal.c +++ b/src/avr/test/hal.c @@ -63,8 +63,6 @@ typedef const char *pstring; float square(float x) {return x * x;} void i2c_set_read_callback(i2c_read_cb_t cb) {} void print_status_flags(uint8_t flags) {DEBUG_CALL();} -uint8_t hw_disable_watchdog() {return 0;} -void hw_restore_watchdog(uint8_t state) {} bool estop = false; diff --git a/src/resources/images/DB25-M2_breakout.png b/src/resources/images/DB25-M2_breakout.png index 3de7079..30906c2 100644 Binary files a/src/resources/images/DB25-M2_breakout.png and b/src/resources/images/DB25-M2_breakout.png differ