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