UDPversion
Dependencies: XBee mbed NetServicesMin
main.cpp@1:3a46d2725374, 2012-04-15 (annotated)
- Committer:
- recotana
- Date:
- Sun Apr 15 10:49:08 2012 +0000
- Revision:
- 1:3a46d2725374
- Parent:
- 0:84a3b029656e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
recotana | 0:84a3b029656e | 1 | #include "mbed.h" |
recotana | 1:3a46d2725374 | 2 | |
recotana | 1:3a46d2725374 | 3 | Serial pc(USBTX, USBRX); |
recotana | 1:3a46d2725374 | 4 | |
recotana | 0:84a3b029656e | 5 | #include "xbee_lib.h" |
recotana | 0:84a3b029656e | 6 | #include "EthernetNetIf.h" |
recotana | 0:84a3b029656e | 7 | #include "config.h" |
recotana | 0:84a3b029656e | 8 | #include "tcp_server.h" |
recotana | 0:84a3b029656e | 9 | #include "tcp_client.h" |
recotana | 0:84a3b029656e | 10 | #include "xbee_rec_route.h" |
recotana | 0:84a3b029656e | 11 | |
recotana | 0:84a3b029656e | 12 | #include "leds.h" |
recotana | 0:84a3b029656e | 13 | |
recotana | 1:3a46d2725374 | 14 | #include "data_models.h" |
recotana | 0:84a3b029656e | 15 | |
recotana | 0:84a3b029656e | 16 | volatile PrefServer pref; |
recotana | 1:3a46d2725374 | 17 | volatile PrefSender spref; |
recotana | 1:3a46d2725374 | 18 | volatile PrefReciever rpref; |
recotana | 0:84a3b029656e | 19 | |
recotana | 0:84a3b029656e | 20 | EthernetNetIf eth ; |
recotana | 0:84a3b029656e | 21 | |
recotana | 0:84a3b029656e | 22 | Ticker connectCheck; |
recotana | 0:84a3b029656e | 23 | |
recotana | 0:84a3b029656e | 24 | DigitalIn recSw(p20); |
recotana | 0:84a3b029656e | 25 | Timeout chatter; |
recotana | 0:84a3b029656e | 26 | bool isPressed; |
recotana | 0:84a3b029656e | 27 | |
recotana | 0:84a3b029656e | 28 | extern bool healthiPad; |
recotana | 0:84a3b029656e | 29 | |
recotana | 0:84a3b029656e | 30 | |
recotana | 0:84a3b029656e | 31 | class Watchdog { |
recotana | 0:84a3b029656e | 32 | public: |
recotana | 0:84a3b029656e | 33 | void kick(float s) { |
recotana | 0:84a3b029656e | 34 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
recotana | 0:84a3b029656e | 35 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
recotana | 0:84a3b029656e | 36 | LPC_WDT->WDTC = s * (float)clk; |
recotana | 0:84a3b029656e | 37 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
recotana | 0:84a3b029656e | 38 | kick(); |
recotana | 0:84a3b029656e | 39 | } |
recotana | 0:84a3b029656e | 40 | |
recotana | 0:84a3b029656e | 41 | void kick() { |
recotana | 0:84a3b029656e | 42 | LPC_WDT->WDFEED = 0xAA; |
recotana | 0:84a3b029656e | 43 | LPC_WDT->WDFEED = 0x55; |
recotana | 0:84a3b029656e | 44 | } |
recotana | 0:84a3b029656e | 45 | }; |
recotana | 0:84a3b029656e | 46 | |
recotana | 0:84a3b029656e | 47 | Watchdog w; |
recotana | 0:84a3b029656e | 48 | |
recotana | 0:84a3b029656e | 49 | |
recotana | 0:84a3b029656e | 50 | |
recotana | 0:84a3b029656e | 51 | |
recotana | 0:84a3b029656e | 52 | |
recotana | 0:84a3b029656e | 53 | |
recotana | 0:84a3b029656e | 54 | void testModeInit(void) |
recotana | 0:84a3b029656e | 55 | { |
recotana | 0:84a3b029656e | 56 | recSw.mode(PullUp); |
recotana | 0:84a3b029656e | 57 | } |
recotana | 0:84a3b029656e | 58 | |
recotana | 0:84a3b029656e | 59 | void testSendRecieveData() |
recotana | 0:84a3b029656e | 60 | { |
recotana | 0:84a3b029656e | 61 | |
recotana | 0:84a3b029656e | 62 | Payload payload; |
recotana | 0:84a3b029656e | 63 | initTestPayloadData(&payload); |
recotana | 0:84a3b029656e | 64 | |
recotana | 0:84a3b029656e | 65 | udpSendPayload(&payload); |
recotana | 0:84a3b029656e | 66 | |
recotana | 0:84a3b029656e | 67 | } |
recotana | 0:84a3b029656e | 68 | |
recotana | 0:84a3b029656e | 69 | |
recotana | 1:3a46d2725374 | 70 | |
recotana | 0:84a3b029656e | 71 | |
recotana | 0:84a3b029656e | 72 | |
recotana | 0:84a3b029656e | 73 | |
recotana | 0:84a3b029656e | 74 | int main() { |
recotana | 0:84a3b029656e | 75 | |
recotana | 0:84a3b029656e | 76 | |
recotana | 0:84a3b029656e | 77 | |
recotana | 0:84a3b029656e | 78 | pc.baud(115200); |
recotana | 0:84a3b029656e | 79 | |
recotana | 1:3a46d2725374 | 80 | |
recotana | 0:84a3b029656e | 81 | uint8_t tmpip[4]={kSserverIp}; |
recotana | 0:84a3b029656e | 82 | uint8_t tmpsubnet[4]={kSubnetMask}; |
recotana | 0:84a3b029656e | 83 | uint8_t tmprt[4]={kRouterIp}; |
recotana | 0:84a3b029656e | 84 | |
recotana | 0:84a3b029656e | 85 | EthernetNetIf eth( // -- static IP address |
recotana | 0:84a3b029656e | 86 | IpAddr(kSserverIp), // IP Address |
recotana | 0:84a3b029656e | 87 | IpAddr(kSubnetMask), // Subnet Mask |
recotana | 0:84a3b029656e | 88 | IpAddr(kRouterIp), // Default Gateway |
recotana | 0:84a3b029656e | 89 | IpAddr(kRouterIp) // DNS Server |
recotana | 0:84a3b029656e | 90 | ) ; |
recotana | 1:3a46d2725374 | 91 | |
recotana | 1:3a46d2725374 | 92 | #ifdef _SERVER_TEST_MODE_ |
recotana | 1:3a46d2725374 | 93 | pc.printf("*.*.*.*.*.*.*. T E S T M O D E *.*.*.*.*.*.*.*.*.*.\n"); |
recotana | 1:3a46d2725374 | 94 | testModeInit(); |
recotana | 1:3a46d2725374 | 95 | #endif |
recotana | 1:3a46d2725374 | 96 | |
recotana | 0:84a3b029656e | 97 | |
recotana | 0:84a3b029656e | 98 | if( eth.setup() != ETH_OK ) |
recotana | 0:84a3b029656e | 99 | { |
recotana | 0:84a3b029656e | 100 | pc.printf("Ether Setup Error\r\n"); |
recotana | 0:84a3b029656e | 101 | return -1; |
recotana | 0:84a3b029656e | 102 | } |
recotana | 0:84a3b029656e | 103 | pc.printf("Ether Setup OK\r\n"); |
recotana | 0:84a3b029656e | 104 | |
recotana | 0:84a3b029656e | 105 | memcpy((void*)pref.serverAddress,tmpip,4); |
recotana | 0:84a3b029656e | 106 | pc.printf("mbed IP Address is [%d.%d.%d.%d]\r\n", pref.serverAddress[0], pref.serverAddress[1], pref.serverAddress[2], pref.serverAddress[3]) ; |
recotana | 0:84a3b029656e | 107 | |
recotana | 0:84a3b029656e | 108 | memcpy((void*)pref.subnetMask,tmpsubnet,4); |
recotana | 0:84a3b029656e | 109 | pc.printf("mbed subnet Address is [%d.%d.%d.%d]\r\n", pref.subnetMask[0], pref.subnetMask[1], pref.subnetMask[2], pref.subnetMask[3]) ; |
recotana | 0:84a3b029656e | 110 | |
recotana | 0:84a3b029656e | 111 | memcpy((void*)pref.routerAddress,tmprt,4); |
recotana | 0:84a3b029656e | 112 | pc.printf("mbed router Address is [%d.%d.%d.%d]\r\n", pref.routerAddress[0], pref.routerAddress[1], pref.routerAddress[2], pref.routerAddress[3]) ; |
recotana | 0:84a3b029656e | 113 | |
recotana | 0:84a3b029656e | 114 | ; |
recotana | 0:84a3b029656e | 115 | |
recotana | 0:84a3b029656e | 116 | xbeeInit(); |
recotana | 0:84a3b029656e | 117 | |
recotana | 0:84a3b029656e | 118 | serverInit(); |
recotana | 0:84a3b029656e | 119 | clientInit(); |
recotana | 0:84a3b029656e | 120 | |
recotana | 0:84a3b029656e | 121 | startLed(); |
recotana | 0:84a3b029656e | 122 | |
recotana | 0:84a3b029656e | 123 | connectCheckStart(); |
recotana | 0:84a3b029656e | 124 | |
recotana | 0:84a3b029656e | 125 | // w.kick(2.5); //watch dog timer set |
recotana | 0:84a3b029656e | 126 | |
recotana | 0:84a3b029656e | 127 | while(1) { |
recotana | 0:84a3b029656e | 128 | |
recotana | 0:84a3b029656e | 129 | w.kick(); |
recotana | 0:84a3b029656e | 130 | |
recotana | 0:84a3b029656e | 131 | Net::poll(); |
recotana | 0:84a3b029656e | 132 | |
recotana | 0:84a3b029656e | 133 | |
recotana | 1:3a46d2725374 | 134 | #ifdef _SERVER_TEST_MODE_ |
recotana | 0:84a3b029656e | 135 | if(!recSw){ |
recotana | 0:84a3b029656e | 136 | testSendRecieveData(); |
recotana | 0:84a3b029656e | 137 | wait(0.5); |
recotana | 0:84a3b029656e | 138 | } |
recotana | 1:3a46d2725374 | 139 | #else |
recotana | 0:84a3b029656e | 140 | xbeeRecieveRouting(); |
recotana | 1:3a46d2725374 | 141 | #endif |
recotana | 0:84a3b029656e | 142 | if(healthiPad) ledTcpConnect(false); |
recotana | 0:84a3b029656e | 143 | else ledTcpConnect(true); |
recotana | 0:84a3b029656e | 144 | |
recotana | 0:84a3b029656e | 145 | } |
recotana | 0:84a3b029656e | 146 | } |