| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /****************************************************************************** |
| 2 | |||
| 3 | USB Host Charger Client Driver |
||
| 4 | |||
| 5 | Description: |
||
| 6 | This is the Charger client driver file for a USB Embedded Host device. This |
||
| 7 | driver should be used in a project with usb_host.c to provided the USB |
||
| 8 | hardware interface. |
||
| 9 | |||
| 10 | To interface with USB Embedded Host layer, the routine USBHostChargerInit() |
||
| 11 | should be specified as the Initialize() function, and |
||
| 12 | USBHostChargerEventHandler() should be specified as the EventHandler() |
||
| 13 | function in the usbClientDrvTable[] array declared in usb_config.c. |
||
| 14 | |||
| 15 | This driver can be used in either the event driven or polling mechanism. |
||
| 16 | |||
| 17 | Summary: |
||
| 18 | This is the Charger client driver file for a USB Embedded Host device. |
||
| 19 | |||
| 20 | *******************************************************************************/ |
||
| 21 | //DOM-IGNORE-BEGIN |
||
| 22 | /****************************************************************************** |
||
| 23 | |||
| 24 | FileName: usb_host_charger.c |
||
| 25 | Dependencies: None |
||
| 26 | Processor: PIC24/PIC32MX |
||
| 27 | Compiler: C30, C32 |
||
| 28 | Company: Microchip Technology, Inc. |
||
| 29 | |||
| 30 | Software License Agreement |
||
| 31 | |||
| 32 | The software supplied herewith by Microchip Technology Incorporated |
||
| 33 | (the Company) for its PICmicro® Microcontroller is intended and |
||
| 34 | supplied to you, the Companys customer, for use solely and |
||
| 35 | exclusively on Microchip PICmicro Microcontroller products. The |
||
| 36 | software is owned by the Company and/or its supplier, and is |
||
| 37 | protected under applicable copyright laws. All rights are reserved. |
||
| 38 | Any use in violation of the foregoing restrictions may subject the |
||
| 39 | user to criminal sanctions under applicable laws, as well as to |
||
| 40 | civil liability for the breach of the terms and conditions of this |
||
| 41 | license. |
||
| 42 | |||
| 43 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
| 44 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
| 45 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
| 46 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
| 47 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
| 48 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
| 49 | |||
| 50 | Change History: |
||
| 51 | Rev Description |
||
| 52 | ----- ----------- |
||
| 53 | 2.6-2.7a No change |
||
| 54 | |||
| 55 | *******************************************************************************/ |
||
| 56 | //DOM-IGNORE-END |
||
| 57 | |||
| 58 | #include <stdlib.h> |
||
| 59 | #include <string.h> |
||
| 60 | #include "GenericTypeDefs.h" |
||
| 61 | #include "USB\usb.h" |
||
| 62 | #include "USB\usb_host_charger.h" |
||
| 63 | |||
| 64 | //#define DEBUG_MODE |
||
| 65 | #ifdef DEBUG_MODE |
||
| 66 | #include "uart2.h" |
||
| 67 | #endif |
||
| 68 | |||
| 69 | |||
| 70 | // ***************************************************************************** |
||
| 71 | // ***************************************************************************** |
||
| 72 | // Section: Global Variables |
||
| 73 | // ***************************************************************************** |
||
| 74 | // ***************************************************************************** |
||
| 75 | |||
| 76 | BYTE currentChargingRecord; |
||
| 77 | USB_CHARGING_DEVICE usbChargingDevices[USB_MAX_CHARGING_DEVICES]; |
||
| 78 | |||
| 79 | |||
| 80 | // ***************************************************************************** |
||
| 81 | // ***************************************************************************** |
||
| 82 | // Section: Local Prototypes |
||
| 83 | // ***************************************************************************** |
||
| 84 | // ***************************************************************************** |
||
| 85 | |||
| 86 | static BOOL _USBHostCharger_FindDevice( BYTE address ); |
||
| 87 | |||
| 88 | |||
| 89 | // ***************************************************************************** |
||
| 90 | // ***************************************************************************** |
||
| 91 | // Section: Host Stack Interface Functions |
||
| 92 | // ***************************************************************************** |
||
| 93 | // ***************************************************************************** |
||
| 94 | |||
| 95 | /**************************************************************************** |
||
| 96 | Function: |
||
| 97 | BOOL USBHostChargerInitialize ( BYTE address, DWORD flags ) |
||
| 98 | |||
| 99 | Summary: |
||
| 100 | This function is called by the USB Embedded Host layer when a device |
||
| 101 | attaches for charging. |
||
| 102 | |||
| 103 | Description: |
||
| 104 | This routine is a call out from the USB Embedded Host layer to the USB |
||
| 105 | charger client driver. It is called when a device that is supported for |
||
| 106 | charging only has been connected to the host. Its purpose is to initialize |
||
| 107 | and activate the USB Charger client driver. |
||
| 108 | |||
| 109 | Preconditions: |
||
| 110 | The device has been configured. |
||
| 111 | |||
| 112 | Parameters: |
||
| 113 | BYTE address - Device's address on the bus |
||
| 114 | DWORD flags - Initialization flags |
||
| 115 | |||
| 116 | Return Values: |
||
| 117 | TRUE - Initialization was successful |
||
| 118 | FALSE - Initialization failed |
||
| 119 | |||
| 120 | Remarks: |
||
| 121 | Multiple client drivers may be used in a single application. The USB |
||
| 122 | Embedded Host layer will call the initialize routine required for the |
||
| 123 | attached device. |
||
| 124 | ***************************************************************************/ |
||
| 125 | |||
| 126 | BOOL USBHostChargerInitialize( BYTE address, DWORD flags ) |
||
| 127 | { |
||
| 128 | BYTE *pDesc; |
||
| 129 | |||
| 130 | // Find a new entry |
||
| 131 | for (currentChargingRecord=0; currentChargingRecord<USB_MAX_CHARGING_DEVICES; currentChargingRecord++) |
||
| 132 | { |
||
| 133 | if (!usbChargingDevices[currentChargingRecord].flags.inUse) break; |
||
| 134 | } |
||
| 135 | if (currentChargingRecord == USB_MAX_CHARGING_DEVICES) |
||
| 136 | { |
||
| 137 | #ifdef DEBUG_MODE |
||
| 138 | UART2PrintString( "CHG: No more space\r\n" ); |
||
| 139 | #endif |
||
| 140 | return FALSE; // We have no more room for a new device. |
||
| 141 | } |
||
| 142 | |||
| 143 | // Initialize state - set the inUse flag. |
||
| 144 | usbChargingDevices[currentChargingRecord].flags.val = 1; |
||
| 145 | |||
| 146 | // Save device the address, VID, & PID |
||
| 147 | usbChargingDevices[currentChargingRecord].ID.deviceAddress = address; |
||
| 148 | pDesc = USBHostGetDeviceDescriptor(address); |
||
| 149 | pDesc += 8; |
||
| 150 | usbChargingDevices[currentChargingRecord].ID.vid = (WORD)*pDesc; pDesc++; |
||
| 151 | usbChargingDevices[currentChargingRecord].ID.vid |= ((WORD)*pDesc) << 8; pDesc++; |
||
| 152 | usbChargingDevices[currentChargingRecord].ID.pid = (WORD)*pDesc; pDesc++; |
||
| 153 | usbChargingDevices[currentChargingRecord].ID.pid |= ((WORD)*pDesc) << 8; pDesc++; |
||
| 154 | |||
| 155 | #ifdef DEBUG_MODE |
||
| 156 | UART2PrintString( "CHG: USB Charging Client Initalized: flags=0x" ); |
||
| 157 | UART2PutHex( flags ); |
||
| 158 | UART2PrintString( " address=" ); |
||
| 159 | UART2PutDec( address ); |
||
| 160 | UART2PrintString( " VID=0x" ); |
||
| 161 | UART2PutHex( usbChargingDevices[currentChargingRecord].ID.vid >> 8 ); |
||
| 162 | UART2PutHex( usbChargingDevices[currentChargingRecord].ID.vid & 0xFF ); |
||
| 163 | UART2PrintString( " PID=0x" ); |
||
| 164 | UART2PutHex( usbChargingDevices[currentChargingRecord].ID.pid >> 8 ); |
||
| 165 | UART2PutHex( usbChargingDevices[currentChargingRecord].ID.pid & 0xFF ); |
||
| 166 | UART2PrintString( "\r\n" ); |
||
| 167 | #endif |
||
| 168 | |||
| 169 | // Notify that application that we've been attached to a device. |
||
| 170 | USB_HOST_APP_EVENT_HANDLER(address, EVENT_CHARGER_ATTACH, |
||
| 171 | &(usbChargingDevices[currentChargingRecord].ID), sizeof(USB_CHARGING_DEVICE_ID) ); |
||
| 172 | |||
| 173 | return TRUE; |
||
| 174 | |||
| 175 | } // USBHostChargerInit |
||
| 176 | |||
| 177 | |||
| 178 | /**************************************************************************** |
||
| 179 | Function: |
||
| 180 | BOOL USBHostChargerEventHandler ( BYTE address, USB_EVENT event, |
||
| 181 | void *data, DWORD size ) |
||
| 182 | |||
| 183 | Summary: |
||
| 184 | This routine is called by the Host layer to notify the charger client of |
||
| 185 | events that occur. |
||
| 186 | |||
| 187 | Description: |
||
| 188 | This routine is called by the Host layer to notify the charger client of |
||
| 189 | events that occur. If the event is recognized, it is handled and the |
||
| 190 | routine returns TRUE. Otherwise, it is ignored and the routine returns |
||
| 191 | FALSE. |
||
| 192 | |||
| 193 | Preconditions: |
||
| 194 | None |
||
| 195 | |||
| 196 | Parameters: |
||
| 197 | BYTE address - Address of device with the event |
||
| 198 | USB_EVENT event - The bus event that occured |
||
| 199 | void *data - Pointer to event-specific data |
||
| 200 | DWORD size - Size of the event-specific data |
||
| 201 | |||
| 202 | Return Values: |
||
| 203 | TRUE - The event was handled |
||
| 204 | FALSE - The event was not handled |
||
| 205 | |||
| 206 | Remarks: |
||
| 207 | None |
||
| 208 | ***************************************************************************/ |
||
| 209 | |||
| 210 | BOOL USBHostChargerEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size ) |
||
| 211 | { |
||
| 212 | // Make sure the event is for a connected device |
||
| 213 | if (!_USBHostCharger_FindDevice( address )) |
||
| 214 | { |
||
| 215 | return FALSE; |
||
| 216 | } |
||
| 217 | |||
| 218 | // Handle specific events. |
||
| 219 | switch (event) |
||
| 220 | { |
||
| 221 | case EVENT_DETACH: |
||
| 222 | // Notify that application that the device has been detached. |
||
| 223 | USB_HOST_APP_EVENT_HANDLER(usbChargingDevices[currentChargingRecord].ID.deviceAddress, EVENT_CHARGER_DETACH, &usbChargingDevices[currentChargingRecord].ID.deviceAddress, sizeof(BYTE) ); |
||
| 224 | usbChargingDevices[currentChargingRecord].flags.val = 0; |
||
| 225 | usbChargingDevices[currentChargingRecord].ID.deviceAddress = 0; |
||
| 226 | #ifdef DEBUG_MODE |
||
| 227 | UART2PrintString( "USB Charging Client Device Detached: address=" ); |
||
| 228 | UART2PutDec( address ); |
||
| 229 | UART2PrintString( "\r\n" ); |
||
| 230 | #endif |
||
| 231 | return TRUE; |
||
| 232 | break; |
||
| 233 | |||
| 234 | case EVENT_SUSPEND: |
||
| 235 | case EVENT_RESUME: |
||
| 236 | case EVENT_BUS_ERROR: |
||
| 237 | default: |
||
| 238 | break; |
||
| 239 | } |
||
| 240 | |||
| 241 | return FALSE; |
||
| 242 | } // USBHostChargerEventHandler |
||
| 243 | |||
| 244 | |||
| 245 | // ***************************************************************************** |
||
| 246 | // ***************************************************************************** |
||
| 247 | // Section: Application Callable Functions |
||
| 248 | // ***************************************************************************** |
||
| 249 | // ***************************************************************************** |
||
| 250 | |||
| 251 | /**************************************************************************** |
||
| 252 | Function: |
||
| 253 | BOOL USBHostChargerDeviceDetached( BYTE deviceAddress ) |
||
| 254 | |||
| 255 | Description: |
||
| 256 | This interface is used to check if the device has been detached from the |
||
| 257 | bus. |
||
| 258 | |||
| 259 | Preconditions: |
||
| 260 | None |
||
| 261 | |||
| 262 | Parameters: |
||
| 263 | deviceAddress - USB Address of the device. |
||
| 264 | |||
| 265 | Return Values: |
||
| 266 | TRUE - The device has been detached, or an invalid deviceAddress is given. |
||
| 267 | FALSE - The device is attached |
||
| 268 | |||
| 269 | Example: |
||
| 270 | <code> |
||
| 271 | if (USBHostChargerDeviceDetached( deviceAddress )) |
||
| 272 | { |
||
| 273 | // Handle detach |
||
| 274 | } |
||
| 275 | </code> |
||
| 276 | |||
| 277 | Remarks: |
||
| 278 | None |
||
| 279 | ***************************************************************************/ |
||
| 280 | |||
| 281 | BOOL USBHostChargerDeviceDetached( BYTE deviceAddress ) |
||
| 282 | { |
||
| 283 | return !_USBHostCharger_FindDevice( deviceAddress ); |
||
| 284 | } |
||
| 285 | |||
| 286 | |||
| 287 | /**************************************************************************** |
||
| 288 | Function: |
||
| 289 | BOOL USBHostChargerGetDeviceAddress(USB_CHARGING_DEVICE_ID *pDevID) |
||
| 290 | |||
| 291 | Description: |
||
| 292 | This interface is used get the address of a specific generic device on |
||
| 293 | the USB. |
||
| 294 | |||
| 295 | Preconditions: |
||
| 296 | The device must be connected and enumerated. |
||
| 297 | |||
| 298 | Parameters: |
||
| 299 | pDevID - Pointer to a structure containing the Device ID Info (VID, |
||
| 300 | PID, and device address). |
||
| 301 | |||
| 302 | Return Values: |
||
| 303 | TRUE - The device is connected |
||
| 304 | FALSE - The device is not connected. |
||
| 305 | |||
| 306 | Example: |
||
| 307 | <code> |
||
| 308 | USB_CHARGING_DEVICE_ID deviceID; |
||
| 309 | BYTE deviceAddress; |
||
| 310 | |||
| 311 | deviceID.vid = 0x1234; |
||
| 312 | deviceID.pid = 0x5678; |
||
| 313 | |||
| 314 | if (USBHostChargerGetDeviceAddress(&deviceID)) |
||
| 315 | { |
||
| 316 | deviceAddress = deviceID.deviceAddress; |
||
| 317 | } |
||
| 318 | </code> |
||
| 319 | |||
| 320 | Remarks: |
||
| 321 | None |
||
| 322 | ***************************************************************************/ |
||
| 323 | |||
| 324 | BOOL USBHostChargerGetDeviceAddress(USB_CHARGING_DEVICE_ID *pDevID) |
||
| 325 | { |
||
| 326 | if (pDevID == NULL) |
||
| 327 | { |
||
| 328 | return FALSE; |
||
| 329 | } |
||
| 330 | |||
| 331 | for (currentChargingRecord=0; currentChargingRecord<USB_MAX_CHARGING_DEVICES; currentChargingRecord++) |
||
| 332 | { |
||
| 333 | if (usbChargingDevices[currentChargingRecord].flags.inUse && |
||
| 334 | (usbChargingDevices[currentChargingRecord].ID.vid == pDevID->vid) && |
||
| 335 | (usbChargingDevices[currentChargingRecord].ID.pid == pDevID->pid)) |
||
| 336 | { |
||
| 337 | pDevID->deviceAddress = usbChargingDevices[currentChargingRecord].ID.deviceAddress; |
||
| 338 | return TRUE; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | return FALSE; // The device was not found. |
||
| 343 | |||
| 344 | } // USBHostChargingGetDeviceAddress |
||
| 345 | |||
| 346 | |||
| 347 | // ***************************************************************************** |
||
| 348 | // ***************************************************************************** |
||
| 349 | // Section: Internal Functions |
||
| 350 | // ***************************************************************************** |
||
| 351 | // ***************************************************************************** |
||
| 352 | |||
| 353 | /**************************************************************************** |
||
| 354 | Function: |
||
| 355 | static BOOL _USBHostCharger_FindDevice( BYTE address ) |
||
| 356 | |||
| 357 | Description: |
||
| 358 | This function looks in the array of charging devices to see if there is |
||
| 359 | currently a device attached at the indicated device. If so, it returns |
||
| 360 | TRUE, and the global variable currentChargingRecord is changed to the |
||
| 361 | index of the device. If not, it returns FALSE. |
||
| 362 | |||
| 363 | Preconditions: |
||
| 364 | None |
||
| 365 | |||
| 366 | Parameters: |
||
| 367 | deviceAddress - USB Address of the device. |
||
| 368 | |||
| 369 | Return Values: |
||
| 370 | TRUE - The indicated device is attached |
||
| 371 | FALSE - The indicated device is not attached |
||
| 372 | |||
| 373 | Remarks: |
||
| 374 | None |
||
| 375 | ***************************************************************************/ |
||
| 376 | |||
| 377 | BOOL _USBHostCharger_FindDevice( BYTE address ) |
||
| 378 | { |
||
| 379 | for (currentChargingRecord=0; currentChargingRecord<USB_MAX_CHARGING_DEVICES; currentChargingRecord++) |
||
| 380 | { |
||
| 381 | if (usbChargingDevices[currentChargingRecord].flags.inUse && |
||
| 382 | (usbChargingDevices[currentChargingRecord].ID.deviceAddress == address)) |
||
| 383 | { |
||
| 384 | return TRUE; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | return FALSE; // The device was not found. |
||
| 388 | } |
||
| 389 | |||
| 390 | |||
| 391 | /************************************************************************* |
||
| 392 | * EOF |
||
| 393 | */ |
||
| 394 | |||
| 395 |
Powered by WebSVN v2.8.3