min.c
void min_rx_byte(uint8_t byte)
{
	/* Regardless of state, three header bytes means "start of frame" and
	 * should reset the frame buffer and be ready to receive frame data
	 *
	 * Two in a row in over the frame means to expect a stuff byte.
	 */
	if(rx_header_bytes_seen == 2) {
		rx_header_bytes_seen = 0;
		if(byte == HEADER_BYTE) {
			rx_frame_state = RECEIVING_ID;
			return;
		}
		if(byte == STUFF_BYTE) {
			/* Discard this byte; carry on receiving on the next character */
			return;
		}
		else {
			/* Something has gone wrong, give up on this frame and look for header again */
			if (min_frame_dropped_callback!=NULL) {
				min_frame_dropped_callback();
		    }
			rx_frame_state = SEARCHING_FOR_SOF;
			return;
		}
	}
	
	if(byte == HEADER_BYTE) {
		rx_header_bytes_seen++;
	}
	else {
		rx_header_bytes_seen = 0;
		//TODO if we are in SEARCHING_FOR_SOF this could be a dropped frame (perhaps some header bytes have to been seen)
	}
	
	switch(rx_frame_state) {
        case SEARCHING_FOR_SOF:
            break;
        case RECEIVING_ID:
            rx_frame_id = byte;
            rx_payload_bytes = 0;
            fletcher16_rx_init();
            fletcher16_rx_step(byte);
            rx_frame_state = RECEIVING_CONTROL;
            break;
        case RECEIVING_CONTROL:
            rx_remaining_frame_length = byte ;
            rx_frame_length = byte;
            fletcher16_rx_step(byte);
            if(rx_remaining_frame_length > 0) {
                rx_frame_state = RECEIVING_PAYLOAD;
            }
            else {
                rx_frame_state = RECEIVING_CHECKSUM_HIGH;
            }
            break;
        case RECEIVING_PAYLOAD:
            rx_frame_buf[rx_payload_bytes++] = byte;
            fletcher16_rx_step(byte);
            if(--rx_remaining_frame_length == 0) {
                rx_frame_state = RECEIVING_CHECKSUM_HIGH;
            }
            break;
        case RECEIVING_CHECKSUM_HIGH:
            rx_frame_checksum = (uint16_t)byte << 8;
            rx_frame_state = RECEIVING_CHECKSUM_LOW;
            break;
        case RECEIVING_CHECKSUM_LOW:
            rx_frame_checksum |= byte;
            if(rx_frame_checksum != fletcher16_rx_finalize()) {
				if (min_frame_dropped_callback!=NULL) {
					min_frame_dropped_callback();
				}
				/* Frame fails the checksum and so is dropped */
                rx_frame_state = SEARCHING_FOR_SOF;
            }
            else {
                /* Checksum passes, go on to check for the end-of-frame marker */
                rx_frame_state = RECEIVING_EOF;
            }
            break;
        case RECEIVING_EOF:
            if(byte == 0x55u) {
                /* Frame received OK, pass up data to handler */
                min_frame_received(rx_frame_buf, rx_frame_length, rx_frame_id);
            } else {
            	/* else discard */
            	if (min_frame_dropped_callback!=NULL) {
					min_frame_dropped_callback();
				}
	        }
            /* Look for next frame */
            rx_frame_state = SEARCHING_FOR_SOF;
            break;
	}
}
/* Main receive function for a byte
 *
 * Typically called from a background task that's reading a FIFO in a loop.
 */
