- Main Page
- Classes
- Files
- Related Pages

- File List
- File Members

# usbhw_lpc.c File Reference

`#include "type.h"`
`#include "usbdebug.h"`
`#include "usbhw_lpc.h"`
`#include "usbapi.h"`

Include dependency graph for usbhw_lpc.c:

## Defines

#define | DEBUG_LED_INIT(x)
#define | DEBUG_LED_ON(x)
#define | DEBUG_LED_OFF(x)
#define | EP2IDX(bEP) ((((bEP)&0xF)<<1)|(((bEP)&0x80)>>7))
#define | IDX2EP(idx) ((((idx)<<7)&0x80)|(((idx)>>1)&0xF))

## Functions

void | USBHwEPConfig (U8 bEP, U16 wMaxPacketSize)
void | USBHwRegisterEPIntHandler (U8 bEP, TFnEPIntHandler *pfnHandler)
void | USBHwRegisterDevIntHandler (TFnDevIntHandler *pfnHandler)
void | USBHwRegisterFrameHandler (TFnFrameHandler *pfnHandler)
void | USBHwSetAddress (U8 bAddr)
void | USBHwConnect (BOOL fConnect)
void | USBHwNakIntEnable (U8 bIntBits)
U8 | USBHwEPGetStatus (U8 bEP)
void | USBHwEPStall (U8 bEP, BOOL fStall)
int | USBHwEPWrite (U8 bEP, U8 *pbBuf, int iLen)
int | USBHwEPRead (U8 bEP, U8 *pbBuf, int iMaxLen)
void | USBHwConfigDevice (BOOL fConfigured)
void | USBHwISR (void)
BOOL | USBHwInit (void)

## Detailed Description

USB hardware layer

## Define Documentation

#define DEBUG_LED_INIT | ( | x |  | ) |

LED initialisation macro

#define DEBUG_LED_OFF | ( | x |  | ) |

turn LED off

#define DEBUG_LED_ON | ( | x |  | ) |

turn LED on

#define EP2IDX | ( | bEP |  | ) | ((((bEP)&0xF)<<1)|(((bEP)&0x80)>>7))

convert from endpoint address to endpoint index

#define IDX2EP | ( | idx |  | ) | ((((idx)<<7)&0x80)|(((idx)>>1)&0xF))

convert from endpoint index to endpoint address

## Function Documentation

void USBHwConfigDevice | ( | BOOL | fConfigured | ) |

Sets the 'configured' state.

All registered endpoints are 'realised' and enabled, and the 'configured' bit is set in the device status register.

Parameters:

  `[in]` | fConfigured | If TRUE, configure device, else unconfigure

void USBHwConnect | ( | BOOL | fConnect | ) |

Connects or disconnects from the USB bus

Parameters:

  `[in]` | fConnect | If TRUE, connect, otherwise disconnect

void USBHwEPConfig | ( | U8 | bEP,
|  | U16 | wMaxPacketSize |
| ) |  |  |

Configures an endpoint and enables it

Parameters:

  `[in]` | bEP | Endpoint number
  `[in]` | wMaxPacketSize | Maximum packet size for this EP

U8 USBHwEPGetStatus | ( | U8 | bEP | ) |

Gets the status from a specific endpoint.

Parameters:

  `[in]` | bEP | Endpoint number

Returns:
  Endpoint status byte (containing EP_STATUS_xxx bits)

int USBHwEPRead | ( | U8 | bEP,
|  | U8 * | pbBuf,
|  | int | iMaxLen |
| ) |  |  |

Reads data from an endpoint buffer

Parameters:

  `[in]` | bEP | Endpoint number
  `[in]` | pbBuf | Endpoint data
  `[in]` | iMaxLen | Maximum number of bytes to read

Returns:
  the number of bytes available in the EP (possibly more than iMaxLen), or <0 in case of error.

void USBHwEPStall | ( | U8 | bEP,
|  | BOOL | fStall |
| ) |  |  |

Sets the stalled property of an endpoint

Parameters:

  `[in]` | bEP | Endpoint number
  `[in]` | fStall | TRUE to stall, FALSE to unstall

int USBHwEPWrite | ( | U8 | bEP,
|  | U8 * | pbBuf,
|  | int | iLen |
| ) |  |  |

Writes data to an endpoint buffer

Parameters:

  `[in]` | bEP | Endpoint number
  `[in]` | pbBuf | Endpoint data
  `[in]` | iLen | Number of bytes to write

Returns:
  TRUE if the data was successfully written or <0 in case of error.

BOOL USBHwInit | ( | void |  | ) |

Initialises the USB hardware

This function assumes that the hardware is connected as shown in section 10.1 of the LPC2148 data sheet: P0.31 controls a switch to connect a 1.5k pull-up to D+ if low. P0.23 is connected to USB VCC.

Embedded artists board: make sure to disconnect P0.23 LED as it acts as a pull-up and so prevents detection of USB disconnect.

Returns:
  TRUE if the hardware was successfully initialised

void USBHwISR | ( | void |  | ) |

USB interrupt handler

Todo:
  Get all 11 bits of frame number instead of just 8

Endpoint interrupts are mapped to the slow interrupt

void USBHwNakIntEnable | ( | U8 | bIntBits | ) |

Enables interrupt on NAK condition

For IN endpoints a NAK is generated when the host wants to read data from the device, but none is available in the endpoint buffer. For OUT endpoints a NAK is generated when the host wants to write data to the device, but the endpoint buffer is still full.

The endpoint interrupt handlers can distinguish regular (ACK) interrupts from NAK interrupt by checking the bits in their bEPStatus argument.

Parameters:

  `[in]` | bIntBits | Bitmap indicating which NAK interrupts to enable

void USBHwRegisterDevIntHandler | ( | TFnDevIntHandler * | pfnHandler | ) |

Registers an device status callback

Parameters:

  `[in]` | pfnHandler | Callback function

void USBHwRegisterEPIntHandler | ( | U8 | bEP,
|  | TFnEPIntHandler * | pfnHandler |
| ) |  |  |

Registers an endpoint event callback

Parameters:

  `[in]` | bEP | Endpoint number
  `[in]` | pfnHandler | Callback function

void USBHwRegisterFrameHandler | ( | TFnFrameHandler * | pfnHandler | ) |

Registers the frame callback

Parameters:

  `[in]` | pfnHandler | Callback function

void USBHwSetAddress | ( | U8 | bAddr | ) |

Sets the USB address.

Parameters:

  `[in]` | bAddr | Device address to set