usbdcdc.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  1. //*****************************************************************************
  2. //
  3. // usbdcdc.c - USB CDC ACM (serial) device class driver.
  4. //
  5. // Copyright (c) 2008-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of AM1808 StarterWare USB Library and reused from revision 6288
  22. // of the Stellaris USB Library.
  23. //
  24. //*****************************************************************************
  25. #include "hw_usb.h"
  26. #include "hw_types.h"
  27. #include "debug.h"
  28. #include "interrupt.h"
  29. #include "usb.h"
  30. #include "usblib.h"
  31. #include "usbdevice.h"
  32. #include "usbcdc.h"
  33. #include "usbdcdc.h"
  34. #include "usbdcomp.h"
  35. #include "usblibpriv.h"
  36. //*****************************************************************************
  37. //
  38. //! \addtogroup cdc_device_class_api
  39. //! @{
  40. //
  41. //*****************************************************************************
  42. //*****************************************************************************
  43. //
  44. // Some assumptions and deviations from the CDC specification
  45. // ----------------------------------------------------------
  46. //
  47. // 1. Although the CDC specification indicates that the following requests
  48. // should be supported by ACM CDC devices, these don't seem relevant to a
  49. // virtual COM port implementation and are never seen when connecting to a
  50. // Windows host and running either Hyperterminal or TeraTerm. As a result,
  51. // this implementation does not support them and stalls endpoint 0 if they are
  52. // received.
  53. // - SEND_ENCAPSULATED_COMMAND
  54. // - GET_ENCAPSULATED_RESPONSE
  55. // - SET_COMM_FEATURE
  56. // - GET_COMM_FEATURE
  57. // - CLEAR_COMM_FEATURE
  58. //
  59. // 2. The CDC specification is very clear on the fact that an ACM device
  60. // should offer two interfaces - a control interface offering an interrupt IN
  61. // endpoint and a data interface offering bulk IN and OUT endpoints. Using
  62. // this descriptor configuration, however, Windows insists on enumerating the
  63. // device as two separate entities resulting in two virtual COM ports or one
  64. // COM port and an Unknown Device (depending upon INF contents) appearing
  65. // in Device Manager. This implementation, derived by experimentation and
  66. // examination of other virtual COM and CDC solutions, uses only a single
  67. // interface combining all three endpoints. This appears to satisfy
  68. // Windows2000, XP and Vista and operates as intended using the Hyperterminal
  69. // and TeraTerm terminal emulators. Your mileage may vary with other
  70. // (untested) operating systems!
  71. //
  72. //*****************************************************************************
  73. //*****************************************************************************
  74. //
  75. // The subset of endpoint status flags that we consider to be reception
  76. // errors. These are passed to the client via USB_EVENT_ERROR if seen.
  77. //
  78. //*****************************************************************************
  79. #define USB_RX_ERROR_FLAGS (USBERR_DEV_RX_DATA_ERROR | \
  80. USBERR_DEV_RX_OVERRUN | \
  81. USBERR_DEV_RX_FIFO_FULL)
  82. //*****************************************************************************
  83. //
  84. // Size of the buffer to hold request-specific data read from the host. This
  85. // must be sized to accommodate the largest request structure that we intend
  86. // processing.
  87. //
  88. //*****************************************************************************
  89. #define MAX_REQUEST_DATA_SIZE sizeof(tLineCoding)
  90. //*****************************************************************************
  91. //This macro is used to diable the bit band operartion. Need to undefine this macro to use the
  92. // bit band operation.
  93. //***************************************************************************
  94. #define DISABLE_BIT_BAND
  95. //*****************************************************************************
  96. //
  97. // Flags that may appear in usDeferredOpFlags to indicate some operation that
  98. // has been requested but could not be processed at the time it was received.
  99. //
  100. //*****************************************************************************
  101. #define CDC_DO_SERIAL_STATE_CHANGE 0
  102. #define CDC_DO_SEND_BREAK 1
  103. #define CDC_DO_CLEAR_BREAK 2
  104. #define CDC_DO_LINE_CODING_CHANGE 3
  105. #define CDC_DO_LINE_STATE_CHANGE 4
  106. #define CDC_DO_PACKET_RX 5
  107. //*****************************************************************************
  108. //
  109. // The subset of deferred operations which result in the receive channel
  110. // being blocked.
  111. //
  112. //*****************************************************************************
  113. #define RX_BLOCK_OPS ((1 << CDC_DO_SEND_BREAK) | \
  114. (1 << CDC_DO_LINE_CODING_CHANGE) | \
  115. (1 << CDC_DO_LINE_STATE_CHANGE))
  116. //*****************************************************************************
  117. //
  118. // Macros to convert between USB controller base address and an index. These
  119. // are currently trivial but are included to allow for the possibility of
  120. // supporting more than one controller in the future.
  121. //
  122. //*****************************************************************************
  123. #if (USB_NUM_INSTANCE == 2)
  124. #define USB_BASE_TO_INDEX(BaseAddr, index) do{ \
  125. if(USB0_BASE==BaseAddr) \
  126. index = 0; \
  127. else if(USB1_BASE==BaseAddr) \
  128. index = 1; \
  129. else \
  130. index = -1; \
  131. }while(0)
  132. #define USB_INDEX_TO_BASE(Index, BaseAddr) ( \
  133. if(0==Index) \
  134. BaseAddr = USB0_BASE; \
  135. else if(1==Index) \
  136. BaseAddr = USB1_BASE; \
  137. else \
  138. BaseAddr = -1; \
  139. )
  140. #else
  141. #define USB_BASE_TO_INDEX(BaseAddr, index) do{ \
  142. if(USB0_BASE==BaseAddr) \
  143. index = 0; \
  144. else \
  145. index = -1; \
  146. }while(0)
  147. #define USB_INDEX_TO_BASE(Index, BaseAddr) ( \
  148. if(0==Index) \
  149. BaseAddr = USB0_BASE; \
  150. else \
  151. BaseAddr = -1; \
  152. )
  153. #endif
  154. //*****************************************************************************
  155. //
  156. // Endpoints to use for each of the required endpoints in the driver.
  157. //
  158. //*****************************************************************************
  159. #define CONTROL_ENDPOINT USB_EP_1
  160. #define DATA_IN_ENDPOINT USB_EP_2
  161. #define DATA_OUT_ENDPOINT USB_EP_1
  162. //*****************************************************************************
  163. //
  164. // The following are the USB interface numbers for the CDC serial device.
  165. //
  166. //*****************************************************************************
  167. #define SERIAL_INTERFACE_CONTROL 0
  168. #define SERIAL_INTERFACE_DATA 1
  169. //*****************************************************************************
  170. //
  171. // Maximum packet size for the bulk endpoints used for serial data
  172. // transmission and reception and the associated FIFO sizes to set aside
  173. // for each endpoint.
  174. //
  175. //*****************************************************************************
  176. #define DATA_IN_EP_FIFO_SIZE USB_FIFO_SZ_64
  177. #define DATA_OUT_EP_FIFO_SIZE USB_FIFO_SZ_64
  178. #define CTL_IN_EP_FIFO_SIZE USB_FIFO_SZ_16
  179. #define DATA_IN_EP_MAX_SIZE USB_FIFO_SZ_TO_BYTES(DATA_IN_EP_FIFO_SIZE)
  180. #define DATA_OUT_EP_MAX_SIZE USB_FIFO_SZ_TO_BYTES(DATA_IN_EP_FIFO_SIZE)
  181. #define CTL_IN_EP_MAX_SIZE USB_FIFO_SZ_TO_BYTES(CTL_IN_EP_FIFO_SIZE)
  182. //*****************************************************************************
  183. //
  184. // The collection of serial state flags indicating character errors.
  185. //
  186. //*****************************************************************************
  187. #define USB_CDC_SERIAL_ERRORS (USB_CDC_SERIAL_STATE_OVERRUN | \
  188. USB_CDC_SERIAL_STATE_PARITY | \
  189. USB_CDC_SERIAL_STATE_FRAMING)
  190. //*****************************************************************************
  191. //
  192. // USB instance Object
  193. //
  194. //*****************************************************************************
  195. extern tUSBInstanceObject g_USBInstance[];
  196. //*****************************************************************************
  197. //
  198. // Device Descriptor. This is stored in RAM to allow several fields to be
  199. // changed at runtime based on the client's requirements.
  200. //
  201. //*****************************************************************************
  202. unsigned char g_pCDCSerDeviceDescriptor[] =
  203. {
  204. 18, // Size of this structure.
  205. USB_DTYPE_DEVICE, // Type of this structure.
  206. USBShort(0x200), // USB version 1.1 (if we say 2.0, hosts assume
  207. // high-speed - see USB 2.0 spec 9.2.6.6)
  208. USB_CLASS_CDC, // USB Device Class (spec 5.1.1)
  209. 0, // USB Device Sub-class (spec 5.1.1)
  210. USB_CDC_PROTOCOL_NONE, // USB Device protocol (spec 5.1.1)
  211. 64, // Maximum packet size for default pipe.
  212. USBShort(0), // Vendor ID (filled in during USBDCDCInit).
  213. USBShort(0), // Product ID (filled in during USBDCDCInit).
  214. USBShort(0x100), // Device Version BCD.
  215. 1, // Manufacturer string identifier.
  216. 2, // Product string identifier.
  217. 3, // Product serial number.
  218. 1 // Number of configurations.
  219. };
  220. //*****************************************************************************
  221. //
  222. // CDC Serial configuration descriptor.
  223. //
  224. // It is vital that the configuration descriptor bConfigurationValue field
  225. // (byte 6) is 1 for the first configuration and increments by 1 for each
  226. // additional configuration defined here. This relationship is assumed in the
  227. // device stack for simplicity even though the USB 2.0 specification imposes
  228. // no such restriction on the bConfigurationValue values.
  229. //
  230. // Note that this structure is deliberately located in RAM since we need to
  231. // be able to patch some values in it based on client requirements.
  232. //
  233. //*****************************************************************************
  234. unsigned char g_pCDCSerDescriptor[] =
  235. {
  236. //
  237. // Configuration descriptor header.
  238. //
  239. 9, // Size of the configuration descriptor.
  240. USB_DTYPE_CONFIGURATION, // Type of this descriptor.
  241. USBShort(9), // The total size of this full structure, this
  242. // will be patched so it is just set to the
  243. // size of this structure.
  244. 2, // The number of interfaces in this
  245. // configuration.
  246. 1, // The unique value for this configuration.
  247. 5, // The string identifier that describes this
  248. // configuration.
  249. USB_CONF_ATTR_SELF_PWR, // Bus Powered, Self Powered, remote wake up.
  250. 250, // The maximum power in 2mA increments.
  251. };
  252. const tConfigSection g_sCDCSerConfigSection =
  253. {
  254. sizeof(g_pCDCSerDescriptor),
  255. g_pCDCSerDescriptor
  256. };
  257. //*****************************************************************************
  258. //
  259. // This is the Interface Association Descriptor for the serial device used in
  260. // composite devices.
  261. //
  262. //*****************************************************************************
  263. unsigned char g_pIADSerDescriptor[] =
  264. {
  265. 8, // Size of the interface descriptor.
  266. USB_DTYPE_INTERFACE_ASC, // Interface Association Type.
  267. 0x0, // Default starting interface is 0.
  268. 0x2, // Number of interfaces in this association.
  269. USB_CLASS_CDC, // The device class for this association.
  270. USB_CDC_SUBCLASS_ABSTRACT_MODEL, // The device subclass for this
  271. // association.
  272. USB_CDC_PROTOCOL_V25TER, // The protocol for this association.
  273. 0 // The string index for this association.
  274. };
  275. const tConfigSection g_sIADSerConfigSection =
  276. {
  277. sizeof(g_pIADSerDescriptor),
  278. g_pIADSerDescriptor
  279. };
  280. //*****************************************************************************
  281. //
  282. // This is the control interface for the serial device.
  283. //
  284. //*****************************************************************************
  285. const unsigned char g_pCDCSerCommInterface[] =
  286. {
  287. //
  288. // Communication Class Interface Descriptor.
  289. //
  290. 9, // Size of the interface descriptor.
  291. USB_DTYPE_INTERFACE, // Type of this descriptor.
  292. SERIAL_INTERFACE_CONTROL, // The index for this interface.
  293. 0, // The alternate setting for this interface.
  294. 1, // The number of endpoints used by this
  295. // interface.
  296. USB_CLASS_CDC, // The interface class constant defined by
  297. // USB-IF (spec 5.1.3).
  298. USB_CDC_SUBCLASS_ABSTRACT_MODEL, // The interface sub-class constant
  299. // defined by USB-IF (spec 5.1.3).
  300. USB_CDC_PROTOCOL_V25TER, // The interface protocol for the sub-class
  301. // specified above.
  302. 4, // The string index for this interface.
  303. //
  304. // Communication Class Interface Functional Descriptor - Header
  305. //
  306. 5, // Size of the functional descriptor.
  307. USB_CDC_CS_INTERFACE, // CDC interface descriptor
  308. USB_CDC_FD_SUBTYPE_HEADER, // Header functional descriptor
  309. USBShort(0x110), // Complies with CDC version 1.1
  310. //
  311. // Communication Class Interface Functional Descriptor - ACM
  312. //
  313. 4, // Size of the functional descriptor.
  314. USB_CDC_CS_INTERFACE, // CDC interface descriptor
  315. USB_CDC_FD_SUBTYPE_ABSTRACT_CTL_MGMT,
  316. USB_CDC_ACM_SUPPORTS_LINE_PARAMS | USB_CDC_ACM_SUPPORTS_SEND_BREAK,
  317. //
  318. // Communication Class Interface Functional Descriptor - Unions
  319. //
  320. 5, // Size of the functional descriptor.
  321. USB_CDC_CS_INTERFACE, // CDC interface descriptor
  322. USB_CDC_FD_SUBTYPE_UNION,
  323. SERIAL_INTERFACE_CONTROL,
  324. SERIAL_INTERFACE_DATA, // Data interface number
  325. //
  326. // Communication Class Interface Functional Descriptor - Call Management
  327. //
  328. 5, // Size of the functional descriptor.
  329. USB_CDC_CS_INTERFACE, // CDC interface descriptor
  330. USB_CDC_FD_SUBTYPE_CALL_MGMT,
  331. USB_CDC_CALL_MGMT_HANDLED,
  332. SERIAL_INTERFACE_DATA, // Data interface number
  333. //
  334. // Endpoint Descriptor (interrupt, IN)
  335. //
  336. 7, // The size of the endpoint descriptor.
  337. USB_DTYPE_ENDPOINT, // Descriptor type is an endpoint.
  338. USB_EP_DESC_IN | USB_EP_TO_INDEX(CONTROL_ENDPOINT),
  339. USB_EP_ATTR_INT, // Endpoint is an interrupt endpoint.
  340. USBShort(CTL_IN_EP_MAX_SIZE), // The maximum packet size.
  341. 10 // The polling interval for this endpoint.
  342. };
  343. const tConfigSection g_sCDCSerCommInterfaceSection =
  344. {
  345. sizeof(g_pCDCSerCommInterface),
  346. g_pCDCSerCommInterface
  347. };
  348. //*****************************************************************************
  349. //
  350. // This is the Data interface for the serial device.
  351. //
  352. //*****************************************************************************
  353. const unsigned char g_pCDCSerDataInterface[] =
  354. {
  355. //
  356. // Communication Class Data Interface Descriptor.
  357. //
  358. 9, // Size of the interface descriptor.
  359. USB_DTYPE_INTERFACE, // Type of this descriptor.
  360. SERIAL_INTERFACE_DATA, // The index for this interface.
  361. 0, // The alternate setting for this interface.
  362. 2, // The number of endpoints used by this
  363. // interface.
  364. USB_CLASS_CDC_DATA, // The interface class constant defined by
  365. // USB-IF (spec 5.1.3).
  366. 0, // The interface sub-class constant
  367. // defined by USB-IF (spec 5.1.3).
  368. USB_CDC_PROTOCOL_NONE, // The interface protocol for the sub-class
  369. // specified above.
  370. 0, // The string index for this interface.
  371. //
  372. // Endpoint Descriptor
  373. //
  374. 7, // The size of the endpoint descriptor.
  375. USB_DTYPE_ENDPOINT, // Descriptor type is an endpoint.
  376. USB_EP_DESC_IN | USB_EP_TO_INDEX(DATA_IN_ENDPOINT),
  377. USB_EP_ATTR_BULK, // Endpoint is a bulk endpoint.
  378. USBShort(DATA_IN_EP_MAX_SIZE), // The maximum packet size.
  379. 0, // The polling interval for this endpoint.
  380. //
  381. // Endpoint Descriptor
  382. //
  383. 7, // The size of the endpoint descriptor.
  384. USB_DTYPE_ENDPOINT, // Descriptor type is an endpoint.
  385. USB_EP_DESC_OUT | USB_EP_TO_INDEX(DATA_OUT_ENDPOINT),
  386. USB_EP_ATTR_BULK, // Endpoint is a bulk endpoint.
  387. USBShort(DATA_OUT_EP_MAX_SIZE), // The maximum packet size.
  388. 0, // The polling interval for this endpoint.
  389. };
  390. const tConfigSection g_sCDCSerDataInterfaceSection =
  391. {
  392. sizeof(g_pCDCSerDataInterface),
  393. g_pCDCSerDataInterface
  394. };
  395. //*****************************************************************************
  396. //
  397. // This array lists all the sections that must be concatenated to make a
  398. // single, complete CDC ACM configuration descriptor.
  399. //
  400. //*****************************************************************************
  401. const tConfigSection *g_psCDCSerSections[] =
  402. {
  403. &g_sCDCSerConfigSection,
  404. &g_sCDCSerCommInterfaceSection,
  405. &g_sCDCSerDataInterfaceSection,
  406. };
  407. #define NUM_CDCSER_SECTIONS (sizeof(g_psCDCSerSections) / \
  408. sizeof(tConfigSection *))
  409. //*****************************************************************************
  410. //
  411. // The header for the single configuration. This is the root of the data
  412. // structure that defines all the bits and pieces that are pulled together to
  413. // generate the configuration descriptor.
  414. //
  415. //*****************************************************************************
  416. const tConfigHeader g_sCDCSerConfigHeader =
  417. {
  418. NUM_CDCSER_SECTIONS,
  419. g_psCDCSerSections
  420. };
  421. //*****************************************************************************
  422. //
  423. // This array lists all the sections that must be concatenated to make a
  424. // single, complete CDC ACM configuration descriptor used in composite devices.
  425. // The only addition is the g_sIADSerConfigSection.
  426. //
  427. //*****************************************************************************
  428. const tConfigSection *g_psCDCCompSerSections[] =
  429. {
  430. &g_sCDCSerConfigSection,
  431. &g_sIADSerConfigSection,
  432. &g_sCDCSerCommInterfaceSection,
  433. &g_sCDCSerDataInterfaceSection,
  434. };
  435. #define NUM_COMP_CDCSER_SECTIONS (sizeof(g_psCDCCompSerSections) / \
  436. sizeof(tConfigSection *))
  437. //*****************************************************************************
  438. //
  439. // The header for the composite configuration. This is the root of the data
  440. // structure that defines all the bits and pieces that are pulled together to
  441. // generate the configuration descriptor.
  442. //
  443. //*****************************************************************************
  444. const tConfigHeader g_sCDCCompSerConfigHeader =
  445. {
  446. NUM_COMP_CDCSER_SECTIONS,
  447. g_psCDCCompSerSections
  448. };
  449. //*****************************************************************************
  450. //
  451. // Configuration Descriptor for the CDC serial class device.
  452. //
  453. //*****************************************************************************
  454. const tConfigHeader * const g_pCDCSerConfigDescriptors[] =
  455. {
  456. &g_sCDCSerConfigHeader
  457. };
  458. //*****************************************************************************
  459. //
  460. // Configuration Descriptor for the CDC serial class device used in a composite
  461. // device.
  462. //
  463. //*****************************************************************************
  464. const tConfigHeader * const g_pCDCCompSerConfigDescriptors[] =
  465. {
  466. &g_sCDCCompSerConfigHeader
  467. };
  468. //*****************************************************************************
  469. //
  470. // Forward references for device handler callbacks
  471. //
  472. //*****************************************************************************
  473. static void HandleRequests(void *pvInstance, tUSBRequest *pUSBRequest,
  474. unsigned int ulIndex);
  475. static void HandleConfigChange(void *pvInstance, unsigned int ulInfo,
  476. unsigned int ulIndex);
  477. static void HandleEP0Data(void *pvInstance, unsigned int ulDataSize,
  478. unsigned int ulIndex);
  479. static void HandleDisconnect(void *pvInstance);
  480. static void HandleEndpoints(void *pvInstance, unsigned int ulStatus,
  481. unsigned int ulIndex);
  482. static void HandleSuspend(void *pvInstance);
  483. static void HandleResume(void *pvInstance);
  484. static void HandleDevice(void *pvInstance, unsigned int ulRequest,
  485. void *pvRequestData);
  486. //*****************************************************************************
  487. //
  488. // The device information structure for the USB serial device.
  489. //
  490. //*****************************************************************************
  491. tDeviceInfo g_sCDCSerDeviceInfo =
  492. {
  493. //
  494. // Device event handler callbacks.
  495. //
  496. {
  497. //
  498. // GetDescriptor
  499. //
  500. 0,
  501. //
  502. // RequestHandler
  503. //
  504. HandleRequests,
  505. //
  506. // InterfaceChange
  507. //
  508. 0,
  509. //
  510. // ConfigChange
  511. //
  512. HandleConfigChange,
  513. //
  514. // DataReceived
  515. //
  516. HandleEP0Data,
  517. //
  518. // DataSentCallback
  519. //
  520. 0,
  521. //
  522. // ResetHandler
  523. //
  524. 0,
  525. //
  526. // SuspendHandler
  527. //
  528. HandleSuspend,
  529. //
  530. // ResumeHandler
  531. //
  532. HandleResume,
  533. //
  534. // DisconnectHandler
  535. //
  536. HandleDisconnect,
  537. //
  538. // EndpointHandler
  539. //
  540. HandleEndpoints,
  541. //
  542. // Device handler.
  543. //
  544. HandleDevice
  545. },
  546. //
  547. // The common device descriptor.
  548. //
  549. g_pCDCSerDeviceDescriptor,
  550. //
  551. // Default to no interrupt endpoint.
  552. //
  553. g_pCDCCompSerConfigDescriptors,
  554. //
  555. // String descriptors will be passed in.
  556. //
  557. 0,
  558. 0,
  559. //
  560. // Use the default USB FIFO configuration.
  561. //
  562. &g_sUSBDefaultFIFOConfig,
  563. //
  564. // Zero out the instance pointer by default.
  565. //
  566. 0
  567. };
  568. //*****************************************************************************
  569. //
  570. // Set or clear deferred operation flags in an "atomic" manner.
  571. //
  572. // \param pusDeferredOp points to the flags variable which is to be modified.
  573. // \param usBit indicates which bit number is to be set or cleared.
  574. // \param bSet indicates the state that the flag must be set to. If \b true,
  575. // the flag is set, if \b false, the flag is cleared.
  576. //
  577. // This function safely sets or clears a bit in a flag variable. The operation
  578. // makes use of bitbanding to ensure that the operation is atomic (no read-
  579. // modify-write is required).
  580. //
  581. // \return None.
  582. //
  583. //*****************************************************************************
  584. static void
  585. SetDeferredOpFlag(volatile unsigned short *pusDeferredOp,
  586. unsigned short usBit, tBoolean bSet)
  587. {
  588. #ifdef DISABLE_BIT_BAND
  589. if(bSet)
  590. {
  591. HWREG(pusDeferredOp) |= (1<<usBit);
  592. }
  593. else
  594. {
  595. HWREG(pusDeferredOp) &= ~(1<<usBit);
  596. }
  597. #else
  598. //
  599. // Set the flag bit to 1 or 0 using a bitband access.
  600. //
  601. HWREGBITH(pusDeferredOp, usBit) = bSet ? 1 : 0;
  602. #endif
  603. }
  604. //*****************************************************************************
  605. //
  606. // Determines whether or not a client has consumed all received data previously
  607. // passed to it.
  608. //
  609. //! \param psDevice is the pointer to the device instance structure as returned
  610. //! by USBDCDCInit().
  611. //
  612. // This function is called to determine whether or not a device has consumed
  613. // all data previously passed to it via its receive callback.
  614. //
  615. // \return Returns \b true on success or \b false on failure.
  616. //
  617. //*****************************************************************************
  618. static tBoolean
  619. DeviceConsumedAllData(const tUSBDCDCDevice *psDevice)
  620. {
  621. unsigned int ulRemaining;
  622. //
  623. // Send the device an event requesting that it tell us how many bytes
  624. // of data it still has to process.
  625. //
  626. ulRemaining = psDevice->pfnRxCallback(psDevice->pvRxCBData,
  627. USB_EVENT_DATA_REMAINING, 0, (void *)0);
  628. //
  629. // If any data remains to be processed, return false, else return true.
  630. //
  631. return (ulRemaining ? false : true);
  632. }
  633. //*****************************************************************************
  634. //
  635. // Notifies the client that it should set or clear a break condition.
  636. //
  637. // \param psDevice is the pointer to the device instance structure as returned
  638. // by USBDCDCInit().
  639. // \param bSend is \b true if a break condition is to be set or \b false if
  640. // it is to be cleared.
  641. //
  642. // This function is called to instruct the client to start or stop sending a
  643. // break condition on its serial transmit line.
  644. //
  645. // \return Returns \b true on success or \b false on failure.
  646. //
  647. //*****************************************************************************
  648. static void
  649. SendBreak(const tUSBDCDCDevice *psDevice, tBoolean bSend)
  650. {
  651. tCDCSerInstance *psInst;
  652. //
  653. // Get our instance data pointer.
  654. //
  655. psInst = psDevice->psPrivateCDCSerData;
  656. //
  657. // Set the break state flags as necessary. If we are turning the break on,
  658. // set the flag to tell ourselves that we need to notify the client when
  659. // it is time to turn it off again.
  660. //
  661. SetDeferredOpFlag(&psInst->usDeferredOpFlags, CDC_DO_SEND_BREAK, false);
  662. SetDeferredOpFlag(&psInst->usDeferredOpFlags, CDC_DO_CLEAR_BREAK, bSend);
  663. //
  664. // Tell the client to start or stop sending the break.
  665. //
  666. psDevice->pfnControlCallback(psDevice->pvControlCBData,
  667. (bSend ? USBD_CDC_EVENT_SEND_BREAK :
  668. USBD_CDC_EVENT_CLEAR_BREAK), 0,
  669. (void *)0);
  670. }
  671. //*****************************************************************************
  672. //
  673. // Notifies the client of a host request to set the serial communication
  674. // parameters.
  675. //
  676. // \param psDevice is the device instance whose communication parameters are to
  677. // be set.
  678. //
  679. // This function is called to notify the client when the host requests a change
  680. // in the serial communication parameters (baud rate, parity, number of bits
  681. // per character and number of stop bits) to use.
  682. //
  683. // \return None.
  684. //
  685. //*****************************************************************************
  686. static void
  687. SendLineCodingChange(const tUSBDCDCDevice *psDevice)
  688. {
  689. tCDCSerInstance *psInst;
  690. //
  691. // Get our instance data pointer.
  692. //
  693. psInst = psDevice->psPrivateCDCSerData;
  694. //
  695. // Clear the flag we use to tell ourselves that the line coding change has
  696. // yet to be notified to the client.
  697. //
  698. SetDeferredOpFlag(&psInst->usDeferredOpFlags, CDC_DO_LINE_CODING_CHANGE,
  699. false);
  700. //
  701. // Tell the client to update their serial line coding parameters.
  702. //
  703. psDevice->pfnControlCallback(psDevice->pvControlCBData,
  704. USBD_CDC_EVENT_SET_LINE_CODING, 0,
  705. &(psInst->sLineCoding));
  706. }
  707. //*****************************************************************************
  708. //
  709. // Notifies the client of a host request to set the RTS and DTR handshake line
  710. // states.
  711. //
  712. // \param psDevice is the device instance whose break condition is to be set or
  713. // cleared.
  714. //
  715. // This function is called to notify the client when the host requests a change
  716. // in the state of one or other of the RTS and DTR handshake lines.
  717. //
  718. // \return None.
  719. //
  720. //*****************************************************************************
  721. static void
  722. SendLineStateChange(const tUSBDCDCDevice *psDevice)
  723. {
  724. tCDCSerInstance *psInst;
  725. //
  726. // Get our instance data pointer.
  727. //
  728. psInst = psDevice->psPrivateCDCSerData;
  729. //
  730. // Clear the flag we use to tell ourselves that the line coding change has
  731. // yet to be notified to the client.
  732. //
  733. SetDeferredOpFlag(&psInst->usDeferredOpFlags, CDC_DO_LINE_STATE_CHANGE,
  734. false);
  735. //
  736. // Tell the client to update their serial line coding parameters.
  737. //
  738. psDevice->pfnControlCallback(psDevice->pvControlCBData,
  739. USBD_CDC_EVENT_SET_CONTROL_LINE_STATE,
  740. psInst->usControlLineState,
  741. (void *)0);
  742. }
  743. //*****************************************************************************
  744. //
  745. // Notifies the client of a break request if no data remains to be processed.
  746. //
  747. // \param psDevice is the device instance that is to be commanded to send a
  748. // break condition.
  749. //
  750. // This function is called when the host requests that the device set a break
  751. // condition on the serial transmit line. If no data received from the host
  752. // remains to be processed, the break request is passed to the control
  753. // callback. If data is outstanding, the call is ignored (with the operation
  754. // being retried on the next timer tick).
  755. //
  756. // \return Returns \b true if the break notification was sent, \b false
  757. // otherwise.
  758. //
  759. //*****************************************************************************
  760. static tBoolean
  761. CheckAndSendBreak(const tUSBDCDCDevice *psDevice, unsigned short usDuration)
  762. {
  763. tBoolean bCanSend;
  764. //
  765. // Has the client consumed all data received from the host yet?
  766. //
  767. bCanSend = DeviceConsumedAllData(psDevice);
  768. //
  769. // Can we send the break request?
  770. //
  771. if(bCanSend)
  772. {
  773. //
  774. // Pass the break request on to the client since no data remains to be
  775. // consumed.
  776. //
  777. SendBreak(psDevice, (usDuration ? true : false));
  778. }
  779. //
  780. // Tell the caller whether or not we sent the notification.
  781. //
  782. return (bCanSend);
  783. }
  784. //*****************************************************************************
  785. //
  786. // Notifies the client of a request to change the serial line parameters if no
  787. // data remains to be processed.
  788. //
  789. // \param psDevice is the device instance whose line coding parameters are to
  790. // be changed.
  791. //
  792. // This function is called when the host requests that the device change the
  793. // serial line coding parameters. If no data received from the host remains
  794. // to be processed, the request is passed to the control callback. If data is
  795. // outstanding, the call is ignored (with the operation being retried on the
  796. // next timer tick).
  797. //
  798. // \return Returns \b true if the notification was sent, \b false otherwise.
  799. //
  800. //*****************************************************************************
  801. static tBoolean
  802. CheckAndSendLineCodingChange(const tUSBDCDCDevice *psDevice)
  803. {
  804. tBoolean bCanSend;
  805. //
  806. // Has the client consumed all data received from the host yet?
  807. //
  808. bCanSend = DeviceConsumedAllData(psDevice);
  809. //
  810. // Can we send the break request?
  811. //
  812. if(bCanSend)
  813. {
  814. //
  815. // Pass the request on to the client since no data remains to be
  816. // consumed.
  817. //
  818. SendLineCodingChange(psDevice);
  819. }
  820. //
  821. // Tell the caller whether or not we sent the notification.
  822. //
  823. return (bCanSend);
  824. }
  825. //*****************************************************************************
  826. //
  827. // Notifies the client of a request to change the handshake line states if no
  828. // data remains to be processed.
  829. //
  830. // \param psDevice is the device instance whose handshake line states are to
  831. // be changed.
  832. //
  833. // This function is called when the host requests that the device change the
  834. // state of one or other of the RTS or DTR handshake lines. If no data
  835. // received from the host remains to be processed, the request is passed to
  836. // the control callback. If data is outstanding, the call is ignored (with
  837. // the operation being retried on the next timer tick).
  838. //
  839. // \return Returns \b true if the notification was sent, \b false otherwise.
  840. //
  841. //*****************************************************************************
  842. static tBoolean
  843. CheckAndSendLineStateChange(const tUSBDCDCDevice *psDevice)
  844. {
  845. tBoolean bCanSend;
  846. //
  847. // Has the client consumed all data received from the host yet?
  848. //
  849. bCanSend = DeviceConsumedAllData(psDevice);
  850. //
  851. // Can we send the break request?
  852. //
  853. if(bCanSend)
  854. {
  855. //
  856. // Pass the request on to the client since no data remains to be
  857. // consumed.
  858. //
  859. SendLineStateChange(psDevice);
  860. }
  861. //
  862. // Tell the caller whether or not we sent the notification.
  863. //
  864. return (bCanSend);
  865. }
  866. //*****************************************************************************
  867. //
  868. // Notifies the client of a change in the serial line state.
  869. //
  870. // \param psInst is the instance whose serial state is to be reported.
  871. //
  872. // This function is called to send the current serial state information to
  873. // the host via the the interrupt IN endpoint. This notification informs the
  874. // host of problems or conditions such as parity errors, breaks received,
  875. // framing errors, etc.
  876. //
  877. // \return Returns \b true on success or \b false on failure.
  878. //
  879. //*****************************************************************************
  880. static tBoolean
  881. SendSerialState(const tUSBDCDCDevice *psDevice)
  882. {
  883. tUSBRequest sRequest;
  884. unsigned short usSerialState;
  885. tCDCSerInstance *psInst;
  886. int iRetcode;
  887. //
  888. // Get a pointer to our instance data.
  889. //
  890. psInst = psDevice->psPrivateCDCSerData;
  891. //
  892. // Remember that we are in the middle of sending a notification.
  893. //
  894. psInst->eCDCInterruptState = CDC_STATE_WAIT_DATA;
  895. //
  896. // Clear the flag we use to indicate that a send is required.
  897. //
  898. SetDeferredOpFlag(&psInst->usDeferredOpFlags, CDC_DO_SERIAL_STATE_CHANGE,
  899. false);
  900. //
  901. // Take a snapshot of the serial state.
  902. //
  903. usSerialState = psInst->usSerialState;
  904. //
  905. // Build the request we will use to send the notification.
  906. //
  907. sRequest.bmRequestType = (USB_RTYPE_DIR_IN | USB_RTYPE_CLASS |
  908. USB_RTYPE_INTERFACE);
  909. sRequest.bRequest = USB_CDC_NOTIFY_SERIAL_STATE;
  910. sRequest.wValue = 0;
  911. sRequest.wIndex = 0;
  912. sRequest.wLength = USB_CDC_NOTIFY_SERIAL_STATE_SIZE;
  913. //
  914. // Write the request structure to the USB FIFO.
  915. //
  916. iRetcode = USBEndpointDataPut(psInst->ulUSBBase, psInst->ucControlEndpoint,
  917. (unsigned char *)&sRequest,
  918. sizeof(tUSBRequest));
  919. iRetcode = USBEndpointDataPut(psInst->ulUSBBase, psInst->ucControlEndpoint,
  920. (unsigned char *)&usSerialState,
  921. USB_CDC_NOTIFY_SERIAL_STATE_SIZE);
  922. //
  923. // Did we correctly write the data to the endpoint FIFO?
  924. //
  925. if(iRetcode != -1)
  926. {
  927. //
  928. // We put the data into the FIFO so now schedule it to be
  929. // sent.
  930. //
  931. iRetcode = USBEndpointDataSend(psInst->ulUSBBase,
  932. psInst->ucControlEndpoint,
  933. USB_TRANS_IN);
  934. }
  935. //
  936. // If an error occurred, mark the endpoint as idle (to prevent possible
  937. // lockup) and return an error.
  938. //
  939. if(iRetcode == -1)
  940. {
  941. psInst->eCDCInterruptState = CDC_STATE_IDLE;
  942. return (false);
  943. }
  944. else
  945. {
  946. //
  947. // Everything went fine. Clear the error bits that we just notified
  948. // and return true.
  949. //
  950. psInst->usSerialState &= ~(usSerialState & USB_CDC_SERIAL_ERRORS);
  951. return (true);
  952. }
  953. }
  954. //*****************************************************************************
  955. //
  956. // Receives notifications related to data received from the host.
  957. //
  958. // \param psDevice is the device instance whose endpoint is to be processed.
  959. // \param ulStatus is the USB interrupt status that caused this function to
  960. // be called.
  961. //
  962. // This function is called from HandleEndpoints for all interrupts signaling
  963. // the arrival of data on the bulk OUT endpoint (in other words, whenever the
  964. // host has sent us a packet of data). We inform the client that a packet
  965. // is available and, on return, check to see if the packet has been read. If
  966. // not, we schedule another notification to the client for a later time.
  967. //
  968. // \return Returns \b true on success or \b false on failure.
  969. //
  970. //*****************************************************************************
  971. tBoolean
  972. ProcessDataFromHost(const tUSBDCDCDevice *psDevice, unsigned int ulStatus,
  973. unsigned int ulIndex)
  974. {
  975. unsigned int ulEPStatus;
  976. unsigned int ulSize;
  977. tCDCSerInstance *psInst;
  978. //
  979. // Get a pointer to our instance data.
  980. //
  981. psInst = psDevice->psPrivateCDCSerData;
  982. //
  983. // Get the endpoint status to see why we were called.
  984. //
  985. ulEPStatus = USBEndpointStatus(psInst->ulUSBBase,
  986. psInst->ucBulkOUTEndpoint);
  987. //
  988. // Clear the status bits.
  989. //
  990. USBDevEndpointStatusClear(psInst->ulUSBBase, psInst->ucBulkOUTEndpoint,
  991. ulEPStatus);
  992. //
  993. // Has a packet been received?
  994. //
  995. if(ulEPStatus & USB_DEV_RX_PKT_RDY)
  996. {
  997. //
  998. // Set the flag we use to indicate that a packet read is pending. This
  999. // will be cleared if the packet is read. If the client doesn't read
  1000. // the packet in the context of the USB_EVENT_RX_AVAILABLE callback,
  1001. // the event will be notified later during tick processing.
  1002. //
  1003. SetDeferredOpFlag(&psInst->usDeferredOpFlags, CDC_DO_PACKET_RX, true);
  1004. //
  1005. // Is the receive channel currently blocked?
  1006. //
  1007. if(!psInst->bControlBlocked && !psInst->bRxBlocked)
  1008. {
  1009. //
  1010. // How big is the packet we've just been sent?
  1011. //
  1012. ulSize = USBEndpointDataAvail(psInst->ulUSBBase,
  1013. psInst->ucBulkOUTEndpoint);
  1014. //
  1015. // The receive channel is not blocked so let the caller know
  1016. // that a packet is waiting. The parameters are set to indicate
  1017. // that the packet has not been read from the hardware FIFO yet.
  1018. //
  1019. psDevice->pfnRxCallback(psDevice->pvRxCBData,
  1020. USB_EVENT_RX_AVAILABLE, ulSize,
  1021. (void *)0);
  1022. }
  1023. }
  1024. else
  1025. {
  1026. //
  1027. // No packet was received. Some error must have been reported. Check
  1028. // and pass this on to the client if necessary.
  1029. //
  1030. if(ulEPStatus & USB_RX_ERROR_FLAGS)
  1031. {
  1032. //
  1033. // This is an error we report to the client so...
  1034. //
  1035. psDevice->pfnRxCallback(psDevice->pvRxCBData,
  1036. USB_EVENT_ERROR,
  1037. (ulEPStatus & USB_RX_ERROR_FLAGS),
  1038. (void *)0);
  1039. }
  1040. return (false);
  1041. }
  1042. return (true);
  1043. }
  1044. //*****************************************************************************
  1045. //
  1046. // Receives notifications related to interrupt messages sent to the host.
  1047. //
  1048. // \param psDevice is the device instance whose endpoint is to be processed.
  1049. // \param ulStatus is the USB interrupt status that caused this function to
  1050. // be called.
  1051. //
  1052. // This function is called from HandleEndpoints for all interrupts originating
  1053. // from the interrupt IN endpoint (in other words, whenever a notification has
  1054. // been transmitted to the USB host).
  1055. //
  1056. // \return Returns \b true on success or \b false on failure.
  1057. //
  1058. //*****************************************************************************
  1059. tBoolean
  1060. ProcessNotificationToHost(const tUSBDCDCDevice *psDevice,
  1061. unsigned int ulStatus)
  1062. {
  1063. unsigned int ulEPStatus;
  1064. tCDCSerInstance *psInst;
  1065. tBoolean bRetcode;
  1066. //
  1067. // Assume all will go well until we have reason to believe otherwise.
  1068. //
  1069. bRetcode = true;
  1070. //
  1071. // Get a pointer to our instance data.
  1072. //
  1073. psInst = psDevice->psPrivateCDCSerData;
  1074. //
  1075. // Get the endpoint status to see why we were called.
  1076. //
  1077. ulEPStatus = USBEndpointStatus(psInst->ulUSBBase,
  1078. psInst->ucControlEndpoint);
  1079. //
  1080. // Clear the status bits.
  1081. //
  1082. USBDevEndpointStatusClear(psInst->ulUSBBase,
  1083. psInst->ucControlEndpoint, ulEPStatus);
  1084. //
  1085. // Did the state change while we were waiting for the previous notification
  1086. // to complete?
  1087. //
  1088. if(psInst->usDeferredOpFlags & (1 << CDC_DO_SERIAL_STATE_CHANGE))
  1089. {
  1090. //
  1091. // The state changed while we were waiting so we need to schedule
  1092. // another notification immediately.
  1093. //
  1094. bRetcode = SendSerialState(psDevice);
  1095. }
  1096. else
  1097. {
  1098. //
  1099. // Our last notification completed and we didn't have any new
  1100. // notifications to make so the interrupt channel is now idle again.
  1101. //
  1102. psInst->eCDCInterruptState = CDC_STATE_IDLE;
  1103. }
  1104. //
  1105. // Tell the caller how things went.
  1106. //
  1107. return (bRetcode);
  1108. }
  1109. //*****************************************************************************
  1110. //
  1111. // Receives notifications related to data sent to the host.
  1112. //
  1113. // \param psDevice is the device instance whose endpoint is to be processed.
  1114. // \param ulStatus is the USB interrupt status that caused this function to
  1115. // be called.
  1116. //
  1117. // This function is called from HandleEndpoints for all interrupts originating
  1118. // from the bulk IN endpoint (in other words, whenever data has been
  1119. // transmitted to the USB host). We examine the cause of the interrupt and,
  1120. // if due to completion of a transmission, notify the client.
  1121. //
  1122. // \return Returns \b true on success or \b false on failure.
  1123. //
  1124. //*****************************************************************************
  1125. tBoolean
  1126. ProcessDataToHost(const tUSBDCDCDevice *psDevice, unsigned int ulStatus,
  1127. unsigned int ulIndex)
  1128. {
  1129. tCDCSerInstance *psInst;
  1130. unsigned int ulEPStatus, ulSize;
  1131. //
  1132. // Get a pointer to our instance data.
  1133. //
  1134. psInst = psDevice->psPrivateCDCSerData;
  1135. //
  1136. // Get the endpoint status to see why we were called.
  1137. //
  1138. ulEPStatus = USBEndpointStatus(psInst->ulUSBBase,
  1139. psInst->ucBulkINEndpoint);
  1140. //
  1141. // Clear the status bits.
  1142. //
  1143. USBDevEndpointStatusClear(psInst->ulUSBBase,
  1144. psInst->ucBulkINEndpoint, ulEPStatus);
  1145. //
  1146. // Our last transmission completed. Clear our state back to idle and
  1147. // see if we need to send any more data.
  1148. //
  1149. psInst->eCDCTxState = CDC_STATE_IDLE;
  1150. //
  1151. // Notify the client that the last transmission completed.
  1152. //
  1153. ulSize = (unsigned int)psInst->usLastTxSize;
  1154. psInst->usLastTxSize = 0;
  1155. psDevice->pfnTxCallback(psDevice->pvTxCBData, USB_EVENT_TX_COMPLETE,
  1156. ulSize, (void *)0);
  1157. return (true);
  1158. }
  1159. //*****************************************************************************
  1160. //
  1161. // Called by the USB stack for any activity involving one of our endpoints
  1162. // other than EP0. This function is a fan out that merely directs the call to
  1163. // the correct handler depending upon the endpoint and transaction direction
  1164. // signaled in ulStatus.
  1165. //
  1166. //*****************************************************************************
  1167. static void
  1168. HandleEndpoints(void *pvInstance, unsigned int ulStatus, unsigned int ulIndex)
  1169. {
  1170. const tUSBDCDCDevice *psDeviceInst;
  1171. tCDCSerInstance *psInst;
  1172. ASSERT(pvInstance != 0);
  1173. //
  1174. // Determine if the serial device is in single or composite mode because
  1175. // the meaning of ulIndex is different in both cases.
  1176. //
  1177. psDeviceInst = pvInstance;
  1178. psInst = psDeviceInst->psPrivateCDCSerData;
  1179. //
  1180. // Handler for the interrupt IN notification endpoint.
  1181. //
  1182. if(ulStatus & (1 << USB_EP_TO_INDEX(psInst->ucControlEndpoint)))
  1183. {
  1184. //
  1185. // We have sent an interrupt notification to the host.
  1186. //
  1187. ProcessNotificationToHost(psDeviceInst, ulStatus);
  1188. }
  1189. //
  1190. // Handler for the bulk OUT data endpoint.
  1191. //
  1192. if(ulStatus & (0x10000 << USB_EP_TO_INDEX(psInst->ucBulkOUTEndpoint)))
  1193. {
  1194. //
  1195. // Data is being sent to us from the host.
  1196. //
  1197. ProcessDataFromHost(psDeviceInst, ulStatus, ulIndex);
  1198. }
  1199. //
  1200. // Handler for the bulk IN data endpoint.
  1201. //
  1202. if(ulStatus & (1 << USB_EP_TO_INDEX(psInst->ucBulkINEndpoint)))
  1203. {
  1204. ProcessDataToHost(psDeviceInst, ulStatus, ulIndex);
  1205. }
  1206. }
  1207. //*****************************************************************************
  1208. //
  1209. // Called by the USB stack whenever a configuration change occurs.
  1210. //
  1211. //*****************************************************************************
  1212. static void
  1213. HandleConfigChange(void *pvInstance, unsigned int ulInfo, unsigned int ulIndex)
  1214. {
  1215. tCDCSerInstance *psInst;
  1216. const tUSBDCDCDevice *psDevice;
  1217. ASSERT(pvInstance != 0);
  1218. //
  1219. // Create a device instance pointer.
  1220. //
  1221. psDevice = (const tUSBDCDCDevice *)pvInstance;
  1222. //
  1223. // Get a pointer to our instance data.
  1224. //
  1225. psInst = psDevice->psPrivateCDCSerData;
  1226. //
  1227. // Set all our endpoints to idle state.
  1228. //
  1229. psInst->eCDCInterruptState = CDC_STATE_IDLE;
  1230. psInst->eCDCRequestState = CDC_STATE_IDLE;
  1231. psInst->eCDCRxState = CDC_STATE_IDLE;
  1232. psInst->eCDCTxState = CDC_STATE_IDLE;
  1233. //
  1234. // If we are not currently connected so let the client know we are open
  1235. // for business.
  1236. //
  1237. if(!psInst->bConnected)
  1238. {
  1239. //
  1240. // Pass the connected event to the client.
  1241. //
  1242. psDevice->pfnControlCallback(psDevice->pvControlCBData,
  1243. USB_EVENT_CONNECTED, 0, (void *)0);
  1244. }
  1245. //
  1246. // Remember that we are connected.
  1247. //
  1248. psInst->bConnected = true;
  1249. }
  1250. //*****************************************************************************
  1251. //
  1252. // USB data received callback.
  1253. //
  1254. // This function is called by the USB stack whenever any data requested from
  1255. // EP0 is received.
  1256. //
  1257. //*****************************************************************************
  1258. static void
  1259. HandleEP0Data(void *pvInstance, unsigned int ulDataSize, unsigned int ulIndex)
  1260. {
  1261. const tUSBDCDCDevice *psDevice;
  1262. tCDCSerInstance *psInst;
  1263. tBoolean bRetcode;
  1264. ASSERT(pvInstance != 0);
  1265. //
  1266. // Create a device instance pointer.
  1267. //
  1268. psDevice = (const tUSBDCDCDevice *)pvInstance;
  1269. //
  1270. // If we were not passed any data, just return.
  1271. //
  1272. if(ulDataSize == 0)
  1273. {
  1274. return;
  1275. }
  1276. //
  1277. // Get our instance data pointer.
  1278. //
  1279. psInst = psDevice->psPrivateCDCSerData;
  1280. //
  1281. // Make sure we are actually expecting something.
  1282. //
  1283. if(psInst->eCDCRequestState != CDC_STATE_WAIT_DATA)
  1284. {
  1285. return;
  1286. }
  1287. //
  1288. // Process the data received. This will be a request-specific data
  1289. // block associated with the last request received.
  1290. //
  1291. switch (psInst->ucPendingRequest)
  1292. {
  1293. //
  1294. // We just got the line coding structure. Make sure the client has
  1295. // read all outstanding data then pass it back to initiate a change
  1296. // in the line state.
  1297. //
  1298. case USB_CDC_SET_LINE_CODING:
  1299. {
  1300. if(ulDataSize != sizeof(tLineCoding))
  1301. {
  1302. USBDCDStallEP0(ulIndex);
  1303. }
  1304. else
  1305. {
  1306. //
  1307. // Set the flag telling us that we need to send a line coding
  1308. // notification to the client.
  1309. //
  1310. SetDeferredOpFlag(&psInst->usDeferredOpFlags,
  1311. CDC_DO_LINE_CODING_CHANGE, true);
  1312. //
  1313. // See if we can send the notification immediately.
  1314. //
  1315. bRetcode = CheckAndSendLineCodingChange(psDevice);
  1316. //
  1317. // If we couldn't send the line coding change request to the
  1318. // client, block reception of more data from the host until
  1319. // previous data is processed and we send the change request.
  1320. //
  1321. if(!bRetcode)
  1322. {
  1323. psInst->bRxBlocked = true;
  1324. }
  1325. }
  1326. break;
  1327. }
  1328. //
  1329. // Oops - we seem to be waiting on a request which has not yet been
  1330. // coded here. Flag the error and stall EP0 anyway (even though
  1331. // this would indicate a coding error).
  1332. //
  1333. default:
  1334. {
  1335. USBDCDStallEP0(ulIndex);ASSERT(ulIndex);
  1336. break;
  1337. }
  1338. }
  1339. //
  1340. // All is well. Set the state back to IDLE.
  1341. //
  1342. psInst->eCDCRequestState = CDC_STATE_IDLE;
  1343. }
  1344. //*****************************************************************************
  1345. //
  1346. // Device instance specific handler.
  1347. //
  1348. //*****************************************************************************
  1349. static void
  1350. HandleDevice(void *pvInstance, unsigned int ulRequest, void *pvRequestData)
  1351. {
  1352. tCDCSerInstance *psInst;
  1353. unsigned char *pucData;
  1354. //
  1355. // Create the serial instance data.
  1356. //
  1357. psInst = ((tUSBDCDCDevice *)pvInstance)->psPrivateCDCSerData;
  1358. //
  1359. // Create the char array used by the events supported by the USB CDC
  1360. // serial class.
  1361. //
  1362. pucData = (unsigned char *)pvRequestData;
  1363. switch(ulRequest)
  1364. {
  1365. //
  1366. // This was an interface change event.
  1367. //
  1368. case USB_EVENT_COMP_IFACE_CHANGE:
  1369. {
  1370. //
  1371. // Save the change to the appropriate interface number.
  1372. //
  1373. if(pucData[0] == SERIAL_INTERFACE_CONTROL)
  1374. {
  1375. psInst->ucInterfaceControl = pucData[1];
  1376. }
  1377. else if(pucData[0] == SERIAL_INTERFACE_DATA)
  1378. {
  1379. psInst->ucInterfaceData = pucData[1];
  1380. }
  1381. break;
  1382. }
  1383. //
  1384. // This was an endpoint change event.
  1385. //
  1386. case USB_EVENT_COMP_EP_CHANGE:
  1387. {
  1388. //
  1389. // Determine if this is an IN or OUT endpoint that has changed.
  1390. //
  1391. if(pucData[0] & USB_EP_DESC_IN)
  1392. {
  1393. //
  1394. // Determine which IN endpoint to modify.
  1395. //
  1396. if((pucData[0] & 0x7f) == USB_EP_TO_INDEX(CONTROL_ENDPOINT))
  1397. {
  1398. psInst->ucControlEndpoint =
  1399. INDEX_TO_USB_EP((pucData[1] & 0x7f));
  1400. }
  1401. else
  1402. {
  1403. psInst->ucBulkINEndpoint =
  1404. INDEX_TO_USB_EP((pucData[1] & 0x7f));
  1405. }
  1406. }
  1407. else
  1408. {
  1409. //
  1410. // Extract the new endpoint number.
  1411. //
  1412. psInst->ucBulkOUTEndpoint =
  1413. INDEX_TO_USB_EP(pucData[1] & 0x7f);
  1414. }
  1415. break;
  1416. }
  1417. //
  1418. // Handle class specific reconfiguring of the configuration descriptor
  1419. // once the composite class has built the full descriptor.
  1420. //
  1421. case USB_EVENT_COMP_CONFIG:
  1422. {
  1423. //
  1424. // This sets the bFirstInterface of the Interface Association
  1425. // descriptor to the first interface which is the control
  1426. // interface used by this instance.
  1427. //
  1428. pucData[2] = psInst->ucInterfaceControl;
  1429. //
  1430. // This sets the bMasterInterface of the Union descriptor to the
  1431. // Control interface and the bSlaveInterface of the Union
  1432. // Descriptor to the Data interface used by this instance.
  1433. //
  1434. pucData[29] = psInst->ucInterfaceControl;
  1435. pucData[30] = psInst->ucInterfaceData;
  1436. //
  1437. // This sets the bDataInterface of the Union descriptor to the
  1438. // Data interface used by this instance.
  1439. pucData[35] = psInst->ucInterfaceData;
  1440. break;
  1441. }
  1442. default:
  1443. {
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. //*****************************************************************************
  1449. //
  1450. // USB non-standard request callback.
  1451. //
  1452. // This function is called by the USB stack whenever any non-standard request
  1453. // is made to the device. The handler should process any requests that it
  1454. // supports or stall EP0 in any unsupported cases.
  1455. //
  1456. //*****************************************************************************
  1457. static void
  1458. HandleRequests(void *pvInstance, tUSBRequest *pUSBRequest,
  1459. unsigned int ulIndex)
  1460. {
  1461. const tUSBDCDCDevice *psDevice;
  1462. tCDCSerInstance *psInst;
  1463. tLineCoding sLineCoding;
  1464. tBoolean bRetcode;
  1465. ASSERT(pvInstance != 0);
  1466. //
  1467. // Create a device instance pointer.
  1468. //
  1469. psDevice = (const tUSBDCDCDevice *)pvInstance;
  1470. //
  1471. // Get our instance data pointer.
  1472. //
  1473. psInst = psDevice->psPrivateCDCSerData;
  1474. //
  1475. // Only handle requests meant for this interface.
  1476. //
  1477. if(pUSBRequest->wIndex != psInst->ucInterfaceControl)
  1478. {
  1479. return;
  1480. }
  1481. //
  1482. // Handle each of the requests that we expect from the host.
  1483. //
  1484. switch(pUSBRequest->bRequest)
  1485. {
  1486. case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  1487. {
  1488. //
  1489. // This implementation makes use of no communication protocol so
  1490. // this request is meaningless. We stall endpoint 0 if we receive
  1491. // it.
  1492. //
  1493. USBDCDStallEP0(ulIndex);
  1494. break;
  1495. }
  1496. case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  1497. {
  1498. //
  1499. // This implementation makes use of no communication protocol so
  1500. // this request is meaningless. We stall endpoint 0 if we receive
  1501. // it.
  1502. //
  1503. USBDCDStallEP0(ulIndex);
  1504. break;
  1505. }
  1506. case USB_CDC_SET_COMM_FEATURE:
  1507. {
  1508. //
  1509. // This request is apparently required by an ACM device but does
  1510. // not appear relevant to a virtual COM port and is never used by
  1511. // Windows (or, at least, is not seen when using Hyperterminal or
  1512. // TeraTerm via a Windows virtual COM port). We stall endpoint 0
  1513. // to indicate that we do not support the request.
  1514. //
  1515. USBDCDStallEP0(ulIndex);
  1516. break;
  1517. }
  1518. case USB_CDC_GET_COMM_FEATURE:
  1519. {
  1520. //
  1521. // This request is apparently required by an ACM device but does
  1522. // not appear relevant to a virtual COM port and is never used by
  1523. // Windows (or, at least, is not seen when using Hyperterminal or
  1524. // TeraTerm via a Windows virtual COM port). We stall endpoint 0
  1525. // to indicate that we do not support the request.
  1526. //
  1527. USBDCDStallEP0(ulIndex);
  1528. break;
  1529. }
  1530. case USB_CDC_CLEAR_COMM_FEATURE:
  1531. {
  1532. //
  1533. // This request is apparently required by an ACM device but does
  1534. // not appear relevant to a virtual COM port and is never used by
  1535. // Windows (or, at least, is not seen when using Hyperterminal or
  1536. // TeraTerm via a Windows virtual COM port). We stall endpoint 0
  1537. // to indicate that we do not support the request.
  1538. //
  1539. USBDCDStallEP0(ulIndex);
  1540. break;
  1541. }
  1542. //
  1543. // Set the serial communication parameters.
  1544. //
  1545. case USB_CDC_SET_LINE_CODING:
  1546. {
  1547. //
  1548. // Remember the request we are processing.
  1549. //
  1550. psInst->ucPendingRequest = USB_CDC_SET_LINE_CODING;
  1551. //
  1552. // Set the state to indicate we are waiting for data.
  1553. //
  1554. psInst->eCDCRequestState = CDC_STATE_WAIT_DATA;
  1555. //
  1556. // Now read the payload of the request. We handle the actual
  1557. // operation in the data callback once this data is received.
  1558. //
  1559. USBDCDRequestDataEP0(0, (unsigned char *)&psInst->sLineCoding,
  1560. sizeof(tLineCoding));
  1561. //
  1562. // ACK what we have already received. We must do this after
  1563. // requesting the data or we get into a race condition where the
  1564. // data may return before we have set the stack state appropriately
  1565. // to receive it.
  1566. //
  1567. USBDevEndpointDataAck(psInst->ulUSBBase, USB_EP_0, false);
  1568. break;
  1569. }
  1570. //
  1571. // Return the serial communication parameters.
  1572. //
  1573. case USB_CDC_GET_LINE_CODING:
  1574. {
  1575. //
  1576. // ACK what we have already received
  1577. //
  1578. USBDevEndpointDataAck(psInst->ulUSBBase, USB_EP_0, false);
  1579. //
  1580. // Ask the client for the current line coding.
  1581. //
  1582. psDevice->pfnControlCallback(psDevice->pvControlCBData,
  1583. USBD_CDC_EVENT_GET_LINE_CODING, 0,
  1584. &sLineCoding);
  1585. //
  1586. // Send the line coding information back to the host.
  1587. //
  1588. USBDCDSendDataEP0(0, (unsigned char *)&sLineCoding,
  1589. sizeof(tLineCoding));
  1590. break;
  1591. }
  1592. case USB_CDC_SET_CONTROL_LINE_STATE:
  1593. {
  1594. //
  1595. // ACK what we have already received
  1596. //
  1597. USBDevEndpointDataAck(psInst->ulUSBBase, USB_EP_0, false);
  1598. //
  1599. // Set the handshake lines as required.
  1600. //
  1601. psInst->usControlLineState = pUSBRequest->wValue;
  1602. //
  1603. // Remember that we are due to notify the client of a line
  1604. // state change.
  1605. //
  1606. SetDeferredOpFlag(&psInst->usDeferredOpFlags,
  1607. CDC_DO_LINE_STATE_CHANGE, true);
  1608. //
  1609. // See if we can notify now.
  1610. //
  1611. bRetcode = CheckAndSendLineStateChange(psDevice);
  1612. //
  1613. // If we couldn't send the line state change request to the
  1614. // client, block reception of more data from the host until
  1615. // previous data is processed and we send the change request.
  1616. //
  1617. if(!bRetcode)
  1618. {
  1619. psInst->bRxBlocked = true;
  1620. }
  1621. break;
  1622. }
  1623. case USB_CDC_SEND_BREAK:
  1624. {
  1625. //
  1626. // ACK what we have already received
  1627. //
  1628. USBDevEndpointDataAck(psInst->ulUSBBase, USB_EP_0, false);
  1629. //
  1630. // Keep a copy of the requested break duration.
  1631. //
  1632. psInst->usBreakDuration = pUSBRequest->wValue;
  1633. //
  1634. // Remember that we need to send a break request.
  1635. //
  1636. SetDeferredOpFlag(&psInst->usDeferredOpFlags,
  1637. CDC_DO_SEND_BREAK, true);
  1638. //
  1639. // Send the break request if all outstanding receive data has been
  1640. // processed.
  1641. //
  1642. bRetcode = CheckAndSendBreak(psDevice, pUSBRequest->wValue);
  1643. //
  1644. // If we couldn't send the line coding change request to the
  1645. // client, block reception of more data from the host until
  1646. // previous data is processed and we send the change request.
  1647. //
  1648. if(!bRetcode)
  1649. {
  1650. psInst->bRxBlocked = true;
  1651. }
  1652. break;
  1653. }
  1654. //
  1655. // These are valid CDC requests but not ones that an ACM device should
  1656. // receive.
  1657. //
  1658. case USB_CDC_SET_AUX_LINE_STATE:
  1659. case USB_CDC_SET_HOOK_STATE:
  1660. case USB_CDC_PULSE_SETUP:
  1661. case USB_CDC_SEND_PULSE:
  1662. case USB_CDC_SET_PULSE_TIME:
  1663. case USB_CDC_RING_AUX_JACK:
  1664. case USB_CDC_SET_RINGER_PARMS:
  1665. case USB_CDC_GET_RINGER_PARMS:
  1666. case USB_CDC_SET_OPERATION_PARMS:
  1667. case USB_CDC_GET_OPERATION_PARMS:
  1668. case USB_CDC_SET_LINE_PARMS:
  1669. case USB_CDC_GET_LINE_PARMS:
  1670. case USB_CDC_DIAL_DIGITS:
  1671. case USB_CDC_SET_UNIT_PARAMETER:
  1672. case USB_CDC_GET_UNIT_PARAMETER:
  1673. case USB_CDC_CLEAR_UNIT_PARAMETER:
  1674. case USB_CDC_GET_PROFILE:
  1675. case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  1676. case USB_CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER:
  1677. case USB_CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER:
  1678. case USB_CDC_SET_ETHERNET_PACKET_FILTER:
  1679. case USB_CDC_GET_ETHERNET_STATISTIC:
  1680. case USB_CDC_SET_ATM_DATA_FORMAT:
  1681. case USB_CDC_GET_ATM_DEVICE_STATISTICS:
  1682. case USB_CDC_SET_ATM_DEFAULT_VC:
  1683. case USB_CDC_GET_ATM_VC_STATISTICS:
  1684. {
  1685. USBDCDStallEP0(ulIndex);
  1686. break;
  1687. }
  1688. default:
  1689. {
  1690. //
  1691. // This request is not part of the CDC specification.
  1692. //
  1693. USBDCDStallEP0(ulIndex);
  1694. break;
  1695. }
  1696. }
  1697. }
  1698. //*****************************************************************************
  1699. //
  1700. // This function is called by the USB device stack whenever the device is
  1701. // disconnected from the host.
  1702. //
  1703. //*****************************************************************************
  1704. static void
  1705. HandleDisconnect(void *pvInstance)
  1706. {
  1707. const tUSBDCDCDevice *psCDCDevice;
  1708. tCDCSerInstance *psInst;
  1709. ASSERT(pvInstance != 0);
  1710. //
  1711. // Create the instance pointer.
  1712. //
  1713. psCDCDevice = (const tUSBDCDCDevice *)pvInstance;
  1714. //
  1715. // Get a pointer to our instance data.
  1716. //
  1717. psInst = psCDCDevice->psPrivateCDCSerData;
  1718. //
  1719. // If we are not currently connected and we have a control callback,
  1720. // let the client know we are open for business.
  1721. //
  1722. if(psInst->bConnected)
  1723. {
  1724. //
  1725. // Pass the disconnected event to the client.
  1726. //
  1727. psCDCDevice->pfnControlCallback(psCDCDevice->pvControlCBData,
  1728. USB_EVENT_DISCONNECTED, 0, (void *)0);
  1729. }
  1730. //
  1731. // Remember that we are no longer connected.
  1732. //
  1733. psInst->bConnected = false;
  1734. }
  1735. //*****************************************************************************
  1736. //
  1737. // This function is called by the USB device stack whenever the bus is put into
  1738. // suspend state.
  1739. //
  1740. //*****************************************************************************
  1741. static void
  1742. HandleSuspend(void *pvInstance)
  1743. {
  1744. const tUSBDCDCDevice *psCDCDevice;
  1745. ASSERT(pvInstance != 0);
  1746. //
  1747. // Create the instance pointer.
  1748. //
  1749. psCDCDevice = (const tUSBDCDCDevice *)pvInstance;
  1750. //
  1751. // Pass the event on to the client.
  1752. //
  1753. psCDCDevice->pfnControlCallback(psCDCDevice->pvControlCBData,
  1754. USB_EVENT_SUSPEND, 0, (void *)0);
  1755. }
  1756. //*****************************************************************************
  1757. //
  1758. // This function is called by the USB device stack whenever the bus is taken
  1759. // out of suspend state.
  1760. //
  1761. //*****************************************************************************
  1762. static void
  1763. HandleResume(void *pvInstance)
  1764. {
  1765. tUSBDCDCDevice *psCDCDevice;
  1766. ASSERT(pvInstance != 0);
  1767. //
  1768. // Create the instance pointer.
  1769. //
  1770. psCDCDevice = (tUSBDCDCDevice *)pvInstance;
  1771. //
  1772. // Pass the event on to the client.
  1773. //
  1774. psCDCDevice->pfnControlCallback(psCDCDevice->pvControlCBData,
  1775. USB_EVENT_RESUME, 0, (void *)0);
  1776. }
  1777. //*****************************************************************************
  1778. //
  1779. // This function is called periodically and provides us with a time reference
  1780. // and method of implementing delayed or time-dependent operations.
  1781. //
  1782. // \param ulIndex is the index of the USB controller for which this tick
  1783. // is being generated.
  1784. // \param ulTimemS is the elapsed time in milliseconds since the last call
  1785. // to this function.
  1786. //
  1787. // \return None.
  1788. //
  1789. //*****************************************************************************
  1790. static void
  1791. CDCTickHandler(void *pvInstance, unsigned int ulTimemS, unsigned int ulIndex)
  1792. {
  1793. tBoolean bCanSend;
  1794. const tUSBDCDCDevice *psDevice;
  1795. tCDCSerInstance *psInst;
  1796. unsigned int ulSize;
  1797. ASSERT(pvInstance != 0);
  1798. //
  1799. // Create the instance pointer.
  1800. //
  1801. psDevice = (const tUSBDCDCDevice *)pvInstance;
  1802. //
  1803. // Get our instance data pointer.
  1804. //
  1805. psInst = psDevice->psPrivateCDCSerData;
  1806. //
  1807. // Is there any outstanding operation that we should try to perform?
  1808. //
  1809. if(psInst->usDeferredOpFlags)
  1810. {
  1811. //
  1812. // Yes - we have at least one deferred operation pending. First check
  1813. // to see if it is time to turn off a break condition.
  1814. //
  1815. if(psInst->usDeferredOpFlags & (1 << CDC_DO_CLEAR_BREAK))
  1816. {
  1817. //
  1818. // Will our break timer expire this time?
  1819. //
  1820. if(psInst->usBreakDuration <= ulTimemS)
  1821. {
  1822. //
  1823. // Yes - turn off the break condition.
  1824. //
  1825. SendBreak(psDevice, false);
  1826. }
  1827. else
  1828. {
  1829. //
  1830. // We have not timed out yet. Decrement the break timer.
  1831. //
  1832. psInst->usBreakDuration -= (unsigned short)ulTimemS;
  1833. }
  1834. }
  1835. // Now check to see if the client has any data remaining to be
  1836. // processed. This information is needed by the remaining deferred
  1837. // operations which are waiting for the receive pipe to be emptied
  1838. // before they can be carried out.
  1839. //
  1840. bCanSend = DeviceConsumedAllData(psDevice);
  1841. //
  1842. // Has all outstanding data been consumed?
  1843. //
  1844. if(bCanSend)
  1845. {
  1846. //
  1847. // Yes - go ahead and notify the client of the various things
  1848. // it has been asked to do while we waited for data to be
  1849. // consumed.
  1850. //
  1851. //
  1852. // Do we need to start sending a break condition?
  1853. //
  1854. if(psInst->usDeferredOpFlags & (1 << CDC_DO_SEND_BREAK))
  1855. {
  1856. SendBreak(psDevice, true);
  1857. }
  1858. //
  1859. // Do we need to set the RTS/DTR states?
  1860. //
  1861. if(psInst->usDeferredOpFlags & (1 << CDC_DO_LINE_STATE_CHANGE))
  1862. {
  1863. SendLineStateChange(psDevice);
  1864. }
  1865. //
  1866. // Do we need to change the line coding parameters?
  1867. //
  1868. if(psInst->usDeferredOpFlags & (1 << CDC_DO_LINE_CODING_CHANGE))
  1869. {
  1870. SendLineCodingChange(psDevice);
  1871. }
  1872. //
  1873. // NOTE: We do not need to handle CDC_DO_SERIAL_STATE_CHANGE here
  1874. // since this is handled in the transmission complete notification
  1875. // for the control IN endpoint (ProcessNotificationToHost()).
  1876. //
  1877. //
  1878. // If all the deferred operations which caused the receive channel
  1879. // to be blocked are now handled, we can unblock receive and handle
  1880. // any packet that is currently waiting to be received.
  1881. //
  1882. if(!(psInst->usDeferredOpFlags & RX_BLOCK_OPS))
  1883. {
  1884. //
  1885. // We can remove the receive block.
  1886. //
  1887. psInst->bRxBlocked = false;
  1888. }
  1889. }
  1890. //
  1891. // Is the receive channel unblocked?
  1892. //
  1893. if(!psInst->bRxBlocked)
  1894. {
  1895. //
  1896. // Do we have a deferred receive waiting
  1897. //
  1898. if(psInst->usDeferredOpFlags & (1 << CDC_DO_PACKET_RX))
  1899. {
  1900. //
  1901. // Yes - how big is the waiting packet?
  1902. //
  1903. ulSize = USBEndpointDataAvail(psInst->ulUSBBase,
  1904. psInst->ucBulkOUTEndpoint);
  1905. // Tell the client that there is a packet waiting for it.
  1906. //
  1907. psDevice->pfnRxCallback(psDevice->pvRxCBData,
  1908. USB_EVENT_RX_AVAILABLE, ulSize,
  1909. (void *)0);
  1910. }
  1911. }
  1912. }
  1913. return;
  1914. }
  1915. //*****************************************************************************
  1916. //
  1917. //! Initializes CDC device operation when used with a composite device.
  1918. //!
  1919. //! \param ulIndex is the index of the USB controller in use.
  1920. //! \param psCDCDevice points to a structure containing parameters customizing
  1921. //! the operation of the CDC device.
  1922. //!
  1923. //! An application wishing to make use of a composite
  1924. //! USB CDC communication channel needs to call this function.
  1925. //! This function is used for initializing an instance related information of the
  1926. //! CDC device.
  1927. //!
  1928. //! \return Returns NULL on failure or the psCDCDevice pointer on success.
  1929. //
  1930. //*****************************************************************************
  1931. void *
  1932. USBDCDCCompositeInit(unsigned int ulIndex, const tUSBDCDCDevice *psCDCDevice)
  1933. {
  1934. tCDCSerInstance *psInst;
  1935. tDeviceDescriptor *psDevDesc;
  1936. //
  1937. // Check parameter validity.
  1938. //
  1939. ASSERT(ulIndex == 0);
  1940. ASSERT(psCDCDevice);
  1941. ASSERT(psCDCDevice->psPrivateCDCSerData);
  1942. ASSERT(psCDCDevice->pfnControlCallback);
  1943. ASSERT(psCDCDevice->pfnRxCallback);
  1944. ASSERT(psCDCDevice->pfnTxCallback);
  1945. if(ulIndex == 0)
  1946. {
  1947. g_USBInstance[ulIndex].uiUSBInstance = ulIndex;
  1948. g_USBInstance[ulIndex].uiBaseAddr = USB0_BASE;
  1949. g_USBInstance[ulIndex].uiSubBaseAddr = USB_0_OTGBASE;
  1950. g_USBInstance[ulIndex].uiInterruptNum = SYS_INT_USB0;
  1951. g_USBInstance[ulIndex].uiSubInterruptNum = SYS_INT_USBSSINT;
  1952. g_USBInstance[ulIndex].uiPHYConfigRegAddr = CFGCHIP2_USBPHYCTRL;
  1953. }
  1954. #if (USB_NUM_INSTANCE == 2)
  1955. else if(ulIndex == 1)
  1956. {
  1957. g_USBInstance[ulIndex].uiUSBInstance = ulIndex;
  1958. g_USBInstance[ulIndex].uiBaseAddr = USB1_BASE;
  1959. g_USBInstance[ulIndex].uiSubBaseAddr = USB_1_OTGBASE;
  1960. g_USBInstance[ulIndex].uiInterruptNum = SYS_INT_USB1;
  1961. g_USBInstance[ulIndex].uiSubInterruptNum = SYS_INT_USBSSINT;
  1962. g_USBInstance[ulIndex].uiPHYConfigRegAddr = CFGCHIP2_USB1PHYCTRL;
  1963. }
  1964. #endif
  1965. //
  1966. // Create an instance pointer to the private data area.
  1967. //
  1968. psInst = psCDCDevice->psPrivateCDCSerData;
  1969. //
  1970. // Set the default endpoint and interface assignments.
  1971. //
  1972. psInst->ucBulkINEndpoint = DATA_IN_ENDPOINT;
  1973. psInst->ucBulkOUTEndpoint = DATA_OUT_ENDPOINT;
  1974. psInst->ucInterfaceControl = SERIAL_INTERFACE_CONTROL;
  1975. psInst->ucInterfaceData = SERIAL_INTERFACE_DATA;
  1976. //
  1977. // By default do not use the interrupt control endpoint. The single
  1978. // instance CDC serial device will turn this on in USBDCDCInit();
  1979. //
  1980. psInst->ucControlEndpoint = CONTROL_ENDPOINT;
  1981. //
  1982. // Initialize the workspace in the passed instance structure.
  1983. //
  1984. psInst->psConfDescriptor = (tConfigDescriptor *)g_pCDCSerDescriptor;
  1985. psInst->psDevInfo = &g_sCDCSerDeviceInfo;
  1986. psInst->ulUSBBase = g_USBInstance[ulIndex].uiBaseAddr;
  1987. psInst->eCDCRxState = CDC_STATE_UNCONFIGURED;
  1988. psInst->eCDCTxState = CDC_STATE_UNCONFIGURED;
  1989. psInst->eCDCInterruptState = CDC_STATE_UNCONFIGURED;
  1990. psInst->eCDCRequestState = CDC_STATE_UNCONFIGURED;
  1991. psInst->ucPendingRequest = 0;
  1992. psInst->usBreakDuration = 0;
  1993. psInst->usSerialState = 0;
  1994. psInst->usDeferredOpFlags = 0;
  1995. psInst->usControlLineState = 0;
  1996. psInst->bRxBlocked = false;
  1997. psInst->bControlBlocked = false;
  1998. psInst->bConnected = false;
  1999. //
  2000. // Fix up the device descriptor with the client-supplied values.
  2001. //
  2002. psDevDesc = (tDeviceDescriptor *)psInst->psDevInfo->pDeviceDescriptor;
  2003. psDevDesc->idVendor = psCDCDevice->usVID;
  2004. psDevDesc->idProduct = psCDCDevice->usPID;
  2005. //
  2006. // Fix up the configuration descriptor with client-supplied values.
  2007. //
  2008. psInst->psConfDescriptor->bmAttributes = psCDCDevice->ucPwrAttributes;
  2009. psInst->psConfDescriptor->bMaxPower =
  2010. (unsigned char)(psCDCDevice->usMaxPowermA / 2);
  2011. //
  2012. // Plug in the client's string stable to the device information
  2013. // structure.
  2014. //
  2015. psInst->psDevInfo->ppStringDescriptors = psCDCDevice->ppStringDescriptors;
  2016. psInst->psDevInfo->ulNumStringDescriptors
  2017. = psCDCDevice->ulNumStringDescriptors;
  2018. //
  2019. // Register our tick handler (this must be done after USBDCDInit).
  2020. //
  2021. InternalUSBRegisterTickHandler(USB_TICK_HANDLER_DEVICE,
  2022. CDCTickHandler,
  2023. (void *)psCDCDevice);
  2024. //
  2025. // Return the pointer to the instance indicating that everything went well.
  2026. //
  2027. return((void *)psCDCDevice);
  2028. }
  2029. //*****************************************************************************
  2030. //
  2031. //! Initializes CDC device operation for a given USB controller.
  2032. //!
  2033. //! \param ulIndex is the index of the USB controller which is to be
  2034. //! initialized for CDC device operation.
  2035. //! \param psCDCDevice points to a structure containing parameters customizing
  2036. //! the operation of the CDC device.
  2037. //!
  2038. //! An application wishing to make use of a USB CDC communication channel and
  2039. //! appear as a virtual serial port on the host system must call this function
  2040. //! to initialize the USB controller and attach the device to the USB bus.
  2041. //! This function performs all required USB initialization.
  2042. //!
  2043. //! The value returned by this function is the \e psCDCDevice pointer passed
  2044. //! to it if successful. This pointer must be passed to all later calls to the
  2045. //! CDC class driver to identify the device instance.
  2046. //!
  2047. //! The USB CDC device class driver offers packet-based transmit and receive
  2048. //! operation. If the application would rather use block based communication
  2049. //! with transmit and receive buffers, USB buffers on the transmit and receive
  2050. //! channels may be used to offer this functionality.
  2051. //!
  2052. //! Transmit Operation:
  2053. //!
  2054. //! Calls to USBDCDCPacketWrite() must send no more than 64 bytes of data at a
  2055. //! time and may only be made when no other transmission is currently
  2056. //! outstanding.
  2057. //!
  2058. //! Once a packet of data has been acknowledged by the USB host, a
  2059. //! \b USB_EVENT_TX_COMPLETE event is sent to the application callback to
  2060. //! inform it that another packet may be transmitted.
  2061. //!
  2062. //! Receive Operation:
  2063. //!
  2064. //! An incoming USB data packet will result in a call to the application
  2065. //! callback with event \b USB_EVENT_RX_AVAILABLE. The application must then
  2066. //! call USBDCDCPacketRead(), passing a buffer capable of holding the received
  2067. //! packet to retrieve the data and acknowledge reception to the USB host. The
  2068. //! size of the received packet may be queried by calling
  2069. //! USBDCDCRxPacketAvailable().
  2070. //!
  2071. //! \note The application must not make any calls to the low level USB Device
  2072. //! API if interacting with USB via the CDC device class API. Doing so
  2073. //! will cause unpredictable (though almost certainly unpleasant) behavior.
  2074. //!
  2075. //! \return Returns NULL on failure or the psCDCDevice pointer on success.
  2076. //
  2077. //*****************************************************************************
  2078. void *
  2079. USBDCDCInit(unsigned int ulIndex, const tUSBDCDCDevice *psCDCDevice)
  2080. {
  2081. void *pvRet;
  2082. tCDCSerInstance *psInst;
  2083. //
  2084. // Initialize the internal state for this class.
  2085. //
  2086. pvRet = USBDCDCCompositeInit(ulIndex, psCDCDevice);
  2087. if(pvRet)
  2088. {
  2089. //
  2090. // Create an instance pointer to the private data area.
  2091. //
  2092. psInst = psCDCDevice->psPrivateCDCSerData;
  2093. //
  2094. // Set the instance data for this device so that USBDCDInit() call can
  2095. // have the instance data.
  2096. //
  2097. psInst->psDevInfo->pvInstance = (void *)psCDCDevice;
  2098. //
  2099. // Enable the default interrupt control endpoint if this class is not
  2100. // being used in a composite device.
  2101. //
  2102. psInst->ucControlEndpoint = CONTROL_ENDPOINT;
  2103. //
  2104. // Use the configuration descriptor with the interrupt control endpoint.
  2105. //
  2106. psInst->psDevInfo->ppConfigDescriptors = g_pCDCSerConfigDescriptors;
  2107. //
  2108. // All is well so now pass the descriptors to the lower layer and put
  2109. // the CDC device on the bus.
  2110. //
  2111. USBDCDInit(ulIndex, psInst->psDevInfo);
  2112. }
  2113. return(pvRet);
  2114. }
  2115. //*****************************************************************************
  2116. //
  2117. //! Shuts down the CDC device instance.
  2118. //!
  2119. //! \param pvInstance is the pointer to the device instance structure as returned
  2120. //! by USBDCDCInit().
  2121. //!
  2122. //! This function terminates CDC operation for the instance supplied and
  2123. //! removes the device from the USB bus. This function should not be called
  2124. //! if the CDC device is part of a composite device and instead the
  2125. //! USBDCompositeTerm() function should be called for the full composite
  2126. //! device.
  2127. //!
  2128. //! Following this call, the \e pvInstance instance should not me used in any
  2129. //! other calls.
  2130. //!
  2131. //! \return None.
  2132. //
  2133. //*****************************************************************************
  2134. void
  2135. USBDCDCTerm(void *pvInstance)
  2136. {
  2137. tCDCSerInstance *psInst;
  2138. int index;
  2139. ASSERT(pvInstance);
  2140. //
  2141. // Get a pointer to our instance data.
  2142. //
  2143. psInst = ((tUSBDCDCDevice *)pvInstance)->psPrivateCDCSerData;
  2144. //
  2145. // Terminate the requested instance.
  2146. //
  2147. USB_BASE_TO_INDEX(psInst->ulUSBBase, index);
  2148. USBDCDTerm(index);
  2149. psInst->ulUSBBase = 0;
  2150. psInst->psDevInfo = (tDeviceInfo *)0;
  2151. psInst->psConfDescriptor = (tConfigDescriptor *)0;
  2152. return;
  2153. }
  2154. //*****************************************************************************
  2155. //
  2156. //! Sets the client-specific pointer for the control callback.
  2157. //!
  2158. //! \param pvInstance is the pointer to the device instance structure as
  2159. //! returned by USBDCDCInit().
  2160. //! \param pvCBData is the pointer that client wishes to be provided on each
  2161. //! event sent to the control channel callback function.
  2162. //!
  2163. //! The client uses this function to change the callback pointer passed in
  2164. //! the first parameter on all callbacks to the \e pfnControlCallback function
  2165. //! passed on USBDCDCInit().
  2166. //!
  2167. //! If a client wants to make runtime changes in the callback pointer, it must
  2168. //! ensure that the psCDCDevice structure passed to USBDCDCInit() resides in
  2169. //! RAM. If this structure is in flash, callback pointer changes will not be
  2170. //! possible.
  2171. //!
  2172. //! \return Returns the previous callback pointer that was being used for
  2173. //! this instance's control callback.
  2174. //
  2175. //*****************************************************************************
  2176. void *
  2177. USBDCDCSetControlCBData(void *pvInstance, void *pvCBData)
  2178. {
  2179. void *pvOldValue;
  2180. ASSERT(pvInstance);
  2181. //
  2182. // Set the callback pointer for the control channel after remembering the
  2183. // previous value.
  2184. //
  2185. pvOldValue = ((tUSBDCDCDevice *)pvInstance)->pvControlCBData;
  2186. ((tUSBDCDCDevice *)pvInstance)->pvControlCBData = pvCBData;
  2187. //
  2188. // Return the previous callback data value.
  2189. //
  2190. return (pvOldValue);
  2191. }
  2192. //*****************************************************************************
  2193. //
  2194. //! Sets the client-specific data parameter for the receive channel callback.
  2195. //!
  2196. //! \param pvInstance is the pointer to the device instance structure as
  2197. //! returned by USBDCDCInit().
  2198. //! \param pvCBData is the pointer that client wishes to be provided on each
  2199. //! event sent to the receive channel callback function.
  2200. //!
  2201. //! The client uses this function to change the callback pointer passed in
  2202. //! the first parameter on all callbacks to the \e pfnRxCallback function
  2203. //! passed on USBDCDCInit().
  2204. //!
  2205. //! If a client wants to make runtime changes in the callback pointer, it must
  2206. //! ensure that the psCDCDevice structure passed to USBDCDCInit() resides in
  2207. //! RAM. If this structure is in flash, callback data changes will not be
  2208. //! possible.
  2209. //!
  2210. //! \return Returns the previous callback pointer that was being used for
  2211. //! this instance's receive callback.
  2212. //
  2213. //*****************************************************************************
  2214. void *
  2215. USBDCDCSetRxCBData(void *pvInstance, void *pvCBData)
  2216. {
  2217. void *pvOldValue;
  2218. ASSERT(pvInstance);
  2219. //
  2220. // Set the callback data for the receive channel after remembering the
  2221. // previous value.
  2222. //
  2223. pvOldValue = ((tUSBDCDCDevice *)pvInstance)->pvRxCBData;
  2224. ((tUSBDCDCDevice *)pvInstance)->pvRxCBData = pvCBData;
  2225. //
  2226. // Return the previous callback pointer.
  2227. //
  2228. return (pvOldValue);
  2229. }
  2230. //*****************************************************************************
  2231. //
  2232. //! Sets the client-specific data parameter for the transmit callback.
  2233. //!
  2234. //! \param pvInstance is the pointer to the device instance structure as
  2235. //! returned by USBDCDCInit().
  2236. //! \param pvCBData is the pointer that client wishes to be provided on each
  2237. //! event sent to the transmit channel callback function.
  2238. //!
  2239. //! The client uses this function to change the callback pointer passed in
  2240. //! the first parameter on all callbacks to the \e pfnTxCallback function
  2241. //! passed on USBDCDCInit().
  2242. //!
  2243. //! If a client wants to make runtime changes in the callback pointer, it must
  2244. //! ensure that the psCDCDevice structure passed to USBDCDCInit() resides in
  2245. //! RAM. If this structure is in flash, callback data changes will not be
  2246. //! possible.
  2247. //!
  2248. //! \return Returns the previous callback pointer that was being used for
  2249. //! this instance's transmit callback.
  2250. //
  2251. //*****************************************************************************
  2252. void *
  2253. USBDCDCSetTxCBData(void *pvInstance, void *pvCBData)
  2254. {
  2255. void *pvOldValue;
  2256. ASSERT(pvInstance);
  2257. //
  2258. // Set the callback data for the transmit channel after remembering the
  2259. // previous value.
  2260. //
  2261. pvOldValue = ((tUSBDCDCDevice *)pvInstance)->pvTxCBData;
  2262. ((tUSBDCDCDevice *)pvInstance)->pvTxCBData = pvCBData;
  2263. //
  2264. // Return the previous callback pointer.
  2265. //
  2266. return (pvOldValue);
  2267. }
  2268. //*****************************************************************************
  2269. //
  2270. //! Transmits a packet of data to the USB host via the CDC data interface.
  2271. //!
  2272. //! \param pvInstance is the pointer to the device instance structure as
  2273. //! returned by USBDCDCInit().
  2274. //! \param pcData points to the first byte of data which is to be transmitted.
  2275. //! \param ulLength is the number of bytes of data to transmit.
  2276. //! \param bLast indicates whether more data is to be written before a packet
  2277. //! should be scheduled for transmission. If \b true, the client will make
  2278. //! a further call to this function. If \b false, no further call will be
  2279. //! made and the driver should schedule transmission of a short packet.
  2280. //!
  2281. //! This function schedules the supplied data for transmission to the USB
  2282. //! host in a single USB packet. If no transmission is currently ongoing
  2283. //! the data is immediately copied to the relevant USB endpoint FIFO. If the
  2284. //! \e bLast parameter is \b true, the newly written packet is then scheduled
  2285. //! for transmission. Whenever a USB packet is acknowledged by the host, a
  2286. //! USB_EVENT_TX_COMPLETE event will be sent to the application transmit
  2287. //! callback indicating that more data can now be transmitted.
  2288. //!
  2289. //! The maximum value for ulLength is 64 bytes (the maximum USB packet size
  2290. //! for the bulk endpoints in use by CDC). Attempts to send more data than
  2291. //! this will result in a return code of 0 indicating that the data cannot be
  2292. //! sent.
  2293. //!
  2294. //! \return Returns the number of bytes actually sent. At this level, this
  2295. //! will either be the number of bytes passed (if less than or equal to the
  2296. //! maximum packet size for the USB endpoint in use and no outstanding
  2297. //! transmission ongoing) or 0 to indicate a failure.
  2298. //
  2299. //*****************************************************************************
  2300. unsigned int
  2301. USBDCDCPacketWrite(void *pvInstance, unsigned char *pcData,
  2302. unsigned int ulLength, tBoolean bLast)
  2303. {
  2304. tCDCSerInstance *psInst;
  2305. int iRetcode;
  2306. ASSERT(pvInstance);
  2307. //
  2308. // Get our instance data pointer
  2309. //
  2310. psInst = ((tUSBDCDCDevice *)pvInstance)->psPrivateCDCSerData;
  2311. //
  2312. // Can we send the data provided?
  2313. //
  2314. if((ulLength > DATA_IN_EP_MAX_SIZE) ||
  2315. (psInst->eCDCTxState != CDC_STATE_IDLE))
  2316. {
  2317. //
  2318. // Either the packet was too big or we are in the middle of sending
  2319. // another packet. Return 0 to indicate that we can't send this data.
  2320. //
  2321. return (0);
  2322. }
  2323. //
  2324. // Copy the data into the USB endpoint FIFO.
  2325. //
  2326. iRetcode = USBEndpointDataPut(psInst->ulUSBBase,
  2327. psInst->ucBulkINEndpoint, pcData,
  2328. ulLength);
  2329. //
  2330. // Did we copy the data successfully?
  2331. //
  2332. if(iRetcode != -1)
  2333. {
  2334. //
  2335. // Remember how many bytes we sent.
  2336. //
  2337. psInst->usLastTxSize += (unsigned short)ulLength;
  2338. //
  2339. // If this is the last call for this packet, schedule transmission.
  2340. //
  2341. if(bLast)
  2342. {
  2343. //
  2344. // Send the packet to the host if we have received all the data we
  2345. // can expect for this packet.
  2346. //
  2347. psInst->eCDCTxState = CDC_STATE_WAIT_DATA;
  2348. iRetcode = USBEndpointDataSend(psInst->ulUSBBase,
  2349. psInst->ucBulkINEndpoint,
  2350. USB_TRANS_IN);
  2351. }
  2352. }
  2353. //
  2354. // Did an error occur while trying to send the data?
  2355. //
  2356. if(iRetcode != -1)
  2357. {
  2358. //
  2359. // No - tell the caller we sent all the bytes provided.
  2360. //
  2361. return (ulLength);
  2362. }
  2363. else
  2364. {
  2365. //
  2366. // Yes - tell the caller we couldn't send the data.
  2367. //
  2368. return (0);
  2369. }
  2370. }
  2371. //*****************************************************************************
  2372. //
  2373. //! Reads a packet of data received from the USB host via the CDC data
  2374. //! interface.
  2375. //!
  2376. //! \param pvInstance is the pointer to the device instance structure as
  2377. //! returned by USBDCDCInit().
  2378. //! \param pcData points to a buffer into which the received data will be
  2379. //! written.
  2380. //! \param ulLength is the size of the buffer pointed to by pcData.
  2381. //! \param bLast indicates whether the client will make a further call to
  2382. //! read additional data from the packet.
  2383. //!
  2384. //! This function reads up to ulLength bytes of data received from the USB
  2385. //! host into the supplied application buffer.
  2386. //!
  2387. //! \note The \e bLast parameter is ignored in this implementation since the
  2388. //! end of a packet can be determined without relying upon the client to
  2389. //! provide this information.
  2390. //!
  2391. //! \return Returns the number of bytes of data read.
  2392. //
  2393. //*****************************************************************************
  2394. unsigned int
  2395. USBDCDCPacketRead(void *pvInstance, unsigned char *pcData,
  2396. unsigned int ulLength, tBoolean bLast)
  2397. {
  2398. unsigned int ulEPStatus, ulPkt;
  2399. unsigned int ulCount;
  2400. tCDCSerInstance *psInst;
  2401. int iRetcode;
  2402. ASSERT(pvInstance);
  2403. //
  2404. // Get our instance data pointer
  2405. //
  2406. psInst = ((tUSBDCDCDevice *)pvInstance)->psPrivateCDCSerData;
  2407. //
  2408. // Does the relevant endpoint FIFO have a packet waiting for us?
  2409. //
  2410. ulEPStatus = USBEndpointStatus(psInst->ulUSBBase,
  2411. psInst->ucBulkOUTEndpoint);
  2412. if(ulEPStatus & USB_DEV_RX_PKT_RDY)
  2413. {
  2414. //
  2415. // If receive is currently blocked or the buffer we were passed is
  2416. // (potentially) too small, set the flag telling us that we have a
  2417. // packet waiting but return 0.
  2418. //
  2419. if(psInst->bRxBlocked || psInst->bControlBlocked)
  2420. {
  2421. SetDeferredOpFlag(&psInst->usDeferredOpFlags,
  2422. CDC_DO_PACKET_RX, true);
  2423. return (0);
  2424. }
  2425. else
  2426. {
  2427. //
  2428. // It is OK to receive the new packet. How many bytes are
  2429. // available for us to receive?
  2430. //
  2431. ulPkt = USBEndpointDataAvail(psInst->ulUSBBase,
  2432. psInst->ucBulkOUTEndpoint);
  2433. //
  2434. // Get as much data as we can.
  2435. //
  2436. ulCount = ulLength;
  2437. iRetcode = USBEndpointDataGet(psInst->ulUSBBase,
  2438. psInst->ucBulkOUTEndpoint,
  2439. pcData, &ulCount);
  2440. //
  2441. // Did we read the last of the packet data?
  2442. //
  2443. if(ulCount == ulPkt)
  2444. {
  2445. //
  2446. // Clear the endpoint status so that we know no packet is
  2447. // waiting.
  2448. //
  2449. USBDevEndpointStatusClear(psInst->ulUSBBase,
  2450. psInst->ucBulkOUTEndpoint,
  2451. ulEPStatus);
  2452. //
  2453. // Acknowledge the data, thus freeing the host to send the
  2454. // next packet.
  2455. //
  2456. USBDevEndpointDataAck(psInst->ulUSBBase,
  2457. psInst->ucBulkOUTEndpoint,
  2458. true);
  2459. //
  2460. // Clear the flag we set to indicate that a packet read is
  2461. // pending.
  2462. //
  2463. SetDeferredOpFlag(&psInst->usDeferredOpFlags,
  2464. CDC_DO_PACKET_RX, false);
  2465. }
  2466. //
  2467. // If all went well, tell the caller how many bytes they got.
  2468. //
  2469. if(iRetcode != -1)
  2470. {
  2471. return (ulCount);
  2472. }
  2473. }
  2474. }
  2475. //
  2476. // No packet was available or an error occurred while reading so tell
  2477. // the caller no bytes were returned.
  2478. //
  2479. return (0);
  2480. }
  2481. //*****************************************************************************
  2482. //
  2483. //! Returns the number of free bytes in the transmit buffer.
  2484. //!
  2485. //! \param pvInstance is the pointer to the device instance structure as
  2486. //! returned by USBDCDCInit().
  2487. //!
  2488. //! This function returns the maximum number of bytes that can be passed on a
  2489. //! call to USBDCDCPacketWrite and accepted for transmission. The value
  2490. //! returned will be the maximum USB packet size (64) if no transmission is
  2491. //! currently outstanding or 0 if a transmission is in progress.
  2492. //!
  2493. //! \return Returns the number of bytes available in the transmit buffer.
  2494. //
  2495. //*****************************************************************************
  2496. unsigned int
  2497. USBDCDCTxPacketAvailable(void *pvInstance)
  2498. {
  2499. tCDCSerInstance *psInst;
  2500. ASSERT(pvInstance);
  2501. //
  2502. // Get our instance data pointer.
  2503. //
  2504. psInst = ((tUSBDCDCDevice *)pvInstance)->psPrivateCDCSerData;
  2505. //
  2506. // Do we have a packet transmission currently ongoing?
  2507. //
  2508. if(psInst->eCDCTxState != CDC_STATE_IDLE)
  2509. {
  2510. //
  2511. // We are not ready to receive a new packet so return 0.
  2512. //
  2513. return (0);
  2514. }
  2515. else
  2516. {
  2517. //
  2518. // We can receive a packet so return the max packet size for the
  2519. // relevant endpoint.
  2520. //
  2521. return (DATA_IN_EP_MAX_SIZE);
  2522. }
  2523. }
  2524. //*****************************************************************************
  2525. //
  2526. //! Determines whether a packet is available and, if so, the size of the
  2527. //! buffer required to read it.
  2528. //!
  2529. //! \param pvInstance is the pointer to the device instance structure as
  2530. //! returned by USBDCDCInit().
  2531. //!
  2532. //! This function may be used to determine if a received packet remains to be
  2533. //! read and allows the application to determine the buffer size needed to
  2534. //! read the data.
  2535. //!
  2536. //! \return Returns 0 if no received packet remains unprocessed or the
  2537. //! size of the packet if a packet is waiting to be read.
  2538. //
  2539. //*****************************************************************************
  2540. unsigned int
  2541. USBDCDCRxPacketAvailable(void *pvInstance)
  2542. {
  2543. unsigned int ulEPStatus;
  2544. unsigned int ulSize;
  2545. tCDCSerInstance *psInst;
  2546. ASSERT(pvInstance);
  2547. //
  2548. // Get our instance data pointer
  2549. //
  2550. psInst = ((tUSBDCDCDevice *)pvInstance)->psPrivateCDCSerData;
  2551. //
  2552. // If receive is currently blocked, return 0.
  2553. //
  2554. if(psInst->bRxBlocked || psInst->bControlBlocked)
  2555. {
  2556. return (0);
  2557. }
  2558. //
  2559. // Does the relevant endpoint FIFO have a packet waiting for us?
  2560. //
  2561. ulEPStatus = USBEndpointStatus(psInst->ulUSBBase,
  2562. psInst->ucBulkOUTEndpoint);
  2563. if(ulEPStatus & USB_DEV_RX_PKT_RDY)
  2564. {
  2565. //
  2566. // Yes - a packet is waiting. How big is it?
  2567. //
  2568. ulSize = USBEndpointDataAvail(psInst->ulUSBBase,
  2569. psInst->ucBulkOUTEndpoint);
  2570. return (ulSize);
  2571. }
  2572. else
  2573. {
  2574. //
  2575. // There is no packet waiting to be received.
  2576. //
  2577. return (0);
  2578. }
  2579. }
  2580. //*****************************************************************************
  2581. //
  2582. //! Informs the CDC module of changes in the serial control line states or
  2583. //! receive error conditions.
  2584. //!
  2585. //! \param pvInstance is the pointer to the device instance structure as
  2586. //! returned by USBDCDCInit().
  2587. //! \param usState indicates the states of the various control lines and
  2588. //! any receive errors detected. Bit definitions are as for the USB CDC
  2589. //! SerialState asynchronous notification and are defined in header file
  2590. //! usbcdc.h.
  2591. //!
  2592. //! The application should call this function whenever the state of any of
  2593. //! the incoming RS232 handshake signals changes or in response to a receive
  2594. //! error or break condition. The usState parameter is the ORed combination
  2595. //! of the following flags with each flag indicating the presence of that
  2596. //! condition.
  2597. //!
  2598. //! - USB_CDC_SERIAL_STATE_OVERRUN
  2599. //! - USB_CDC_SERIAL_STATE_PARITY
  2600. //! - USB_CDC_SERIAL_STATE_FRAMING
  2601. //! - USB_CDC_SERIAL_STATE_RING_SIGNAL
  2602. //! - USB_CDC_SERIAL_STATE_BREAK
  2603. //! - USB_CDC_SERIAL_STATE_TXCARRIER
  2604. //! - USB_CDC_SERIAL_STATE_RXCARRIER
  2605. //!
  2606. //! This function should be called only when the state of any flag changes.
  2607. //!
  2608. //! \return None.
  2609. //
  2610. //*****************************************************************************
  2611. void
  2612. USBDCDCSerialStateChange(void *pvInstance, unsigned short usState)
  2613. {
  2614. tCDCSerInstance *psInst;
  2615. ASSERT(pvInstance);
  2616. //
  2617. // Get our instance data pointer
  2618. //
  2619. psInst = ((tUSBDCDCDevice *)pvInstance)->psPrivateCDCSerData;
  2620. //
  2621. // Add the newly reported state bits to the current collection. We do this
  2622. // in case two state changes occur back-to-back before the first has been
  2623. // notified. There are two distinct types of signals that we report here
  2624. // and we deal with them differently:
  2625. //
  2626. // 1. Errors (overrun, parity, framing error) are ORed together so that
  2627. // any reported error is sent on the next notification.
  2628. // 2. Signal line states (RI, break, TX carrier, RX carrier) always
  2629. // report the last state notified to us. The implementation here will
  2630. // send an interrupt showing the last state but, if two state changes
  2631. // occur very quickly, the host may receive a notification containing
  2632. // the same state that was last reported (in other words, a short pulse
  2633. // will be lost). It would be possible to reduce the likelihood of
  2634. // this happening by building a queue of state changes and sending
  2635. // these in order but you are left with exactly the same problem if the
  2636. // queue fills up. For now, therefore, we run the risk of missing very
  2637. // short pulses on the "steady-state" signal lines.
  2638. //
  2639. psInst->usSerialState |= (usState & USB_CDC_SERIAL_ERRORS);
  2640. psInst->usSerialState &= ~USB_CDC_SERIAL_ERRORS;
  2641. psInst->usSerialState |= (usState & ~USB_CDC_SERIAL_ERRORS);
  2642. //
  2643. // Set the flag indicating that a serial state change is to be sent.
  2644. //
  2645. SetDeferredOpFlag(&psInst->usDeferredOpFlags, CDC_DO_SERIAL_STATE_CHANGE,
  2646. true);
  2647. //
  2648. // Can we send the state change immediately?
  2649. //
  2650. if(psInst->eCDCInterruptState == CDC_STATE_IDLE)
  2651. {
  2652. //
  2653. // The interrupt channel is free so send the notification immediately.
  2654. // If we can't do this, the tick timer will catch this next time
  2655. // round.
  2656. //
  2657. psInst->eCDCInterruptState = CDC_STATE_WAIT_DATA;
  2658. SendSerialState(pvInstance);
  2659. }
  2660. return;
  2661. }
  2662. //*****************************************************************************
  2663. //
  2664. //! Reports the device power status (bus- or self-powered) to the USB library.
  2665. //!
  2666. //! \param pvInstance is the pointer to the CDC device instance structure.
  2667. //! \param ucPower indicates the current power status, either \b
  2668. //! USB_STATUS_SELF_PWR or \b USB_STATUS_BUS_PWR.
  2669. //!
  2670. //! Applications which support switching between bus- or self-powered
  2671. //! operation should call this function whenever the power source changes
  2672. //! to indicate the current power status to the USB library. This information
  2673. //! is required by the USB library to allow correct responses to be provided
  2674. //! when the host requests status from the device.
  2675. //!
  2676. //! \return None.
  2677. //
  2678. //*****************************************************************************
  2679. void
  2680. USBDCDCPowerStatusSet(void *pvInstance, unsigned char ucPower)
  2681. {
  2682. ASSERT(pvInstance);
  2683. //
  2684. // Pass the request through to the lower layer.
  2685. //
  2686. USBDCDPowerStatusSet(0, ucPower);
  2687. }
  2688. //*****************************************************************************
  2689. //
  2690. //! Requests a remote wakeup to resume communication when in suspended state.
  2691. //!
  2692. //! \param pvInstance is the pointer to the CDC device instance structure.
  2693. //!
  2694. //! When the bus is suspended, an application which supports remote wakeup
  2695. //! (advertised to the host via the config descriptor) may call this function
  2696. //! to initiate remote wakeup signaling to the host. If the remote wakeup
  2697. //! feature has not been disabled by the host, this will cause the bus to
  2698. //! resume operation within 20mS. If the host has disabled remote wakeup,
  2699. //! \b false will be returned to indicate that the wakeup request was not
  2700. //! successful.
  2701. //!
  2702. //! \return Returns \b true if the remote wakeup is not disabled and the
  2703. //! signaling was started or \b false if remote wakeup is disabled or if
  2704. //! signaling is currently ongoing following a previous call to this function.
  2705. //
  2706. //*****************************************************************************
  2707. tBoolean
  2708. USBDCDCRemoteWakeupRequest(void *pvInstance)
  2709. {
  2710. ASSERT(pvInstance);
  2711. //
  2712. // Pass the request through to the lower layer.
  2713. //
  2714. return(USBDCDRemoteWakeupRequest(0));
  2715. }
  2716. //*****************************************************************************
  2717. //
  2718. // Close the Doxygen group.
  2719. //! @}
  2720. //
  2721. //*****************************************************************************