]> oss.titaniummirror.com Git - rgblamp.git/commitdiff
Fixes from picbtnisr work
authorR. Steve McKown <rsmckown@gmail.com>
Sat, 10 Dec 2011 06:24:41 +0000 (23:24 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Sat, 10 Dec 2011 06:24:41 +0000 (23:24 -0700)
btn.c
btn.h
isr.c
isr.h
main.c
task.c
task.h
tmr.c
tmr.h

diff --git a/btn.c b/btn.c
index 7b3c75e70ff4593627c05b6179340c4affe29f44..101ec9bf6b24a22a3fbe8381aad7a2a66090fd93 100644 (file)
--- a/btn.c
+++ b/btn.c
@@ -29,22 +29,22 @@ void btn_isr()
 {
   if (IOCIF) {
     if (IOCBF0) {
-      _btn_pbdis();
+      btn_pbdis();
       tmr_start(TMR_BTN_PB, 1);
     }
     if (IOCBF4) {
-      _btn_rsdis();
+      btn_rsdis();
       tmr_start(TMR_BTN_RS, 1);
     }
     if (IOCBF5) {
-      _btn_rsdis();
+      btn_rsdis();
       tmr_start(TMR_BTN_RS, 1);
     }
   }
 
   if (tmr_fired(TMR_BTN_PB))
-    _task_post(TASK_BTN_PB);
+    task_post(TASK_BTN_PB);
   if (tmr_fired(TMR_BTN_RS))
-    _task_post(TASK_BTN_RS);
+    task_post(TASK_BTN_RS);
 }
 
diff --git a/btn.h b/btn.h
index 857ec6f39ccde14f3c2e6a51e8ebf8bda336ad3b..e0ce0aeb1498f8150d618ff331517ec91a9f5196 100644 (file)
--- a/btn.h
+++ b/btn.h
@@ -32,53 +32,33 @@ enum {
 /* Read the current state of the rocker switch */
 #define btn_rs() ((RB4 + ((unsigned char)RB5 << 1)) % 3)
 
-/* Enable the pushbutton from ISR */
-#define _btn_pben() do { \
-    IOCBP0 = 1; IOCBN0 = 1; IOCBF0 = 0; \
-  } while (0)
-
-/* Disable the pushbutton from ISR */
-#define _btn_pbdis() do { \
-    IOCBP0 = 0; IOCBN0 = 0; IOCBF0 = 0; \
-  } while (0)
-
-/* Enable the rocker switch from ISR */
-#define _btn_rsen() do { \
-    IOCBP4 = 1; IOCBN4 = 1; IOCBF4 = 0; \
-    IOCBP5 = 1; IOCBN5 = 1; IOCBF5 = 0; \
-  } while (0)
-
-/* Disable the rocker switch from ISR */
-#define _btn_rsdis() do { \
-    IOCBP4 = 0; IOCBN4 = 0; IOCBF4 = 0; \
-    IOCBP5 = 0; IOCBN5 = 0; IOCBF5 = 0; \
-  } while (0)
-
 /* Enable the pushbutton from user code */
 #define btn_pben() do { \
     ndi(); \
-    _btn_pben(); \
+    IOCBP0 = 1; IOCBN0 = 1; IOCBF0 = 0; \
     nei(); \
   } while (0)
 
 /* Disable the pushbutton from user code */
 #define btn_pbdis() do { \
     ndi(); \
-    _btn_pbdis(); \
+    IOCBP0 = 0; IOCBN0 = 0; IOCBF0 = 0; \
     nei(); \
   } while (0)
 
 /* Enable the rocker switch from user code */
 #define btn_rsen() do { \
     ndi(); \
-    _btn_rsen(); \
+    IOCBP4 = 1; IOCBN4 = 1; IOCBF4 = 0; \
+    IOCBP5 = 1; IOCBN5 = 1; IOCBF5 = 0; \
     nei(); \
   } while (0)
 
 /* Disable the rocker switch from user code */
 #define btn_rsdis() do { \
     ndi(); \
-    _btn_rsdis(); \
+    IOCBP4 = 0; IOCBN4 = 0; IOCBF4 = 0; \
+    IOCBP5 = 0; IOCBN5 = 0; IOCBF5 = 0; \
     nei(); \
   } while (0)
 
diff --git a/isr.c b/isr.c
index 78291457794134af01f760a09286284a1f79af4f..a1b9a491d3602259a8dc9396fcff7b793be95c10 100644 (file)
--- a/isr.c
+++ b/isr.c
@@ -10,8 +10,8 @@
 #include "btn.h"
 #include "task.h"
 
-bit isr_gie; /* Used to store the state of GIE for nested ndi()/nei() */
-unsigned char isr_di; /* Count of nested ndi() */
+bit _isr_gie; /* Used to store the state of GIE for nested ndi()/nei() */
+unsigned char _isr_di; /* Count of nested ndi() */
 
 void interrupt isr()
 {
@@ -19,13 +19,13 @@ void interrupt isr()
   btn_isr();
 
   if (tmr_fired(TMR_AUTO_OFFON))
-    _task_post(TASK_AUTO_OFFON);
+    task_post(TASK_AUTO_OFFON);
   if (tmr_fired(TMR_FADE))
-    _task_post(TASK_FADE);
+    task_post(TASK_FADE);
   if (tmr_fired(TMR_INCOLOR))
-    _task_post(TASK_INCOLOR);
+    task_post(TASK_INCOLOR);
 #if 0
   if (tmr_fired(TMR_DIM))
-    _task_post(TMR_DIM);
+    task_post(TMR_DIM);
 #endif
 }
