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_STM32F4.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #if defined(TARGET_STM32F4)
  19. #include "USBHAL.h"
  20. #include "USBRegs_STM32.h"
  21. #include "pinmap.h"
  22. USBHAL * USBHAL::instance;
  23. static volatile int epComplete = 0;
  24. static uint32_t bufferEnd = 0;
  25. static const uint32_t rxFifoSize = 512;
  26. static uint32_t rxFifoCount = 0;
  27. static uint32_t setupBuffer[MAX_PACKET_SIZE_EP0 >> 2];
  28. uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {
  29. return 0;
  30. }
  31. USBHAL::USBHAL(void) {
  32. NVIC_DisableIRQ(OTG_FS_IRQn);
  33. epCallback[0] = &USBHAL::EP1_OUT_callback;
  34. epCallback[1] = &USBHAL::EP1_IN_callback;
  35. epCallback[2] = &USBHAL::EP2_OUT_callback;
  36. epCallback[3] = &USBHAL::EP2_IN_callback;
  37. epCallback[4] = &USBHAL::EP3_OUT_callback;
  38. epCallback[5] = &USBHAL::EP3_IN_callback;
  39. // Enable power and clocking
  40. RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  41. #if defined(TARGET_STM32F407VG) || defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE)
  42. pin_function(PA_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
  43. pin_function(PA_9, STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLDOWN, GPIO_AF10_OTG_FS));
  44. pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS));
  45. pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
  46. pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
  47. #else
  48. pin_function(PA_8, STM_PIN_DATA(2, 10));
  49. pin_function(PA_9, STM_PIN_DATA(0, 0));
  50. pin_function(PA_10, STM_PIN_DATA(2, 10));
  51. pin_function(PA_11, STM_PIN_DATA(2, 10));
  52. pin_function(PA_12, STM_PIN_DATA(2, 10));
  53. // Set ID pin to open drain with pull-up resistor
  54. pin_mode(PA_10, OpenDrain);
  55. GPIOA->PUPDR &= ~(0x3 << 20);
  56. GPIOA->PUPDR |= 1 << 20;
  57. // Set VBUS pin to open drain
  58. pin_mode(PA_9, OpenDrain);
  59. #endif
  60. RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
  61. // Enable interrupts
  62. OTG_FS->GREGS.GAHBCFG |= (1 << 0);
  63. // Turnaround time to maximum value - too small causes packet loss
  64. OTG_FS->GREGS.GUSBCFG |= (0xF << 10);
  65. // Unmask global interrupts
  66. OTG_FS->GREGS.GINTMSK |= (1 << 3) | // SOF
  67. (1 << 4) | // RX FIFO not empty
  68. (1 << 12); // USB reset
  69. OTG_FS->DREGS.DCFG |= (0x3 << 0) | // Full speed
  70. (1 << 2); // Non-zero-length status OUT handshake
  71. OTG_FS->GREGS.GCCFG |= (1 << 19) | // Enable VBUS sensing
  72. (1 << 16); // Power Up
  73. instance = this;
  74. NVIC_SetVector(OTG_FS_IRQn, (uint32_t)&_usbisr);
  75. NVIC_SetPriority(OTG_FS_IRQn, 1);
  76. }
  77. USBHAL::~USBHAL(void) {
  78. }
  79. void USBHAL::connect(void) {
  80. NVIC_EnableIRQ(OTG_FS_IRQn);
  81. }
  82. void USBHAL::disconnect(void) {
  83. NVIC_DisableIRQ(OTG_FS_IRQn);
  84. }
  85. void USBHAL::configureDevice(void) {
  86. // Not needed
  87. }
  88. void USBHAL::unconfigureDevice(void) {
  89. // Not needed
  90. }
  91. void USBHAL::setAddress(uint8_t address) {
  92. OTG_FS->DREGS.DCFG |= (address << 4);
  93. EP0write(0, 0);
  94. }
  95. bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket,
  96. uint32_t flags) {
  97. uint32_t epIndex = endpoint >> 1;
  98. uint32_t type;
  99. switch (endpoint) {
  100. case EP0IN:
  101. case EP0OUT:
  102. type = 0;
  103. break;
  104. case EPISO_IN:
  105. case EPISO_OUT:
  106. type = 1;
  107. case EPBULK_IN:
  108. case EPBULK_OUT:
  109. type = 2;
  110. break;
  111. case EPINT_IN:
  112. case EPINT_OUT:
  113. type = 3;
  114. break;
  115. }
  116. // Generic in or out EP controls
  117. uint32_t control = (maxPacket << 0) | // Packet size
  118. (1 << 15) | // Active endpoint
  119. (type << 18); // Endpoint type
  120. if (endpoint & 0x1) { // In Endpoint
  121. // Set up the Tx FIFO
  122. if (endpoint == EP0IN) {
  123. OTG_FS->GREGS.DIEPTXF0_HNPTXFSIZ = ((maxPacket >> 2) << 16) |
  124. (bufferEnd << 0);
  125. }
  126. else {
  127. OTG_FS->GREGS.DIEPTXF[epIndex - 1] = ((maxPacket >> 2) << 16) |
  128. (bufferEnd << 0);
  129. }
  130. bufferEnd += maxPacket >> 2;
  131. // Set the In EP specific control settings
  132. if (endpoint != EP0IN) {
  133. control |= (1 << 28); // SD0PID
  134. }
  135. control |= (epIndex << 22) | // TxFIFO index
  136. (1 << 27); // SNAK
  137. OTG_FS->INEP_REGS[epIndex].DIEPCTL = control;
  138. // Unmask the interrupt
  139. OTG_FS->DREGS.DAINTMSK |= (1 << epIndex);
  140. }
  141. else { // Out endpoint
  142. // Set the out EP specific control settings
  143. control |= (1 << 26); // CNAK
  144. OTG_FS->OUTEP_REGS[epIndex].DOEPCTL = control;
  145. // Unmask the interrupt
  146. OTG_FS->DREGS.DAINTMSK |= (1 << (epIndex + 16));
  147. }
  148. return true;
  149. }
  150. // read setup packet
  151. void USBHAL::EP0setup(uint8_t *buffer) {
  152. memcpy(buffer, setupBuffer, MAX_PACKET_SIZE_EP0);
  153. }
  154. void USBHAL::EP0readStage(void) {
  155. }
  156. void USBHAL::EP0read(void) {
  157. }
  158. uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {
  159. uint32_t* buffer32 = (uint32_t *) buffer;
  160. uint32_t length = rxFifoCount;
  161. for (uint32_t i = 0; i < length; i += 4) {
  162. buffer32[i >> 2] = OTG_FS->FIFO[0][0];
  163. }
  164. rxFifoCount = 0;
  165. return length;
  166. }
  167. void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
  168. endpointWrite(0, buffer, size);
  169. }
  170. void USBHAL::EP0getWriteResult(void) {
  171. }
  172. void USBHAL::EP0stall(void) {
  173. // If we stall the out endpoint here then we have problems transferring
  174. // and setup requests after the (stalled) get device qualifier requests.
  175. // TODO: Find out if this is correct behavior, or whether we are doing
  176. // something else wrong
  177. stallEndpoint(EP0IN);
  178. // stallEndpoint(EP0OUT);
  179. }
  180. EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
  181. uint32_t epIndex = endpoint >> 1;
  182. uint32_t size = (1 << 19) | // 1 packet
  183. (maximumSize << 0); // Packet size
  184. // if (endpoint == EP0OUT) {
  185. size |= (1 << 29); // 1 setup packet
  186. // }
  187. OTG_FS->OUTEP_REGS[epIndex].DOEPTSIZ = size;
  188. OTG_FS->OUTEP_REGS[epIndex].DOEPCTL |= (1 << 31) | // Enable endpoint
  189. (1 << 26); // Clear NAK
  190. epComplete &= ~(1 << endpoint);
  191. return EP_PENDING;
  192. }
  193. EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {
  194. if (!(epComplete & (1 << endpoint))) {
  195. return EP_PENDING;
  196. }
  197. uint32_t* buffer32 = (uint32_t *) buffer;
  198. uint32_t length = rxFifoCount;
  199. for (uint32_t i = 0; i < length; i += 4) {
  200. buffer32[i >> 2] = OTG_FS->FIFO[endpoint >> 1][0];
  201. }
  202. rxFifoCount = 0;
  203. *bytesRead = length;
  204. return EP_COMPLETED;
  205. }
  206. EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {
  207. uint32_t epIndex = endpoint >> 1;
  208. OTG_FS->INEP_REGS[epIndex].DIEPTSIZ = (1 << 19) | // 1 packet
  209. (size << 0); // Size of packet
  210. OTG_FS->INEP_REGS[epIndex].DIEPCTL |= (1 << 31) | // Enable endpoint
  211. (1 << 26); // CNAK
  212. OTG_FS->DREGS.DIEPEMPMSK = (1 << epIndex);
  213. while ((OTG_FS->INEP_REGS[epIndex].DTXFSTS & 0XFFFF) < ((size + 3) >> 2));
  214. for (uint32_t i=0; i<(size + 3) >> 2; i++, data+=4) {
  215. OTG_FS->FIFO[epIndex][0] = *(uint32_t *)data;
  216. }
  217. epComplete &= ~(1 << endpoint);
  218. return EP_PENDING;
  219. }
  220. EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
  221. if (epComplete & (1 << endpoint)) {
  222. epComplete &= ~(1 << endpoint);
  223. return EP_COMPLETED;
  224. }
  225. return EP_PENDING;
  226. }
  227. void USBHAL::stallEndpoint(uint8_t endpoint) {
  228. if (endpoint & 0x1) { // In EP
  229. OTG_FS->INEP_REGS[endpoint >> 1].DIEPCTL |= (1 << 30) | // Disable
  230. (1 << 21); // Stall
  231. }
  232. else { // Out EP
  233. OTG_FS->DREGS.DCTL |= (1 << 9); // Set global out NAK
  234. OTG_FS->OUTEP_REGS[endpoint >> 1].DOEPCTL |= (1 << 30) | // Disable
  235. (1 << 21); // Stall
  236. }
  237. }
  238. void USBHAL::unstallEndpoint(uint8_t endpoint) {
  239. }
  240. bool USBHAL::getEndpointStallState(uint8_t endpoint) {
  241. return false;
  242. }
  243. void USBHAL::remoteWakeup(void) {
  244. }
  245. void USBHAL::_usbisr(void) {
  246. instance->usbisr();
  247. }
  248. void USBHAL::usbisr(void) {
  249. if (OTG_FS->GREGS.GINTSTS & (1 << 12)) { // USB Reset
  250. // Set SNAK bits
  251. OTG_FS->OUTEP_REGS[0].DOEPCTL |= (1 << 27);
  252. OTG_FS->OUTEP_REGS[1].DOEPCTL |= (1 << 27);
  253. OTG_FS->OUTEP_REGS[2].DOEPCTL |= (1 << 27);
  254. OTG_FS->OUTEP_REGS[3].DOEPCTL |= (1 << 27);
  255. OTG_FS->DREGS.DIEPMSK = (1 << 0);
  256. bufferEnd = 0;
  257. // Set the receive FIFO size
  258. OTG_FS->GREGS.GRXFSIZ = rxFifoSize >> 2;
  259. bufferEnd += rxFifoSize >> 2;
  260. // Create the endpoints, and wait for setup packets on out EP0
  261. realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0);
  262. realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0);
  263. endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
  264. OTG_FS->GREGS.GINTSTS = (1 << 12);
  265. }
  266. if (OTG_FS->GREGS.GINTSTS & (1 << 4)) { // RX FIFO not empty
  267. uint32_t status = OTG_FS->GREGS.GRXSTSP;
  268. uint32_t endpoint = (status & 0xF) << 1;
  269. uint32_t length = (status >> 4) & 0x7FF;
  270. uint32_t type = (status >> 17) & 0xF;
  271. rxFifoCount = length;
  272. if (type == 0x6) {
  273. // Setup packet
  274. for (uint32_t i=0; i<length; i+=4) {
  275. setupBuffer[i >> 2] = OTG_FS->FIFO[0][i >> 2];
  276. }
  277. rxFifoCount = 0;
  278. }
  279. if (type == 0x4) {
  280. // Setup complete
  281. EP0setupCallback();
  282. endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
  283. }
  284. if (type == 0x2) {
  285. // Out packet
  286. if (endpoint == EP0OUT) {
  287. EP0out();
  288. }
  289. else {
  290. epComplete |= (1 << endpoint);
  291. if ((instance->*(epCallback[endpoint - 2]))()) {
  292. epComplete &= (1 << endpoint);
  293. }
  294. }
  295. }
  296. for (uint32_t i=0; i<rxFifoCount; i+=4) {
  297. (void) OTG_FS->FIFO[0][0];
  298. }
  299. OTG_FS->GREGS.GINTSTS = (1 << 4);
  300. }
  301. if (OTG_FS->GREGS.GINTSTS & (1 << 18)) { // In endpoint interrupt
  302. // Loop through the in endpoints
  303. for (uint32_t i=0; i<4; i++) {
  304. if (OTG_FS->DREGS.DAINT & (1 << i)) { // Interrupt is on endpoint
  305. if (OTG_FS->INEP_REGS[i].DIEPINT & (1 << 7)) {// Tx FIFO empty
  306. // If the Tx FIFO is empty on EP0 we need to send a further
  307. // packet, so call EP0in()
  308. if (i == 0) {
  309. EP0in();
  310. }
  311. // Clear the interrupt
  312. OTG_FS->INEP_REGS[i].DIEPINT = (1 << 7);
  313. // Stop firing Tx empty interrupts
  314. // Will get turned on again if another write is called
  315. OTG_FS->DREGS.DIEPEMPMSK &= ~(1 << i);
  316. }
  317. // If the transfer is complete
  318. if (OTG_FS->INEP_REGS[i].DIEPINT & (1 << 0)) { // Tx Complete
  319. epComplete |= (1 << (1 + (i << 1)));
  320. OTG_FS->INEP_REGS[i].DIEPINT = (1 << 0);
  321. }
  322. }
  323. }
  324. OTG_FS->GREGS.GINTSTS = (1 << 18);
  325. }
  326. if (OTG_FS->GREGS.GINTSTS & (1 << 3)) { // Start of frame
  327. SOF((OTG_FS->GREGS.GRXSTSR >> 17) & 0xF);
  328. OTG_FS->GREGS.GINTSTS = (1 << 3);
  329. }
  330. }
  331. #endif