LIN_master  0.1
Arduino LIN master emulation with preemptive background operation
LIN_master.h
Go to the documentation of this file.
1 
11 /*-----------------------------------------------------------------------------
12  MODULE DEFINITION FOR MULTIPLE INCLUSION
13 -----------------------------------------------------------------------------*/
14 #ifndef _LIN_MASTER_H_
15 #define _LIN_MASTER_H_
16 
17 
18 /*-----------------------------------------------------------------------------
19  GLOBAL DEFINES
20 -----------------------------------------------------------------------------*/
21 
22 #define LIN_DEBUG_SERIAL Serial
23 #define LIN_DEBUG_LEVEL 0
24 
25 
26 /*-----------------------------------------------------------------------------
27  INCLUDE FILES
28 -----------------------------------------------------------------------------*/
29 
30 // Check library dependencies
31 #if defined(__AVR__) //the __has_include() macro only kind of works at least with the Arduino MEGA
32  #if !__has_include("../../Task_Scheduler/src/Tasks.h")
33  #error This LIN library requires the "Task Scheduler" library by Kai Liebich & Georg Icking-Konert. Please install it via the Arduino library manager!
34  #endif
35 #endif
36 
37 // include required libs
38 #include "Arduino.h"
39 #include "Tasks.h"
40 
41 
42 /*-----------------------------------------------------------------------------
43  GLOBAL ENUMS/STRUCTS
44 -----------------------------------------------------------------------------*/
45 
49 typedef enum {
50  LIN_V1 = 1,
51  LIN_V2 = 2
53 
54 
58 typedef enum {
61 } LIN_frame_t;
62 
63 
67 typedef enum {
68  LIN_SUCCESS = 0x00,
69  LIN_ERROR_STATE = 0x01,
70  LIN_ERROR_ECHO = 0x02,
72  LIN_ERROR_CHK = 0x08,
74 } LIN_error_t;
75 
76 
80 typedef enum {
85 } LIN_status_t;
86 
87 
91 typedef void (*decoder_t)(uint8_t,uint8_t*);
92 
93 
94 
95 /*-----------------------------------------------------------------------------
96  GLOBAL CLASS
97 -----------------------------------------------------------------------------*/
98 
105 {
106  protected:
107 
108  // internal variables
109  HardwareSerial *pSerial;
110  #if defined(__AVR__)
111  volatile uint8_t *UCSRA;
112  #endif
113  void (*wrapperSend)(void);
114  void (*wrapperReceive)(void);
115  void (*wrapperDefaultCallback)(uint8_t, uint8_t*);
116  #if (LIN_DEBUG_LEVEL != 0)
117  char serialName[20];
118  #endif
119  uint16_t baudrate;
121  bool background;
122  uint8_t durationBreak;
124  uint8_t bufTx[12];
125  uint8_t lenTx;
126  uint8_t bufRx[12];
127  uint8_t lenRx;
128  uint8_t durationFrame;
130  void (*rx_handler)(uint8_t, uint8_t*);
131  uint8_t *dataPtr;
132 
133  // internal methods
134  uint8_t protectID(uint8_t id);
135  uint8_t checksum(uint8_t id, uint8_t numData, uint8_t *data);
136 
137 
138  public:
139 
140  // public variables
144 
145  // public methods
146  void begin(uint16_t Baudrate, LIN_version_t Version, bool Background);
147  void end(void);
148  LIN_error_t sendMasterRequest(uint8_t id, uint8_t numData, uint8_t *data);
149  LIN_error_t receiveSlaveResponse(uint8_t id, uint8_t numData, void (*Rx_handler)(uint8_t, uint8_t*));
150  LIN_error_t receiveSlaveResponse(uint8_t id, uint8_t numData, uint8_t *data);
151 
153  void handlerSend(void);
154  void handlerReceive(void);
155  void defaultCallback(uint8_t numData, uint8_t *data);
156 
157 };
158 
159 /*-----------------------------------------------------------------------------
160  END OF MODULE DEFINITION FOR MULTIPLE INLUSION
161 -----------------------------------------------------------------------------*/
162 #endif // _LIN_MASTER_H_
void defaultCallback(uint8_t numData, uint8_t *data)
receive callback function to copy data to buffer
Definition: LIN_master.cpp:664
bool flagRxComplete
flag to indicate that data reception is complete. Must be cleared manually
Definition: LIN_master.h:142
uint8_t checksum(uint8_t id, uint8_t numData, uint8_t *data)
calculate frame checksum
Definition: LIN_master.cpp:109
uint8_t lenRx
receive buffer length (max. 12)
Definition: LIN_master.h:127
void begin(uint16_t Baudrate, LIN_version_t Version, bool Background)
setup UART and LIN framework
Definition: LIN_master.cpp:24
error reading LIN echo
Definition: LIN_master.h:70
error in LIN state machine
Definition: LIN_master.h:69
LIN_status_t
state of LIN master state machine
Definition: LIN_master.h:80
LIN_error_t
LIN communication error codes.
Definition: LIN_master.h:67
LIN_version_t
LIN version of checksum.
Definition: LIN_master.h:49
uint8_t * dataPtr
pointer to data buffer in LIN_master3_copy()
Definition: LIN_master.h:131
misc error, should not occur
Definition: LIN_master.h:73
LIN protocol version 2.
Definition: LIN_master.h:60
LIN_error_t receiveSlaveResponse(uint8_t id, uint8_t numData, void(*Rx_handler)(uint8_t, uint8_t *))
receive a slave response frame with callback function
Definition: LIN_master.cpp:253
void(* wrapperDefaultCallback)(uint8_t, uint8_t *)
wrapper for default receive callback function
Definition: LIN_master.h:115
break is being transmitted
Definition: LIN_master.h:83
uint8_t bufRx[12]
receive buffer incl. SYNC, DATA and CHK (max. 11B)
Definition: LIN_master.h:126
uint8_t lenTx
send buffer length (max. 12)
Definition: LIN_master.h:125
uint8_t durationFrame
duration of frame w/o BREAK [ms]
Definition: LIN_master.h:128
LIN instance inactive.
Definition: LIN_master.h:81
no error
Definition: LIN_master.h:68
bool flagTxComplete
flag to indicate that data transmission is complete. Must be cleared manually
Definition: LIN_master.h:141
void handlerSend(void)
LIN master receive handler for task scheduler.
Definition: LIN_master.cpp:378
void end(void)
end UART communication void end(void); //!< end UART communication
Definition: LIN_master.cpp:57
no LIN transmission ongoing
Definition: LIN_master.h:82
void(* decoder_t)(uint8_t, uint8_t *)
typedef for data decoder to hadle received data
Definition: LIN_master.h:91
void handlerReceive(void)
send handler for task scheduler
Definition: LIN_master.cpp:481
LIN_frame_t
LIN frame type.
Definition: LIN_master.h:58
LIN_error_t error
error state. Is latched until cleared
Definition: LIN_master.h:143
uint16_t baudrate
communication baudrate [Baud]
Definition: LIN_master.h:119
LIN master node base class.
Definition: LIN_master.h:104
LIN_status_t state
status of LIN state machine
Definition: LIN_master.h:129
LIN protocol version 2.
Definition: LIN_master.h:51
bool background
background or blocking operation
Definition: LIN_master.h:121
LIN_frame_t frameType
LIN frame type.
Definition: LIN_master.h:123
uint8_t durationBreak
duration of sync break [ms]
Definition: LIN_master.h:122
uint8_t protectID(uint8_t id)
calculate protected LIN ID
Definition: LIN_master.cpp:79
LIN_version_t version
LIN version for checksum calculation.
Definition: LIN_master.h:120
void(* wrapperReceive)(void)
wrapper for reception handler (for task scheduler)
Definition: LIN_master.h:114
LIN receive timeout.
Definition: LIN_master.h:71
LIN_error_t sendMasterRequest(uint8_t id, uint8_t numData, uint8_t *data)
send a master request frame
Definition: LIN_master.cpp:146
LIN protocol version 1.
Definition: LIN_master.h:59
void(* rx_handler)(uint8_t, uint8_t *)
handler to decode slave response (for receiveFrame())
Definition: LIN_master.h:130
frame is being transmitted
Definition: LIN_master.h:84
LIN checksum error.
Definition: LIN_master.h:72
void(* wrapperSend)(void)
wrapper for transmission handler (for task scheduler)
Definition: LIN_master.h:113
HardwareSerial * pSerial
pointer to used serial
Definition: LIN_master.h:109
LIN protocol version 1.
Definition: LIN_master.h:50
uint8_t bufTx[12]
send buffer incl. BREAK, SYNC, DATA and CHK (max. 12B)
Definition: LIN_master.h:124