move on_second_tick callout to on_idle event, also provide a flag to detect interrupt...
authorMichael Moon <triffid.hunter@gmail.com>
Tue, 15 Jan 2013 04:22:35 +0000 (15:22 +1100)
committerMichael Moon <triffid.hunter@gmail.com>
Tue, 15 Jan 2013 04:59:13 +0000 (15:59 +1100)
src/libs/Kernel.cpp
src/libs/SlowTicker.cpp
src/libs/SlowTicker.h
src/libs/StepTicker.cpp
src/libs/utils.cpp
src/libs/utils.h
src/main.cpp

index 420fdb4..cd067ee 100644 (file)
@@ -68,7 +68,7 @@ Kernel::Kernel(){
     this->add_module( this->serial );
 
     // HAL stuff
-    this->slow_ticker          = new SlowTicker();
+    add_module( this->slow_ticker          = new SlowTicker());
     this->step_ticker          = new StepTicker();
     this->adc                  = new Adc();
     this->digipot              = new Digipot();
index 0673f26..1461aef 100644 (file)
@@ -32,6 +32,11 @@ SlowTicker::SlowTicker(){
     flag_1s_count = SystemCoreClock;
 }
 
+void SlowTicker::on_module_loaded()
+{
+    register_for_event(ON_IDLE);
+}
+
 void SlowTicker::set_frequency( int frequency ){
     this->interval = int(floor((SystemCoreClock >> 2)/frequency));   // SystemCoreClock/4 = Timer increments in a second
     LPC_TIM2->MR0 = this->interval;
@@ -41,6 +46,8 @@ void SlowTicker::set_frequency( int frequency ){
 
 void SlowTicker::tick()
 {
+    _isr_context = true;
+
     LPC_GPIO1->FIODIR |= 1<<20;
     LPC_GPIO1->FIOSET = 1<<20;
 
@@ -65,6 +72,8 @@ void SlowTicker::tick()
 
     if (ispbtn.get() == 0)
         __debugbreak();
+
+    _isr_context = false;
 }
 
 bool SlowTicker::flag_1s(){
@@ -79,6 +88,12 @@ bool SlowTicker::flag_1s(){
     return false;
 }
 
+void SlowTicker::on_idle(void*)
+{
+    if (flag_1s())
+        kernel->call_event(ON_SECOND_TICK);
+}
+
 extern "C" void TIMER2_IRQHandler (void){
     if((LPC_TIM2->IR >> 0) & 1){  // If interrupt register set for MR0
         LPC_TIM2->IR |= 1 << 0;   // Reset it
index 393dbcf..a288864 100644 (file)
@@ -25,6 +25,10 @@ using namespace std;
 class SlowTicker : public Module{
     public:
         SlowTicker();
+
+        void on_module_loaded(void);
+        void on_idle(void*);
+
         void set_frequency( int frequency );
         void tick();
         // For some reason this can't go in the .cpp, see :  http://mbed.org/forum/mbed/topic/2774/?page=1#comment-14221
index 65470cf..38a372f 100644 (file)
@@ -77,6 +77,8 @@ StepperMotor* StepTicker::add_stepper_motor(StepperMotor* stepper_motor){
 
 // Call tick() on each active motor
 inline void StepTicker::tick(){
+    _isr_context = true;
+
     int i;
     uint32_t bm;
     for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
@@ -86,11 +88,15 @@ inline void StepTicker::tick(){
             this->active_motors[i]->tick();
         }
     }
+
+    _isr_context = false;
 }
 
 // Call signal_mode_finished() on each active motor that asked to be signaled. We do this instead of inside of tick() so that
 // all tick()s are called before we do the move finishing
 void StepTicker::signal_moves_finished(){
+    _isr_context = true;
+
     int i;
     uint32_t bm;
     for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
@@ -112,10 +118,14 @@ void StepTicker::signal_moves_finished(){
         }
     }
     this->moves_finished = false;
+
+    _isr_context = false;
 }
 
 // Reset step pins on all active motors
 inline void StepTicker::reset_tick(){
+    _isr_context = true;
+
     int i;
     uint32_t bm;
     for (i = 0, bm = 1; i < 12; i++, bm <<= 1)
@@ -123,6 +133,8 @@ inline void StepTicker::reset_tick(){
         if (this->active_motor_bm & bm)
             this->active_motors[i]->step_pin->set(0);
     }
+
+    _isr_context = false;
 }
 
 extern "C" void TIMER1_IRQHandler (void){
index 7b21418..883652e 100644 (file)
@@ -13,6 +13,7 @@ using namespace std;
 using std::string;
 #include <cstring>
 
+volatile bool _isr_context = false;
 
 uint16_t get_checksum(string to_check){
    // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum
index 5b6ee16..5c17893 100644 (file)
@@ -7,6 +7,8 @@ using namespace std;
 #include <vector>
 using std::string;
 
+extern volatile bool _isr_context;
+
 string lc(string str);
 
 string remove_non_number( string str );
index ea92eaf..056ee58 100644 (file)
@@ -127,7 +127,5 @@ int main() {
     while(1){
         kernel->call_event(ON_MAIN_LOOP);
         kernel->call_event(ON_IDLE);
-        if (kernel->slow_ticker->flag_1s())
-            kernel->call_event(ON_SECOND_TICK);
     }
 }