- Fixed pin fault output.
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Sat, 24 Feb 2018 04:49:19 +0000 (20:49 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Sat, 24 Feb 2018 04:49:19 +0000 (20:49 -0800)
 - No longer using interupts for switch inputs.  Debouncing on clock tick.
 - Updated DB25 M2 breakout diagram.
 - Enabled AVR watchdog.

12 files changed:
CHANGELOG.md
package.json
src/avr/src/command.c
src/avr/src/config.h
src/avr/src/hardware.c
src/avr/src/hardware.h
src/avr/src/main.c
src/avr/src/rtc.c
src/avr/src/switch.c
src/avr/src/vars.c
src/avr/test/hal.c
src/resources/images/DB25-M2_breakout.png

index 52c0dbfa70c4f06de7ba214d4fa67be92c7bbaa1..1fe4ecc0df8126d93038f2f278e80c175c61903d 100644 (file)
@@ -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
index 18e229ed411cef18333f78694f70595b9cac8c98..30d65d6fd5e76373a8256341692b772fba72e9dc 100644 (file)
@@ -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+",
index 8688abd9c2e54d3f8ea341e9141fb2029189b19f..d11beab090af18502235a682fc3a06c012fa3724 100644 (file)
@@ -43,7 +43,6 @@
 #include "cpp_magic.h"
 
 #ifdef __AVR__
-#include <avr/wdt.h>
 #include <util/atomic.h>
 #else
 #define ATOMIC_BLOCK(x)
index 06874900a214b7f234c06356ec2e6bdd64a944e4..edc2584c479d169a1e2ebb87398c3a93a491f20f 100644 (file)
@@ -100,7 +100,7 @@ enum {
 
 
 // Switch settings.  See switch.c
-#define SWITCH_INTLVL  PORT_INT0LVL_MED_gc
+#define SWITCH_DEBOUNCE 5 // ms
 
 
 // Motor ISRs
index 54cfdc69a713fafddd55a79e0ad8039d49001c81..af919d774b53da5262c1c4d3c962ebd8fe618584 100644 (file)
@@ -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;}
index 3bdef8965b8f3b8f2f9552a4fd1126117c3bc422..27407cb61b27a730cae96c75f2a3802639595238 100644 (file)
@@ -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);
index 94b7948ee61d268610e690251a94ffb87a1e2886..7fcc49725922bae20b0336e6b8bc38a3c690ba91 100644 (file)
@@ -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;
index 00d8217cce0fe49788d28ef8fc18ff172c28b2a6..edf9779725a5b963d843273700f500b929bf691f 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <avr/io.h>
 #include <avr/interrupt.h>
+#include <avr/wdt.h>
 
 #include <string.h>
 
@@ -48,6 +49,7 @@ ISR(RTC_OVF_vect) {
   hy_rtc_callback();
   if (!(ticks & 255)) motor_rtc_callback();
   lcd_rtc_callback();
+  wdt_reset();
 }
 
 
index aee93464d3d525b459d42119fdd0015ec651a52e..6da31c7147cc1384e741e5e93fb91f383a7dace5 100644 (file)
@@ -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);
   }
 }
 
index 8db1a364863e91d80dc7677c531f14f7e76e2e4f..736fca1e1d0a458c9ce1a4dc7516b15c6709e589 100644 (file)
@@ -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);
 }
 
 
index 3c3b1b677f88beb930864326807ee405ba9ed59e..6a081b5f0633a7d60f204e8ee0ad344a586bdb16 100644 (file)
@@ -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;
index 3de7079a4a056dbfac5d573ff81fab2686e81b3c..30906c2bc2650580f3bea52a67e77f68330fa150 100644 (file)
Binary files a/src/resources/images/DB25-M2_breakout.png and b/src/resources/images/DB25-M2_breakout.png differ