TEXT   65

USB libraries for the PicBasic

Guest on 26th April 2022 03:39:05 PM

  1. Using the PICBASIC PRO(TM) Compiler with the Microchip PIC18F2450, 2455, 2550,
  2. 4450, 4455 and 4550 full-speed USB devices.
  4. The USB18 subdirectory contains the PIC18Fxxx USB libraries for the PicBasic
  5. Pro Compiler, as well as example programs and some documentation files.  USB
  6. programs require several addtional files to operate (which are in the USB18
  7. subdirectory), some of which may require modification for your particular
  8. application.  The files in this subdirectory are:
  10.         18F*.BAS        Device header files that use the USB libraries
  11.         CDCDESC.ASM     Descriptor file for CDC serial demo
  12.         JADESC.ASM      Descriptor file for Jan Axelson demo
  13.         MOUSDESC.ASM    Descriptor file for mouse demo
  14.         PBPCDC.INF      INF file for the CDC serial driver
  15.         USB.TXT         This file
  16.         USB18.ASM       USB assembler file
  17.         USB18.INC       USB definitions file
  18.         USB18MEM.ASM    USB memory allocation file
  19.         USBCDC.BAS      CDC serial demo BASIC program file
  20.         USBDESC.ASM     Include file for descriptor file
  21.         USBJADEM.BAS    Jan Axelson demo BASIC program file
  22.         USBMOUSE.BAS    Mouse demo BASIC program file
  25. USB communications is much more complicated than synchronous (Shiftin and
  26. Shiftout) and asynchronous (Serin, Serout and so forth) communications.
  27. There is much more to know about USB operation that can possibly be described
  28. here.  The USB information on the Microchip web site needs to be studied.
  29. Also, the book "USB Complete" by Jan Axelson may be helpful.
  33. BASIC USB Programs
  35. The PICBASIC PRO(TM) Compiler currently supports both the HID and CDC classes
  36. of USB devices.  The CDC class allows the USB port to look like an asychronous
  37. serial port to PC programs.
  39. A USB program consists of the BASIC source code along with additional USB
  40. files, including USB18.ASM, USB18MEM.ASM, USB18.INC and a USB descriptor
  41. file.  The compiler automatically places the USB routines after the PICBASIC
  42. PRO library code and before the BASIC program code.
  44. There are 2 different header files provided for each USB device.  The ones in
  45. the PBP\USB18 subdirectory automatically include the required USB files
  46. mentioned above.  They also only allow the compiler to access the first half
  47. of the RAM.  The remainder of the RAM is dual port and is used by the USB
  48. peripheral.
  50. The header files in the PBP subdirectory do not include the USB code.  This
  51. allows the devices to be used just like any other PIC.  The reason one may
  52. want to do this is that these PICs have more memory and can run at a faster
  53. clip than most other PICs.  These header files are 18F2450.BAS, 18F2455.BAS,
  54. 18F2550.BAS, 18F4450.BAS, 18F4455.BAS and 18F4550.BAS.
  56. The USB header files for these PICs are set for a default oscillator of 20MHz.
  57. The other settings for full-speed USB are HSPLL, USB clock is 96MHz PLL / 2
  58. and system clock postscaler is divided by 2.  For low-speed USB operation,
  59. the main changes are to set the system clock postscaler to divide by 4 and
  60. change the define for UCFG_VAL in the descriptor file for the project.  The
  61. sample project descriptor files show settings of UCFG_VAL for both full-
  62. speed and low-speed.  With these settings, for full-speed, the PIC core runs
  63. at 48MHz.  For low-speed, the PIC core runs at 24MHz.
  65. One of the compiler included files is USBDESC.ASM.  This is a user modified
  66. file that contains an include to the actual descriptor file used for the
  67. current project.  Only one descriptor file must be selected in this file.
  69. The header file for the device, the files USB18.ASM, USB18MEM.ASM, USB18.INC,
  70. USBDESC.ASM and the project descriptor file, as well as any desired sample
  71. programs, should be copied to the project source subdirectory before being
  72. used.
  76. Sample Programs
  78. USBMOUSE.BAS is the Microchip mouse demo written in BASIC.  It makes the mouse
  79. move in a small circle when connected to a PC.  MOUSDESC.ASM contains the
  80. descriptors required by the demo program to operate.  This file is compiled
  81. into the program automatically by PBP.  The descriptor files are named in the
  82. file USBDESC.ASM.  This file can be edited to name another descriptor file.
  85. USBJADEM.BAS is the Jan Axelson demo program written in BASIC.  It
  86. communicates with a program running on the PC, USBHIDIO, that can be found on
  87. Jan Axelson's web site at www.lvr.com.  This demo takes 2 numbers from the PC
  88. program, increments them and sends them back.  It shows how to send data to
  89. and from the PC.  JADESC.ASM contains the descriptors required by the demo
  90. program to operate.  This file is compiled into the program automatically by
  91. PBP.  The descriptor files are named in the file USBDESC.ASM.  This file must
  92. be edited to name the descriptor file JADESC.ASM.
  95. USBCDC.BAS is a BASIC program that demonstrates USB simulating a serial port
  96. on a Windows PC.  Windows asks to install the appropriate driver when a CDC
  97. device is first plugged in.  It actually might take a moment or two for
  98. Windows to discover the device.  After that, Windows will ask for a driver
  99. file.  The file PBPCDC.INF in the USB18 subdirectory is the file that Windows
  100. can be directed to when it wants the driver information file.  CDCDESC.ASM
  101. contains the descriptors required by the demo program to operate.  This file
  102. is compiled into the program automatically by PBP.  The descriptor files are
  103. named in the file USBDESC.ASM.  This file must be edited to name the
  104. descriptor file CDCDESC.ASM.
  108. New USB Commands
  110. Four new PBP commands have been implemented to support the PIC18Fxxxx full-
  111. speed USB devices.  These commands are shown below:
  113.         USBInit
  115. USBInit needs to be one of the first statements in the program.  It will
  116. initialize the USB portion of the PICmicro MCU.
  118.         USBIn enpoint, buffer, countvar, label
  120. USBIn gets any available USB data for the endpoint and places it in the
  121. buffer.  Buffer must be a byte array of suitable length to contain the data.
  122. Countvar should be set to the size of the buffer before USBIn is executed.
  123. It will contain the number of bytes transferred to the buffer after USBIn
  124. is executed.  Label will be jumped to if no data is available.
  126.         USBOut endpoint, buffer, count, label
  128. USBOut takes count number of bytes from the buffer and sends them to the USB
  129. endpoint.  If the USB buffer does not have room for the data because of a
  130. pending transmission, no data will be transferred and program execution will
  131. continue at label.
  133.         USBService
  135. USBService needs to be executed repeatedly in the program.  Since the provided
  136. USB code is polled rather than interrupt driven, USBService needs to be
  137. executed at least every 10ms throughout the program.  If it is not, the device
  138. may drop off the USB bus.
  140. See the included USB sample programs for examples of each of these commands.
  144. PICBASIC PRO is a trademark of Microchip Technology Inc. in the U.S.A. and
  145. other countries.

Raw Paste

Login or Register to edit or fork this paste. It's free.