diff --git a/isr.h b/isr.h
index 7b1ede094537344a26c3e2e8e73b4a03919c8d49..4b847740632b9036432607db89e009b99f50829c 100644 (file)
--- a/isr.h
+++ b/isr.h
@@ -9,26 +9,23 @@
 
 #include <htc.h>
 
-extern bit isr_gie;
-extern unsigned char isr_di;
+extern bit _isr_gie;
+extern unsigned char _isr_di;
 
-/* Nested disable interrupts inline function, for use outside ISR */
+/* Nested disable interrupts inline function.  Should be OK even in ISR. */
 #define ndi() \
   do { \
-    if (isr_di++ == 0) \
-      isr_gie = GIE; \
-      if (isr_gie) \
-        di(); \
+    if (_isr_di++ == 0) { \
+      _isr_gie = GIE; \
+      di(); \
+    } \
   } while (0)
 
-/* Nested enable interrupts inline function, for use outside ISR */
+/* Nested enable interrupts inline function.  Should be OK even in ISR. */
 #define nei() \
   do { \
-    if (--isr_di == 0) \
-      if (isr_gie) { \
-       isr_gie = 0; \
-        ei(); \
-      } \
+    if (--_isr_di == 0 && _isr_gie) \
+      ei(); \
   } while (0)
 
 void interrupt isr();
diff --git a/main.c b/main.c
index 637879f2cab0c18349df39617866dc8eea61c4fe..ae9d1e193855eefbe52532ad827c3f8e620cad19 100644 (file)
--- a/main.c
+++ b/main.c
@@ -264,5 +264,8 @@ int main(void)
 
   /* Process tasks forever */
   user_tasks(1);
+
+  /* Prevent return from main, which causes a device reset */
+  while (1);
 }
 
diff --git a/task.c b/task.c
index a3489248a6d6242d77392cea0e3d4d9557f06f0b..26a0b46a626f71e44a0db5135ed0a91aeaa38202 100644 (file)
--- a/task.c
+++ b/task.c
@@ -27,7 +27,7 @@ bit task_check()
 
 task_id_t task_get(unsigned char block)
 {
-  task_id_t t = 0;
+  task_id_t t = -1;
   unsigned long ids;
 
   do {
@@ -35,8 +35,8 @@ task_id_t task_get(unsigned char block)
     ids = _task_ids;
     nei();
     if (ids) {
-      for (unsigned char i = 0; t == 0 && i < TASK_COUNT; i++) {
-        if (ids & (1UL << _task_bitno))
+      for (unsigned char i = 0; t == -1 && i < TASK_COUNT; i++) {
+        if (bit_get(ids, _task_bitno))
           t = _task_bitno;
         _task_bitno = (_task_bitno + 1) % TASK_COUNT;
       }
@@ -45,14 +45,12 @@ task_id_t task_get(unsigned char block)
     else
       SLEEP();
 #endif
-  } while (t == 0 && block == 1);
+  } while (t == -1 && block == 1);
+  if (t) {
+    ndi();
+    bit_clr(_task_ids, t);
+    nei();
+  }
   return t;
 }
 
-void task_post(task_id_t t)
-{
-  ndi();
-  _task_post(t);
-  nei();
-}
-
diff --git a/task.h b/task.h
index f95d7159fa710ad879a7e330f76b840be8e82a6d..b53640f6ae946965f8d28a485dbf9d76f1898ef2 100644 (file)
--- a/task.h
+++ b/task.h
 #include "task_defs.h"
 #include "bit.h"
 
-typedef unsigned char task_id_t;
+typedef signed char task_id_t;
 
 /* Do not use directly; only for the 'inline' functions below */
 extern unsigned long _task_ids;
 extern task_id_t _task_bitno;
 
-/* Post task t from ISR */
-#define _task_post(t) do { _task_ids |= 1UL << t; } while (0)
+/* Post task t.  No need for ndi() since bit_set is a single instruction. */
+#define task_post(t) do { bit_set(_task_ids, t); } while (0)
 
 /* Initialize the task subsystem */
 #define task_init() do { _task_ids = 0; _task_bitno = 0; } while (0)
@@ -57,7 +57,4 @@ bit task_check();
  */
 task_id_t task_get(unsigned char block);
 
-/* Post task t from user code */
-void task_post(task_id_t t);
-
 #endif
diff --git a/tmr.c b/tmr.c
index aa3f58df722b644e007b1dc98118991827af5354..670d0e142398c57313573e21b8e9666bc814ce6e 100644 (file)
--- a/tmr.c
+++ b/tmr.c
@@ -15,7 +15,7 @@
 persistent tmr_time_t _tmr_ticks;
 tmr_bitno_t _tmr_on;
 tmr_bitno_t _tmr_periodic;
-static tmr_bitno_t _tmr_fired;
+static tmr_bitno_t _tmr_flag;
 static tmr_time_t _tmr_t0[TMR_COUNT];
 static tmr_time_t _tmr_elapsed[TMR_COUNT];
 
@@ -52,7 +52,7 @@ void tmr_start(tmr_bitno_t t, tmr_time_t elapsed)
   ndi();
   bit_set(_tmr_on, t);
   bit_clr(_tmr_periodic, t);
-  bit_clr(_tmr_fired, t);
+  bit_clr(_tmr_flag, t);
   _tmr_t0[t] = _tmr_ticks;
   _tmr_elapsed[t] = elapsed;
   nei();
@@ -63,8 +63,8 @@ void tmr_startAt(tmr_bitno_t t, tmr_time_t t0, tmr_time_t elapsed)
   ndi();
   bit_set(_tmr_on, t);
   bit_clr(_tmr_periodic, t);
-  bit_clr(_tmr_fired, t);
-  _tmr_t0[t] = t0 + elapsed;
+  bit_clr(_tmr_flag, t);
+  _tmr_t0[t] = t0;
   nei();
 }
 
