usbd_stm32l052_devfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #warning 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. USB->CNTR = USB_CNTR_CTRM | USB_CNTR_RESETM | USB_CNTR_ERRM |
  133. #if !defined(USBD_SOF_DISABLED)
  134. USB_CNTR_SOFM |
  135. #endif
  136. USB_CNTR_SUSPM | USB_CNTR_WKUPM;
  137. } else if (RCC->APB1ENR & RCC_APB1ENR_USBEN) {
  138. USB->BCDR = 0;
  139. RCC->APB1RSTR |= RCC_APB1RSTR_USBRST;
  140. RCC->APB1ENR &= ~RCC_APB1ENR_USBEN;
  141. }
  142. }
  143. uint8_t connect(bool connect) {
  144. uint8_t res;
  145. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_DCDEN;
  146. if (USB->BCDR & USB_BCDR_DCDET) {
  147. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_PDEN;
  148. if (USB->BCDR & USB_BCDR_PS2DET) {
  149. res = usbd_lane_unk;
  150. } else if (USB->BCDR & USB_BCDR_PDET) {
  151. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_SDEN;
  152. if (USB->BCDR & USB_BCDR_SDET) {
  153. res = usbd_lane_dcp;
  154. } else {
  155. res = usbd_lane_cdp;
  156. }
  157. } else {
  158. res = usbd_lane_sdp;
  159. }
  160. } else {
  161. res = usbd_lane_dsc;
  162. }
  163. USB->BCDR = (connect) ? USB_BCDR_DPPU : 0;
  164. return res;
  165. }
  166. void setaddr (uint8_t addr) {
  167. USB->DADDR = USB_DADDR_EF | addr;
  168. }
  169. bool ep_config(uint8_t ep, uint8_t eptype, uint16_t epsize) {
  170. volatile uint16_t *reg = EPR(ep);
  171. pma_table *tbl = EPT(ep);
  172. /* epsize should be 16-bit aligned */
  173. if (epsize & 0x01) epsize++;
  174. switch (eptype) {
  175. case USB_EPTYPE_CONTROL:
  176. *reg = USB_EP_CONTROL | (ep & 0x07);
  177. break;
  178. case USB_EPTYPE_ISOCHRONUS:
  179. *reg = USB_EP_ISOCHRONOUS | (ep & 0x07);
  180. break;
  181. case USB_EPTYPE_BULK:
  182. *reg = USB_EP_BULK | (ep & 0x07);
  183. break;
  184. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  185. *reg = USB_EP_BULK | USB_EP_KIND | (ep & 0x07);
  186. break;
  187. default:
  188. *reg = USB_EP_INTERRUPT | (ep & 0x07);
  189. break;
  190. }
  191. /* if it TX or CONTROL endpoint */
  192. if ((ep & 0x80) || (eptype == USB_EPTYPE_CONTROL)) {
  193. uint16_t _pma;
  194. _pma = get_next_pma(epsize);
  195. if (_pma == 0) return false;
  196. tbl->tx.addr = _pma;
  197. tbl->tx.cnt = 0;
  198. if ((eptype == USB_EPTYPE_ISOCHRONUS) ||
  199. (eptype == (USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF))) {
  200. _pma = get_next_pma(epsize);
  201. if (_pma == 0) return false;
  202. tbl->tx1.addr = _pma;
  203. tbl->tx1.cnt = 0;
  204. EP_DTX_UNSTALL(reg);
  205. } else {
  206. EP_TX_UNSTALL(reg);
  207. }
  208. }
  209. if (!(ep & 0x80)) {
  210. uint16_t _rxcnt;
  211. uint16_t _pma;
  212. if (epsize > 62) {
  213. if (epsize & 0x1F) {
  214. epsize &= 0x1F;
  215. } else {
  216. epsize -= 0x20;
  217. }
  218. _rxcnt = 0x8000 | (epsize << 5);
  219. epsize += 0x20;
  220. } else {
  221. _rxcnt = epsize << 9;
  222. }
  223. _pma = get_next_pma(epsize);
  224. if (_pma == 0) return false;
  225. tbl->rx.addr = _pma;
  226. tbl->rx.cnt = _rxcnt;
  227. if ((eptype == USB_EPTYPE_ISOCHRONUS) ||
  228. (eptype == (USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF))) {
  229. _pma = get_next_pma(epsize);
  230. if (_pma == 0) return false;
  231. tbl->rx0.addr = _pma;
  232. tbl->rx0.cnt = _rxcnt;
  233. EP_DRX_UNSTALL(reg);
  234. } else {
  235. EP_RX_UNSTALL(reg);
  236. }
  237. }
  238. return true;
  239. }
  240. void ep_deconfig(uint8_t ep) {
  241. pma_table *ept = EPT(ep);
  242. *EPR(ep) &= ~USB_EPREG_MASK;
  243. ept->rx.addr = 0;
  244. ept->rx.cnt = 0;
  245. ept->tx.addr = 0;
  246. ept->tx.cnt = 0;
  247. }
  248. static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) {
  249. uint16_t *pma = (void*)(USB_PMAADDR + rx->addr);
  250. uint16_t rxcnt = rx->cnt & 0x03FF;
  251. rx->cnt &= ~0x3FF;
  252. if (blen > rxcnt) {
  253. blen = rxcnt;
  254. }
  255. rxcnt = blen;
  256. while (blen) {
  257. uint16_t _t = *pma;
  258. *buf++ = _t & 0xFF;
  259. if (--blen) {
  260. *buf++ = _t >> 8;
  261. pma++;
  262. blen--;
  263. } else break;
  264. }
  265. return rxcnt;
  266. }
  267. int32_t ep_read(uint8_t ep, void *buf, uint16_t blen) {
  268. pma_table *tbl = EPT(ep);
  269. volatile uint16_t *reg = EPR(ep);
  270. switch (*reg & (USB_EPRX_STAT | USB_EP_T_FIELD | USB_EP_KIND)) {
  271. /* doublebuffered bulk endpoint */
  272. case (USB_EP_RX_VALID | USB_EP_BULK | USB_EP_KIND):
  273. /* switching SWBUF if EP is NAKED */
  274. switch (*reg & (USB_EP_DTOG_RX | USB_EP_SWBUF_RX)) {
  275. case 0:
  276. case (USB_EP_DTOG_RX | USB_EP_SWBUF_RX):
  277. *reg = (*reg & USB_EPREG_MASK) | USB_EP_SWBUF_RX;
  278. default:
  279. break;
  280. }
  281. if (*reg & USB_EP_SWBUF_RX) {
  282. return pma_read(buf, blen, &(tbl->rx1));
  283. } else {
  284. return pma_read(buf, blen, &(tbl->rx0));
  285. }
  286. /* isochronous endpoint */
  287. case (USB_EP_RX_VALID | USB_EP_ISOCHRONOUS):
  288. if (*reg & USB_EP_DTOG_RX) {
  289. return pma_read(buf, blen, &(tbl->rx1));
  290. } else {
  291. return pma_read(buf, blen, &(tbl->rx0));
  292. }
  293. /* regular endpoint */
  294. case (USB_EP_RX_NAK | USB_EP_BULK):
  295. case (USB_EP_RX_NAK | USB_EP_CONTROL):
  296. case (USB_EP_RX_NAK | USB_EP_INTERRUPT):
  297. {
  298. int32_t res = pma_read(buf, blen, &(tbl->rx));
  299. /* setting endpoint to VALID state */
  300. EP_RX_VALID(reg);
  301. return res;
  302. }
  303. /* invalid or not ready */
  304. default:
  305. return -1;
  306. }
  307. }
  308. static void pma_write(uint8_t *buf, uint16_t blen, pma_rec *tx) {
  309. uint16_t *pma = (void*)(USB_PMAADDR + tx->addr);
  310. tx->cnt = blen;
  311. while (blen > 1) {
  312. *pma++ = buf[1] << 8 | buf[0];
  313. buf += 2;
  314. blen -= 2;
  315. }
  316. if (blen) *pma = *buf;
  317. }
  318. int32_t ep_write(uint8_t ep, void *buf, uint16_t blen) {
  319. pma_table *tbl = EPT(ep);
  320. volatile uint16_t *reg = EPR(ep);
  321. switch (*reg & (USB_EPTX_STAT | USB_EP_T_FIELD | USB_EP_KIND)) {
  322. /* doublebuffered bulk endpoint */
  323. case (USB_EP_TX_NAK | USB_EP_BULK | USB_EP_KIND):
  324. if (*reg & USB_EP_SWBUF_TX) {
  325. pma_write(buf, blen, &(tbl->tx1));
  326. } else {
  327. pma_write(buf, blen, &(tbl->tx0));
  328. }
  329. *reg = (*reg & USB_EPREG_MASK) | USB_EP_SWBUF_TX;
  330. break;
  331. /* isochronous endpoint */
  332. case (USB_EP_TX_VALID | USB_EP_ISOCHRONOUS):
  333. if (!(*reg & USB_EP_DTOG_TX)) {
  334. pma_write(buf, blen, &(tbl->tx1));
  335. } else {
  336. pma_write(buf, blen, &(tbl->tx0));
  337. }
  338. break;
  339. /* regular endpoint */
  340. case (USB_EP_TX_NAK | USB_EP_BULK):
  341. case (USB_EP_TX_NAK | USB_EP_CONTROL):
  342. case (USB_EP_TX_NAK | USB_EP_INTERRUPT):
  343. pma_write(buf, blen, &(tbl->tx));
  344. EP_TX_VALID(reg);
  345. break;
  346. /* invalid or not ready */
  347. default:
  348. return -1;
  349. }
  350. return blen;
  351. }
  352. uint16_t get_frame (void) {
  353. return USB->FNR & USB_FNR_FN;
  354. }
  355. void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
  356. uint8_t _ev, _ep;
  357. uint16_t _istr = USB->ISTR;
  358. _ep = _istr & USB_ISTR_EP_ID;
  359. if (_istr & USB_ISTR_CTR) {
  360. volatile uint16_t *reg = EPR(_ep);
  361. if (*reg & USB_EP_CTR_TX) {
  362. *reg &= (USB_EPREG_MASK ^ USB_EP_CTR_TX);
  363. _ep |= 0x80;
  364. _ev = usbd_evt_eptx;
  365. } else {
  366. *reg &= (USB_EPREG_MASK ^ USB_EP_CTR_RX);
  367. _ev = (*reg & USB_EP_SETUP) ? usbd_evt_epsetup : usbd_evt_eprx;
  368. }
  369. } else if (_istr & USB_ISTR_RESET) {
  370. USB->ISTR &= ~USB_ISTR_RESET;
  371. USB->BTABLE = 0;
  372. for (int i = 0; i < 8; i++) {
  373. ep_deconfig(i);
  374. }
  375. _ev = usbd_evt_reset;
  376. #if !defined(USBD_SOF_DISABLED)
  377. } else if (_istr & USB_ISTR_SOF) {
  378. _ev = usbd_evt_sof;
  379. USB->ISTR &= ~USB_ISTR_SOF;
  380. #endif
  381. } else if (_istr & USB_ISTR_WKUP) {
  382. _ev = usbd_evt_wkup;
  383. USB->CNTR &= ~USB_CNTR_FSUSP;
  384. USB->ISTR &= ~USB_ISTR_WKUP;
  385. } else if (_istr & USB_ISTR_SUSP) {
  386. _ev = usbd_evt_susp;
  387. USB->CNTR |= USB_CNTR_FSUSP;
  388. USB->ISTR &= ~USB_ISTR_SUSP;
  389. } else if (_istr & USB_ISTR_ERR) {
  390. USB->ISTR &= ~USB_ISTR_ERR;
  391. _ev = usbd_evt_error;
  392. } else {
  393. return;
  394. }
  395. callback(dev, _ev, _ep);
  396. }
  397. static uint32_t fnv1a32_turn (uint32_t fnv, uint32_t data ) {
  398. for (int i = 0; i < 4 ; i++) {
  399. fnv ^= (data & 0xFF);
  400. fnv *= 16777619;
  401. data >>= 8;
  402. }
  403. return fnv;
  404. }
  405. uint16_t get_serialno_desc(void *buffer) {
  406. struct usb_string_descriptor *dsc = buffer;
  407. uint16_t *str = dsc->wString;
  408. uint32_t fnv = 2166136261;
  409. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x00));
  410. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x04));
  411. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x14));
  412. for (int i = 28; i >= 0; i -= 4 ) {
  413. uint16_t c = (fnv >> i) & 0x0F;
  414. c += (c < 10) ? '0' : ('A' - 10);
  415. *str++ = c;
  416. }
  417. dsc->bDescriptorType = USB_DTYPE_STRING;
  418. dsc->bLength = 18;
  419. return 18;
  420. }
  421. __attribute__((externally_visible)) const struct usbd_driver usbd_devfs = {
  422. getinfo,
  423. enable,
  424. connect,
  425. setaddr,
  426. ep_config,
  427. ep_deconfig,
  428. ep_read,
  429. ep_write,
  430. ep_setstall,
  431. ep_isstalled,
  432. evt_poll,
  433. get_frame,
  434. get_serialno_desc,
  435. };
  436. #endif //USBD_STM32L052