Skip to content

Commit

Permalink
K64F - USBDevice addition
Browse files Browse the repository at this point in the history
  • Loading branch information
0xc0170 committed Feb 26, 2014
1 parent 9fcdbb8 commit c416573
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion libraries/USBDevice/USBDevice/USBEndpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef enum {
#include "USBEndpoints_LPC17_LPC23.h"
#elif defined(TARGET_LPC11UXX) || defined(TARGET_LPC1347)
#include "USBEndpoints_LPC11U.h"
#elif defined(TARGET_KL25Z)
#elif defined(TARGET_KL25Z) | defined(TARGET_K64F)
#include "USBEndpoints_KL25Z.h"
#elif defined (TARGET_STM32F4XX)
#include "USBEndpoints_STM32F4.h"
Expand Down
62 changes: 31 additions & 31 deletions libraries/USBDevice/USBDevice/USBHAL_KL25Z.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#if defined(TARGET_KL25Z)
#if defined(TARGET_KL25Z) | defined(TARGET_K64F)

#include "USBHAL.h"

Expand Down Expand Up @@ -59,7 +59,7 @@ typedef struct BDT {
uint8_t dummy; // RSVD: BD[8:15]
uint16_t byte_count; // BD[16:32]
uint32_t address; // Addr
} BDT;
} BDT;


// there are:
Expand All @@ -82,10 +82,10 @@ uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {
return 0;
}

USBHAL::USBHAL(void) {
USBHAL::USBHAL(void) {
// Disable IRQ
NVIC_DisableIRQ(USB0_IRQn);

// fill in callback array
epCallback[0] = &USBHAL::EP1_OUT_callback;
epCallback[1] = &USBHAL::EP1_IN_callback;
Expand Down Expand Up @@ -117,24 +117,24 @@ USBHAL::USBHAL(void) {
epCallback[27] = &USBHAL::EP14_IN_callback;
epCallback[28] = &USBHAL::EP15_OUT_callback;
epCallback[29] = &USBHAL::EP15_IN_callback;


// choose usb src as PLL
SIM->SOPT2 |= (SIM_SOPT2_USBSRC_MASK | SIM_SOPT2_PLLFLLSEL_MASK);

// enable OTG clock
SIM->SCGC4 |= SIM_SCGC4_USBOTG_MASK;

// Attach IRQ
instance = this;
NVIC_SetVector(USB0_IRQn, (uint32_t)&_usbisr);
NVIC_EnableIRQ(USB0_IRQn);

// USB Module Configuration
// Reset USB Module
USB0->USBTRC0 |= USB_USBTRC0_USBRESET_MASK;
while(USB0->USBTRC0 & USB_USBTRC0_USBRESET_MASK);

// Set BDT Base Register
USB0->BDTPAGE1=(uint8_t)((uint32_t)bdt>>8);
USB0->BDTPAGE2=(uint8_t)((uint32_t)bdt>>16);
Expand All @@ -144,14 +144,14 @@ USBHAL::USBHAL(void) {
USB0->ISTAT = 0xff;

// USB Interrupt Enablers
USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK |
USB_INTEN_SOFTOKEN_MASK |
USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK |
USB_INTEN_SOFTOKEN_MASK |
USB_INTEN_ERROREN_MASK |
USB_INTEN_USBRSTEN_MASK;
// Disable weak pull downs
USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK);

// Disable weak pull downs
USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK);

USB0->USBTRC0 |= 0x40;
}

Expand Down Expand Up @@ -296,9 +296,9 @@ EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_
uint32_t n, sz, idx, setup = 0;
uint8_t not_iso;
uint8_t * ep_buf;

uint32_t log_endpoint = PHY_TO_LOG(endpoint);

if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
return EP_INVALID;
}
Expand Down Expand Up @@ -335,7 +335,7 @@ EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_
if (((Data1 >> endpoint) & 1) == ((bdt[idx].info >> 6) & 1)) {
if (setup && (buffer[6] == 0)) // if no setup data stage,
Data1 &= ~1UL; // set DATA0
else
else
Data1 ^= (1 << endpoint);
}

Expand All @@ -345,7 +345,7 @@ EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_
else {
bdt[idx].info = BD_DTS_MASK | BD_OWN_MASK;
}

USB0->CTL &= ~USB_CTL_TXSUSPENDTOKENBUSY_MASK;
*bytesRead = sz;

Expand All @@ -368,27 +368,27 @@ EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)

idx = EP_BDT_IDX(PHY_TO_LOG(endpoint), TX, 0);
bdt[idx].byte_count = size;


// 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];
}

if ((Data1 >> endpoint) & 1) {
bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK;
} else {
bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK | BD_DATA01_MASK;
}

Data1 ^= (1 << endpoint);

return EP_PENDING;
}

Expand Down Expand Up @@ -429,7 +429,7 @@ void USBHAL::usbisr(void) {
uint8_t istat = USB0->ISTAT;

// reset interrupt
if (istat & USB_ISTAT_USBRST_MASK) {
if (istat & USB_ISTAT_USBRST_MASK) {
// disable all endpt
for(i = 0; i < 16; i++) {
USB0->ENDPOINT[i].ENDPT = 0x00;
Expand Down Expand Up @@ -457,11 +457,11 @@ void USBHAL::usbisr(void) {

// SOF interrupt
if (istat & USB_ISTAT_SOFTOK_MASK) {
USB0->ISTAT = USB_ISTAT_SOFTOK_MASK;
USB0->ISTAT = USB_ISTAT_SOFTOK_MASK;
// SOF event, read frame number
SOF(frameNumber());
}

// stall interrupt
if (istat & 1<<7) {
if (USB0->ENDPOINT[0].ENDPT & USB_ENDPT_EPSTALL_MASK)
Expand All @@ -483,7 +483,7 @@ void USBHAL::usbisr(void) {

// EP0 SETUP event (SETUP data received)
EP0setupCallback();

} else {
// OUT packet
if (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == OUT_TOKEN) {
Expand Down Expand Up @@ -517,11 +517,11 @@ void USBHAL::usbisr(void) {

USB0->ISTAT = USB_ISTAT_TOKDNE_MASK;
}

// sleep interrupt
if (istat & 1<<4) {
USB0->ISTAT |= USB_ISTAT_SLEEP_MASK;
}
}

// error interrupt
if (istat & USB_ISTAT_ERROR_MASK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ static void handle_interrupt_in(PortName port, int ch_base) {
for (i = 0; i < 32; i++) {
if (port_hal_read_pin_interrupt_flag(port, i)) {
uint32_t id = channel_ids[ch_base + i];
if (id == 0)
if (id == 0) {
continue;
}

gpio_irq_event event = IRQ_NONE;
switch (BR_PORT_PCRn_IRQC(port, i)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
#include "fsl_clock_manager.h"

void pin_function(PinName pin, int function) {
if (pin == (PinName)NC)
if (pin == (PinName)NC) {
return;
}

clock_manager_set_gate(kClockModulePORT, pin >> GPIO_PORT_SHIFT, true);
port_hal_mux_control(pin >> GPIO_PORT_SHIFT, pin & 0xFF, (port_mux_t)function);
}

void pin_mode(PinName pin, PinMode mode) {
if (pin == (PinName)NC)
if (pin == (PinName)NC) {
return;
}

uint32_t instance = pin >> GPIO_PORT_SHIFT;
uint32_t pinName = pin & 0xFF;
Expand Down

0 comments on commit c416573

Please sign in to comment.