picusb.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. This work is licensed under the Creative Commons Attribution 3.0 Unported License.
  3. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/
  4. or send a letter to
  5. Creative Commons,
  6. 171 Second Street,
  7. Suite 300,
  8. San Francisco,
  9. California,
  10. 94105,
  11. USA.
  12. // JTR v0.2a 26th Jan 2012
  13. // JTR v0.2b 10th March 2012
  14. Comments by JTR.
  15. This file is a combined header for all the PIC18F and PIC24F
  16. USB PICs for the Honken USB stack. It has been corrected for
  17. the PIC24 parts and slightly rearranged to reduce duplicate
  18. entries common to both PIC families.
  19. */
  20. #ifndef _picusb
  21. #define _picusb
  22. // JTR TODO Add support for the PIC24E and dsPIC33E parts when they are released.
  23. #if defined(__18F2450) || defined(__18F2455) || defined(__18F2458) || defined(__18F2550) || defined(__18F2553)|| defined(__18F4450) || defined(__18F4455) || defined(__18F4458) || defined(__18F4550) || defined(__18F4553)
  24. #define PIC_18F
  25. #define PIC18Fxx5x
  26. #define BD_RAM = 0x400
  27. #elif defined(__18F13K50) || defined(__18F14K50) //JTR add
  28. #define PIC_18F
  29. #define PIC18FxxK
  30. #define BD_RAM 0x200
  31. #elif defined(__18F24J50) || defined(__18F25J50) || defined(__18F26J50) || defined(__18F44J50) || defined(__18F45J50) || defined(__18F46J50)
  32. #define PIC_18F
  33. #define IS_18J
  34. #define PLLEN_REQD
  35. #define BD_RAM = 0x400
  36. #elif defined(__18F26J53) || defined(__18F27J53) || defined(__18F46J53) || defined(__18F47J53)
  37. #define PIC_18F
  38. #define IS_18J
  39. #define PLLEN_REQD
  40. #define BD_RAM = 0xd00
  41. #elif defined(__18F65J50) || defined(__18F66J50) || defined(__18F66J55) || defined(__18F67J50) || defined(__18F85J50) || defined(__18F86J50) || defined(__18F66J55) || defined(__18F67J50)
  42. #define PIC_18F
  43. #define IS_18J
  44. #define PLLEN_REQD
  45. #define USE_ALT_ANCON
  46. #define BD_RAM = 0x400
  47. #elif defined(__PIC24FJ64GB106__) || defined(__PIC24FJ128GB106__) || defined(__PIC24FJ192GB106__) || defined(__PIC24FJ256GB106__) || defined(__PIC24FJ64GB108__) || defined(__PIC24FJ128GB108__) || defined(__PIC24FJ192GB108__) || defined(__PIC24FJ256GB108__) || defined(__PIC24FJ64GB110__) || defined(__PIC24FJ128GB110__) || defined(__PIC24FJ192GB110__) || defined(__PIC24FJ256GB110__)
  48. #define PIC_24F
  49. #elif defined(__PIC24FJ32GB002__) || defined(__PIC24FJ32GB004__) || defined(__PIC24FJ64GB002__) || defined(__PIC24FJ64GB004__)
  50. #define PIC_24F
  51. #define PLLEN_REQD
  52. #elif defined(__PIC24FJ128GB210__) || defined(__PIC24FJ128GB206__) || defined(PIC24FJ256GB206__) || defined(PIC24FJ256GB210__)
  53. #define PIC_24F
  54. #define USE_ANS
  55. #elif defined(__PIC24FJ128DA106__) || defined(__PIC24FJ128DA110__) || defined(__PIC24FJ128DA206__) || defined(__PIC24FJ128DA210__) || defined(__PIC24FJ256DA106__) || defined(__PIC24FJ256DA110__) || defined(_PIC24FJ256DA206__) || defined(__PIC24FJ256DA210__)
  56. #define PIC_24F
  57. #define PLLEN_REQD
  58. #define USE_ANS
  59. #endif
  60. #if defined(__18F13K50) || defined(__18F14K50) //JTR add
  61. #define MAX_CHIP_EP (8u)
  62. #else
  63. #define MAX_CHIP_EP (16u)
  64. #endif
  65. /* COMMON PIC DEFINES TO BOTH PIC18 AND PIC24 */
  66. // JTR moved from usb_stack.h
  67. #define UOWN 0x80
  68. #define DTS 0x40
  69. #define KEN 0x20
  70. #define INCDIS 0x10
  71. #define DTSEN 0x08
  72. #define BSTALL 0x04
  73. #define BC98 0x03
  74. #define USB_DIR_OUT 0
  75. #define USB_DIR_IN 1
  76. #define USB_PP_EVEN 0
  77. #define USB_PP_ODD 1
  78. /* PIC DEFINES SPECIFIC TO PIC18 */
  79. #if defined(PIC_18F)
  80. #define MyProcessor // JTR check that a PIC is defined
  81. #include <p18cxxx.h>
  82. #define USTAT_ODD_EVEN (2) // JTR may be required for ping-pong BD* calculations and are different from PIC18 to PIC24
  83. #define USTAT_ODD_EVEN_SHIFT (1)
  84. // JTR removed not used and the values???
  85. // #define USB_EP_INTERRUPT (0)
  86. // #define USB_EP_BULK (1)
  87. // #define USB_EP_ISOCHRONOUS (2)
  88. #define USB_UEP_EPSTALL (0x01) // JTR Note. to self. these values are different between the PIC18 and PIC24
  89. #define USB_UEP_EPINEN (0x02)
  90. #define USB_UEP_EPOUTEN (0x04)
  91. #define USB_UEP_EPCONDIS (0x08)
  92. #define USB_UEP_EPHSHK (0x10)
  93. #define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS)
  94. #define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN)
  95. #define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS)
  96. #define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS)
  97. #define USB_EP_NONE (0x00)
  98. typedef unsigned char usb_uep_t;
  99. #define USB_UEP ((usb_uep_t*) (&UEP0))
  100. #define USB_UEP0 UEP0
  101. #define USB_UEP1 UEP1
  102. #define USB_UEP2 UEP2
  103. #define USB_UEP3 UEP3
  104. #define USB_UEP4 UEP4
  105. #define USB_UEP5 UEP5
  106. #define USB_UEP6 UEP6
  107. #define USB_UEP7 UEP7
  108. #ifndef PIC18FxxK
  109. #define USB_UEP8 UEP8
  110. #define USB_UEP9 UEP9
  111. #define USB_UEP10 UEP10
  112. #define USB_UEP11 UEP11
  113. #define USB_UEP12 UEP12
  114. #define USB_UEP13 UEP13
  115. #define USB_UEP14 UEP14
  116. #define USB_UEP15 UEP15
  117. #endif
  118. /* Interrupt */
  119. #define USB_SOF 0x40
  120. #define USB_STALL 0x20
  121. #define USB_IDLE 0x10
  122. #define USB_TRN 0x08
  123. #define USB_RESUM 0x04
  124. #define USB_UERR 0x02
  125. #define USB_URST 0x01
  126. #define USB_RESET_FLAG UIRbits.URSTIF
  127. #define USB_ERROR_FLAG UIRbits.UERRIF
  128. #define USB_RESUME_FLAG UIRbits.ACTVIF
  129. #define USB_IDLE_FLAG UIRbits.IDLEIF
  130. #define USB_STALL_FLAG UIRbits.STALLIF
  131. #define USB_SOF_FLAG UIRbits.SOFIF
  132. #define USB_TRANSACTION_FLAG UIRbits.TRNIF
  133. #define UsbInterruptFlags() (UIR)
  134. #define UsbErrorInterruptFlags() (UEIR)
  135. #define ClearGlobalUsbInterruptFlag() PIR2bits.USBIF = 0
  136. #define TestUsbTrfInterruptFlag() UIR (x)
  137. #define ClearUsbInterruptFlag(x) UIR &= ~(x)
  138. #define ClearAllUsbInterruptFlags() UIR = 0
  139. #define ClearUsbErrorInterruptFlag(x) UEIR &= ~(x)
  140. #define ClearAllUsbErrorInterruptFlags() UEIR = 0
  141. #define DisableGlobalUsbInterrupt() PIE2bits.USBIE = 0
  142. #define DisableUsbInterrupt(x) UIE &= ~(x)
  143. #define DisableAllUsbInterrupts() UIE = 0
  144. #define DisableUsbErrorInterrupt(x) UEIE &= ~(x)
  145. #define DisableAllUsbErrorInterrupts() UEIE = 0
  146. #define EnableUsbGlobalInterrupt() PIE2bits.USBIE = 1
  147. #define EnableUsbPerifInterrupts(x) UIE |= (x)
  148. #define TestGlobalUsbInterruptEnable() (PIE2bits.USBIE)
  149. #define EnableAllUsbInterrupts() UIE = 0xFF
  150. #define EnableUsbErrorInterrupt(x) UEIE |= (x)
  151. #define EnableAllUsbErrorInterrupts() UEIE = 0xFF
  152. // Depreciated and eliminated March 10 2012
  153. //#define EnableUsbHighPriInterrupt() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 1; INTCONbits.GIEH = 1;} while(0) // JTR new
  154. //#define EnableUsbLowPriInterrupt() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 0; INTCONbits.GIEL = 1;} while(0) // JTR new
  155. // JTR TODO define for NO priority interrupt.
  156. /* UCON */
  157. #define ResetPPbuffers() do {UCONbits.PPBRST = 1; UCONbits.PPBRST=0;} while(0)
  158. #define SingleEndedZeroIsSet() (UCONbits.SE0)
  159. #define EnablePacketTransfer() UCONbits.PKTDIS = 0
  160. #define EnableUsb() UCONbits.USBEN = 1
  161. #define SignalResume() do {UCONbits.RESUME = 1; delay_ms(10); UCONbits.RESUME = 0;} while(0)
  162. #define SuspendUsb() UCONbits.SUSPND = 1
  163. #define WakeupUsb() do {UCONbits.SUSPND = 0; while(USB_RESUME_FLAG){USB_RESUME_FLAG = 0;}} while(0)
  164. /* UADDR */
  165. #define SetUsbAddress(x) (UADDR = (x))
  166. #define GetUsbAddress() (UADDR)
  167. /* USTAT */
  168. // typedef unsigned char usb_status_t;
  169. #define GetUsbTransaction() (USTAT)
  170. #define USB_STAT2EP(x) ((x>>3)&0x0F)
  171. #define USB_STAT2DIR(x) ((x>>2)&0x01)
  172. #define USB_STAT2ADDR(x) ((x>>2)&0x1F)
  173. #define USB_STAT2PPI(x) ((x>>1)&0x01)
  174. #define DIRBIT 0x4 // JTR addition. Different between PIC18 and PIC24
  175. /* Hardware implementations */
  176. #if defined USB_INTERNAL_PULLUPS
  177. #define USB_UCFG_UPUEN_VALUE (1<<4)
  178. #elif defined USB_EXTERNAL_PULLUPS
  179. #define USB_UCFG_UPUEN_VALUE (0)
  180. #else
  181. #error "Neither internal nor external pullups defined"
  182. #endif
  183. #if defined USB_INTERNAL_TRANSCIEVER
  184. #define USB_UCFG_UTRDIS_VALUE (0)
  185. #elif defined USB_EXTERNAL_TRANSCIEVER
  186. #define USB_UCFG_UTRDIS_VALUE (1<<3)
  187. #else
  188. #error "Neither internal nor external transciever defined"
  189. #endif
  190. #if defined USB_FULL_SPEED_DEVICE
  191. #define USB_UCFG_FSEN_VALUE (1<<2)
  192. #elif defined USB_LOW_SPEED_DEVICE
  193. #define USB_UCFG_FSEN_VALUE (0)
  194. #else
  195. #error "Neither internal nor external pullups defined"
  196. #endif
  197. #if defined USB_BUS_POWERED
  198. #ifndef usb_low_power_request
  199. /* Default low power mode is DUD */
  200. #define usb_low_power_request() Nop()
  201. #endif
  202. #ifndef usb_low_power_resume
  203. #define usb_low_power_resume() Nop()
  204. #endif
  205. #elif defined USB_SELF_POWERED
  206. #define usb_low_power_request() Nop()
  207. #define usb_low_power_resume() Nop()
  208. #else
  209. #error "No source of device power defined"
  210. #endif
  211. #ifndef USB_INTERNAL_VREG
  212. #warning "Use of internal voltage regulator not defined. User must supply 3.3V on Vusb pin."
  213. #endif
  214. //#define USB_DIR_OUT 0
  215. //#define USB_DIR_IN 1
  216. //#define USB_PP_EVEN 0
  217. //#define USB_PP_ODD 1
  218. /* PingPong buffer descriptor table index calculations */
  219. #if USB_PP_BUF_MODE == NO_PINGPONG
  220. #define USB_USTAT2BD(X) ( (X)/4 )
  221. #define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) )
  222. #elif USB_PP_BUF_MODE == 1
  223. #error "USB_PP_BUF_MODE outside scope."
  224. #define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 )
  225. #define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) )
  226. #elif USB_PP_BUF_MODE == 2
  227. #error "USB_PP_BUF_MODE outside scope."
  228. #define USB_USTAT2BD(X) ( (X)/2 )
  229. #define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) )
  230. #elif USB_PP_BUF_MODE == 3
  231. #error "USB_PP_BUF_MODE outside scope."
  232. #define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 )
  233. #define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 )
  234. #else
  235. #error "USB_PP_BUF_MODE outside scope."
  236. #endif
  237. #define USB_UCFG_REGISTER_VALUE ((USB_UCFG_UPUEN_VALUE) | \
  238. (USB_UCFG_UTRDIS_VALUE) | \
  239. (USB_UCFG_FSEN_VALUE) | \
  240. (USB_PP_BUF_MODE))
  241. #define ConfigureUsbHardware() do {UCFG = USB_UCFG_REGISTER_VALUE;} while(0)
  242. #define ROM far rom
  243. #define ROMPTR far rom
  244. #define ARCH_memcpy memcpypgm2ram
  245. typedef struct BDENTRY {
  246. unsigned char
  247. /* struct {
  248. unsigned BCH:2;
  249. unsigned BSTALL:1;
  250. unsigned DTSEN:1;
  251. unsigned INCDIS:1;
  252. unsigned KEN:1;
  253. unsigned DTS:1;
  254. unsigned UOWN:1;
  255. }*/
  256. BDSTAT;
  257. unsigned char BDCNT;
  258. unsigned char *BDADDR;
  259. } BDentry;
  260. #endif // End of specific PIC18 defines.
  261. /* PIC DEFINES SPECIFIC TO PIC24 FOLLOW */
  262. #if defined(PIC_24F)
  263. #define MyProcessor
  264. #include <p24fxxxx.h>
  265. #define USTAT_ODD_EVEN (4) // JTR PIC24 fixup potentially ?? Only required when ping-pong buffering is enabled.
  266. #define USTAT_ODD_EVEN_SHIFT (2) // JTR these are required for BD* calculations and are different for the PIC24
  267. /* Bitmasks */
  268. #define USB_UEP_EPHSHK (0x01)
  269. #define USB_UEP_EPSTALL (0x02)
  270. #define USB_UEP_EPINEN (0x04)
  271. #define USB_UEP_EPOUTEN (0x08)
  272. #define USB_UEP_EPCONDIS (0x10)
  273. #define USB_EP_INOUT (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS)
  274. #define USB_EP_CONTROL (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPOUTEN)
  275. #define USB_EP_OUT (USB_UEP_EPHSHK | USB_UEP_EPOUTEN | USB_UEP_EPCONDIS)
  276. #define USB_EP_IN (USB_UEP_EPHSHK | USB_UEP_EPINEN | USB_UEP_EPCONDIS)
  277. #define USB_EP_NONE (0x00)
  278. // JTR removed not used and the values??
  279. // #define USB_EP_INTERRUPT (0)
  280. // #define USB_EP_BULK (1)
  281. // #define USB_EP_ISOCHRONOUS (2)
  282. typedef unsigned int usb_uep_t; // JTR PIC24 fixup potentially ?? changed from char.
  283. // See http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=1651&start=150#p17703
  284. // However the stack as modified by JTR does not actually use it. Then again it might as it is tested and works.
  285. #define USB_UEP ((usb_uep_t*)(&U1EP0))
  286. #define USB_UEP0 U1EP0
  287. #define USB_UEP1 U1EP1
  288. #define USB_UEP2 U1EP2
  289. #define USB_UEP3 U1EP3
  290. #define USB_UEP4 U1EP4
  291. #define USB_UEP5 U1EP5
  292. #define USB_UEP6 U1EP6
  293. #define USB_UEP7 U1EP7
  294. #define USB_UEP8 U1EP8
  295. #define USB_UEP9 U1EP9
  296. #define USB_UEP10 U1EP10
  297. #define USB_UEP11 U1EP11
  298. #define USB_UEP12 U1EP12
  299. #define USB_UEP13 U1EP13
  300. #define USB_UEP14 U1EP14
  301. #define USB_UEP15 U1EP15
  302. /* Interrupt */
  303. #define USB_STALL 0x0080
  304. #define USB_RESUM 0x0020
  305. #define USB_IDLE 0x0010
  306. #define USB_TRN 0x0008
  307. #define USB_SOF 0x0004
  308. #define USB_UERR 0x0002
  309. #define USB_URST 0x0001
  310. #define USB_RESET_FLAG U1IRbits.URSTIF
  311. #define USB_ERROR_FLAG U1IRbits.UERRIF
  312. #define USB_RESUME_FLAG U1IRbits.RESUMEIF
  313. #define USB_IDLE_FLAG U1IRbits.IDLEIF
  314. #define USB_STALL_FLAG U1IRbits.STALLIF
  315. #define USB_SOF_FLAG U1IRbits.SOFIF
  316. #define USB_TRANSACTION_FLAG U1IRbits.TRNIF
  317. #define UsbInterruptFlags() (U1IR)
  318. #define UsbErrorInterruptFlags() (U1EIR)
  319. #define ClearUsbInterruptFlag(x) U1IR = x
  320. #define ClearAllUsbInterruptFlags() U1IR = 0xFF
  321. #define ClearUsbErrorInterruptFlag(x) U1EIR = x
  322. #define ClearAllUsbErrorInterruptFlags() U1EIR = 0xFF
  323. #define DisableGlobalUsbInterrupt() IEC5bits.USB1IE=0 //PIE2bits.USBIE = 0 /*FIX*/
  324. #define DisableUsbInterrupt(x) U1IE &= ~(x)
  325. #define DisableAllUsbInterrupts() U1IE = 0
  326. #define DisableUsbErrorInterrupt(x) U1EIE &= ~(x)
  327. #define DisableAllUsbErrorInterrupts() U1EIE = 0
  328. #define EnableUsbGlobalInterrupt() IEC5bits.USB1IE=1 //PIE2bits.USBIE = 1 /*FIX*/
  329. #define TestGlobalUsbInterruptEnable() (IEC5bits.USB1IE)
  330. #define EnableUsbPerifInterrupts(x) U1IE |= (x)
  331. #define EnableAllUsbInterrupts() U1IE = 0x00FF
  332. #define EnableUsbErrorInterrupt(x) U1EIE |= (x)
  333. #define EnableAllUsbErrorInterrupts() U1EIE = 0x00FF
  334. #define EnableUsbHighPriInterrupt()
  335. #define ClearGlobalUsbInterruptFlag() IFS5bits.USB1IF = 0
  336. /* UCON */
  337. #define ResetPPbuffers() do {U1CONbits.PPBRST = 1; U1CONbits.PPBRST=0;} while(0)
  338. #define SingleEndedZeroIsSet() (U1CONbits.SE0)
  339. #define EnablePacketTransfer() U1CONbits.PKTDIS = 0
  340. #define EnableUsb() while(!U1CONbits.USBEN){U1CONbits.USBEN = 1;} // U1CONbits.USBEN = 1
  341. #define SignalResume() do {U1CONbits.RESUME = 1; delay_ms(10); U1CONbits.RESUME = 0;} while(0)
  342. #define SuspendUsb() U1PWRCbits.USUSPND = 1
  343. #define WakeupUsb() do {U1PWRCbits.USUSPND = 0; while(USB_RESUME_FLAG){USB_RESUME_FLAG = 0;}} while(0)
  344. /* UADDR */
  345. #define SetUsbAddress(x) (U1ADDR = (x))
  346. #define GetUsbAddress() (U1ADDR)
  347. /* USTAT */
  348. // JTR moved to usb_stack.h
  349. //typedef unsigned char usb_status_t;
  350. #define GetUsbTransaction() (U1STAT)
  351. #define USB_STAT2EP(x) ((x>>4)&0x0F) //((x>>3)&0x0F) JTR PIC24 fixups
  352. #define USB_STAT2DIR(x) ((x>>3)&0x01) //((x>>2)&0x01) ""
  353. #define USB_STAT2ADDR(x) ((x>>3)&0x1F) //((x>>2)&0x1F) ""
  354. #define USB_STAT2PPI(x) ((x>>2 &0x01) //((x>>1)&0x01) ""
  355. #define DIRBIT 0x8 // JTR add PIC24 fixup as the mask is 0x4 for the PIC18
  356. /* Hardware implementations */
  357. #if defined USB_INTERNAL_PULLUPS
  358. #elif defined USB_EXTERNAL_PULLUPS
  359. #define USB_U1OTGCON_UPUEN_VALUE (0)
  360. #else
  361. #error "Neither internal nor external pullups defined"
  362. #endif
  363. #if defined USB_INTERNAL_TRANSCIEVER
  364. #define USB_U1CNFG2_UTRDIS_VALUE (0)
  365. #elif defined USB_EXTERNAL_TRANSCIEVER
  366. #define USB_U1CNFG2_UTRDIS_VALUE (1)
  367. #else
  368. #error "Neither internal nor external transciever defined"
  369. #endif
  370. #if defined USB_FULL_SPEED_DEVICE
  371. #if defined USB_INTERNAL_PULLUPS
  372. #define USB_U1OTGCON_DPPULUP_VALUE (1<<7)
  373. #define USB_U1OTGCON_DMPULUP_VALUE (0)
  374. #else
  375. #define USB_U1OTGCON_DPPULUP_VALUE (0)
  376. #define USB_U1OTGCON_DMPULUP_VALUE (0)
  377. #endif
  378. #elif defined USB_LOW_SPEED_DEVICE
  379. #if defined USB_INTERNAL_PULLUPS
  380. #define USB_U1OTGCON_DPPULUP_VALUE (0)
  381. #define USB_U1OTGCON_DMPULUP_VALUE (1<<6)
  382. #else
  383. #define USB_U1OTGCON_DPPULUP_VALUE (0)
  384. #define USB_U1OTGCON_DMPULUP_VALUE (0)
  385. #endif
  386. #else
  387. #error "Neither internal nor external pullups defined"
  388. #endif
  389. #if defined USB_BUS_POWERED
  390. #ifndef usb_low_power_request
  391. /* Default low power mode is DUD */
  392. #define usb_low_power_request() Nop()
  393. #endif
  394. #ifndef usb_low_power_resume
  395. #define usb_low_power_resume() Nop()
  396. #endif
  397. #elif defined USB_SELF_POWERED
  398. #define usb_low_power_request() Nop()
  399. #define usb_low_power_resume() Nop()
  400. #else
  401. #error "No source of device power defined"
  402. #endif
  403. #ifndef USB_INTERNAL_VREG
  404. #warning "Use of internal voltage regulator not implemented. User must supply 3.3V on Vusb pin."
  405. #endif
  406. /* PingPong buffer descriptor table index calculations */
  407. #if USB_PP_BUF_MODE == NO_PINGPONG
  408. #define USB_USTAT2BD(X) ( (X)/8 ) //JTR PIC24 fixups
  409. #define USB_CALC_BD(ep, dir, sync) ( 2*(ep)+(dir) )
  410. // JTR TODO these values may need to be changed for the PIC24
  411. //#elif USB_PP_BUF_MODE == 1
  412. //#define USB_USTAT2BD(X) ( ((X)>2)? (X)/4+1 : (X)/2 )
  413. //#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0 && (dir)==0)? (sync) : 2*(ep)+(dir) )
  414. //#elif USB_PP_BUF_MODE == 2
  415. //#define USB_USTAT2BD(X) ( (X)/2 )
  416. //#define USB_CALC_BD(ep, dir, sync) ( 4*(ep)+2*(dir)+(sync) )
  417. //#elif USB_PP_BUF_MODE == 3
  418. //#define USB_USTAT2BD(X) ( ((X)>4)? (X)/2-2 : (X)/4 )
  419. //#define USB_CALC_BD(ep, dir, sync) ( ((ep)==0)? (dir) : 4*(ep)+2*(dir)+(sync)-2 )
  420. #else
  421. #error "USB_PP_BUF_MODE outside scope."
  422. #endif
  423. // JTR PIC24 fixups some may not required but are "more correct." The added /256 is required!
  424. #define ConfigureUsbHardware() do { \
  425. U1CNFG1 = USB_PP_BUF_MODE; \
  426. U1CNFG2 = USB_U1CNFG2_UTRDIS_VALUE; \
  427. U1BDTP1 = (unsigned int) usb_bdt/256; \
  428. U1OTGCON = USB_U1OTGCON_DPPULUP_VALUE | \
  429. USB_U1OTGCON_DMPULUP_VALUE; \
  430. U1PWRCbits.USBPWR = 1; \
  431. } while(0)
  432. #define ROM __attribute__((space(auto_psv)))
  433. #define ROMPTR
  434. #define ARCH_memcpy memcpy
  435. typedef struct BDENTRY {
  436. unsigned char BDCNT; // JTR PIC24 fixup Note that the endianness is swapped from the PIC18
  437. // This is probably what had people hitting their heads!
  438. unsigned char BDSTAT;
  439. unsigned char *BDADDR;
  440. } BDentry;
  441. #endif
  442. #ifndef MyProcessor
  443. #error "No PIC family defined"
  444. #endif
  445. #endif