2 ===============================================================================
6 Copyright : $(copyright)
7 Description : main definition
8 ===============================================================================
15 #include "DAP_config.h"
19 #include "usb_driver.h"
23 #define TICKRATE_100msec (10)
24 #define TICKRATE_10msec (100)
25 #define TICKRATE_1msec (1000)
27 #define TICKRATE TICKRATE_1msec
29 // Connected LED RED PIN 21: PIO0_11
30 #define LED_ERROR_PORT 0
31 #define LED_ERROR_BIT 11
32 #define IOCON_ERROR_REG (LPC_IOCON->TDI_PIO0_11)
34 void indicator_code (int code);
36 void init_error_led () {
37 IOCON_ERROR_REG = 1 | 1 << 4 | 1 << 7;
38 LPC_GPIO->DIR[LED_ERROR_PORT] |= 1 << LED_ERROR_BIT;
39 LPC_GPIO->SET[LED_ERROR_PORT] = 1 << LED_ERROR_BIT;
42 INLINE void LED_ERROR_ON () {
43 LPC_GPIO->CLR[LED_ERROR_PORT] = 1 << LED_ERROR_BIT;
46 INLINE void LED_ERROR_OFF () {
47 LPC_GPIO->SET[LED_ERROR_PORT] = 1 << LED_ERROR_BIT;
51 static volatile uint32_t sys_time = 0;
54 void SysTick_Handler () {
58 void sleep_ms (int ms) {
60 int e = sys_time * (1000/TICKRATE) + ms;
61 while ((sys_time * (1000/TICKRATE)) <= e) {
66 // HID callbacks and buffer
68 typedef struct _IBDAP_HND_T {
69 volatile uint8_t usb_outs_end_idx;
70 volatile uint8_t usb_outs_start_idx;
71 uint8_t usb_outs[DAP_PACKET_COUNT][DAP_PACKET_SIZE];
72 volatile uint8_t usb_ins_end_idx;
73 volatile uint8_t usb_ins_start_idx;
74 volatile uint8_t usb_ins_busy;
75 uint8_t usb_ins[DAP_PACKET_COUNT][DAP_PACKET_SIZE];
78 IBDAP_HND_T* ibdap = 0;
81 ErrorCode_t DAP_GetReport_Callback( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* length) {
85 ErrorCode_t DAP_SetReport_Callback( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length) {
89 ErrorCode_t DAP_EpInOut_Hdlr_Callback (USBD_HANDLE_T hUsb, void* data, uint32_t event) {
90 USB_HID_CTRL_T *pHidCtrl = (USB_HID_CTRL_T *) data;
93 if (ibdap->usb_ins_start_idx == ibdap->usb_ins_end_idx) {
94 ibdap->usb_ins_busy = 0;
97 USBD_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, ibdap->usb_ins[ibdap->usb_ins_start_idx], DAP_PACKET_SIZE);
98 ibdap->usb_ins_start_idx = (ibdap->usb_ins_start_idx+1) % DAP_PACKET_COUNT;
99 //LED_RUNNING_OUT (1);
100 //LED_CONNECTED_OUT (0);
103 USBD_API->hw->ReadEP(hUsb, pHidCtrl->epout_adr, ibdap->usb_outs[ibdap->usb_outs_end_idx]);
104 ibdap->usb_outs_end_idx = (ibdap->usb_outs_end_idx+1) % DAP_PACKET_COUNT;
105 //LED_CONNECTED_OUT (1);
106 //LED_RUNNING_OUT (0);
121 void error_code (int code) {
124 if (code & 0x01 && !(code & 0x02 )) { // code == 1
125 LED_CONNECTED_OUT (1);
128 LED_CONNECTED_OUT (0);
129 } else if (!(code & 0x01) && (code & 0x02)) { // code == 2
130 LED_CONNECTED_OUT (0);
134 } else if ((code & 0x01) == 3) {
135 LED_CONNECTED_OUT (1);
138 LED_CONNECTED_OUT (0);
141 LED_CONNECTED_OUT (1);
144 LED_CONNECTED_OUT (0);
152 void indicator_code (int code) {
155 if (code & 0x01 && !(code & 0x02 )) { // code == 1
156 LED_CONNECTED_OUT (1);
159 LED_CONNECTED_OUT (0);
160 } else if (!(code & 0x01) && (code & 0x02)) { // code == 2
161 LED_CONNECTED_OUT (0);
165 } else if ((code & 0x01) == 3) {
166 LED_CONNECTED_OUT (1);
169 LED_CONNECTED_OUT (0);
172 LED_CONNECTED_OUT (1);
175 LED_CONNECTED_OUT (0);
182 // TODO: insert other definitions and declarations here
185 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << 6; // enable gpio clock
192 USBD_API_INIT_PARAM_T usb_param;
194 SysTick_Config(SystemCoreClock / TICKRATE);
203 if (init_usb_driver (&usb_param)) {
210 if (init_usb_hid (&usb_param,
211 &DAP_GetReport_Callback, &DAP_SetReport_Callback,
212 &DAP_EpInOut_Hdlr_Callback, &DAP_EpInOut_Hdlr_Callback,
213 (uint8_t**)&ibdap, sizeof (IBDAP_HND_T))) {
220 connect_to_usb_bus ();
229 if (ibdap->usb_outs_start_idx == ibdap->usb_outs_end_idx) {
234 LED_CONNECTED_OUT (1);
237 DAP_ProcessCommand (ibdap->usb_outs[ibdap->usb_outs_start_idx], ibdap->usb_ins[ibdap->usb_ins_end_idx]);
238 ibdap->usb_outs_start_idx = (ibdap->usb_outs_start_idx+1) % DAP_PACKET_COUNT;
239 ibdap->usb_ins_end_idx = (ibdap->usb_ins_end_idx+1) % DAP_PACKET_COUNT;
240 if (!ibdap->usb_ins_busy) { // kickstart
241 ibdap->usb_ins_busy = 1;
242 uint8_t idx = ibdap->usb_ins_start_idx;
243 ibdap->usb_ins_start_idx = (ibdap->usb_ins_start_idx+1) % DAP_PACKET_COUNT;
244 USBD_API->hw->WriteEP(g_usb_hnd, HID_EP_IN, ibdap->usb_ins[idx], DAP_PACKET_SIZE);
246 //LED_RUNNING_OUT (1);
247 //LED_CONNECTED_OUT (0);