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. #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. typedef struct {
  36. uint16_t addr;
  37. uint16_t cnt;
  38. } pma_rec;
  39. typedef union pma_table {
  40. struct {
  41. pma_rec tx;
  42. pma_rec rx;
  43. };
  44. struct {
  45. pma_rec tx0;
  46. pma_rec tx1;
  47. };
  48. struct {
  49. pma_rec rx0;
  50. pma_rec rx1;
  51. };
  52. } pma_table;
  53. /** \brief Helper function. Returns pointer to the buffer descriptor table.
  54. */
  55. inline static pma_table *EPT(uint8_t ep) {
  56. return (pma_table*)((ep & 0x07) * 8 + USB_PMAADDR);
  57. }
  58. /** \brief Helper function. Returns pointer to the endpoint control register.
  59. */
  60. inline static volatile uint16_t *EPR(uint8_t ep) {
  61. return (uint16_t*)((ep & 0x07) * 4 + USB_BASE);
  62. }
  63. /** \brief Helper function. Returns next available PMA buffer.
  64. *
  65. * \param sz uint16_t Requested buffer size.
  66. * \return uint16_t Buffer address for PMA table.
  67. * \note PMA buffers grown from top to bottom like stack.
  68. */
  69. static uint16_t get_next_pma(uint16_t sz) {
  70. unsigned _result = USB_PMASIZE;
  71. for (int i = 0; i < 8; i++) {
  72. pma_table *tbl = EPT(i);
  73. if ((tbl->rx.addr) && (tbl->rx.addr < _result)) _result = tbl->rx.addr;
  74. if ((tbl->tx.addr) && (tbl->tx.addr < _result)) _result = tbl->tx.addr;
  75. }
  76. if ( _result < (8 * sizeof(pma_table) + sz)) {
  77. return 0;
  78. } else {
  79. return _result - sz;
  80. }
  81. }
  82. void ep_setstall(uint8_t ep, bool stall) {
  83. volatile uint16_t *reg = EPR(ep);
  84. /* ISOCHRONOUS endpoint can't be stalled or unstalled */
  85. if (USB_EP_ISOCHRONOUS == (*reg & USB_EP_T_FIELD)) return;
  86. /* If it's an IN endpoint */
  87. if (ep & 0x80) {
  88. /* DISABLED endpoint can't be stalled or unstalled */
  89. if (USB_EP_TX_DIS == (*reg & USB_EPTX_STAT)) return;
  90. if (stall) {
  91. EP_TX_STALL(reg);
  92. } else {
  93. /* if it's a doublebuffered endpoint */
  94. if ((USB_EP_KIND | USB_EP_BULK) == (*reg & (USB_EP_T_FIELD | USB_EP_KIND))) {
  95. /* set endpoint to VALID and clear DTOG_TX & SWBUF_TX */
  96. EP_DTX_UNSTALL(reg);
  97. } else {
  98. /* set endpoint to NAKED and clear DTOG_TX */
  99. EP_TX_UNSTALL(reg);
  100. }
  101. }
  102. } else {
  103. if (USB_EP_RX_DIS == (*reg & USB_EPRX_STAT)) return;
  104. if (stall) {
  105. EP_RX_STALL(reg);
  106. } else {
  107. /* if it's a doublebuffered endpoint */
  108. if ((USB_EP_KIND | USB_EP_BULK) == (*reg & (USB_EP_T_FIELD | USB_EP_KIND))) {
  109. /* set endpoint to VALID, clear DTOG_RX, set SWBUF_RX */
  110. EP_DRX_UNSTALL(reg);
  111. } else {
  112. /* set endpoint to VALID and clear DTOG_RX */
  113. EP_RX_UNSTALL(reg);
  114. }
  115. }
  116. }
  117. }
  118. bool ep_isstalled(uint8_t ep) {
  119. if (ep & 0x80) {
  120. return (USB_EP_TX_STALL == (USB_EPTX_STAT & *EPR(ep)));
  121. } else {
  122. return (USB_EP_RX_STALL == (USB_EPRX_STAT & *EPR(ep)));
  123. }
  124. }
  125. void enable(bool enable) {
  126. if (enable) {
  127. RCC->APB1ENR |= RCC_APB1ENR_USBEN;
  128. RCC->APB1RSTR |= RCC_APB1RSTR_USBRST;
  129. RCC->APB1RSTR &= ~RCC_APB1RSTR_USBRST;
  130. USB->CNTR = USB_CNTR_CTRM | USB_CNTR_RESETM | USB_CNTR_ERRM |
  131. #if !defined(USBD_SOF_DISABLED)
  132. USB_CNTR_SOFM |
  133. #endif
  134. USB_CNTR_SUSPM | USB_CNTR_WKUPM;
  135. } else if (RCC->APB1ENR & RCC_APB1ENR_USBEN) {
  136. USB->BCDR = 0;
  137. RCC->APB1RSTR |= RCC_APB1RSTR_USBRST;
  138. RCC->APB1ENR &= ~RCC_APB1ENR_USBEN;
  139. }
  140. }
  141. void reset (void) {
  142. USB->CNTR |= USB_CNTR_FRES;
  143. USB->CNTR &= ~USB_CNTR_FRES;
  144. }
  145. uint8_t connect(bool connect) {
  146. uint8_t res;
  147. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_DCDEN;
  148. if (USB->BCDR & USB_BCDR_DCDET) {
  149. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_PDEN;
  150. if (USB->BCDR & USB_BCDR_PS2DET) {
  151. res = usbd_lane_unk;
  152. } else if (USB->BCDR & USB_BCDR_PDET) {
  153. USB->BCDR = USB_BCDR_BCDEN | USB_BCDR_SDEN;
  154. if (USB->BCDR & USB_BCDR_SDET) {
  155. res = usbd_lane_dcp;
  156. } else {
  157. res = usbd_lane_cdp;
  158. }
  159. } else {
  160. res = usbd_lane_sdp;
  161. }
  162. } else {
  163. res = usbd_lane_dsc;
  164. }
  165. USB->BCDR = (connect) ? USB_BCDR_DPPU : 0;
  166. return res;
  167. }
  168. void setaddr (uint8_t addr) {
  169. USB->DADDR = USB_DADDR_EF | addr;
  170. }
  171. bool ep_config(uint8_t ep, uint8_t eptype, uint16_t epsize) {
  172. volatile uint16_t *reg = EPR(ep);
  173. pma_table *tbl = EPT(ep);
  174. /* epsize should be 16-bit aligned */
  175. if (epsize & 0x01) epsize++;
  176. switch (eptype) {
  177. case USB_EPTYPE_CONTROL:
  178. *reg = USB_EP_CONTROL | (ep & 0x07);
  179. break;
  180. case USB_EPTYPE_ISOCHRONUS:
  181. *reg = USB_EP_ISOCHRONOUS | (ep & 0x07);
  182. break;
  183. case USB_EPTYPE_BULK:
  184. *reg = USB_EP_BULK | (ep & 0x07);
  185. break;
  186. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  187. *reg = USB_EP_BULK | USB_EP_KIND | (ep & 0x07);
  188. break;
  189. default:
  190. *reg = USB_EP_INTERRUPT | (ep & 0x07);
  191. break;
  192. }
  193. /* if it TX or CONTROL endpoint */
  194. if ((ep & 0x80) || (eptype == USB_EPTYPE_CONTROL)) {
  195. uint16_t _pma;
  196. _pma = get_next_pma(epsize);
  197. if (_pma == 0) return false;
  198. tbl->tx.addr = _pma;
  199. tbl->tx.cnt = 0;
  200. if ((eptype == USB_EPTYPE_ISOCHRONUS) ||
  201. (eptype == (USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF))) {
  202. _pma = get_next_pma(epsize);
  203. if (_pma == 0) return false;
  204. tbl->tx1.addr = _pma;
  205. tbl->tx1.cnt = 0;
  206. EP_DTX_UNSTALL(reg);
  207. } else {
  208. EP_TX_UNSTALL(reg);
  209. }
  210. }
  211. if (!(ep & 0x80)) {
  212. uint16_t _rxcnt;
  213. uint16_t _pma;
  214. if (epsize > 62) {
  215. if (epsize & 0x1F) {
  216. epsize &= 0x1F;
  217. } else {
  218. epsize -= 0x20;
  219. }
  220. _rxcnt = 0x8000 | (epsize << 5);
  221. epsize += 0x20;
  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. 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. tx->cnt = blen;
  313. while (blen > 1) {
  314. *pma++ = buf[1] << 8 | buf[0];
  315. buf += 2;
  316. blen -= 2;
  317. }
  318. if (blen) *pma = *buf;
  319. }
  320. int32_t ep_write(uint8_t ep, void *buf, uint16_t blen) {
  321. pma_table *tbl = EPT(ep);
  322. volatile uint16_t *reg = EPR(ep);
  323. switch (*reg & (USB_EPTX_STAT | USB_EP_T_FIELD | USB_EP_KIND)) {
  324. /* doublebuffered bulk endpoint */
  325. case (USB_EP_TX_NAK | USB_EP_BULK | USB_EP_KIND):
  326. if (*reg & USB_EP_SWBUF_TX) {
  327. pma_write(buf, blen, &(tbl->tx1));
  328. } else {
  329. pma_write(buf, blen, &(tbl->tx0));
  330. }
  331. *reg = (*reg & USB_EPREG_MASK) | USB_EP_SWBUF_TX;
  332. break;
  333. /* isochronous endpoint */
  334. case (USB_EP_TX_VALID | USB_EP_ISOCHRONOUS):
  335. if (!(*reg & USB_EP_DTOG_TX)) {
  336. pma_write(buf, blen, &(tbl->tx1));
  337. } else {
  338. pma_write(buf, blen, &(tbl->tx0));
  339. }
  340. break;
  341. /* regular endpoint */
  342. case (USB_EP_TX_NAK | USB_EP_BULK):
  343. case (USB_EP_TX_NAK | USB_EP_CONTROL):
  344. case (USB_EP_TX_NAK | USB_EP_INTERRUPT):
  345. pma_write(buf, blen, &(tbl->tx));
  346. EP_TX_VALID(reg);
  347. break;
  348. /* invalid or not ready */
  349. default:
  350. return -1;
  351. }
  352. return blen;
  353. }
  354. uint16_t get_frame (void) {
  355. return USB->FNR & USB_FNR_FN;
  356. }
  357. void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
  358. uint8_t _ev, _ep;
  359. uint16_t _istr = USB->ISTR;
  360. _ep = _istr & USB_ISTR_EP_ID;
  361. if (_istr & USB_ISTR_CTR) {
  362. volatile uint16_t *reg = EPR(_ep);
  363. if (*reg & USB_EP_CTR_TX) {
  364. *reg &= (USB_EPREG_MASK ^ USB_EP_CTR_TX);
  365. _ep |= 0x80;
  366. _ev = usbd_evt_eptx;
  367. } else {
  368. *reg &= (USB_EPREG_MASK ^ USB_EP_CTR_RX);
  369. _ev = (*reg & USB_EP_SETUP) ? usbd_evt_epsetup : usbd_evt_eprx;
  370. }
  371. } else if (_istr & USB_ISTR_RESET) {
  372. USB->ISTR &= ~USB_ISTR_RESET;
  373. USB->BTABLE = 0;
  374. for (int i = 0; i < 8; i++) {
  375. ep_deconfig(i);
  376. }
  377. _ev = usbd_evt_reset;
  378. #if !defined(USBD_SOF_DISABLED)
  379. } else if (_istr & USB_ISTR_SOF) {
  380. _ev = usbd_evt_sof;
  381. USB->ISTR &= ~USB_ISTR_SOF;
  382. #endif
  383. } else if (_istr & USB_ISTR_WKUP) {
  384. _ev = usbd_evt_wkup;
  385. USB->CNTR &= ~USB_CNTR_FSUSP;
  386. USB->ISTR &= ~USB_ISTR_WKUP;
  387. } else if (_istr & USB_ISTR_SUSP) {
  388. _ev = usbd_evt_susp;
  389. USB->CNTR |= USB_CNTR_FSUSP;
  390. USB->ISTR &= ~USB_ISTR_SUSP;
  391. } else if (_istr & USB_ISTR_ERR) {
  392. USB->ISTR &= ~USB_ISTR_ERR;
  393. _ev = usbd_evt_error;
  394. } else {
  395. return;
  396. }
  397. callback(dev, _ev, _ep);
  398. }
  399. static uint32_t fnv1a32_turn (uint32_t fnv, uint32_t data ) {
  400. for (int i = 0; i < 4 ; i++) {
  401. fnv ^= (data & 0xFF);
  402. fnv *= 16777619;
  403. data >>= 8;
  404. }
  405. return fnv;
  406. }
  407. uint16_t get_serialno_desc(void *buffer) {
  408. struct usb_string_descriptor *dsc = buffer;
  409. uint16_t *str = dsc->wString;
  410. uint32_t fnv = 2166136261;
  411. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x00));
  412. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x04));
  413. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x14));
  414. for (int i = 28; i >= 0; i -= 4 ) {
  415. uint16_t c = (fnv >> i) & 0x0F;
  416. c += (c < 10) ? '0' : ('A' - 10);
  417. *str++ = c;
  418. }
  419. dsc->bDescriptorType = USB_DTYPE_STRING;
  420. dsc->bLength = 18;
  421. return 18;
  422. }
  423. const struct usbd_driver usbd_devfs = {
  424. USBD_HW_BC,
  425. enable,
  426. reset,
  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