LIN_master  0.1
Arduino LIN master emulation with preemptive background operation
LIN_background.ino
Go to the documentation of this file.
1 
10 // include files
11 #include "LIN_master3.h" // muDuino LIN via Serial3
12 #include "Tasks.h"
13 
14 // task scheduler periods [ms]
15 #define PRINT_PERIOD 1000 // period for status output
16 #define LIN_PERIOD 10 // LIN frame every N ms
17 
18 // pin to demonstrate background operation
19 #define PIN_TOGGLE 30
20 
21 // helper routine to print slave status
22 void printStatus(void);
23 
24 
25 // global variables
26 uint8_t Rx[8]; // received data
27 
28 
29 void setup(void)
30 {
31  // show background operation
32  pinMode(PIN_TOGGLE, OUTPUT);
33 
34  // for user interaction via console
35  Serial.begin(115200); while(!Serial);
36 
37  // initialize LIN master (background operation)
38  LIN_master3.begin(19200, LIN_V2, true);
39 
40  // init task scheduler (also required for LIN master emulation!)
41  Tasks_Init();
42  Tasks_Add((Task) LIN_scheduler, LIN_PERIOD, 0);
43  Tasks_Add((Task) printStatus, PRINT_PERIOD, PRINT_PERIOD);
44  Tasks_Start();
45 
46 } // setup()
47 
48 
49 
50 void loop(void)
51 {
52  // toggle pin to show background operation
53  digitalWrite(PIN_TOGGLE, !digitalRead(PIN_TOGGLE));
54 
55 } // loop()
56 
57 
58 
59 // actual LIN scheduler. Periodically called by task scheduler
60 void LIN_scheduler(void)
61 {
62  static uint8_t count = 0;
63  uint8_t id;
64  uint8_t numData;
65  uint8_t data[8];
66 
67  // FED1.0 Daimler MRA2: speed request
68  if (count == 0) {
69 
70  // assemble frame data (dummy)
71  id = 0x3B;
72  numData = 2;
73  memset(data, 0, 8);
74 
75  // send master request
76  LIN_master3.sendMasterRequest(id, numData, data);
77 
78  // advance to next message
79  count++;
80 
81  } // count == 0
82 
83  else {
84 
85  // assemble frame data
86  id = 0x1B;
87  numData = 8;
88 
89  // get slave status. Copy data to buffer after reception
90  LIN_master3.receiveSlaveResponse(id, numData, Rx);
91 
92  // restart LIN scheduler
93  count=0;
94 
95  } // count == 1
96 
97 } // LIN_scheduler()
98 
99 
100 
101 // print slave response signals. Periodically called by task scheduler
102 void printStatus(void)
103 {
104  // print time
105  Serial.print(millis()); Serial.println("ms");
106 
107  // LIN ok -> print received data
109  {
110  for (uint8_t i=0; i<8; i++)
111  {
112  Serial.print(i); Serial.print("\t0x"); Serial.println(Rx[i], HEX);
113  }
114  }
115 
116  // print LIN error status
117  else {
118  Serial.print("LIN error (0x");
119  Serial.print(LIN_master3.error, HEX);
120  Serial.print("): ");
122  Serial.println("statemachine");
123  else if (LIN_master3.error & LIN_ERROR_ECHO)
124  Serial.println("echo");
126  Serial.println("timeout");
127  else if (LIN_master3.error & LIN_ERROR_CHK)
128  Serial.println("checksum");
129  else if (LIN_master3.error & LIN_ERROR_MISC)
130  Serial.println("misc");
131  } // error
132 
133  Serial.println();
134 
135  // reset latched error and flag for data received
137  LIN_master3.flagRxComplete = false;
138 
139 } // printStatus()
bool flagRxComplete
flag to indicate that data reception is complete. Must be cleared manually
Definition: LIN_master.h:142
void begin(uint16_t Baudrate, LIN_version_t Version, bool Background)
setup UART and LIN framework
Definition: LIN_master.cpp:24
LIN master emulation library for Serial3.
error reading LIN echo
Definition: LIN_master.h:70
error in LIN state machine
Definition: LIN_master.h:69
misc error, should not occur
Definition: LIN_master.h:73
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
no error
Definition: LIN_master.h:68
LIN_error_t error
error state. Is latched until cleared
Definition: LIN_master.h:143
LIN protocol version 2.
Definition: LIN_master.h:51
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 checksum error.
Definition: LIN_master.h:72
LIN_Master_3 LIN_master3
instance of LIN master via Serial3
Definition: LIN_master3.cpp:17