Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

USBHAL_Maxim.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*******************************************************************************
  2. * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Except as contained in this notice, the name of Maxim Integrated
  23. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  24. * Products, Inc. Branding Policy.
  25. *
  26. * The mere transfer of this software does not imply any licenses
  27. * of trade secrets, proprietary technology, copyrights, patents,
  28. * trademarks, maskwork rights, or any other form of intellectual
  29. * property whatsoever. Maxim Integrated Products, Inc. retains all
  30. * ownership rights.
  31. *******************************************************************************
  32. */
  33. #if defined(TARGET_Maxim)
  34. #include "USBHAL.h"
  35. #include "usb_regs.h"
  36. #include "clkman_regs.h"
  37. #define CONNECT_INTS (MXC_F_USB_DEV_INTEN_BRST | MXC_F_USB_DEV_INTEN_SETUP | MXC_F_USB_DEV_INTEN_EP_IN | MXC_F_USB_DEV_INTEN_EP_OUT | MXC_F_USB_DEV_INTEN_DMA_ERR)
  38. USBHAL *USBHAL::instance;
  39. typedef struct {
  40. volatile uint32_t buf0_desc;
  41. volatile uint32_t buf0_address;
  42. volatile uint32_t buf1_desc;
  43. volatile uint32_t buf1_address;
  44. } ep_buffer_t;
  45. typedef struct {
  46. ep_buffer_t out_buffer;
  47. ep_buffer_t in_buffer;
  48. } ep0_buffer_t;
  49. typedef struct {
  50. ep0_buffer_t ep0;
  51. ep_buffer_t ep[MXC_USB_NUM_EP - 1];
  52. } ep_buffer_descriptor_t;
  53. // Static storage for endpoint buffer descriptor table. Must be 512 byte alligned for DMA.
  54. #ifdef __IAR_SYSTEMS_ICC__
  55. #pragma data_alignment = 512
  56. #else
  57. __attribute__ ((aligned (512)))
  58. #endif
  59. ep_buffer_descriptor_t ep_buffer_descriptor;
  60. // static storage for temporary data buffers. Must be 32 byte alligned.
  61. #ifdef __IAR_SYSTEMS_ICC__
  62. #pragma data_alignment = 4
  63. #else
  64. __attribute__ ((aligned (4)))
  65. #endif
  66. static uint8_t aligned_buffer[NUMBER_OF_LOGICAL_ENDPOINTS][MXC_USB_MAX_PACKET];
  67. // contorl packet state
  68. static enum {
  69. CTRL_NONE = 0,
  70. CTRL_SETUP,
  71. CTRL_OUT,
  72. CTRL_IN,
  73. } control_state;
  74. USBHAL::USBHAL(void)
  75. {
  76. NVIC_DisableIRQ(USB_IRQn);
  77. // The PLL must be enabled for USB
  78. MBED_ASSERT(MXC_CLKMAN->clk_config & MXC_F_CLKMAN_CLK_CONFIG_PLL_ENABLE);
  79. // Enable the USB clock
  80. MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_USB_GATE_N;
  81. // reset the device
  82. MXC_USB->cn = 0;
  83. MXC_USB->cn = 1;
  84. MXC_USB->dev_inten = 0;
  85. MXC_USB->dev_cn = 0;
  86. MXC_USB->dev_cn = MXC_F_USB_DEV_CN_URST;
  87. MXC_USB->dev_cn = 0;
  88. // fill in callback arrays
  89. epCallback[EP0OUT] = NULL;
  90. epCallback[EP0IN] = NULL;
  91. epCallback[EP1OUT] = &USBHAL::EP1_OUT_callback;
  92. epCallback[EP1IN ] = &USBHAL::EP1_IN_callback;
  93. epCallback[EP2OUT] = &USBHAL::EP2_OUT_callback;
  94. epCallback[EP2IN ] = &USBHAL::EP2_IN_callback;
  95. epCallback[EP3OUT] = &USBHAL::EP3_OUT_callback;
  96. epCallback[EP3IN ] = &USBHAL::EP3_IN_callback;
  97. epCallback[EP4OUT] = &USBHAL::EP4_OUT_callback;
  98. epCallback[EP4IN ] = &USBHAL::EP4_IN_callback;
  99. epCallback[EP5OUT] = &USBHAL::EP5_OUT_callback;
  100. epCallback[EP5IN ] = &USBHAL::EP5_IN_callback;
  101. epCallback[EP6OUT] = &USBHAL::EP6_OUT_callback;
  102. epCallback[EP6IN ] = &USBHAL::EP6_IN_callback;
  103. epCallback[EP7OUT] = &USBHAL::EP7_OUT_callback;
  104. epCallback[EP7IN ] = &USBHAL::EP7_IN_callback;
  105. // clear driver state
  106. control_state = CTRL_NONE;
  107. // set the descriptor location
  108. MXC_USB->ep_base = (uint32_t)&ep_buffer_descriptor;
  109. // attach IRQ handler and enable interrupts
  110. instance = this;
  111. NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr);
  112. NVIC_EnableIRQ(USB_IRQn);
  113. }
  114. USBHAL::~USBHAL(void)
  115. {
  116. MXC_USB->dev_cn = MXC_F_USB_DEV_CN_URST;
  117. MXC_USB->dev_cn = 0;
  118. MXC_USB->cn = 0;
  119. }
  120. void USBHAL::connect(void)
  121. {
  122. // enable interrupts
  123. MXC_USB->dev_inten |= CONNECT_INTS;
  124. // allow interrupts on ep0
  125. MXC_USB->ep[0] |= MXC_F_USB_EP_INT_EN;
  126. // pullup enable
  127. MXC_USB->dev_cn |= (MXC_F_USB_DEV_CN_CONNECT | MXC_F_USB_DEV_CN_FIFO_MODE);
  128. }
  129. void USBHAL::disconnect(void)
  130. {
  131. // disable interrupts
  132. MXC_USB->dev_inten &= ~CONNECT_INTS;
  133. // disable pullup
  134. MXC_USB->dev_cn &= ~MXC_F_USB_DEV_CN_CONNECT;
  135. }
  136. void USBHAL::configureDevice(void)
  137. {
  138. // do nothing
  139. }
  140. void USBHAL::unconfigureDevice(void)
  141. {
  142. // reset endpoints
  143. for (int i = 0; i < MXC_USB_NUM_EP; i++) {
  144. // Disable endpoint and clear the data toggle
  145. MXC_USB->ep[i] &= ~MXC_F_USB_EP_DIR;
  146. MXC_USB->ep[i] |= MXC_F_USB_EP_DT;
  147. }
  148. }
  149. void USBHAL::setAddress(uint8_t address)
  150. {
  151. // do nothing
  152. }
  153. void USBHAL::remoteWakeup(void)
  154. {
  155. // do nothing
  156. }
  157. static ep_buffer_t *get_desc(uint8_t endpoint)
  158. {
  159. uint8_t epnum = EP_NUM(endpoint);
  160. ep_buffer_t *desc;
  161. if (epnum == 0) {
  162. if (IN_EP(endpoint)) {
  163. desc = &ep_buffer_descriptor.ep0.in_buffer;
  164. } else {
  165. desc = &ep_buffer_descriptor.ep0.out_buffer;
  166. }
  167. } else {
  168. desc = &ep_buffer_descriptor.ep[epnum - 1];
  169. }
  170. return desc;
  171. }
  172. void USBHAL::EP0setup(uint8_t *buffer)
  173. {
  174. memcpy(buffer, (void*)&MXC_USB->setup0, 8); // setup packet is fixed at 8 bytes
  175. }
  176. void USBHAL::EP0read(void)
  177. {
  178. if (control_state == CTRL_IN) {
  179. // This is the status stage. ACK.
  180. MXC_USB->ep[0] |= MXC_F_USB_EP_ST_ACK;
  181. control_state = CTRL_NONE;
  182. return;
  183. }
  184. control_state = CTRL_OUT;
  185. endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
  186. }
  187. void USBHAL::EP0readStage(void)
  188. {
  189. // do nothing
  190. }
  191. uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
  192. {
  193. uint32_t size;
  194. if (MXC_USB->out_owner & 1) {
  195. return 0;
  196. }
  197. // get the packet length and contents
  198. ep_buffer_t *desc = get_desc(EP0OUT);
  199. size = desc->buf0_desc;
  200. memcpy(buffer, aligned_buffer[0], size);
  201. return size;
  202. }
  203. void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
  204. {
  205. if ((size == 0) && (control_state != CTRL_IN)) {
  206. // This is a status stage ACK. Handle in hardware.
  207. MXC_USB->ep[0] |= MXC_F_USB_EP_ST_ACK;
  208. control_state = CTRL_NONE;
  209. return;
  210. }
  211. control_state = CTRL_IN;
  212. endpointWrite(EP0IN, buffer, size);
  213. }
  214. void USBHAL::EP0stall(void)
  215. {
  216. stallEndpoint(0);
  217. }
  218. EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize)
  219. {
  220. uint8_t epnum = EP_NUM(endpoint);
  221. if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || IN_EP(endpoint)) {
  222. return EP_INVALID;
  223. }
  224. if (maximumSize > MXC_USB_MAX_PACKET) {
  225. return EP_INVALID;
  226. }
  227. uint32_t mask = (1 << epnum);
  228. if (MXC_USB->out_owner & mask) {
  229. return EP_INVALID;
  230. }
  231. ep_buffer_t *desc = get_desc(endpoint);
  232. desc->buf0_desc = maximumSize;
  233. desc->buf0_address = (uint32_t)aligned_buffer[epnum];
  234. MXC_USB->out_owner = mask;
  235. return EP_PENDING;
  236. }
  237. EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead)
  238. {
  239. if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || IN_EP(endpoint)) {
  240. return EP_INVALID;
  241. }
  242. uint32_t mask = (1 << EP_NUM(endpoint));
  243. if (MXC_USB->out_owner & mask) {
  244. return EP_PENDING;
  245. }
  246. // get the packet length and contents
  247. ep_buffer_t *desc = get_desc(endpoint);
  248. *bytesRead = desc->buf0_desc;
  249. memcpy(data, aligned_buffer[EP_NUM(endpoint)], *bytesRead);
  250. return EP_COMPLETED;
  251. }
  252. EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
  253. {
  254. uint8_t epnum = EP_NUM(endpoint);
  255. if ((endpoint >= NUMBER_OF_PHYSICAL_ENDPOINTS) || OUT_EP(endpoint)) {
  256. return EP_INVALID;
  257. }
  258. if (size > MXC_USB_MAX_PACKET) {
  259. return EP_INVALID;
  260. }
  261. uint32_t mask = (1 << epnum);
  262. if (MXC_USB->in_owner & mask) {
  263. return EP_INVALID;
  264. }
  265. memcpy(aligned_buffer[epnum], data, size);
  266. ep_buffer_t *desc = get_desc(endpoint);
  267. desc->buf0_desc = size;
  268. desc->buf0_address = (uint32_t)aligned_buffer[epnum];
  269. // start the DMA
  270. MXC_USB->in_owner = mask;
  271. return EP_PENDING;
  272. }
  273. EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
  274. {
  275. uint32_t mask = (1 << EP_NUM(endpoint));
  276. if (MXC_USB->in_owner & mask) {
  277. return EP_PENDING;
  278. }
  279. return EP_COMPLETED;
  280. }
  281. void USBHAL::stallEndpoint(uint8_t endpoint)
  282. {
  283. uint8_t epnum = EP_NUM(endpoint);
  284. if (epnum == 0) {
  285. MXC_USB->ep[epnum] |= MXC_F_USB_EP_ST_STALL;
  286. }
  287. MXC_USB->ep[epnum] |= MXC_F_USB_EP_STALL;
  288. }
  289. void USBHAL::unstallEndpoint(uint8_t endpoint)
  290. {
  291. MXC_USB->ep[EP_NUM(endpoint)] &= ~MXC_F_USB_EP_STALL;
  292. }
  293. bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options)
  294. {
  295. uint8_t epnum = EP_NUM(endpoint);
  296. uint32_t ep_ctrl;
  297. if (epnum >= NUMBER_OF_PHYSICAL_ENDPOINTS) {
  298. return false;
  299. }
  300. if (IN_EP(endpoint)) {
  301. ep_ctrl = (MXC_V_USB_EP_DIR_IN << MXC_F_USB_EP_DIR_POS);
  302. } else {
  303. ep_ctrl = (MXC_S_USB_EP_DIR_OUT << MXC_F_USB_EP_DIR_POS);
  304. }
  305. ep_ctrl |= (MXC_F_USB_EP_DT | MXC_F_USB_EP_INT_EN);
  306. MXC_USB->ep[epnum] = ep_ctrl;
  307. return true;
  308. }
  309. bool USBHAL::getEndpointStallState(unsigned char endpoint)
  310. {
  311. return !!(MXC_USB->ep[endpoint] & MXC_F_USB_EP_STALL);
  312. }
  313. void USBHAL::_usbisr(void)
  314. {
  315. instance->usbisr();
  316. }
  317. void USBHAL::usbisr(void)
  318. {
  319. // get and clear irqs
  320. uint32_t irq_flags = MXC_USB->dev_intfl;
  321. MXC_USB->dev_intfl = irq_flags;
  322. // process only enabled interrupts
  323. irq_flags &= MXC_USB->dev_inten;
  324. // suspend
  325. if (irq_flags & MXC_F_USB_DEV_INTFL_SUSP) {
  326. suspendStateChanged(1);
  327. }
  328. // bus reset
  329. if (irq_flags & MXC_F_USB_DEV_INTFL_BRST) {
  330. // reset endpoints
  331. for (int i = 0; i < MXC_USB_NUM_EP; i++) {
  332. // Disable endpoint and clear the data toggle
  333. MXC_USB->ep[i] &= ~MXC_F_USB_EP_DIR;
  334. MXC_USB->ep[i] |= MXC_F_USB_EP_DT;
  335. }
  336. // clear driver state
  337. control_state = CTRL_NONE;
  338. busReset();
  339. // no need to process events after reset
  340. return;
  341. }
  342. // Setup packet
  343. if (irq_flags & MXC_F_USB_DEV_INTFL_SETUP) {
  344. control_state = CTRL_SETUP;
  345. EP0setupCallback();
  346. }
  347. // IN packets
  348. if (irq_flags & MXC_F_USB_DEV_INTFL_EP_IN) {
  349. // get and clear IN irqs
  350. uint32_t in_irqs = MXC_USB->in_int;
  351. MXC_USB->in_int = in_irqs;
  352. if (in_irqs & 1) {
  353. EP0in();
  354. }
  355. for (uint8_t epnum = 1; epnum < NUMBER_OF_LOGICAL_ENDPOINTS; epnum++) {
  356. uint32_t irq_mask = (1 << epnum);
  357. if (in_irqs & irq_mask) {
  358. uint8_t endpoint = (epnum << 1) | DIR_IN;
  359. (instance->*(epCallback[endpoint]))();
  360. }
  361. }
  362. }
  363. // OUT packets
  364. if (irq_flags & MXC_F_USB_DEV_INTFL_EP_OUT) {
  365. // get and clear OUT irqs
  366. uint32_t out_irqs = MXC_USB->out_int;
  367. MXC_USB->out_int = out_irqs;
  368. if (out_irqs & 1) {
  369. EP0out();
  370. }
  371. for (uint8_t epnum = 1; epnum < NUMBER_OF_LOGICAL_ENDPOINTS; epnum++) {
  372. uint32_t irq_mask = (1 << epnum);
  373. if (out_irqs & irq_mask) {
  374. uint8_t endpoint = (epnum << 1) | DIR_OUT;
  375. (instance->*(epCallback[endpoint]))();
  376. }
  377. }
  378. }
  379. }
  380. #endif