usbd_stm32l052_devfs.c 14 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. 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. 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. 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. 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. 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. void setaddr (uint8_t addr) {
  171. USB->DADDR = USB_DADDR_EF | addr;
  172. }
  173. 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. 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 *pma = (void*)(USB_PMAADDR + rx->addr);
  252. uint16_t rxcnt = rx->cnt & 0x03FF;
  253. rx->cnt &= ~0x3FF;
  254. if (blen > rxcnt) {
  255. blen = rxcnt;
  256. }
  257. rxcnt = blen;
  258. while (blen) {
  259. uint16_t _t = *pma;
  260. *buf++ = _t & 0xFF;
  261. if (--blen) {
  262. *buf++ = _t >> 8;
  263. pma++;
  264. blen--;
  265. } else break;
  266. }
  267. return rxcnt;
  268. }
  269. int32_t ep_read(uint8_t ep, void *buf, uint16_t blen) {
  270. pma_table *tbl = EPT(ep);
  271. volatile uint16_t *reg = EPR(ep);
  272. switch (*reg & (USB_EPRX_STAT | USB_EP_T_FIELD | USB_EP_KIND)) {
  273. /* doublebuffered bulk endpoint */
  274. case (USB_EP_RX_VALID | USB_EP_BULK | USB_EP_KIND):
  275. /* switching SWBUF if EP is NAKED */
  276. switch (*reg & (USB_EP_DTOG_RX | USB_EP_SWBUF_RX)) {
  277. case 0:
  278. case (USB_EP_DTOG_RX | USB_EP_SWBUF_RX):
  279. *reg = (*reg & USB_EPREG_MASK) | USB_EP_SWBUF_RX;
  280. break;
  281. default:
  282. break;
  283. }
  284. if (*reg & USB_EP_SWBUF_RX) {
  285. return pma_read(buf, blen, &(tbl->rx1));
  286. } else {
  287. return pma_read(buf, blen, &(tbl->rx0));
  288. }
  289. /* isochronous endpoint */
  290. case (USB_EP_RX_VALID | USB_EP_ISOCHRONOUS):
  291. if (*reg & USB_EP_DTOG_RX) {
  292. return pma_read(buf, blen, &(tbl->rx1));
  293. } else {
  294. return pma_read(buf, blen, &(tbl->rx0));
  295. }
  296. /* regular endpoint */
  297. case (USB_EP_RX_NAK | USB_EP_BULK):
  298. case (USB_EP_RX_NAK | USB_EP_CONTROL):
  299. case (USB_EP_RX_NAK | USB_EP_INTERRUPT):
  300. {
  301. int32_t res = pma_read(buf, blen, &(tbl->rx));
  302. /* setting endpoint to VALID state */
  303. EP_RX_VALID(reg);
  304. return res;
  305. }
  306. /* invalid or not ready */
  307. default:
  308. return -1;
  309. }
  310. }
  311. static void pma_write(uint8_t *buf, uint16_t blen, pma_rec *tx) {
  312. uint16_t *pma = (void*)(USB_PMAADDR + tx->addr);
  313. tx->cnt = blen;
  314. while (blen > 1) {
  315. *pma++ = buf[1] << 8 | buf[0];
  316. buf += 2;
  317. blen -= 2;
  318. }
  319. if (blen) *pma = *buf;
  320. }
  321. int32_t ep_write(uint8_t ep, void *buf, uint16_t blen) {
  322. pma_table *tbl = EPT(ep);
  323. volatile uint16_t *reg = EPR(ep);
  324. switch (*reg & (USB_EPTX_STAT | USB_EP_T_FIELD | USB_EP_KIND)) {
  325. /* doublebuffered bulk endpoint */
  326. case (USB_EP_TX_NAK | USB_EP_BULK | USB_EP_KIND):
  327. if (*reg & USB_EP_SWBUF_TX) {
  328. pma_write(buf, blen, &(tbl->tx1));
  329. } else {
  330. pma_write(buf, blen, &(tbl->tx0));
  331. }
  332. *reg = (*reg & USB_EPREG_MASK) | USB_EP_SWBUF_TX;
  333. break;
  334. /* isochronous endpoint */
  335. case (USB_EP_TX_VALID | USB_EP_ISOCHRONOUS):
  336. if (!(*reg & USB_EP_DTOG_TX)) {
  337. pma_write(buf, blen, &(tbl->tx1));
  338. } else {
  339. pma_write(buf, blen, &(tbl->tx0));
  340. }
  341. break;
  342. /* regular endpoint */
  343. case (USB_EP_TX_NAK | USB_EP_BULK):
  344. case (USB_EP_TX_NAK | USB_EP_CONTROL):
  345. case (USB_EP_TX_NAK | USB_EP_INTERRUPT):
  346. pma_write(buf, blen, &(tbl->tx));
  347. EP_TX_VALID(reg);
  348. break;
  349. /* invalid or not ready */
  350. default:
  351. return -1;
  352. }
  353. return blen;
  354. }
  355. uint16_t get_frame (void) {
  356. return USB->FNR & USB_FNR_FN;
  357. }
  358. void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
  359. uint8_t _ev, _ep;
  360. uint16_t _istr = USB->ISTR;
  361. _ep = _istr & USB_ISTR_EP_ID;
  362. if (_istr & USB_ISTR_CTR) {
  363. volatile uint16_t *reg = EPR(_ep);
  364. if (*reg & USB_EP_CTR_TX) {
  365. *reg &= (USB_EPREG_MASK ^ USB_EP_CTR_TX);
  366. _ep |= 0x80;
  367. _ev = usbd_evt_eptx;
  368. } else {
  369. *reg &= (USB_EPREG_MASK ^ USB_EP_CTR_RX);
  370. _ev = (*reg & USB_EP_SETUP) ? usbd_evt_epsetup : usbd_evt_eprx;
  371. }
  372. } else if (_istr & USB_ISTR_RESET) {
  373. USB->ISTR &= ~USB_ISTR_RESET;
  374. USB->BTABLE = 0;
  375. for (int i = 0; i < 8; i++) {
  376. ep_deconfig(i);
  377. }
  378. _ev = usbd_evt_reset;
  379. #if !defined(USBD_SOF_DISABLED)
  380. } else if (_istr & USB_ISTR_SOF) {
  381. _ev = usbd_evt_sof;
  382. USB->ISTR &= ~USB_ISTR_SOF;
  383. #endif
  384. } else if (_istr & USB_ISTR_WKUP) {
  385. _ev = usbd_evt_wkup;
  386. USB->CNTR &= ~USB_CNTR_FSUSP;
  387. USB->ISTR &= ~USB_ISTR_WKUP;
  388. } else if (_istr & USB_ISTR_SUSP) {
  389. _ev = usbd_evt_susp;
  390. USB->CNTR |= USB_CNTR_FSUSP;
  391. USB->ISTR &= ~USB_ISTR_SUSP;
  392. } else if (_istr & USB_ISTR_ERR) {
  393. USB->ISTR &= ~USB_ISTR_ERR;
  394. _ev = usbd_evt_error;
  395. } else {
  396. return;
  397. }
  398. callback(dev, _ev, _ep);
  399. }
  400. static uint32_t fnv1a32_turn (uint32_t fnv, uint32_t data ) {
  401. for (int i = 0; i < 4 ; i++) {
  402. fnv ^= (data & 0xFF);
  403. fnv *= 16777619;
  404. data >>= 8;
  405. }
  406. return fnv;
  407. }
  408. uint16_t get_serialno_desc(void *buffer) {
  409. struct usb_string_descriptor *dsc = buffer;
  410. uint16_t *str = dsc->wString;
  411. uint32_t fnv = 2166136261;
  412. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x00));
  413. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x04));
  414. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x14));
  415. for (int i = 28; i >= 0; i -= 4 ) {
  416. uint16_t c = (fnv >> i) & 0x0F;
  417. c += (c < 10) ? '0' : ('A' - 10);
  418. *str++ = c;
  419. }
  420. dsc->bDescriptorType = USB_DTYPE_STRING;
  421. dsc->bLength = 18;
  422. return 18;
  423. }
  424. __attribute__((externally_visible)) const struct usbd_driver usbd_devfs = {
  425. getinfo,
  426. enable,
  427. connect,
  428. setaddr,
  429. ep_config,
  430. ep_deconfig,
  431. ep_read,
  432. ep_write,
  433. ep_setstall,
  434. ep_isstalled,
  435. evt_poll,
  436. get_frame,
  437. get_serialno_desc,
  438. };
  439. #endif //USBD_STM32L052