Skip to content

Commit 5daec72

Browse files
author
Max 'MaxMax' Mönikes
committed
Added example to implement a terminal interface with a few functions from the user manual
1 parent faa6638 commit 5daec72

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#include "TLE9012.h"
2+
#include <stdio.h>
3+
4+
#define TXPIN 17
5+
#define RXPIN 16
6+
7+
TLE9012 tle9012;
8+
9+
//Watchdog Kicker Status Variables
10+
uint8_t watchdog_active = 0;
11+
uint16_t watchdog_time = 0;
12+
uint32_t last_wd_trigger = 0;
13+
14+
//Command Line Buffer
15+
uint8_t terminalbufferrecvlen = 0;
16+
char terminalbuffer[64];
17+
18+
19+
void setup() {
20+
Serial.begin(115200); //Start Console interface
21+
tle9012.init(&Serial2, 2000000,RXPIN,TXPIN); //Initialize driver with 2Mbit
22+
}
23+
24+
void loop() {
25+
26+
handleTerminal(); //Handle the command line interface
27+
28+
//Check if Watchdog timer is activated and trigger a reset if watchdog_time parameter expires
29+
if(watchdog_active)
30+
{
31+
if((millis()-last_wd_trigger) > watchdog_time)
32+
{
33+
tle9012.resetWatchdog();
34+
last_wd_trigger = millis();
35+
}
36+
}
37+
38+
}
39+
40+
/**
41+
* @brief Handel the command line buffer and look for EOL characters
42+
*
43+
* This function checks if new data is available over the Serial Stream
44+
* If new data was found, it is copied into a separate commandbuffer
45+
*
46+
* If an overflow of the commandbuffer was detected, the buffer is cleared and resetted before new data is copied into the buffer.
47+
* On detecting an \n Character, the buffer is passed to the interpretCommand function
48+
*/
49+
50+
void handleTerminal()
51+
{
52+
53+
//Check if new data was received
54+
uint8_t rx_len = Serial.available();
55+
56+
//Handle overflow situations by resetting the buffer
57+
if((rx_len+terminalbufferrecvlen) > 64)
58+
{
59+
for(uint8_t n = 0; n < 64; n++)
60+
terminalbuffer[n] = 0;
61+
62+
terminalbufferrecvlen = 0;
63+
}
64+
65+
//Read Bytes from Serial RX Buffer to terminalbuffer
66+
Serial.readBytes(&terminalbuffer[terminalbufferrecvlen], rx_len);
67+
68+
//Check newly received data for \n end of line character
69+
uint8_t eol_char_found = 0;
70+
for(uint8_t n = terminalbufferrecvlen; n < (terminalbufferrecvlen+rx_len); n++)
71+
{
72+
if(terminalbuffer[n] == '\n') eol_char_found = 1;
73+
}
74+
75+
terminalbufferrecvlen += rx_len; //Increase received length by number of received characters
76+
77+
//Call interpret command function and clean up terminalbuffer
78+
if(eol_char_found)
79+
{
80+
interpretCommand(terminalbuffer, terminalbufferrecvlen);
81+
82+
for(uint8_t n = 0; n < 64; n++)
83+
terminalbuffer[n] = 0;
84+
85+
terminalbufferrecvlen = 0;
86+
}
87+
88+
}
89+
90+
/**
91+
* @brief This function interprets a command line and executes a command if a valid input was detected
92+
*
93+
* @param commandbuffer buffer containing a complete command line string
94+
* @param bufferlength length of the buffer
95+
*/
96+
void interpretCommand(const char commandbuffer[], uint8_t bufferlength)
97+
{
98+
char cmd[64]; //Size might be reduced if no robustness against bufferoverflows due to excessive input string length can be accepted
99+
100+
if(sscanf(commandbuffer,"%s",cmd) == 1) //Read command
101+
{
102+
if(!strcmp(cmd,"IL")) //Emulate IL Command (no function because no ring bus mode is supported by this library)
103+
{
104+
Serial.println("IL OK");
105+
}
106+
107+
else if(!strcmp(cmd,"IH")) //Emulate IH Command (no function because no ring bus mode is supported by this library)
108+
{
109+
Serial.println("IH OK");
110+
}
111+
112+
else if((strcmp(cmd,"WH") == 0) | (strcmp(cmd,"WL") == 0)) //Write data to a single register of a single device on the bus
113+
{
114+
uint8_t dev_address;
115+
uint8_t reg_address;
116+
uint16_t data;
117+
if(sscanf(commandbuffer, "%s %d %x %x", cmd, &dev_address, &reg_address, &data) == 4)
118+
{
119+
iso_uart_status_t status = tle9012.writeRegisterSingle(dev_address, reg_address, data);
120+
if(status == isoUART_OK)
121+
{
122+
Serial.println("OK 8000");
123+
}
124+
if(status == isoUART_TIMEOUT)
125+
{
126+
Serial.println("TIMEOUT");
127+
}
128+
if(status == isoUART_CRC_ERROR)
129+
{
130+
Serial.println("CRC ERROR");
131+
}
132+
}
133+
else
134+
{
135+
Serial.println("INVALID COMMAND FORMAT");
136+
}
137+
}
138+
139+
else if((strcmp(cmd,"RH") == 0) | (strcmp(cmd,"RL") == 0)) //Read data from a single register of a single device on the bus
140+
{
141+
142+
uint8_t dev_address;
143+
uint8_t reg_address;
144+
uint16_t data;
145+
146+
if(sscanf(commandbuffer, "%s %d %x", cmd, &dev_address, &reg_address) == 3)
147+
{
148+
iso_uart_status_t status = tle9012.readRegisterSingle(dev_address, reg_address, &data);
149+
if(status == isoUART_OK)
150+
{
151+
Serial.print(data,HEX);
152+
Serial.print(" OK ");
153+
Serial.println("8000");
154+
}
155+
if(status == isoUART_TIMEOUT)
156+
{
157+
Serial.println("TIMEOUT");
158+
}
159+
if(status == isoUART_CRC_ERROR)
160+
{
161+
Serial.println("CRC ERROR");
162+
}
163+
}
164+
else
165+
{
166+
Serial.println("INVALID COMMAND FORMAT");
167+
}
168+
}
169+
170+
else if(!strcmp(cmd,"K")) //Activate watchdog kicker (deactivated for time = 0)
171+
{
172+
uint16_t wd_time = 0;
173+
if(sscanf(commandbuffer, "%s %d", cmd, &wd_time) == 2)
174+
{
175+
if(wd_time != 0)
176+
{
177+
watchdog_time = wd_time;
178+
watchdog_active = 1;
179+
Serial.print("Watchdog kicking time change to ");
180+
Serial.print(watchdog_time);
181+
Serial.println(" ms");
182+
}
183+
else
184+
{
185+
watchdog_active = 0;
186+
Serial.println("Watchdog kicker deactivated");
187+
}
188+
}
189+
}
190+
else //No valid command was detected
191+
{
192+
Serial.println("INVALID COMMAND");
193+
}
194+
195+
}
196+
}

0 commit comments

Comments
 (0)