main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // Copyright (c) 2017-2022, Alex Taradov <alex@taradov.com>. All rights reserved.
  3. /*- Includes ----------------------------------------------------------------*/
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #include <stdalign.h>
  8. #include <string.h>
  9. #include "samd11.h"
  10. #include "hal_config.h"
  11. #include "nvm_data.h"
  12. #include "usb.h"
  13. #include "uart.h"
  14. #include "dap.h"
  15. #include "dap_config.h"
  16. /*- Definitions -------------------------------------------------------------*/
  17. #define USB_BUFFER_SIZE 64
  18. #define UART_WAIT_TIMEOUT 10 // ms
  19. #define STATUS_TIMEOUT 250 // ms
  20. /*- Variables ---------------------------------------------------------------*/
  21. static alignas(4) uint8_t app_req_buf_hid[DAP_CONFIG_PACKET_SIZE];
  22. static alignas(4) uint8_t app_req_buf_bulk[DAP_CONFIG_PACKET_SIZE];
  23. static alignas(4) uint8_t app_req_buf[DAP_CONFIG_PACKET_SIZE];
  24. static alignas(4) uint8_t app_resp_buf[DAP_CONFIG_PACKET_SIZE];
  25. static int app_req_buf_hid_size = 0;
  26. static int app_req_buf_bulk_size = 0;
  27. static bool app_resp_free = true;
  28. static uint64_t app_system_time = 0;
  29. static uint64_t app_status_timeout = 0;
  30. static bool app_dap_event = false;
  31. #ifdef HAL_CONFIG_ENABLE_VCP
  32. static alignas(4) uint8_t app_recv_buffer[USB_BUFFER_SIZE];
  33. static alignas(4) uint8_t app_send_buffer[USB_BUFFER_SIZE];
  34. static int app_recv_buffer_size = 0;
  35. static int app_recv_buffer_ptr = 0;
  36. static int app_send_buffer_ptr = 0;
  37. static bool app_send_buffer_free = true;
  38. static bool app_send_zlp = false;
  39. static uint64_t app_uart_timeout = 0;
  40. static uint64_t app_break_timeout = 0;
  41. static bool app_vcp_event = false;
  42. static bool app_vcp_open = false;
  43. #endif
  44. /*- Implementations ---------------------------------------------------------*/
  45. //-----------------------------------------------------------------------------
  46. static void sys_init(void)
  47. {
  48. uint32_t coarse, fine;
  49. NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_RWS(1);
  50. SYSCTRL->INTFLAG.reg = SYSCTRL_INTFLAG_BOD33RDY | SYSCTRL_INTFLAG_BOD33DET |
  51. SYSCTRL_INTFLAG_DFLLRDY;
  52. coarse = NVM_READ_CAL(NVM_DFLL48M_COARSE_CAL);
  53. fine = NVM_READ_CAL(NVM_DFLL48M_FINE_CAL);
  54. SYSCTRL->DFLLCTRL.reg = 0; // See Errata 9905
  55. while (0 == (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY));
  56. SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_MUL(48000);
  57. SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(fine);
  58. SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE | SYSCTRL_DFLLCTRL_USBCRM |
  59. SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_CCDIS;
  60. while (0 == (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY));
  61. GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(0) | GCLK_GENCTRL_SRC(GCLK_SOURCE_DFLL48M) |
  62. GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN;
  63. while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
  64. }
  65. //-----------------------------------------------------------------------------
  66. static void serial_number_init(void)
  67. {
  68. uint32_t wuid[4];
  69. uint8_t *uid = (uint8_t *)wuid;
  70. uint32_t sn = 5381;
  71. wuid[0] = *(volatile uint32_t *)0x0080a00c;
  72. wuid[1] = *(volatile uint32_t *)0x0080a040;
  73. wuid[2] = *(volatile uint32_t *)0x0080a044;
  74. wuid[3] = *(volatile uint32_t *)0x0080a048;
  75. for (int i = 0; i < 16; i++)
  76. sn = ((sn << 5) + sn) ^ uid[i];
  77. for (int i = 0; i < 8; i++)
  78. usb_serial_number[i] = "0123456789ABCDEF"[(sn >> (i * 4)) & 0xf];
  79. usb_serial_number[8] = 0;
  80. }
  81. //-----------------------------------------------------------------------------
  82. static void sys_time_init(void)
  83. {
  84. SysTick->VAL = 0;
  85. SysTick->LOAD = F_CPU / 1000ul;
  86. SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
  87. app_system_time = 0;
  88. }
  89. //-----------------------------------------------------------------------------
  90. static void sys_time_task(void)
  91. {
  92. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
  93. app_system_time++;
  94. }
  95. #ifdef HAL_CONFIG_ENABLE_VCP
  96. //-----------------------------------------------------------------------------
  97. static void tx_task(void)
  98. {
  99. while (app_recv_buffer_size)
  100. {
  101. if (!uart_write_byte(app_recv_buffer[app_recv_buffer_ptr]))
  102. break;
  103. app_recv_buffer_ptr++;
  104. app_recv_buffer_size--;
  105. app_vcp_event = true;
  106. if (0 == app_recv_buffer_size)
  107. usb_cdc_recv(app_recv_buffer, sizeof(app_recv_buffer));
  108. }
  109. }
  110. //-----------------------------------------------------------------------------
  111. static void send_buffer(void)
  112. {
  113. app_send_buffer_free = false;
  114. app_send_zlp = (USB_BUFFER_SIZE == app_send_buffer_ptr);
  115. usb_cdc_send(app_send_buffer, app_send_buffer_ptr);
  116. app_send_buffer_ptr = 0;
  117. }
  118. //-----------------------------------------------------------------------------
  119. static void rx_task(void)
  120. {
  121. int byte;
  122. if (!app_send_buffer_free)
  123. return;
  124. while (uart_read_byte(&byte))
  125. {
  126. int state = (byte >> 8) & 0xff;
  127. app_uart_timeout = app_system_time + UART_WAIT_TIMEOUT;
  128. app_vcp_event = true;
  129. if (state)
  130. {
  131. usb_cdc_set_state(state);
  132. }
  133. else
  134. {
  135. app_send_buffer[app_send_buffer_ptr++] = byte;
  136. if (USB_BUFFER_SIZE == app_send_buffer_ptr)
  137. {
  138. send_buffer();
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. //-----------------------------------------------------------------------------
  145. static void break_task(void)
  146. {
  147. if (app_break_timeout && app_system_time > app_break_timeout)
  148. {
  149. uart_set_break(false);
  150. app_break_timeout = 0;
  151. }
  152. }
  153. //-----------------------------------------------------------------------------
  154. static void uart_timer_task(void)
  155. {
  156. if (app_uart_timeout && app_system_time > app_uart_timeout)
  157. {
  158. if (app_send_zlp || app_send_buffer_ptr)
  159. send_buffer();
  160. app_uart_timeout = 0;
  161. }
  162. }
  163. //-----------------------------------------------------------------------------
  164. void usb_cdc_line_coding_updated(usb_cdc_line_coding_t *line_coding)
  165. {
  166. uart_init(line_coding);
  167. }
  168. //-----------------------------------------------------------------------------
  169. void usb_cdc_control_line_state_update(int line_state)
  170. {
  171. bool status = line_state & USB_CDC_CTRL_SIGNAL_DTE_PRESENT;
  172. app_vcp_open = status;
  173. app_send_buffer_ptr = 0;
  174. app_uart_timeout = 0;
  175. app_break_timeout = 0;
  176. if (app_vcp_open)
  177. uart_init(usb_cdc_get_line_coding());
  178. else
  179. uart_close();
  180. }
  181. //-----------------------------------------------------------------------------
  182. void usb_cdc_send_break(int duration)
  183. {
  184. if (USB_CDC_BREAK_DURATION_DISABLE == duration)
  185. {
  186. app_break_timeout = 0;
  187. uart_set_break(false);
  188. }
  189. else if (USB_CDC_BREAK_DURATION_INFINITE == duration)
  190. {
  191. app_break_timeout = 0;
  192. uart_set_break(true);
  193. }
  194. else
  195. {
  196. app_break_timeout = app_system_time + duration;
  197. uart_set_break(true);
  198. }
  199. }
  200. //-----------------------------------------------------------------------------
  201. void usb_cdc_send_callback(void)
  202. {
  203. app_send_buffer_free = true;
  204. }
  205. //-----------------------------------------------------------------------------
  206. void usb_cdc_recv_callback(int size)
  207. {
  208. app_recv_buffer_ptr = 0;
  209. app_recv_buffer_size = size;
  210. }
  211. #endif // HAL_CONFIG_ENABLE_VCP
  212. //-----------------------------------------------------------------------------
  213. void usb_hid_send_callback(void)
  214. {
  215. app_resp_free = true;
  216. }
  217. //-----------------------------------------------------------------------------
  218. void usb_hid_recv_callback(int size)
  219. {
  220. app_req_buf_hid_size = size;
  221. }
  222. //-----------------------------------------------------------------------------
  223. static void usb_bulk_send_callback(void)
  224. {
  225. app_resp_free = true;
  226. }
  227. //-----------------------------------------------------------------------------
  228. static void usb_bulk_recv_callback(int size)
  229. {
  230. app_req_buf_bulk_size = size;
  231. }
  232. //-----------------------------------------------------------------------------
  233. static void dap_task(void)
  234. {
  235. int interface, size;
  236. if (!app_resp_free)
  237. return;
  238. if (app_req_buf_hid_size)
  239. {
  240. interface = USB_INTF_HID;
  241. size = app_req_buf_hid_size;
  242. app_req_buf_hid_size = 0;
  243. memcpy(app_req_buf, app_req_buf_hid, size);
  244. usb_hid_recv(app_req_buf_hid, sizeof(app_req_buf_hid));
  245. }
  246. else if (app_req_buf_bulk_size)
  247. {
  248. interface = USB_INTF_BULK;
  249. size = app_req_buf_bulk_size;
  250. app_req_buf_bulk_size = 0;
  251. memcpy(app_req_buf, app_req_buf_bulk, size);
  252. usb_recv(USB_BULK_EP_RECV, app_req_buf_bulk, sizeof(app_req_buf_bulk));
  253. }
  254. else
  255. {
  256. return;
  257. }
  258. size = dap_process_request(app_req_buf, size, app_resp_buf, sizeof(app_resp_buf));
  259. if (USB_INTF_BULK == interface)
  260. usb_send(USB_BULK_EP_SEND, app_resp_buf, size);
  261. else
  262. usb_hid_send(app_resp_buf, sizeof(app_resp_buf));
  263. app_resp_free = false;
  264. app_dap_event = true;
  265. }
  266. //-----------------------------------------------------------------------------
  267. void usb_configuration_callback(int config)
  268. {
  269. app_resp_free = true;
  270. app_req_buf_hid_size = 0;
  271. app_req_buf_bulk_size = 0;
  272. usb_set_send_callback(USB_BULK_EP_SEND, usb_bulk_send_callback);
  273. usb_set_recv_callback(USB_BULK_EP_RECV, usb_bulk_recv_callback);
  274. usb_hid_recv(app_req_buf_hid, sizeof(app_req_buf_hid));
  275. usb_recv(USB_BULK_EP_RECV, app_req_buf_bulk, sizeof(app_req_buf_bulk));
  276. #ifdef HAL_CONFIG_ENABLE_VCP
  277. usb_cdc_recv(app_recv_buffer, sizeof(app_recv_buffer));
  278. app_send_buffer_free = true;
  279. app_send_buffer_ptr = 0;
  280. #endif
  281. (void)config;
  282. }
  283. //-----------------------------------------------------------------------------
  284. static void status_timer_task(void)
  285. {
  286. if (app_system_time < app_status_timeout)
  287. return;
  288. app_status_timeout = app_system_time + STATUS_TIMEOUT;
  289. if (app_dap_event)
  290. HAL_GPIO_DAP_STATUS_toggle();
  291. else
  292. HAL_GPIO_DAP_STATUS_set();
  293. app_dap_event = false;
  294. #ifdef HAL_CONFIG_ENABLE_VCP
  295. if (app_vcp_event)
  296. HAL_GPIO_VCP_STATUS_toggle();
  297. else
  298. HAL_GPIO_VCP_STATUS_write(app_vcp_open);
  299. app_vcp_event = false;
  300. #endif
  301. }
  302. //-----------------------------------------------------------------------------
  303. int main(void)
  304. {
  305. sys_init();
  306. sys_time_init();
  307. dap_init();
  308. usb_init();
  309. #ifdef HAL_CONFIG_ENABLE_VCP
  310. usb_cdc_init();
  311. #endif
  312. usb_hid_init();
  313. serial_number_init();
  314. app_status_timeout = STATUS_TIMEOUT;
  315. #ifdef HAL_CONFIG_ENABLE_VCP
  316. HAL_GPIO_VCP_STATUS_out();
  317. HAL_GPIO_VCP_STATUS_clr();
  318. #endif
  319. HAL_GPIO_DAP_STATUS_out();
  320. HAL_GPIO_DAP_STATUS_set();
  321. HAL_GPIO_BOOT_ENTER_in();
  322. HAL_GPIO_BOOT_ENTER_pullup();
  323. while (1)
  324. {
  325. sys_time_task();
  326. status_timer_task();
  327. usb_task();
  328. dap_task();
  329. #ifdef HAL_CONFIG_ENABLE_VCP
  330. tx_task();
  331. rx_task();
  332. break_task();
  333. uart_timer_task();
  334. #endif
  335. if (0 == HAL_GPIO_BOOT_ENTER_read())
  336. NVIC_SystemReset();
  337. }
  338. return 0;
  339. }