Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed
Fork of F103-Serial-to-Ethernet by
main.cpp@33:c906ccc220ba, 2015-01-25 (annotated)
- Committer:
 - olympux
 - Date:
 - Sun Jan 25 14:04:04 2015 +0000
 - Revision:
 - 33:c906ccc220ba
 - Parent:
 - 32:db2e8ea06ee1
 - Child:
 - 34:32299b819067
 
Auto reset after configured
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| olympux | 0:c2eac797face | 1 | /* | 
| olympux | 30:15e23257e786 | 2 | * Firmware supports NNIO and RPC protocols | 
| olympux | 30:15e23257e786 | 3 | */ | 
| olympux | 0:c2eac797face | 4 | #include "mbed.h" | 
| olympux | 27:22f289beceb8 | 5 | #include "rtos.h" | 
| olympux | 27:22f289beceb8 | 6 | #include "mbed_rpc.h" | 
| olympux | 27:22f289beceb8 | 7 | #include "Arguments.h" | 
| olympux | 27:22f289beceb8 | 8 | |
| olympux | 12:7c152c0ca4d8 | 9 | #include "eeprom.h" | 
| olympux | 0:c2eac797face | 10 | #include "EthernetInterface.h" | 
| olympux | 9:d2534ecf88c6 | 11 | #include "NTPClient.h" | 
| olympux | 0:c2eac797face | 12 | |
| olympux | 12:7c152c0ca4d8 | 13 | #include "my_eeprom_funcs.h" | 
| olympux | 20:71c7950fdd91 | 14 | #include "Watchdog.h" | 
| olympux | 12:7c152c0ca4d8 | 15 | |
| olympux | 0:c2eac797face | 16 | |
| olympux | 31:2e4b6de6c2f3 | 17 | /** Debug option | 
| olympux | 31:2e4b6de6c2f3 | 18 | * | 
| olympux | 31:2e4b6de6c2f3 | 19 | */ | 
| olympux | 25:48dd18cc147c | 20 | #if 1 | 
| olympux | 16:84a5bf7285d0 | 21 | //Enable debug | 
| olympux | 16:84a5bf7285d0 | 22 | #include <cstdio> | 
| olympux | 31:2e4b6de6c2f3 | 23 | #define DBG(x, ...) std::printf("[main : DBG]"x"\r\n", ##__VA_ARGS__); | 
| olympux | 31:2e4b6de6c2f3 | 24 | #define WARN(x, ...) std::printf("[main : WARN]"x"\r\n", ##__VA_ARGS__); | 
| olympux | 31:2e4b6de6c2f3 | 25 | #define ERR(x, ...) std::printf("[main : ERR]"x"\r\n", ##__VA_ARGS__); | 
| olympux | 16:84a5bf7285d0 | 26 | #else | 
| olympux | 16:84a5bf7285d0 | 27 | //Disable debug | 
| olympux | 31:2e4b6de6c2f3 | 28 | #define DBG(x, ...) | 
| olympux | 16:84a5bf7285d0 | 29 | #define WARN(x, ...) | 
| olympux | 31:2e4b6de6c2f3 | 30 | #define ERR(x, ...) | 
| olympux | 16:84a5bf7285d0 | 31 | #endif | 
| olympux | 16:84a5bf7285d0 | 32 | |
| olympux | 16:84a5bf7285d0 | 33 | |
| olympux | 2:18f10e7209f4 | 34 | /* | 
| olympux | 2:18f10e7209f4 | 35 | * Hardware defines | 
| olympux | 0:c2eac797face | 36 | */ | 
| olympux | 9:d2534ecf88c6 | 37 | // Ethernet | 
| olympux | 0:c2eac797face | 38 | SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk | 
| olympux | 11:709f90a3b599 | 39 | EthernetInterface eth(&spi, PA_4, PC_9); // spi, cs, reset | 
| olympux | 30:15e23257e786 | 40 | |
| olympux | 30:15e23257e786 | 41 | /* | 
| olympux | 30:15e23257e786 | 42 | * EEPROM section | 
| olympux | 30:15e23257e786 | 43 | */ | 
| olympux | 30:15e23257e786 | 44 | // Virtual address defined by the user: 0xFFFF value is prohibited | 
| olympux | 30:15e23257e786 | 45 | uint16_t VirtAddVarTab[NumbOfVar] = {0x1212, 0x1313, 0x1414, 0x1515, // IP_Addr | 
| olympux | 30:15e23257e786 | 46 | 0x2212, 0x2313, 0x2414, 0x2515, // IP_Subnet | 
| olympux | 30:15e23257e786 | 47 | 0x3212, 0x3313, 0x3414, 0x3515, // IP_Gateway | 
| olympux | 30:15e23257e786 | 48 | 0x4212, // TCP server port, not used | 
| olympux | 30:15e23257e786 | 49 | 0x5212, // UDP server port, not used | 
| olympux | 30:15e23257e786 | 50 | 0x8888, // 1st run? 0xA5A5 = configured | 
| olympux | 30:15e23257e786 | 51 | 0x6212, 0x6313, 0x6414, // MAC | 
| olympux | 31:2e4b6de6c2f3 | 52 | |
| olympux | 30:15e23257e786 | 53 | // this section is for the TCP server that this device connects to in TCP client mode | 
| olympux | 30:15e23257e786 | 54 | 0x7212, 0x7313, // 0xA5A5 = auto transmit status, time period | 
| olympux | 30:15e23257e786 | 55 | 0x8212, 0x8313,0x8414, 0x8515, // TCP server IP address | 
| olympux | 30:15e23257e786 | 56 | 0x9212, // TCP server port | 
| olympux | 31:2e4b6de6c2f3 | 57 | |
| olympux | 30:15e23257e786 | 58 | // this section is for selecting protocol, not used | 
| olympux | 30:15e23257e786 | 59 | 0xA212, // 0xA5A5 = enable TCP server | 
| olympux | 30:15e23257e786 | 60 | 0xA313, // 0xA5A5 = eanble TCP client | 
| olympux | 30:15e23257e786 | 61 | 0xA414 // 0xA5A5 = enable UDP server | 
| olympux | 31:2e4b6de6c2f3 | 62 | }; | 
| olympux | 31:2e4b6de6c2f3 | 63 | |
| olympux | 30:15e23257e786 | 64 | |
| olympux | 30:15e23257e786 | 65 | /* | 
| olympux | 30:15e23257e786 | 66 | * Network configuration | 
| olympux | 30:15e23257e786 | 67 | */ | 
| olympux | 30:15e23257e786 | 68 | #define TCP_SERVER | 
| olympux | 30:15e23257e786 | 69 | #define TCP_CLIENT | 
| olympux | 30:15e23257e786 | 70 | #define UDP_SERVER | 
| olympux | 30:15e23257e786 | 71 | //#define UDP_CLIENT | 
| olympux | 30:15e23257e786 | 72 | #define NTP | 
| olympux | 30:15e23257e786 | 73 | |
| olympux | 30:15e23257e786 | 74 | #define TCP_SERVER_WAIT_CLIENT_TIMEOUT 200 // timeout for local tcp server wait for a remote client | 
| olympux | 30:15e23257e786 | 75 | #define TCP_SERVER_RECEIVE_TIMEOUT 2000 // timeout for local tcp server wait to receive from remote client | 
| olympux | 30:15e23257e786 | 76 | #define TCP_CLIENT_RECEIVE_TIMEOUT 200 // timeout for local tcp client try to connect remote server | 
| olympux | 30:15e23257e786 | 77 | #define UDP_SERVER_RECEIVE_TIMEOUT 100 // timeout for checking config command | 
| olympux | 30:15e23257e786 | 78 | |
| olympux | 0:c2eac797face | 79 | |
| olympux | 30:15e23257e786 | 80 | // TCP server function | 
| olympux | 30:15e23257e786 | 81 | TCPSocketServer tcp_server; | 
| olympux | 30:15e23257e786 | 82 | TCPSocketConnection tcp_client; | 
| olympux | 30:15e23257e786 | 83 | // TCP client function | 
| olympux | 30:15e23257e786 | 84 | TCPSocketConnection tcp_sock; | 
| olympux | 30:15e23257e786 | 85 | // UDP server | 
| olympux | 30:15e23257e786 | 86 | UDPSocket udp_server; | 
| olympux | 30:15e23257e786 | 87 | Endpoint ep_udp_client; | 
| olympux | 30:15e23257e786 | 88 | // NTP | 
| olympux | 30:15e23257e786 | 89 | NTPClient ntp; | 
| olympux | 30:15e23257e786 | 90 | |
| olympux | 30:15e23257e786 | 91 | |
| olympux | 30:15e23257e786 | 92 | /* | 
| olympux | 30:15e23257e786 | 93 | * Variables for network configuration, TCP server | 
| olympux | 30:15e23257e786 | 94 | */ | 
| olympux | 30:15e23257e786 | 95 | uint8_t u8mac[6], u8ip_addr[4];// keep mac and ip address in 8-bits | 
| olympux | 30:15e23257e786 | 96 | uint16_t u16mac_addr[3], u16ip_addr[4], u16ip_subnet[4], u16ip_gateway[4]; // 16-bits, directly loaded from eeprom | 
| olympux | 30:15e23257e786 | 97 | char str_ip_addr[16], str_ip_subnet[16], str_ip_gateway[16]; // for printf, converted from 16-bits u16ip_xxx | 
| olympux | 30:15e23257e786 | 98 | uint16_t configured_ip = 0; // static ip configured flag | 
| olympux | 30:15e23257e786 | 99 | |
| olympux | 30:15e23257e786 | 100 | const uint16_t tcp_server_local_port = 10000; // fixed, change to 7000 if internet required | 
| olympux | 30:15e23257e786 | 101 | const uint16_t udp_server_local_port = 11000; // fixed | 
| olympux | 30:15e23257e786 | 102 | |
| olympux | 30:15e23257e786 | 103 | // TCP client: this section is used for the TCP server that this device connects to in TCP client mode | 
| olympux | 30:15e23257e786 | 104 | // this device will transmit status every transmit_time_period | 
| olympux | 30:15e23257e786 | 105 | uint16_t auto_transmit_flag = 0, transmit_time_period = 1000; // auto transmit status, time period = 1s | 
| olympux | 30:15e23257e786 | 106 | uint16_t u16server_ip_addr[4]; // directly loaded from eeprom | 
| olympux | 30:15e23257e786 | 107 | uint8_t u8server_ip_addr[4]; // server ip address in 8-bits | 
| olympux | 30:15e23257e786 | 108 | char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr | 
| olympux | 30:15e23257e786 | 109 | uint16_t u16tcp_server_port; // directly loaded from eeprom | 
| olympux | 30:15e23257e786 | 110 | uint16_t u16enable_tcp_client, u16enable_tcp_server;// flags for enabling TCP client or TCP server | 
| olympux | 30:15e23257e786 | 111 | |
| olympux | 30:15e23257e786 | 112 | #define NET_BUF_LEN 256 | 
| olympux | 30:15e23257e786 | 113 | char tcp_receiving_buffer[NET_BUF_LEN]; | 
| olympux | 30:15e23257e786 | 114 | char udp_receiving_buffer[NET_BUF_LEN]; | 
| olympux | 31:2e4b6de6c2f3 | 115 | char network_output_buffer[NET_BUF_LEN]; // output buffer for TCP/UDP control command | 
| olympux | 30:15e23257e786 | 116 | |
| olympux | 30:15e23257e786 | 117 | |
| olympux | 30:15e23257e786 | 118 | /* | 
| olympux | 30:15e23257e786 | 119 | * RPC Protocol | 
| olympux | 30:15e23257e786 | 120 | * Use the RPC enabled wrapped class - see RpcClasses.h for more info | 
| olympux | 30:15e23257e786 | 121 | */ | 
| olympux | 27:22f289beceb8 | 122 | // DigitalIn | 
| olympux | 27:22f289beceb8 | 123 | RpcDigitalIn di0(PB_14, "din0"); | 
| olympux | 27:22f289beceb8 | 124 | RpcDigitalIn di1(PB_12, "din1"); | 
| olympux | 27:22f289beceb8 | 125 | RpcDigitalIn di2(PB_10, "din2"); | 
| olympux | 27:22f289beceb8 | 126 | RpcDigitalIn di3(PB_1, "din3"); | 
| olympux | 27:22f289beceb8 | 127 | RpcDigitalIn di4(PB_15, "din4"); | 
| olympux | 27:22f289beceb8 | 128 | RpcDigitalIn di5(PB_13, "din5"); | 
| olympux | 27:22f289beceb8 | 129 | RpcDigitalIn di6(PB_11, "din6"); | 
| olympux | 27:22f289beceb8 | 130 | RpcDigitalIn di7(PB_2, "din7"); | 
| olympux | 11:709f90a3b599 | 131 | DigitalIn din0(PB_14); | 
| olympux | 11:709f90a3b599 | 132 | DigitalIn din1(PB_12); | 
| olympux | 11:709f90a3b599 | 133 | DigitalIn din2(PB_10); | 
| olympux | 11:709f90a3b599 | 134 | DigitalIn din3(PB_1); | 
| olympux | 11:709f90a3b599 | 135 | DigitalIn din4(PB_15); | 
| olympux | 11:709f90a3b599 | 136 | DigitalIn din5(PB_13); | 
| olympux | 11:709f90a3b599 | 137 | DigitalIn din6(PB_11); | 
| olympux | 11:709f90a3b599 | 138 | DigitalIn din7(PB_2); | 
| olympux | 27:22f289beceb8 | 139 | // DigitalOut | 
| olympux | 27:22f289beceb8 | 140 | RpcDigitalOut do0(PB_3, "dout0"); | 
| olympux | 27:22f289beceb8 | 141 | RpcDigitalOut do1(PB_5, "dout1"); | 
| olympux | 27:22f289beceb8 | 142 | RpcDigitalOut do2(PB_7, "dout2"); | 
| olympux | 27:22f289beceb8 | 143 | RpcDigitalOut do3(PB_9, "dout3"); | 
| olympux | 27:22f289beceb8 | 144 | RpcDigitalOut do4(PD_2, "dout4"); | 
| olympux | 27:22f289beceb8 | 145 | RpcDigitalOut do5(PB_4, "dout5"); | 
| olympux | 27:22f289beceb8 | 146 | RpcDigitalOut do6(PB_6, "dout6"); | 
| olympux | 27:22f289beceb8 | 147 | RpcDigitalOut do7(PB_8, "dout7"); | 
| olympux | 11:709f90a3b599 | 148 | DigitalOut dout0(PB_3); | 
| olympux | 11:709f90a3b599 | 149 | DigitalOut dout1(PB_5); | 
| olympux | 11:709f90a3b599 | 150 | DigitalOut dout2(PB_7); | 
| olympux | 11:709f90a3b599 | 151 | DigitalOut dout3(PB_9); | 
| olympux | 11:709f90a3b599 | 152 | DigitalOut dout4(PD_2); | 
| olympux | 11:709f90a3b599 | 153 | DigitalOut dout5(PB_4); | 
| olympux | 11:709f90a3b599 | 154 | DigitalOut dout6(PB_6); | 
| olympux | 11:709f90a3b599 | 155 | DigitalOut dout7(PB_8); | 
| olympux | 27:22f289beceb8 | 156 | // AnalogIn | 
| olympux | 27:22f289beceb8 | 157 | RpcAnalogIn adc10(PC_0, "ain0"); // adc10 | 
| olympux | 27:22f289beceb8 | 158 | RpcAnalogIn adc11(PC_1, "ain1"); // adc11 | 
| olympux | 11:709f90a3b599 | 159 | AnalogIn ain0(PC_0); | 
| olympux | 11:709f90a3b599 | 160 | AnalogIn ain1(PC_1); | 
| olympux | 27:22f289beceb8 | 161 | // AnalogOut, PWM | 
| olympux | 27:22f289beceb8 | 162 | RpcPwmOut pwm11(PA_8, "pwm0"); // pwm11 | 
| olympux | 27:22f289beceb8 | 163 | RpcPwmOut pwm21(PA_15, "pwm1"); // pwm21 | 
| olympux | 27:22f289beceb8 | 164 | // Serial | 
| olympux | 27:22f289beceb8 | 165 | RpcSerial usart2(USBTX, USBRX, "uart"); // usart2 | 
| olympux | 27:22f289beceb8 | 166 | Serial uart(USBTX,USBRX); | 
| olympux | 27:22f289beceb8 | 167 | // Timer | 
| olympux | 27:22f289beceb8 | 168 | RpcTimer timer1("timer1"); | 
| olympux | 20:71c7950fdd91 | 169 | // Watchdog | 
| olympux | 20:71c7950fdd91 | 170 | Watchdog wdt; | 
| olympux | 20:71c7950fdd91 | 171 | |
| olympux | 20:71c7950fdd91 | 172 | |
| olympux | 13:bcf840da68fd | 173 | /* | 
| olympux | 30:15e23257e786 | 174 | * NNIO Protocol | 
| olympux | 30:15e23257e786 | 175 | */ | 
| olympux | 9:d2534ecf88c6 | 176 | // Commands | 
| olympux | 29:bc052f283ada | 177 | #define DEVICE_DESCRIPTION "NNIO" | 
| olympux | 27:22f289beceb8 | 178 | #define DEVICE_CONFIG_CODE "NNCF" | 
| olympux | 27:22f289beceb8 | 179 | #define DEVICE_CONTROL_CODE "NNIO" | 
| olympux | 17:88ef7a078095 | 180 | |
| olympux | 17:88ef7a078095 | 181 | #define RECEIVING_PROTOCOL_LENGTH 58 | 
| olympux | 17:88ef7a078095 | 182 | #define SENDING_PROTOCOL_LENGTH 39 | 
| olympux | 26:09e0dd020900 | 183 | #define QUERY_NETWORK_CONFIG_CMD_LENGTH 6 | 
| olympux | 17:88ef7a078095 | 184 | #define SET_NETWORK_CONFIG_CMD_LENGTH 19 | 
| olympux | 26:09e0dd020900 | 185 | #define UPDATE_TCP_SERVER_INFO_COMMAND_LENGTH 12 | 
| olympux | 17:88ef7a078095 | 186 | |
| olympux | 27:22f289beceb8 | 187 | #define QUERY_DISCOVERY_CMD "NNCFDS" | 
| olympux | 27:22f289beceb8 | 188 | #define QUERY_IP_CMD "NNCFIP" | 
| olympux | 27:22f289beceb8 | 189 | #define QUERY_SUBNET_CMD "NNCFSN" | 
| olympux | 27:22f289beceb8 | 190 | #define QUERY_GATEWAY_CMD "NNCFGW" | 
| olympux | 27:22f289beceb8 | 191 | #define QUERY_MAC_CMD "NNCFMC" | 
| olympux | 27:22f289beceb8 | 192 | #define QUERY_UDP_PORT_CMD "NNCFUP" | 
| olympux | 27:22f289beceb8 | 193 | #define QUERY_TCP_PORT_CMD "NNCFTP" | 
| olympux | 27:22f289beceb8 | 194 | #define QUERY_UPDATE_TIME_CMD "NNCFTM" | 
| olympux | 11:709f90a3b599 | 195 | #define RECEIVING_PROTOCOL_ENABLE_OUTPUT 'O' | 
| olympux | 11:709f90a3b599 | 196 | #define QUERY_STATUS_COMMAND 'Q' | 
| olympux | 11:709f90a3b599 | 197 | #define DIGITAL_HIGH 'H' | 
| olympux | 11:709f90a3b599 | 198 | #define DIGITAL_LOW 'L' | 
| olympux | 9:d2534ecf88c6 | 199 | |
| olympux | 9:d2534ecf88c6 | 200 | |
| olympux | 9:d2534ecf88c6 | 201 | // Positions | 
| olympux | 11:709f90a3b599 | 202 | #define RECEIVING_PROTOCOL_ID_POS 0 | 
| olympux | 9:d2534ecf88c6 | 203 | #define RECEIVING_PROTOCOL_OP_POS 4 | 
| olympux | 9:d2534ecf88c6 | 204 | #define RECEIVING_PROTOCOL_EN_DO_POS RECEIVING_PROTOCOL_OP_POS + 0 | 
| olympux | 9:d2534ecf88c6 | 205 | #define RECEIVING_PROTOCOL_EN_A0O_POS RECEIVING_PROTOCOL_OP_POS + 1 | 
| olympux | 9:d2534ecf88c6 | 206 | #define RECEIVING_PROTOCOL_EN_A1O_POS RECEIVING_PROTOCOL_OP_POS + 2 | 
| olympux | 9:d2534ecf88c6 | 207 | #define RECEIVING_PROTOCOL_EN_UART_POS RECEIVING_PROTOCOL_OP_POS + 3 | 
| olympux | 9:d2534ecf88c6 | 208 | #define RECEIVING_PROTOCOL_COMMAND_POS RECEIVING_PROTOCOL_OP_POS + 4 | 
| olympux | 11:709f90a3b599 | 209 | |
| olympux | 11:709f90a3b599 | 210 | #define RECEIVING_PROTOCOL_IP_POS 9 | 
| olympux | 11:709f90a3b599 | 211 | #define RECEIVING_PROTOCOL_DO_POS 13 | 
| olympux | 11:709f90a3b599 | 212 | #define RECEIVING_PROTOCOL_A0O_POS 21 | 
| olympux | 11:709f90a3b599 | 213 | #define RECEIVING_PROTOCOL_A01_POS 23 | 
| olympux | 11:709f90a3b599 | 214 | #define RECEIVING_PROTOCOL_UART_POS 25 | 
| olympux | 9:d2534ecf88c6 | 215 | |
| olympux | 9:d2534ecf88c6 | 216 | |
| olympux | 11:709f90a3b599 | 217 | #define SENDING_PROTOCOL_ID_POS 0 | 
| olympux | 11:709f90a3b599 | 218 | #define SENDING_PROTOCOL_MAC_POS 4 | 
| olympux | 11:709f90a3b599 | 219 | #define SENDING_PROTOCOL_IP_POS 10 | 
| olympux | 11:709f90a3b599 | 220 | #define SENDING_PROTOCOL_DI_POS 14 | 
| olympux | 11:709f90a3b599 | 221 | #define SENDING_PROTOCOL_DO_POS 22 | 
| olympux | 11:709f90a3b599 | 222 | #define SENDING_PROTOCOL_AI0_POS 30 | 
| olympux | 11:709f90a3b599 | 223 | #define SENDING_PROTOCOL_AI1_POS 32 | 
| olympux | 11:709f90a3b599 | 224 | #define SENDING_PROTOCOL_AO0_POS 34 | 
| olympux | 11:709f90a3b599 | 225 | #define SENDING_PROTOCOL_AO1_POS 36 | 
| olympux | 11:709f90a3b599 | 226 | #define SENDING_PROTOCOL_CR_POS 38 | 
| olympux | 0:c2eac797face | 227 | |
| olympux | 2:18f10e7209f4 | 228 | |
| olympux | 2:18f10e7209f4 | 229 | /* | 
| olympux | 2:18f10e7209f4 | 230 | * RTOS | 
| olympux | 2:18f10e7209f4 | 231 | */ | 
| olympux | 2:18f10e7209f4 | 232 | struct message_t { | 
| olympux | 2:18f10e7209f4 | 233 | int len; | 
| olympux | 2:18f10e7209f4 | 234 | char *msg; | 
| olympux | 2:18f10e7209f4 | 235 | }; | 
| olympux | 3:972ed747474c | 236 | Queue<message_t, 16> uart_queue; | 
| olympux | 15:edeb0aed160d | 237 | Queue<bool, 1> auto_update_queue; | 
| olympux | 2:18f10e7209f4 | 238 | |
| olympux | 2:18f10e7209f4 | 239 | |
| olympux | 26:09e0dd020900 | 240 | |
| olympux | 26:09e0dd020900 | 241 | // Prototypes | 
| olympux | 26:09e0dd020900 | 242 | int ethernet_init(void); | 
| olympux | 28:00c0c20d03c1 | 243 | int process_control_command(char* received_buffer, int len); | 
| olympux | 26:09e0dd020900 | 244 | void process_config_command(char* received_buffer, int len); | 
| olympux | 26:09e0dd020900 | 245 | void update_digital_outputs(char* buf); | 
| olympux | 26:09e0dd020900 | 246 | void update_sending_frame(char* buf); | 
| olympux | 26:09e0dd020900 | 247 | |
| olympux | 26:09e0dd020900 | 248 | |
| olympux | 2:18f10e7209f4 | 249 | /* | 
| olympux | 2:18f10e7209f4 | 250 | * Threads | 
| olympux | 2:18f10e7209f4 | 251 | */ | 
| olympux | 31:2e4b6de6c2f3 | 252 | // Timer thread for auto update in TCP client function | 
| olympux | 31:2e4b6de6c2f3 | 253 | void auto_update_timer_thread(void const* args) | 
| olympux | 31:2e4b6de6c2f3 | 254 | { | 
| olympux | 18:ca499a2e7da6 | 255 | bool update_flag = true; | 
| olympux | 31:2e4b6de6c2f3 | 256 | |
| olympux | 15:edeb0aed160d | 257 | Thread::wait(500); | 
| olympux | 15:edeb0aed160d | 258 | while(true) { | 
| olympux | 15:edeb0aed160d | 259 | auto_update_queue.put(&update_flag); | 
| olympux | 18:ca499a2e7da6 | 260 | Thread::wait(1000*transmit_time_period); // Thread::wait() in ms | 
| olympux | 15:edeb0aed160d | 261 | } | 
| olympux | 15:edeb0aed160d | 262 | } | 
| olympux | 15:edeb0aed160d | 263 | |
| olympux | 15:edeb0aed160d | 264 | |
| olympux | 20:71c7950fdd91 | 265 | // WDT reset | 
| olympux | 31:2e4b6de6c2f3 | 266 | void wdt_reset_thread(void const* args) | 
| olympux | 31:2e4b6de6c2f3 | 267 | { | 
| olympux | 20:71c7950fdd91 | 268 | while (true) | 
| olympux | 20:71c7950fdd91 | 269 | wdt.Service(); | 
| olympux | 20:71c7950fdd91 | 270 | } | 
| olympux | 20:71c7950fdd91 | 271 | |
| olympux | 20:71c7950fdd91 | 272 | |
| olympux | 4:568c97f2a407 | 273 | int main() | 
| olympux | 4:568c97f2a407 | 274 | { | 
| olympux | 9:d2534ecf88c6 | 275 | int n, ret; | 
| olympux | 31:2e4b6de6c2f3 | 276 | |
| olympux | 20:71c7950fdd91 | 277 | Thread::wait(500); // turn on delay | 
| olympux | 31:2e4b6de6c2f3 | 278 | |
| olympux | 4:568c97f2a407 | 279 | /* | 
| olympux | 9:d2534ecf88c6 | 280 | * Configure | 
| olympux | 4:568c97f2a407 | 281 | */ | 
| olympux | 11:709f90a3b599 | 282 | uart.baud(115200); | 
| olympux | 18:ca499a2e7da6 | 283 | DBG("\r\nStarting..."); | 
| olympux | 31:2e4b6de6c2f3 | 284 | |
| olympux | 31:2e4b6de6c2f3 | 285 | // check watchdog | 
| olympux | 20:71c7950fdd91 | 286 | if (wdt.WatchdogCausedReset()) | 
| olympux | 20:71c7950fdd91 | 287 | DBG("Watchdog caused reset."); | 
| olympux | 20:71c7950fdd91 | 288 | wdt.Configure(4); | 
| olympux | 31:2e4b6de6c2f3 | 289 | |
| olympux | 6:d054e394fba3 | 290 | /* | 
| olympux | 6:d054e394fba3 | 291 | * FLASH | 
| olympux | 6:d054e394fba3 | 292 | */ | 
| olympux | 12:7c152c0ca4d8 | 293 | load_eeprom_network(); | 
| olympux | 15:edeb0aed160d | 294 | load_eeprom_tcpserver(); | 
| olympux | 31:2e4b6de6c2f3 | 295 | |
| olympux | 20:71c7950fdd91 | 296 | /* | 
| olympux | 20:71c7950fdd91 | 297 | * UI threads | 
| olympux | 20:71c7950fdd91 | 298 | */ | 
| olympux | 33:c906ccc220ba | 299 | Thread t2(auto_update_timer_thread); | 
| olympux | 20:71c7950fdd91 | 300 | Thread t3(wdt_reset_thread); | 
| olympux | 31:2e4b6de6c2f3 | 301 | |
| olympux | 4:568c97f2a407 | 302 | /* | 
| olympux | 4:568c97f2a407 | 303 | * Ethernet | 
| olympux | 4:568c97f2a407 | 304 | */ | 
| olympux | 6:d054e394fba3 | 305 | ret = ethernet_init(); | 
| olympux | 4:568c97f2a407 | 306 | if (ret) { | 
| olympux | 18:ca499a2e7da6 | 307 | ERR("Ethernet initialisation failed. App halted."); | 
| olympux | 4:568c97f2a407 | 308 | while (true) {}; | 
| olympux | 4:568c97f2a407 | 309 | } | 
| olympux | 31:2e4b6de6c2f3 | 310 | |
| olympux | 23:47ee805435b1 | 311 | Thread::wait(2000); // TCP/UDP stack delay | 
| olympux | 12:7c152c0ca4d8 | 312 | |
| olympux | 31:2e4b6de6c2f3 | 313 | /* | 
| olympux | 31:2e4b6de6c2f3 | 314 | * UDP server | 
| olympux | 31:2e4b6de6c2f3 | 315 | * TCP server/client | 
| olympux | 31:2e4b6de6c2f3 | 316 | */ | 
| olympux | 20:71c7950fdd91 | 317 | #ifdef UDP_SERVER | 
| olympux | 20:71c7950fdd91 | 318 | ret = udp_server.bind(udp_server_local_port); | 
| olympux | 20:71c7950fdd91 | 319 | DBG("UDP server started (sock.bind = %d)...", ret); | 
| olympux | 20:71c7950fdd91 | 320 | udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT); | 
| olympux | 20:71c7950fdd91 | 321 | #endif | 
| olympux | 20:71c7950fdd91 | 322 | |
| olympux | 3:972ed747474c | 323 | #ifdef TCP_SERVER | 
| olympux | 12:7c152c0ca4d8 | 324 | tcp_server.bind(tcp_server_local_port); | 
| olympux | 3:972ed747474c | 325 | tcp_server.listen(); | 
| olympux | 18:ca499a2e7da6 | 326 | DBG("TCP server started..."); | 
| olympux | 4:568c97f2a407 | 327 | tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT); | 
| olympux | 3:972ed747474c | 328 | #endif | 
| olympux | 14:18eda020a589 | 329 | |
| olympux | 14:18eda020a589 | 330 | #ifdef TCP_CLIENT | 
| olympux | 18:ca499a2e7da6 | 331 | |
| olympux | 14:18eda020a589 | 332 | #endif | 
| olympux | 31:2e4b6de6c2f3 | 333 | |
| olympux | 12:7c152c0ca4d8 | 334 | /* | 
| olympux | 19:05934ee9ee67 | 335 | * Network loop processor | 
| olympux | 12:7c152c0ca4d8 | 336 | */ | 
| olympux | 0:c2eac797face | 337 | while (true) { | 
| olympux | 30:15e23257e786 | 338 | #ifdef TCP_CLIENT // auto update device status to a remote TCP server | 
| olympux | 33:c906ccc220ba | 339 | if (auto_transmit_flag == DEFAULT_ENABLE_FLAG_VALUE) { | 
| olympux | 18:ca499a2e7da6 | 340 | // connect to TCP server if required | 
| olympux | 18:ca499a2e7da6 | 341 | if (!tcp_sock.is_connected()) { | 
| olympux | 20:71c7950fdd91 | 342 | ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port); // timeout is default in connect() in W5500.h | 
| olympux | 18:ca499a2e7da6 | 343 | if (ret > -1) { | 
| olympux | 18:ca499a2e7da6 | 344 | DBG("Successfully connected to %s on port %d", str_server_ip_addr, u16tcp_server_port); | 
| olympux | 31:2e4b6de6c2f3 | 345 | } else { | 
| olympux | 18:ca499a2e7da6 | 346 | ERR("Unable to connect to %s on port %d", str_server_ip_addr, u16tcp_server_port); | 
| olympux | 18:ca499a2e7da6 | 347 | } | 
| olympux | 16:84a5bf7285d0 | 348 | } | 
| olympux | 31:2e4b6de6c2f3 | 349 | |
| olympux | 18:ca499a2e7da6 | 350 | // transmit data if connected | 
| olympux | 18:ca499a2e7da6 | 351 | if (tcp_sock.is_connected()) { | 
| olympux | 18:ca499a2e7da6 | 352 | osEvent evt = auto_update_queue.get(1); // timeout after 1ms | 
| olympux | 18:ca499a2e7da6 | 353 | if (evt.status == osEventMessage) { | 
| olympux | 18:ca499a2e7da6 | 354 | DBG("Updating..."); | 
| olympux | 31:2e4b6de6c2f3 | 355 | update_sending_frame(network_output_buffer); | 
| olympux | 31:2e4b6de6c2f3 | 356 | tcp_sock.send_all(network_output_buffer, SENDING_PROTOCOL_LENGTH); | 
| olympux | 19:05934ee9ee67 | 357 | } | 
| olympux | 31:2e4b6de6c2f3 | 358 | |
| olympux | 19:05934ee9ee67 | 359 | // check to receive or timeout | 
| olympux | 19:05934ee9ee67 | 360 | tcp_sock.set_blocking(false, TCP_CLIENT_RECEIVE_TIMEOUT); | 
| olympux | 19:05934ee9ee67 | 361 | n = tcp_sock.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer)); | 
| olympux | 19:05934ee9ee67 | 362 | if (n > 0) { | 
| olympux | 19:05934ee9ee67 | 363 | // got some data, test it | 
| olympux | 19:05934ee9ee67 | 364 | DBG("TCP client received %d bytes: %s", n, tcp_receiving_buffer); | 
| olympux | 32:db2e8ea06ee1 | 365 | n = process_control_command(tcp_receiving_buffer, n); | 
| olympux | 32:db2e8ea06ee1 | 366 | // send reply back to client, NNIO protocol always returns 0 | 
| olympux | 32:db2e8ea06ee1 | 367 | // RPC-style protocol | 
| olympux | 32:db2e8ea06ee1 | 368 | if (n > 0) { | 
| olympux | 32:db2e8ea06ee1 | 369 | network_output_buffer[n] = '\0'; | 
| olympux | 32:db2e8ea06ee1 | 370 | tcp_sock.send_all(network_output_buffer, strlen(network_output_buffer)); | 
| olympux | 32:db2e8ea06ee1 | 371 | } // RPC-style protocol | 
| olympux | 32:db2e8ea06ee1 | 372 | else if (n == 0) { | 
| olympux | 32:db2e8ea06ee1 | 373 | // then, check query status command and sending protocol if required | 
| olympux | 32:db2e8ea06ee1 | 374 | if (tcp_receiving_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) { | 
| olympux | 32:db2e8ea06ee1 | 375 | DBG("Requested to send device status through TCP"); | 
| olympux | 32:db2e8ea06ee1 | 376 | // sending protocol | 
| olympux | 32:db2e8ea06ee1 | 377 | update_sending_frame(network_output_buffer); | 
| olympux | 32:db2e8ea06ee1 | 378 | tcp_sock.send_all(network_output_buffer, SENDING_PROTOCOL_LENGTH); | 
| olympux | 32:db2e8ea06ee1 | 379 | DBG("Sent"); | 
| olympux | 32:db2e8ea06ee1 | 380 | } | 
| olympux | 32:db2e8ea06ee1 | 381 | } // NNIO protocol | 
| olympux | 18:ca499a2e7da6 | 382 | } | 
| olympux | 16:84a5bf7285d0 | 383 | } | 
| olympux | 18:ca499a2e7da6 | 384 | } // if tcp client enabled && auto transmit | 
| olympux | 15:edeb0aed160d | 385 | #endif | 
| olympux | 15:edeb0aed160d | 386 | |
| olympux | 15:edeb0aed160d | 387 | |
| olympux | 30:15e23257e786 | 388 | #ifdef TCP_SERVER // control and monitor from a remote TCP client, both NNIO and RPC-style | 
| olympux | 30:15e23257e786 | 389 | // no tcp client connected{ | 
| olympux | 20:71c7950fdd91 | 390 | if (1) { | 
| olympux | 4:568c97f2a407 | 391 | // wait for client within timeout | 
| olympux | 4:568c97f2a407 | 392 | ret = tcp_server.accept(tcp_client); | 
| olympux | 31:2e4b6de6c2f3 | 393 | |
| olympux | 4:568c97f2a407 | 394 | // tcp client connected | 
| olympux | 4:568c97f2a407 | 395 | if (ret > -1) { | 
| olympux | 18:ca499a2e7da6 | 396 | DBG("Connection from: %s", tcp_client.get_address()); | 
| olympux | 31:2e4b6de6c2f3 | 397 | |
| olympux | 4:568c97f2a407 | 398 | // loop waiting and receiving data within timeout | 
| olympux | 4:568c97f2a407 | 399 | tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds | 
| olympux | 31:2e4b6de6c2f3 | 400 | while (tcp_client.is_connected()) { | 
| olympux | 19:05934ee9ee67 | 401 | n = tcp_client.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer)); | 
| olympux | 4:568c97f2a407 | 402 | if (n <= 0) break; | 
| olympux | 31:2e4b6de6c2f3 | 403 | |
| olympux | 28:00c0c20d03c1 | 404 | // got some data, process it | 
| olympux | 27:22f289beceb8 | 405 | tcp_receiving_buffer[n] = '\0'; // for debugging purpose | 
| olympux | 19:05934ee9ee67 | 406 | DBG("TCP server received: %s", tcp_receiving_buffer); | 
| olympux | 28:00c0c20d03c1 | 407 | n = process_control_command(tcp_receiving_buffer, n); | 
| olympux | 31:2e4b6de6c2f3 | 408 | // send reply back to client, NNIO protocol always returns 0 | 
| olympux | 31:2e4b6de6c2f3 | 409 | // RPC-style protocol | 
| olympux | 28:00c0c20d03c1 | 410 | if (n > 0) { | 
| olympux | 31:2e4b6de6c2f3 | 411 | network_output_buffer[n] = '\0'; | 
| olympux | 31:2e4b6de6c2f3 | 412 | tcp_client.send_all(network_output_buffer, strlen(network_output_buffer)); | 
| olympux | 31:2e4b6de6c2f3 | 413 | } // RPC-style protocol | 
| olympux | 31:2e4b6de6c2f3 | 414 | else if (n == 0) { | 
| olympux | 31:2e4b6de6c2f3 | 415 | // then, check query status command and sending protocol if required | 
| olympux | 31:2e4b6de6c2f3 | 416 | if (tcp_receiving_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) { | 
| olympux | 31:2e4b6de6c2f3 | 417 | DBG("Requested to send device status through TCP"); | 
| olympux | 31:2e4b6de6c2f3 | 418 | // sending protocol | 
| olympux | 31:2e4b6de6c2f3 | 419 | update_sending_frame(network_output_buffer); | 
| olympux | 31:2e4b6de6c2f3 | 420 | tcp_client.send_all(network_output_buffer, SENDING_PROTOCOL_LENGTH); | 
| olympux | 31:2e4b6de6c2f3 | 421 | DBG("Sent"); | 
| olympux | 31:2e4b6de6c2f3 | 422 | } | 
| olympux | 31:2e4b6de6c2f3 | 423 | } // NNIO protocol | 
| olympux | 4:568c97f2a407 | 424 | } // end loop if no data received within timeout | 
| olympux | 4:568c97f2a407 | 425 | } // if client connected | 
| olympux | 20:71c7950fdd91 | 426 | tcp_client.close(); | 
| olympux | 18:ca499a2e7da6 | 427 | } // if tcp server enabled && no client connected | 
| olympux | 3:972ed747474c | 428 | #endif | 
| olympux | 20:71c7950fdd91 | 429 | |
| olympux | 30:15e23257e786 | 430 | #ifdef UDP_SERVER // configuration and control, both NNIO and RPC-style | 
| olympux | 27:22f289beceb8 | 431 | bool discovery_mode_flag, config_mode_flag; | 
| olympux | 31:2e4b6de6c2f3 | 432 | |
| olympux | 19:05934ee9ee67 | 433 | n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer)); | 
| olympux | 31:2e4b6de6c2f3 | 434 | |
| olympux | 25:48dd18cc147c | 435 | // check to see if it is a query command | 
| olympux | 25:48dd18cc147c | 436 | // if yes, is it a discovery command or an ip query to enter config mode | 
| olympux | 27:22f289beceb8 | 437 | discovery_mode_flag = false; | 
| olympux | 25:48dd18cc147c | 438 | config_mode_flag = false; | 
| olympux | 26:09e0dd020900 | 439 | if ((n == QUERY_NETWORK_CONFIG_CMD_LENGTH) && (strstr(udp_receiving_buffer, QUERY_DISCOVERY_CMD) != NULL)) { | 
| olympux | 27:22f289beceb8 | 440 | discovery_mode_flag = true; | 
| olympux | 25:48dd18cc147c | 441 | DBG("Received discovery command"); | 
| olympux | 25:48dd18cc147c | 442 | char str[50]; | 
| olympux | 29:bc052f283ada | 443 | sprintf(str, "%s%s%s", DEVICE_DESCRIPTION, eth.getMACAddress(), eth.getIPAddress()); | 
| olympux | 25:48dd18cc147c | 444 | udp_server.sendTo(ep_udp_client, str, strlen(str)); | 
| olympux | 27:22f289beceb8 | 445 | } // NNCFDS | 
| olympux | 26:09e0dd020900 | 446 | else if ((n == QUERY_NETWORK_CONFIG_CMD_LENGTH) && (strstr(udp_receiving_buffer, QUERY_IP_CMD) != NULL)) { | 
| olympux | 25:48dd18cc147c | 447 | config_mode_flag = true; | 
| olympux | 27:22f289beceb8 | 448 | DBG("Entered configuration mode..."); | 
| olympux | 20:71c7950fdd91 | 449 | DBG("!!! RESET when finished"); | 
| olympux | 27:22f289beceb8 | 450 | } // NNCFIP | 
| olympux | 31:2e4b6de6c2f3 | 451 | |
| olympux | 27:22f289beceb8 | 452 | // if received NNCFIP, enter config mode | 
| olympux | 25:48dd18cc147c | 453 | if (config_mode_flag) { | 
| olympux | 31:2e4b6de6c2f3 | 454 | while (n > 0) { | 
| olympux | 20:71c7950fdd91 | 455 | // got some data, test it | 
| olympux | 27:22f289beceb8 | 456 | DBG("UDP received (%s) from (%s:%d)", udp_receiving_buffer, ep_udp_client.get_address(), ep_udp_client.get_port()); | 
| olympux | 26:09e0dd020900 | 457 | process_config_command(udp_receiving_buffer, n); | 
| olympux | 20:71c7950fdd91 | 458 | // wait to receive new config command | 
| olympux | 20:71c7950fdd91 | 459 | udp_server.set_blocking(true); | 
| olympux | 20:71c7950fdd91 | 460 | n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer)); | 
| olympux | 26:09e0dd020900 | 461 | } // while (n > 0), config loop | 
| olympux | 25:48dd18cc147c | 462 | } // if (config_mode_flag) | 
| olympux | 29:bc052f283ada | 463 | // process control packages sent using UDP | 
| olympux | 29:bc052f283ada | 464 | else if ((n > 0) && (!discovery_mode_flag)) { | 
| olympux | 28:00c0c20d03c1 | 465 | n = process_control_command(udp_receiving_buffer, n); | 
| olympux | 28:00c0c20d03c1 | 466 | // send rpc reply back to client, NNIO protocol always returns 0 | 
| olympux | 31:2e4b6de6c2f3 | 467 | // RPC-style protocol | 
| olympux | 28:00c0c20d03c1 | 468 | if (n > 0) { | 
| olympux | 31:2e4b6de6c2f3 | 469 | network_output_buffer[n] = '\0'; | 
| olympux | 31:2e4b6de6c2f3 | 470 | udp_server.sendTo(ep_udp_client, network_output_buffer, strlen(network_output_buffer)); | 
| olympux | 31:2e4b6de6c2f3 | 471 | } // RPC-style protocol | 
| olympux | 31:2e4b6de6c2f3 | 472 | else if (n == 0) { | 
| olympux | 31:2e4b6de6c2f3 | 473 | // then, check query status command and sending protocol if required | 
| olympux | 31:2e4b6de6c2f3 | 474 | if (udp_receiving_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) { | 
| olympux | 31:2e4b6de6c2f3 | 475 | DBG("Requested to send device status through UDP"); | 
| olympux | 31:2e4b6de6c2f3 | 476 | // sending protocol | 
| olympux | 31:2e4b6de6c2f3 | 477 | update_sending_frame(network_output_buffer); | 
| olympux | 31:2e4b6de6c2f3 | 478 | udp_server.sendTo(ep_udp_client, network_output_buffer, SENDING_PROTOCOL_LENGTH); | 
| olympux | 31:2e4b6de6c2f3 | 479 | DBG("Sent"); | 
| olympux | 31:2e4b6de6c2f3 | 480 | } | 
| olympux | 32:db2e8ea06ee1 | 481 | } // NNIO protocol | 
| olympux | 25:48dd18cc147c | 482 | } | 
| olympux | 3:972ed747474c | 483 | #endif | 
| olympux | 4:568c97f2a407 | 484 | } // network processor | 
| olympux | 11:709f90a3b599 | 485 | } | 
| olympux | 11:709f90a3b599 | 486 | |
| olympux | 26:09e0dd020900 | 487 | |
| olympux | 28:00c0c20d03c1 | 488 | /* | 
| olympux | 28:00c0c20d03c1 | 489 | * Process NNCF commands | 
| olympux | 28:00c0c20d03c1 | 490 | */ | 
| olympux | 26:09e0dd020900 | 491 | void process_config_command(char* received_buffer, int len) | 
| olympux | 26:09e0dd020900 | 492 | { | 
| olympux | 27:22f289beceb8 | 493 | DBG("Processing configuration command"); | 
| olympux | 31:2e4b6de6c2f3 | 494 | |
| olympux | 26:09e0dd020900 | 495 | // a configuration command always starts with NN | 
| olympux | 27:22f289beceb8 | 496 | if ((received_buffer[0] == 'N') && (received_buffer[1] == 'N') && | 
| olympux | 31:2e4b6de6c2f3 | 497 | (received_buffer[2] == 'C') && (received_buffer[3] == 'F')) { | 
| olympux | 26:09e0dd020900 | 498 | switch (len) { | 
| olympux | 31:2e4b6de6c2f3 | 499 | // length = 6, a QUERY command (discovery command, TCP port, or UDP port) | 
| olympux | 31:2e4b6de6c2f3 | 500 | // Format: NNCFDS, NNCFTP, NNCFUP, NNCFTM | 
| olympux | 30:15e23257e786 | 501 | case QUERY_NETWORK_CONFIG_CMD_LENGTH: { | 
| olympux | 26:09e0dd020900 | 502 | if (strstr(received_buffer, QUERY_IP_CMD) != NULL) { | 
| olympux | 26:09e0dd020900 | 503 | udp_server.sendTo(ep_udp_client, eth.getIPAddress(), strlen(eth.getIPAddress())); | 
| olympux | 27:22f289beceb8 | 504 | } // NNCFIP | 
| olympux | 26:09e0dd020900 | 505 | else if (strstr(received_buffer, QUERY_SUBNET_CMD) != NULL) { | 
| olympux | 26:09e0dd020900 | 506 | udp_server.sendTo(ep_udp_client, eth.getNetworkMask(), strlen(eth.getNetworkMask())); | 
| olympux | 27:22f289beceb8 | 507 | } // NNCFSN | 
| olympux | 26:09e0dd020900 | 508 | else if (strstr(received_buffer, QUERY_GATEWAY_CMD) != NULL) { | 
| olympux | 26:09e0dd020900 | 509 | udp_server.sendTo(ep_udp_client, eth.getGateway(), strlen(eth.getGateway())); | 
| olympux | 27:22f289beceb8 | 510 | } // NNCFGW | 
| olympux | 26:09e0dd020900 | 511 | else if (strstr(received_buffer, QUERY_MAC_CMD) != NULL) { | 
| olympux | 26:09e0dd020900 | 512 | udp_server.sendTo(ep_udp_client, eth.getMACAddress(), strlen(eth.getMACAddress())); | 
| olympux | 27:22f289beceb8 | 513 | } // NNCFMC | 
| olympux | 26:09e0dd020900 | 514 | // ask for TCP server port | 
| olympux | 26:09e0dd020900 | 515 | else if (strstr(received_buffer, QUERY_TCP_PORT_CMD) != NULL) { | 
| olympux | 26:09e0dd020900 | 516 | char port[5]; | 
| olympux | 26:09e0dd020900 | 517 | sprintf(port, "%5d", tcp_server_local_port); | 
| olympux | 26:09e0dd020900 | 518 | udp_server.sendTo(ep_udp_client, port, strlen(port)); | 
| olympux | 27:22f289beceb8 | 519 | } // NNCFTP | 
| olympux | 26:09e0dd020900 | 520 | // ask for UDP server port | 
| olympux | 26:09e0dd020900 | 521 | else if (strstr(received_buffer, QUERY_UDP_PORT_CMD) != NULL) { | 
| olympux | 26:09e0dd020900 | 522 | char port[5]; | 
| olympux | 26:09e0dd020900 | 523 | sprintf(port, "%5d", udp_server_local_port); | 
| olympux | 26:09e0dd020900 | 524 | udp_server.sendTo(ep_udp_client, port, strlen(port)); | 
| olympux | 27:22f289beceb8 | 525 | } // NNCFUP | 
| olympux | 26:09e0dd020900 | 526 | else if (strstr(received_buffer, QUERY_UPDATE_TIME_CMD) != NULL) { | 
| olympux | 26:09e0dd020900 | 527 | #ifdef NTP | 
| olympux | 26:09e0dd020900 | 528 | char str_time[50]; | 
| olympux | 26:09e0dd020900 | 529 | |
| olympux | 26:09e0dd020900 | 530 | DBG("Trying to update time..."); | 
| olympux | 26:09e0dd020900 | 531 | if (ntp.setTime("0.pool.ntp.org") == 0) { | 
| olympux | 26:09e0dd020900 | 532 | DBG("Set time successfully"); | 
| olympux | 26:09e0dd020900 | 533 | time_t ctTime; | 
| olympux | 26:09e0dd020900 | 534 | ctTime = time(NULL); | 
| olympux | 26:09e0dd020900 | 535 | |
| olympux | 26:09e0dd020900 | 536 | DBG("Time is set to (UTC): %s", ctime(&ctTime)); | 
| olympux | 26:09e0dd020900 | 537 | sprintf(str_time, "%s", ctime(&ctTime)); | 
| olympux | 26:09e0dd020900 | 538 | udp_server.sendTo(ep_udp_client, str_time, strlen(str_time)); | 
| olympux | 26:09e0dd020900 | 539 | } else { | 
| olympux | 26:09e0dd020900 | 540 | WARN("Error"); | 
| olympux | 26:09e0dd020900 | 541 | sprintf(str_time, "ERR"); | 
| olympux | 26:09e0dd020900 | 542 | udp_server.sendTo(ep_udp_client, str_time, strlen(str_time)); | 
| olympux | 26:09e0dd020900 | 543 | } | 
| olympux | 26:09e0dd020900 | 544 | #elif | 
| olympux | 26:09e0dd020900 | 545 | WARN("NTP disabled"); | 
| olympux | 26:09e0dd020900 | 546 | sprintf(str_time, "DIS"); | 
| olympux | 26:09e0dd020900 | 547 | udp_server.sendTo(ep_udp_client, str_time, strlen(str_time)); | 
| olympux | 26:09e0dd020900 | 548 | #endif | 
| olympux | 27:22f289beceb8 | 549 | } // NNCFTM | 
| olympux | 26:09e0dd020900 | 550 | break; | 
| olympux | 30:15e23257e786 | 551 | } | 
| olympux | 30:15e23257e786 | 552 | // length = 19, SET NETWORK CONFIGURATION | 
| olympux | 30:15e23257e786 | 553 | // Format: 4E 4E 43 46 C0 A8 00 78 FF FF FF 00 C0 A8 00 01 00 00 01 | 
| olympux | 30:15e23257e786 | 554 | // (NNCF; IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1) | 
| olympux | 26:09e0dd020900 | 555 | case SET_NETWORK_CONFIG_CMD_LENGTH: { | 
| olympux | 26:09e0dd020900 | 556 | // check device id | 
| olympux | 27:22f289beceb8 | 557 | char* id = strstr(received_buffer, DEVICE_CONFIG_CODE); | 
| olympux | 26:09e0dd020900 | 558 | if (id == NULL) | 
| olympux | 26:09e0dd020900 | 559 | break; | 
| olympux | 26:09e0dd020900 | 560 | else if ((id - received_buffer) > 0) | 
| olympux | 26:09e0dd020900 | 561 | break; | 
| olympux | 26:09e0dd020900 | 562 | |
| olympux | 26:09e0dd020900 | 563 | DBG("Received user configuration"); | 
| olympux | 27:22f289beceb8 | 564 | write_eeprom_network(&received_buffer[strlen(DEVICE_CONFIG_CODE)]); // parameters from 5th char, 15-bytes | 
| olympux | 33:c906ccc220ba | 565 | NVIC_SystemReset(); | 
| olympux | 26:09e0dd020900 | 566 | break; | 
| olympux | 26:09e0dd020900 | 567 | } | 
| olympux | 26:09e0dd020900 | 568 | // length = 12, SET TCP SERVER CONFIGURATION | 
| olympux | 26:09e0dd020900 | 569 | // auto update & its time period, TCP server configuration (IP & port) | 
| olympux | 27:22f289beceb8 | 570 | // Format: 4E 4E 43 46 'Y' 01 C0 A8 00 09 E0 2E (LSB MSB) | 
| olympux | 27:22f289beceb8 | 571 | // NNCF Auto 1s 192.168.0.9 12000 | 
| olympux | 26:09e0dd020900 | 572 | case UPDATE_TCP_SERVER_INFO_COMMAND_LENGTH: { | 
| olympux | 27:22f289beceb8 | 573 | char* id = strstr(received_buffer, DEVICE_CONFIG_CODE); | 
| olympux | 26:09e0dd020900 | 574 | if (id == NULL) | 
| olympux | 26:09e0dd020900 | 575 | break; | 
| olympux | 26:09e0dd020900 | 576 | else if ((id - received_buffer) > 0) | 
| olympux | 26:09e0dd020900 | 577 | break; | 
| olympux | 26:09e0dd020900 | 578 | |
| olympux | 26:09e0dd020900 | 579 | DBG("Received TCP server configuration"); | 
| olympux | 27:22f289beceb8 | 580 | write_eeprom_tcpserver(&received_buffer[strlen(DEVICE_CONFIG_CODE)]); // parameters from 5th char | 
| olympux | 33:c906ccc220ba | 581 | NVIC_SystemReset(); | 
| olympux | 26:09e0dd020900 | 582 | break; | 
| olympux | 26:09e0dd020900 | 583 | } | 
| olympux | 26:09e0dd020900 | 584 | default: | 
| olympux | 26:09e0dd020900 | 585 | break; | 
| olympux | 26:09e0dd020900 | 586 | } // switch (n), check configuration command length | 
| olympux | 27:22f289beceb8 | 587 | } // if starts with NNCF, a config command | 
| olympux | 30:15e23257e786 | 588 | else { // if not a configuration command | 
| olympux | 26:09e0dd020900 | 589 | } | 
| olympux | 26:09e0dd020900 | 590 | } | 
| olympux | 26:09e0dd020900 | 591 | |
| olympux | 12:7c152c0ca4d8 | 592 | /* | 
| olympux | 30:15e23257e786 | 593 | * Procedure to process receiving protocol, which includes command to control outputs. | 
| olympux | 30:15e23257e786 | 594 | * Support both NNIO and RPC-style commands. | 
| olympux | 30:15e23257e786 | 595 | * RPC always starts with '/'. | 
| olympux | 30:15e23257e786 | 596 | * Return: | 
| olympux | 30:15e23257e786 | 597 | * 0 if NNIO protocol; | 
| olympux | 30:15e23257e786 | 598 | * length of rpc output buffer if rpc; | 
| olympux | 30:15e23257e786 | 599 | * -1 if rpc failed | 
| olympux | 28:00c0c20d03c1 | 600 | */ | 
| olympux | 28:00c0c20d03c1 | 601 | int process_control_command(char* received_buffer, int len) | 
| olympux | 26:09e0dd020900 | 602 | { | 
| olympux | 26:09e0dd020900 | 603 | char* received_frame; | 
| olympux | 30:15e23257e786 | 604 | bool rpc_style; | 
| olympux | 26:09e0dd020900 | 605 | int pos; | 
| olympux | 31:2e4b6de6c2f3 | 606 | char inbuf[NET_BUF_LEN]; | 
| olympux | 31:2e4b6de6c2f3 | 607 | |
| olympux | 27:22f289beceb8 | 608 | DBG("Processing control command"); | 
| olympux | 31:2e4b6de6c2f3 | 609 | |
| olympux | 30:15e23257e786 | 610 | /* | 
| olympux | 30:15e23257e786 | 611 | * This section is for RPC-style command | 
| olympux | 30:15e23257e786 | 612 | */ | 
| olympux | 30:15e23257e786 | 613 | // check if it is a RPC-style command | 
| olympux | 30:15e23257e786 | 614 | rpc_style = false; | 
| olympux | 27:22f289beceb8 | 615 | strncpy(inbuf, received_buffer, len); | 
| olympux | 27:22f289beceb8 | 616 | inbuf[len] = '\r'; // use inbuf for RPC protocol | 
| olympux | 27:22f289beceb8 | 617 | inbuf[len+1] = '\n'; | 
| olympux | 27:22f289beceb8 | 618 | inbuf[len+2] = '\0'; // add CR-LF | 
| olympux | 27:22f289beceb8 | 619 | if ((len > 0) && (inbuf[0] == '/')) { | 
| olympux | 29:bc052f283ada | 620 | char obj_name[32]; | 
| olympux | 27:22f289beceb8 | 621 | bool result; | 
| olympux | 31:2e4b6de6c2f3 | 622 | |
| olympux | 30:15e23257e786 | 623 | rpc_style = true; | 
| olympux | 29:bc052f283ada | 624 | // find RPC object name | 
| olympux | 29:bc052f283ada | 625 | for (int i = 1; i < strlen(inbuf); i++) { | 
| olympux | 29:bc052f283ada | 626 | if (inbuf[i] != '/') { | 
| olympux | 29:bc052f283ada | 627 | obj_name[i-1] = inbuf[i]; | 
| olympux | 31:2e4b6de6c2f3 | 628 | } else { | 
| olympux | 29:bc052f283ada | 629 | obj_name[i-1] = '\0'; | 
| olympux | 29:bc052f283ada | 630 | break; | 
| olympux | 29:bc052f283ada | 631 | } | 
| olympux | 29:bc052f283ada | 632 | } | 
| olympux | 28:00c0c20d03c1 | 633 | DBG("Rpc command = %s", inbuf); | 
| olympux | 29:bc052f283ada | 634 | /* | 
| olympux | 29:bc052f283ada | 635 | * execute RPC command, return reply length and reply in rpc_outbuf | 
| olympux | 29:bc052f283ada | 636 | */ | 
| olympux | 31:2e4b6de6c2f3 | 637 | result = RPC::call(inbuf, network_output_buffer); | 
| olympux | 27:22f289beceb8 | 638 | if (result) { | 
| olympux | 29:bc052f283ada | 639 | // re-arrange output buffer as following: object_name:output_value | 
| olympux | 31:2e4b6de6c2f3 | 640 | strcpy(inbuf, network_output_buffer); // use inbuf as temp | 
| olympux | 31:2e4b6de6c2f3 | 641 | strcpy(network_output_buffer, obj_name); // rpc object name | 
| olympux | 31:2e4b6de6c2f3 | 642 | strcat(network_output_buffer, ":"); | 
| olympux | 31:2e4b6de6c2f3 | 643 | strcat(network_output_buffer, inbuf); // concat rpc reply | 
| olympux | 31:2e4b6de6c2f3 | 644 | strcat(network_output_buffer, "\r\n"); // CR-LF | 
| olympux | 31:2e4b6de6c2f3 | 645 | int j = strlen(network_output_buffer); | 
| olympux | 31:2e4b6de6c2f3 | 646 | DBG("Reply of rpc command on \"%s\" (%d bytes): %s", obj_name, j, network_output_buffer); | 
| olympux | 29:bc052f283ada | 647 | return j; // return length of rpc_outbuf | 
| olympux | 31:2e4b6de6c2f3 | 648 | } else { | 
| olympux | 27:22f289beceb8 | 649 | ERR("Failed: %s", inbuf); | 
| olympux | 28:00c0c20d03c1 | 650 | return -1; | 
| olympux | 27:22f289beceb8 | 651 | } | 
| olympux | 27:22f289beceb8 | 652 | } | 
| olympux | 31:2e4b6de6c2f3 | 653 | |
| olympux | 28:00c0c20d03c1 | 654 | /* | 
| olympux | 28:00c0c20d03c1 | 655 | * This section below is for NNIO protocol | 
| olympux | 28:00c0c20d03c1 | 656 | */ | 
| olympux | 30:15e23257e786 | 657 | while ((!rpc_style) && (len >= RECEIVING_PROTOCOL_LENGTH)) { | 
| olympux | 26:09e0dd020900 | 658 | // find device ID | 
| olympux | 26:09e0dd020900 | 659 | DBG("Checking device ID..."); | 
| olympux | 27:22f289beceb8 | 660 | char* id = strstr(received_buffer, DEVICE_CONTROL_CODE); | 
| olympux | 26:09e0dd020900 | 661 | if (id == NULL) { | 
| olympux | 26:09e0dd020900 | 662 | DBG("No device ID found"); | 
| olympux | 26:09e0dd020900 | 663 | break; | 
| olympux | 26:09e0dd020900 | 664 | } | 
| olympux | 26:09e0dd020900 | 665 | pos = id - received_buffer; | 
| olympux | 26:09e0dd020900 | 666 | DBG("Found a frame at %d", pos); | 
| olympux | 31:2e4b6de6c2f3 | 667 | |
| olympux | 26:09e0dd020900 | 668 | // extract this frame | 
| olympux | 26:09e0dd020900 | 669 | received_frame = &received_buffer[pos]; | 
| olympux | 26:09e0dd020900 | 670 | // calculate the rest | 
| olympux | 26:09e0dd020900 | 671 | received_buffer = &received_buffer[pos + RECEIVING_PROTOCOL_LENGTH]; | 
| olympux | 26:09e0dd020900 | 672 | len -= RECEIVING_PROTOCOL_LENGTH; | 
| olympux | 31:2e4b6de6c2f3 | 673 | |
| olympux | 26:09e0dd020900 | 674 | // process this received frame | 
| olympux | 26:09e0dd020900 | 675 | // firstly, update outputs if required | 
| olympux | 26:09e0dd020900 | 676 | // digital outputs | 
| olympux | 26:09e0dd020900 | 677 | if (received_frame[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { | 
| olympux | 26:09e0dd020900 | 678 | DBG("Update digital outputs"); | 
| olympux | 26:09e0dd020900 | 679 | char str_dout[9]; | 
| olympux | 26:09e0dd020900 | 680 | memcpy(str_dout, &received_frame[RECEIVING_PROTOCOL_DO_POS], 8); | 
| olympux | 26:09e0dd020900 | 681 | str_dout[8] = '\0'; | 
| olympux | 26:09e0dd020900 | 682 | update_digital_outputs(str_dout); | 
| olympux | 26:09e0dd020900 | 683 | } | 
| olympux | 26:09e0dd020900 | 684 | // analog output 0 | 
| olympux | 26:09e0dd020900 | 685 | if (received_frame[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { | 
| olympux | 26:09e0dd020900 | 686 | DBG("Update analog output 0"); | 
| olympux | 26:09e0dd020900 | 687 | //TODO Update analog output | 
| olympux | 26:09e0dd020900 | 688 | } | 
| olympux | 26:09e0dd020900 | 689 | // analog output 1 | 
| olympux | 26:09e0dd020900 | 690 | if (received_buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { | 
| olympux | 26:09e0dd020900 | 691 | DBG("Update analog output 1"); | 
| olympux | 26:09e0dd020900 | 692 | //TODO Update analog output | 
| olympux | 26:09e0dd020900 | 693 | } | 
| olympux | 26:09e0dd020900 | 694 | // UART | 
| olympux | 26:09e0dd020900 | 695 | if (received_frame[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { | 
| olympux | 26:09e0dd020900 | 696 | DBG("UART data: "); | 
| olympux | 26:09e0dd020900 | 697 | char str_uart[33]; | 
| olympux | 26:09e0dd020900 | 698 | memcpy(str_uart, &received_frame[RECEIVING_PROTOCOL_UART_POS], 32); | 
| olympux | 26:09e0dd020900 | 699 | str_uart[32] = '\0'; | 
| olympux | 26:09e0dd020900 | 700 | uart.printf("%s\r\n", str_uart); | 
| olympux | 26:09e0dd020900 | 701 | } | 
| olympux | 26:09e0dd020900 | 702 | } | 
| olympux | 31:2e4b6de6c2f3 | 703 | |
| olympux | 27:22f289beceb8 | 704 | DBG("Successfully processed."); | 
| olympux | 28:00c0c20d03c1 | 705 | return 0; | 
| olympux | 26:09e0dd020900 | 706 | } | 
| olympux | 26:09e0dd020900 | 707 | |
| olympux | 26:09e0dd020900 | 708 | |
| olympux | 26:09e0dd020900 | 709 | /* | 
| olympux | 26:09e0dd020900 | 710 | * W5500 Ethernet init | 
| olympux | 26:09e0dd020900 | 711 | */ | 
| olympux | 31:2e4b6de6c2f3 | 712 | int ethernet_init(void) | 
| olympux | 31:2e4b6de6c2f3 | 713 | { | 
| olympux | 26:09e0dd020900 | 714 | int dhcp_ret, ret; | 
| olympux | 31:2e4b6de6c2f3 | 715 | |
| olympux | 27:22f289beceb8 | 716 | DBG("Initialising ethernet..."); | 
| olympux | 31:2e4b6de6c2f3 | 717 | |
| olympux | 26:09e0dd020900 | 718 | // if not configured, try dhcp | 
| olympux | 26:09e0dd020900 | 719 | dhcp_ret = -1; | 
| olympux | 26:09e0dd020900 | 720 | if (configured_ip != DEFAULT_ENABLE_FLAG_VALUE) { | 
| olympux | 27:22f289beceb8 | 721 | DBG("Connecting to DHCP server..."); | 
| olympux | 26:09e0dd020900 | 722 | dhcp_ret = eth.init(u8mac); | 
| olympux | 26:09e0dd020900 | 723 | if (dhcp_ret == 0) | 
| olympux | 26:09e0dd020900 | 724 | dhcp_ret = eth.connect(); | 
| olympux | 26:09e0dd020900 | 725 | } | 
| olympux | 31:2e4b6de6c2f3 | 726 | |
| olympux | 26:09e0dd020900 | 727 | if (dhcp_ret != 0) { | 
| olympux | 26:09e0dd020900 | 728 | DBG("No DHCP, load static IP configuration"); | 
| olympux | 26:09e0dd020900 | 729 | ret = eth.init(u8mac, str_ip_addr, str_ip_subnet, str_ip_gateway); // static | 
| olympux | 26:09e0dd020900 | 730 | } else { | 
| olympux | 26:09e0dd020900 | 731 | snprintf(str_ip_addr, 16, "%s", eth.getIPAddress()); | 
| olympux | 26:09e0dd020900 | 732 | snprintf(str_ip_subnet, 16, "%s", eth.getNetworkMask()); | 
| olympux | 26:09e0dd020900 | 733 | snprintf(str_ip_gateway, 16, "%s", eth.getGateway()); | 
| olympux | 26:09e0dd020900 | 734 | ret = 0; | 
| olympux | 26:09e0dd020900 | 735 | } | 
| olympux | 26:09e0dd020900 | 736 | |
| olympux | 26:09e0dd020900 | 737 | if (ret == 0) { | 
| olympux | 26:09e0dd020900 | 738 | DBG("Initialized, MAC: %s", eth.getMACAddress()); | 
| olympux | 26:09e0dd020900 | 739 | } else { | 
| olympux | 26:09e0dd020900 | 740 | ERR("Error eth.init() - ret = %d", ret); | 
| olympux | 26:09e0dd020900 | 741 | return -1; | 
| olympux | 26:09e0dd020900 | 742 | } | 
| olympux | 26:09e0dd020900 | 743 | |
| olympux | 26:09e0dd020900 | 744 | ret = eth.connect(); | 
| olympux | 26:09e0dd020900 | 745 | if (!ret) { | 
| olympux | 26:09e0dd020900 | 746 | DBG("IP: %s, MASK: %s, GW: %s", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); | 
| olympux | 26:09e0dd020900 | 747 | } else { | 
| olympux | 26:09e0dd020900 | 748 | ERR("Error eth.connect() - ret = %d", ret); | 
| olympux | 26:09e0dd020900 | 749 | return -1; | 
| olympux | 26:09e0dd020900 | 750 | } | 
| olympux | 31:2e4b6de6c2f3 | 751 | |
| olympux | 26:09e0dd020900 | 752 | return 0; | 
| olympux | 26:09e0dd020900 | 753 | } | 
| olympux | 26:09e0dd020900 | 754 | |
| olympux | 26:09e0dd020900 | 755 | |
| olympux | 26:09e0dd020900 | 756 | /* | 
| olympux | 26:09e0dd020900 | 757 | * Update digital outputs according to receiving protocol | 
| olympux | 12:7c152c0ca4d8 | 758 | */ | 
| olympux | 31:2e4b6de6c2f3 | 759 | void update_digital_outputs(char* buf) | 
| olympux | 31:2e4b6de6c2f3 | 760 | { | 
| olympux | 18:ca499a2e7da6 | 761 | DBG("Digital outputs: %s", buf); | 
| olympux | 31:2e4b6de6c2f3 | 762 | |
| olympux | 12:7c152c0ca4d8 | 763 | dout0 = (buf[0] == DIGITAL_HIGH)? 1 : 0; | 
| olympux | 12:7c152c0ca4d8 | 764 | dout1 = (buf[1] == DIGITAL_HIGH)? 1 : 0; | 
| olympux | 12:7c152c0ca4d8 | 765 | dout2 = (buf[2] == DIGITAL_HIGH)? 1 : 0; | 
| olympux | 12:7c152c0ca4d8 | 766 | dout3 = (buf[3] == DIGITAL_HIGH)? 1 : 0; | 
| olympux | 12:7c152c0ca4d8 | 767 | dout4 = (buf[4] == DIGITAL_HIGH)? 1 : 0; | 
| olympux | 12:7c152c0ca4d8 | 768 | dout5 = (buf[5] == DIGITAL_HIGH)? 1 : 0; | 
| olympux | 12:7c152c0ca4d8 | 769 | dout6 = (buf[6] == DIGITAL_HIGH)? 1 : 0; | 
| olympux | 12:7c152c0ca4d8 | 770 | dout7 = (buf[7] == DIGITAL_HIGH)? 1 : 0; | 
| olympux | 12:7c152c0ca4d8 | 771 | } | 
| olympux | 12:7c152c0ca4d8 | 772 | |
| olympux | 26:09e0dd020900 | 773 | /* | 
| olympux | 26:09e0dd020900 | 774 | * Prepare a frame for sending protocol, which includes status of I/Os | 
| olympux | 26:09e0dd020900 | 775 | */ | 
| olympux | 31:2e4b6de6c2f3 | 776 | void update_sending_frame(char* buf) | 
| olympux | 31:2e4b6de6c2f3 | 777 | { | 
| olympux | 27:22f289beceb8 | 778 | memcpy(&buf[SENDING_PROTOCOL_ID_POS], DEVICE_CONTROL_CODE, 4); // device id | 
| olympux | 12:7c152c0ca4d8 | 779 | memcpy(&buf[SENDING_PROTOCOL_MAC_POS], &u8mac, 6); | 
| olympux | 12:7c152c0ca4d8 | 780 | memcpy(&buf[SENDING_PROTOCOL_IP_POS], &u8ip_addr, 4); | 
| olympux | 31:2e4b6de6c2f3 | 781 | |
| olympux | 12:7c152c0ca4d8 | 782 | buf[SENDING_PROTOCOL_DI_POS+0] = (din0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 783 | buf[SENDING_PROTOCOL_DI_POS+1] = (din1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 784 | buf[SENDING_PROTOCOL_DI_POS+2] = (din2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 785 | buf[SENDING_PROTOCOL_DI_POS+3] = (din3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 786 | buf[SENDING_PROTOCOL_DI_POS+4] = (din4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 787 | buf[SENDING_PROTOCOL_DI_POS+5] = (din5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 788 | buf[SENDING_PROTOCOL_DI_POS+6] = (din6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 789 | buf[SENDING_PROTOCOL_DI_POS+7] = (din7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 31:2e4b6de6c2f3 | 790 | |
| olympux | 12:7c152c0ca4d8 | 791 | buf[SENDING_PROTOCOL_DO_POS+0] = (dout0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 792 | buf[SENDING_PROTOCOL_DO_POS+1] = (dout1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 793 | buf[SENDING_PROTOCOL_DO_POS+2] = (dout2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 794 | buf[SENDING_PROTOCOL_DO_POS+3] = (dout3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 795 | buf[SENDING_PROTOCOL_DO_POS+4] = (dout4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 796 | buf[SENDING_PROTOCOL_DO_POS+5] = (dout5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 797 | buf[SENDING_PROTOCOL_DO_POS+6] = (dout6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 12:7c152c0ca4d8 | 798 | buf[SENDING_PROTOCOL_DO_POS+7] = (dout7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; | 
| olympux | 31:2e4b6de6c2f3 | 799 | |
| olympux | 15:edeb0aed160d | 800 | uint16_t val = ain0.read_u16(); // 16-bits normalised | 
| olympux | 15:edeb0aed160d | 801 | memcpy(&buf[SENDING_PROTOCOL_AI0_POS], &val, 2); // LSB MSB | 
| olympux | 15:edeb0aed160d | 802 | val = ain1.read_u16(); // 16-bits normalised | 
| olympux | 15:edeb0aed160d | 803 | memcpy(&buf[SENDING_PROTOCOL_AI1_POS], &val, 2); // LSB MSB | 
| olympux | 23:47ee805435b1 | 804 | val = 0x1234; | 
| olympux | 15:edeb0aed160d | 805 | memcpy(&buf[SENDING_PROTOCOL_AO0_POS], &val, 2); // LSB MSB | 
| olympux | 23:47ee805435b1 | 806 | val = 0x5678; | 
| olympux | 15:edeb0aed160d | 807 | memcpy(&buf[SENDING_PROTOCOL_AO1_POS], &val, 2); // LSB MSB | 
| olympux | 12:7c152c0ca4d8 | 808 | buf[SENDING_PROTOCOL_CR_POS] = 0x0D; | 
| olympux | 12:7c152c0ca4d8 | 809 | buf[SENDING_PROTOCOL_CR_POS+1] = '\0'; | 
| olympux | 4:568c97f2a407 | 810 | } | 
