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 * fsm.h - Network Control Protocol Finite State Machine header file.
Yann 0:f30e2135b0db 3 *
Yann 0:f30e2135b0db 4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
Yann 0:f30e2135b0db 5 * Copyright (c) 1997 Global Election Systems Inc.
Yann 0:f30e2135b0db 6 *
Yann 0:f30e2135b0db 7 * The authors hereby grant permission to use, copy, modify, distribute,
Yann 0:f30e2135b0db 8 * and license this software and its documentation for any purpose, provided
Yann 0:f30e2135b0db 9 * that existing copyright notices are retained in all copies and that this
Yann 0:f30e2135b0db 10 * notice and the following disclaimer are included verbatim in any
Yann 0:f30e2135b0db 11 * distributions. No written agreement, license, or royalty fee is required
Yann 0:f30e2135b0db 12 * for any of the authorized uses.
Yann 0:f30e2135b0db 13 *
Yann 0:f30e2135b0db 14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
Yann 0:f30e2135b0db 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Yann 0:f30e2135b0db 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Yann 0:f30e2135b0db 17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
Yann 0:f30e2135b0db 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Yann 0:f30e2135b0db 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Yann 0:f30e2135b0db 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Yann 0:f30e2135b0db 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Yann 0:f30e2135b0db 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Yann 0:f30e2135b0db 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Yann 0:f30e2135b0db 24 *
Yann 0:f30e2135b0db 25 ******************************************************************************
Yann 0:f30e2135b0db 26 * REVISION HISTORY
Yann 0:f30e2135b0db 27 *
Yann 0:f30e2135b0db 28 * 03-01-01 Marc Boucher <marc@mbsi.ca>
Yann 0:f30e2135b0db 29 * Ported to lwIP.
Yann 0:f30e2135b0db 30 * 97-11-05 Guy Lancaster <glanca@gesn.com>, Global Election Systems Inc.
Yann 0:f30e2135b0db 31 * Original based on BSD code.
Yann 0:f30e2135b0db 32 *****************************************************************************/
Yann 0:f30e2135b0db 33 /*
Yann 0:f30e2135b0db 34 * fsm.h - {Link, IP} Control Protocol Finite State Machine definitions.
Yann 0:f30e2135b0db 35 *
Yann 0:f30e2135b0db 36 * Copyright (c) 1989 Carnegie Mellon University.
Yann 0:f30e2135b0db 37 * All rights reserved.
Yann 0:f30e2135b0db 38 *
Yann 0:f30e2135b0db 39 * Redistribution and use in source and binary forms are permitted
Yann 0:f30e2135b0db 40 * provided that the above copyright notice and this paragraph are
Yann 0:f30e2135b0db 41 * duplicated in all such forms and that any documentation,
Yann 0:f30e2135b0db 42 * advertising materials, and other materials related to such
Yann 0:f30e2135b0db 43 * distribution and use acknowledge that the software was developed
Yann 0:f30e2135b0db 44 * by Carnegie Mellon University. The name of the
Yann 0:f30e2135b0db 45 * University may not be used to endorse or promote products derived
Yann 0:f30e2135b0db 46 * from this software without specific prior written permission.
Yann 0:f30e2135b0db 47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
Yann 0:f30e2135b0db 48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
Yann 0:f30e2135b0db 49 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Yann 0:f30e2135b0db 50 *
Yann 0:f30e2135b0db 51 * $Id: fsm.h,v 1.5 2009/12/31 17:08:08 goldsimon Exp $
Yann 0:f30e2135b0db 52 */
Yann 0:f30e2135b0db 53
Yann 0:f30e2135b0db 54 #ifndef FSM_H
Yann 0:f30e2135b0db 55 #define FSM_H
Yann 0:f30e2135b0db 56
Yann 0:f30e2135b0db 57 /*
Yann 0:f30e2135b0db 58 * LCP Packet header = Code, id, length.
Yann 0:f30e2135b0db 59 */
Yann 0:f30e2135b0db 60 #define HEADERLEN (sizeof (u_char) + sizeof (u_char) + sizeof (u_short))
Yann 0:f30e2135b0db 61
Yann 0:f30e2135b0db 62
Yann 0:f30e2135b0db 63 /*
Yann 0:f30e2135b0db 64 * CP (LCP, IPCP, etc.) codes.
Yann 0:f30e2135b0db 65 */
Yann 0:f30e2135b0db 66 #define CONFREQ 1 /* Configuration Request */
Yann 0:f30e2135b0db 67 #define CONFACK 2 /* Configuration Ack */
Yann 0:f30e2135b0db 68 #define CONFNAK 3 /* Configuration Nak */
Yann 0:f30e2135b0db 69 #define CONFREJ 4 /* Configuration Reject */
Yann 0:f30e2135b0db 70 #define TERMREQ 5 /* Termination Request */
Yann 0:f30e2135b0db 71 #define TERMACK 6 /* Termination Ack */
Yann 0:f30e2135b0db 72 #define CODEREJ 7 /* Code Reject */
Yann 0:f30e2135b0db 73
Yann 0:f30e2135b0db 74
Yann 0:f30e2135b0db 75 /*
Yann 0:f30e2135b0db 76 * Each FSM is described by an fsm structure and fsm callbacks.
Yann 0:f30e2135b0db 77 */
Yann 0:f30e2135b0db 78 typedef struct fsm {
Yann 0:f30e2135b0db 79 int unit; /* Interface unit number */
Yann 0:f30e2135b0db 80 u_short protocol; /* Data Link Layer Protocol field value */
Yann 0:f30e2135b0db 81 int state; /* State */
Yann 0:f30e2135b0db 82 int flags; /* Contains option bits */
Yann 0:f30e2135b0db 83 u_char id; /* Current id */
Yann 0:f30e2135b0db 84 u_char reqid; /* Current request id */
Yann 0:f30e2135b0db 85 u_char seen_ack; /* Have received valid Ack/Nak/Rej to Req */
Yann 0:f30e2135b0db 86 int timeouttime; /* Timeout time in milliseconds */
Yann 0:f30e2135b0db 87 int maxconfreqtransmits; /* Maximum Configure-Request transmissions */
Yann 0:f30e2135b0db 88 int retransmits; /* Number of retransmissions left */
Yann 0:f30e2135b0db 89 int maxtermtransmits; /* Maximum Terminate-Request transmissions */
Yann 0:f30e2135b0db 90 int nakloops; /* Number of nak loops since last ack */
Yann 0:f30e2135b0db 91 int maxnakloops; /* Maximum number of nak loops tolerated */
Yann 0:f30e2135b0db 92 struct fsm_callbacks* callbacks; /* Callback routines */
Yann 0:f30e2135b0db 93 char* term_reason; /* Reason for closing protocol */
Yann 0:f30e2135b0db 94 int term_reason_len; /* Length of term_reason */
Yann 0:f30e2135b0db 95 } fsm;
Yann 0:f30e2135b0db 96
Yann 0:f30e2135b0db 97
Yann 0:f30e2135b0db 98 typedef struct fsm_callbacks {
Yann 0:f30e2135b0db 99 void (*resetci)(fsm*); /* Reset our Configuration Information */
Yann 0:f30e2135b0db 100 int (*cilen)(fsm*); /* Length of our Configuration Information */
Yann 0:f30e2135b0db 101 void (*addci)(fsm*, u_char*, int*); /* Add our Configuration Information */
Yann 0:f30e2135b0db 102 int (*ackci)(fsm*, u_char*, int); /* ACK our Configuration Information */
Yann 0:f30e2135b0db 103 int (*nakci)(fsm*, u_char*, int); /* NAK our Configuration Information */
Yann 0:f30e2135b0db 104 int (*rejci)(fsm*, u_char*, int); /* Reject our Configuration Information */
Yann 0:f30e2135b0db 105 int (*reqci)(fsm*, u_char*, int*, int); /* Request peer's Configuration Information */
Yann 0:f30e2135b0db 106 void (*up)(fsm*); /* Called when fsm reaches LS_OPENED state */
Yann 0:f30e2135b0db 107 void (*down)(fsm*); /* Called when fsm leaves LS_OPENED state */
Yann 0:f30e2135b0db 108 void (*starting)(fsm*); /* Called when we want the lower layer */
Yann 0:f30e2135b0db 109 void (*finished)(fsm*); /* Called when we don't want the lower layer */
Yann 0:f30e2135b0db 110 void (*protreject)(int); /* Called when Protocol-Reject received */
Yann 0:f30e2135b0db 111 void (*retransmit)(fsm*); /* Retransmission is necessary */
Yann 0:f30e2135b0db 112 int (*extcode)(fsm*, int, u_char, u_char*, int); /* Called when unknown code received */
Yann 0:f30e2135b0db 113 char *proto_name; /* String name for protocol (for messages) */
Yann 0:f30e2135b0db 114 } fsm_callbacks;
Yann 0:f30e2135b0db 115
Yann 0:f30e2135b0db 116
Yann 0:f30e2135b0db 117 /*
Yann 0:f30e2135b0db 118 * Link states.
Yann 0:f30e2135b0db 119 */
Yann 0:f30e2135b0db 120 #define LS_INITIAL 0 /* Down, hasn't been opened */
Yann 0:f30e2135b0db 121 #define LS_STARTING 1 /* Down, been opened */
Yann 0:f30e2135b0db 122 #define LS_CLOSED 2 /* Up, hasn't been opened */
Yann 0:f30e2135b0db 123 #define LS_STOPPED 3 /* Open, waiting for down event */
Yann 0:f30e2135b0db 124 #define LS_CLOSING 4 /* Terminating the connection, not open */
Yann 0:f30e2135b0db 125 #define LS_STOPPING 5 /* Terminating, but open */
Yann 0:f30e2135b0db 126 #define LS_REQSENT 6 /* We've sent a Config Request */
Yann 0:f30e2135b0db 127 #define LS_ACKRCVD 7 /* We've received a Config Ack */
Yann 0:f30e2135b0db 128 #define LS_ACKSENT 8 /* We've sent a Config Ack */
Yann 0:f30e2135b0db 129 #define LS_OPENED 9 /* Connection available */
Yann 0:f30e2135b0db 130
Yann 0:f30e2135b0db 131 /*
Yann 0:f30e2135b0db 132 * Flags - indicate options controlling FSM operation
Yann 0:f30e2135b0db 133 */
Yann 0:f30e2135b0db 134 #define OPT_PASSIVE 1 /* Don't die if we don't get a response */
Yann 0:f30e2135b0db 135 #define OPT_RESTART 2 /* Treat 2nd OPEN as DOWN, UP */
Yann 0:f30e2135b0db 136 #define OPT_SILENT 4 /* Wait for peer to speak first */
Yann 0:f30e2135b0db 137
Yann 0:f30e2135b0db 138
Yann 0:f30e2135b0db 139 /*
Yann 0:f30e2135b0db 140 * Prototypes
Yann 0:f30e2135b0db 141 */
Yann 0:f30e2135b0db 142 void fsm_init (fsm*);
Yann 0:f30e2135b0db 143 void fsm_lowerup (fsm*);
Yann 0:f30e2135b0db 144 void fsm_lowerdown (fsm*);
Yann 0:f30e2135b0db 145 void fsm_open (fsm*);
Yann 0:f30e2135b0db 146 void fsm_close (fsm*, char*);
Yann 0:f30e2135b0db 147 void fsm_input (fsm*, u_char*, int);
Yann 0:f30e2135b0db 148 void fsm_protreject (fsm*);
Yann 0:f30e2135b0db 149 void fsm_sdata (fsm*, u_char, u_char, u_char*, int);
Yann 0:f30e2135b0db 150
Yann 0:f30e2135b0db 151
Yann 0:f30e2135b0db 152 /*
Yann 0:f30e2135b0db 153 * Variables
Yann 0:f30e2135b0db 154 */
Yann 0:f30e2135b0db 155 extern int peer_mru[]; /* currently negotiated peer MRU (per unit) */
Yann 0:f30e2135b0db 156
Yann 0:f30e2135b0db 157 #endif /* FSM_H */