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 #ifndef USB_SERIAL_H
Yann 0:f30e2135b0db 25 #define USB_SERIAL_H
Yann 0:f30e2135b0db 26
Yann 0:f30e2135b0db 27 //DG 2010
Yann 0:f30e2135b0db 28 //Essentially a clone of Serial if
Yann 0:f30e2135b0db 29 #include "Stream.h"
Yann 0:f30e2135b0db 30 #include "mbed.h"
Yann 0:f30e2135b0db 31
Yann 0:f30e2135b0db 32 #include "drv/usb/UsbDevice.h"
Yann 0:f30e2135b0db 33 #include "drv/usb/UsbEndpoint.h"
Yann 0:f30e2135b0db 34
Yann 0:f30e2135b0db 35 namespace mbed {
Yann 0:f30e2135b0db 36
Yann 0:f30e2135b0db 37 class UsbSerial : public Stream {
Yann 0:f30e2135b0db 38
Yann 0:f30e2135b0db 39 public:
Yann 0:f30e2135b0db 40
Yann 0:f30e2135b0db 41 UsbSerial(UsbDevice* pDevice, int epIn, int epOut, const char* name = NULL);
Yann 0:f30e2135b0db 42 virtual ~UsbSerial();
Yann 0:f30e2135b0db 43 //Apart from the ctor/dtor, exactly the same protos as Serial
Yann 0:f30e2135b0db 44
Yann 0:f30e2135b0db 45 void baud(int baudrate);
Yann 0:f30e2135b0db 46
Yann 0:f30e2135b0db 47 enum Parity {
Yann 0:f30e2135b0db 48 None = 0,
Yann 0:f30e2135b0db 49 Odd = 1,
Yann 0:f30e2135b0db 50 Even = 2,
Yann 0:f30e2135b0db 51 Forced1 = 3,
Yann 0:f30e2135b0db 52 Forced0 = 4
Yann 0:f30e2135b0db 53 };
Yann 0:f30e2135b0db 54
Yann 0:f30e2135b0db 55 enum IrqType {
Yann 0:f30e2135b0db 56 RxIrq = 0
Yann 0:f30e2135b0db 57 , TxIrq
Yann 0:f30e2135b0db 58 };
Yann 0:f30e2135b0db 59
Yann 0:f30e2135b0db 60 void format(int bits, int parity, int stop);
Yann 0:f30e2135b0db 61
Yann 0:f30e2135b0db 62 class CDummy;
Yann 0:f30e2135b0db 63 template <class T>
Yann 0:f30e2135b0db 64 void attach(T* pCbItem, void (T::*pCbMeth)(), IrqType type = RxIrq)
Yann 0:f30e2135b0db 65 {
Yann 0:f30e2135b0db 66 if(type == RxIrq)
Yann 0:f30e2135b0db 67 {
Yann 0:f30e2135b0db 68 m_pInCbItem = (CDummy*) pCbItem;
Yann 0:f30e2135b0db 69 m_pInCbMeth = (void (CDummy::*)()) pCbMeth;
Yann 0:f30e2135b0db 70 }
Yann 0:f30e2135b0db 71 else
Yann 0:f30e2135b0db 72 {
Yann 0:f30e2135b0db 73 m_pOutCbItem = (CDummy*) pCbItem;
Yann 0:f30e2135b0db 74 m_pOutCbMeth = (void (CDummy::*)()) pCbMeth;
Yann 0:f30e2135b0db 75 }
Yann 0:f30e2135b0db 76 }
Yann 0:f30e2135b0db 77
Yann 0:f30e2135b0db 78 #if 0 // Inhereted from Stream, for documentation only
Yann 0:f30e2135b0db 79
Yann 0:f30e2135b0db 80 /* Function: putc
Yann 0:f30e2135b0db 81 * Write a character
Yann 0:f30e2135b0db 82 *
Yann 0:f30e2135b0db 83 * Variables:
Yann 0:f30e2135b0db 84 * c - The character to write to the serial port
Yann 0:f30e2135b0db 85 */
Yann 0:f30e2135b0db 86 int putc(int c);
Yann 0:f30e2135b0db 87
Yann 0:f30e2135b0db 88 /* Function: getc
Yann 0:f30e2135b0db 89 * Read a character
Yann 0:f30e2135b0db 90 *
Yann 0:f30e2135b0db 91 * Variables:
Yann 0:f30e2135b0db 92 * returns - The character read from the serial port
Yann 0:f30e2135b0db 93 */
Yann 0:f30e2135b0db 94 int getc();
Yann 0:f30e2135b0db 95
Yann 0:f30e2135b0db 96 /* Function: printf
Yann 0:f30e2135b0db 97 * Write a formated string
Yann 0:f30e2135b0db 98 *
Yann 0:f30e2135b0db 99 * Variables:
Yann 0:f30e2135b0db 100 * format - A printf-style format string, followed by the
Yann 0:f30e2135b0db 101 * variables to use in formating the string.
Yann 0:f30e2135b0db 102 */
Yann 0:f30e2135b0db 103 int printf(const char* format, ...);
Yann 0:f30e2135b0db 104
Yann 0:f30e2135b0db 105 /* Function: scanf
Yann 0:f30e2135b0db 106 * Read a formated string
Yann 0:f30e2135b0db 107 *
Yann 0:f30e2135b0db 108 * Variables:
Yann 0:f30e2135b0db 109 * format - A scanf-style format string,
Yann 0:f30e2135b0db 110 * followed by the pointers to variables to store the results.
Yann 0:f30e2135b0db 111 */
Yann 0:f30e2135b0db 112 int scanf(const char* format, ...);
Yann 0:f30e2135b0db 113
Yann 0:f30e2135b0db 114 #endif
Yann 0:f30e2135b0db 115
Yann 0:f30e2135b0db 116 /* Function: readable
Yann 0:f30e2135b0db 117 * Determine if there is a character available to read
Yann 0:f30e2135b0db 118 *
Yann 0:f30e2135b0db 119 * Variables:
Yann 0:f30e2135b0db 120 * returns - 1 if there is a character available to read, else 0
Yann 0:f30e2135b0db 121 */
Yann 0:f30e2135b0db 122 int readable();
Yann 0:f30e2135b0db 123
Yann 0:f30e2135b0db 124 /* Function: writeable
Yann 0:f30e2135b0db 125 * Determine if there is space available to write a character
Yann 0:f30e2135b0db 126 *
Yann 0:f30e2135b0db 127 * Variables:
Yann 0:f30e2135b0db 128 * returns - 1 if there is space to write a character, else 0
Yann 0:f30e2135b0db 129 */
Yann 0:f30e2135b0db 130 int writeable();
Yann 0:f30e2135b0db 131
Yann 0:f30e2135b0db 132 #ifdef MBED_RPC
Yann 0:f30e2135b0db 133 virtual const struct rpc_method *get_rpc_methods();
Yann 0:f30e2135b0db 134 static struct rpc_class *get_rpc_class();
Yann 0:f30e2135b0db 135 #endif
Yann 0:f30e2135b0db 136
Yann 0:f30e2135b0db 137 protected:
Yann 0:f30e2135b0db 138
Yann 0:f30e2135b0db 139 virtual int _getc();
Yann 0:f30e2135b0db 140 virtual int _putc(int c);
Yann 0:f30e2135b0db 141
Yann 0:f30e2135b0db 142 void onReadable();
Yann 0:f30e2135b0db 143 void onWriteable();
Yann 0:f30e2135b0db 144
Yann 0:f30e2135b0db 145 void onEpInTransfer();
Yann 0:f30e2135b0db 146 void onEpOutTransfer();
Yann 0:f30e2135b0db 147
Yann 0:f30e2135b0db 148 private:
Yann 0:f30e2135b0db 149
Yann 0:f30e2135b0db 150 UsbEndpoint m_epIn;
Yann 0:f30e2135b0db 151 UsbEndpoint m_epOut;
Yann 0:f30e2135b0db 152
Yann 0:f30e2135b0db 153 CDummy* m_pInCbItem;
Yann 0:f30e2135b0db 154 void (CDummy::*m_pInCbMeth)();
Yann 0:f30e2135b0db 155
Yann 0:f30e2135b0db 156 CDummy* m_pOutCbItem;
Yann 0:f30e2135b0db 157 void (CDummy::*m_pOutCbMeth)();
Yann 0:f30e2135b0db 158
Yann 0:f30e2135b0db 159 void startTx();
Yann 0:f30e2135b0db 160 void startRx();
Yann 0:f30e2135b0db 161
Yann 0:f30e2135b0db 162 Timeout m_txTimeout;
Yann 0:f30e2135b0db 163 volatile bool m_timeout;
Yann 0:f30e2135b0db 164
Yann 0:f30e2135b0db 165 volatile char* m_inBufEven;
Yann 0:f30e2135b0db 166 volatile char* m_inBufOdd;
Yann 0:f30e2135b0db 167 volatile char* m_inBufUsr;
Yann 0:f30e2135b0db 168 volatile char* m_inBufTrmt;
Yann 0:f30e2135b0db 169
Yann 0:f30e2135b0db 170 volatile char* m_outBufEven;
Yann 0:f30e2135b0db 171 volatile char* m_outBufOdd;
Yann 0:f30e2135b0db 172 volatile char* m_outBufUsr;
Yann 0:f30e2135b0db 173 volatile char* m_outBufTrmt;
Yann 0:f30e2135b0db 174
Yann 0:f30e2135b0db 175 volatile int m_inBufLen;
Yann 0:f30e2135b0db 176 volatile int m_outBufLen;
Yann 0:f30e2135b0db 177
Yann 0:f30e2135b0db 178 volatile char* m_pInBufPos;
Yann 0:f30e2135b0db 179 volatile char* m_pOutBufPos;
Yann 0:f30e2135b0db 180
Yann 0:f30e2135b0db 181
Yann 0:f30e2135b0db 182
Yann 0:f30e2135b0db 183 };
Yann 0:f30e2135b0db 184
Yann 0:f30e2135b0db 185 }
Yann 0:f30e2135b0db 186
Yann 0:f30e2135b0db 187
Yann 0:f30e2135b0db 188
Yann 0:f30e2135b0db 189 #endif