flabbergast's ARM/STM32 examples using libopencm3
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.

cdcacm.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * This file is part of the libopencm3 project.
  3. *
  4. * Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
  5. *
  6. * This library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <libopencm3/stm32/rcc.h>
  21. #include <libopencm3/stm32/flash.h>
  22. #include <libopencm3/stm32/gpio.h>
  23. #include <libopencm3/stm32/syscfg.h>
  24. #include <libopencm3/stm32/crs.h>
  25. #include <libopencm3/usb/usbd.h>
  26. #include <libopencm3/usb/cdc.h>
  27. static const struct usb_device_descriptor dev = {
  28. .bLength = USB_DT_DEVICE_SIZE,
  29. .bDescriptorType = USB_DT_DEVICE,
  30. .bcdUSB = 0x0200,
  31. .bDeviceClass = USB_CLASS_CDC,
  32. .bDeviceSubClass = 0,
  33. .bDeviceProtocol = 0,
  34. .bMaxPacketSize0 = 64,
  35. .idVendor = 0x0483,
  36. .idProduct = 0x5740,
  37. .bcdDevice = 0x0200,
  38. .iManufacturer = 1,
  39. .iProduct = 2,
  40. .iSerialNumber = 3,
  41. .bNumConfigurations = 1,
  42. };
  43. /*
  44. * This notification endpoint isn't implemented. According to CDC spec its
  45. * optional, but its absence causes a NULL pointer dereference in Linux
  46. * cdc_acm driver.
  47. */
  48. static const struct usb_endpoint_descriptor comm_endp[] = {{
  49. .bLength = USB_DT_ENDPOINT_SIZE,
  50. .bDescriptorType = USB_DT_ENDPOINT,
  51. .bEndpointAddress = 0x83,
  52. .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
  53. .wMaxPacketSize = 16,
  54. .bInterval = 255,
  55. }};
  56. static const struct usb_endpoint_descriptor data_endp[] = {{
  57. .bLength = USB_DT_ENDPOINT_SIZE,
  58. .bDescriptorType = USB_DT_ENDPOINT,
  59. .bEndpointAddress = 0x01,
  60. .bmAttributes = USB_ENDPOINT_ATTR_BULK,
  61. .wMaxPacketSize = 64,
  62. .bInterval = 1,
  63. }, {
  64. .bLength = USB_DT_ENDPOINT_SIZE,
  65. .bDescriptorType = USB_DT_ENDPOINT,
  66. .bEndpointAddress = 0x82,
  67. .bmAttributes = USB_ENDPOINT_ATTR_BULK,
  68. .wMaxPacketSize = 64,
  69. .bInterval = 1,
  70. }};
  71. static const struct {
  72. struct usb_cdc_header_descriptor header;
  73. struct usb_cdc_call_management_descriptor call_mgmt;
  74. struct usb_cdc_acm_descriptor acm;
  75. struct usb_cdc_union_descriptor cdc_union;
  76. } __attribute__((packed)) cdcacm_functional_descriptors = {
  77. .header = {
  78. .bFunctionLength = sizeof(struct usb_cdc_header_descriptor),
  79. .bDescriptorType = CS_INTERFACE,
  80. .bDescriptorSubtype = USB_CDC_TYPE_HEADER,
  81. .bcdCDC = 0x0110,
  82. },
  83. .call_mgmt = {
  84. .bFunctionLength =
  85. sizeof(struct usb_cdc_call_management_descriptor),
  86. .bDescriptorType = CS_INTERFACE,
  87. .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT,
  88. .bmCapabilities = 0,
  89. .bDataInterface = 1,
  90. },
  91. .acm = {
  92. .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor),
  93. .bDescriptorType = CS_INTERFACE,
  94. .bDescriptorSubtype = USB_CDC_TYPE_ACM,
  95. .bmCapabilities = 0,
  96. },
  97. .cdc_union = {
  98. .bFunctionLength = sizeof(struct usb_cdc_union_descriptor),
  99. .bDescriptorType = CS_INTERFACE,
  100. .bDescriptorSubtype = USB_CDC_TYPE_UNION,
  101. .bControlInterface = 0,
  102. .bSubordinateInterface0 = 1,
  103. }
  104. };
  105. static const struct usb_interface_descriptor comm_iface[] = {{
  106. .bLength = USB_DT_INTERFACE_SIZE,
  107. .bDescriptorType = USB_DT_INTERFACE,
  108. .bInterfaceNumber = 0,
  109. .bAlternateSetting = 0,
  110. .bNumEndpoints = 1,
  111. .bInterfaceClass = USB_CLASS_CDC,
  112. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  113. .bInterfaceProtocol = USB_CDC_PROTOCOL_AT,
  114. .iInterface = 0,
  115. .endpoint = comm_endp,
  116. .extra = &cdcacm_functional_descriptors,
  117. .extralen = sizeof(cdcacm_functional_descriptors)
  118. }};
  119. static const struct usb_interface_descriptor data_iface[] = {{
  120. .bLength = USB_DT_INTERFACE_SIZE,
  121. .bDescriptorType = USB_DT_INTERFACE,
  122. .bInterfaceNumber = 1,
  123. .bAlternateSetting = 0,
  124. .bNumEndpoints = 2,
  125. .bInterfaceClass = USB_CLASS_DATA,
  126. .bInterfaceSubClass = 0,
  127. .bInterfaceProtocol = 0,
  128. .iInterface = 0,
  129. .endpoint = data_endp,
  130. }};
  131. static const struct usb_interface ifaces[] = {{
  132. .num_altsetting = 1,
  133. .altsetting = comm_iface,
  134. }, {
  135. .num_altsetting = 1,
  136. .altsetting = data_iface,
  137. }};
  138. static const struct usb_config_descriptor config = {
  139. .bLength = USB_DT_CONFIGURATION_SIZE,
  140. .bDescriptorType = USB_DT_CONFIGURATION,
  141. .wTotalLength = 0,
  142. .bNumInterfaces = 2,
  143. .bConfigurationValue = 1,
  144. .iConfiguration = 0,
  145. .bmAttributes = 0x80,
  146. .bMaxPower = 0x32,
  147. .interface = ifaces,
  148. };
  149. static const char *usb_strings[] = {
  150. "flabbergast",
  151. "CDC-ACM Test",
  152. "DeMo",
  153. };
  154. /* Buffer to be used for control requests. */
  155. uint8_t usbd_control_buffer[128];
  156. /* Aux functions */
  157. static void led_flip(void) {
  158. if(GPIOA_IDR & (1<<15)) {
  159. GPIOA_BRR = GPIO15;
  160. } else {
  161. GPIOA_BSRR = GPIO15;
  162. }
  163. }
  164. static int cdcacm_control_request(usbd_device *usbd_dev,
  165. struct usb_setup_data *req, uint8_t **buf, uint16_t *len,
  166. void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
  167. {
  168. (void)complete;
  169. (void)buf;
  170. (void)usbd_dev;
  171. switch(req->bRequest) {
  172. case USB_CDC_REQ_SET_CONTROL_LINE_STATE: {
  173. /*
  174. * This Linux cdc_acm driver requires this to be implemented
  175. * even though it's optional in the CDC spec, and we don't
  176. * advertise it in the ACM functional descriptor.
  177. */
  178. char local_buf[10];
  179. struct usb_cdc_notification *notif = (void *)local_buf;
  180. /* We echo signals back to host as notification. */
  181. notif->bmRequestType = 0xA1;
  182. notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
  183. notif->wValue = 0;
  184. notif->wIndex = 0;
  185. notif->wLength = 2;
  186. local_buf[8] = req->wValue & 3;
  187. local_buf[9] = 0;
  188. // usbd_ep_write_packet(0x83, buf, 10);
  189. return 1;
  190. }
  191. case USB_CDC_REQ_SET_LINE_CODING:
  192. if(*len < sizeof(struct usb_cdc_line_coding))
  193. return 0;
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
  199. {
  200. (void)ep;
  201. char buf[64];
  202. int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64);
  203. if (len) {
  204. usbd_ep_write_packet(usbd_dev, 0x82, buf, len);
  205. buf[len] = 0;
  206. led_flip();
  207. }
  208. }
  209. static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue)
  210. {
  211. (void)wValue;
  212. usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb);
  213. usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL);
  214. usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
  215. usbd_register_control_callback(
  216. usbd_dev,
  217. USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
  218. USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
  219. cdcacm_control_request);
  220. }
  221. int main(void)
  222. {
  223. usbd_device *usbd_dev;
  224. #if defined(BOOTLOADER8K)
  225. # define SCB_VTOR MMIO32(0xE000ED08)
  226. SCB_VTOR = (uint32_t) 0x08002000;
  227. #endif
  228. #if defined(STM32F0)
  229. rcc_clock_setup_in_hsi_out_48mhz();
  230. crs_autotrim_usb_enable();
  231. rcc_set_usbclk_source(RCC_HSI48);
  232. // this is needed for F042 in TSSOP-20 package; USB is on shared pins
  233. //rcc_periph_clock_enable(RCC_SYSCFG_COMP);
  234. //SYSCFG_CFGR1 |= SYSCFG_CFGR1_PA11_PA12_RMP;
  235. #elif defined(STM32L0)
  236. // libopencm3 clock setup can be a bit ad-hoc, so let's configure manually
  237. // 32MHz, PLL supplied by the internal HSI16 oscillator
  238. /* Set the flash latency */
  239. FLASH_ACR |= FLASH_ACR_LATENCY_1WS;
  240. /* Turn on HSI16 */
  241. RCC_CR |= RCC_CR_HSI16ON;
  242. while ((RCC_CR & RCC_CR_HSI16RDY) == 0);
  243. /* Make sure PLL is off (it should be after reset, but ...) */
  244. RCC_CR &= ~RCC_CR_PLLON;
  245. while (RCC_CR & RCC_CR_PLLRDY);
  246. /* Set the PLL source to HSI16 */
  247. RCC_CFGR &= ~(1<<16); // RCC_CFGR_PLLSRC
  248. /* Set up the PLL */
  249. uint32_t reg = RCC_CFGR
  250. & ~( (RCC_CFGR_PLLMUL_MASK << RCC_CFGR_PLLMUL_SHIFT)
  251. | (RCC_CFGR_PLLDIV_MASK << RCC_CFGR_PLLDIV_SHIFT));
  252. RCC_CFGR = reg | (RCC_CFGR_PLLMUL_MUL4 << RCC_CFGR_PLLMUL_SHIFT)
  253. | (RCC_CFGR_PLLDIV_DIV2 << RCC_CFGR_PLLDIV_SHIFT);
  254. /* Turn on PLL and switch to it */
  255. RCC_CR |= RCC_CR_PLLON;
  256. while ((RCC_CR & RCC_CR_PLLRDY) == 0);
  257. RCC_CFGR |= RCC_CFGR_SW_PLL;
  258. /* Set the peripheral clock frequencies used. */
  259. rcc_ahb_frequency = 32000000;
  260. rcc_apb1_frequency = 32000000;
  261. rcc_apb2_frequency = 32000000;
  262. // end of manual clock configuration
  263. // Enable VREFINT reference for HSI48
  264. rcc_periph_clock_enable(RCC_SYSCFG);
  265. SYSCFG_CFGR3 |= SYSCFG_CFGR3_ENREF_HSI48;
  266. rcc_set_hsi48_source_rc48();
  267. crs_autotrim_usb_enable();
  268. rcc_osc_on(RCC_HSI48);
  269. rcc_wait_for_osc_ready(RCC_HSI48);
  270. /* MCO on PA9, to verify MCU speed with a scope */
  271. gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9);
  272. gpio_set_af(GPIOA, GPIO_AF0, GPIO9);
  273. RCC_CFGR |= (RCC_CFGR_MCO_SYSCLK << RCC_CFGR_MCO_SHIFT) | (RCC_CFGR_MCOPRE_DIV16 << 28);
  274. #else
  275. #error "Edit the sources and do the clock setup!"
  276. #endif
  277. rcc_periph_clock_enable(RCC_GPIOA);
  278. gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO15); // LED
  279. gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO1); // button, has external pull-down
  280. usbd_dev = usbd_init(&st_usbfs_v2_usb_driver, &dev, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer));
  281. usbd_register_set_config_callback(usbd_dev, cdcacm_set_config);
  282. char greeting[16] = "hello, world\r\n";
  283. while (1) {
  284. usbd_poll(usbd_dev);
  285. if (gpio_get(GPIOA, GPIO1)) {
  286. usbd_ep_write_packet(usbd_dev, 0x82, greeting, 15);
  287. for (uint32_t i = 0; i < 2000000; i++) { /* Wait a bit, cheap "debounce" */
  288. __asm__("nop");
  289. }
  290. }
  291. }
  292. }
  293. // vim: tabstop=4:shiftwidth=4:noexpandtab