Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ait_link.h@0:8e8dfc870cb2, 2016-04-04 (annotated)
- Committer:
- bhepp
- Date:
- Mon Apr 04 11:19:55 2016 +0000
- Revision:
- 0:8e8dfc870cb2
- Child:
- 3:bfc4928cd279
Initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bhepp | 0:8e8dfc870cb2 | 1 | // Adapted by Benjamin Hepp from ArduHDLC library |
bhepp | 0:8e8dfc870cb2 | 2 | |
bhepp | 0:8e8dfc870cb2 | 3 | /* |
bhepp | 0:8e8dfc870cb2 | 4 | Arduhdlc is Arduino HDLC library, which can be used over any interface. |
bhepp | 0:8e8dfc870cb2 | 5 | Copyright (C) 2015 Jarkko Hautakorpi et al. see LICENSE.txt |
bhepp | 0:8e8dfc870cb2 | 6 | This program is free software; you can redistribute it and/or |
bhepp | 0:8e8dfc870cb2 | 7 | modify it under the terms of the GNU General Public License |
bhepp | 0:8e8dfc870cb2 | 8 | as published by the Free Software Foundation; either version 2 |
bhepp | 0:8e8dfc870cb2 | 9 | of the License, or (at your option) any later version. |
bhepp | 0:8e8dfc870cb2 | 10 | This program is distributed in the hope that it will be useful, |
bhepp | 0:8e8dfc870cb2 | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
bhepp | 0:8e8dfc870cb2 | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bhepp | 0:8e8dfc870cb2 | 13 | GNU General Public License for more details. |
bhepp | 0:8e8dfc870cb2 | 14 | You should have received a copy of the GNU General Public License |
bhepp | 0:8e8dfc870cb2 | 15 | along with this program; if not, write to the Free Software |
bhepp | 0:8e8dfc870cb2 | 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
bhepp | 0:8e8dfc870cb2 | 17 | */ |
bhepp | 0:8e8dfc870cb2 | 18 | |
bhepp | 0:8e8dfc870cb2 | 19 | |
bhepp | 0:8e8dfc870cb2 | 20 | #pragma once |
bhepp | 0:8e8dfc870cb2 | 21 | |
bhepp | 0:8e8dfc870cb2 | 22 | #include <stdint.h> |
bhepp | 0:8e8dfc870cb2 | 23 | #include <stdbool.h> |
bhepp | 0:8e8dfc870cb2 | 24 | #include <stddef.h> |
bhepp | 0:8e8dfc870cb2 | 25 | |
bhepp | 0:8e8dfc870cb2 | 26 | namespace ait { |
bhepp | 0:8e8dfc870cb2 | 27 | |
bhepp | 0:8e8dfc870cb2 | 28 | class AITLink { |
bhepp | 0:8e8dfc870cb2 | 29 | public: |
bhepp | 0:8e8dfc870cb2 | 30 | /* HDLC Asynchronous framing */ |
bhepp | 0:8e8dfc870cb2 | 31 | /* The frame boundary octet is 01111110, (7E in hexadecimal notation) */ |
bhepp | 0:8e8dfc870cb2 | 32 | const static uint8_t FRAME_BOUNDARY_OCTET = 0x7E; |
bhepp | 0:8e8dfc870cb2 | 33 | |
bhepp | 0:8e8dfc870cb2 | 34 | /* A "control escape octet", has the bit sequence '01111101', (7D hexadecimal) */ |
bhepp | 0:8e8dfc870cb2 | 35 | const static uint8_t CONTROL_ESCAPE_OCTET = 0x7D; |
bhepp | 0:8e8dfc870cb2 | 36 | |
bhepp | 0:8e8dfc870cb2 | 37 | /* If either of these two octets appears in the transmitted data, an escape octet is sent, */ |
bhepp | 0:8e8dfc870cb2 | 38 | /* followed by the original data octet with bit 5 inverted */ |
bhepp | 0:8e8dfc870cb2 | 39 | const static uint8_t INVERT_OCTET = 0x20; |
bhepp | 0:8e8dfc870cb2 | 40 | |
bhepp | 0:8e8dfc870cb2 | 41 | /* The frame check sequence (FCS) is a 16-bit CRC-CCITT */ |
bhepp | 0:8e8dfc870cb2 | 42 | /* AVR Libc CRC function is _crc_ccitt_update() */ |
bhepp | 0:8e8dfc870cb2 | 43 | /* Corresponding CRC function in Qt (www.qt.io) is qChecksum() */ |
bhepp | 0:8e8dfc870cb2 | 44 | const static uint16_t CRC16_CCITT_INIT_VAL = 0xFFFF; |
bhepp | 0:8e8dfc870cb2 | 45 | |
bhepp | 0:8e8dfc870cb2 | 46 | AITLink(size_t max_frame_length = 1024); |
bhepp | 0:8e8dfc870cb2 | 47 | virtual ~AITLink(); |
bhepp | 0:8e8dfc870cb2 | 48 | |
bhepp | 0:8e8dfc870cb2 | 49 | bool sendFrame(const uint8_t* frame_buffer, size_t frame_length); |
bhepp | 0:8e8dfc870cb2 | 50 | void inputReceivedChar(uint8_t data); |
bhepp | 0:8e8dfc870cb2 | 51 | |
bhepp | 0:8e8dfc870cb2 | 52 | /* User must define a function, that will process the valid received frame */ |
bhepp | 0:8e8dfc870cb2 | 53 | /* This function can act like a command router/dispatcher */ |
bhepp | 0:8e8dfc870cb2 | 54 | void registerFrameHandler(void (*callback)(void* user_data, const uint8_t* frame_buffer, size_t frame_length), void* user_data); |
bhepp | 0:8e8dfc870cb2 | 55 | |
bhepp | 0:8e8dfc870cb2 | 56 | protected: |
bhepp | 0:8e8dfc870cb2 | 57 | virtual void handleFrame(const uint8_t* frame_buffer, size_t frame_length); |
bhepp | 0:8e8dfc870cb2 | 58 | /* User must define a function to send a byte throug USART, I2C, SPI etc.*/ |
bhepp | 0:8e8dfc870cb2 | 59 | virtual void sendChar(uint8_t data) = 0; |
bhepp | 0:8e8dfc870cb2 | 60 | |
bhepp | 0:8e8dfc870cb2 | 61 | private: |
bhepp | 0:8e8dfc870cb2 | 62 | uint16_t crcCcittUpdate(uint16_t crc, uint8_t data); |
bhepp | 0:8e8dfc870cb2 | 63 | |
bhepp | 0:8e8dfc870cb2 | 64 | bool escape_character; |
bhepp | 0:8e8dfc870cb2 | 65 | size_t frame_position; |
bhepp | 0:8e8dfc870cb2 | 66 | // 16bit CRC sum for crcCcittUpdate |
bhepp | 0:8e8dfc870cb2 | 67 | uint16_t frame_checksum; |
bhepp | 0:8e8dfc870cb2 | 68 | size_t max_frame_length; |
bhepp | 0:8e8dfc870cb2 | 69 | uint8_t* receive_frame_buffer; |
bhepp | 0:8e8dfc870cb2 | 70 | void (*handle_frame_callback)(void* user_data, const uint8_t* frame_buffer, size_t frame_length); |
bhepp | 0:8e8dfc870cb2 | 71 | void* callback_user_data; |
bhepp | 0:8e8dfc870cb2 | 72 | }; |
bhepp | 0:8e8dfc870cb2 | 73 | |
bhepp | 0:8e8dfc870cb2 | 74 | } |
bhepp | 0:8e8dfc870cb2 | 75 |