forked from raburton/rboot-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
175 lines (146 loc) · 4.97 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//////////////////////////////////////////////////
// rBoot sample project.
// Copyright 2015 Richard A Burton
// See license.txt for license terms.
//////////////////////////////////////////////////
#include <c_types.h>
#include <osapi.h>
#include <user_interface.h>
#include <time.h>
#include <mem.h>
#include "main.h"
#include "user_config.h"
#include "rboot-ota.h"
#include "uart.h"
static os_timer_t network_timer;
void ICACHE_FLASH_ATTR user_rf_pre_init() {
}
void ICACHE_FLASH_ATTR network_wait_for_ip() {
struct ip_info ipconfig;
os_timer_disarm(&network_timer);
wifi_get_ip_info(STATION_IF, &ipconfig);
if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) {
char page_buffer[40];
os_sprintf(page_buffer,"ip: %d.%d.%d.%d\r\n",IP2STR(&ipconfig.ip));
uart0_send(page_buffer);
} else {
char page_buffer[40];
os_sprintf(page_buffer,"network retry, status: %d\r\n",wifi_station_get_connect_status());
if(wifi_station_get_connect_status() == 3) wifi_station_connect();
uart0_send(page_buffer);
os_timer_setfn(&network_timer, (os_timer_func_t *)network_wait_for_ip, NULL);
os_timer_arm(&network_timer, 2000, 0);
}
}
void ICACHE_FLASH_ATTR wifi_config_station() {
struct station_config stationConf;
wifi_set_opmode(0x1);
stationConf.bssid_set = 0;
os_strcpy(&stationConf.ssid, WIFI_SSID, os_strlen(WIFI_SSID));
os_strcpy(&stationConf.password, WIFI_PWD, os_strlen(WIFI_PWD));
wifi_station_set_config(&stationConf);
uart0_send("wifi connecting...\r\n");
wifi_station_connect();
os_timer_disarm(&network_timer);
os_timer_setfn(&network_timer, (os_timer_func_t *)network_wait_for_ip, NULL);
os_timer_arm(&network_timer, 2000, 0);
}
void ICACHE_FLASH_ATTR ShowIP() {
struct ip_info ipconfig;
char msg[50];
wifi_get_ip_info(STATION_IF, &ipconfig);
if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) {
os_sprintf(msg, "ip: %d.%d.%d.%d, mask: %d.%d.%d.%d, gw: %d.%d.%d.%d\r\n",
IP2STR(&ipconfig.ip), IP2STR(&ipconfig.netmask), IP2STR(&ipconfig.gw));
} else {
os_sprintf(msg, "network status: %d\r\n", wifi_station_get_connect_status());
}
uart0_send(msg);
}
void ICACHE_FLASH_ATTR ShowInfo() {
char msg[50];
os_sprintf(msg, "\r\nSDK: v%s\r\n", system_get_sdk_version());
uart0_send(msg);
os_sprintf(msg, "Free Heap: %d\r\n", system_get_free_heap_size());
uart0_send(msg);
os_sprintf(msg, "CPU Frequency: %d MHz\r\n", system_get_cpu_freq());
uart0_send(msg);
os_sprintf(msg, "System Chip ID: 0x%x\r\n", system_get_chip_id());
uart0_send(msg);
os_sprintf(msg, "SPI Flash ID: 0x%x\r\n", spi_flash_get_id());
uart0_send(msg);
os_sprintf(msg, "SPI Flash Size: %d\r\n", (1 << ((spi_flash_get_id() >> 16) & 0xff)));
uart0_send(msg);
}
void ICACHE_FLASH_ATTR Switch() {
char msg[50];
uint8 before, after;
before = rboot_get_current_rom();
if (before == 0) after = 1; else after = 0;
os_sprintf(msg, "Swapping from rom %d to rom %d.\r\n", before, after);
uart0_send(msg);
rboot_set_current_rom(after);
uart0_send("Restarting...\r\n\r\n");
system_restart();
}
static void ICACHE_FLASH_ATTR OtaUpdate_CallBack(bool result, uint8 rom_slot) {
if(result == true) {
// success
if (rom_slot == FLASH_BY_ADDR) {
uart0_send("Write successful.\r\n");
} else {
// set to boot new rom and then reboot
char msg[40];
os_sprintf(msg, "Firmware updated, rebooting to rom %d...\r\n", rom_slot);
uart0_send(msg);
rboot_set_current_rom(rom_slot);
system_restart();
}
} else {
// fail
uart0_send("Firmware update failed!\r\n");
}
}
static void ICACHE_FLASH_ATTR OtaUpdate() {
// start the upgrade process
if (rboot_ota_start((ota_callback)OtaUpdate_CallBack)) {
uart0_send("Updating...\r\n");
} else {
uart0_send("Updating failed!\r\n\r\n");
}
}
void ICACHE_FLASH_ATTR ProcessCommand(char* str) {
if (!strcmp(str, "help")) {
uart0_send("available commands\r\n");
uart0_send(" help - display this message\r\n");
uart0_send(" ip - show current ip address\r\n");
uart0_send(" connect - connect to wifi\r\n");
uart0_send(" restart - restart the esp8266\r\n");
uart0_send(" switch - switch to the other rom and reboot\r\n");
uart0_send(" ota - perform ota update, switch rom and reboot\r\n");
uart0_send(" info - show esp8266 info\r\n");
uart0_send("\r\n");
} else if (!strcmp(str, "connect")) {
wifi_config_station();
} else if (!strcmp(str, "restart")) {
uart0_send("Restarting...\r\n\r\n");
system_restart();
} else if (!strcmp(str, "switch")) {
Switch();
} else if (!strcmp(str, "ota")) {
OtaUpdate();
} else if (!strcmp(str, "ip")) {
ShowIP();
} else if (!strcmp(str, "info")) {
ShowInfo();
}
}
void ICACHE_FLASH_ATTR user_init(void) {
char msg[50];
uart_init(BIT_RATE_115200,BIT_RATE_115200);
uart0_send("\r\n\r\nrBoot Sample Project\r\n");
os_sprintf(msg, "\r\nCurrently running rom %d.\r\n", rboot_get_current_rom());
uart0_send(msg);
uart0_send("type \"help\" and press <enter> for help...\r\n");
}