Webserver only w/o any other functions, single thread. Running on STM32F013+W5500

Dependencies:   NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed

Fork of F103-Serial-to-Ethernet by Chau Vo

Committer:
olympux
Date:
Sat Nov 15 11:46:12 2014 +0000
Revision:
20:71c7950fdd91
Parent:
19:05934ee9ee67
Child:
21:fae96e0d6bb1
Added watchdog timer.; Config mode is looped forever.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
olympux 0:c2eac797face 1 /*
olympux 0:c2eac797face 2 *
olympux 2:18f10e7209f4 3 * Alarm and Monitoring application
olympux 0:c2eac797face 4
olympux 0:c2eac797face 5 */
olympux 0:c2eac797face 6 #include "mbed.h"
olympux 12:7c152c0ca4d8 7 #include "eeprom.h"
olympux 0:c2eac797face 8 #include "EthernetInterface.h"
olympux 9:d2534ecf88c6 9 #include "NTPClient.h"
olympux 0:c2eac797face 10 #include "rtos.h"
olympux 0:c2eac797face 11
olympux 12:7c152c0ca4d8 12 #include "my_eeprom_funcs.h"
olympux 20:71c7950fdd91 13 #include "Watchdog.h"
olympux 12:7c152c0ca4d8 14
olympux 0:c2eac797face 15
olympux 16:84a5bf7285d0 16 //Debug is disabled by default
olympux 16:84a5bf7285d0 17 #if 1
olympux 16:84a5bf7285d0 18 //Enable debug
olympux 16:84a5bf7285d0 19 #include <cstdio>
olympux 16:84a5bf7285d0 20 #define DBG(x, ...) std::printf("[main : DBG]"x"\r\n", ##__VA_ARGS__);
olympux 16:84a5bf7285d0 21 #define WARN(x, ...) std::printf("[main : WARN]"x"\r\n", ##__VA_ARGS__);
olympux 16:84a5bf7285d0 22 #define ERR(x, ...) std::printf("[main : ERR]"x"\r\n", ##__VA_ARGS__);
olympux 16:84a5bf7285d0 23
olympux 16:84a5bf7285d0 24 #else
olympux 16:84a5bf7285d0 25 //Disable debug
olympux 16:84a5bf7285d0 26 #define DBG(x, ...)
olympux 16:84a5bf7285d0 27 #define WARN(x, ...)
olympux 16:84a5bf7285d0 28 #define ERR(x, ...)
olympux 16:84a5bf7285d0 29
olympux 16:84a5bf7285d0 30 #endif
olympux 16:84a5bf7285d0 31
olympux 16:84a5bf7285d0 32
olympux 2:18f10e7209f4 33 /*
olympux 2:18f10e7209f4 34 * Hardware defines
olympux 0:c2eac797face 35 */
olympux 2:18f10e7209f4 36 #define ST_NUCLEO // hardware pin mapping
olympux 0:c2eac797face 37
olympux 0:c2eac797face 38 #ifdef ST_NUCLEO
olympux 9:d2534ecf88c6 39 // Ethernet
olympux 0:c2eac797face 40 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
olympux 11:709f90a3b599 41 EthernetInterface eth(&spi, PA_4, PC_9); // spi, cs, reset
olympux 0:c2eac797face 42 #endif
olympux 0:c2eac797face 43
olympux 9:d2534ecf88c6 44 // Serial
olympux 2:18f10e7209f4 45 Serial uart(USBTX,USBRX);
olympux 2:18f10e7209f4 46
olympux 9:d2534ecf88c6 47 // Digital inputs
olympux 11:709f90a3b599 48 DigitalIn din0(PB_14);
olympux 11:709f90a3b599 49 DigitalIn din1(PB_12);
olympux 11:709f90a3b599 50 DigitalIn din2(PB_10);
olympux 11:709f90a3b599 51 DigitalIn din3(PB_1);
olympux 11:709f90a3b599 52 DigitalIn din4(PB_15);
olympux 11:709f90a3b599 53 DigitalIn din5(PB_13);
olympux 11:709f90a3b599 54 DigitalIn din6(PB_11);
olympux 11:709f90a3b599 55 DigitalIn din7(PB_2);
olympux 9:d2534ecf88c6 56 // Digital outputs
olympux 11:709f90a3b599 57 DigitalOut dout0(PB_3);
olympux 11:709f90a3b599 58 DigitalOut dout1(PB_5);
olympux 11:709f90a3b599 59 DigitalOut dout2(PB_7);
olympux 11:709f90a3b599 60 DigitalOut dout3(PB_9);
olympux 11:709f90a3b599 61 DigitalOut dout4(PD_2);
olympux 11:709f90a3b599 62 DigitalOut dout5(PB_4);
olympux 11:709f90a3b599 63 DigitalOut dout6(PB_6);
olympux 11:709f90a3b599 64 DigitalOut dout7(PB_8);
olympux 9:d2534ecf88c6 65 // Analog inputs
olympux 11:709f90a3b599 66 AnalogIn ain0(PC_0);
olympux 11:709f90a3b599 67 AnalogIn ain1(PC_1);
olympux 9:d2534ecf88c6 68 // Analog outputs
olympux 11:709f90a3b599 69 //AnalogOut ano0(PA_8);
olympux 11:709f90a3b599 70 //AnalogOut ano1(PA_15);
olympux 11:709f90a3b599 71
olympux 20:71c7950fdd91 72 // Watchdog
olympux 20:71c7950fdd91 73 Watchdog wdt;
olympux 20:71c7950fdd91 74
olympux 20:71c7950fdd91 75
olympux 12:7c152c0ca4d8 76 void update_digital_outputs(char* buf);
olympux 12:7c152c0ca4d8 77 void update_sending_frame(char* buf);
olympux 11:709f90a3b599 78
olympux 9:d2534ecf88c6 79
olympux 9:d2534ecf88c6 80
olympux 13:bcf840da68fd 81 /*
olympux 13:bcf840da68fd 82 * EEPROM section
olympux 13:bcf840da68fd 83 */
olympux 6:d054e394fba3 84 // Virtual address defined by the user: 0xFFFF value is prohibited
olympux 6:d054e394fba3 85 uint16_t VirtAddVarTab[NumbOfVar] = {0x1212, 0x1313, 0x1414, 0x1515, // IP_Addr
olympux 6:d054e394fba3 86 0x2212, 0x2313, 0x2414, 0x2515, // IP_Subnet
olympux 6:d054e394fba3 87 0x3212, 0x3313, 0x3414, 0x3515, // IP_Gateway
olympux 6:d054e394fba3 88 0x4212, // TCP server port, not used
olympux 8:64848959adb9 89 0x5212, // UDP server port, not used
olympux 16:84a5bf7285d0 90 0x8888, // 1st run? 0xA5A5 = configured
olympux 12:7c152c0ca4d8 91 0x6212, 0x6313, 0x6414, // MAC
olympux 12:7c152c0ca4d8 92
olympux 12:7c152c0ca4d8 93 // this section is for the TCP server that this device connects to in TCP client mode
olympux 16:84a5bf7285d0 94 0x7212, 0x7313, // 0xA5 = auto transmit status, time period
olympux 12:7c152c0ca4d8 95 0x8212, 0x8313,0x8414, 0x8515, // TCP server IP address
olympux 16:84a5bf7285d0 96 0x9212, // TCP server port
olympux 16:84a5bf7285d0 97
olympux 16:84a5bf7285d0 98 // this section is for selecting protocol
olympux 16:84a5bf7285d0 99 0xA212, // 0xA5A5 = enable TCP server
olympux 16:84a5bf7285d0 100 0xA313, // 0xA5A5 = eanble TCP client
olympux 18:ca499a2e7da6 101 0xA414 // 0xA5A5 = enable UDP server
olympux 6:d054e394fba3 102 };
olympux 0:c2eac797face 103
olympux 2:18f10e7209f4 104 /*
olympux 2:18f10e7209f4 105 * Network configuration
olympux 2:18f10e7209f4 106 */
olympux 4:568c97f2a407 107 #define TCP_SERVER
olympux 15:edeb0aed160d 108 #define TCP_CLIENT
olympux 4:568c97f2a407 109 #define UDP_SERVER
olympux 4:568c97f2a407 110 //#define UDP_CLIENT
olympux 10:4cd965d79de0 111 #define NTP
olympux 4:568c97f2a407 112
olympux 20:71c7950fdd91 113 #define TCP_SERVER_WAIT_CLIENT_TIMEOUT 200 // timeout for local tcp server wait for a remote client
olympux 20:71c7950fdd91 114 #define TCP_SERVER_RECEIVE_TIMEOUT 2000 // timeout for local tcp server wait to receive from remote client
olympux 20:71c7950fdd91 115 #define TCP_CLIENT_RECEIVE_TIMEOUT 200 // timeout for local tcp client try to connect remote server
olympux 20:71c7950fdd91 116 #define UDP_SERVER_RECEIVE_TIMEOUT 100 // timeout for checking config command
olympux 4:568c97f2a407 117
olympux 15:edeb0aed160d 118
olympux 15:edeb0aed160d 119 // TCP server function
olympux 15:edeb0aed160d 120 TCPSocketServer tcp_server;
olympux 15:edeb0aed160d 121 TCPSocketConnection tcp_client;
olympux 15:edeb0aed160d 122 // TCP client function
olympux 15:edeb0aed160d 123 TCPSocketConnection tcp_sock;
olympux 15:edeb0aed160d 124 // UDP server
olympux 15:edeb0aed160d 125 UDPSocket udp_server;
olympux 15:edeb0aed160d 126 Endpoint ep_udp_client;
olympux 15:edeb0aed160d 127 // NTP
olympux 13:bcf840da68fd 128 NTPClient ntp;
olympux 13:bcf840da68fd 129
olympux 13:bcf840da68fd 130
olympux 15:edeb0aed160d 131
olympux 13:bcf840da68fd 132 /*
olympux 13:bcf840da68fd 133 * Variables for network configuration, server
olympux 13:bcf840da68fd 134 */
olympux 11:709f90a3b599 135 uint8_t u8mac[6], u8ip_addr[4];// keep mac and ip address in 8-bits
olympux 11:709f90a3b599 136 uint16_t u16mac_addr[3], u16ip_addr[4], u16ip_subnet[4], u16ip_gateway[4]; // 16-bits, directly loaded from eeprom
olympux 11:709f90a3b599 137 char str_ip_addr[16], str_ip_subnet[16], str_ip_gateway[16]; // for printf, converted from 16-bits u16ip_xxx
olympux 9:d2534ecf88c6 138 uint16_t first_run = 0; // first run flag
olympux 9:d2534ecf88c6 139
olympux 12:7c152c0ca4d8 140 const uint16_t tcp_server_local_port = 10000; // fixed
olympux 12:7c152c0ca4d8 141 const uint16_t udp_server_local_port = 11000; // fixed
olympux 12:7c152c0ca4d8 142
olympux 12:7c152c0ca4d8 143 // TCP client: this section is used for the TCP server that this device connects to in TCP client mode
olympux 12:7c152c0ca4d8 144 // this device will transmit status every transmit_time_period
olympux 12:7c152c0ca4d8 145 uint16_t auto_transmit_flag = 0, transmit_time_period = 1000; // auto transmit status, time period = 1s
olympux 12:7c152c0ca4d8 146 uint16_t u16server_ip_addr[4]; // directly loaded from eeprom
olympux 14:18eda020a589 147 uint8_t u8server_ip_addr[4]; // server ip address in 8-bits
olympux 14:18eda020a589 148 char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr
olympux 12:7c152c0ca4d8 149 uint16_t u16tcp_server_port; // directly loaded from eeprom
olympux 18:ca499a2e7da6 150 uint16_t u16enable_tcp_client, u16enable_tcp_server;// flags for enabling TCP client or TCP server
olympux 12:7c152c0ca4d8 151
olympux 19:05934ee9ee67 152 char tcp_receiving_buffer[256];
olympux 19:05934ee9ee67 153 char tcp_sending_buffer[256]; // socket buffer
olympux 19:05934ee9ee67 154 char udp_receiving_buffer[256];
olympux 12:7c152c0ca4d8 155
olympux 0:c2eac797face 156
olympux 13:bcf840da68fd 157 /*
olympux 13:bcf840da68fd 158 * Protocol
olympux 13:bcf840da68fd 159 */
olympux 9:d2534ecf88c6 160 // Commands
olympux 17:88ef7a078095 161 #define DEVICE_ID "NNIO"
olympux 17:88ef7a078095 162
olympux 17:88ef7a078095 163 #define RECEIVING_PROTOCOL_LENGTH 58
olympux 17:88ef7a078095 164 #define SENDING_PROTOCOL_LENGTH 39
olympux 17:88ef7a078095 165 #define QUERY_CMD_LENGTH 6
olympux 17:88ef7a078095 166 #define SET_NETWORK_CONFIG_CMD_LENGTH 19
olympux 17:88ef7a078095 167 #define UPDATE_TCP_SERVER_INFO_CMD_LENGTH 12
olympux 17:88ef7a078095 168
olympux 17:88ef7a078095 169 #define QUERY_DISCOVERY_CMD "NNIODS"
olympux 17:88ef7a078095 170 #define QUERY_IP_CMD "NNIOIP"
olympux 17:88ef7a078095 171 #define QUERY_SUBNET_CMD "NNIOSN"
olympux 17:88ef7a078095 172 #define QUERY_GATEWAY_CMD "NNIOGW"
olympux 17:88ef7a078095 173 #define QUERY_MAC_CMD "NNIOMC"
olympux 17:88ef7a078095 174 #define QUERY_UDP_PORT_CMD "NNIOUP"
olympux 17:88ef7a078095 175 #define QUERY_TCP_PORT_CMD "NNIOTP"
olympux 17:88ef7a078095 176 #define QUERY_UPDATE_TIME_CMD "NNIOTM"
olympux 11:709f90a3b599 177 #define RECEIVING_PROTOCOL_ENABLE_OUTPUT 'O'
olympux 11:709f90a3b599 178 #define QUERY_STATUS_COMMAND 'Q'
olympux 11:709f90a3b599 179 #define DIGITAL_HIGH 'H'
olympux 11:709f90a3b599 180 #define DIGITAL_LOW 'L'
olympux 9:d2534ecf88c6 181
olympux 9:d2534ecf88c6 182
olympux 9:d2534ecf88c6 183 // Positions
olympux 11:709f90a3b599 184 #define RECEIVING_PROTOCOL_ID_POS 0
olympux 9:d2534ecf88c6 185 #define RECEIVING_PROTOCOL_OP_POS 4
olympux 9:d2534ecf88c6 186 #define RECEIVING_PROTOCOL_EN_DO_POS RECEIVING_PROTOCOL_OP_POS + 0
olympux 9:d2534ecf88c6 187 #define RECEIVING_PROTOCOL_EN_A0O_POS RECEIVING_PROTOCOL_OP_POS + 1
olympux 9:d2534ecf88c6 188 #define RECEIVING_PROTOCOL_EN_A1O_POS RECEIVING_PROTOCOL_OP_POS + 2
olympux 9:d2534ecf88c6 189 #define RECEIVING_PROTOCOL_EN_UART_POS RECEIVING_PROTOCOL_OP_POS + 3
olympux 9:d2534ecf88c6 190 #define RECEIVING_PROTOCOL_COMMAND_POS RECEIVING_PROTOCOL_OP_POS + 4
olympux 11:709f90a3b599 191
olympux 11:709f90a3b599 192 #define RECEIVING_PROTOCOL_IP_POS 9
olympux 11:709f90a3b599 193 #define RECEIVING_PROTOCOL_DO_POS 13
olympux 11:709f90a3b599 194 #define RECEIVING_PROTOCOL_A0O_POS 21
olympux 11:709f90a3b599 195 #define RECEIVING_PROTOCOL_A01_POS 23
olympux 11:709f90a3b599 196 #define RECEIVING_PROTOCOL_UART_POS 25
olympux 9:d2534ecf88c6 197
olympux 9:d2534ecf88c6 198
olympux 11:709f90a3b599 199 #define SENDING_PROTOCOL_ID_POS 0
olympux 11:709f90a3b599 200 #define SENDING_PROTOCOL_MAC_POS 4
olympux 11:709f90a3b599 201 #define SENDING_PROTOCOL_IP_POS 10
olympux 11:709f90a3b599 202 #define SENDING_PROTOCOL_DI_POS 14
olympux 11:709f90a3b599 203 #define SENDING_PROTOCOL_DO_POS 22
olympux 11:709f90a3b599 204 #define SENDING_PROTOCOL_AI0_POS 30
olympux 11:709f90a3b599 205 #define SENDING_PROTOCOL_AI1_POS 32
olympux 11:709f90a3b599 206 #define SENDING_PROTOCOL_AO0_POS 34
olympux 11:709f90a3b599 207 #define SENDING_PROTOCOL_AO1_POS 36
olympux 11:709f90a3b599 208 #define SENDING_PROTOCOL_CR_POS 38
olympux 0:c2eac797face 209
olympux 2:18f10e7209f4 210
olympux 2:18f10e7209f4 211 /*
olympux 2:18f10e7209f4 212 * RTOS
olympux 2:18f10e7209f4 213 */
olympux 2:18f10e7209f4 214 struct message_t {
olympux 2:18f10e7209f4 215 int len;
olympux 2:18f10e7209f4 216 char *msg;
olympux 2:18f10e7209f4 217 };
olympux 3:972ed747474c 218 Queue<message_t, 16> uart_queue;
olympux 15:edeb0aed160d 219 Queue<bool, 1> auto_update_queue;
olympux 2:18f10e7209f4 220
olympux 3:972ed747474c 221 Mutex uart_mutex;
olympux 2:18f10e7209f4 222
olympux 2:18f10e7209f4 223
olympux 2:18f10e7209f4 224 /*
olympux 2:18f10e7209f4 225 * Threads
olympux 2:18f10e7209f4 226 */
olympux 15:edeb0aed160d 227 // Timer thread for auto update
olympux 15:edeb0aed160d 228 void auto_update_timer_thread(void const* args) {
olympux 18:ca499a2e7da6 229 bool update_flag = true;
olympux 15:edeb0aed160d 230
olympux 15:edeb0aed160d 231 Thread::wait(500);
olympux 15:edeb0aed160d 232 while(true) {
olympux 15:edeb0aed160d 233 auto_update_queue.put(&update_flag);
olympux 18:ca499a2e7da6 234 Thread::wait(1000*transmit_time_period); // Thread::wait() in ms
olympux 15:edeb0aed160d 235 }
olympux 15:edeb0aed160d 236 }
olympux 15:edeb0aed160d 237
olympux 15:edeb0aed160d 238
olympux 20:71c7950fdd91 239 // WDT reset
olympux 20:71c7950fdd91 240 void wdt_reset_thread(void const* args) {
olympux 20:71c7950fdd91 241 while (true)
olympux 20:71c7950fdd91 242 wdt.Service();
olympux 20:71c7950fdd91 243 }
olympux 20:71c7950fdd91 244
olympux 20:71c7950fdd91 245
olympux 6:d054e394fba3 246 /*
olympux 6:d054e394fba3 247 * Ethernet init
olympux 6:d054e394fba3 248 */
olympux 4:568c97f2a407 249 int ethernet_init(void) {
olympux 18:ca499a2e7da6 250 DBG("Start initialising ethernet");
olympux 14:18eda020a589 251 int ret = eth.init(u8mac, str_ip_addr, str_ip_subnet, str_ip_gateway); // static
olympux 0:c2eac797face 252
olympux 0:c2eac797face 253 if (!ret) {
olympux 18:ca499a2e7da6 254 DBG("Initialized, MAC: %s", eth.getMACAddress());
olympux 0:c2eac797face 255 } else {
olympux 18:ca499a2e7da6 256 ERR("Error eth.init() - ret = %d", ret);
olympux 0:c2eac797face 257 return -1;
olympux 0:c2eac797face 258 }
olympux 0:c2eac797face 259
olympux 0:c2eac797face 260 ret = eth.connect();
olympux 0:c2eac797face 261 if (!ret) {
olympux 18:ca499a2e7da6 262 DBG("IP: %s, MASK: %s, GW: %s", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
olympux 0:c2eac797face 263 } else {
olympux 18:ca499a2e7da6 264 ERR("Error eth.connect() - ret = %d", ret);
olympux 0:c2eac797face 265 return -1;
olympux 0:c2eac797face 266 }
olympux 3:972ed747474c 267
olympux 4:568c97f2a407 268 return 0;
olympux 4:568c97f2a407 269 }
olympux 4:568c97f2a407 270
olympux 4:568c97f2a407 271
olympux 19:05934ee9ee67 272 /*
olympux 19:05934ee9ee67 273 * Procedure to process receiving protocol
olympux 19:05934ee9ee67 274 */
olympux 19:05934ee9ee67 275 void process_received_tcp_data(char* received_buffer, int len)
olympux 19:05934ee9ee67 276 {
olympux 19:05934ee9ee67 277 char* received_frame;
olympux 19:05934ee9ee67 278 int pos;
olympux 19:05934ee9ee67 279
olympux 19:05934ee9ee67 280 while (len >= RECEIVING_PROTOCOL_LENGTH) {
olympux 19:05934ee9ee67 281 // find device ID
olympux 19:05934ee9ee67 282 DBG("Checking device ID...");
olympux 19:05934ee9ee67 283 char* id = strstr(received_buffer, DEVICE_ID);
olympux 19:05934ee9ee67 284 if (id == NULL) {
olympux 19:05934ee9ee67 285 DBG("No device ID found");
olympux 19:05934ee9ee67 286 break;
olympux 19:05934ee9ee67 287 }
olympux 19:05934ee9ee67 288 pos = id - received_buffer;
olympux 19:05934ee9ee67 289 DBG("Found a frame at %d", pos);
olympux 19:05934ee9ee67 290
olympux 19:05934ee9ee67 291 // extract this frame
olympux 19:05934ee9ee67 292 received_frame = &received_buffer[pos];
olympux 19:05934ee9ee67 293 // calculate the rest
olympux 19:05934ee9ee67 294 received_buffer = &received_buffer[pos + RECEIVING_PROTOCOL_LENGTH];
olympux 19:05934ee9ee67 295 len -= RECEIVING_PROTOCOL_LENGTH;
olympux 19:05934ee9ee67 296
olympux 19:05934ee9ee67 297 // process this received frame
olympux 19:05934ee9ee67 298 // firstly, update outputs if required
olympux 19:05934ee9ee67 299 // digital outputs
olympux 19:05934ee9ee67 300 if (received_frame[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 19:05934ee9ee67 301 DBG("Update digital outputs");
olympux 19:05934ee9ee67 302 char str_dout[9];
olympux 19:05934ee9ee67 303 memcpy(str_dout, &received_frame[RECEIVING_PROTOCOL_DO_POS], 8);
olympux 19:05934ee9ee67 304 str_dout[8] = '\0';
olympux 19:05934ee9ee67 305 update_digital_outputs(str_dout);
olympux 19:05934ee9ee67 306 }
olympux 19:05934ee9ee67 307 // analog output 0
olympux 19:05934ee9ee67 308 if (received_frame[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 19:05934ee9ee67 309 DBG("Update analog output 0");
olympux 19:05934ee9ee67 310 }
olympux 19:05934ee9ee67 311 // analog output 1
olympux 19:05934ee9ee67 312 if (received_buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 19:05934ee9ee67 313 DBG("Update analog output 1");
olympux 19:05934ee9ee67 314 }
olympux 19:05934ee9ee67 315 // UART
olympux 19:05934ee9ee67 316 if (received_frame[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 19:05934ee9ee67 317 DBG("UART data: ");
olympux 19:05934ee9ee67 318 char str_uart[33];
olympux 19:05934ee9ee67 319 memcpy(str_uart, &received_frame[RECEIVING_PROTOCOL_UART_POS], 32);
olympux 19:05934ee9ee67 320 str_uart[32] = '\0';
olympux 19:05934ee9ee67 321 printf("%s\r\n", str_uart);
olympux 19:05934ee9ee67 322 }
olympux 19:05934ee9ee67 323
olympux 19:05934ee9ee67 324 // then, check query status command and sending protocol if required
olympux 19:05934ee9ee67 325 if (received_frame[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
olympux 19:05934ee9ee67 326 DBG("Requested to send device status through TCP");
olympux 19:05934ee9ee67 327 // sending protocol
olympux 19:05934ee9ee67 328 update_sending_frame(tcp_sending_buffer);
olympux 19:05934ee9ee67 329 tcp_client.send_all(tcp_sending_buffer, SENDING_PROTOCOL_LENGTH);
olympux 19:05934ee9ee67 330 DBG("Sent");
olympux 19:05934ee9ee67 331 }
olympux 19:05934ee9ee67 332 }
olympux 19:05934ee9ee67 333
olympux 19:05934ee9ee67 334 DBG("Successful");
olympux 19:05934ee9ee67 335 }
olympux 6:d054e394fba3 336
olympux 4:568c97f2a407 337
olympux 4:568c97f2a407 338 int main()
olympux 4:568c97f2a407 339 {
olympux 9:d2534ecf88c6 340 int n, ret;
olympux 4:568c97f2a407 341
olympux 20:71c7950fdd91 342 Thread::wait(500); // turn on delay
olympux 20:71c7950fdd91 343
olympux 4:568c97f2a407 344 /*
olympux 9:d2534ecf88c6 345 * Configure
olympux 4:568c97f2a407 346 */
olympux 11:709f90a3b599 347 uart.baud(115200);
olympux 18:ca499a2e7da6 348 DBG("\r\nStarting...");
olympux 20:71c7950fdd91 349
olympux 20:71c7950fdd91 350 if (wdt.WatchdogCausedReset())
olympux 20:71c7950fdd91 351 DBG("Watchdog caused reset.");
olympux 20:71c7950fdd91 352 wdt.Configure(4);
olympux 6:d054e394fba3 353
olympux 6:d054e394fba3 354 /*
olympux 6:d054e394fba3 355 * FLASH
olympux 6:d054e394fba3 356 */
olympux 12:7c152c0ca4d8 357 load_eeprom_network();
olympux 15:edeb0aed160d 358 load_eeprom_tcpserver();
olympux 20:71c7950fdd91 359
olympux 20:71c7950fdd91 360 /*
olympux 20:71c7950fdd91 361 * UI threads
olympux 20:71c7950fdd91 362 */
olympux 20:71c7950fdd91 363 Thread t2(auto_update_timer_thread);
olympux 20:71c7950fdd91 364 Thread t3(wdt_reset_thread);
olympux 20:71c7950fdd91 365
olympux 4:568c97f2a407 366 /*
olympux 4:568c97f2a407 367 * Ethernet
olympux 4:568c97f2a407 368 */
olympux 6:d054e394fba3 369 ret = ethernet_init();
olympux 4:568c97f2a407 370 if (ret) {
olympux 18:ca499a2e7da6 371 ERR("Ethernet initialisation failed. App halted.");
olympux 4:568c97f2a407 372 while (true) {};
olympux 4:568c97f2a407 373 }
olympux 4:568c97f2a407 374
olympux 20:71c7950fdd91 375 Thread::wait(2000); // turn on delay
olympux 12:7c152c0ca4d8 376
olympux 12:7c152c0ca4d8 377 /*
olympux 20:71c7950fdd91 378 * UDP server
olympux 20:71c7950fdd91 379 * TCP server/client
olympux 12:7c152c0ca4d8 380 */
olympux 20:71c7950fdd91 381 #ifdef UDP_SERVER
olympux 20:71c7950fdd91 382 ret = udp_server.bind(udp_server_local_port);
olympux 20:71c7950fdd91 383 DBG("UDP server started (sock.bind = %d)...", ret);
olympux 20:71c7950fdd91 384 udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT);
olympux 20:71c7950fdd91 385 #endif
olympux 20:71c7950fdd91 386
olympux 3:972ed747474c 387 #ifdef TCP_SERVER
olympux 12:7c152c0ca4d8 388 tcp_server.bind(tcp_server_local_port);
olympux 3:972ed747474c 389 tcp_server.listen();
olympux 18:ca499a2e7da6 390 DBG("TCP server started...");
olympux 4:568c97f2a407 391 tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT);
olympux 3:972ed747474c 392 #endif
olympux 14:18eda020a589 393
olympux 14:18eda020a589 394 #ifdef TCP_CLIENT
olympux 18:ca499a2e7da6 395
olympux 14:18eda020a589 396 #endif
olympux 3:972ed747474c 397
olympux 12:7c152c0ca4d8 398 /*
olympux 19:05934ee9ee67 399 * Network loop processor
olympux 12:7c152c0ca4d8 400 */
olympux 0:c2eac797face 401 while (true) {
olympux 15:edeb0aed160d 402 #ifdef TCP_CLIENT
olympux 18:ca499a2e7da6 403 // FOR AUTO TRANSMIT DEVICE STATUS
olympux 18:ca499a2e7da6 404 //if ((u16enable_tcp_client == 0xA5A5) && (auto_transmit_flag == 0xA5A5)) {
olympux 18:ca499a2e7da6 405 if (auto_transmit_flag == 0xA5A5) {
olympux 18:ca499a2e7da6 406 // connect to TCP server if required
olympux 18:ca499a2e7da6 407 if (!tcp_sock.is_connected()) {
olympux 20:71c7950fdd91 408 ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port); // timeout is default in connect() in W5500.h
olympux 18:ca499a2e7da6 409 if (ret > -1) {
olympux 18:ca499a2e7da6 410 DBG("Successfully connected to %s on port %d", str_server_ip_addr, u16tcp_server_port);
olympux 18:ca499a2e7da6 411 }
olympux 18:ca499a2e7da6 412 else {
olympux 18:ca499a2e7da6 413 ERR("Unable to connect to %s on port %d", str_server_ip_addr, u16tcp_server_port);
olympux 18:ca499a2e7da6 414 }
olympux 16:84a5bf7285d0 415 }
olympux 18:ca499a2e7da6 416
olympux 18:ca499a2e7da6 417 // transmit data if connected
olympux 18:ca499a2e7da6 418 if (tcp_sock.is_connected()) {
olympux 18:ca499a2e7da6 419 osEvent evt = auto_update_queue.get(1); // timeout after 1ms
olympux 18:ca499a2e7da6 420 if (evt.status == osEventMessage) {
olympux 18:ca499a2e7da6 421 DBG("Updating...");
olympux 19:05934ee9ee67 422 update_sending_frame(tcp_sending_buffer);
olympux 19:05934ee9ee67 423 tcp_sock.send_all(tcp_sending_buffer, SENDING_PROTOCOL_LENGTH);
olympux 19:05934ee9ee67 424 }
olympux 19:05934ee9ee67 425
olympux 19:05934ee9ee67 426 // check to receive or timeout
olympux 19:05934ee9ee67 427 tcp_sock.set_blocking(false, TCP_CLIENT_RECEIVE_TIMEOUT);
olympux 19:05934ee9ee67 428 n = tcp_sock.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer));
olympux 19:05934ee9ee67 429 if (n > 0) {
olympux 19:05934ee9ee67 430 // got some data, test it
olympux 19:05934ee9ee67 431 DBG("TCP client received %d bytes: %s", n, tcp_receiving_buffer);
olympux 19:05934ee9ee67 432 process_received_tcp_data(tcp_receiving_buffer, n);
olympux 18:ca499a2e7da6 433 }
olympux 16:84a5bf7285d0 434 }
olympux 18:ca499a2e7da6 435 } // if tcp client enabled && auto transmit
olympux 15:edeb0aed160d 436 #endif
olympux 15:edeb0aed160d 437
olympux 15:edeb0aed160d 438
olympux 9:d2534ecf88c6 439 // FOR INTERFACING
olympux 4:568c97f2a407 440 #ifdef TCP_SERVER
olympux 4:568c97f2a407 441 // no tcp client connected
olympux 18:ca499a2e7da6 442 //if ((u16enable_tcp_server == 0xA5A5) && (!tcp_client.is_connected())) {
olympux 20:71c7950fdd91 443 //if (!tcp_client.is_connected()) {
olympux 20:71c7950fdd91 444 if (1) {
olympux 4:568c97f2a407 445 // wait for client within timeout
olympux 4:568c97f2a407 446 ret = tcp_server.accept(tcp_client);
olympux 2:18f10e7209f4 447
olympux 4:568c97f2a407 448 // tcp client connected
olympux 4:568c97f2a407 449 if (ret > -1) {
olympux 18:ca499a2e7da6 450 DBG("Connection from: %s", tcp_client.get_address());
olympux 4:568c97f2a407 451
olympux 4:568c97f2a407 452 // loop waiting and receiving data within timeout
olympux 4:568c97f2a407 453 tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds
olympux 4:568c97f2a407 454 while (true) {
olympux 19:05934ee9ee67 455 n = tcp_client.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer));
olympux 4:568c97f2a407 456 if (n <= 0) break;
olympux 4:568c97f2a407 457
olympux 9:d2534ecf88c6 458 // got some data, test it
olympux 19:05934ee9ee67 459 DBG("TCP server received: %s", tcp_receiving_buffer);
olympux 19:05934ee9ee67 460 process_received_tcp_data(tcp_receiving_buffer, n);
olympux 4:568c97f2a407 461 } // end loop if no data received within timeout
olympux 4:568c97f2a407 462 } // if client connected
olympux 20:71c7950fdd91 463 tcp_client.close();
olympux 18:ca499a2e7da6 464 } // if tcp server enabled && no client connected
olympux 3:972ed747474c 465 #endif
olympux 20:71c7950fdd91 466
olympux 9:d2534ecf88c6 467 #ifdef UDP_SERVER
olympux 19:05934ee9ee67 468 n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer));
olympux 20:71c7950fdd91 469 if (n > 0) {
olympux 20:71c7950fdd91 470 // if receive any config command, stay in config mode forever
olympux 20:71c7950fdd91 471 DBG("Enabled configuration mode...");
olympux 20:71c7950fdd91 472 DBG("!!! RESET when finished");
olympux 20:71c7950fdd91 473 while (n > 0) {
olympux 20:71c7950fdd91 474 // got some data, test it
olympux 20:71c7950fdd91 475 DBG("UDP received (%s) from (%s) and port (%d)", udp_receiving_buffer, ep_udp_client.get_address(), ep_udp_client.get_port());
olympux 20:71c7950fdd91 476 // process received data
olympux 20:71c7950fdd91 477 switch (n) {
olympux 20:71c7950fdd91 478 // length = 6, a QUERY command (discovery command, TCP port, or UDP port)
olympux 20:71c7950fdd91 479 // Format: NNIODS, NNIOTP, NNIOUP, NNIOTM
olympux 20:71c7950fdd91 480 case QUERY_CMD_LENGTH:
olympux 20:71c7950fdd91 481 // discovery command
olympux 20:71c7950fdd91 482 if (strstr(udp_receiving_buffer, QUERY_DISCOVERY_CMD) != NULL) {
olympux 20:71c7950fdd91 483 char str[30];
olympux 20:71c7950fdd91 484 sprintf(str, "%s%s", DEVICE_ID, eth.getIPAddress());
olympux 20:71c7950fdd91 485 udp_server.sendTo(ep_udp_client, str, strlen(str));
olympux 20:71c7950fdd91 486 } // NNIODS
olympux 20:71c7950fdd91 487 else if (strstr(udp_receiving_buffer, QUERY_IP_CMD) != NULL) {
olympux 20:71c7950fdd91 488 udp_server.sendTo(ep_udp_client, eth.getIPAddress(), strlen(eth.getIPAddress()));
olympux 20:71c7950fdd91 489 } // NNIOIP
olympux 20:71c7950fdd91 490 else if (strstr(udp_receiving_buffer, QUERY_SUBNET_CMD) != NULL) {
olympux 20:71c7950fdd91 491 udp_server.sendTo(ep_udp_client, eth.getNetworkMask(), strlen(eth.getNetworkMask()));
olympux 20:71c7950fdd91 492 } // NNIOSN
olympux 20:71c7950fdd91 493 else if (strstr(udp_receiving_buffer, QUERY_GATEWAY_CMD) != NULL) {
olympux 20:71c7950fdd91 494 udp_server.sendTo(ep_udp_client, eth.getGateway(), strlen(eth.getGateway()));
olympux 20:71c7950fdd91 495 } // NNIOGW
olympux 20:71c7950fdd91 496 else if (strstr(udp_receiving_buffer, QUERY_MAC_CMD) != NULL) {
olympux 20:71c7950fdd91 497 udp_server.sendTo(ep_udp_client, eth.getMACAddress(), strlen(eth.getMACAddress()));
olympux 20:71c7950fdd91 498 } // NNIOMC
olympux 20:71c7950fdd91 499 // ask for TCP server port
olympux 20:71c7950fdd91 500 else if (strstr(udp_receiving_buffer, QUERY_TCP_PORT_CMD) != NULL) {
olympux 20:71c7950fdd91 501 char port[5];
olympux 20:71c7950fdd91 502 sprintf(port, "%5d", tcp_server_local_port);
olympux 20:71c7950fdd91 503 udp_server.sendTo(ep_udp_client, port, strlen(port));
olympux 20:71c7950fdd91 504 } // NNIOTP
olympux 20:71c7950fdd91 505 // ask for UDP server port
olympux 20:71c7950fdd91 506 else if (strstr(udp_receiving_buffer, QUERY_UDP_PORT_CMD) != NULL) {
olympux 20:71c7950fdd91 507 char port[5];
olympux 20:71c7950fdd91 508 sprintf(port, "%5d", udp_server_local_port);
olympux 20:71c7950fdd91 509 udp_server.sendTo(ep_udp_client, port, strlen(port));
olympux 20:71c7950fdd91 510 } // NNIOUP
olympux 20:71c7950fdd91 511 else if (strstr(udp_receiving_buffer, QUERY_UPDATE_TIME_CMD) != NULL) {
olympux 10:4cd965d79de0 512 #ifdef NTP
olympux 20:71c7950fdd91 513 char str_time[50];
olympux 20:71c7950fdd91 514
olympux 20:71c7950fdd91 515 DBG("Trying to update time...");
olympux 20:71c7950fdd91 516 if (ntp.setTime("0.pool.ntp.org") == 0) {
olympux 20:71c7950fdd91 517 DBG("Set time successfully");
olympux 20:71c7950fdd91 518 time_t ctTime;
olympux 20:71c7950fdd91 519 ctTime = time(NULL);
olympux 20:71c7950fdd91 520
olympux 20:71c7950fdd91 521 DBG("Time is set to (UTC): %s", ctime(&ctTime));
olympux 20:71c7950fdd91 522 sprintf(str_time, "%s", ctime(&ctTime));
olympux 20:71c7950fdd91 523 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 20:71c7950fdd91 524 }
olympux 20:71c7950fdd91 525 else {
olympux 20:71c7950fdd91 526 WARN("Error");
olympux 20:71c7950fdd91 527 sprintf(str_time, "ERR");
olympux 20:71c7950fdd91 528 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 20:71c7950fdd91 529 }
olympux 10:4cd965d79de0 530 #elif
olympux 20:71c7950fdd91 531 WARN("NTP disabled");
olympux 20:71c7950fdd91 532 sprintf(str_time, "DIS");
olympux 20:71c7950fdd91 533 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 10:4cd965d79de0 534 #endif
olympux 20:71c7950fdd91 535 } // NNIOTM
olympux 20:71c7950fdd91 536
olympux 20:71c7950fdd91 537 break;
olympux 20:71c7950fdd91 538 // length = 19, SET NETWORK CONFIGURATION
olympux 20:71c7950fdd91 539 // Format: 4E 4E 49 4F C0 A8 00 78 FF FF FF 00 C0 A8 00 01 00 00 01
olympux 20:71c7950fdd91 540 // (NNIO; IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1)
olympux 20:71c7950fdd91 541 case SET_NETWORK_CONFIG_CMD_LENGTH: {
olympux 20:71c7950fdd91 542 // check device id
olympux 20:71c7950fdd91 543 char* id = strstr(udp_receiving_buffer, DEVICE_ID);
olympux 20:71c7950fdd91 544 if (id == NULL)
olympux 20:71c7950fdd91 545 break;
olympux 20:71c7950fdd91 546 else if ((id - udp_receiving_buffer) > 0)
olympux 20:71c7950fdd91 547 break;
olympux 20:71c7950fdd91 548
olympux 20:71c7950fdd91 549 DBG("Received user configuration");
olympux 20:71c7950fdd91 550 write_eeprom_network(&udp_receiving_buffer[strlen(DEVICE_ID)]); // parameters from 5th char, 15-bytes
olympux 20:71c7950fdd91 551 break;
olympux 20:71c7950fdd91 552 }
olympux 20:71c7950fdd91 553 // length = 12, SET TCP SERVER CONFIGURATION
olympux 20:71c7950fdd91 554 // auto update & its time period, TCP server configuration (IP & port)
olympux 20:71c7950fdd91 555 // Format: 4E 4E 49 4F 'Y' 01 C0 A8 00 09 E0 2E (LSB MSB)
olympux 20:71c7950fdd91 556 // NNIO Auto 1s 192.168.0.9 12000
olympux 20:71c7950fdd91 557 case UPDATE_TCP_SERVER_INFO_CMD_LENGTH: {
olympux 20:71c7950fdd91 558 char* id = strstr(udp_receiving_buffer, DEVICE_ID);
olympux 20:71c7950fdd91 559 if (id == NULL)
olympux 20:71c7950fdd91 560 break;
olympux 20:71c7950fdd91 561 else if ((id - udp_receiving_buffer) > 0)
olympux 20:71c7950fdd91 562 break;
olympux 20:71c7950fdd91 563
olympux 20:71c7950fdd91 564 DBG("Received TCP server configuration");
olympux 20:71c7950fdd91 565 write_eeprom_tcpserver(&udp_receiving_buffer[strlen(DEVICE_ID)]); // parameters from 5th char
olympux 20:71c7950fdd91 566 break;
olympux 20:71c7950fdd91 567 }
olympux 20:71c7950fdd91 568 default:
olympux 20:71c7950fdd91 569 break;
olympux 20:71c7950fdd91 570 } // switch (n)
olympux 15:edeb0aed160d 571
olympux 20:71c7950fdd91 572 // wait to receive new config command
olympux 20:71c7950fdd91 573 udp_server.set_blocking(true);
olympux 20:71c7950fdd91 574 n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer));
olympux 20:71c7950fdd91 575 } // while (1): config commands processor
olympux 20:71c7950fdd91 576 } // udp config timeout?
olympux 3:972ed747474c 577 #endif
olympux 4:568c97f2a407 578 } // network processor
olympux 11:709f90a3b599 579 }
olympux 11:709f90a3b599 580
olympux 12:7c152c0ca4d8 581 /*
olympux 12:7c152c0ca4d8 582 * Update digital outputs following receiving frame from TCP client
olympux 12:7c152c0ca4d8 583 */
olympux 12:7c152c0ca4d8 584 void update_digital_outputs(char* buf) {
olympux 18:ca499a2e7da6 585 DBG("Digital outputs: %s", buf);
olympux 11:709f90a3b599 586
olympux 12:7c152c0ca4d8 587 dout0 = (buf[0] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 588 dout1 = (buf[1] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 589 dout2 = (buf[2] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 590 dout3 = (buf[3] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 591 dout4 = (buf[4] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 592 dout5 = (buf[5] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 593 dout6 = (buf[6] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 594 dout7 = (buf[7] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 595 }
olympux 12:7c152c0ca4d8 596
olympux 12:7c152c0ca4d8 597 void update_sending_frame(char* buf) {
olympux 12:7c152c0ca4d8 598 memcpy(&buf[SENDING_PROTOCOL_ID_POS], DEVICE_ID, 4); // device id
olympux 12:7c152c0ca4d8 599 memcpy(&buf[SENDING_PROTOCOL_MAC_POS], &u8mac, 6);
olympux 12:7c152c0ca4d8 600 memcpy(&buf[SENDING_PROTOCOL_IP_POS], &u8ip_addr, 4);
olympux 11:709f90a3b599 601
olympux 12:7c152c0ca4d8 602 buf[SENDING_PROTOCOL_DI_POS+0] = (din0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 603 buf[SENDING_PROTOCOL_DI_POS+1] = (din1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 604 buf[SENDING_PROTOCOL_DI_POS+2] = (din2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 605 buf[SENDING_PROTOCOL_DI_POS+3] = (din3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 606 buf[SENDING_PROTOCOL_DI_POS+4] = (din4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 607 buf[SENDING_PROTOCOL_DI_POS+5] = (din5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 608 buf[SENDING_PROTOCOL_DI_POS+6] = (din6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 609 buf[SENDING_PROTOCOL_DI_POS+7] = (din7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 11:709f90a3b599 610
olympux 12:7c152c0ca4d8 611 buf[SENDING_PROTOCOL_DO_POS+0] = (dout0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 612 buf[SENDING_PROTOCOL_DO_POS+1] = (dout1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 613 buf[SENDING_PROTOCOL_DO_POS+2] = (dout2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 614 buf[SENDING_PROTOCOL_DO_POS+3] = (dout3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 615 buf[SENDING_PROTOCOL_DO_POS+4] = (dout4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 616 buf[SENDING_PROTOCOL_DO_POS+5] = (dout5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 617 buf[SENDING_PROTOCOL_DO_POS+6] = (dout6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 618 buf[SENDING_PROTOCOL_DO_POS+7] = (dout7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 11:709f90a3b599 619
olympux 15:edeb0aed160d 620 uint16_t val = ain0.read_u16(); // 16-bits normalised
olympux 15:edeb0aed160d 621 memcpy(&buf[SENDING_PROTOCOL_AI0_POS], &val, 2); // LSB MSB
olympux 15:edeb0aed160d 622 val = ain1.read_u16(); // 16-bits normalised
olympux 15:edeb0aed160d 623 memcpy(&buf[SENDING_PROTOCOL_AI1_POS], &val, 2); // LSB MSB
olympux 15:edeb0aed160d 624 val = 0x0180;
olympux 15:edeb0aed160d 625 memcpy(&buf[SENDING_PROTOCOL_AO0_POS], &val, 2); // LSB MSB
olympux 15:edeb0aed160d 626 val = 0x0180;
olympux 15:edeb0aed160d 627 memcpy(&buf[SENDING_PROTOCOL_AO1_POS], &val, 2); // LSB MSB
olympux 12:7c152c0ca4d8 628 buf[SENDING_PROTOCOL_CR_POS] = 0x0D;
olympux 12:7c152c0ca4d8 629 buf[SENDING_PROTOCOL_CR_POS+1] = '\0';
olympux 4:568c97f2a407 630 }