Main receive function for a byte
Typically called from a background task that's reading a FIFO in a loop.
Main receive function for a byte Typically called from a background task that ' s reading a FIFO in a loop .
void min_rx_byte(uint8_t byte)
{
	if(rx_header_bytes_seen == 2) {
		rx_header_bytes_seen = 0;
		if(byte == HEADER_BYTE) {
			rx_frame_state = RECEIVING_ID;
			return;
		}
		if(byte == STUFF_BYTE) {
			return;
		}
		else {
			if (min_frame_dropped_callback!=NULL) {
				min_frame_dropped_callback();
		    }
			rx_frame_state = SEARCHING_FOR_SOF;
			return;
		}
	}
	if(byte == HEADER_BYTE) {
		rx_header_bytes_seen++;
	}
	else {
		rx_header_bytes_seen = 0;
	}
	switch(rx_frame_state) {
        case SEARCHING_FOR_SOF:
            break;
        case RECEIVING_ID:
            rx_frame_id = byte;
            rx_payload_bytes = 0;
            fletcher16_rx_init();
            fletcher16_rx_step(byte);
            rx_frame_state = RECEIVING_CONTROL;
            break;
        case RECEIVING_CONTROL:
            rx_remaining_frame_length = byte ;
            rx_frame_length = byte;
            fletcher16_rx_step(byte);
            if(rx_remaining_frame_length > 0) {
                rx_frame_state = RECEIVING_PAYLOAD;
            }
            else {
                rx_frame_state = RECEIVING_CHECKSUM_HIGH;
            }
            break;
        case RECEIVING_PAYLOAD:
            rx_frame_buf[rx_payload_bytes++] = byte;
            fletcher16_rx_step(byte);
            if(--rx_remaining_frame_length == 0) {
                rx_frame_state = RECEIVING_CHECKSUM_HIGH;
            }
            break;
        case RECEIVING_CHECKSUM_HIGH:
            rx_frame_checksum = (uint16_t)byte << 8;
            rx_frame_state = RECEIVING_CHECKSUM_LOW;
            break;
        case RECEIVING_CHECKSUM_LOW:
            rx_frame_checksum |= byte;
            if(rx_frame_checksum != fletcher16_rx_finalize()) {
				if (min_frame_dropped_callback!=NULL) {
					min_frame_dropped_callback();
				}
                rx_frame_state = SEARCHING_FOR_SOF;
            }
            else {
                rx_frame_state = RECEIVING_EOF;
            }
            break;
        case RECEIVING_EOF:
            if(byte == 0x55u) {
                min_frame_received(rx_frame_buf, rx_frame_length, rx_frame_id);
            } else {
            	if (min_frame_dropped_callback!=NULL) {
					min_frame_dropped_callback();
				}
	        }
            rx_frame_state = SEARCHING_FOR_SOF;
            break;
	}
}
void min_rx_byte ( uint8_t byte ) { if ( rx_header_bytes_seen == 2 ) { rx_header_bytes_seen = 0 ; if ( byte == HEADER_BYTE ) { rx_frame_state = RECEIVING_ID ; return ; } if ( byte == STUFF_BYTE ) { return ; } else { if ( min_frame_dropped_callback != NULL ) { min_frame_dropped_callback ( ) ; } rx_frame_state = SEARCHING_FOR_SOF ; return ; } } if ( byte == HEADER_BYTE ) { rx_header_bytes_seen ++ ; } else { rx_header_bytes_seen = 0 ; } switch ( rx_frame_state ) { case SEARCHING_FOR_SOF : break ; case RECEIVING_ID : rx_frame_id = byte ; rx_payload_bytes = 0 ; fletcher16_rx_init ( ) ; fletcher16_rx_step ( byte ) ; rx_frame_state = RECEIVING_CONTROL ; break ; case RECEIVING_CONTROL : rx_remaining_frame_length = byte ; rx_frame_length = byte ; fletcher16_rx_step ( byte ) ; if ( rx_remaining_frame_length > 0 ) { rx_frame_state = RECEIVING_PAYLOAD ; } else { rx_frame_state = RECEIVING_CHECKSUM_HIGH ; } break ; case RECEIVING_PAYLOAD : rx_frame_buf [ rx_payload_bytes ++ ] = byte ; fletcher16_rx_step ( byte ) ; if ( -- rx_remaining_frame_length == 0 ) { rx_frame_state = RECEIVING_CHECKSUM_HIGH ; } break ; case RECEIVING_CHECKSUM_HIGH : rx_frame_checksum = ( uint16_t ) byte << 8 ; rx_frame_state = RECEIVING_CHECKSUM_LOW ; break ; case RECEIVING_CHECKSUM_LOW : rx_frame_checksum |= byte ; if ( rx_frame_checksum != fletcher16_rx_finalize ( ) ) { if ( min_frame_dropped_callback != NULL ) { min_frame_dropped_callback ( ) ; } rx_frame_state = SEARCHING_FOR_SOF ; } else { rx_frame_state = RECEIVING_EOF ; } break ; case RECEIVING_EOF : if ( byte == 0x55u ) { min_frame_received ( rx_frame_buf , rx_frame_length , rx_frame_id ) ; } else { if ( min_frame_dropped_callback != NULL ) { min_frame_dropped_callback ( ) ; } } rx_frame_state = SEARCHING_FOR_SOF ; break ; } }
Main receive function for a byte
 Typically called from a background task that's reading a FIFO in a loop.
Main receive function for a byte Typically called from a background task that ' s reading a FIFO in a loop .
/* Regardless of state, three header bytes means "start of frame" and
	 * should reset the frame buffer and be ready to receive frame data
	 *
	 * Two in a row in over the frame means to expect a stuff byte.
	 */ /* Discard this byte; carry on receiving on the next character */ /* Something has gone wrong, give up on this frame and look for header again */ //TODO if we are in SEARCHING_FOR_SOF this could be a dropped frame (perhaps some header bytes have to been seen) /* Frame fails the checksum and so is dropped */ /* Checksum passes, go on to check for the end-of-frame marker */ /* Frame received OK, pass up data to handler */ /* else discard */ /* Look for next frame */