c - Parsing a Circular Buffer -
i'm implementing little zigbee library atmel's xmega devices. zigbee radio communicates mcu using internal usart.
when started writing library using simple approach of fixed array along interrupts received data. 1 time had finish command (i know have finish command because zigbee states frame length - minus starting delimiter , checksum @ end), set flag in interrupt service routine , copied array array in main().
once had array copied, routine zbprocessframe takes on , parses frame , takes appropriate action.
the potential issue approach while i'm copying array message come , alter shared variable.
by reading online, seems can either switch off interrupts while copying array or should utilize circular buffer, can avoid copying array entirely. i've implemented 32 byte circular buffer issue is, how tell actual info began , how many bytes have come since starting delimiter. isr has following:
isr(receiver interrupt) { ring->add(usart_data); } should check starting delimiter here , set flag incase there's valid command here? main() can @ flag continuously , if raised, implies there's valid command present.
is valid approach or should alternative?
the circular buffer design. leave isr simple possible, have it. if main super-loop add together phone call new routine zbreceiveframe, reads next available byte (up available bytes individually) ring buffer , processes in state-machine. example, first state expects start-of-frame delimiter , advances next state when it's received. next state receives , interprets frame length. next state receives frame body , final state validates frame crc (all examples). states implemented within zbreceiveframe using switch statement. state variable used within zbreceiveframe should static state remembered 1 phone call next. when final state identifies valid frame sets flag prompt main phone call zbprocessframe. whether zbreceiveframe processes 1 or available chars depends on how time-critical other stuff in main super-loop.
this technique creates loose coupling, desirable. isr responsible receiving byte ring buffer , has no knowledge of frame is. zbreceiveframe knows how read ring buffer , delimit frame doesn't know how interpret data. , zbprocessframe responsible interpreting info within frame has no knowledge of ring buffer.
c embedded circular-buffer
No comments:
Post a Comment