Benjamin Hepp / ait_link
Committer:
bhepp
Date:
Wed Apr 06 08:27:25 2016 +0000
Revision:
3:bfc4928cd279
Parent:
0:8e8dfc870cb2
Updated license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bhepp 3:bfc4928cd279 1 //
bhepp 3:bfc4928cd279 2 // HDLC based serial communication class.
bhepp 3:bfc4928cd279 3 //
bhepp 3:bfc4928cd279 4 // Adapted by Benjamin Hepp from ArduHDLC library (see LICENSE.txt)
bhepp 3:bfc4928cd279 5 // Original work Copyright (c) 2015 Jarkko Hautakorpi et al.
bhepp 3:bfc4928cd279 6 // Modified work Copyright (c) 2016 Benjamin Hepp.
bhepp 3:bfc4928cd279 7
bhepp 3:bfc4928cd279 8 #pragma once
bhepp 3:bfc4928cd279 9
bhepp 3:bfc4928cd279 10 #include <stdint.h>
bhepp 3:bfc4928cd279 11 #include <stdbool.h>
bhepp 3:bfc4928cd279 12 #include <stddef.h>
bhepp 3:bfc4928cd279 13
bhepp 3:bfc4928cd279 14 namespace ait {
bhepp 3:bfc4928cd279 15
bhepp 3:bfc4928cd279 16 class AITLink {
bhepp 3:bfc4928cd279 17 public:
bhepp 3:bfc4928cd279 18 /* HDLC Asynchronous framing */
bhepp 3:bfc4928cd279 19 /* The frame boundary octet is 01111110, (7E in hexadecimal notation) */
bhepp 3:bfc4928cd279 20 const static uint8_t FRAME_BOUNDARY_OCTET = 0x7E;
bhepp 3:bfc4928cd279 21
bhepp 3:bfc4928cd279 22 /* A "control escape octet", has the bit sequence '01111101', (7D hexadecimal) */
bhepp 3:bfc4928cd279 23 const static uint8_t CONTROL_ESCAPE_OCTET = 0x7D;
bhepp 3:bfc4928cd279 24
bhepp 3:bfc4928cd279 25 /* If either of these two octets appears in the transmitted data, an escape octet is sent, */
bhepp 3:bfc4928cd279 26 /* followed by the original data octet with bit 5 inverted */
bhepp 3:bfc4928cd279 27 const static uint8_t INVERT_OCTET = 0x20;
bhepp 0:8e8dfc870cb2 28
bhepp 3:bfc4928cd279 29 /* The frame check sequence (FCS) is a 16-bit CRC-CCITT */
bhepp 3:bfc4928cd279 30 /* AVR Libc CRC function is _crc_ccitt_update() */
bhepp 3:bfc4928cd279 31 /* Corresponding CRC function in Qt (www.qt.io) is qChecksum() */
bhepp 3:bfc4928cd279 32 const static uint16_t CRC16_CCITT_INIT_VAL = 0xFFFF;
bhepp 3:bfc4928cd279 33
bhepp 3:bfc4928cd279 34 AITLink(size_t max_frame_length = 1024);
bhepp 3:bfc4928cd279 35 virtual ~AITLink();
bhepp 3:bfc4928cd279 36
bhepp 3:bfc4928cd279 37 bool sendFrame(const uint8_t* frame_buffer, size_t frame_length);
bhepp 3:bfc4928cd279 38 void inputReceivedChar(uint8_t data);
bhepp 3:bfc4928cd279 39
bhepp 3:bfc4928cd279 40 /* User must define a function, that will process the valid received frame */
bhepp 3:bfc4928cd279 41 /* This function can act like a command router/dispatcher */
bhepp 3:bfc4928cd279 42 void registerFrameHandler(void (*callback)(void* user_data, const uint8_t* frame_buffer, size_t frame_length), void* user_data);
bhepp 3:bfc4928cd279 43
bhepp 3:bfc4928cd279 44 protected:
bhepp 3:bfc4928cd279 45 virtual void handleFrame(const uint8_t* frame_buffer, size_t frame_length);
bhepp 3:bfc4928cd279 46 /* User must define a function to send a byte throug USART, I2C, SPI etc.*/
bhepp 3:bfc4928cd279 47 virtual void sendChar(uint8_t data) = 0;
bhepp 3:bfc4928cd279 48
bhepp 3:bfc4928cd279 49 private:
bhepp 3:bfc4928cd279 50 uint16_t crcCcittUpdate(uint16_t crc, uint8_t data);
bhepp 3:bfc4928cd279 51
bhepp 3:bfc4928cd279 52 bool escape_character;
bhepp 3:bfc4928cd279 53 size_t frame_position;
bhepp 3:bfc4928cd279 54 // 16bit CRC sum for crcCcittUpdate
bhepp 3:bfc4928cd279 55 uint16_t frame_checksum;
bhepp 3:bfc4928cd279 56 size_t max_frame_length;
bhepp 3:bfc4928cd279 57 uint8_t* receive_frame_buffer;
bhepp 3:bfc4928cd279 58 void (*handle_frame_callback)(void* user_data, const uint8_t* frame_buffer, size_t frame_length);
bhepp 3:bfc4928cd279 59 void* callback_user_data;
bhepp 3:bfc4928cd279 60 };
bhepp 3:bfc4928cd279 61
bhepp 3:bfc4928cd279 62 }