Version of USBDevice that works for KL25Z on Mbed OS2.
Fork of USBDevice by
Revision 72:d236bae8866e, committed 2017-08-29
- Comitter:
- jeffmajeff
- Date:
- Tue Aug 29 17:48:52 2017 +0000
- Parent:
- 71:53949e6131f6
- Commit message:
- Fixed initial value for epComplete in USBHAL for KL25Z.
Changed in this revision
targets/TARGET_Freescale/USBHAL_KL25Z.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 53949e6131f6 -r d236bae8866e targets/TARGET_Freescale/USBHAL_KL25Z.cpp --- a/targets/TARGET_Freescale/USBHAL_KL25Z.cpp Thu Jul 27 12:14:04 2017 +0100 +++ b/targets/TARGET_Freescale/USBHAL_KL25Z.cpp Tue Aug 29 17:48:52 2017 +0000 @@ -25,7 +25,9 @@ USBHAL * USBHAL::instance; -static volatile int epComplete = 0; +// *** BUG FIX: USBDevice::write stuck in infinite loop upon first write if epComplete is initialized as 0x00 *** +static volatile int epComplete = 0x20; +//static volatile int epComplete = 0; // Convert physical endpoint number to register bit #define EP(endpoint) (1<<(endpoint)) @@ -64,12 +66,14 @@ uint32_t address; // Addr } BDT; + // there are: -// * 4 bidirectionnal endpt -> 8 physical endpt -// * as there are ODD and EVEN buffer -> 8*2 bdt +// * 16 bidirectionnal endpt -> 32 physical endpt +// * as there are ODD and EVEN buffer -> 32*2 bdt MBED_ALIGN(512) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2]; // 512 bytes aligned! -uint8_t * endpoint_buffer[NUMBER_OF_PHYSICAL_ENDPOINTS * 2]; +uint8_t * endpoint_buffer[(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2]; +uint8_t * endpoint_buffer_iso[2*2]; static uint8_t set_addr = 0; static uint8_t addr = 0; @@ -100,6 +104,28 @@ epCallback[5] = &USBHAL::EP3_IN_callback; epCallback[6] = &USBHAL::EP4_OUT_callback; epCallback[7] = &USBHAL::EP4_IN_callback; + epCallback[8] = &USBHAL::EP5_OUT_callback; + epCallback[9] = &USBHAL::EP5_IN_callback; + epCallback[10] = &USBHAL::EP6_OUT_callback; + epCallback[11] = &USBHAL::EP6_IN_callback; + epCallback[12] = &USBHAL::EP7_OUT_callback; + epCallback[13] = &USBHAL::EP7_IN_callback; + epCallback[14] = &USBHAL::EP8_OUT_callback; + epCallback[15] = &USBHAL::EP8_IN_callback; + epCallback[16] = &USBHAL::EP9_OUT_callback; + epCallback[17] = &USBHAL::EP9_IN_callback; + epCallback[18] = &USBHAL::EP10_OUT_callback; + epCallback[19] = &USBHAL::EP10_IN_callback; + epCallback[20] = &USBHAL::EP11_OUT_callback; + epCallback[21] = &USBHAL::EP11_IN_callback; + epCallback[22] = &USBHAL::EP12_OUT_callback; + epCallback[23] = &USBHAL::EP12_IN_callback; + epCallback[24] = &USBHAL::EP13_OUT_callback; + epCallback[25] = &USBHAL::EP13_IN_callback; + epCallback[26] = &USBHAL::EP14_OUT_callback; + epCallback[27] = &USBHAL::EP14_IN_callback; + epCallback[28] = &USBHAL::EP15_OUT_callback; + epCallback[29] = &USBHAL::EP15_IN_callback; #if defined(TARGET_KL43Z) || defined(TARGET_K22F) || defined(TARGET_K64F) // enable USBFS clock @@ -138,9 +164,9 @@ USB0->USBTRC0 |= 0x40; - /* Allocate control endpoint buffers */ - endpoint_buffer[EP_BDT_IDX(0, TX, ODD)] = (uint8_t *)malloc(MAX_PACKET_SIZE_EP0); - endpoint_buffer[EP_BDT_IDX(0, RX, ODD)] = (uint8_t *)malloc(MAX_PACKET_SIZE_EP0); + + + } USBHAL::~USBHAL(void) { } @@ -151,14 +177,14 @@ // Pull up enable USB0->CONTROL |= USB_CONTROL_DPPULLUPNONOTG_MASK; - // Allocate endpoint buffers; do allocate control endpoint buffers - for (int i = 4; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2); i++) { - if ((i == EPISO_OUT) || (i == EPISO_IN)) { - endpoint_buffer[i] = (uint8_t *)malloc(MAX_PACKET_SIZE_EPISO); - } else { - endpoint_buffer[i] = (uint8_t *)malloc(MAX_PACKET_SIZE_EPBULK); - } - } + + + + + + + + } void USBHAL::disconnect(void) { @@ -167,11 +193,15 @@ // Pull up disable USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK; - //Free buffers if required; do not free the control endpoint buffers - for (int i = 4; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2); i++) { + //Free buffers if required: + for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) { free(endpoint_buffer[i]); endpoint_buffer[i] = NULL; } + free(endpoint_buffer_iso[2]); + endpoint_buffer_iso[2] = NULL; + free(endpoint_buffer_iso[0]); + endpoint_buffer_iso[0] = NULL; } void USBHAL::configureDevice(void) { @@ -202,12 +232,27 @@ if ((flags & ISOCHRONOUS) == 0) { handshake_flag = USB_ENDPT_EPHSHK_MASK; - } + - if (IN_EP(endpoint)) { - buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0]; + if (IN_EP(endpoint)) { + if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL) + endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] = (uint8_t *) malloc (64); + buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0]; + } else { + if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL) + endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] = (uint8_t *) malloc (64); + buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0]; + } } else { - buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0]; + if (IN_EP(endpoint)) { + if (endpoint_buffer_iso[2] == NULL) + endpoint_buffer_iso[2] = (uint8_t *) malloc (1023); + buf = &endpoint_buffer_iso[2][0]; + } else { + if (endpoint_buffer_iso[0] == NULL) + endpoint_buffer_iso[0] = (uint8_t *) malloc (1023); + buf = &endpoint_buffer_iso[0][0]; + } } // IN endpt -> device to host (TX) @@ -301,7 +346,12 @@ setup = 1; } - ep_buf = endpoint_buffer[idx]; + // non iso endpoint + if (not_iso) { + ep_buf = endpoint_buffer[idx]; + } else { + ep_buf = endpoint_buffer_iso[0]; + } for (n = 0; n < sz; n++) { buffer[n] = ep_buf[n]; @@ -344,7 +394,13 @@ idx = EP_BDT_IDX(PHY_TO_LOG(endpoint), TX, 0); bdt[idx].byte_count = size; - ep_buf = endpoint_buffer[idx]; + + // non iso endpoint + if (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPHSHK_MASK) { + ep_buf = endpoint_buffer[idx]; + } else { + ep_buf = endpoint_buffer_iso[2]; + } for (n = 0; n < size; n++) { ep_buf[n] = data[n]; @@ -416,8 +472,8 @@ USB0->ERREN = 0xFF; // enable error interrupt sources USB0->ADDR = 0x00; // set default address - // reset bus for USBDevice layer - busReset(); + + return; }