usbd_stm32l052_devfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "stm32.h"
  18. #include "usb.h"
  19. #if defined(USBD_STM32L052)
  20. #ifndef USB_PMASIZE
  21. #pragma message "PMA memory size is not defined. Use 1k by default"
  22. #define USB_PMASIZE 0x400
  23. #endif
  24. #define USB_EP_SWBUF_TX USB_EP_DTOG_RX
  25. #define USB_EP_SWBUF_RX USB_EP_DTOG_TX
  26. #define EP_TOGGLE_SET(epr, bits, mask) *(epr) = (*(epr) ^ (bits)) & (USB_EPREG_MASK | (mask))
  27. #define EP_TX_STALL(epr) EP_TOGGLE_SET((epr), USB_EP_TX_STALL, USB_EPTX_STAT)
  28. #define EP_RX_STALL(epr) EP_TOGGLE_SET((epr), USB_EP_RX_STALL, USB_EPRX_STAT)
  29. #define EP_TX_UNSTALL(epr) EP_TOGGLE_SET((epr), USB_EP_TX_NAK, USB_EPTX_STAT | USB_EP_DTOG_TX)
  30. #define EP_RX_UNSTALL(epr) EP_TOGGLE_SET((epr), USB_EP_RX_VALID, USB_EPRX_STAT | USB_EP_DTOG_RX)
  31. #define EP_DTX_UNSTALL(epr) EP_TOGGLE_SET((epr), USB_EP_TX_VALID, USB_EPTX_STAT | USB_EP_DTOG_TX | USB_EP_SWBUF_TX)
  32. #define EP_DRX_UNSTALL(epr) EP_TOGGLE_SET((epr), USB_EP_RX_VALID | USB_EP_SWBUF_RX, USB_EPRX_STAT | USB_EP_DTOG_RX | USB_EP_SWBUF_RX)
  33. #define EP_TX_VALID(epr) EP_TOGGLE_SET((epr), USB_EP_TX_VALID, USB_EPTX_STAT)
  34. #define EP_RX_VALID(epr) EP_TOGGLE_SET((epr), USB_EP_RX_VALID, USB_EPRX_STAT)
  35. #define STATUS_VAL(x) (USBD_HW_BC | (x))
  36. typedef struct {
  37. uint16_t addr;
  38. uint16_t cnt;
  39. } pma_rec;
  40. typedef union pma_table {
  41. struct {
  42. pma_rec tx;
  43. pma_rec rx;
  44. };
  45. struct {
  46. pma_rec tx0;
  47. pma_rec tx1;
  48. };
  49. struct {
  50. pma_rec rx0;
  51. pma_rec rx1;
  52. };
  53. } pma_table;
  54. /** \brief Helper function. Returns pointer to the buffer descriptor table.
  55. */
  56. inline static pma_table *EPT(uint8_t ep) {
  57. return (pma_table*)((ep & 0x07) * 8 + USB_PMAADDR);
  58. }
  59. /** \brief Helper function. Returns pointer to the endpoint control register.
  60. */
  61. inline static volatile uint16_t *EPR(uint8_t ep) {
  62. return (uint16_t*)((ep & 0x07) * 4 + USB_BASE);
  63. }
  64. /** \brief Helper function. Returns next available PMA buffer.
  65. *
  66. * \param sz uint16_t Requested buffer size.
  67. * \return uint16_t Buffer address for PMA table.
  68. * \note PMA buffers grown from top to bottom like stack.
  69. */
  70. static uint16_t get_next_pma(uint16_t sz) {
  71. unsigned _result = USB_PMASIZE;
  72. for (int i = 0; i < 8; i++) {
  73. pma_table *tbl = EPT(i);
  74. if ((tbl->rx.addr) && (tbl->rx.addr < _result)) _result = tbl->rx.addr;
  75. if ((tbl->tx.addr) && (tbl->tx.addr < _result)) _result = tbl->tx.addr;
  76. }
  77. return (_result < (0x020 + sz)) ? 0 : (_result - sz);
  78. }
  79. static uint32_t getinfo(void) {
  80. if (!(RCC->APB1ENR & RCC_APB1ENR_USBEN)) return STATUS_VAL(0);
  81. if (USB->BCDR & USB_BCDR_DPPU) return STATUS_VAL(USBD_HW_ENABLED | USBD_HW_SPEED_FS);
  82. return STATUS_VAL(USBD_HW_ENABLED);
  83. }
  84. static void ep_setstall(uint8_t ep, bool stall) {
  85. volatile uint16_t *reg = EPR(ep);
  86. /* ISOCHRONOUS endpoint can't be stalled or unstalled */
  87. if (USB_EP_ISOCHRONOUS == (*reg & USB_EP_T_FIELD)) return;
  88. /* If it's an IN endpoint */
  89. if (ep & 0x80) {
  90. /* DISABLED endpoint can't be stalled or unstalled */
  91. if (USB_EP_TX_DIS == (*reg & USB_EPTX_STAT)) return;
  92. if (stall) {
  93. EP_TX_STALL(reg);
  94. } else {
  95. /* if it's a doublebuffered endpoint */
  96. if ((USB_EP_KIND | USB_EP_BULK) == (*reg & (USB_EP_T_FIELD | USB_EP_KIND))) {
  97. /* set endpoint to VALID and clear DTOG_TX & SWBUF_TX */
  98. EP_DTX_UNSTALL(reg);
  99. } else {
  100. /* set endpoint to NAKED and clear DTOG_TX */
  101. EP_TX_UNSTALL(reg);
  102. }
  103. }
  104. } else {
  105. if (USB_EP_RX_DIS == (*reg & USB_EPRX_STAT)) return;
  106. if (stall) {
  107. EP_RX_STALL(reg);
  108. } else {
  109. /* if it's a doublebuffered endpoint */
  110. if ((USB_EP_KIND | USB_EP_BULK) == (*reg & (USB_EP_T_FIELD | USB_EP_KIND))) {
  111. /* set endpoint to VALID, clear DTOG_RX, set SWBUF_RX */
  112. EP_DRX_UNSTALL(reg);
  113. } else {
  114. /* set endpoint to VALID and clear DTOG_RX */
  115. EP_RX_UNSTALL(reg);
  116. }
  117. }
  118. }
  119. }
  120. static bool ep_isstalled(uint8_t ep) {
  121. if (ep & 0x80) {
  122. return (USB_EP_TX_STALL == (USB_EPTX_STAT & *EPR(ep)));
  123. } else {
  124. return (USB_EP_RX_STALL == (USB_EPRX_STAT & *EPR(ep)));
  125. }
  126. }
  127. static void enable(bool enable) {
  128. if (enable) {
  129. RCC->APB1ENR |= RCC_APB1ENR_USBEN;
  130. RCC->APB1RSTR |= RCC_APB1RSTR_USBRST;
  131. RCC->APB1RSTR &= ~RCC_APB1RSTR_USBRST;
  132. #if defined(USBD_PINS_REMAP) && (defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F070x6))
  133. RCC->APB2ENR |= RCC_APB2ENR_SYSCFGCOMPEN;
  134. SYSCFG->CFGR1 |= SYSCFG_CFGR1_PA11_PA12_RMP; // remap USB pins for small packages
  135. #endif
  136. USB->CNTR = USB_CNTR_CTRM | USB_CNTR_RESETM | USB_CNTR_ERRM |
  137. #if !defined(USBD_SOF_DISABLED)
  138. USB_CNTR_SOFM |
  139. #endif
  140. USB_CNTR_SUSPM | USB_CNTR_WKUPM;
  141. } else if (RCC->APB1ENR & RCC_APB1ENR_USBEN) {
  142. USB->BCDR = 0;
  143. RCC->APB1RSTR |= RCC_APB1RSTR_USBRST;
  144. RCC->APB1ENR &= ~RCC_APB1ENR_USBEN;
  145. }
  146. }
  147. static uint8_t connect(bool connect) {
  148. uint8_t res;
  149. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_DCDEN;
  150. if (USB->BCDR & USB_BCDR_DCDET) {
  151. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_PDEN;
  152. if (USB->BCDR & USB_BCDR_PS2DET) {
  153. res = usbd_lane_unk;
  154. } else if (USB->BCDR & USB_BCDR_PDET) {
  155. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_SDEN;
  156. if (USB->BCDR & USB_BCDR_SDET) {
  157. res = usbd_lane_dcp;
  158. } else {
  159. res = usbd_lane_cdp;
  160. }
  161. } else {
  162. res = usbd_lane_sdp;
  163. }
  164. } else {
  165. res = usbd_lane_dsc;
  166. }
  167. USB->BCDR = (connect) ? USB_BCDR_DPPU : 0;
  168. return res;
  169. }
  170. static void setaddr (uint8_t addr) {
  171. USB->DADDR = USB_DADDR_EF | addr;
  172. }
  173. static bool ep_config(uint8_t ep, uint8_t eptype, uint16_t epsize) {
  174. volatile uint16_t *reg = EPR(ep);
  175. pma_table *tbl = EPT(ep);
  176. /* epsize should be 16-bit aligned */
  177. if (epsize & 0x01) epsize++;
  178. switch (eptype) {
  179. case USB_EPTYPE_CONTROL:
  180. *reg = USB_EP_CONTROL | (ep & 0x07);
  181. break;
  182. case USB_EPTYPE_ISOCHRONUS:
  183. *reg = USB_EP_ISOCHRONOUS | (ep & 0x07);
  184. break;
  185. case USB_EPTYPE_BULK:
  186. *reg = USB_EP_BULK | (ep & 0x07);
  187. break;
  188. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  189. *reg = USB_EP_BULK | USB_EP_KIND | (ep & 0x07);
  190. break;
  191. default:
  192. *reg = USB_EP_INTERRUPT | (ep & 0x07);
  193. break;
  194. }
  195. /* if it TX or CONTROL endpoint */
  196. if ((ep & 0x80) || (eptype == USB_EPTYPE_CONTROL)) {
  197. uint16_t _pma;
  198. _pma = get_next_pma(epsize);
  199. if (_pma == 0) return false;
  200. tbl->tx.addr = _pma;
  201. tbl->tx.cnt = 0;
  202. if ((eptype == USB_EPTYPE_ISOCHRONUS) ||
  203. (eptype == (USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF))) {
  204. _pma = get_next_pma(epsize);
  205. if (_pma == 0) return false;
  206. tbl->tx1.addr = _pma;
  207. tbl->tx1.cnt = 0;
  208. EP_DTX_UNSTALL(reg);
  209. } else {
  210. EP_TX_UNSTALL(reg);
  211. }
  212. }
  213. if (!(ep & 0x80)) {
  214. uint16_t _rxcnt;
  215. uint16_t _pma;
  216. if (epsize > 62) {
  217. if (epsize & 0x1F) {
  218. epsize &= ~0x1F;
  219. epsize += 0x20;
  220. }
  221. _rxcnt = 0x8000 - 0x20 + (epsize << 5);
  222. } else {
  223. _rxcnt = epsize << 9;
  224. }
  225. _pma = get_next_pma(epsize);
  226. if (_pma == 0) return false;
  227. tbl->rx.addr = _pma;
  228. tbl->rx.cnt = _rxcnt;
  229. if ((eptype == USB_EPTYPE_ISOCHRONUS) ||
  230. (eptype == (USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF))) {
  231. _pma = get_next_pma(epsize);
  232. if (_pma == 0) return false;
  233. tbl->rx0.addr = _pma;
  234. tbl->rx0.cnt = _rxcnt;
  235. EP_DRX_UNSTALL(reg);
  236. } else {
  237. EP_RX_UNSTALL(reg);
  238. }
  239. }
  240. return true;
  241. }
  242. static void ep_deconfig(uint8_t ep) {
  243. pma_table *ept = EPT(ep);
  244. *EPR(ep) &= ~USB_EPREG_MASK;
  245. ept->rx.addr = 0;
  246. ept->rx.cnt = 0;
  247. ept->tx.addr = 0;
  248. ept->tx.cnt = 0;
  249. }
  250. static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) {
  251. uint16_t tmp;
  252. uint16_t *pma = (void*)(USB_PMAADDR + rx->addr);
  253. uint16_t rxcnt = rx->cnt & 0x03FF;
  254. rx->cnt &= ~0x3FF;
  255. for(int idx = 0; idx < rxcnt; idx++) {
  256. if ((idx & 0x01) == 0) {
  257. tmp = *pma++;
  258. }
  259. if (idx < blen) {
  260. buf[idx] = tmp & 0xFF;
  261. tmp >>= 8;
  262. } else {
  263. return blen;
  264. }
  265. }
  266. return rxcnt;
  267. }
  268. static int32_t ep_read(uint8_t ep, void *buf, uint16_t blen) {
  269. pma_table *tbl = EPT(ep);
  270. volatile uint16_t *reg = EPR(ep);
  271. switch (*reg & (USB_EPRX_STAT | USB_EP_T_FIELD | USB_EP_KIND)) {
  272. /* doublebuffered bulk endpoint */
  273. case (USB_EP_RX_VALID | USB_EP_BULK | USB_EP_KIND):
  274. /* switching SWBUF if EP is NAKED */
  275. switch (*reg & (USB_EP_DTOG_RX | USB_EP_SWBUF_RX)) {
  276. case 0:
  277. case (USB_EP_DTOG_RX | USB_EP_SWBUF_RX):
  278. *reg = (*reg & USB_EPREG_MASK) | USB_EP_SWBUF_RX;
  279. break;
  280. default:
  281. break;
  282. }
  283. if (*reg & USB_EP_SWBUF_RX) {
  284. return pma_read(buf, blen, &(tbl->rx1));
  285. } else {
  286. return pma_read(buf, blen, &(tbl->rx0));
  287. }
  288. /* isochronous endpoint */
  289. case (USB_EP_RX_VALID | USB_EP_ISOCHRONOUS):
  290. if (*reg & USB_EP_DTOG_RX) {
  291. return pma_read(buf, blen, &(tbl->rx1));
  292. } else {
  293. return pma_read(buf, blen, &(tbl->rx0));
  294. }
  295. /* regular endpoint */
  296. case (USB_EP_RX_NAK | USB_EP_BULK):
  297. case (USB_EP_RX_NAK | USB_EP_CONTROL):
  298. case (USB_EP_RX_NAK | USB_EP_INTERRUPT):
  299. {
  300. int32_t res = pma_read(buf, blen, &(tbl->rx));
  301. /* setting endpoint to VALID state */
  302. EP_RX_VALID(reg);
  303. return res;
  304. }
  305. /* invalid or not ready */
  306. default:
  307. return -1;
  308. }
  309. }
  310. static void pma_write(uint8_t *buf, uint16_t blen, pma_rec *tx) {
  311. uint16_t *pma = (void*)(USB_PMAADDR + tx->addr);
  312. uint16_t tmp = 0;
  313. tx->cnt = blen;
  314. for (int idx=0; idx < blen; idx++) {
  315. tmp |= buf[idx] << ((idx & 0x01) ? 8 : 0);
  316. if ((idx & 0x01) || (idx + 1) == blen) {
  317. *pma++ = tmp;
  318. tmp = 0;
  319. }
  320. }
  321. }
  322. static int32_t ep_write(uint8_t ep, void *buf, uint16_t blen) {
  323. pma_table *tbl = EPT(ep);
  324. volatile uint16_t *reg = EPR(ep);
  325. switch (*reg & (USB_EPTX_STAT | USB_EP_T_FIELD | USB_EP_KIND)) {
  326. /* doublebuffered bulk endpoint */
  327. case (USB_EP_TX_NAK | USB_EP_BULK | USB_EP_KIND):
  328. if (*reg & USB_EP_SWBUF_TX) {
  329. pma_write(buf, blen, &(tbl->tx1));
  330. } else {
  331. pma_write(buf, blen, &(tbl->tx0));
  332. }
  333. *reg = (*reg & USB_EPREG_MASK) | USB_EP_SWBUF_TX;
  334. break;
  335. /* isochronous endpoint */
  336. case (USB_EP_TX_VALID | USB_EP_ISOCHRONOUS):
  337. if (!(*reg & USB_EP_DTOG_TX)) {
  338. pma_write(buf, blen, &(tbl->tx1));
  339. } else {
  340. pma_write(buf, blen, &(tbl->tx0));
  341. }
  342. break;
  343. /* regular endpoint */
  344. case (USB_EP_TX_NAK | USB_EP_BULK):
  345. case (USB_EP_TX_NAK | USB_EP_CONTROL):
  346. case (USB_EP_TX_NAK | USB_EP_INTERRUPT):
  347. pma_write(buf, blen, &(tbl->tx));
  348. EP_TX_VALID(reg);
  349. break;
  350. /* invalid or not ready */
  351. default:
  352. return -1;
  353. }
  354. return blen;
  355. }
  356. static uint16_t get_frame (void) {
  357. return USB->FNR & USB_FNR_FN;
  358. }
  359. static void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
  360. uint8_t _ev, _ep;
  361. uint16_t _istr = USB->ISTR;
  362. _ep = _istr & USB_ISTR_EP_ID;
  363. if (_istr & USB_ISTR_CTR) {
  364. volatile uint16_t *reg = EPR(_ep);
  365. if (*reg & USB_EP_CTR_TX) {
  366. *reg &= (USB_EPREG_MASK ^ USB_EP_CTR_TX);
  367. _ep |= 0x80;
  368. _ev = usbd_evt_eptx;
  369. } else {
  370. *reg &= (USB_EPREG_MASK ^ USB_EP_CTR_RX);
  371. _ev = (*reg & USB_EP_SETUP) ? usbd_evt_epsetup : usbd_evt_eprx;
  372. }
  373. } else if (_istr & USB_ISTR_RESET) {
  374. USB->ISTR &= ~USB_ISTR_RESET;
  375. USB->BTABLE = 0;
  376. for (int i = 0; i < 8; i++) {
  377. ep_deconfig(i);
  378. }
  379. _ev = usbd_evt_reset;
  380. #if !defined(USBD_SOF_DISABLED)
  381. } else if (_istr & USB_ISTR_SOF) {
  382. _ev = usbd_evt_sof;
  383. USB->ISTR &= ~USB_ISTR_SOF;
  384. #endif
  385. } else if (_istr & USB_ISTR_WKUP) {
  386. _ev = usbd_evt_wkup;
  387. USB->CNTR &= ~USB_CNTR_FSUSP;
  388. USB->ISTR &= ~USB_ISTR_WKUP;
  389. } else if (_istr & USB_ISTR_SUSP) {
  390. _ev = usbd_evt_susp;
  391. USB->CNTR |= USB_CNTR_FSUSP;
  392. USB->ISTR &= ~USB_ISTR_SUSP;
  393. } else if (_istr & USB_ISTR_ERR) {
  394. USB->ISTR &= ~USB_ISTR_ERR;
  395. _ev = usbd_evt_error;
  396. } else {
  397. return;
  398. }
  399. callback(dev, _ev, _ep);
  400. }
  401. static uint32_t fnv1a32_turn (uint32_t fnv, uint32_t data ) {
  402. for (int i = 0; i < 4 ; i++) {
  403. fnv ^= (data & 0xFF);
  404. fnv *= 16777619;
  405. data >>= 8;
  406. }
  407. return fnv;
  408. }
  409. static uint16_t get_serialno_desc(void *buffer) {
  410. struct usb_string_descriptor *dsc = buffer;
  411. uint16_t *str = dsc->wString;
  412. uint32_t fnv = 2166136261;
  413. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x00));
  414. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x04));
  415. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x14));
  416. for (int i = 28; i >= 0; i -= 4 ) {
  417. uint16_t c = (fnv >> i) & 0x0F;
  418. c += (c < 10) ? '0' : ('A' - 10);
  419. *str++ = c;
  420. }
  421. dsc->bDescriptorType = USB_DTYPE_STRING;
  422. dsc->bLength = 18;
  423. return 18;
  424. }
  425. __attribute__((externally_visible)) const struct usbd_driver usbd_devfs = {
  426. getinfo,
  427. enable,
  428. connect,
  429. setaddr,
  430. ep_config,
  431. ep_deconfig,
  432. ep_read,
  433. ep_write,
  434. ep_setstall,
  435. ep_isstalled,
  436. evt_poll,
  437. get_frame,
  438. get_serialno_desc,
  439. };
  440. #endif //USBD_STM32L052