Hi all,
I am new to this group but checking over recent postings seems to indicate a fair amount of Clue kicking around [1].
A bit of background before y'all tell me to RTFM or JFGI.
I've got quite a lot of experience in programming 8 bit microcontrollers with assembler; I've also done quite a lot of 32 bit embedded stuff using an RTOS with C. What's new to me is using a 16 bit mid-range PIC18 device in C.
So I'm trying to make sure that I come up with a fairly sensible architecture. Googling isn't much help - most of the "tutorial" stuff out there is aimed at a very low level, or else you find discussion at the nuts and bolts level of a particular interrupt handler.
The application is probably fairly typical - inputs / commands come in via the UART, peripheral devices need to be turned on and off in response, other devices need to be queried via I2C for data, etc.
My current thinking is to have a main() loop that is subdivided into separate sections that deal with each peripheral in turn.
Each code block will be non-blocking, and will implement a state machine for each peripheral. Incoming comms will run off interrupts. In RTOS terms, a sort-of round-robin system.
If, for example, incoming commands are ASCII sequences like "L11" for "LED 1 on" followed by an end-of-message checksum byte, in pseudo-code, something like this:
uart_isr() { // push chars into circular buffer push_char_to_uart_buf() if (char == end_of_message) message_flag = 1; }
main_loop { if (message_flag) { move_chars_from_uart_buf_to_command_buf() switch (command) { case foo: foo_state = 1; case bar: bar_state = 1; } } // foo machine if (foo_state) { switch (foo_state) { case 1: if (foo1()) foo_state = 2; case 2: if (foo2()) foo_state = 0; } // bar machine if (bar_state) { switch (bar_state) { case 1: if (bar1()) bar_state = 2; case 2: if (bar2()) bar_state = 0; } }
Does that make sense? Should the ISR do as little as possible or is it a good idea to give it some "awareness"?
The above pseudo-code is just meant to give an idea of where I'm heading, so don't take it too seriously. I'm really looking for general pointers on how to approach a mid-level architecture.
Oh go on then, rip me to shreds.
[1] I've also dug out the FAQ.