Read NTP time from Ethernet and drive six 7-segment LEDs using a MAX7221 to show time, date, month, week, year etc....

Dependencies:   NTPClient_NetServices mbed

Committer:
cheungderek
Date:
Wed Jun 22 01:31:47 2011 +0000
Revision:
0:e083abcfe7a8
First release.
Just some testing but working code to read NTP time and drive 6 LEDs to display time, date, month, year etc...

Who changed what in which revision?

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