From 08428f8300b657771d51c491aa3680d76dfbc2bd Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Mon, 19 Mar 2018 17:07:42 -0500 Subject: [PATCH] Fix Kinetis endpoint initialization Fully setup endpoint before enabling it in endpoint_add. This prevents undefined behavior due to previous configuration. --- targets/TARGET_Freescale/usb/USBPhy_Kinetis.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/targets/TARGET_Freescale/usb/USBPhy_Kinetis.cpp b/targets/TARGET_Freescale/usb/USBPhy_Kinetis.cpp index 3c98280a2c..55599a922e 100644 --- a/targets/TARGET_Freescale/usb/USBPhy_Kinetis.cpp +++ b/targets/TARGET_Freescale/usb/USBPhy_Kinetis.cpp @@ -366,15 +366,14 @@ bool USBPhyHw::endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_ // IN endpt -> device to host (TX) if (DESC_EP_IN(endpoint)) { + bdt[EP_BDT_IDX(log_endpoint, TX, ODD )].address = (uint32_t) buf; + bdt[EP_BDT_IDX(log_endpoint, TX, ODD )].info = 0; + bdt[EP_BDT_IDX(log_endpoint, TX, EVEN)].address = 0; USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag | // ep handshaking (not if iso endpoint) USB_ENDPT_EPTXEN_MASK; // en TX (IN) tran - bdt[EP_BDT_IDX(log_endpoint, TX, ODD )].address = (uint32_t) buf; - bdt[EP_BDT_IDX(log_endpoint, TX, EVEN)].address = 0; } // OUT endpt -> host to device (RX) else { - USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag | // ep handshaking (not if iso endpoint) - USB_ENDPT_EPRXEN_MASK; // en RX (OUT) tran. bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].byte_count = max_packet; bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].address = (uint32_t) buf; bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].info = BD_DTS_MASK; @@ -383,6 +382,8 @@ bool USBPhyHw::endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_ // Prepare for setup packet bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].info |= BD_OWN_MASK; } + USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag | // ep handshaking (not if iso endpoint) + USB_ENDPT_EPRXEN_MASK; // en RX (OUT) tran. } // First transfer will be a DATA0 packet