This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Revision:
1:995212d326ca
Parent:
0:f30e2135b0db
V0.0.0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:f30e2135b0db 1
Yann 0:f30e2135b0db 2 /*
Yann 0:f30e2135b0db 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
Yann 0:f30e2135b0db 4
Yann 0:f30e2135b0db 5 Permission is hereby granted, free of charge, to any person obtaining a copy
Yann 0:f30e2135b0db 6 of this software and associated documentation files (the "Software"), to deal
Yann 0:f30e2135b0db 7 in the Software without restriction, including without limitation the rights
Yann 0:f30e2135b0db 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Yann 0:f30e2135b0db 9 copies of the Software, and to permit persons to whom the Software is
Yann 0:f30e2135b0db 10 furnished to do so, subject to the following conditions:
Yann 0:f30e2135b0db 11
Yann 0:f30e2135b0db 12 The above copyright notice and this permission notice shall be included in
Yann 0:f30e2135b0db 13 all copies or substantial portions of the Software.
Yann 0:f30e2135b0db 14
Yann 0:f30e2135b0db 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Yann 0:f30e2135b0db 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Yann 0:f30e2135b0db 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Yann 0:f30e2135b0db 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Yann 0:f30e2135b0db 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Yann 0:f30e2135b0db 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Yann 0:f30e2135b0db 21 THE SOFTWARE.
Yann 0:f30e2135b0db 22 */
Yann 0:f30e2135b0db 23
Yann 0:f30e2135b0db 24 #include "UsbDevice.h"
Yann 0:f30e2135b0db 25
Yann 0:f30e2135b0db 26 #include "netCfg.h"
Yann 0:f30e2135b0db 27 #if NET_USB
Yann 0:f30e2135b0db 28
Yann 0:f30e2135b0db 29 //#define __DEBUG
Yann 0:f30e2135b0db 30 #include "dbg/dbg.h"
Yann 0:f30e2135b0db 31
Yann 0:f30e2135b0db 32 UsbDevice::UsbDevice( UsbHostMgr* pMgr, int hub, int port, int addr ) : m_pControlEp(NULL), /*m_controlEp( this, 0x00, false, USB_CONTROL, 8 ),*/
Yann 0:f30e2135b0db 33 m_pMgr(pMgr), m_connected(false), m_enumerated(false), m_hub(hub), m_port(port), m_addr(addr), m_refs(0),
Yann 0:f30e2135b0db 34 m_vid(0), m_pid(0)
Yann 0:f30e2135b0db 35 {
Yann 0:f30e2135b0db 36
Yann 0:f30e2135b0db 37 }
Yann 0:f30e2135b0db 38
Yann 0:f30e2135b0db 39 UsbDevice::~UsbDevice()
Yann 0:f30e2135b0db 40 {
Yann 0:f30e2135b0db 41 if(m_pControlEp)
Yann 0:f30e2135b0db 42 delete m_pControlEp;
Yann 0:f30e2135b0db 43 }
Yann 0:f30e2135b0db 44
Yann 0:f30e2135b0db 45 UsbErr UsbDevice::enumerate()
Yann 0:f30e2135b0db 46 {
Yann 0:f30e2135b0db 47 // USB_INT32S rc;
Yann 0:f30e2135b0db 48
Yann 0:f30e2135b0db 49 UsbErr rc;
Yann 0:f30e2135b0db 50
Yann 0:f30e2135b0db 51 DBG("Starting enumeration (m_pMgr = %p)\n", m_pMgr);
Yann 0:f30e2135b0db 52
Yann 0:f30e2135b0db 53 #if 1
Yann 0:f30e2135b0db 54 m_pMgr->resetPort(m_hub, m_port);
Yann 0:f30e2135b0db 55 #else
Yann 0:f30e2135b0db 56 wait_ms(100); /* USB 2.0 spec says atleast 50ms delay beore port reset */
Yann 0:f30e2135b0db 57 LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRS; // Initiate port reset
Yann 0:f30e2135b0db 58 while (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRS)
Yann 0:f30e2135b0db 59 __WFI(); // Wait for port reset to complete...
Yann 0:f30e2135b0db 60 LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; // ...and clear port reset signal
Yann 0:f30e2135b0db 61 wait_ms(200); /* Wait for 100 MS after port reset */
Yann 0:f30e2135b0db 62 #endif
Yann 0:f30e2135b0db 63
Yann 0:f30e2135b0db 64 DBG("Port reset\n");
Yann 0:f30e2135b0db 65
Yann 0:f30e2135b0db 66 wait_ms(200);
Yann 0:f30e2135b0db 67
Yann 0:f30e2135b0db 68 m_pControlEp = new UsbEndpoint( this, 0x00, false, USB_CONTROL, 8, 0 );
Yann 0:f30e2135b0db 69
Yann 0:f30e2135b0db 70 //EDCtrl->Control = 8 << 16;/* Put max pkt size = 8 */
Yann 0:f30e2135b0db 71 /* Read first 8 bytes of device desc */
Yann 0:f30e2135b0db 72 rc = controlReceive(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, (USB_DESCRIPTOR_TYPE_DEVICE << 8)|(0), 0, m_controlDataBuf, 8);
Yann 0:f30e2135b0db 73 if (rc)
Yann 0:f30e2135b0db 74 {
Yann 0:f30e2135b0db 75 DBG("RC=%d",rc);
Yann 0:f30e2135b0db 76 return (rc);
Yann 0:f30e2135b0db 77 }
Yann 0:f30e2135b0db 78
Yann 0:f30e2135b0db 79 DBG("Got descriptor, max ep size is %d\n", m_controlDataBuf[7]);
Yann 0:f30e2135b0db 80
Yann 0:f30e2135b0db 81 m_pControlEp->updateSize(m_controlDataBuf[7]); /* Get max pkt size of endpoint 0 */
Yann 0:f30e2135b0db 82 rc = controlSend(USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, SET_ADDRESS, m_addr, 0, NULL, 0); /* Set the device address to m_addr */
Yann 0:f30e2135b0db 83 if (rc)
Yann 0:f30e2135b0db 84 {
Yann 0:f30e2135b0db 85 // PRINT_Err(rc);
Yann 0:f30e2135b0db 86 return (rc);
Yann 0:f30e2135b0db 87 }
Yann 0:f30e2135b0db 88 wait_ms(2);
Yann 0:f30e2135b0db 89 //EDCtrl->Control = (EDCtrl->Control) | 1; /* Modify control pipe with address 1 */
Yann 0:f30e2135b0db 90
Yann 0:f30e2135b0db 91 //Update address
Yann 0:f30e2135b0db 92 m_pControlEp->updateAddr(m_addr);
Yann 0:f30e2135b0db 93 DBG("Ep addr is now %d", m_addr);
Yann 0:f30e2135b0db 94 /**/
Yann 0:f30e2135b0db 95
Yann 0:f30e2135b0db 96 //rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_DEVICE, 0, TDBuffer, 17); //Read full device descriptor
Yann 0:f30e2135b0db 97 rc = controlReceive(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, (USB_DESCRIPTOR_TYPE_DEVICE << 8)|(0), 0, m_controlDataBuf, 17);
Yann 0:f30e2135b0db 98 if (rc)
Yann 0:f30e2135b0db 99 {
Yann 0:f30e2135b0db 100 //PRINT_Err(rc);
Yann 0:f30e2135b0db 101 return (rc);
Yann 0:f30e2135b0db 102 }
Yann 0:f30e2135b0db 103 /*
Yann 0:f30e2135b0db 104 rc = SerialCheckVidPid();
Yann 0:f30e2135b0db 105 if (rc != OK) {
Yann 0:f30e2135b0db 106 PRINT_Err(rc);
Yann 0:f30e2135b0db 107 return (rc);
Yann 0:f30e2135b0db 108 }
Yann 0:f30e2135b0db 109 */
Yann 0:f30e2135b0db 110 /**/
Yann 0:f30e2135b0db 111
Yann 0:f30e2135b0db 112 m_vid = *((uint16_t*)&m_controlDataBuf[8]);
Yann 0:f30e2135b0db 113 m_pid = *((uint16_t*)&m_controlDataBuf[10]);
Yann 0:f30e2135b0db 114
Yann 0:f30e2135b0db 115 DBG("VID: %02x, PID: %02x\n", m_vid, m_pid);
Yann 0:f30e2135b0db 116 /* Get the configuration descriptor */
Yann 0:f30e2135b0db 117 //rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, 9);
Yann 0:f30e2135b0db 118 rc = controlReceive(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, (USB_DESCRIPTOR_TYPE_CONFIGURATION << 8)|(0), 0, m_controlDataBuf, 9);
Yann 0:f30e2135b0db 119 if (rc)
Yann 0:f30e2135b0db 120 {
Yann 0:f30e2135b0db 121 //PRINT_Err(rc);
Yann 0:f30e2135b0db 122 return (rc);
Yann 0:f30e2135b0db 123 }
Yann 0:f30e2135b0db 124 /* Get the first configuration data */
Yann 0:f30e2135b0db 125 //rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, *((uint16_t*)&TDBuffer[2]));
Yann 0:f30e2135b0db 126 rc = controlReceive(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, (USB_DESCRIPTOR_TYPE_CONFIGURATION << 8)|(0), 0, m_controlDataBuf, *((uint16_t*)&m_controlDataBuf[2]));
Yann 0:f30e2135b0db 127 if (rc)
Yann 0:f30e2135b0db 128 {
Yann 0:f30e2135b0db 129 //PRINT_Err(rc);
Yann 0:f30e2135b0db 130 return (rc);
Yann 0:f30e2135b0db 131 }
Yann 0:f30e2135b0db 132
Yann 0:f30e2135b0db 133 DBG("Desc len is %d\n", *((uint16_t*)&m_controlDataBuf[2]));
Yann 0:f30e2135b0db 134
Yann 0:f30e2135b0db 135 DBG("Set configuration\n");
Yann 0:f30e2135b0db 136
Yann 0:f30e2135b0db 137 //rc = USBH_SET_CONFIGURATION(1);/* Select device configuration 1 */
Yann 0:f30e2135b0db 138 rc = controlSend(USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, SET_CONFIGURATION, 1, 0, NULL, 0);
Yann 0:f30e2135b0db 139 if (rc)
Yann 0:f30e2135b0db 140 {
Yann 0:f30e2135b0db 141 // PRINT_Err(rc);
Yann 0:f30e2135b0db 142 return rc;
Yann 0:f30e2135b0db 143 }
Yann 0:f30e2135b0db 144 wait_ms(100);/* Some devices may require this delay */
Yann 0:f30e2135b0db 145
Yann 0:f30e2135b0db 146 m_enumerated = true;
Yann 0:f30e2135b0db 147 return USBERR_OK;
Yann 0:f30e2135b0db 148 }
Yann 0:f30e2135b0db 149
Yann 0:f30e2135b0db 150 bool UsbDevice::connected()
Yann 0:f30e2135b0db 151 {
Yann 0:f30e2135b0db 152 return m_connected;
Yann 0:f30e2135b0db 153 }
Yann 0:f30e2135b0db 154
Yann 0:f30e2135b0db 155 bool UsbDevice::enumerated()
Yann 0:f30e2135b0db 156 {
Yann 0:f30e2135b0db 157 return m_enumerated;
Yann 0:f30e2135b0db 158 }
Yann 0:f30e2135b0db 159
Yann 0:f30e2135b0db 160 int UsbDevice::getPid()
Yann 0:f30e2135b0db 161 {
Yann 0:f30e2135b0db 162 return m_pid;
Yann 0:f30e2135b0db 163 }
Yann 0:f30e2135b0db 164
Yann 0:f30e2135b0db 165 int UsbDevice::getVid()
Yann 0:f30e2135b0db 166 {
Yann 0:f30e2135b0db 167 return m_vid;
Yann 0:f30e2135b0db 168 }
Yann 0:f30e2135b0db 169
Yann 0:f30e2135b0db 170 UsbErr UsbDevice::getConfigurationDescriptor(int config, uint8_t** pBuf)
Yann 0:f30e2135b0db 171 {
Yann 0:f30e2135b0db 172 //For now olny one config
Yann 0:f30e2135b0db 173 *pBuf = m_controlDataBuf;
Yann 0:f30e2135b0db 174 return USBERR_OK;
Yann 0:f30e2135b0db 175 }
Yann 0:f30e2135b0db 176
Yann 0:f30e2135b0db 177 UsbErr UsbDevice::getInterfaceDescriptor(int config, int item, uint8_t** pBuf)
Yann 0:f30e2135b0db 178 {
Yann 0:f30e2135b0db 179 byte* desc_ptr = m_controlDataBuf;
Yann 0:f30e2135b0db 180
Yann 0:f30e2135b0db 181 /* if (desc_ptr[1] != USB_DESCRIPTOR_TYPE_CONFIGURATION)
Yann 0:f30e2135b0db 182 {
Yann 0:f30e2135b0db 183 return USBERR_BADCONFIG;
Yann 0:f30e2135b0db 184 }*/
Yann 0:f30e2135b0db 185
Yann 0:f30e2135b0db 186 if(item>=m_controlDataBuf[4])//Interfaces count
Yann 0:f30e2135b0db 187 return USBERR_NOTFOUND;
Yann 0:f30e2135b0db 188
Yann 0:f30e2135b0db 189 desc_ptr += desc_ptr[0];
Yann 0:f30e2135b0db 190
Yann 0:f30e2135b0db 191 *pBuf = NULL;
Yann 0:f30e2135b0db 192
Yann 0:f30e2135b0db 193 while (desc_ptr < m_controlDataBuf + *((uint16_t*)&m_controlDataBuf[2]))
Yann 0:f30e2135b0db 194 {
Yann 0:f30e2135b0db 195
Yann 0:f30e2135b0db 196 switch (desc_ptr[1]) {
Yann 0:f30e2135b0db 197 case USB_DESCRIPTOR_TYPE_INTERFACE:
Yann 0:f30e2135b0db 198 if(desc_ptr[2] == item)
Yann 0:f30e2135b0db 199 {
Yann 0:f30e2135b0db 200 *pBuf = desc_ptr;
Yann 0:f30e2135b0db 201 return USBERR_OK;
Yann 0:f30e2135b0db 202 }
Yann 0:f30e2135b0db 203 desc_ptr += desc_ptr[0]; // Move to next descriptor start
Yann 0:f30e2135b0db 204 break;
Yann 0:f30e2135b0db 205 }
Yann 0:f30e2135b0db 206
Yann 0:f30e2135b0db 207 }
Yann 0:f30e2135b0db 208
Yann 0:f30e2135b0db 209 if(*pBuf == NULL)
Yann 0:f30e2135b0db 210 return USBERR_NOTFOUND;
Yann 0:f30e2135b0db 211
Yann 0:f30e2135b0db 212 return USBERR_OK;
Yann 0:f30e2135b0db 213 }
Yann 0:f30e2135b0db 214
Yann 0:f30e2135b0db 215
Yann 0:f30e2135b0db 216 UsbErr UsbDevice::setConfiguration(int config)
Yann 0:f30e2135b0db 217 {
Yann 0:f30e2135b0db 218 return USBERR_OK;
Yann 0:f30e2135b0db 219 }
Yann 0:f30e2135b0db 220
Yann 0:f30e2135b0db 221 UsbErr UsbDevice::controlSend(byte requestType, byte request, word value, word index, const byte* buf, int len)
Yann 0:f30e2135b0db 222 {
Yann 0:f30e2135b0db 223 UsbErr rc;
Yann 0:f30e2135b0db 224 fillControlBuf(requestType, request, value, index, len);
Yann 0:f30e2135b0db 225 m_pControlEp->setNextToken(TD_SETUP);
Yann 0:f30e2135b0db 226 rc = m_pControlEp->transfer(m_controlBuf, 8);
Yann 0:f30e2135b0db 227 while(m_pControlEp->status() == USBERR_PROCESSING);
Yann 0:f30e2135b0db 228 rc = (UsbErr) MIN(0, m_pControlEp->status());
Yann 0:f30e2135b0db 229 if(rc)
Yann 0:f30e2135b0db 230 return rc;
Yann 0:f30e2135b0db 231 if(len)
Yann 0:f30e2135b0db 232 {
Yann 0:f30e2135b0db 233 m_pControlEp->setNextToken(TD_OUT);
Yann 0:f30e2135b0db 234 rc = m_pControlEp->transfer((byte*)buf, len);
Yann 0:f30e2135b0db 235 while(m_pControlEp->status() == USBERR_PROCESSING);
Yann 0:f30e2135b0db 236 rc = (UsbErr) MIN(0, m_pControlEp->status());
Yann 0:f30e2135b0db 237 if(rc)
Yann 0:f30e2135b0db 238 return rc;
Yann 0:f30e2135b0db 239 }
Yann 0:f30e2135b0db 240 m_pControlEp->setNextToken(TD_IN);
Yann 0:f30e2135b0db 241 rc = m_pControlEp->transfer(NULL, 0);
Yann 0:f30e2135b0db 242 while(m_pControlEp->status() == USBERR_PROCESSING);
Yann 0:f30e2135b0db 243 rc = (UsbErr) MIN(0, m_pControlEp->status());
Yann 0:f30e2135b0db 244 if(rc)
Yann 0:f30e2135b0db 245 return rc;
Yann 0:f30e2135b0db 246 return USBERR_OK;
Yann 0:f30e2135b0db 247 }
Yann 0:f30e2135b0db 248
Yann 0:f30e2135b0db 249 UsbErr UsbDevice::controlReceive(byte requestType, byte request, word value, word index, const byte* buf, int len)
Yann 0:f30e2135b0db 250 {
Yann 0:f30e2135b0db 251 UsbErr rc;
Yann 0:f30e2135b0db 252 fillControlBuf(requestType, request, value, index, len);
Yann 0:f30e2135b0db 253 m_pControlEp->setNextToken(TD_SETUP);
Yann 0:f30e2135b0db 254 rc = m_pControlEp->transfer(m_controlBuf, 8);
Yann 0:f30e2135b0db 255 while(m_pControlEp->status() == USBERR_PROCESSING);
Yann 0:f30e2135b0db 256 rc = (UsbErr) MIN(0, m_pControlEp->status());
Yann 0:f30e2135b0db 257 if(rc)
Yann 0:f30e2135b0db 258 return rc;
Yann 0:f30e2135b0db 259 if(len)
Yann 0:f30e2135b0db 260 {
Yann 0:f30e2135b0db 261 m_pControlEp->setNextToken(TD_IN);
Yann 0:f30e2135b0db 262 rc = m_pControlEp->transfer( (byte*) buf, len);
Yann 0:f30e2135b0db 263 while(m_pControlEp->status() == USBERR_PROCESSING);
Yann 0:f30e2135b0db 264 rc = (UsbErr) MIN(0, m_pControlEp->status());
Yann 0:f30e2135b0db 265 if(rc)
Yann 0:f30e2135b0db 266 return rc;
Yann 0:f30e2135b0db 267 }
Yann 0:f30e2135b0db 268 m_pControlEp->setNextToken(TD_OUT);
Yann 0:f30e2135b0db 269 rc = m_pControlEp->transfer(NULL, 0);
Yann 0:f30e2135b0db 270 while(m_pControlEp->status() == USBERR_PROCESSING);
Yann 0:f30e2135b0db 271 rc = (UsbErr) MIN(0, m_pControlEp->status());
Yann 0:f30e2135b0db 272 if(rc)
Yann 0:f30e2135b0db 273 return rc;
Yann 0:f30e2135b0db 274 return USBERR_OK;
Yann 0:f30e2135b0db 275 }
Yann 0:f30e2135b0db 276
Yann 0:f30e2135b0db 277 void UsbDevice::fillControlBuf(byte requestType, byte request, word value, word index, int len)
Yann 0:f30e2135b0db 278 {
Yann 0:f30e2135b0db 279 #ifdef __BIG_ENDIAN
Yann 0:f30e2135b0db 280 #error "Must implement BE to LE conv here"
Yann 0:f30e2135b0db 281 #endif
Yann 0:f30e2135b0db 282 m_controlBuf[0] = requestType;
Yann 0:f30e2135b0db 283 m_controlBuf[1] = request;
Yann 0:f30e2135b0db 284 //We are in LE so it's fine
Yann 0:f30e2135b0db 285 *((word*)&m_controlBuf[2]) = value;
Yann 0:f30e2135b0db 286 *((word*)&m_controlBuf[4]) = index;
Yann 0:f30e2135b0db 287 *((word*)&m_controlBuf[6]) = (word) len;
Yann 0:f30e2135b0db 288 }
Yann 0:f30e2135b0db 289
Yann 0:f30e2135b0db 290
Yann 0:f30e2135b0db 291 #endif