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 /** \file
Yann 0:f30e2135b0db 25 TCP Socket header file
Yann 0:f30e2135b0db 26 */
Yann 0:f30e2135b0db 27
Yann 0:f30e2135b0db 28 #ifndef TCPSOCKET_H
Yann 0:f30e2135b0db 29 #define TCPSOCKET_H
Yann 0:f30e2135b0db 30
Yann 0:f30e2135b0db 31 #include "core/net.h"
Yann 0:f30e2135b0db 32 #include "core/host.h"
Yann 0:f30e2135b0db 33 //Essentially it is a safe interface to NetTcpSocket
Yann 0:f30e2135b0db 34
Yann 0:f30e2135b0db 35 ///TCP Socket error codes
Yann 0:f30e2135b0db 36 enum TCPSocketErr
Yann 0:f30e2135b0db 37 {
Yann 0:f30e2135b0db 38 __TCPSOCKET_MIN = -0xFFFF,
Yann 0:f30e2135b0db 39 TCPSOCKET_SETUP, ///<TCPSocket not properly configured
Yann 0:f30e2135b0db 40 TCPSOCKET_TIMEOUT, ///<Connection timed out
Yann 0:f30e2135b0db 41 TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
Yann 0:f30e2135b0db 42 TCPSOCKET_MEM, ///<Not enough mem
Yann 0:f30e2135b0db 43 TCPSOCKET_INUSE, ///<Interface / Port is in use
Yann 0:f30e2135b0db 44 TCPSOCKET_EMPTY, ///<Connections queue is empty
Yann 0:f30e2135b0db 45 TCPSOCKET_RST, ///<Connection was reset by remote host
Yann 0:f30e2135b0db 46 //...
Yann 0:f30e2135b0db 47 TCPSOCKET_OK = 0 ///<Success
Yann 0:f30e2135b0db 48 };
Yann 0:f30e2135b0db 49
Yann 0:f30e2135b0db 50 ///TCP Socket Events
Yann 0:f30e2135b0db 51 enum TCPSocketEvent
Yann 0:f30e2135b0db 52 {
Yann 0:f30e2135b0db 53 TCPSOCKET_CONNECTED, ///<Connected to host
Yann 0:f30e2135b0db 54 TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket
Yann 0:f30e2135b0db 55 TCPSOCKET_READABLE, ///<Data in buf
Yann 0:f30e2135b0db 56 TCPSOCKET_WRITEABLE, ///<Can write data to buf
Yann 0:f30e2135b0db 57 TCPSOCKET_CONTIMEOUT, ///<Connection timed out
Yann 0:f30e2135b0db 58 TCPSOCKET_CONRST, ///<Connection was reset by remote host
Yann 0:f30e2135b0db 59 TCPSOCKET_CONABRT, ///<Connection was aborted
Yann 0:f30e2135b0db 60 TCPSOCKET_ERROR, ///<Unknown error
Yann 0:f30e2135b0db 61 TCPSOCKET_DISCONNECTED ///<Disconnected
Yann 0:f30e2135b0db 62 };
Yann 0:f30e2135b0db 63
Yann 0:f30e2135b0db 64 class NetTcpSocket;
Yann 0:f30e2135b0db 65 enum NetTcpSocketEvent;
Yann 0:f30e2135b0db 66
Yann 0:f30e2135b0db 67 ///This is a simple TCP Socket class
Yann 0:f30e2135b0db 68 /**
Yann 0:f30e2135b0db 69 This class exposes an API to deal with TCP Sockets
Yann 0:f30e2135b0db 70 */
Yann 0:f30e2135b0db 71 class TCPSocket
Yann 0:f30e2135b0db 72 {
Yann 0:f30e2135b0db 73 public:
Yann 0:f30e2135b0db 74 ///Creates a new socket
Yann 0:f30e2135b0db 75 TCPSocket();
Yann 0:f30e2135b0db 76 protected:
Yann 0:f30e2135b0db 77 TCPSocket(NetTcpSocket* pNetTcpSocket);
Yann 0:f30e2135b0db 78 public:
Yann 0:f30e2135b0db 79 ///Closes if needed and destroys the socket
Yann 0:f30e2135b0db 80 ~TCPSocket(); //close()
Yann 0:f30e2135b0db 81
Yann 0:f30e2135b0db 82 ///Binds the socket to (local) host
Yann 0:f30e2135b0db 83 TCPSocketErr bind(const Host& me);
Yann 0:f30e2135b0db 84
Yann 0:f30e2135b0db 85 ///Starts listening
Yann 0:f30e2135b0db 86 TCPSocketErr listen();
Yann 0:f30e2135b0db 87
Yann 0:f30e2135b0db 88 ///Connects socket to host
Yann 0:f30e2135b0db 89 TCPSocketErr connect(const Host& host);
Yann 0:f30e2135b0db 90
Yann 0:f30e2135b0db 91 ///Accepts connection from client and gets connected socket
Yann 0:f30e2135b0db 92 TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
Yann 0:f30e2135b0db 93
Yann 0:f30e2135b0db 94 ///Sends data
Yann 0:f30e2135b0db 95 /*
Yann 0:f30e2135b0db 96 @return a negative error code or the number of bytes transmitted
Yann 0:f30e2135b0db 97 */
Yann 0:f30e2135b0db 98 int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
Yann 0:f30e2135b0db 99
Yann 0:f30e2135b0db 100 ///Receives data
Yann 0:f30e2135b0db 101 /*
Yann 0:f30e2135b0db 102 @return a negative error code or the number of bytes received
Yann 0:f30e2135b0db 103 */
Yann 0:f30e2135b0db 104 int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
Yann 0:f30e2135b0db 105
Yann 0:f30e2135b0db 106 /* TODO NTH : printf / scanf helpers that call send/recv */
Yann 0:f30e2135b0db 107
Yann 0:f30e2135b0db 108 ///Closes socket
Yann 0:f30e2135b0db 109 TCPSocketErr close();
Yann 0:f30e2135b0db 110
Yann 0:f30e2135b0db 111 //Callbacks
Yann 0:f30e2135b0db 112 ///Setups callback
Yann 0:f30e2135b0db 113 /**
Yann 0:f30e2135b0db 114 @param pMethod : callback function
Yann 0:f30e2135b0db 115 */
Yann 0:f30e2135b0db 116 void setOnEvent( void (*pMethod)(TCPSocketEvent) );
Yann 0:f30e2135b0db 117
Yann 0:f30e2135b0db 118 class CDummy;
Yann 0:f30e2135b0db 119 ///Setups callback
Yann 0:f30e2135b0db 120 /**
Yann 0:f30e2135b0db 121 @param pItem : instance of class on which to execute the callback method
Yann 0:f30e2135b0db 122 @param pMethod : callback method
Yann 0:f30e2135b0db 123 */
Yann 0:f30e2135b0db 124 template<class T>
Yann 0:f30e2135b0db 125 void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
Yann 0:f30e2135b0db 126 {
Yann 0:f30e2135b0db 127 m_pCbItem = (CDummy*) pItem;
Yann 0:f30e2135b0db 128 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
Yann 0:f30e2135b0db 129 }
Yann 0:f30e2135b0db 130
Yann 0:f30e2135b0db 131 ///Disables callback
Yann 0:f30e2135b0db 132 void resetOnEvent();
Yann 0:f30e2135b0db 133
Yann 0:f30e2135b0db 134 protected:
Yann 0:f30e2135b0db 135 void onNetTcpSocketEvent(NetTcpSocketEvent e);
Yann 0:f30e2135b0db 136 TCPSocketErr checkInst();
Yann 0:f30e2135b0db 137
Yann 0:f30e2135b0db 138 private:
Yann 0:f30e2135b0db 139 NetTcpSocket* m_pNetTcpSocket;
Yann 0:f30e2135b0db 140
Yann 0:f30e2135b0db 141 CDummy* m_pCbItem;
Yann 0:f30e2135b0db 142 void (CDummy::*m_pCbMeth)(TCPSocketEvent);
Yann 0:f30e2135b0db 143
Yann 0:f30e2135b0db 144 void (*m_pCb)(TCPSocketEvent);
Yann 0:f30e2135b0db 145
Yann 0:f30e2135b0db 146 };
Yann 0:f30e2135b0db 147
Yann 0:f30e2135b0db 148 #endif