@@ -73,8 +73,8 @@ void tmr_startPeriodic(tmr_bitno_t t, tmr_time_t elapsed)
   ndi();
   bit_set(_tmr_on, t);
   bit_set(_tmr_periodic, t);
-  bit_clr(_tmr_fired, t);
-  _tmr_t0[t] = _tmr_ticks + elapsed;
+  bit_clr(_tmr_flag, t);
+  _tmr_t0[t] = _tmr_ticks;
   _tmr_elapsed[t] = elapsed;
   nei();
 }
@@ -84,24 +84,22 @@ void tmr_startPeriodicAt(tmr_bitno_t t, tmr_time_t t0, tmr_time_t elapsed)
   ndi();
   bit_set(_tmr_on, t);
   bit_set(_tmr_periodic, t);
-  bit_clr(_tmr_fired, t);
-  _tmr_t0[t] = t0 + elapsed;
+  bit_clr(_tmr_flag, t);
+  _tmr_t0[t] = t0;
   _tmr_elapsed[t] = elapsed;
   nei();
 }
 
 bit tmr_fired(tmr_bitno_t t)
 {
-  /* FIXME: if called from ISR ndi()/nei() is not required */
-  static unsigned char last_fired;
   unsigned char fired;
 
   ndi();
-  fired = bit_get(_tmr_fired, t) != 0;
+  fired = bit_get(_tmr_flag, t);
   if (fired)
-    bit_clr(_tmr_fired, t);
+    bit_clr(_tmr_flag, t);
   nei();
-  return fired;
+  return fired != 0;
 }
 
 void tmr_isr()
@@ -110,8 +108,8 @@ void tmr_isr()
     TMR0IF = 0;
     _tmr_ticks++;
     for (tmr_bitno_t t = 0; t < TMR_COUNT; t++) {
-      if (_tmr_ticks - _tmr_t0[t] <= 0) {
-        bit_set(_tmr_fired, t);
+      if (bit_get(_tmr_on, t) && _tmr_ticks - _tmr_t0[t] <= _tmr_elapsed[t]) {
+        bit_set(_tmr_flag, t);
         if (bit_get(_tmr_periodic, t))
           _tmr_t0[t] += _tmr_elapsed[t];
         else
diff --git a/tmr.h b/tmr.h
index d748e949ee72f1a5318e816c11285903f47e9efc..b25e8ad25473b4145f3e971c9d61627e27a5e1a0 100644 (file)
--- a/tmr.h
+++ b/tmr.h
@@ -61,7 +61,7 @@ void tmr_init();
 /* FIXME: this may not be atomic WRT ISR */
 #define tmr_periodic(t) (bit_get(_tmr_periodic, (t)))
 
-/* Start a timer, expecting it to fire in elapsed ticks */
+/* Start a timer, expecting it to fire in elapsed ticks, user code */
 void tmr_start(tmr_bitno_t t, tmr_time_t elapsed);
 
 /* Start a timer, expecting it to fire in elapsed ticks from t0 */
@@ -81,7 +81,7 @@ void tmr_startPeriodicAt(tmr_bitno_t t, tmr_time_t t0, tmr_time_t elapsed);
     nei(); \
   } while(0)
 
-/* Return 1 if the timer has fired, resetting the fired bit */
+/* Return 1 if the timer has fired, resetting the fired bit, user code */
 bit tmr_fired(tmr_bitno_t t);
 
 /* Return the current number of timer ticks, for use outside ISR */
@@ -99,7 +99,7 @@ void tmr_isr();
 #define tmr_cwait(c) \
   do { \
     unsigned t0 = TMR0; \
-    while ((unsigned)(TMR0 - t0) <= c); /* cast prevents integral promotion */ \
+    while (TMR0 - t0 <= c); /* cast prevents integral promotion */ \
   } while (0)
 
 /* Wait for a number of us.  This is pretty accurate. */