forked from PuceBaboon/Share_the_Warmth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_local_serial.jal
618 lines (542 loc) · 23.1 KB
/
usb_local_serial.jal
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
-- Title: USB Serial
-- Author: Albert Faber, Copyright (c) 2009, all rights reserved.
-- Adapted-by:
-- Compiler: >=2.4k
-- Revision: $Revision: 2760 $
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the ZLIB license (http://www.opensource.org/licenses/zlib-license.html)
--
-- Description: The library will ease the use of for serial communication over
-- the USB interface. By default nothing has to be defined by the user, below
-- an example how to use the library
-- --
-- include usb_serial
-- ..
-- usb_serial_init()
-- ..
-- ;-- optionally wait till USB becomes available
-- while ( usb_cdc_line_status() == 0x00 ) loop
-- end loop
-- ..
-- ;-- main loop
-- var byte ch
-- forever loop
-- ; -- call the flush routine on a regular base in the main loop
-- ; -- in order to keep the USB communicaiton alive
-- usb_serial_flush()
-- if ( usb_serial_read( ch ) )
-- usb_serial_data = ch -- echo
-- end if
-- .....
-- end loop
-- --
-- In addition this library can be used in combination with other JAL
-- libraries, like print.jal and format.jal, for example to send a
-- string, one can use the following code fragment:
-- --
-- const byte str[] = "Hello World"
-- print_string( usb_serial_data, str )
-- --
-- customization
-- The usb_serial library offers the following custimization prameters,
-- the user has the ability to set these const parameter(s) before the
-- inclusion of the usb_serial file
-- --
-- const byte USB_CDC_RX_BUFFER_SIZE = 0x?? -- set receive FIFO size
-- const byte USB_CDC_TX_BUFFER_SIZE = 0x?? -- set transmit FIFO size
-- const word USB_SERIAL_PRODUCT_ID = 0x???? -- set USB device product ID
-- const word USB_SERIAL_VENDOR_ID = 0x???? -- set USB device vendor ID
-- const byte USB_STRING0[] = { .. } -- set USB language string
-- const byte USB_STRING1[] = { .. } -- set USB mfg string
-- const byte USB_STRING2[] = { .. } -- set USB product string
--
-- Notes: -
--
-------------------------------------------------------------------------
-- consts to enable debugging
-------------------------------------------------------------------------
if defined( USB_SERIAL_DEBUG ) == true then
const USB_CDC_DEBUG = 1
const USB_DEBUG = 1
const USB_DEBUG_HIGH = 1
const USB_CDC_DEBUG_HIGH = 1
end if
include usb_defs
-------------------------------------------------------------------------
-- Setup USB endpoints, three enpoints are required for a CDC class
-- The default control enpoint
-- Interrupt endpoint
-- And the bulk endpoint
-------------------------------------------------------------------------
const bit USB_EP0 = 1
const byte USB_EP0_OUT_SIZE = 8
-- const word USB_EP0_OUT_ADDR = (USB_BASE_ADDRESS + 0x0020 )
const word USB_EP0_OUT_ADDR = (USB_BASE_ADDRESS + 0x0010 )
const byte USB_EP0_IN_SIZE = 8
const word USB_EP0_IN_ADDR = (USB_EP0_OUT_ADDR + USB_EP0_OUT_SIZE )
var volatile byte usb_ep0out_buf[ USB_EP0_OUT_SIZE ] at USB_EP0_OUT_ADDR
var volatile byte usb_ep0in_buf[ USB_EP0_IN_SIZE ] at USB_EP0_IN_ADDR
const bit USB_EP1 = 1
-- const byte USB_EP1_OUT_SIZE = 8
const byte USB_EP1_OUT_SIZE = 64 + 6 -- 18f27j53 writes using 64-bytes frame
-- const word USB_EP1_OUT_ADDR = (USB_BASE_ADDRESS + 0x0030 )
const word USB_EP1_OUT_ADDR = ( USB_EP0_IN_ADDR + USB_EP0_IN_SIZE )
const byte USB_EP1_IN_SIZE = 8
const word USB_EP1_IN_ADDR = (USB_EP1_OUT_ADDR + USB_EP1_OUT_SIZE )
var volatile byte usb_ep1out_buf[ USB_EP1_OUT_SIZE ] at USB_EP1_OUT_ADDR
var volatile byte usb_ep1in_buf[ USB_EP1_IN_SIZE ] at USB_EP1_IN_ADDR
const bit USB_EP2 = 1
const byte USB_EP2_OUT_SIZE = 8
const word USB_EP2_OUT_ADDR = (USB_BASE_ADDRESS + 0x0040 )
const byte USB_EP2_IN_SIZE = 8
const word USB_EP2_IN_ADDR = (USB_EP2_OUT_ADDR + USB_EP2_OUT_SIZE )
var volatile byte usb_ep2out_buf[ USB_EP2_OUT_SIZE ] at USB_EP2_OUT_ADDR
var volatile byte usb_ep2in_buf[ USB_EP2_IN_SIZE ] at USB_EP2_IN_ADDR
const bit USB_EP3 = 1
const byte USB_EP3_OUT_SIZE = 8
const word USB_EP3_OUT_ADDR = (USB_BASE_ADDRESS + 0x0050 )
const byte USB_EP3_IN_SIZE = 8
const word USB_EP3_IN_ADDR = (USB_EP3_OUT_ADDR + USB_EP3_OUT_SIZE )
var volatile byte usb_ep3out_buf[ USB_EP3_OUT_SIZE ] at USB_EP3_OUT_ADDR
var volatile byte usb_ep3in_buf[ USB_EP3_IN_SIZE ] at USB_EP3_IN_ADDR
if defined( USB_CDC_TX_BUFFER_SIZE ) == false then
-- -----------------------------------------------------------------------
-- USB_CDC_TX_BUFFER_SIZE contains the size of the transmit FIFO buffer
-- a large(r) buffer can speed up the transmission speed at the cost
-- of free memory space. The user of the usb_serial library has the
-- ability to control size of the FIFO buffer, by defining the
-- const byte USB_CDC_TX_BUFFER_SIZE = 0x?? before the inclusion of
-- the usb_serial file
-- -----------------------------------------------------------------------
const byte USB_CDC_TX_BUFFER_SIZE = 0x20
end if
if defined( USB_CDC_RX_BUFFER_SIZE ) == false then
-- -----------------------------------------------------------------------
-- USB_CDC_RX_BUFFER_SIZE contains the size of the receive FIFO buffer
-- a large(r) buffer can speed up the transmission speed at the cost
-- of free memory space. The user of the usb_serial library has the
-- ability to control size of the FIFO buffer, by defining the
-- const byte USB_CDC_RX_BUFFER_SIZE = 0x?? before the inclusion of
-- the usb_serial file
-- -----------------------------------------------------------------------
const byte USB_CDC_RX_BUFFER_SIZE = 0x10
end if
-------------------------------------------------------------------------
-- USB_CDC_DATA_ENDPOINT specifies the USB CDC Data end point
-- This usb serial library uses end point 3
-------------------------------------------------------------------------
const byte USB_CDC_DATA_ENDPOINT = 3
-------------------------------------------------------------------------
-- CDC class Functional Descriptors related constants
-- Type Values for the bDscType Field (see USB specification
-------------------------------------------------------------------------
const byte USB_CS_INTERFACE = 0x24
const byte USB_CS_ENDPOINT = 0x25
const byte USB_DCS_INTERFACE = 0x04
const byte USB_CDC_DATA_INTF = 0x0A
const byte USB_CDC_NO_PROTOCOL = 0x00 -- No class specific protocol required
-------------------------------------------------------------------------
-- CDC class Functional Descriptors related constants
-- bDscSubType in Functional Descriptors
-- more.... see Table 25 in USB CDC Specification 1.1
-------------------------------------------------------------------------
const byte USB_DSC_FN_HEADER = 0x00
const byte USB_DSC_FN_CALL_MGT = 0x01
const byte USB_DSC_FN_ACM = 0x02 -- ACM - Abstract Control Management
const byte USB_DSC_FN_DLM = 0x03 -- DLM - Direct Line Managment
const byte USB_DSC_FN_TELEPHONE_RINGER = 0x04
const byte USB_DSC_FN_RPT_CAPABILITIES = 0x05
const byte USB_DSC_FN_UNION = 0x06
const byte USB_DSC_FN_COUNTRY_SELECTION = 0x07
const byte USB_DSC_FN_TEL_OP_MODES = 0x08
const byte USB_DSC_FN_USB_TERMINAL = 0x09
-- set the communication interface ID
const byte USB_CDC_COMM_INTF_ID = 0x00
-- set the size of the interrupt interface
const byte USB_CDC_INT_EP_SIZE = 8
-- set the interrupt interface ID
const byte USB_CDC_DATA_INTF_ID = 0x01
const byte USB_CDC_BULK_OUT_EP_SIZE = USB_EP3_OUT_SIZE
const byte USB_CDC_BULK_IN_EP_SIZE = USB_EP3_IN_SIZE
const word USB_CDC_BULK_OUT_EP_ADDR = USB_EP3_OUT_ADDR
const word USB_CDC_BULK_IN_EP_ADDR = USB_EP3_IN_ADDR
-------------------------------------------------------------------------
-- define the buffer in dual port of the USB SIE, so data is
-- accessible from the JAL code
-------------------------------------------------------------------------
var volatile byte usb_cdc_eptx_buffer[USB_EP3_IN_SIZE] at USB_EP3_IN_ADDR
var volatile byte usb_cdc_eprx_buffer[USB_EP3_OUT_SIZE] at USB_EP3_OUT_ADDR
if defined( USB_SERIAL_VENDOR_ID ) == false then-- -----------------------------------------------------------------------
-- USB_SERIAL_VENDOR_ID is set to Microchip if const is not defined by
-- the user of the usb_serial library. One can set the USB_SERIAL_VENDOR_ID
-- by defining the const before inclusion of the usb_serial library
-- -----------------------------------------------------------------------
const word USB_SERIAL_VENDOR_ID = 0x04D8
end if
if defined( USB_SERIAL_PRODUCT_ID ) == false then
-- -----------------------------------------------------------------------
-- USB_SERIAL_PRODUCT_ID is set to Microchip CDC if const is not defined
-- by the user of the usb_serial library can set the USB_SERIAL_PRODUCT_ID
-- by defining the const before inclusion of the usb_serial library
-- -----------------------------------------------------------------------
const word USB_SERIAL_PRODUCT_ID = 0x000A
end if
if defined( USB_DEVICE_DESCRIPTOR ) == false then
-- -----------------------------------------------------------------------
-- Sets the default USB_DEVICE_DESCRIPTOR record. It is not expected
-- that the user of the usb_serial library needs to create its own
-- record. However, if needed in exeptional cases, the user has the
-- ability to define the
-- const byte USB_DEVICE_DESCRIPTOR[USB_DEVICE_DESCRIPTOR_SIZE]
-- before inclusion of the usb_serial library
-- -----------------------------------------------------------------------
const byte USB_DEVICE_DESCRIPTOR[USB_DEVICE_DESCRIPTOR_SIZE] = {
USB_DEVICE_DESCRIPTOR_SIZE, -- 18 bytes long
USB_DT_DEVICE, -- DEVICE 01h
0x00,
0x02, -- usb version 2.00
USB_CDC_DEVICE, -- cdc class
0x00, -- subclass
0x00, -- protocol
0x08, -- max packet size for end point 0
byte( USB_SERIAL_VENDOR_ID & 0xff ),
byte( USB_SERIAL_VENDOR_ID >> 8 ), -- USB vendor ID
byte( USB_SERIAL_PRODUCT_ID & 0xff ),
byte( USB_SERIAL_PRODUCT_ID >> 8 ), -- USB product ID
0x01,
0x00, -- Device release number in BCD format
0x01, -- string 1 for manufacturer
0x02, -- string 2 for product
0x00, -- string 3 for serial number
0x01 -- number of configurations
}
end if
if defined( USB_CONFIGURATION_DESCRIPTOR ) == false then
const USB_CONFIGURATION_DESCRIPTOR_SIZE = 0x09 + 0x09 + 0x05 + 0x04 + 0x05 + 0x05 + 0x07 + 0x09 + 0x07 + 0x07
-- -----------------------------------------------------------------------
-- Sets the default USB_CONFIGURATION_DESCRIPTOR record. It is not expected
-- that the user of the usb_serial library needs to create its own
-- record. However, if needed in exeptional cases, the user has the
-- ability to define the const byte USB_CONFIGURATION_DESCRIPTOR[]
-- before inclusion of the usb_serial library to customize the USB
-- conifguration
-- -----------------------------------------------------------------------
const byte USB_CONFIGURATION_DESCRIPTOR[ USB_CONFIGURATION_DESCRIPTOR_SIZE ]=
{
-- configuration descriptor - - - - - - - - - - 0x09 bytes
0x09, -- length,
USB_DT_CONFIGURATION, -- descriptor_type
USB_CONFIGURATION_DESCRIPTOR_SIZE,
0x00, -- total_length;
0x02, -- num_interfaces,
0x01, -- configuration_value,
0x00, -- configuration_string_id,
0b10000000, -- attributes (bus powered, no remote wake up)
50, -- max_power; (2x ma)
-- interface descriptor - - - - - - - - - - - - 0x09 bytes
0x09, -- length,
USB_DT_INTERFACE, -- descriptor_type,
0x00, -- interface_number, (starts at zero)
0x00, -- alternate_setting, (no alternatives)
0x01, -- num_endpoints,
USB_COMM_INTF, -- interface_class, (CDC)
USB_ABSTRACT_CONTROL_MODEL, -- interface_subclass, (COMM interface)
USB_V25TER, -- interface_protocol, (hayes type modem)
0x00, -- interface_string_id;
-- Functional Descriptor Structure - See CDC Specification 1.1 for details
-- functional descriptor - - - - - - - - - - - - 0x05 bytes
0X05, -- length
USB_CS_INTERFACE, -- DSC Type
USB_DSC_FN_HEADER, -- DSC Sub Type
0x10,
0x01, -- CDC (bcd)
-- functional descriptor - - - - - - - - - - - - 0x04 bytes
0X04, -- length
USB_CS_INTERFACE, -- DSC Type
USB_DSC_FN_ACM, -- DSC Sub Type
0x02, -- Capabilities
-- functional descriptor - - - - - - - - - - - - 0x05 bytes
0X05, -- length
USB_CS_INTERFACE, -- DSC Type
USB_DSC_FN_UNION, -- DSC Sub Type
USB_CDC_COMM_INTF_ID, -- MasterIntf
USB_CDC_DATA_INTF_ID, -- SaveIntf0
-- functional descriptor - - - - - - - - - - - - 0x05 bytes
0X05, -- length
USB_CS_INTERFACE, -- DSC Type
USB_DSC_FN_CALL_MGT, -- DSC Sub Type
0x00, -- Capabilities
USB_CDC_DATA_INTF_ID, -- DataInterface
-- end point descriptor - - - - - - - - - - - - - - - - 0x07 bytes
0x07, -- length,
USB_DT_ENDPOINT, -- descriptor_type,
0b10000010, -- endpoint_address, (Endpoint 2, IN)
USB_EPT_INT, -- attributes
USB_CDC_INT_EP_SIZE, --
0x00, -- max_packet_size
0x02 , -- interval
-- interface descriptor - - - - - - - - - - - - 0x09 bytes
0x09, -- length,
USB_DCS_INTERFACE, -- descriptor_type,
0x01, -- interface_number, (starts at zero)
0x00, -- alternate_setting, (no alternatives)
0x02, -- num_endpoints
USB_CDC_DATA_INTF, -- interface_class
0x00, -- interface_subclass
USB_CDC_NO_PROTOCOL, -- interface_protocol
0x00, -- interface_string_id;
-- end point descriptor - - - - - - - - - - - - - - - - 0x07 bytes
0x07, -- length,
USB_DT_ENDPOINT, -- descriptor_type,
0b00000011, -- endpoint_address, (Endpoint 3, OUT)
USB_EPT_BULK, -- attributes
USB_CDC_BULK_OUT_EP_SIZE,
0x00, -- max_packet_size
0x00, -- interval
-- end point descriptor - - - - - - - - - - - - - - - - 0x07 bytes
0x07, -- length,
USB_DT_ENDPOINT, -- descriptor_type,
0b10000011, -- endpoint_address, (Endpoint 3, IN)
USB_EPT_BULK, -- attributes
USB_CDC_BULK_OUT_EP_SIZE,
0x00, -- max_packet_size
0x00 -- interval
}
end if
if defined( USB_STRING0 ) == false then
-- -----------------------------------------------------------------------
-- USB_STRING0 shall contain the USB strings language ID
-- The user has the ability to define the string content of
-- USB_STRING0 before inclusion of the usb_serial library to
-- override the default content ( 0x0904 ).
-- note that the USB shall contain 16 bit unicode characters, little endian
-- encoded, and that the first field of USB_STRING0 shall contain the size
-- of the entire USB_STRING0 array, the second byte shall be set to
-- USB_DT_STRING (=0x03).
-- -----------------------------------------------------------------------
const byte USB_STRING0[] =
{
0x04, -- bLength
USB_DT_STRING, -- bDescriptorType
0x09, -- wLANGID[0] (low byte)
0x04 -- wLANGID[0] (high byte)
}
end if
if defined( USB_STRING1 ) == false then
-- -----------------------------------------------------------------------
-- USB_STRING1 shall contain the USB device descriptor string
-- The user has the ability to define the string content of
-- USB_STRING1 before inclusion of the usb_serial library to
-- override the default content ("Microchip Technology Inc.").
-- note that the USB shall contain 16 bit unicode characters, little endian
-- encoded, and that the first field of USB_STRING1 shall contain the size
-- of the entire USB_STRING1 array, the second byte shall be set to
-- USB_DT_STRING (=0x03).
-- -----------------------------------------------------------------------
const byte USB_STRING1[0x36] =
{
0x36, -- bLength
USB_DT_STRING, -- bDescriptorType
"M", 0x00,
"i", 0x00,
"c", 0x00,
"r", 0x00,
"o", 0x00,
"c", 0x00,
"h", 0x00,
"i", 0x00,
"p", 0x00,
" ", 0x00,
"T", 0x00,
"e", 0x00,
"c", 0x00,
"h", 0x00,
"n", 0x00,
"o", 0x00,
"l", 0x00,
"o", 0x00,
"g", 0x00,
"y", 0x00,
",", 0x00,
" ", 0x00,
"I", 0x00,
"n", 0x00,
"c", 0x00,
".", 0x00
}
end if
if defined( USB_STRING2 ) == false then
-- -----------------------------------------------------------------------
-- USB_STRING2 shall contain the USB product descriptor string
-- The user has the ability to define the string content of
-- USB_STRING1 before inclusion of the usb_serial library to
-- override the default content ("JAL Serial.").
-- note that the USB shall contain 16 bit unicode characters, little endian
-- encoded, and that the first field of USB_STRING1 shall contain the size
-- of the entire USB_STRING2 array, the second byte shall be set to
-- USB_DT_STRING (=0x03).
-- -----------------------------------------------------------------------
const byte USB_STRING2[30] =
{
30, -- bLength
USB_DT_STRING, -- bDescriptorType
"J", 0x00,
"A", 0x00,
"L", 0x00,
"L", 0x00,
"I", 0x00,
"B", 0x00,
" ", 0x00,
"S", 0x00,
"e", 0x00,
"r", 0x00,
"i", 0x00,
"a", 0x00,
"l", 0x00,
".", 0x00
}
end if
-------------------------------------------------------------------------
-- Include reset of USB driver libraries
-------------------------------------------------------------------------
include usb_drv_core
include usb_drv_cdc_class
include usb_drv
-- -----------------------------------------------------------------------
-- usb_serial_flush shall be called on a regular base (millisecond range)
-- in order to keep the USB transmision alive. Therfore it is adviced to
-- call the usb_serial_flush procedure in the main loop of your
-- application
-- -----------------------------------------------------------------------
procedure usb_serial_flush() is
-- Servicing Hardware
-- use polling method
usb_handle_isr()
usb_cdc_handle_tx()
end procedure
-- -----------------------------------------------------------------------
-- usb_serial_init will setup the USB communication, however, this can be
-- a time consuming process, therefore this call is non blocking.
-- In order to check if the USB device has established contact with the
-- HOST USB device, one can call the usb_is_configured() function, see
-- usb_is_configured() documentation for additional details.
-- -----------------------------------------------------------------------
procedure usb_serial_init() is
-- setup the USB device
usb_setup()
-- setup USB communication class
usb_cdc_setup()
-- enable USB device
usb_enable_module()
end procedure
-- -----------------------------------------------------------------------
-- usb_serial_write - write char to the USB port, blocking
-- -----------------------------------------------------------------------
-- Asynchronuous serial send routine, using the USB Cummunication class
-- Sends byte X (8 bit) to the USB host (via virtual COM port)
-- Checks (and waits if necessary) if FIFO buffer is full
-- Note that characters are dropped when the communcation channel on the
-- USB host side is not opened (yet), this is a design choice in order to
-- avoid lockup(s)
-- -----------------------------------------------------------------------
procedure usb_serial_write(byte in data) is
-- give up when DTE is not online
-- in order to avoid blocking
if ( usb_cdc_line_status() == 0x00 ) then
return
end if
var byte cdc_tx_next = cdc_tx_wr + 1 -- get next buffer position
if (cdc_tx_next == USB_CDC_TX_BUFFER_SIZE) then
-- if we're at the end
cdc_tx_next = 0 -- wrap to the beginning
end if
while (cdc_tx_next == cdc_tx_rd) loop
usb_serial_flush()
end loop
usb_cdc_putc(data)
end procedure
-- -----------------------------------------------------------------------
-- _usb_serial_read - internal use only!
-- -----------------------------------------------------------------------
-- _usb_serial_read returns true if a character has been received,
-- otherwise returns false. If function returns true, the data variable
-- is valid, and contains the character that has been received. In other
-- cases the data variable is not defined
-- Note that characters are dropped when the communcation channel on the
-- USB host side is not opened (yet), this is a design choice in order to
-- avoid lockup(s)
-- -----------------------------------------------------------------------
function _usb_serial_read(byte out data) return bit is
-- give up when DTE is not online
-- in order to avoid blocking
if ( usb_cdc_line_status() == 0x00 ) then
return false
end if
if ( !usb_cdc_rx_avail() ) then
return false
end if
-- get the character
data = usb_cdc_getc()
return true
end function
function usb_serial_data_available'get() return bit is
if ( usb_cdc_line_status() == 0x00 ) then
return false
end if
if ( !usb_cdc_rx_avail() ) then
return false
end if
return true
end function
-- -----------------------------------------------------------------------
-- usb_serial_read - read char if available (non-blocking)
-- -----------------------------------------------------------------------
-- usb_serial_read returns true if a character has been received,
-- otherwise returns false. If function returns true, the data variable
-- is valid, and contains the character that has been received. In other
-- cases the data variable is not defined
-- Note that characters are dropped when the communcation channel on the
-- USB host side is not opened (yet), this is a design choice in order to
-- avoid lockup(s)
-- -----------------------------------------------------------------------
function usb_serial_read(byte out data) return bit is
return _usb_serial_read(data)
end function
-- -----------------------------------------------------------------------
-- usb_serial_data'put - write char to the USB port, blocking
-- -----------------------------------------------------------------------
-- Asynchronuous serial send routine, using the USB Cummunication class
-- Sends byte X (8 bit) to the USB host (via virtual COM port)
-- Note that characters are dropped when the communcation channel on the
-- USB host side is not opened (yet), this is a design choice in order to
-- avoid lockup(s)
-- -----------------------------------------------------------------------
procedure usb_serial_data'put(byte in data) is
usb_serial_write( data )
end procedure
-- -----------------------------------------------------------------------
-- usb_serial_data'get() - read a char (blocking!)
-- -----------------------------------------------------------------------
-- usb_serial_data'get() waits till a character has been received and
-- returns it
-- -----------------------------------------------------------------------
function usb_serial_data'get() return byte is
var byte data
while !usb_serial_read(data) loop
-- keep usb alive
usb_serial_flush()
end loop
return data
end function
-- -----------------------------------------------------------------------
-- Check if the serial port has been opened by the host.
-- -----------------------------------------------------------------------
function usb_serial_port_open'get() return bit is
pragma inline
if cdc_line_status == 0 then
return 0
else
return 1
end if
end function