Compare commits
10 Commits
a3e823c57a
...
9754257a40
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9754257a40 | ||
|
|
ee28ccf5a7 | ||
|
|
21b4e242a0 | ||
|
|
793817d00e | ||
|
|
88dadb4f0e | ||
|
|
a61905726e | ||
|
|
a8a5bb61af | ||
|
|
1e15f28f72 | ||
|
|
c026c49fac | ||
|
|
ce9fac6b32 |
@@ -32,6 +32,7 @@ list(APPEND srcs "drivers/sertrf/sertrf.c")
|
||||
list(APPEND srcs "drivers/sertrf/device.c")
|
||||
list(APPEND srcs "drivers/sertrf/key.c")
|
||||
list(APPEND srcs "drivers/sertrf/ota_u.c")
|
||||
list(APPEND srcs "drivers/sertrf/tool.c")
|
||||
list(APPEND srcs "drivers/sertrf/ring_buffer.c")
|
||||
list(APPEND srcs "drivers/sertrf/protocol/MSP.c")
|
||||
list(APPEND srcs "drivers/sertrf/protocol/p_protocol.c")
|
||||
|
||||
@@ -6,14 +6,18 @@ rmt_transmit_config_t tx_config = {
|
||||
.loop_count = 0, // no transfer loop
|
||||
};
|
||||
|
||||
// 颜色结构体
|
||||
static rgb_color_t expression[EXAMPLE_LED_NUMBERS];
|
||||
static rgb_color_t expression_lat[EXAMPLE_LED_NUMBERS] = {0};
|
||||
// 延时计数
|
||||
static uint16_t rgb_toggle_cnt[EXAMPLE_LED_NUMBERS] = {10,10};
|
||||
static uint16_t rgb_toggle_cnt_lat[EXAMPLE_LED_NUMBERS] = {0};
|
||||
static uint16_t rgb_toggle_cnt_lat[EXAMPLE_LED_NUMBERS] = {2000,2000};
|
||||
|
||||
static uint8_t led_strip_pixels[EXAMPLE_LED_NUMBERS * 3];
|
||||
uint16_t toggle_cycle = 1000;
|
||||
// 翻转标志位
|
||||
bool toggle_flag[EXAMPLE_LED_NUMBERS] = {true,true};
|
||||
bool toggle_flag_lat[EXAMPLE_LED_NUMBERS] = {true,true};
|
||||
|
||||
rgb_color_t rgb_color_rad = {0,255,0,0,0,1,0,RGB_COLOR_RAD};
|
||||
rgb_color_t rgb_color_orange = {0,255,80,0,0,1,0,RGB_COLOR_ORANGE};
|
||||
@@ -95,39 +99,44 @@ void rgb_update_cyle(uint8_t index, uint16_t cyle)
|
||||
}
|
||||
void rgb_color_change(uint8_t index, uint8_t color)
|
||||
{
|
||||
switch(color)
|
||||
{
|
||||
case RGB_COLOR_RAD:
|
||||
memcpy(&expression[index], &rgb_color_rad, sizeof(rgb_color_rad));
|
||||
break;
|
||||
case RGB_COLOR_ORANGE:
|
||||
memcpy(&expression[index], &rgb_color_orange, sizeof(rgb_color_orange));
|
||||
break;
|
||||
case RGB_COLOR_GREEN:
|
||||
memcpy(&expression[index], &rgb_color_green, sizeof(rgb_color_green));
|
||||
break;
|
||||
case RGB_COLOR_WHITE:
|
||||
memcpy(&expression[index], &rgb_color_white, sizeof(rgb_color_white));
|
||||
break;
|
||||
case RGB_COLOR_PURPLE:
|
||||
memcpy(&expression[index], &rgb_color_purple, sizeof(rgb_color_purple));
|
||||
break;
|
||||
case RGB_COLOR_CYAN:
|
||||
memcpy(&expression[index], &rgb_color_cyan, sizeof(rgb_color_cyan));
|
||||
break;
|
||||
case RGB_COLOR_BLUE:
|
||||
memcpy(&expression[index], &rgb_color_blue, sizeof(rgb_color_blue));
|
||||
break;
|
||||
case RGB_COLOR_GREEN_WHITE:
|
||||
memcpy(&expression[index], &rgb_color_green_white, sizeof(rgb_color_green_white));
|
||||
break;
|
||||
case RGB_COLOR_GREEN_PURPLE:
|
||||
memcpy(&expression[index], &rgb_color_green_purple, sizeof(rgb_color_green_purple));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
expression[index].index = index;
|
||||
static uint8_t rgb_color_lat[EXAMPLE_LED_NUMBERS] = {0};
|
||||
if(rgb_color_lat[index] != color)
|
||||
{
|
||||
switch(color)
|
||||
{
|
||||
case RGB_COLOR_RAD:
|
||||
memcpy(&expression[index], &rgb_color_rad, sizeof(rgb_color_rad));
|
||||
break;
|
||||
case RGB_COLOR_ORANGE:
|
||||
memcpy(&expression[index], &rgb_color_orange, sizeof(rgb_color_orange));
|
||||
break;
|
||||
case RGB_COLOR_GREEN:
|
||||
memcpy(&expression[index], &rgb_color_green, sizeof(rgb_color_green));
|
||||
break;
|
||||
case RGB_COLOR_WHITE:
|
||||
memcpy(&expression[index], &rgb_color_white, sizeof(rgb_color_white));
|
||||
break;
|
||||
case RGB_COLOR_PURPLE:
|
||||
memcpy(&expression[index], &rgb_color_purple, sizeof(rgb_color_purple));
|
||||
break;
|
||||
case RGB_COLOR_CYAN:
|
||||
memcpy(&expression[index], &rgb_color_cyan, sizeof(rgb_color_cyan));
|
||||
break;
|
||||
case RGB_COLOR_BLUE:
|
||||
memcpy(&expression[index], &rgb_color_blue, sizeof(rgb_color_blue));
|
||||
break;
|
||||
case RGB_COLOR_GREEN_WHITE:
|
||||
memcpy(&expression[index], &rgb_color_green_white, sizeof(rgb_color_green_white));
|
||||
break;
|
||||
case RGB_COLOR_GREEN_PURPLE:
|
||||
memcpy(&expression[index], &rgb_color_green_purple, sizeof(rgb_color_green_purple));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
expression[index].index = index;
|
||||
rgb_color_lat[index] = color;
|
||||
}
|
||||
}
|
||||
void rgb_toggle(uint8_t index, rgb_color_t* rgb_color)
|
||||
{
|
||||
@@ -220,19 +229,26 @@ void rgb_Indicator_light_on(uint8_t index, uint8_t color, uint16_t cyle)
|
||||
memcpy(&expression_lat[index], &expression[index], sizeof(rgb_color_t));
|
||||
rgb_color_change(index, color);
|
||||
}
|
||||
if(rgb_toggle_cnt_lat[index] == 0 && rgb_toggle_cnt[index] != cyle)
|
||||
// 设置较大的时间计数,防止数值被轻易到达
|
||||
if(rgb_toggle_cnt_lat[index] == 2000 )
|
||||
{
|
||||
rgb_toggle_cnt_lat[index] = rgb_toggle_cnt[index];
|
||||
printf("rgb_toggle_cnt[index]:%d\n",rgb_toggle_cnt[index]);
|
||||
rgb_update_cyle(index, cyle);
|
||||
}
|
||||
|
||||
if(toggle_flag[index] == false)
|
||||
{
|
||||
toggle_flag_lat[index] = !toggle_flag_lat[index];
|
||||
toggle_flag[index] = !toggle_flag[index];
|
||||
}
|
||||
}
|
||||
void rgb_Indicator_light_off(uint8_t index)
|
||||
{
|
||||
|
||||
rgb_color_change(index, expression_lat[index].color_type);
|
||||
rgb_update_cyle(index,(rgb_toggle_cnt_lat[index] + 1) * WORD_TIME_MS );
|
||||
|
||||
toggle_flag[index] = toggle_flag_lat[index];
|
||||
|
||||
memset(&expression_lat[index], 0, sizeof(rgb_color_t));
|
||||
rgb_toggle_cnt_lat[index] = 0;
|
||||
rgb_toggle_cnt_lat[index] = 2000;
|
||||
}
|
||||
|
||||
@@ -83,6 +83,20 @@ void pin_cfg_output(const cfg_board_pin_io_t *pin)
|
||||
gpio_config(&io_conf);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void gpio_to_high_z(const cfg_board_pin_io_t *pin)
|
||||
{
|
||||
gpio_config_t io = {
|
||||
.pin_bit_mask = 1ULL << pin->pin,
|
||||
.mode = GPIO_MODE_DISABLE, // 或 GPIO_MODE_INPUT(都不会驱动外部)
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
gpio_config(&io);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief IO 基本操作:设置使输出是否为有效的电平
|
||||
*
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
void pin_cfg_input(const cfg_board_pin_io_t *pin);
|
||||
bool pin_get_valid(const cfg_board_pin_io_t *pin);
|
||||
|
||||
|
||||
void gpio_to_high_z(const cfg_board_pin_io_t *pin);
|
||||
void pin_cfg_output(const cfg_board_pin_io_t *pin);
|
||||
void pin_set_valid(const cfg_board_pin_io_t *pin, bool en);
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@
|
||||
*/
|
||||
static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect);
|
||||
|
||||
// 内部全局变量
|
||||
static uint8_t* rf_flag_off_overall = NULL;
|
||||
static device_t *device_inside;
|
||||
|
||||
uint8_t device_init(device_t *port)
|
||||
{
|
||||
uint8_t res = DEVICE_OK;
|
||||
@@ -53,12 +57,17 @@ uint8_t device_init(device_t *port)
|
||||
SYS_LOG_ERR("app device choice error");
|
||||
}
|
||||
SYS_LOG_INF("device init success");
|
||||
// 赋值全局变量 RF开关
|
||||
rf_flag_off_overall = &port->rf_flag_off;
|
||||
// 赋值设备
|
||||
device_inside = port;
|
||||
|
||||
return DEVICE_OK;
|
||||
}
|
||||
uint8_t uart_init(init_device_t *port)
|
||||
{
|
||||
// port->uart_port = sb_uart_port_bind(g_cfg_board->uart_fc.id, g_cfg_board->uart_fc.br, g_cfg_board->uart_fc.pin_txd.pin, g_cfg_board->uart_fc.pin_rxd.pin, g_cfg_board->uart_fc.irq_prior, 1024, 0, NULL);
|
||||
port->uart_port = sb_uart_port_bind(1, 115200, 21, 20, 21, 1024, 0, NULL);
|
||||
port->uart_port = sb_uart_port_bind(1, 115200, 21, 20, 21, 2048, 0, NULL);
|
||||
|
||||
if(port->uart_port == NULL)
|
||||
{
|
||||
@@ -75,21 +84,26 @@ uint8_t uart_init(init_device_t *port)
|
||||
// 蓝牙连接回调函数
|
||||
static void ble_server_connect_handler(ble_server_status_t status)
|
||||
{
|
||||
// 处理连接状态,例如:
|
||||
if (status == BLE_SERVER_STATUS_CONNECTED) {
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, CONNECT_BLE);
|
||||
if(res == DEVICE_PC_ERROR){
|
||||
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0){
|
||||
// 处理连接状态,例如:
|
||||
if (status == BLE_SERVER_STATUS_CONNECTED) {
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, CONNECT_BLE);
|
||||
if(res == DEVICE_PC_ERROR){
|
||||
}
|
||||
device_wifi_stop();
|
||||
SYS_LOG_INF("ble Connected");
|
||||
} else if(status == BLE_SERVER_STATUS_DISCONNECTED){
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
|
||||
if(res == DEVICE_PC_ERROR){
|
||||
}
|
||||
device_wifi_start();
|
||||
SYS_LOG_INF("ble dis Connected");
|
||||
// disconnected / other
|
||||
} else {
|
||||
SYS_LOG_INF("ble stop");
|
||||
}
|
||||
SYS_LOG_INF("ble Connected");
|
||||
} else if(status == BLE_SERVER_STATUS_DISCONNECTED){
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
|
||||
if(res == DEVICE_PC_ERROR){
|
||||
}
|
||||
SYS_LOG_INF("ble dis Connected");
|
||||
// disconnected / other
|
||||
} else {
|
||||
SYS_LOG_INF("ble stop");
|
||||
}
|
||||
|
||||
}
|
||||
static void _sb_manufacturer_encode(uint8_t manufacturer_data[20])
|
||||
{
|
||||
@@ -155,42 +169,50 @@ static void wifi_event_handler(bool is_connect, sb_data_port_t *port)
|
||||
}
|
||||
static void wifi_ap_connect_handler(wifi_ap_connect_status_t status, uint8_t connect_cnt)
|
||||
{
|
||||
SYS_LOG_WRN("wifi ap connect status %d %d", (int)status, (int)connect_cnt);
|
||||
//实现自动切换发送对象
|
||||
if((connect_cnt == 1 && status == WIFI_AP_CONNECT_GOT_STA)){
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("wifi pc device choice error");
|
||||
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0)
|
||||
{
|
||||
SYS_LOG_WRN("wifi ap connect status %d %d", (int)status, (int)connect_cnt);
|
||||
//实现自动切换发送对象
|
||||
if((connect_cnt == 1 && status == WIFI_AP_CONNECT_GOT_STA)){
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("wifi pc device choice error");
|
||||
}
|
||||
device_ble_stop();
|
||||
SYS_LOG_INF("wifi connect");
|
||||
}else{
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("wifi pc device choice error");
|
||||
}
|
||||
device_ble_start();
|
||||
SYS_LOG_INF("wifi disconnect");
|
||||
}
|
||||
SYS_LOG_INF("wifi connect");
|
||||
}else{
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("wifi pc device choice error");
|
||||
}
|
||||
SYS_LOG_INF("wifi disconnect");
|
||||
}
|
||||
}
|
||||
static void wifi_sta_connect_handler(wifi_sta_connect_status_t status, uint8_t ip_v4[4])
|
||||
{
|
||||
SYS_LOG_WRN("wifi ap connect status %d", (int)status);
|
||||
//实现自动切换发送对象
|
||||
if(status == WIFI_STA_CONNECT_GOT_IP){
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("wifi pc device choice error");
|
||||
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0)
|
||||
{
|
||||
SYS_LOG_WRN("wifi ap connect status %d", (int)status);
|
||||
//实现自动切换发送对象
|
||||
if(status == WIFI_STA_CONNECT_GOT_IP){
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("wifi pc device choice error");
|
||||
}
|
||||
SYS_LOG_INF("wifi connect");
|
||||
}else{
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("wifi pc device choice error");
|
||||
}
|
||||
SYS_LOG_INF("wifi disconnect");
|
||||
}
|
||||
SYS_LOG_INF("wifi connect");
|
||||
}else{
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("wifi pc device choice error");
|
||||
}
|
||||
SYS_LOG_INF("wifi disconnect");
|
||||
}
|
||||
}
|
||||
uint8_t wifi_init(init_device_t *port)
|
||||
@@ -283,6 +305,51 @@ void wifi_mode_switch(init_device_t *port)
|
||||
}
|
||||
wifi_start();
|
||||
}
|
||||
void device_wifi_start(void)
|
||||
{
|
||||
device_inside->wifi_flag_off = 0;
|
||||
sb_data_port_start(device_inside->init_device.wifi_tcp);
|
||||
sb_data_port_start(device_inside->init_device.wifi_udp);
|
||||
}
|
||||
void device_wifi_stop(void)
|
||||
{
|
||||
device_inside->wifi_flag_off = 1;
|
||||
sb_data_port_stop(device_inside->init_device.wifi_tcp);
|
||||
sb_data_port_stop(device_inside->init_device.wifi_udp);
|
||||
}
|
||||
void device_ble_start(void)
|
||||
{
|
||||
device_inside->ble_flag_off = 0;
|
||||
sb_data_port_start(device_inside->init_device.ble_spp_server_cmd);
|
||||
sb_data_port_start(device_inside->init_device.ble_spp_server_val);
|
||||
}
|
||||
void device_ble_stop(void)
|
||||
{
|
||||
device_inside->ble_flag_off = 1;
|
||||
sb_data_port_stop(device_inside->init_device.ble_spp_server_cmd);
|
||||
sb_data_port_stop(device_inside->init_device.ble_spp_server_val);
|
||||
}
|
||||
|
||||
void rf_start(device_t *port)
|
||||
{
|
||||
port->rf_flag_off = 0;
|
||||
sb_data_port_start(port->init_device.wifi_tcp);
|
||||
sb_data_port_start(port->init_device.wifi_udp);
|
||||
sb_data_port_start(port->init_device.ble_spp_server_cmd);
|
||||
sb_data_port_start(port->init_device.ble_spp_server_val);
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
|
||||
}
|
||||
|
||||
void rf_stop(device_t *port)
|
||||
{
|
||||
port->rf_flag_off = 1;
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_RF_OFF, CONNECT_RF_OFF);
|
||||
sb_data_port_stop(port->init_device.wifi_tcp);
|
||||
sb_data_port_stop(port->init_device.wifi_udp);
|
||||
sb_data_port_stop(port->init_device.ble_spp_server_cmd);
|
||||
sb_data_port_stop(port->init_device.ble_spp_server_val);
|
||||
}
|
||||
|
||||
uint8_t embedded_device_choice(device_t *port, uint8_t type)
|
||||
{
|
||||
//端口与PC一致
|
||||
@@ -352,13 +419,12 @@ uint32_t embedded_device_get_rx_length(device_t *port)
|
||||
static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect)
|
||||
{
|
||||
static device_t* port_device_inside = NULL;
|
||||
SYS_LOG_WRN("pc_device_choice_inside type %d connect %d", type, connect);
|
||||
if(port != NULL && port_device_inside == NULL){
|
||||
SYS_LOG_INF("pc_device_choice_inside");
|
||||
pc_device_choice(port, type);
|
||||
port_device_inside = port;
|
||||
port_device_inside->connect_pc = connect;
|
||||
}else if(port == NULL && port_device_inside != NULL && (port_device_inside->pc_device_type == type || connect != DISCONNECT)){
|
||||
}else if(port == NULL && port_device_inside != NULL && (port_device_inside->pc_device_type == type || connect != DISCONNECT || port_device_inside->pc_device_type == DATA_PORT_TYPE_RF_OFF)){
|
||||
pc_device_choice(port_device_inside, type);
|
||||
port_device_inside->connect_pc = connect;
|
||||
}
|
||||
@@ -410,6 +476,9 @@ int pc_device_read(device_t *port, void *buffer, uint32_t length)
|
||||
{
|
||||
if (port->pc_device == NULL)
|
||||
return -1;
|
||||
//判断是否开启射频
|
||||
if(port->rf_flag_off && port->pc_device_type > 1)
|
||||
return -2;
|
||||
|
||||
return sb_data_port_read(port->pc_device, buffer, length, 0);
|
||||
}
|
||||
@@ -418,6 +487,9 @@ int pc_device_write(device_t *port, void *buffer, uint32_t length)
|
||||
{
|
||||
if(port->pc_device == NULL)
|
||||
return -1;
|
||||
//判断是否开启射频
|
||||
if(port->rf_flag_off && port->pc_device_type > 1)
|
||||
return -2;
|
||||
|
||||
if(port->connect_pc)
|
||||
return sb_data_port_write(port->pc_device, buffer, length, 0);
|
||||
@@ -428,7 +500,9 @@ uint32_t pc_device_get_rx_length(device_t *port)
|
||||
{
|
||||
if(port->pc_device == NULL)
|
||||
return 0;
|
||||
|
||||
//判断是否开启射频
|
||||
if(port->rf_flag_off && port->pc_device_type > 1)
|
||||
return 0;
|
||||
return sb_data_port_get_rx_length(port->pc_device);
|
||||
}
|
||||
|
||||
@@ -479,6 +553,10 @@ int app_device_read(device_t *port, void *buffer, uint32_t length, uint32_t time
|
||||
if(port->app_device == NULL)
|
||||
return -1;
|
||||
|
||||
//判断是否开启射频
|
||||
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
||||
return -2;
|
||||
|
||||
return sb_data_port_read(port->app_device, buffer, length, timeout);
|
||||
}
|
||||
|
||||
@@ -487,6 +565,10 @@ int app_device_write(device_t *port, void *buffer, uint32_t length)
|
||||
if(port->app_device == NULL)
|
||||
return -1;
|
||||
|
||||
//判断是否开启射频
|
||||
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
||||
return -2;
|
||||
|
||||
return sb_data_port_write(port->app_device, buffer, length, 0);
|
||||
}
|
||||
uint32_t app_device_get_rx_length(device_t *port)
|
||||
@@ -494,5 +576,9 @@ uint32_t app_device_get_rx_length(device_t *port)
|
||||
if(port->app_device == NULL)
|
||||
return 0;
|
||||
|
||||
//判断是否开启射频
|
||||
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
||||
return -2;
|
||||
|
||||
return sb_data_port_get_rx_length(port->app_device);
|
||||
}
|
||||
@@ -39,7 +39,9 @@ enum DATA_PORT_TYPE
|
||||
DATA_PORT_TYPE_WIFI_TCP,
|
||||
DATA_PORT_TYPE_WIFI_UDP,
|
||||
DATA_PORT_TYPE_BLE_CMD,
|
||||
DATA_PORT_TYPE_BLE_VAL
|
||||
DATA_PORT_TYPE_BLE_VAL,
|
||||
|
||||
DATA_PORT_TYPE_RF_OFF = 25,
|
||||
};
|
||||
enum CONNECT_TYPE
|
||||
{
|
||||
@@ -48,6 +50,8 @@ enum CONNECT_TYPE
|
||||
CONNECT_BLE,
|
||||
CONNECT_WIFI_TCP,
|
||||
CONNECT_WIFI_UDP,
|
||||
|
||||
CONNECT_RF_OFF = 25,
|
||||
};
|
||||
enum WIFI_MODE
|
||||
{
|
||||
@@ -84,6 +88,10 @@ typedef struct
|
||||
|
||||
uint8_t connect_embedded;
|
||||
uint8_t connect_pc;
|
||||
//射频信号是否开启 0开启 1关闭
|
||||
uint8_t rf_flag_off;
|
||||
uint8_t wifi_flag_off;
|
||||
uint8_t ble_flag_off;
|
||||
// int (*embedded_read)(device_t *port, void *buffer, uint32_t length);
|
||||
// int (*embedded_write)(device_t *port, const void *data, uint32_t size);
|
||||
uint8_t last_color;
|
||||
@@ -122,6 +130,35 @@ uint8_t wifi_init(init_device_t *port);
|
||||
* @brief WIFI 模式切换
|
||||
*/
|
||||
void wifi_mode_switch(init_device_t *port);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
void device_wifi_start(void);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
void device_wifi_stop(void);
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
void device_ble_start(void);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
void device_ble_stop(void);
|
||||
/**
|
||||
* @brief 开启所有射频信号
|
||||
*/
|
||||
void rf_start(device_t *port);
|
||||
|
||||
/**
|
||||
* @brief 关闭所有射频信号
|
||||
*/
|
||||
void rf_stop(device_t *port);
|
||||
|
||||
/**
|
||||
* @brief 嵌入式设备数据接口
|
||||
*
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#include "key.h"
|
||||
|
||||
#include "sertrf.h"
|
||||
button_t btn;
|
||||
|
||||
cfg_board_pin_io_t key_switch = {
|
||||
.pin = 9,
|
||||
.en_lev = 0,};
|
||||
cfg_board_pin_io_t boot_switch = {
|
||||
.pin = ~0,
|
||||
.pin = 18,
|
||||
.en_lev = 0,};
|
||||
// 新增事件类型
|
||||
// EVT_DOUBLE_CLICK: 双击
|
||||
@@ -14,7 +14,7 @@ cfg_board_pin_io_t boot_switch = {
|
||||
|
||||
bool key_single_click = 0,key_press_down = 0,key_press_up = 0;
|
||||
|
||||
|
||||
static os_work_t work_handler_button;
|
||||
// 用户在此处理按键事件
|
||||
static void my_button_handler(button_event_t evt) {
|
||||
switch (evt) {
|
||||
@@ -34,11 +34,15 @@ static void my_button_handler(button_event_t evt) {
|
||||
break;
|
||||
case EVT_DOUBLE_CLICK: //双击
|
||||
// SYS_LOG_INF("[Event] DOUBLE_CLICK");
|
||||
// sertrf_rf_switch(1);
|
||||
// aes_test();
|
||||
key_test();
|
||||
break;
|
||||
case EVT_LONG_PRESS: //长按
|
||||
SYS_LOG_INF("[Event] LONG_PRESS");
|
||||
Examples_run();
|
||||
rgb_Indicator_light_off(0);
|
||||
rgb_Indicator_light_off(0); //如果处理有变色,需要该函数先调用
|
||||
// sertrf_rf_switch(0);
|
||||
break;
|
||||
case EVT_SINGLE_LONG_PRESS: //单击后长按
|
||||
rgb_Indicator_light_off(0);
|
||||
@@ -169,16 +173,39 @@ void _work_button(void *arg) {
|
||||
void button_work_init() {
|
||||
|
||||
pin_cfg_input(&key_switch);
|
||||
pin_cfg_output(&boot_switch);
|
||||
// pin_cfg_output(&boot_switch);
|
||||
gpio_to_high_z(&boot_switch);
|
||||
|
||||
button_init(&btn, read_button_pin, my_button_handler);
|
||||
|
||||
static os_work_t work_handler_button;
|
||||
os_work_create(&work_handler_button, "work-button", _work_button, NULL, OS_PRIORITY_NORMAL);
|
||||
os_work_submit(default_os_work_q_hdl, &work_handler_button, 10);
|
||||
}
|
||||
|
||||
void boot_set(uint8_t value)
|
||||
{
|
||||
// 设置按键为高阻态,防止冲突
|
||||
gpio_to_high_z(&key_switch);
|
||||
os_work_suspend(&work_handler_button);
|
||||
|
||||
pin_cfg_output(&boot_switch);
|
||||
pin_set_valid(&boot_switch, value);
|
||||
}
|
||||
|
||||
void boot_set_high_z(void)
|
||||
{
|
||||
gpio_to_high_z(&boot_switch);
|
||||
//重新初始化按键
|
||||
pin_cfg_input(&key_switch);
|
||||
os_work_resume(&work_handler_button,10);
|
||||
// os_work_later(10);
|
||||
}
|
||||
|
||||
void key_test(void)
|
||||
{
|
||||
boot_set(0);
|
||||
|
||||
os_thread_sleep(5000);
|
||||
|
||||
boot_set_high_z();
|
||||
}
|
||||
@@ -47,5 +47,17 @@ typedef struct {
|
||||
|
||||
extern bool key_single_click,key_press_down,key_press_up;
|
||||
void _work_button(void *arg);
|
||||
/**
|
||||
* @brief 初始化按键
|
||||
*/
|
||||
void button_work_init();
|
||||
/**
|
||||
* @brief 设置boot按键输出
|
||||
*/
|
||||
void boot_set(uint8_t value);
|
||||
/**
|
||||
* @brief 设置boot按键为高阻态
|
||||
*/
|
||||
void boot_set_high_z(void);
|
||||
|
||||
void key_test(void);
|
||||
@@ -15,7 +15,7 @@ typedef struct
|
||||
esp_ota_handle_t ota_handle;
|
||||
int total_size;
|
||||
uint16_t ota_data_cnt;
|
||||
uint8_t ota_buff[20480 + 512];
|
||||
// uint8_t ota_buff[20480 + 512];
|
||||
}ota_u_t;
|
||||
|
||||
typedef struct
|
||||
|
||||
@@ -145,15 +145,17 @@ protocol_status_t get_protocol_status(void)
|
||||
{
|
||||
protocol.protocol_status = PROTOCOL_STATUS_OK;
|
||||
|
||||
if(protocol.pro_type == PROTOCOL_IDLE)
|
||||
// 先OTA升级、未知协议、未收到飞控数据
|
||||
if(protocol.message_status == MESSAGE_OTA)
|
||||
{
|
||||
protocol.protocol_status = PROTOCOL_STATUS_IN_OTA;
|
||||
}
|
||||
else if(protocol.pro_type == PROTOCOL_IDLE)
|
||||
{
|
||||
protocol.protocol_status = PROTOCOL_STATUS_TYPE_IDLE;
|
||||
} else if(protocol.analysis_sussess_count == 0)
|
||||
{
|
||||
protocol.protocol_status = PROTOCOL_STATUS_NO_DATA;
|
||||
} else if(protocol.message_status == MESSAGE_OTA)
|
||||
{
|
||||
protocol.protocol_status = PROTOCOL_STATUS_IN_OTA;
|
||||
}
|
||||
|
||||
protocol.analysis_sussess_count = 0;
|
||||
|
||||
@@ -34,6 +34,8 @@ typedef enum
|
||||
|
||||
RESEND_CMD_DATA_ACK = 0x06,
|
||||
|
||||
RESEND_CMD_MODE_STATUS = 0x07,
|
||||
|
||||
RESEND_CMD_OTA_GET_PARAM = 0x010,
|
||||
RESEND_CMD_OTA_START = 0x011,
|
||||
RESEND_CMD_OTA_DATA = 0x012,
|
||||
|
||||
@@ -71,7 +71,7 @@ int send_cmd_wait(stmisp_device_t *stmisp_device, uint8_t cmd, uint32_t timeout)
|
||||
switch(ack)
|
||||
{
|
||||
case 1:
|
||||
printf("stmisp: ACK to cmd 0x%02x\n", cmd);
|
||||
// printf("stmisp: ACK to cmd 0x%02x\n", cmd);
|
||||
return 1;
|
||||
case 2:
|
||||
printf("stmisp: NACK to cmd 0x%02x\n", cmd);
|
||||
@@ -235,7 +235,7 @@ int cmd_write_memory(stmisp_device_t *stmisp_device, uint32_t addr, const uint8_
|
||||
stmisp_device->send((void *)&data[idx], chunk, 0);
|
||||
stmisp_device->send(&cs, 1, 0);
|
||||
|
||||
if(wait_ack(stmisp_device, 5000) != 1) return 0;
|
||||
if(wait_ack(stmisp_device, 1000) != 1) return 0;
|
||||
//是否需要校验写入的数据块
|
||||
if (verify)
|
||||
{
|
||||
|
||||
@@ -38,6 +38,10 @@ typedef struct
|
||||
uint32_t adder_offset; //地址偏移
|
||||
}stmisp_device_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t stmisp_file_size;
|
||||
}stmisp_parm_t;
|
||||
/**
|
||||
* @brief 计算校验和
|
||||
*/
|
||||
|
||||
@@ -33,6 +33,15 @@ void sertrf_init(void)
|
||||
|
||||
//获取飞控代码地址
|
||||
sertrf.fc_address = parse_hex_or_dec(FC_ADDRESS);
|
||||
|
||||
//获取加密后的efuse MAC 地址
|
||||
if(!esp_efuse_mac_get_default_id(sertrf.efuse_mac))
|
||||
{
|
||||
sertrf_aes_ctr_encrypt(sertrf.efuse_mac, 6, sertrf.efuse_mac_encrypt);
|
||||
}
|
||||
//初始化环形buff
|
||||
rb_init(&sertrf.data_handle_buffer, DATA_HANDLE_BUFFER_SIZE, sizeof(uint8_t));
|
||||
|
||||
//线程启动
|
||||
sertrf_start();
|
||||
|
||||
@@ -44,13 +53,13 @@ void sertrf_start(void)
|
||||
"embedded_thread",
|
||||
embedded_thread,
|
||||
NULL,
|
||||
4096,
|
||||
2048,
|
||||
20);
|
||||
os_thread_create(&sertrf.pc_thread,
|
||||
"pc_thread",
|
||||
pc_thread,
|
||||
NULL,
|
||||
4096,
|
||||
2048,
|
||||
20);
|
||||
os_thread_create(&sertrf.app_thread,
|
||||
"app_thread",
|
||||
@@ -64,6 +73,24 @@ void sertrf_start(void)
|
||||
NULL,
|
||||
2048,
|
||||
10);
|
||||
os_thread_create(&sertrf.data_handle_thread,
|
||||
"data_handle_thread",
|
||||
data_handle_thread,
|
||||
NULL,
|
||||
4096,
|
||||
10);
|
||||
}
|
||||
void sertrf_rf_switch(uint8_t on)
|
||||
{
|
||||
switch(on)
|
||||
{
|
||||
case 1:
|
||||
rf_start(&sertrf.device);
|
||||
break;
|
||||
case 0:
|
||||
rf_stop(&sertrf.device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
//因为 串口
|
||||
void embedded_thread(void* arg)
|
||||
@@ -124,7 +151,7 @@ void app_thread(void* arg)
|
||||
strcpy(sertrf_stauct.wifi_ap_password, g_cfg_app->app_config_wifi_para.wifi_ap_password);
|
||||
strcpy(sertrf_stauct.wifi_sta_ssid, g_cfg_app->app_config_wifi_para.wifi_sta_ssid);
|
||||
strcpy(sertrf_stauct.wifi_sta_password, g_cfg_app->app_config_wifi_para.wifi_sta_password);
|
||||
|
||||
memcpy(sertrf_stauct.efuse_mac_encrypt, sertrf.efuse_mac_encrypt, 6);
|
||||
resend_send_data(&sertrf.resend_device, RESEND_CMD_GET_PARAM, &sertrf_stauct, sizeof(sertrf_stauct_t), 1000);
|
||||
|
||||
sertrf.resend_device.handle_flag = 0;//标志位清零
|
||||
@@ -135,6 +162,12 @@ void app_thread(void* arg)
|
||||
resend_send_data(&sertrf.resend_device, RESEND_CMD_OTA_GET_PARAM, &ota_parm, sizeof(ota_parm_t), 1000);
|
||||
sertrf.resend_device.handle_flag = 0;//标志位清零
|
||||
break;
|
||||
case RESEND_CMD_FC_ISP_GET_PARAM:
|
||||
printf("RESEND_CMD_FC_ISP_GET_PARAM\r\n");
|
||||
stmisp_parm_t stmisp_parm;
|
||||
resend_send_data(&sertrf.resend_device, RESEND_CMD_FC_ISP_GET_PARAM, &stmisp_parm, sizeof(stmisp_parm), 1000);
|
||||
sertrf.resend_device.handle_flag = 0;//标志位清零
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -174,7 +207,8 @@ void task_thread(void* arg)
|
||||
switch(get_protocol_status())
|
||||
{
|
||||
case PROTOCOL_STATUS_OK:
|
||||
rgb_color_change(1, sertrf.device.last_color);
|
||||
// rgb_color_change(1, sertrf.device.last_color);
|
||||
rgb_color_change(1, RGB_COLOR_ORANGE);
|
||||
break;
|
||||
case PROTOCOL_STATUS_NO_DATA:
|
||||
rgb_color_change(1, RGB_COLOR_RAD);
|
||||
@@ -197,6 +231,110 @@ void task_thread(void* arg)
|
||||
os_thread_sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
void data_handle_thread(void* arg)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
switch(sertrf.mode_status.task_state)
|
||||
{
|
||||
case DATA_HANDLE_OTA_DATA:
|
||||
{
|
||||
size_t data_size = rb_size(&sertrf.data_handle_buffer);
|
||||
if(data_size > 1024)
|
||||
{
|
||||
uint8_t data[data_size];
|
||||
rb_get_bulk(&sertrf.data_handle_buffer, data, data_size);
|
||||
|
||||
if(otau_write(&sertrf.otau, data, data_size))
|
||||
{
|
||||
SYS_LOG_DBG("stmisp write error");
|
||||
}
|
||||
else
|
||||
{
|
||||
SYS_LOG_DBG("stmisp write ok");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
case DATA_HANDLE_OTA_DATA_END:
|
||||
{
|
||||
size_t data_size = rb_size(&sertrf.data_handle_buffer);
|
||||
if(data_size > 0)
|
||||
{
|
||||
uint8_t data[data_size];
|
||||
rb_get_bulk(&sertrf.data_handle_buffer, data, data_size);
|
||||
if(otau_write(&sertrf.otau, data, data_size))
|
||||
{
|
||||
SYS_LOG_DBG("stmisp write error");
|
||||
}
|
||||
else
|
||||
{
|
||||
SYS_LOG_DBG("stmisp write ok");
|
||||
sertrf.mode_status.task_state = DATA_HANDLE_IDLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DATA_HANDLE_ISP_DATA:
|
||||
{
|
||||
size_t data_size = rb_size(&sertrf.data_handle_buffer);
|
||||
if(data_size > 0)
|
||||
{
|
||||
uint8_t data[data_size];
|
||||
rb_get_bulk(&sertrf.data_handle_buffer, data, data_size);
|
||||
|
||||
uint32_t addr = sertrf.fc_address + sertrf.stmisp_device.adder_offset;
|
||||
|
||||
if(cmd_write_memory(&sertrf.stmisp_device, addr, data, data_size, false))
|
||||
{
|
||||
resend_send_cmd(&sertrf.resend_device, RESEND_CMD_ACK, 0);
|
||||
sertrf.stmisp_device.adder_offset += data_size;
|
||||
SYS_LOG_INF("stmisp write ok %d %d\n",data_size, sertrf.stmisp_device.adder_offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
sertrf.stmisp_device.adder_offset = 0;
|
||||
sertrf.mode_status.task_state = DATA_HANDLE_IDLE;
|
||||
SYS_LOG_DBG("stmisp write error");
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DATA_HANDLE_ISP_DATA_END:
|
||||
{
|
||||
size_t data_size = rb_size(&sertrf.data_handle_buffer);
|
||||
if(data_size > 0)
|
||||
{
|
||||
uint8_t data[data_size];
|
||||
rb_get_bulk(&sertrf.data_handle_buffer, data, data_size);
|
||||
|
||||
uint32_t addr = sertrf.fc_address + sertrf.stmisp_device.adder_offset;
|
||||
|
||||
if(cmd_write_memory(&sertrf.stmisp_device, addr, data, data_size, true))
|
||||
{
|
||||
sertrf.stmisp_device.adder_offset = 0;
|
||||
sertrf.mode_status.task_state = DATA_HANDLE_IDLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
SYS_LOG_DBG("stmisp write end error");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sertrf.stmisp_device.adder_offset = 0;
|
||||
sertrf.mode_status.task_state = DATA_HANDLE_IDLE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
os_thread_sleep(1);
|
||||
}
|
||||
}
|
||||
void pc_link_rgb_color(device_t* device)
|
||||
{
|
||||
static uint8_t last_connect = 255, last_wifi_mode = 0;
|
||||
@@ -232,7 +370,14 @@ void pc_link_rgb_color(device_t* device)
|
||||
break;
|
||||
}
|
||||
case CONNECT_BLE:
|
||||
{
|
||||
new_color = RGB_COLOR_GREEN;
|
||||
}
|
||||
break;
|
||||
case CONNECT_RF_OFF:
|
||||
{
|
||||
new_color = RGB_COLOR_BLUE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -243,45 +388,17 @@ void pc_link_rgb_color(device_t* device)
|
||||
|
||||
last_connect = device->connect_pc;
|
||||
last_wifi_mode = device->init_device.wifi_mode;
|
||||
}
|
||||
if(device->connect_pc == DISCONNECT)
|
||||
{
|
||||
rgb_update_cyle(0,50);
|
||||
}
|
||||
else if(device->connect_pc){
|
||||
rgb_update_cyle(0,888);
|
||||
}else{
|
||||
rgb_update_cyle(0,500);
|
||||
}
|
||||
}
|
||||
|
||||
void printf_chill_time(uint8_t chill_time, uint16_t type)
|
||||
{
|
||||
static size_t last_time[24] = {0};
|
||||
static uint16_t cnt[24] = {0};
|
||||
static uint32_t type_cnt_time[24] = {0};
|
||||
size_t now_time = os_get_sys_time();
|
||||
cnt[chill_time]++;
|
||||
type_cnt_time[chill_time] += now_time - last_time[chill_time];
|
||||
if(cnt[chill_time] % type == 0 && type_cnt_time[chill_time] / type >= now_time - last_time[chill_time] - 1)
|
||||
{
|
||||
SYS_LOG_INF("TIME_CHILL%d : %d : %d",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
|
||||
cnt[chill_time] = 0;
|
||||
type_cnt_time[chill_time] = 0;
|
||||
if(device->connect_pc == DISCONNECT)
|
||||
{
|
||||
rgb_update_cyle(0,50);
|
||||
}
|
||||
else if(device->connect_pc){
|
||||
rgb_update_cyle(0,888);
|
||||
}else{
|
||||
rgb_update_cyle(0,500);
|
||||
}
|
||||
}
|
||||
else if(cnt[chill_time] % type == 0)
|
||||
{
|
||||
SYS_LOG_WRN("TIME_CHILL%d : %d : %d",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
|
||||
cnt[chill_time] = 0;
|
||||
type_cnt_time[chill_time] = 0;
|
||||
}
|
||||
last_time[chill_time] = now_time;
|
||||
}
|
||||
|
||||
uint32_t parse_hex_or_dec(const char *s) {
|
||||
if (!s) return 0;
|
||||
if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) return (uint32_t)strtoul(s+2, NULL, 16);
|
||||
return (uint32_t)strtoul(s, NULL, 0);
|
||||
}
|
||||
|
||||
int resend_send(void* data, uint16_t len, int timeout)
|
||||
@@ -340,53 +457,152 @@ void resend_user_parse(void *resend_device)
|
||||
//设置协议状态,为了后续灯颜色变化
|
||||
protocol_set_message_status(MESSAGE_OTA);
|
||||
// if(!sertrf.resend_device.status.resend_flag)
|
||||
//初始化ota会占用大量系统资源,需要先把飞控获取数据关闭,不然会导致栈溢出
|
||||
sb_data_port_stop(sertrf.device.embedded_device);
|
||||
otau_init(&sertrf.otau);
|
||||
|
||||
|
||||
sertrf.mode_status.task_state = DATA_HANDLE_OTA_DATA;
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
}
|
||||
break;
|
||||
case RESEND_CMD_OTA_DATA:
|
||||
{
|
||||
memcpy(sertrf.otau.ota_buff + sertrf.otau.ota_data_cnt, sertrf.resend_device.rx_frame.payload, sertrf.resend_device.rx_frame.len);
|
||||
|
||||
sertrf.otau.ota_data_cnt += sertrf.resend_device.rx_frame.len;
|
||||
// if(!sertrf.resend_device.status.resend_flag)
|
||||
if(sertrf.otau.ota_data_cnt >= 20480 )
|
||||
protocol_set_message_status(MESSAGE_OTA);
|
||||
// 使用环形buff
|
||||
if(rb_size(&sertrf.data_handle_buffer) + sertrf.resend_device.rx_frame.len <= DATA_HANDLE_BUFFER_SIZE)
|
||||
{
|
||||
SYS_LOG_DBG("KUYI_CMD_OTA_DATA %d\n", sertrf.resend_device.rx_frame.seq);
|
||||
otau_write(&sertrf.otau, sertrf.otau.ota_buff, sertrf.otau.ota_data_cnt);
|
||||
sertrf.otau.ota_data_cnt = 0;
|
||||
rb_put_bulk(&sertrf.data_handle_buffer, sertrf.resend_device.rx_frame.payload, sertrf.resend_device.rx_frame.len);
|
||||
}
|
||||
else
|
||||
{
|
||||
SYS_LOG_DBG("RESEND_CMD_OTA_DATA buffer full");
|
||||
}
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
}
|
||||
break;
|
||||
case RESEND_CMD_OTA_END:
|
||||
{
|
||||
if(sertrf.otau.ota_data_cnt > 0)
|
||||
{
|
||||
otau_write(&sertrf.otau, sertrf.otau.ota_buff, sertrf.otau.ota_data_cnt);
|
||||
sertrf.otau.ota_data_cnt = 0;
|
||||
}
|
||||
// if(!sertrf.resend_device.status.resend_flag)
|
||||
sertrf.mode_status.task_state = DATA_HANDLE_OTA_DATA_END;
|
||||
while (sertrf.mode_status.task_state != DATA_HANDLE_IDLE){
|
||||
os_thread_sleep(10);}
|
||||
otau_end(&sertrf.otau);
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
os_thread_sleep(2);
|
||||
esp_restart();
|
||||
}
|
||||
break;
|
||||
|
||||
case RESEND_CMD_FC_ISP_GET_PARAM:
|
||||
if(sizeof(stmisp_parm_t) != resend_parse->rx_frame.len)
|
||||
{
|
||||
printf("RESEND_CMD_FC_ISP_GET_PARAM len error\r\n");
|
||||
}
|
||||
stmisp_parm_t* stmisp_parm = (stmisp_parm_t*)resend_parse->rx_frame.payload;
|
||||
printf("RESEND_CMD_FC_ISP_GET_PARAM %d\n", stmisp_parm->stmisp_file_size);
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
break;
|
||||
case RESEND_CMD_FC_ISP_START:
|
||||
/*关闭其他线程中相同串口的使用,并复位飞控,使其进入isp烧录模式,串口切换为偶校验模式
|
||||
使用sync与isp确定通讯波特率,并建立联系 解除其写保护 并重新sync建立连接后擦除全部区域内存*/
|
||||
//关闭其他线程中相同串口的使用
|
||||
sertrf.stmisp_device.flag = 1;
|
||||
// 重启飞控,并使其进入isp烧录模式
|
||||
fc_reboot();
|
||||
boot_set(0);
|
||||
os_thread_sleep(1000);
|
||||
//串口切换为偶校验模式,并清除缓存
|
||||
uart_set_parity_switch(sertrf.device.embedded_device, 0x02);
|
||||
os_thread_sleep(100);
|
||||
// 与isp确定通讯波特率,并建立联系
|
||||
if(!send_sync(&sertrf.stmisp_device, 5))
|
||||
printf("stmisp: sync error\n");
|
||||
else
|
||||
printf("stmisp: sync ok\n");
|
||||
|
||||
os_thread_sleep(100);
|
||||
|
||||
// 解除写保护
|
||||
if(!cmd_write_unprotect(&sertrf.stmisp_device))
|
||||
printf("stmisp: write unprotect error\n");
|
||||
else
|
||||
printf("stmisp: write unprotect ok\n");
|
||||
|
||||
os_thread_sleep(100);
|
||||
|
||||
|
||||
if(!send_sync(&sertrf.stmisp_device, 5))
|
||||
printf("stmisp: sync error\n");
|
||||
else
|
||||
printf("stmisp: sync ok\n");
|
||||
os_thread_sleep(100);
|
||||
|
||||
boot_set_high_z();
|
||||
// 全盘擦除
|
||||
if(!cmd_extended_erase_mass(&sertrf.stmisp_device))
|
||||
printf("stmisp: erase mass error\n");
|
||||
else
|
||||
printf("stmisp: erase mass ok\n");
|
||||
|
||||
os_thread_sleep(10);
|
||||
|
||||
printf("stmisp: isp start\n");
|
||||
|
||||
sertrf.mode_status.task_state = DATA_HANDLE_ISP_DATA;
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
break;
|
||||
case RESEND_CMD_FC_ISP_DATA:
|
||||
// 使用环形buff
|
||||
if(rb_size(&sertrf.data_handle_buffer) + sertrf.resend_device.rx_frame.len <= DATA_HANDLE_BUFFER_SIZE)
|
||||
{
|
||||
rb_put_bulk(&sertrf.data_handle_buffer, sertrf.resend_device.rx_frame.payload, sertrf.resend_device.rx_frame.len);
|
||||
}
|
||||
else
|
||||
{
|
||||
SYS_LOG_DBG("RESEND_CMD_FC_ISP_DATA buffer full");
|
||||
}
|
||||
// resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
break;
|
||||
case RESEND_CMD_FC_ISP_END:
|
||||
// 告诉数据处理线程,ISP烧录数据传输完成
|
||||
sertrf.mode_status.task_state = DATA_HANDLE_ISP_DATA_END;
|
||||
// 等待数据处理线程写入完成
|
||||
SYS_LOG_INF("RESEND_CMD_FC_ISP_END1");
|
||||
while(sertrf.mode_status.task_state != DATA_HANDLE_IDLE){
|
||||
os_thread_sleep(10);}
|
||||
// ISP下跳转到应用层运行地址
|
||||
if(cmd_go(&sertrf.stmisp_device, sertrf.fc_address))
|
||||
{
|
||||
printf("stmisp: go ok\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("stmisp: go error\n");
|
||||
}
|
||||
//开启其他线程中相同串口的使用
|
||||
sertrf.stmisp_device.flag = 0;
|
||||
// 将串口切换为无校验模式
|
||||
uart_set_parity_switch(sertrf.device.embedded_device, 0x00);
|
||||
SYS_LOG_INF("RESEND_CMD_FC_ISP_END2");
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Examples_run(void)
|
||||
{
|
||||
//关闭其他线程中相同串口的使用
|
||||
// // //关闭其他线程中相同串口的使用
|
||||
sertrf.stmisp_device.flag = 1;
|
||||
// 重启飞控,并使其进入isp烧录模式
|
||||
// // // 重启飞控,并使其进入isp烧录模式
|
||||
fc_reboot();
|
||||
boot_set(1);
|
||||
os_thread_sleep(1000);
|
||||
boot_set(0);
|
||||
os_thread_sleep(500);
|
||||
uart_set_parity_switch(sertrf.device.embedded_device, 0x02);
|
||||
os_thread_sleep(100);
|
||||
os_thread_sleep(500);
|
||||
// 与isp确定通讯波特率,并建立联系
|
||||
if(!send_sync(&sertrf.stmisp_device, 5))
|
||||
printf("stmisp: sync error\n");
|
||||
@@ -407,6 +623,8 @@ void Examples_run(void)
|
||||
else
|
||||
printf("stmisp: sync ok\n");
|
||||
os_thread_sleep(100);
|
||||
|
||||
boot_set_high_z();
|
||||
// 全盘擦除
|
||||
if(!cmd_extended_erase_mass(&sertrf.stmisp_device))
|
||||
printf("stmisp: erase mass error\n");
|
||||
|
||||
@@ -7,17 +7,37 @@
|
||||
#include "protocol/resend_protl.h"
|
||||
#include "protocol/stmisp.h"
|
||||
#include "ota_u.h"
|
||||
#include "tool.h"
|
||||
#include "ring_buffer.h"
|
||||
#include "../../config/app_config.h"
|
||||
|
||||
#define FC_ADDRESS "0x08000000"
|
||||
#define DATA_HANDLE_BUFFER_SIZE 4096
|
||||
typedef enum
|
||||
{
|
||||
DATA_HANDLE_IDLE = 0,
|
||||
DATA_HANDLE_OTA_DATA,
|
||||
DATA_HANDLE_OTA_DATA_END,
|
||||
DATA_HANDLE_ISP_DATA,
|
||||
DATA_HANDLE_ISP_DATA_END
|
||||
}data_handle_e;
|
||||
typedef struct
|
||||
{
|
||||
data_handle_e task_state;
|
||||
|
||||
}sertrf_mode_status_t;
|
||||
typedef struct
|
||||
{
|
||||
device_t device;
|
||||
os_thread_t embedded_thread;
|
||||
os_thread_t pc_thread;
|
||||
os_thread_t task_thread;
|
||||
os_thread_t app_thread;
|
||||
// 任务
|
||||
os_thread_t embedded_thread; //处理飞控接收
|
||||
os_thread_t pc_thread; //处理app接收
|
||||
os_thread_t task_thread; //处理其他任务
|
||||
os_thread_t app_thread; //处理app任务
|
||||
os_thread_t data_handle_thread; //数据处理任务
|
||||
|
||||
sertrf_mode_status_t mode_status;
|
||||
// ota
|
||||
ota_u_t otau;
|
||||
|
||||
//自定义协议
|
||||
@@ -27,16 +47,24 @@ typedef struct
|
||||
//STMISP协议
|
||||
stmisp_device_t stmisp_device;
|
||||
|
||||
// 环形buff
|
||||
RingBuffer data_handle_buffer;
|
||||
uint32_t fc_address;
|
||||
|
||||
// efuse MAC 地址
|
||||
uint8_t efuse_mac[6];
|
||||
uint8_t efuse_mac_encrypt[6];
|
||||
|
||||
}sertrf_t;
|
||||
|
||||
typedef struct
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char ble_name[32];
|
||||
char wifi_ap_ssid[32];
|
||||
char wifi_ap_password[32];
|
||||
char wifi_sta_ssid[32];
|
||||
char wifi_sta_password[32];
|
||||
uint8_t efuse_mac_encrypt[6];
|
||||
}sertrf_stauct_t;
|
||||
/**
|
||||
* @brief 模块初始化
|
||||
@@ -53,6 +81,11 @@ void sertrf_start(void);
|
||||
*/
|
||||
void sertrf_stop(void);
|
||||
|
||||
/**
|
||||
* @brief 射频模块开关
|
||||
*/
|
||||
void sertrf_rf_switch(uint8_t on);
|
||||
|
||||
/**
|
||||
* @brief 模块数据查看
|
||||
*/
|
||||
@@ -75,18 +108,16 @@ void app_thread(void* arg);
|
||||
* @brief task thread
|
||||
*/
|
||||
void task_thread(void* arg);
|
||||
/**
|
||||
* @brief data handle thread
|
||||
*/
|
||||
void data_handle_thread(void* arg);
|
||||
|
||||
/**
|
||||
* @brief 根据连接状态显示不同的颜色
|
||||
*
|
||||
* @param connect 连接状态
|
||||
*/
|
||||
void pc_link_rgb_color(device_t* device);
|
||||
/**
|
||||
* @brief 打印时间间隔
|
||||
*/
|
||||
void printf_chill_time(uint8_t chill_time, uint16_t type);
|
||||
/**
|
||||
* @brief 字符串转十进制
|
||||
*/
|
||||
uint32_t parse_hex_or_dec(const char *s);
|
||||
|
||||
|
||||
|
||||
140
app/drivers/sertrf/tool.c
Normal file
140
app/drivers/sertrf/tool.c
Normal file
@@ -0,0 +1,140 @@
|
||||
#include "tool.h"
|
||||
|
||||
uint8_t efuse_mac[6];
|
||||
int time_out(uint32_t* time_start, uint32_t timeout_ms)
|
||||
{
|
||||
uint32_t time_new = os_get_sys_time();
|
||||
if(time_new - *time_start > timeout_ms)
|
||||
{
|
||||
*time_start = time_new;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printf_chill_time(uint8_t chill_time, uint16_t type)
|
||||
{
|
||||
static size_t last_time[24] = {0};
|
||||
static uint16_t cnt[24] = {0};
|
||||
static uint32_t type_cnt_time[24] = {0};
|
||||
size_t now_time = os_get_sys_time();
|
||||
cnt[chill_time]++;
|
||||
type_cnt_time[chill_time] += now_time - last_time[chill_time];
|
||||
if(cnt[chill_time] % type == 0 && type_cnt_time[chill_time] / type >= now_time - last_time[chill_time] - 1)
|
||||
{
|
||||
SYS_LOG_INF("TIME_CHILL%d : %d : %d\n",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
|
||||
cnt[chill_time] = 0;
|
||||
type_cnt_time[chill_time] = 0;
|
||||
}
|
||||
else if(cnt[chill_time] % type == 0)
|
||||
{
|
||||
SYS_LOG_WRN("TIME_CHILL%d : %d : %d",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
|
||||
cnt[chill_time] = 0;
|
||||
type_cnt_time[chill_time] = 0;
|
||||
}
|
||||
last_time[chill_time] = now_time;
|
||||
}
|
||||
|
||||
uint32_t parse_hex_or_dec(const char *s) {
|
||||
if (!s) return 0;
|
||||
if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) return (uint32_t)strtoul(s+2, NULL, 16);
|
||||
return (uint32_t)strtoul(s, NULL, 0);
|
||||
}
|
||||
|
||||
void aes_test(void)
|
||||
{
|
||||
// if(!esp_efuse_mac_get_default_id(efuse_mac))
|
||||
// printf("mac: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_mac[0], efuse_mac[1], efuse_mac[2], efuse_mac[3], efuse_mac[4], efuse_mac[5]);
|
||||
|
||||
// const unsigned char key[16] = "1234567890abcdef"; // 128-bit key
|
||||
// unsigned char nonce_counter[16] = {0}; // 初始计数器块 (可用随机数 + 计数)
|
||||
// unsigned char stream_block[16] = {0}; // 内部缓冲
|
||||
// size_t nc_off = 0;
|
||||
|
||||
// const unsigned char input[] = "Hello AES-CTR on ESP32!";
|
||||
// unsigned char output[64] = {0};
|
||||
|
||||
// mbedtls_aes_context aes;
|
||||
// mbedtls_aes_init(&aes);
|
||||
// mbedtls_aes_setkey_enc(&aes, key, 128);
|
||||
|
||||
// // 加密
|
||||
// mbedtls_aes_crypt_ctr(&aes, sizeof(efuse_mac), &nc_off,
|
||||
// nonce_counter, stream_block, efuse_mac, output);
|
||||
|
||||
// printf("Ciphertext (hex): ");
|
||||
// for (int i = 0; i < sizeof(efuse_mac); i++)
|
||||
// printf("%02X", output[i]);
|
||||
// printf("\n");
|
||||
|
||||
// // 解密(同一函数)
|
||||
// unsigned char decrypted[64] = {0};
|
||||
// nc_off = 0;
|
||||
// memset(nonce_counter, 0, 16);
|
||||
// memset(stream_block, 0, 16);
|
||||
|
||||
// mbedtls_aes_crypt_ctr(&aes, sizeof(efuse_mac), &nc_off,
|
||||
// nonce_counter, stream_block, output, decrypted);
|
||||
|
||||
// printf("Decrypted: %s\n", decrypted);
|
||||
|
||||
// mbedtls_aes_free(&aes);
|
||||
|
||||
|
||||
if(!esp_efuse_mac_get_default_id(efuse_mac))
|
||||
printf("mac: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_mac[0], efuse_mac[1], efuse_mac[2], efuse_mac[3], efuse_mac[4], efuse_mac[5]);
|
||||
|
||||
uint8_t efuse_mac_encrypt[6] = {0};
|
||||
sertrf_aes_ctr_encrypt(efuse_mac, 6, efuse_mac_encrypt);
|
||||
printf("mac: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_mac_encrypt[0], efuse_mac_encrypt[1], efuse_mac_encrypt[2], efuse_mac_encrypt[3], efuse_mac_encrypt[4], efuse_mac_encrypt[5]);
|
||||
|
||||
uint8_t efuse_mac_decrypt[6] = {0};
|
||||
sertrf_aes_ctr_decrypt(efuse_mac_encrypt, 6, efuse_mac_decrypt);
|
||||
printf("mac: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_mac_decrypt[0], efuse_mac_decrypt[1], efuse_mac_decrypt[2], efuse_mac_decrypt[3], efuse_mac_decrypt[4], efuse_mac_decrypt[5]);
|
||||
}
|
||||
|
||||
void sertrf_aes_ctr_encrypt(uint8_t *data, uint32_t len,uint8_t* output)
|
||||
{
|
||||
size_t nc_off = 0;
|
||||
unsigned char nonce_counter[16] = {0}; // 初始计数器块 (可用随机数 + 计数)
|
||||
unsigned char stream_block[16] = {0}; // 内部缓冲
|
||||
|
||||
const unsigned char key[16] = PRIVATE_KEY; // 128-bit key
|
||||
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
mbedtls_aes_setkey_enc(&aes, key, 128);
|
||||
|
||||
// 加密
|
||||
mbedtls_aes_crypt_ctr(&aes, len, &nc_off,
|
||||
nonce_counter, stream_block, data, output);
|
||||
mbedtls_aes_free(&aes);
|
||||
}
|
||||
void sertrf_aes_ctr_decrypt(uint8_t *data, uint32_t len,uint8_t* output)
|
||||
{
|
||||
size_t nc_off = 0;
|
||||
unsigned char nonce_counter[16] = {0}; // 初始计数器块 (可用随机数 + 计数)
|
||||
unsigned char stream_block[16] = {0}; // 内部缓冲
|
||||
|
||||
const unsigned char key[16] = PRIVATE_KEY; // 128-bit key
|
||||
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
mbedtls_aes_setkey_enc(&aes, key, 128);
|
||||
|
||||
mbedtls_aes_crypt_ctr(&aes, len, &nc_off,
|
||||
nonce_counter, stream_block, data, output);
|
||||
mbedtls_aes_free(&aes);
|
||||
}
|
||||
esp_err_t esp_efuse_mac_get_default_id(uint8_t *mac)
|
||||
{
|
||||
esp_err_t err = esp_efuse_mac_get_default(mac);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
#if CONFIG_SOC_IEEE802154_SUPPORTED
|
||||
return insert_mac_ext_into_mac(mac);
|
||||
#else
|
||||
return ESP_OK;
|
||||
#endif
|
||||
}
|
||||
55
app/drivers/sertrf/tool.h
Normal file
55
app/drivers/sertrf/tool.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include "os/os.h"
|
||||
#include "sys_log.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include <esp_err.h>
|
||||
#include "esp_system.h"
|
||||
#include "esp_mac.h"
|
||||
#include "../data_port/ble_spp/ble_spp_server.h"
|
||||
|
||||
#define PRIVATE_KEY "1234567890abcdef"
|
||||
|
||||
/**
|
||||
* @brief 超时判断
|
||||
*
|
||||
*/
|
||||
int time_out(uint32_t* time_start, uint32_t timeout_ms);
|
||||
/**
|
||||
* @brief 打印时间间隔
|
||||
*/
|
||||
void printf_chill_time(uint8_t chill_time, uint16_t type);
|
||||
/**
|
||||
* @brief 字符串转十进制
|
||||
*/
|
||||
uint32_t parse_hex_or_dec(const char *s);
|
||||
|
||||
/**
|
||||
* @brief AEStest
|
||||
*/
|
||||
void aes_test(void);
|
||||
|
||||
/**
|
||||
* @brief AES加密
|
||||
*/
|
||||
void sertrf_aes_ctr_encrypt(uint8_t *data, uint32_t len,uint8_t* output);
|
||||
/**
|
||||
* @brief AES解密
|
||||
*/
|
||||
void sertrf_aes_ctr_decrypt(uint8_t *data, uint32_t len,uint8_t* output);
|
||||
|
||||
/**
|
||||
* @breif 获取ESP MAC地址
|
||||
*/
|
||||
esp_err_t esp_efuse_mac_get_default_id(uint8_t *mac);
|
||||
|
||||
/**
|
||||
* @brief 获取BLE MAC地址
|
||||
*/
|
||||
esp_err_t esp_efuse_ble_mac_get_default(uint8_t *mac);
|
||||
@@ -25,3 +25,7 @@ CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=1536
|
||||
CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID=y
|
||||
CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS=y
|
||||
CONFIG_LOG_DEFAULT_LEVEL_NONE=y
|
||||
|
||||
#定义APP配置
|
||||
CONFIG_BOARD_NAME_SERTRF_ESP32C3=y
|
||||
CONFIG_PRODUCT_ID_SERTRF=y
|
||||
|
||||
Reference in New Issue
Block a user