LINXx firmware
Extensions to LINX toolkit
listener/utility/LinxDevice.h
1/****************************************************************************************
2** LINX header for generic LINX Device.
3**
4** For more information see: www.labviewmakerhub.com/linx
5** For support visit the forums at: www.labviewmakerhub.com/forums/linx
6**
7** Written By Sam Kristoff, modified by CBC Girard
8**
9** BSD2 License.
10****************************************************************************************/
11
12#ifndef LINX_DEVICE_H
13#define LINX_DEVICE_H
14
15/****************************************************************************************
16** Includes
17****************************************************************************************/
18
19/****************************************************************************************
20** Defines
21****************************************************************************************/
22//GPIO
23#ifndef INPUT
24 #define INPUT 0x00
25#endif
26
27#ifndef OUTPUT
28 #define OUTPUT 0x01
29#endif
30
31#ifndef HIGH
32 #define HIGH 0x01
33#endif
34
35#ifndef LOW
36 #define LOW 0x00
37#endif
38
39//SPI
40#ifndef IS_M0
41
42#ifndef LSBFIRST
43 #define LSBFIRST 0
44#endif
45
46#ifndef MSBFIRST
47 #define MSBFIRST 1
48#endif
49
50#endif
51
52//I2C
53#define EOF_STOP 0
54#define EOF_RESTART 1
55#define EOF_RESTART_NOSTOP 2
56#define EOF_NOSTOP 3
57
58//UART
59#ifndef BIN
60 #define BIN 2
61#endif
62#ifndef OCT
63 #define OCT 8
64#endif
65#ifndef DEC
66 #define DEC 10
67#endif
68#ifndef HEX
69 #define HEX 16
70#endif
71
72//Non-Volatile Storage Addresses
73#define NVS_USERID 0x00
74#define NVS_ETHERNET_IP 0x02
75#define NVS_ETHERNET_PORT 0x06
76#define NVS_WIFI_IP 0x08
77#define NVS_WIFI_PORT 0x0C
78#define NVS_WIFI_SSID_SIZE 0x0E
79#define NVS_WIFI_SSID 0x0F
80#define NVS_WIFI_SECURITY_TYPE 0x30
81#define NVS_WIFI_PW_SIZE 0x31
82#define NVS_WIFI_PW 0x32
83#define NVS_SERIAL_INTERFACE_MAX_BAUD 0x72
84
85//DEBUG
86#define TX 0
87#define RX 1
88
89/****************************************************************************************
90** Typedefs
91****************************************************************************************/
92typedef enum LinxStatus
93{
94 L_OK = 0,
95 L_FUNCTION_NOT_SUPPORTED,
96 L_REQUEST_RESEND,
97 L_UNKNOWN_ERROR,
98 L_DISCONNECT
99}LinxStatus;
100
101typedef enum AioStatus
102{
103 LANALOG_REF_MODE_ERROR=129,
104 LANALOG_REF_VAL_ERROR=130
105}AioStatus;
106
107typedef enum DioStatus
108{
109 LDIGITAL_PIN_DNE=128,
110}DioStatus;
111
112
113typedef enum SPIStatus
114{
115 LSPI_OPEN_FAIL = 128,
116 LSPI_TRANSFER_FAIL
117}SPIStatus;
118
119typedef enum I2CStatus
120{
121 LI2C_SADDR=128,
122 LI2C_EOF,
123 LI2C_WRITE_FAIL,
124 LI2C_READ_FAIL,
125 LI2C_CLOSE_FAIL,
126 LI2C_OPEN_FAIL
127}I2CStatus;
128
129typedef enum UartStatus
130{
131 LUART_OPEN_FAIL=128,
132 LUART_SET_BAUD_FAIL,
133 LUART_AVAILABLE_FAIL,
134 LUART_READ_FAIL,
135 LUART_WRITE_FAIL,
136 LUART_CLOSE_FAIL
137}UartStatus;
138
139typedef enum ResetWhat
140{
141 LRESET_NONE,
142 LRESET_SERIAL,
143 LRESET_TCP,
144 LRESET_ALL
145}ResetWhat;
146
147class LinxDevice
148{
149 public:
150 /****************************************************************************************
151 ** Member Variables
152 ****************************************************************************************/
153
154 //Device ID
155 unsigned char DeviceFamily;
156 unsigned char DeviceId;
157 unsigned char DeviceNameLen;
158 const unsigned char* DeviceName;
159 unsigned char ListenerBufferSize;
160
161 //LINX API Version
162 unsigned char LinxApiMajor;
163 unsigned char LinxApiMinor;
164 unsigned char LinxApiSubminor;
165
166 //----Peripherals----
167
168 //DIO
169 unsigned char NumDigitalChans;
170 const unsigned char* DigitalChans;
171
172 //AI
173 unsigned char NumAiChans;
174 const unsigned char* AiChans;
175 unsigned char AiResolution;
176 unsigned long AiRefDefault;
177 unsigned long AiRefSet;
178
179 //AO
180 unsigned char NumAoChans;
181 const unsigned char* AoChans;
182 unsigned char AoResolution;
183 unsigned long AoRefDefault;
184 unsigned long AoRefSet;
185
186 //PWM
187 unsigned char NumPwmChans;
188 const unsigned char* PwmChans;
189
190 //QE
191 unsigned char NumQeChans;
192 const unsigned char* QeChans;
193
194 //UART
195 unsigned char NumUartChans;
196 const unsigned char* UartChans;
197 unsigned long UartMaxBaud;
198
199 //I2C
200 unsigned char NumI2cChans;
201 const unsigned char* I2cChans;
202
203 //SPI
204 unsigned char NumSpiChans;
205 const unsigned char* SpiChans;
206
207 //CAN
208 unsigned char NumCanChans;
209 const unsigned char* CanChans;
210
211 //Servo
212 unsigned char NumServoChans;
213 const unsigned char* ServoChans;
214
215 //User Configured Values
216 unsigned short userId;
217
218 unsigned long ethernetIp;
219 unsigned short ethernetPort;
220
221
222 unsigned long WifiIp;
223 unsigned short WifiPort;
224 unsigned char WifiSsidSize;
225 char WifiSsid[32];
226 unsigned char WifiSecurity;
227 unsigned char WifiPwSize;
228 char WifiPw[64];
229
230
231 unsigned long serialInterfaceMaxBaud;
232
233 //LINXx values
234 ResetWhat ResetTarget=LRESET_NONE;
235 unsigned char WifiStatus=0xFF;
236
237 /****************************************************************************************
238 ** Constructors/Destructor
239 ****************************************************************************************/
240 LinxDevice();
241 virtual ~LinxDevice();
242
243 /****************************************************************************************
244 ** Functions
245 ****************************************************************************************/
246
247 //Analog
248 virtual int AnalogRead(unsigned char numChans, unsigned char* channels, unsigned char* values) = 0;
249 virtual int AnalogReadNoPacking(unsigned char numChans, unsigned char* channels, unsigned long* values); //Values Are ADC Ticks And Not Bit Packed
250 virtual int AnalogSetRef(unsigned char mode, unsigned long voltage) = 0;
251
252 virtual int AnalogWrite(unsigned char numChans, unsigned char* channels, unsigned char* values) = 0; //Values Are Bit Packed
253 virtual int AnalogWriteNoPacking(unsigned char numChans, unsigned char* channels, unsigned long* values); //Values Are Not Bit Packed
254
255 //DIGITAL
256 virtual int DigitalWrite(unsigned char numChans, unsigned char* channels, unsigned char* values) = 0; //Values Are Bit Packed
257 virtual int DigitalWriteNoPacking(unsigned char numChans, unsigned char* channels, unsigned char* values); //Values Are Not Bit Packed
258 virtual int DigitalRead(unsigned char numChans, unsigned char* channels, unsigned char* values) = 0;
259 virtual int DigitalReadNoPacking(unsigned char numChans, unsigned char* channels, unsigned char* values); //Response Not Bit Packed
260 virtual int DigitalWriteSquareWave(unsigned char channel, unsigned long freq, unsigned long duration) = 0;
261 virtual int DigitalReadPulseWidth(unsigned char stimChan, unsigned char stimType, unsigned char respChan, unsigned char respType, unsigned long timeout, unsigned long* width) = 0;
262
263 //PWM
264 virtual int PwmSetDutyCycle(unsigned char numChans, unsigned char* channels, unsigned char* values) = 0;
265 virtual int PwmSetFrequency(unsigned char numChans, unsigned char* channels, unsigned long* values);
266
267 //SPI
268 virtual int SpiOpenMaster(unsigned char channel) = 0;
269 virtual int SpiSetBitOrder(unsigned char channel, unsigned char bitOrder) = 0;
270 virtual int SpiSetMode(unsigned char channel, unsigned char mode) = 0;
271 virtual int SpiSetSpeed(unsigned char channel, unsigned long speed, unsigned long* actualSpeed) = 0;
272 virtual int SpiWriteRead(unsigned char channel, unsigned char frameSize, unsigned char numFrames, unsigned char csChan, unsigned char csLL, unsigned char* sendBuffer, unsigned char* recBuffer) = 0;
273
274 //I2C
275 virtual int I2cOpenMaster(unsigned char channel) = 0;
276 virtual int I2cSetSpeed(unsigned char channel, unsigned long speed, unsigned long* actualSpeed) = 0;
277 virtual int I2cWrite(unsigned char channel, unsigned char slaveAddress, unsigned char eofConfig, unsigned char numBytes, unsigned char* sendBuffer) = 0;
278 virtual int I2cRead(unsigned char channel, unsigned char slaveAddress, unsigned char eofConfig, unsigned char numBytes, unsigned int timeout, unsigned char* recBuffer) = 0;
279 virtual int I2cClose(unsigned char channel) = 0;
280
281 //UART
282 virtual int UartOpen(unsigned char channel, unsigned long baudRate, unsigned long* actualBaud) = 0;
283 virtual int UartSetBaudRate(unsigned char channel, unsigned long baudRate, unsigned long* actualBaud) = 0;
284 virtual int UartGetBytesAvailable(unsigned char channel, unsigned char *numBytes) = 0;
285 virtual int UartRead(unsigned char channel, unsigned char numBytes, unsigned char* recBuffer, unsigned char* numBytesRead) = 0;
286 virtual int UartWrite(unsigned char channel, unsigned char numBytes, unsigned char* sendBuffer) = 0;
287 virtual void UartWrite(unsigned char channel, char c);
288 virtual void UartWrite(unsigned char channel, const char s[]);
289 virtual void UartWrite(unsigned char channel, unsigned char c);
290 virtual void UartWrite(unsigned char channel, int n);
291 virtual void UartWrite(unsigned char channel, unsigned int n);
292 virtual void UartWrite(unsigned char channel, long n);
293 virtual void UartWrite(unsigned char channel, unsigned long n);
294 virtual void UartWrite(unsigned char channel, long n, int base);
295 virtual void UartWriteln(unsigned char channel);
296 virtual void UartWriteln(unsigned char channel, char c);
297 virtual void UartWriteln(unsigned char channel, const char s[]);
298 virtual void UartWriteln(unsigned char channel, unsigned char c);
299 virtual void UartWriteln(unsigned char channel, int n);
300 virtual void UartWriteln(unsigned char channel, long n);
301 virtual void UartWriteln(unsigned char channel, unsigned long n);
302 virtual void UartWriteln(unsigned char channel, long n, int base);
303 virtual int UartClose(unsigned char channel) = 0;
304
305 //Servo
306 virtual int ServoOpen(unsigned char numChans, unsigned char* channels) = 0;
307 virtual int ServoSetPulseWidth(unsigned char numChans, unsigned char* channels, unsigned short* pulseWidths) = 0;
308 virtual int ServoClose(unsigned char numChans, unsigned char* channels) = 0;
309
310 //WS2812
311 virtual int Ws2812Open(unsigned short numLeds, unsigned char dataChan);
312 virtual int Ws2812WriteOnePixel(unsigned short pixelIndex, unsigned char red, unsigned char green, unsigned char blue, unsigned char refresh);
313 virtual int Ws2812WriteNPixels(unsigned short startPixel, unsigned short numPixels, unsigned char* data, unsigned char refresh);
314 virtual int Ws2812Refresh();
315 virtual int Ws2812Close();
316
317 //General
318 unsigned char ReverseBits(unsigned char b);
319 virtual unsigned long GetMilliSeconds() = 0;
320 virtual unsigned long GetSeconds() = 0;
321 virtual void DelayMs(unsigned long ms);
322 virtual void NonVolatileWrite(int address, unsigned char data) = 0;
323 virtual unsigned char NonVolatileRead(int address) = 0;
324
325 //Debug
326 virtual void EnableDebug(unsigned char channel);
327
328 virtual void DebugPrint(char c);
329 virtual void DebugPrint(const char s[]);
330 virtual void DebugPrint(unsigned char c);
331 virtual void DebugPrint(int n);
332 virtual void DebugPrint(unsigned int n);
333 virtual void DebugPrint(long n);
334 virtual void DebugPrint(unsigned long n);
335 virtual void DebugPrint(long n, int base);
336
337 virtual void DebugPrintln();
338 virtual void DebugPrintln(char c);
339 virtual void DebugPrintln(const char s[]);
340 virtual void DebugPrintln(unsigned char c);
341 virtual void DebugPrintln(int n);
342 virtual void DebugPrintln(long n);
343 virtual void DebugPrintln(unsigned long n);
344 virtual void DebugPrintln(long n, int base);
345
346
347 virtual void DebugPrintPacket(unsigned char direction, const unsigned char* packetBuffer);
348
349 // management
350 virtual int BoardCommands(unsigned char command, unsigned char numInputBytes, unsigned char* input, unsigned char* numResponseBytes, unsigned char* response);
351 virtual int Reset(ResetWhat target);
352
353 private:
354 /****************************************************************************************
355 ** Variables
356 ****************************************************************************************/
357 virtual void UartWriteNumber(unsigned char channel, unsigned long n, unsigned char bases);
358
359 /****************************************************************************************
360 ** Functions
361 ****************************************************************************************/
362};
363#endif //LINX_DEVICE_H
Base class for board functionality; extended by family and board-specific code.
Definition: device/utility/LinxDevice.h:163