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 LWIPNETTCPSOCKET_H
Yann 0:f30e2135b0db 25 #define LWIPNETTCPSOCKET_H
Yann 0:f30e2135b0db 26
Yann 0:f30e2135b0db 27 #define NET_LWIP_STACK 1
Yann 0:f30e2135b0db 28 #include "if/net/nettcpsocket.h"
Yann 0:f30e2135b0db 29 #include "LwipNetIf.h"
Yann 0:f30e2135b0db 30
Yann 0:f30e2135b0db 31 #include "stdint.h"
Yann 0:f30e2135b0db 32
Yann 0:f30e2135b0db 33 //Implements NetTcpSockets over lwIP raw API
Yann 0:f30e2135b0db 34
Yann 0:f30e2135b0db 35 struct tcp_pcb; //Represents a Tcp Connection, "Protocol Control Block", see rawapi.txt & tcp.h
Yann 0:f30e2135b0db 36 struct pbuf; //Lwip Buffer Container
Yann 0:f30e2135b0db 37
Yann 0:f30e2135b0db 38 typedef signed char err_t;
Yann 0:f30e2135b0db 39 typedef uint16_t u16_t;
Yann 0:f30e2135b0db 40
Yann 0:f30e2135b0db 41 class LwipNetTcpSocket: public NetTcpSocket
Yann 0:f30e2135b0db 42 {
Yann 0:f30e2135b0db 43 public:
Yann 0:f30e2135b0db 44 LwipNetTcpSocket(tcp_pcb* pPcb = NULL); //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
Yann 0:f30e2135b0db 45 virtual ~LwipNetTcpSocket();
Yann 0:f30e2135b0db 46
Yann 0:f30e2135b0db 47 virtual NetTcpSocketErr bind(const Host& me);
Yann 0:f30e2135b0db 48 virtual NetTcpSocketErr listen();
Yann 0:f30e2135b0db 49 virtual NetTcpSocketErr connect(const Host& host);
Yann 0:f30e2135b0db 50 virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket);
Yann 0:f30e2135b0db 51
Yann 0:f30e2135b0db 52 virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len);
Yann 0:f30e2135b0db 53 virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len);
Yann 0:f30e2135b0db 54
Yann 0:f30e2135b0db 55 virtual NetTcpSocketErr close();
Yann 0:f30e2135b0db 56
Yann 0:f30e2135b0db 57 virtual NetTcpSocketErr poll();
Yann 0:f30e2135b0db 58
Yann 0:f30e2135b0db 59 protected:
Yann 0:f30e2135b0db 60 volatile tcp_pcb* m_pPcb;
Yann 0:f30e2135b0db 61
Yann 0:f30e2135b0db 62 //Events callbacks from lwIp
Yann 0:f30e2135b0db 63 err_t acceptCb(tcp_pcb* newpcb, err_t err);
Yann 0:f30e2135b0db 64 err_t connectedCb(tcp_pcb* tpcb, err_t err);
Yann 0:f30e2135b0db 65
Yann 0:f30e2135b0db 66 void errCb(err_t err);
Yann 0:f30e2135b0db 67
Yann 0:f30e2135b0db 68 err_t sentCb(tcp_pcb* tpcb, u16_t len);
Yann 0:f30e2135b0db 69 err_t recvCb(tcp_pcb* tpcb, pbuf *p, err_t err);
Yann 0:f30e2135b0db 70
Yann 0:f30e2135b0db 71 private:
Yann 0:f30e2135b0db 72 void cleanUp(); //Flush input buffer
Yann 0:f30e2135b0db 73
Yann 0:f30e2135b0db 74 // queue<tcp_pcb*> m_lpInPcb; //Incoming connections that have not been accepted yet
Yann 0:f30e2135b0db 75 queue<LwipNetTcpSocket*> m_lpInNetTcpSocket; //Incoming connections that have not been accepted yet
Yann 0:f30e2135b0db 76
Yann 0:f30e2135b0db 77 volatile pbuf* m_pReadPbuf; //Ptr to read buffer
Yann 0:f30e2135b0db 78
Yann 0:f30e2135b0db 79 //Static callbacks : Transforms into a C++ callback
Yann 0:f30e2135b0db 80 static err_t sAcceptCb(void *arg, struct tcp_pcb *newpcb, err_t err);
Yann 0:f30e2135b0db 81 static err_t sConnectedCb(void *arg, struct tcp_pcb *tpcb, err_t err);
Yann 0:f30e2135b0db 82
Yann 0:f30e2135b0db 83 static void sErrCb(void *arg, err_t err);
Yann 0:f30e2135b0db 84
Yann 0:f30e2135b0db 85 static err_t sSentCb(void *arg, struct tcp_pcb *tpcb, u16_t len);
Yann 0:f30e2135b0db 86 static err_t sRecvCb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
Yann 0:f30e2135b0db 87
Yann 0:f30e2135b0db 88 };
Yann 0:f30e2135b0db 89
Yann 0:f30e2135b0db 90 #endif