Skip to content

Reorganize task scheduling and fix bug in use of volatile variables #148

@t0mpr1c3

Description

@t0mpr1c3

The suggestions by @jpcornil-git are:

  • To change main.cpp to something like:
void setup() {
  // Objects running in async context
  GlobalCom::init();
  GlobalKnitter::init();
  GlobalLedGreen::init();
  GlobalLedYellow::init();
  GlobalBeeper::init();

  // Objects running in interrupt context
  GlobalEncoders::init(); 
}

void loop() {
  // Non-blocking methods/cooperative RR scheduling
  GlobalCom::update();
  GlobalKnitter::update();
  GlobalLedGreen::update();
  GlobalLedYellow::update();
  GlobalBeeper::update();
}
  • fsm should really be part of the knitter object while isr code should move from knitter to encoders

  • care should be taken to synchronize handover of machine state data between interrupt and main contexts

An example of a bug is the following:

At the moment this is done in the interrupt context (https://github.com/AllYarnsAreBeautiful/ayab-firmware/blob/v1.0-dev/src/ayab/knitter.cpp#L107) but it is not safe because if an interrupt fires between L402 and L403 of the same file, m_direction may change in-between.

To fix this, Knitter::update() should call, before running the fsm::update(), an encoder update() method that enter a critical section/disable interrupts, update machine state data, exit the critical section and return.

This bug is mostly hidden today as most of the code is executed after and interrupt/before the next one but if something takes more time than expected and delay fsm code such that it hits the next interrupt it may expose it (and a webserver will add more tasks moving forward).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions