forked from art103/LasaurGrbl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
425 lines (363 loc) · 11.6 KB
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
serial.c - Low level functions for sending and recieving bytes via the serial port.
Part of LasaurGrbl
Copyright (c) 2009-2011 Simen Svale Skogsrud
Copyright (c) 2011 Sungeun K. Jeon
Copyright (c) 2011 Stefan Hechenberger
Inspired by the wiring_serial module by David A. Mellis which
used to be a part of the Arduino project.
LasaurGrbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LasaurGrbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <inc/hw_types.h>
#include <inc/hw_memmap.h>
#include <inc/hw_timer.h>
#include <inc/hw_gpio.h>
#include <inc/hw_ints.h>
#include <driverlib/gpio.h>
#include <driverlib/sysctl.h>
#include <driverlib/timer.h>
#include <driverlib/interrupt.h>
#include "usblib/usblib.h"
#include "usblib/usbcdc.h"
#include "usblib/usb-ids.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdcdc.h"
#include "config.h"
#include "serial.h"
#include "stepper.h"
#include "gcode.h"
#include "tasks.h"
#include "usb_serial_structs.h"
//*****************************************************************************
//
// Global flag indicating that a USB configuration has been set.
//
//*****************************************************************************
static volatile bool g_bUSBConfigured = false;
static uint32_t txByte(const uint8_t data);
void serial_init() {
/* Enable the USB peripheral and PLL */
//SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);
//SysCtlUSBPLLEnable();
/* Setup pins for USB operation */
GPIOPinTypeUSBAnalog(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5);
//
// Initialize the transmit and receive buffers.
//
USBBufferInit(&g_sTxBuffer);
USBBufferInit(&g_sRxBuffer);
//
// Set the USB stack mode to Device mode with VBUS monitoring.
//
USBStackModeSet(0, eUSBModeForceDevice, 0);
//
// Pass our device information to the USB library and place the device
// on the bus.
//
USBDCDCInit(0, &g_sCDCDevice);
IntPrioritySet(INT_USB0, CONFIG_USB_PRIORITY);
}
uint32_t serial_write(const uint8_t *pStr, uint32_t length)
{
unsigned long buffAvailSize;
uint32_t bufferedCount = 0;
uint32_t sendCount = 0;
uint8_t *sendPtr;
if (!g_bUSBConfigured)
#if 1
return 0;
#else
USBCDCD_waitForConnect();
#endif
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
while (bufferedCount != length) {
/* Determine the buffer size available */
buffAvailSize = USBBufferSpaceAvailable(&g_sTxBuffer);
/* Determine how much needs to be sent */
if ((length - bufferedCount) > buffAvailSize) {
sendCount = buffAvailSize;
}
else {
sendCount = length - bufferedCount;
}
/* Adjust the pointer to the data */
sendPtr = (uint8_t *)pStr + bufferedCount;
/* Place the contents into the USB BUffer */
bufferedCount += USBBufferWrite(&g_sTxBuffer, sendPtr, sendCount);
}
return (bufferedCount);
}
void printString(const char *s) {
serial_write((const uint8_t *)s, strlen(s));
}
// Print a string stored in PGM-memory
void printPgmString(const char *s) {
serial_write((const uint8_t *)s, strlen(s));
}
void printIntegerInBase(unsigned long n, unsigned long base) {
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
if (n == 0) {
txByte('0');
return;
}
while (n > 0) {
buf[i++] = n % base;
n /= base;
}
for (; i > 0; i--) {
txByte((buf[i - 1] < 10)?'0' + buf[i - 1]:'A' + buf[i - 1] - 10);
}
}
void printInteger(long n) {
if (n < 0) {
txByte('-');
n = -n;
}
printIntegerInBase(n, 10);
}
void printFloat(double n) {
if (n < 0) {
txByte('-');
n = -n;
}
n += 0.5/1000; // Add rounding factor
long integer_part;
integer_part = (int)n;
printIntegerInBase(integer_part,10);
txByte('.');
n -= integer_part;
int decimals = 3;
uint8_t decimal_part;
while(decimals-- > 0) {
n *= 10;
decimal_part = (int) n;
txByte('0'+decimal_part);
n -= decimal_part;
}
}
static tLineCoding g_sLineCoding =
{
115200, /* 115200 baud rate. */
1, /* 1 Stop Bit. */
0, /* No Parity. */
8 /* 8 Bits of data. */
};
//*****************************************************************************
//
// Handles CDC driver notifications related to control and setup of the device.
//
// \param pvCBData is the client-supplied callback pointer for this channel.
// \param ui32Event identifies the event we are being notified about.
// \param ui32MsgValue is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the CDC driver to perform control-related
// operations on behalf of the USB host. These functions include setting
// and querying the serial communication parameters, setting handshake line
// states and sending break conditions.
//
// \return The return value is event-specific.
//
//*****************************************************************************
uint32_t
ControlHandler(void *pvCBData, uint32_t ui32Event,
uint32_t ui32MsgValue, void *pvMsgData)
{
//
// Which event are we being asked to process?
//
switch(ui32Event)
{
//
// We are connected to a host and communication is now possible.
//
case USB_EVENT_CONNECTED:
g_bUSBConfigured = true;
//GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
//
// Flush our buffers.
//
USBBufferFlush(&g_sTxBuffer);
USBBufferFlush(&g_sRxBuffer);
break;
//
// The host has disconnected.
//
case USB_EVENT_DISCONNECTED:
g_bUSBConfigured = false;
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
break;
//
// Return the current serial communication parameters.
//
case USBD_CDC_EVENT_GET_LINE_CODING:
{
tLineCoding *psLineCoding = pvMsgData;
/* Copy the current line coding information into the structure. */
psLineCoding->ui32Rate = g_sLineCoding.ui32Rate;
psLineCoding->ui8Stop = g_sLineCoding.ui8Stop;
psLineCoding->ui8Parity = g_sLineCoding.ui8Parity;
psLineCoding->ui8Databits = g_sLineCoding.ui8Databits;
}
break;
//
// Set the current serial communication parameters.
//
case USBD_CDC_EVENT_SET_LINE_CODING:
{
tLineCoding *psLineCoding = pvMsgData;
g_sLineCoding.ui32Rate = psLineCoding->ui32Rate;
g_sLineCoding.ui8Stop = psLineCoding->ui8Stop;
g_sLineCoding.ui8Parity = psLineCoding->ui8Parity;
g_sLineCoding.ui8Databits = psLineCoding->ui8Databits;
}
break;
//
// Set the current serial communication parameters.
//
case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE:
break;
//
// Send a break condition on the serial line.
//
case USBD_CDC_EVENT_SEND_BREAK:
break;
//
// Clear the break condition on the serial line.
//
case USBD_CDC_EVENT_CLEAR_BREAK:
break;
//
// Ignore SUSPEND and RESUME for now.
//
case USB_EVENT_SUSPEND:
case USB_EVENT_RESUME:
break;
//
// We don't expect to receive any other events.
//
default:
break;
}
return(0);
}
//*****************************************************************************
//
// Handles CDC driver notifications related to the transmit channel (data to
// the USB host).
//
// \param ui32CBData is the client-supplied callback pointer for this channel.
// \param ui32Event identifies the event we are being notified about.
// \param ui32MsgValue is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the CDC driver to notify us of any events
// related to operation of the transmit data channel (the IN channel carrying
// data to the USB host).
//
// \return The return value is event-specific.
//
//*****************************************************************************
uint32_t
TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,
void *pvMsgData)
{
//
// Which event have we been sent?
//
switch(ui32Event)
{
case USB_EVENT_TX_COMPLETE:
//
// Since we are using the USBBuffer, we don't need to do anything
// here.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
break;
//
// We don't expect to receive any other events. Ignore any that show
// up in a release build or hang in a debug build.
//
default:
break;
}
return(0);
}
//*****************************************************************************
//
// Handles CDC driver notifications related to the receive channel (data from
// the USB host).
//
// \param ui32CBData is the client-supplied callback data value for this channel.
// \param ui32Event identifies the event we are being notified about.
// \param ui32MsgValue is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the CDC driver to notify us of any events
// related to operation of the receive data channel (the OUT channel carrying
// data from the USB host).
//
// \return The return value is event-specific.
//
//*****************************************************************************
uint32_t
RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,
void *pvMsgData)
{
//
// Which event are we being sent?
//
switch(ui32Event)
{
//
// A new packet has been received.
//
case USB_EVENT_RX_AVAILABLE:
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
task_enable(TASK_SERIAL_RX, (void*)&g_sRxBuffer);
break;
//
// We are being asked how much unprocessed data we have still to
// process. We return 0 if the UART is currently idle or 1 if it is
// in the process of transmitting something. The actual number of
// bytes in the UART FIFO is not important here, merely whether or
// not everything previously sent to us has been transmitted.
//
case USB_EVENT_DATA_REMAINING:
{
return 0;
}
//
// We are being asked to provide a buffer into which the next packet
// can be read. We do not support this mode of receiving data so let
// the driver know by returning 0. The CDC driver should not be sending
// this message but this is included just for illustration and
// completeness.
//
case USB_EVENT_REQUEST_BUFFER:
{
return 0;
}
//
// We don't expect to receive any other events.
//
default:
break;
}
return(0);
}
static uint32_t txByte(uint8_t data)
{
return serial_write(&data, 1);
}