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 NETTCPSOCKET_H
Yann 0:f30e2135b0db 25 #define NETTCPSOCKET_H
Yann 0:f30e2135b0db 26
Yann 0:f30e2135b0db 27 #include "net.h"
Yann 0:f30e2135b0db 28 #include "host.h"
Yann 0:f30e2135b0db 29
Yann 0:f30e2135b0db 30 #include <queue>
Yann 0:f30e2135b0db 31 using std::queue;
Yann 0:f30e2135b0db 32
Yann 0:f30e2135b0db 33 //Implements a Berkeley-like socket if
Yann 0:f30e2135b0db 34 //Can be interfaced either to lwip or a Telit module
Yann 0:f30e2135b0db 35
Yann 0:f30e2135b0db 36 enum NetTcpSocketErr
Yann 0:f30e2135b0db 37 {
Yann 0:f30e2135b0db 38 __NETTCPSOCKET_MIN = -0xFFFF,
Yann 0:f30e2135b0db 39 NETTCPSOCKET_SETUP, //NetTcpSocket not properly configured
Yann 0:f30e2135b0db 40 NETTCPSOCKET_TIMEOUT,
Yann 0:f30e2135b0db 41 NETTCPSOCKET_IF, //If has problems
Yann 0:f30e2135b0db 42 NETTCPSOCKET_MEM, //Not enough mem
Yann 0:f30e2135b0db 43 NETTCPSOCKET_INUSE, //If/Port is in use
Yann 0:f30e2135b0db 44 NETTCPSOCKET_EMPTY, //Connections queue is empty
Yann 0:f30e2135b0db 45 NETTCPSOCKET_RST, // Connection was reset by remote host
Yann 0:f30e2135b0db 46 //...
Yann 0:f30e2135b0db 47 NETTCPSOCKET_OK = 0
Yann 0:f30e2135b0db 48 };
Yann 0:f30e2135b0db 49
Yann 0:f30e2135b0db 50 enum NetTcpSocketEvent
Yann 0:f30e2135b0db 51 {
Yann 0:f30e2135b0db 52 NETTCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening
Yann 0:f30e2135b0db 53 NETTCPSOCKET_ACCEPT, //Connected to client
Yann 0:f30e2135b0db 54 NETTCPSOCKET_READABLE, //Data in buf
Yann 0:f30e2135b0db 55 NETTCPSOCKET_WRITEABLE, //Can write data to buf
Yann 0:f30e2135b0db 56 NETTCPSOCKET_CONTIMEOUT,
Yann 0:f30e2135b0db 57 NETTCPSOCKET_CONRST,
Yann 0:f30e2135b0db 58 NETTCPSOCKET_CONABRT,
Yann 0:f30e2135b0db 59 NETTCPSOCKET_ERROR,
Yann 0:f30e2135b0db 60 NETTCPSOCKET_DISCONNECTED
Yann 0:f30e2135b0db 61 };
Yann 0:f30e2135b0db 62
Yann 0:f30e2135b0db 63
Yann 0:f30e2135b0db 64 class NetTcpSocket
Yann 0:f30e2135b0db 65 {
Yann 0:f30e2135b0db 66 public:
Yann 0:f30e2135b0db 67 NetTcpSocket();
Yann 0:f30e2135b0db 68 virtual ~NetTcpSocket(); //close()
Yann 0:f30e2135b0db 69
Yann 0:f30e2135b0db 70 virtual NetTcpSocketErr bind(const Host& me) = 0;
Yann 0:f30e2135b0db 71 virtual NetTcpSocketErr listen() = 0;
Yann 0:f30e2135b0db 72 virtual NetTcpSocketErr connect(const Host& host) = 0;
Yann 0:f30e2135b0db 73 virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) = 0;
Yann 0:f30e2135b0db 74
Yann 0:f30e2135b0db 75 virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len) = 0;
Yann 0:f30e2135b0db 76 virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len) = 0;
Yann 0:f30e2135b0db 77
Yann 0:f30e2135b0db 78 virtual NetTcpSocketErr close() = 0;
Yann 0:f30e2135b0db 79
Yann 0:f30e2135b0db 80 virtual NetTcpSocketErr poll() = 0;
Yann 0:f30e2135b0db 81
Yann 0:f30e2135b0db 82 class CDummy;
Yann 0:f30e2135b0db 83 //Callbacks
Yann 0:f30e2135b0db 84 template<class T>
Yann 0:f30e2135b0db 85 //Linker bug : Must be defined here :(
Yann 0:f30e2135b0db 86 void setOnEvent( T* pItem, void (T::*pMethod)(NetTcpSocketEvent) )
Yann 0:f30e2135b0db 87 {
Yann 0:f30e2135b0db 88 m_pCbItem = (CDummy*) pItem;
Yann 0:f30e2135b0db 89 m_pCbMeth = (void (CDummy::*)(NetTcpSocketEvent)) pMethod;
Yann 0:f30e2135b0db 90 }
Yann 0:f30e2135b0db 91
Yann 0:f30e2135b0db 92 void resetOnEvent(); //Disable callback
Yann 0:f30e2135b0db 93
Yann 0:f30e2135b0db 94 protected:
Yann 0:f30e2135b0db 95 void queueEvent(NetTcpSocketEvent e);
Yann 0:f30e2135b0db 96 void discardEvents();
Yann 0:f30e2135b0db 97 void flushEvents(); //to be called during polling
Yann 0:f30e2135b0db 98
Yann 0:f30e2135b0db 99 Host m_host;
Yann 0:f30e2135b0db 100 Host m_client;
Yann 0:f30e2135b0db 101
Yann 0:f30e2135b0db 102 friend class Net;
Yann 0:f30e2135b0db 103 int m_refs;
Yann 0:f30e2135b0db 104
Yann 0:f30e2135b0db 105 bool m_closed;
Yann 0:f30e2135b0db 106 bool m_removed;
Yann 0:f30e2135b0db 107
Yann 0:f30e2135b0db 108 private:
Yann 0:f30e2135b0db 109 //We do not want to execute user code in interrupt routines, so we queue events until the server is polled
Yann 0:f30e2135b0db 110 //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) )
Yann 0:f30e2135b0db 111 void onEvent(NetTcpSocketEvent e); //To be called on poll
Yann 0:f30e2135b0db 112 CDummy* m_pCbItem;
Yann 0:f30e2135b0db 113 void (CDummy::*m_pCbMeth)(NetTcpSocketEvent);
Yann 0:f30e2135b0db 114 queue<NetTcpSocketEvent> m_events;
Yann 0:f30e2135b0db 115
Yann 0:f30e2135b0db 116 };
Yann 0:f30e2135b0db 117
Yann 0:f30e2135b0db 118 #endif