diff --git a/board/TencentOS_Tiny_EVB_STM32WL/BSP/Inc/usart.h b/board/TencentOS_Tiny_EVB_STM32WL/BSP/Inc/usart.h index 8e79aa59..11070341 100644 --- a/board/TencentOS_Tiny_EVB_STM32WL/BSP/Inc/usart.h +++ b/board/TencentOS_Tiny_EVB_STM32WL/BSP/Inc/usart.h @@ -50,9 +50,11 @@ extern "C" { extern UART_HandleTypeDef huart1; extern UART_HandleTypeDef huart2; +extern UART_HandleTypeDef hlpuart1; void MX_USART1_UART_Init(void); void MX_USART2_UART_Init(void); +void MX_LPUART1_UART_Init(void); #ifdef __cplusplus } diff --git a/board/TencentOS_Tiny_EVB_STM32WL/BSP/Src/mcu_init.c b/board/TencentOS_Tiny_EVB_STM32WL/BSP/Src/mcu_init.c index e89c8cfe..efbf4507 100644 --- a/board/TencentOS_Tiny_EVB_STM32WL/BSP/Src/mcu_init.c +++ b/board/TencentOS_Tiny_EVB_STM32WL/BSP/Src/mcu_init.c @@ -30,6 +30,7 @@ void board_init(void) HAL_Init(); SystemClock_Config(); MX_USART1_UART_Init(); + //MX_LPUART1_UART_Init(); } /** diff --git a/board/TencentOS_Tiny_EVB_STM32WL/BSP/Src/usart.c b/board/TencentOS_Tiny_EVB_STM32WL/BSP/Src/usart.c index 3e48acef..dd8303cb 100644 --- a/board/TencentOS_Tiny_EVB_STM32WL/BSP/Src/usart.c +++ b/board/TencentOS_Tiny_EVB_STM32WL/BSP/Src/usart.c @@ -25,8 +25,10 @@ /* External variables --------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/ -uint8_t msg; +uint8_t pm2d5_byte_data; +uint8_t data; +UART_HandleTypeDef hlpuart1; UART_HandleTypeDef huart1; UART_HandleTypeDef huart2; @@ -35,6 +37,27 @@ UART_HandleTypeDef huart2; /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Functions Definition ------------------------------------------------------*/ +void MX_LPUART1_UART_Init(void) +{ + + hlpuart1.Instance = LPUART1; + hlpuart1.Init.BaudRate = 115200; + hlpuart1.Init.WordLength = UART_WORDLENGTH_8B; + hlpuart1.Init.StopBits = UART_STOPBITS_1; + hlpuart1.Init.Parity = UART_PARITY_NONE; + hlpuart1.Init.Mode = UART_MODE_TX_RX; + hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; + hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; + if (HAL_UART_Init(&hlpuart1) != HAL_OK) + { + Error_Handler(); + } + HAL_UART_Receive_IT(&hlpuart1, &data, 1); + /* USER CODE BEGIN LPUART1_Init 2 */ + /* USER CODE END LPUART1_Init 2 */ + +} void MX_USART1_UART_Init(void) { @@ -71,15 +94,36 @@ void MX_USART2_UART_Init(void) Error_Handler(); } - HAL_UART_Receive_IT(&huart2, &msg, 1); + HAL_UART_Receive_IT(&huart2, &pm2d5_byte_data, 1); } void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; - - if(uartHandle->Instance==USART2) + if(uartHandle->Instance==LPUART1) + { + __HAL_RCC_LPUART1_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**LPUART1 GPIO Configuration + PC0 ------> LPUART1_RX + PC1 ------> LPUART1_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF8_LPUART1; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /* LPUART1 interrupt Init */ + HAL_NVIC_SetPriority(LPUART1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(LPUART1_IRQn); + /* USER CODE BEGIN LPUART1_MspInit 1 */ + + /* USER CODE END LPUART1_MspInit 1 */ + } + else if(uartHandle->Instance==USART2) { /* USER CODE BEGIN USART2_MspInit 0 */ diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan/TencentOS_Tiny.uvprojx b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan/TencentOS_Tiny.uvprojx index c2bf2dec..47efa927 100644 --- a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan/TencentOS_Tiny.uvprojx +++ b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan/TencentOS_Tiny.uvprojx @@ -16,7 +16,7 @@ STM32WLE5JCIx STMicroelectronics - Keil.STM32WLxx_DFP.1.0.2.OEM + Keil.STM32WLxx_DFP.1.0.7 http://www.keil.com/pack IRAM(0x20000000,0x10000) IROM(0x08000000,0x40000) CPUTYPE("Cortex-M4") CLOCK(12000000) ELITTLE diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/app_lorawan.c b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/app_lorawan.c index 8bc8909d..9fc2971c 100644 --- a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/app_lorawan.c +++ b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/app_lorawan.c @@ -1,30 +1,194 @@ - /* Includes ------------------------------------------------------------------*/ #include "timer.h" #include "app_system.h" #include "app_lorawan.h" #include "stm32_lpm.h" - #include "adc_if.h" #include "lorawan_version.h" -#include -#include "sensor_parser.h" +#include "pm2d5_parser.h" +#include "tos_k.h" +#include "esp8266_tencent_firmware.h" +#include "tencent_firmware_module_wrapper.h" +#include "math.h" + +//#define USE_WIFI #define APP_TX_DUTYCYCLE 30000 //ms #define LORAWAN_DOWNLINK_PORT 2 #define LORAWAN_UPLINK_PORT 10 +#define WIFI_SSID "TencentOS" +#define WIFI_PASSWD "tencentostiny" +#define PRODUCT_ID "ZLC54JVTAH" +#define DEVICE_NAME "node_20DEBC0A0000" +#define DEVICE_KEY "QXiswx4I1T+bZyHOrHvaqg==" + + +#define REPORT_DATA_TEMPLATE "{\"method\":\"report\",\"clientToken\":\"%s\"," \ + "\"params\":{\"a\":%d," \ + "\"b\":%d," \ + "\"c\":%d," \ + "\"d\":%d," \ + "\"e\":%d," \ + "\"f\":%d," \ + "\"g\":%d," \ + "\"h\":%d," \ + "\"i\":%d," \ + "\"j\":%d," \ + "\"k\":%d," \ + "\"l\":%d," \ + "\"v\":%d," \ + "\"ec\":%d" \ + "}}" + +void default_message_handler(mqtt_message_t* msg) +{ + printf("callback:\r\n"); + printf("---------------------------------------------------------\r\n"); + printf("\ttopic:%s\r\n", msg->topic); + printf("\tpayload:%s\r\n", msg->payload); + printf("---------------------------------------------------------\r\n"); +} + +char payload[1024] = {0}; +static char report_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; +static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; k_mail_q_t mail_q; -#define DATA_CNT 26 -uint8_t mail_buf[DATA_CNT]; +pm2d5_data_u pm2d5_value; +uint8_t pm2d5_value_pool[3 * sizeof(pm2d5_data_u)]; +void generate_client_token(char* buffer, size_t size) +{ + long client_token_value; + + memset(buffer, 0, size); + client_token_value = ((long)tos_systick_get()) % (long)(pow(10, size)); + snprintf(buffer, size, "%ld", client_token_value); +} -typedef struct device_data_st { - uint8_t data[DATA_CNT]; -} __PACKED__ dev_data_t; +void mqtt_demo_task(void) +{ + int ret = 0; + int size = 0; + int i = 0; + char *product_id = PRODUCT_ID; + char *device_name = DEVICE_NAME; + char *key = DEVICE_KEY; + + device_info_t dev_info; + memset(&dev_info, 0, sizeof(device_info_t)); + char str[16]; + size_t mail_size; + uint8_t report_error_count = 0; + char client_token[10]; + /** + * Please Choose your AT Port first, default is HAL_UART_2(USART2) + */ + ret = esp8266_tencent_firmware_sal_init(HAL_UART_PORT_0); + + if (ret < 0) { + printf("esp8266 tencent firmware sal init fail, ret is %d\r\n", ret); + return; + } + + while (1) { + if ( esp8266_tencent_firmware_join_ap(WIFI_SSID, WIFI_PASSWD) == 0) { + printf("module WIFI connect success\n"); + break; + } + printf("module WIFI connect fail\n"); + tos_sleep_ms(2000); + } + + strncpy(dev_info.product_id, product_id, PRODUCT_ID_MAX_SIZE); + strncpy(dev_info.device_name, device_name, DEVICE_NAME_MAX_SIZE); + strncpy(dev_info.device_serc, key, DEVICE_SERC_MAX_SIZE); + tos_tf_module_info_set(&dev_info, TLS_MODE_PSK); + mqtt_param_t init_params = DEFAULT_MQTT_PARAMS; + while (1) { + if (tos_tf_module_mqtt_conn(init_params) == 0) { + printf("module mqtt connect success\n"); + break; + } + printf("module mqtt connect fail\n"); + tos_sleep_ms(5000); + } + + /* 开始订阅topic */ + size = snprintf(report_reply_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/down/property/%s/%s", product_id, device_name); + + if (size < 0 || size > sizeof(report_reply_topic_name) - 1) { + printf("sub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_reply_topic_name)); + } + if (tos_tf_module_mqtt_sub(report_reply_topic_name, QOS0, default_message_handler) != 0) { + printf("module mqtt sub fail\n"); + } else { + printf("module mqtt sub success\n"); + } + + memset(report_topic_name, 0, sizeof(report_topic_name)); + size = snprintf(report_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/up/property/%s/%s", product_id, device_name); + + if (size < 0 || size > sizeof(report_topic_name) - 1) { + printf("pub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_topic_name)); + } + + /* 创建邮箱 */ + tos_mail_q_create(&mail_q, pm2d5_value_pool, 3, sizeof(pm2d5_data_u)); + + HAL_NVIC_DisableIRQ(USART2_IRQn); + + if (pm2d5_parser_init() == -1) { + printf("pm2d5 parser init fail\r\n"); + return; + } + + while (1) { + /* 通过接收邮件来读取数据 */ + HAL_NVIC_EnableIRQ(USART2_IRQn); + tos_mail_q_pend(&mail_q, (uint8_t*)&pm2d5_value, &mail_size, TOS_TIME_FOREVER); + HAL_NVIC_DisableIRQ(USART2_IRQn); + + //收到之后打印信息 + printf("\r\n\r\n\r\n"); + for (i = 0; i < 13; i++) { + printf("data[%d]:%d ug/m3\r\n", i+1, pm2d5_value.data[i]); + } + + + /* 上报值 */ + generate_client_token(client_token, sizeof(client_token)); + memset(payload, 0, 1024); + snprintf(payload, 1024, REPORT_DATA_TEMPLATE, client_token, + pm2d5_value.pm2d5_data.data1, pm2d5_value.pm2d5_data.data2, + pm2d5_value.pm2d5_data.data3, pm2d5_value.pm2d5_data.data4, + pm2d5_value.pm2d5_data.data5, pm2d5_value.pm2d5_data.data6, + pm2d5_value.pm2d5_data.data7, pm2d5_value.pm2d5_data.data8, + pm2d5_value.pm2d5_data.data9, pm2d5_value.pm2d5_data.data10, + pm2d5_value.pm2d5_data.data11, pm2d5_value.pm2d5_data.data12, + 2, pm2d5_value.pm2d5_data.err_code); + + if (tos_tf_module_mqtt_publ(report_topic_name, QOS0, payload) != 0) { + report_error_count++; + printf("module mqtt publ fail, count: %d\n", report_error_count); + sprintf(str, "# report fail"); + } else { + report_error_count = 0; + printf("module mqtt publ success\n"); + sprintf(str, "# report ok"); + } + + if (report_error_count >= 6) { + HAL_NVIC_SystemReset(); + } + + tos_sleep_ms(5000); + + } +} /*! * user main_callback function declare , you need to implement them @@ -110,10 +274,6 @@ void MX_LoRaWAN_Init(void) printf("lorawan init ok.\r\n"); } -uint8_t pool[DATA_CNT]; -#define CMD_LEN_MAX 50 -char cmd_buf[CMD_LEN_MAX]; -dev_data_t dev_data; uint16_t report_period = 10; @@ -143,13 +303,25 @@ void recv_callback(uint8_t *data, uint8_t len) void application_entry(void *arg) { - +#ifdef USE_WIFI + mqtt_demo_task(); + while (1) { + printf("This is a mqtt demo!\r\n"); + tos_task_delay(1000); + } +#else int i = 0; int ret = 0; int send_failed_count = 0; + size_t mail_size; + tos_mail_q_create(&mail_q, pm2d5_value_pool, 3, sizeof(pm2d5_data_u)); - tos_mail_q_create(&mail_q, pool, DATA_CNT, sizeof(uint8_t)); - tos_shell_init(cmd_buf, sizeof(cmd_buf), uart_output); + HAL_NVIC_DisableIRQ(USART2_IRQn); + + if (pm2d5_parser_init() == -1) { + printf("pm2d5 parser init fail\r\n"); + return; + } //create task to process loramac tos_sem_create_max(&lora_mac_process_sem, 0, 1); @@ -164,17 +336,21 @@ void application_entry(void *arg) //report pm2.5 data while (1) { - size_t mail_size; - - tos_mail_q_pend(&mail_q, &dev_data.data, &mail_size, TOS_TIME_FOREVER); + HAL_NVIC_EnableIRQ(USART2_IRQn); + tos_mail_q_pend(&mail_q, (uint8_t*)&pm2d5_value, &mail_size, TOS_TIME_FOREVER); + HAL_NVIC_DisableIRQ(USART2_IRQn); + //收到之后打印信息 + printf("\r\n\r\n\r\n"); + for (i = 0; i < 13; i++) { + printf("data[%d]:%d ug/m3\r\n", i+1, pm2d5_value.data[i]); + } /*fill the data*/ i = 0; app_data.port = LORAWAN_UPLINK_PORT; for (i = 0; i < mail_size; i++) { - app_data.buff[i] = dev_data.data[i]; - printf("[%d] %x\n", i, dev_data.data[i]); + app_data.buff[i] = pm2d5_value.data[i]; } printf("\n\n"); app_data.size = i; @@ -209,6 +385,7 @@ void application_entry(void *arg) } //Lora_start_send(); +#endif } void Lora_start_send(void) diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/mqtt_iot_explorer_esp8266.c b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/mqtt_iot_explorer_esp8266.c new file mode 100644 index 00000000..dcdec3da --- /dev/null +++ b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/mqtt_iot_explorer_esp8266.c @@ -0,0 +1,193 @@ +#include "tos_k.h" +#include "esp8266_tencent_firmware.h" +#include "tencent_firmware_module_wrapper.h" +#include "pm2d5_parser.h" +#include "math.h" + +#define WIFI_SSID "Tencent-GuestWiFi" +#define WIFI_PASSWD "" +#define PRODUCT_ID "ZLC54JVTAH" +#define DEVICE_NAME "node_20DEBC0A0000" +#define DEVICE_KEY "QXiswx4I1T+bZyHOrHvaqg==" + + +#define REPORT_DATA_TEMPLATE "{\"method\":\"report\",\"clientToken\":\"%s\"," \ + "\"params\":{\"a\":%d," \ + "\"b\":%d," \ + "\"c\":%d," \ + "\"d\":%d," \ + "\"e\":%d," \ + "\"f\":%d," \ + "\"g\":%d," \ + "\"h\":%d," \ + "\"i\":%d," \ + "\"j\":%d," \ + "\"k\":%d," \ + "\"l\":%d," \ + "\"v\":%d," \ + "\"ec\":%d" \ + "}}" + +void default_message_handler(mqtt_message_t* msg) +{ + printf("callback:\r\n"); + printf("---------------------------------------------------------\r\n"); + printf("\ttopic:%s\r\n", msg->topic); + printf("\tpayload:%s\r\n", msg->payload); + printf("---------------------------------------------------------\r\n"); +} + +char payload[1024] = {0}; +static char report_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; +static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0}; + +k_mail_q_t mail_q; +pm2d5_data_u pm2d5_value; +uint8_t pm2d5_value_pool[3 * sizeof(pm2d5_data_u)]; + +void generate_client_token(char* buffer, size_t size) +{ + long client_token_value; + + memset(buffer, 0, size); + client_token_value = ((long)tos_systick_get()) % (long)(pow(10, size)); + snprintf(buffer, size, "%ld", client_token_value); +} + +void mqtt_demo_task(void) +{ + int ret = 0; + int size = 0; + int i = 0; + char *product_id = PRODUCT_ID; + char *device_name = DEVICE_NAME; + char *key = DEVICE_KEY; + + device_info_t dev_info; + memset(&dev_info, 0, sizeof(device_info_t)); + char str[16]; + size_t mail_size; + uint8_t report_error_count = 0; + char client_token[10]; + + /** + * Please Choose your AT Port first, default is HAL_UART_2(USART2) + */ + ret = esp8266_tencent_firmware_sal_init(HAL_UART_PORT_2); + + if (ret < 0) { + printf("esp8266 tencent firmware sal init fail, ret is %d\r\n", ret); + return; + } + + while (1) { + if ( esp8266_tencent_firmware_join_ap(WIFI_SSID, WIFI_PASSWD) == 0) { + printf("module WIFI connect success\n"); + break; + } + printf("module WIFI connect fail\n"); + tos_sleep_ms(2000); + } + + strncpy(dev_info.product_id, product_id, PRODUCT_ID_MAX_SIZE); + strncpy(dev_info.device_name, device_name, DEVICE_NAME_MAX_SIZE); + strncpy(dev_info.device_serc, key, DEVICE_SERC_MAX_SIZE); + tos_tf_module_info_set(&dev_info, TLS_MODE_PSK); + mqtt_param_t init_params = DEFAULT_MQTT_PARAMS; + while (1) { + if (tos_tf_module_mqtt_conn(init_params) == 0) { + printf("module mqtt connect success\n"); + break; + } + printf("module mqtt connect fail\n"); + tos_sleep_ms(5000); + } + + /* 开始订阅topic */ + size = snprintf(report_reply_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/down/property/%s/%s", product_id, device_name); + + if (size < 0 || size > sizeof(report_reply_topic_name) - 1) { + printf("sub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_reply_topic_name)); + } + if (tos_tf_module_mqtt_sub(report_reply_topic_name, QOS0, default_message_handler) != 0) { + printf("module mqtt sub fail\n"); + } else { + printf("module mqtt sub success\n"); + } + + memset(report_topic_name, 0, sizeof(report_topic_name)); + size = snprintf(report_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/up/property/%s/%s", product_id, device_name); + + if (size < 0 || size > sizeof(report_topic_name) - 1) { + printf("pub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_topic_name)); + } + + /* 创建邮箱 */ + tos_mail_q_create(&mail_q, pm2d5_value_pool, 3, sizeof(pm2d5_data_u)); + + HAL_NVIC_DisableIRQ(USART2_IRQn); + + if (pm2d5_parser_init() == -1) { + printf("pm2d5 parser init fail\r\n"); + return; + } + + while (1) { + /* 通过接收邮件来读取数据 */ + HAL_NVIC_EnableIRQ(USART2_IRQn); + tos_mail_q_pend(&mail_q, (uint8_t*)&pm2d5_value, &mail_size, TOS_TIME_FOREVER); + HAL_NVIC_DisableIRQ(USART2_IRQn); + + //收到之后打印信息 + printf("\r\n\r\n\r\n"); + for (i = 0; i < 13; i++) { + printf("data[%d]:%d ug/m3\r\n", i+1, pm2d5_value.data[i]); + } + + /* 显示PM2.5的值 */ + sprintf(str, "PM2.5:%4d ug/m3", pm2d5_value.pm2d5_data.data2); + OLED_ShowString(0,0,(uint8_t*)str,16); + + /* 上报值 */ + generate_client_token(client_token, sizeof(client_token)); + memset(payload, 0, 1024); + snprintf(payload, 1024, REPORT_DATA_TEMPLATE, client_token, + pm2d5_value.pm2d5_data.data1, pm2d5_value.pm2d5_data.data2, + pm2d5_value.pm2d5_data.data3, pm2d5_value.pm2d5_data.data4, + pm2d5_value.pm2d5_data.data5, pm2d5_value.pm2d5_data.data6, + pm2d5_value.pm2d5_data.data7, pm2d5_value.pm2d5_data.data8, + pm2d5_value.pm2d5_data.data9, pm2d5_value.pm2d5_data.data10, + pm2d5_value.pm2d5_data.data11, pm2d5_value.pm2d5_data.data12, + 2, pm2d5_value.pm2d5_data.err_code); + + if (tos_tf_module_mqtt_publ(report_topic_name, QOS0, payload) != 0) { + report_error_count++; + printf("module mqtt publ fail, count: %d\n", report_error_count); + sprintf(str, "# report fail"); + } else { + report_error_count = 0; + printf("module mqtt publ success\n"); + sprintf(str, "# report ok"); + } + + if (report_error_count >= 6) { + HAL_NVIC_SystemReset(); + } + + tos_sleep_ms(5000); + + } +} + +void application_entry(void *arg) +{ + char *str = "TencentOS-Tiny"; + + /* 初始化OLED */ + + mqtt_demo_task(); + while (1) { + printf("This is a mqtt demo!\r\n"); + tos_task_delay(1000); + } +} diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/pm2d5_parser.c b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/pm2d5_parser.c new file mode 100644 index 00000000..9d85678b --- /dev/null +++ b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/pm2d5_parser.c @@ -0,0 +1,231 @@ +/*---------------------------------------------------------------------------- + * Tencent is pleased to support the open source community by making TencentOS + * available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * If you have downloaded a copy of the TencentOS binary from Tencent, please + * note that the TencentOS binary is licensed under the BSD 3-Clause License. + * + * If you have downloaded a copy of the TencentOS source code from Tencent, + * please note that TencentOS source code is licensed under the BSD 3-Clause + * License, except for the third-party components listed below which are + * subject to different license terms. Your integration of TencentOS into your + * own projects may require compliance with the BSD 3-Clause License, as well + * as the other licenses applicable to the third-party components included + * within TencentOS. + *---------------------------------------------------------------------------*/ +#include "pm2d5_parser.h" + +static pm2d5_parser_ctrl_t pm2d5_parser_ctrl; + +static k_stack_t pm2d5_parser_task_stack[PM2D5_PARSER_TASK_STACK_SIZE]; + +static uint8_t pm2d5_parser_buffer[PM2D5_PARSER_BUFFER_SIZE]; + +/** + * @brief PM2D5传感器原始数据 + * @note 传感器每次上报32字节,头部0x42和0x4d固定,用于解析包头,帧长度用于判断是否是正常数据包,所以只存储后面的28字节 + */ +typedef struct pm2d5_raw_data_st { + + uint8_t data1_h; + uint8_t data1_l; + + uint8_t data2_h; + uint8_t data2_l; + + uint8_t data3_h; + uint8_t data3_l; + + uint8_t data4_h; + uint8_t data4_l; + + uint8_t data5_h; + uint8_t data5_l; + + uint8_t data6_h; + uint8_t data6_l; + + uint8_t data7_h; + uint8_t data7_l; + + uint8_t data8_h; + uint8_t data8_l; + + uint8_t data9_h; + uint8_t data9_l; + + uint8_t data10_h; + uint8_t data10_l; + + uint8_t data11_h; + uint8_t data11_l; + + uint8_t data12_h; + uint8_t data12_l; + + uint8_t data13_h; //版本号 + uint8_t data13_l; //错误代码 + + uint8_t chk_sum_h; //和校验高8位 + uint8_t chk_sum_l; //和校验低8位 +} pm2d5_raw_data_t; + +typedef union pm2d5_raw_data_un { + uint8_t data[30]; + pm2d5_raw_data_t pm2d5_raw_data; +}pm2d5_raw_data_u; + +/** + * @brief 向PM2D5解析器中送入一个字节数据 + * @param data 送入的数据 + * @retval none + * @note 需要用户在串口中断函数中手动调用 +*/ +void pm2d5_parser_input_byte(uint8_t data) +{ + if (tos_chr_fifo_push(&pm2d5_parser_ctrl.parser_rx_fifo, data) == K_ERR_NONE) { + /* 送入数据成功,释放信号量,计数 */ + tos_sem_post(&pm2d5_parser_ctrl.parser_rx_sem); + } +} + +/** + * @brief PM2D5解析器从chr fifo中取出一个字节数据 + * @param none + * @retval 正常返回读取数据,错误返回-1 +*/ +static int pm2d5_parser_getchar(void) +{ + uint8_t chr; + k_err_t err; + + /* 永久等待信号量,信号量为空表示chr fifo中无数据 */ + if (tos_sem_pend(&pm2d5_parser_ctrl.parser_rx_sem, TOS_TIME_FOREVER) != K_ERR_NONE) { + return -1; + } + + /* 从chr fifo中取出数据 */ + err = tos_chr_fifo_pop(&pm2d5_parser_ctrl.parser_rx_fifo, &chr); + + return err == K_ERR_NONE ? chr : -1; +} + +/** + * @brief PM2D5读取传感器原始数据并解析 + * @param void + * @retval 解析成功返回0,解析失败返回-1 +*/ +static int pm2d5_parser_read_raw_data(pm2d5_raw_data_u *pm2d5_raw_data, pm2d5_data_u *pm2d5_data) +{ + int i; + uint8_t len_h,len_l; + uint16_t len; + uint16_t check_sum; + uint16_t check_sum_cal = 0x42 + 0x4d; + + /* 读取并计算帧长度 */ + len_h = pm2d5_parser_getchar(); + len_l = pm2d5_parser_getchar(); + len = (len_h << 8) | len_l; + + if ( len != 0x001C) { + //非传感器值数据,清空缓存 + for (i = 0; i < len; i++) { + pm2d5_parser_getchar(); + } + return -1; + } + + /* 读取传感器原始数据 */ + for (i = 0; i < len; i++) { + pm2d5_raw_data->data[i] = pm2d5_parser_getchar(); + } + + /* 和校验 */ + //通过数据计算和校验 + check_sum_cal = check_sum_cal + len_h + len_l; + for (i = 0; i < len -2; i++) { + check_sum_cal += pm2d5_raw_data->data[i]; + } + //协议中给出的和校验值 + check_sum = (pm2d5_raw_data->pm2d5_raw_data.chk_sum_h << 8) + pm2d5_raw_data->pm2d5_raw_data.chk_sum_l; + if (check_sum_cal != check_sum) { + return -1; + } + + /* 存储传感器值 */ + for (i = 0; i < sizeof(pm2d5_data_t); i++) { + pm2d5_data->data[i] = (pm2d5_raw_data->data[i*2] << 8) | pm2d5_raw_data->data[i*2 + 1]; + } + + return 0; +} + +extern k_mail_q_t mail_q; +pm2d5_raw_data_u pm2d5_raw_data; +pm2d5_data_u pm2d5_data; + +/** + * @brief PM2D5解析器任务 +*/ +static void pm2d5_parser_task_entry(void *arg) +{ + int chr, last_chr = 0; + + while (1) { + + chr = pm2d5_parser_getchar(); + if (chr < 0) { + printf("parser task get char fail!\r\n"); + continue; + } + + if (chr == 0x4d && last_chr == 0x42) { + /* 解析到包头 */ + if (0 == pm2d5_parser_read_raw_data(&pm2d5_raw_data, &pm2d5_data)) { + /* 正常解析之后通过邮箱发送 */ + tos_mail_q_post(&mail_q, &pm2d5_data, sizeof(pm2d5_data_t)); + } + } + + last_chr = chr; + } +} + +/** + * @brief 初始化PM2D5解析器 + * @param none + * @retval 全部创建成功返回0,任何一个创建失败则返回-1 +*/ +int pm2d5_parser_init(void) +{ + k_err_t ret; + + memset((pm2d5_parser_ctrl_t*)&pm2d5_parser_ctrl, 0, sizeof(pm2d5_parser_ctrl)); + + /* 创建 chr fifo */ + ret = tos_chr_fifo_create(&pm2d5_parser_ctrl.parser_rx_fifo, pm2d5_parser_buffer, sizeof(pm2d5_parser_buffer)); + if (ret != K_ERR_NONE) { + printf("pm2d5 parser chr fifo create fail, ret = %d\r\n", ret); + return -1; + } + + /* 创建信号量 */ + ret = tos_sem_create(&pm2d5_parser_ctrl.parser_rx_sem, 0); + if (ret != K_ERR_NONE) { + printf("pm2d5 parser_rx_sem create fail, ret = %d\r\n", ret); + return -1; + } + + /* 创建线程 */ + ret = tos_task_create(&pm2d5_parser_ctrl.parser_task, "pm2d5_parser_task", + pm2d5_parser_task_entry, NULL, PM2D5_PARSER_TASK_PRIO, + pm2d5_parser_task_stack,PM2D5_PARSER_TASK_STACK_SIZE,0); + if (ret != K_ERR_NONE) { + printf("pm2d5 parser task create fail, ret = %d\r\n", ret); + return -1; + } + + return 0; +} diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/pm2d5_parser.h b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/pm2d5_parser.h new file mode 100644 index 00000000..991644d9 --- /dev/null +++ b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/pm2d5_parser.h @@ -0,0 +1,67 @@ +/*---------------------------------------------------------------------------- + * Tencent is pleased to support the open source community by making TencentOS + * available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * If you have downloaded a copy of the TencentOS binary from Tencent, please + * note that the TencentOS binary is licensed under the BSD 3-Clause License. + * + * If you have downloaded a copy of the TencentOS source code from Tencent, + * please note that TencentOS source code is licensed under the BSD 3-Clause + * License, except for the third-party components listed below which are + * subject to different license terms. Your integration of TencentOS into your + * own projects may require compliance with the BSD 3-Clause License, as well + * as the other licenses applicable to the third-party components included + * within TencentOS. + *---------------------------------------------------------------------------*/ +#ifndef _PM2D5_PARSER_H_ +#define _PM2D5_PARSER_H_ + +#include "tos_k.h" + +/* pm2d5 parser config */ +#define PM2D5_PARSER_TASK_STACK_SIZE 512 +#define PM2D5_PARSER_TASK_PRIO 5 +#define PM2D5_PARSER_BUFFER_SIZE 64 + +/* PM2.5 鏁版嵁瑙f瀽鍣ㄦ帶鍒跺潡 */ +typedef struct pm2d5_parser_control_st { + k_task_t parser_task; //瑙f瀽鍣ㄤ换鍔℃帶鍒跺潡 + + k_sem_t parser_rx_sem; //琛ㄧず瑙f瀽鍣ㄤ粠涓插彛鎺ユ敹鍒版暟鎹 + k_chr_fifo_t parser_rx_fifo; //瀛樻斁瑙f瀽鍣ㄦ帴鏀跺埌鐨勬暟鎹 +} pm2d5_parser_ctrl_t; + +/** + * @brief 瑙f瀽鍑虹殑PM2D5鏁版嵁鍊 + * @note 鍙互浣滀负閭欢鍙戦佺粰鍏朵粬浠诲姟杩涜杩涗竴姝ュ鐞 + */ +typedef struct pm2d5_data_st { + uint16_t data1; + uint16_t data2; + uint16_t data3; + uint16_t data4; + uint16_t data5; + uint16_t data6; + uint16_t data7; + uint16_t data8; + uint16_t data9; + uint16_t data10; + uint16_t data11; + uint16_t data12; + uint8_t version; + uint8_t err_code; +}pm2d5_data_t; + +typedef union pm2d5_data_un { + uint16_t data[13]; + pm2d5_data_t pm2d5_data; +} pm2d5_data_u; + +void pm2d5_parser_input_byte(uint8_t byte); + +int pm2d5_send_read_cmd(void); + +int pm2d5_parser_init(void); + +#endif /* _PM2D5_PARSER_H_ */ diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/sensor_parser.c b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/sensor_parser.c deleted file mode 100644 index 675b690c..00000000 --- a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/sensor_parser.c +++ /dev/null @@ -1,337 +0,0 @@ -/*---------------------------------------------------------------------------- - * Tencent is pleased to support the open source community by making TencentOS - * available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * If you have downloaded a copy of the TencentOS binary from Tencent, please - * note that the TencentOS binary is licensed under the BSD 3-Clause License. - * - * If you have downloaded a copy of the TencentOS source code from Tencent, - * please note that TencentOS source code is licensed under the BSD 3-Clause - * License, except for the third-party components listed below which are - * subject to different license terms. Your integration of TencentOS into your - * own projects may require compliance with the BSD 3-Clause License, as well - * as the other licenses applicable to the third-party components included - * within TencentOS. - *---------------------------------------------------------------------------*/ - -#include "sensor_parser.h" - -__STATIC__ shell_ctl_t shell_ctl; - -__STATIC__ k_stack_t shell_parser_task_stack[SHELL_PARSER_TASK_STACK_SIZE]; - -__STATIC__ int shell_getchar(void) -{ - uint8_t chr; - k_err_t err; - - if (tos_sem_pend(&SHELL_CTL->shell_rx_sem, TOS_TIME_FOREVER) != K_ERR_NONE) { - return -1; - } - - err = tos_chr_fifo_pop(&SHELL_CTL->shell_rx_fifo, &chr); - - return err == K_ERR_NONE ? chr : -1; -} - -typedef struct sensor_frame_st { - uint16_t len; - uint16_t data1; - uint16_t data2; - uint16_t data3; - uint16_t data4; - uint16_t data5; - uint16_t data6; - uint16_t data7; - uint16_t data8; - uint16_t data9; - uint16_t data10; - uint16_t data11; - uint16_t data12; - uint16_t data13; - uint16_t chk_sum; - - uint8_t len_h; - uint8_t len_l; - - uint8_t data1_h; - uint8_t data1_l; - - uint8_t data2_h; - uint8_t data2_l; - - uint8_t data3_h; - uint8_t data3_l; - - uint8_t data4_h; - uint8_t data4_l; - - uint8_t data5_h; - uint8_t data5_l; - - uint8_t data6_h; - uint8_t data6_l; - - uint8_t data7_h; - uint8_t data7_l; - - uint8_t data8_h; - uint8_t data8_l; - - uint8_t data9_h; - uint8_t data9_l; - - uint8_t data10_h; - uint8_t data10_l; - - uint8_t data11_h; - uint8_t data11_l; - - uint8_t data12_h; - uint8_t data12_l; - - uint8_t data13_h; - uint8_t data13_l; - - uint8_t chk_sum_h; - uint8_t chk_sum_l; -} __PACKED__ sensor_frame_t; - -extern k_mail_q_t mail_q; - -k_tick_t last_report = 0; - -int read_frame(sensor_frame_t *frame) -{ - frame->len_h = shell_getchar(); - frame->len_l = shell_getchar(); - - frame->data1_h = shell_getchar(); - frame->data1_l = shell_getchar(); - - frame->data2_h = shell_getchar(); - frame->data2_l = shell_getchar(); - - frame->data3_h = shell_getchar(); - frame->data3_l = shell_getchar(); - - frame->data4_h = shell_getchar(); - frame->data4_l = shell_getchar(); - - frame->data5_h = shell_getchar(); - frame->data5_l = shell_getchar(); - - frame->data6_h = shell_getchar(); - frame->data6_l = shell_getchar(); - - frame->data7_h = shell_getchar(); - frame->data7_l = shell_getchar(); - - frame->data8_h = shell_getchar(); - frame->data8_l = shell_getchar(); - - frame->data9_h = shell_getchar(); - frame->data9_l = shell_getchar(); - - frame->data10_h = shell_getchar(); - frame->data10_l = shell_getchar(); - - frame->data11_h = shell_getchar(); - frame->data11_l = shell_getchar(); - - frame->data12_h = shell_getchar(); - frame->data12_l = shell_getchar(); - - frame->data13_h = shell_getchar(); - frame->data13_l = shell_getchar(); - - frame->chk_sum_h = shell_getchar(); - frame->chk_sum_l = shell_getchar(); - - - frame->len = (frame->len_h << 8) | frame->len_l; - frame->data1 = (frame->data1_h << 8) | frame->data1_l; - frame->data2 = (frame->data2_h << 8) | frame->data2_l; - frame->data3 = (frame->data3_h << 8) | frame->data3_l; - frame->data4 = (frame->data4_h << 8) | frame->data4_l; - frame->data5 = (frame->data5_h << 8) | frame->data5_l; - frame->data6 = (frame->data6_h << 8) | frame->data6_l; - frame->data7 = (frame->data7_h << 8) | frame->data7_l; - frame->data8 = (frame->data8_h << 8) | frame->data8_l; - frame->data9 = (frame->data9_h << 8) | frame->data9_l; - frame->data10 = (frame->data10_h << 8) | frame->data10_l; - frame->data11 = (frame->data11_h << 8) | frame->data11_l; - frame->data12 = (frame->data12_h << 8) | frame->data12_l; - frame->data13 = (frame->data13_h << 8) | frame->data13_l; - frame->chk_sum = (frame->chk_sum_h << 8) | frame->chk_sum_l; - - - uint16_t chsum; - chsum = frame->len_h + - frame->len_l + - frame->data1_h + - frame->data1_l + - frame->data2_h + - frame->data2_l + - frame->data3_h + - frame->data3_l + - frame->data4_h + - frame->data4_l + - frame->data5_h + - frame->data5_l + - frame->data6_h + - frame->data6_l + - frame->data7_h + - frame->data7_l + - frame->data8_h + - frame->data8_l + - frame->data9_h + - frame->data9_l + - frame->data10_h + - frame->data10_l + - frame->data11_h + - frame->data11_l + - frame->data12_h + - frame->data12_l + - frame->data13_h + - frame->data13_l; - - if (chsum + 0x42 + 0x4d != frame->chk_sum) { - return -1; - } - - k_tick_t now = tos_systick_get(); - if (last_report == 0 || now - last_report >= 15000) { - last_report = now; - - printf("data1: %d\n", frame->data1); - printf("data2: %d\n", frame->data2); - printf("data3: %d\n", frame->data3); - printf("data4: %d\n", frame->data4); - printf("data5: %d\n", frame->data5); - printf("data6: %d\n", frame->data6); - printf("data7: %d\n", frame->data7); - printf("data8: %d\n", frame->data8); - printf("data9: %d\n", frame->data9); - printf("data10: %d\n", frame->data10); - printf("data11: %d\n", frame->data11); - printf("data12: %d\n", frame->data12); - printf("data13: %d\n", frame->data13); - printf("\n\n"); - - return 0; - } - - return -1; -} - -sensor_frame_t frame; - -__STATIC__ int shell_readline(void) -{ - int chr, last_chr = 0; - char *buf = SHELL_CTL->cmd_buffer; - - while (K_TRUE) { - if (buf - SHELL_CTL->cmd_buffer >= (SHELL_CTL->cmd_buffer_size - 1)) { - return -1; - } - - chr = shell_getchar(); - if (chr < 0) { - return -1; - } - - if (chr == 0x4d && last_chr == 0x42) { - if (read_frame(&frame) == 0) { - tos_mail_q_post(&mail_q, &frame.data1_h, 13 * 2); - } - } - - *buf++ = chr; - last_chr = chr; - } -} - - -__STATIC__ void shell_parser(void *arg) -{ - while (K_TRUE) { - shell_readline(); - } -} - -__API__ int tos_shell_init(char *cmd_buf, int cmd_buf_size, shell_output_t output) -{ - void *buffer = K_NULL; - - TOS_PTR_SANITY_CHECK_RC(cmd_buf, -1); - TOS_PTR_SANITY_CHECK_RC(output, -1); - - memset(SHELL_CTL, 0, sizeof(shell_ctl_t)); - - SHELL_CTL->cmd_buffer = cmd_buf; - SHELL_CTL->cmd_buffer_size = cmd_buf_size; - SHELL_CTL->output = output; - - buffer = tos_mmheap_alloc(cmd_buf_size * 2); - if (!buffer) { - return -1; - } - - SHELL_CTL->shell_rx_fifo_buffer = (uint8_t *)buffer; - tos_chr_fifo_create(&SHELL_CTL->shell_rx_fifo, buffer, cmd_buf_size * 2); - - if (tos_sem_create(&SHELL_CTL->shell_rx_sem, (k_sem_cnt_t)0u) != K_ERR_NONE) { - goto errout0; - } - - if (tos_task_create(&SHELL_CTL->parser, "shell_parser", shell_parser, - K_NULL, SHELL_PARSER_TASK_PRIO, shell_parser_task_stack, - SHELL_PARSER_TASK_STACK_SIZE, 0) != K_ERR_NONE) { - goto errout1; - } - - return 0; - -errout1: - tos_sem_destroy(&SHELL_CTL->shell_rx_sem); - -errout0: - tos_mmheap_free(SHELL_CTL->shell_rx_fifo_buffer); - SHELL_CTL->shell_rx_fifo_buffer = K_NULL; - tos_chr_fifo_destroy(&SHELL_CTL->shell_rx_fifo); - - return -1; -} - -__API__ void tos_shell_deinit(void) -{ - tos_task_destroy(&SHELL_CTL->parser); - tos_sem_destroy(&SHELL_CTL->shell_rx_sem); - - tos_mmheap_free(SHELL_CTL->shell_rx_fifo_buffer); - SHELL_CTL->shell_rx_fifo_buffer = K_NULL; - tos_chr_fifo_destroy(&SHELL_CTL->shell_rx_fifo); -} - -__API__ void tos_shell_printf(const char *format, ...) -{ - va_list args; - static char buffer[SHELL_OUTPUT_MAX]; - - va_start(args, format); - vsnprintf(buffer, sizeof(buffer), format, args); - va_end(args); - - (SHELL_CTL->output)(buffer); -} - -__API__ void tos_shell_input_byte(uint8_t data) -{ - if (tos_chr_fifo_push(&SHELL_CTL->shell_rx_fifo, data) == K_ERR_NONE) { - tos_sem_post(&SHELL_CTL->shell_rx_sem); - } -} - diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/sensor_parser.h b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/sensor_parser.h deleted file mode 100644 index b69c8169..00000000 --- a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/sensor_parser.h +++ /dev/null @@ -1,54 +0,0 @@ -/*---------------------------------------------------------------------------- - * Tencent is pleased to support the open source community by making TencentOS - * available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * If you have downloaded a copy of the TencentOS binary from Tencent, please - * note that the TencentOS binary is licensed under the BSD 3-Clause License. - * - * If you have downloaded a copy of the TencentOS source code from Tencent, - * please note that TencentOS source code is licensed under the BSD 3-Clause - * License, except for the third-party components listed below which are - * subject to different license terms. Your integration of TencentOS into your - * own projects may require compliance with the BSD 3-Clause License, as well - * as the other licenses applicable to the third-party components included - * within TencentOS. - *---------------------------------------------------------------------------*/ - -#ifndef _TOS_SHELL_H_ -#define _TOS_SHELL_H_ - -#include "tos_k.h" - -#define SHELL_CMD_ARGV_MAX 16 -#define SHELL_OUTPUT_MAX 256 -#define SHELL_PARSER_TASK_STACK_SIZE 1024 -#define SHELL_PARSER_TASK_PRIO 5 - -typedef void (*shell_output_t)(const char *str); - -typedef struct shell_control_st { - k_task_t parser; - - shell_output_t output; - - char *cmd_buffer; - int cmd_buffer_size; - - k_sem_t shell_rx_sem; - k_chr_fifo_t shell_rx_fifo; - uint8_t *shell_rx_fifo_buffer; -} shell_ctl_t; - -#define SHELL_CTL ((shell_ctl_t *)(&shell_ctl)) - -__API__ int tos_shell_init(char *cmd_buf, int cmd_buf_size, shell_output_t output); - -__API__ void tos_shell_deinit(void); - -__API__ void tos_shell_printf(const char *format, ...); - -__API__ void tos_shell_input_byte(uint8_t data); - -#endif /* _TOS_SHELL_H_ */ - diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/stm32wlxx_it.c b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/stm32wlxx_it.c index 6d5001dd..2a20c08d 100644 --- a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/stm32wlxx_it.c +++ b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/App/stm32wlxx_it.c @@ -25,7 +25,8 @@ #include "main.h" #include "stm32wlxx_it.h" #include "tos_k.h" -#include "sensor_parser.h" +#include "pm2d5_parser.h" +#include "tos_at.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ @@ -63,6 +64,7 @@ /* External variables --------------------------------------------------------*/ extern UART_HandleTypeDef huart1; extern UART_HandleTypeDef huart2; +extern UART_HandleTypeDef hlpuart1; extern SUBGHZ_HandleTypeDef hsubghz; /* USER CODE BEGIN EV */ @@ -209,7 +211,18 @@ void SysTick_Handler(void) /* For the available peripheral interrupt handler names, */ /* please refer to the startup file (startup_stm32wlxx.s). */ /******************************************************************************/ +void LPUART1_IRQHandler(void) +{ + /* USER CODE BEGIN LPUART1_IRQn 0 */ + /* USER CODE END LPUART1_IRQn 0 */ + tos_knl_irq_enter(); + HAL_UART_IRQHandler(&hlpuart1); + tos_knl_irq_leave(); + /* USER CODE BEGIN LPUART1_IRQn 1 */ + + /* USER CODE END LPUART1_IRQn 1 */ +} void USART1_IRQHandler(void) { /* USER CODE BEGIN USART1_IRQn 0 */ @@ -242,13 +255,16 @@ void Radio_IRQHandler(void) /* USER CODE BEGIN 1 */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { - extern uint8_t msg; - + extern uint8_t pm2d5_byte_data; + extern uint8_t data; if(huart ->Instance == USART2) { - tos_shell_input_byte(msg); - HAL_UART_Receive_IT(&huart2, (uint8_t*)&msg, 1); - + HAL_UART_Receive_IT(&huart2, (uint8_t*)&pm2d5_byte_data, 1); + pm2d5_parser_input_byte(pm2d5_byte_data); + } + else if (huart->Instance == LPUART1) { + HAL_UART_Receive_IT(&hlpuart1, &data, 1); + tos_at_uart_input_byte(data); } } /* USER CODE END 1 */ diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/TencentOS_Tiny.uvoptx b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/TencentOS_Tiny.uvoptx index fbb23bdc..39a447bc 100644 --- a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/TencentOS_Tiny.uvoptx +++ b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/TencentOS_Tiny.uvoptx @@ -152,9 +152,25 @@ 0 0 + 153 + 1 +
134218060
+ 0 + 0 + 0 + 0 + 0 + 1 + .\startup_stm32wle5xx.s + + \\TencentOS_Tiny\startup_stm32wle5xx.s\153 +
+ + 1 + 0 122 0 -
134244104
+
134279228
0 0 0 @@ -165,38 +181,6 @@ \\TencentOS_Tiny\../../../../kernel/core/tos_task.c\122
- - 1 - 0 - 105 - 1 -
134218200
- 0 - 0 - 0 - 0 - 0 - 1 - ..\..\..\..\arch\arm\arm-v7m\cortex-m4\armcc\port_s.S - - \\TencentOS_Tiny\../../../../arch/arm/arm-v7m/cortex-m4/armcc/port_s.S\105 -
- - 2 - 0 - 97 - 1 -
134218196
- 0 - 0 - 0 - 0 - 0 - 1 - ..\..\..\..\arch\arm\arm-v7m\cortex-m4\armcc\port_s.S - - \\TencentOS_Tiny\../../../../arch/arm/arm-v7m/cortex-m4/armcc/port_s.S\97 -
@@ -283,6 +267,18 @@ 0 0 0 + ..\..\BSP\Src\main.c + main.c + 0 + 0 + + + 2 + 3 + 1 + 0 + 0 + 0 ..\..\BSP\Src\adc.c adc.c 0 @@ -290,7 +286,7 @@ 2 - 3 + 4 1 0 0 @@ -302,7 +298,7 @@ 2 - 4 + 5 1 0 0 @@ -314,7 +310,7 @@ 2 - 5 + 6 1 0 0 @@ -326,7 +322,7 @@ 2 - 6 + 7 1 0 0 @@ -338,7 +334,7 @@ 2 - 7 + 8 1 0 0 @@ -350,7 +346,7 @@ 2 - 8 + 9 1 0 0 @@ -362,7 +358,7 @@ 2 - 9 + 10 1 0 0 @@ -374,7 +370,7 @@ 2 - 10 + 11 1 0 0 @@ -386,7 +382,7 @@ 2 - 11 + 12 1 0 0 @@ -398,7 +394,7 @@ 2 - 12 + 13 1 0 0 @@ -410,7 +406,7 @@ 2 - 13 + 14 1 0 0 @@ -430,7 +426,7 @@ 0 3 - 14 + 15 1 0 0 @@ -442,7 +438,7 @@ 3 - 15 + 16 1 0 0 @@ -454,7 +450,7 @@ 3 - 16 + 17 1 0 0 @@ -466,7 +462,7 @@ 3 - 17 + 18 1 0 0 @@ -478,7 +474,7 @@ 3 - 18 + 19 1 0 0 @@ -490,7 +486,7 @@ 3 - 19 + 20 1 0 0 @@ -502,7 +498,7 @@ 3 - 20 + 21 1 0 0 @@ -514,7 +510,7 @@ 3 - 21 + 22 1 0 0 @@ -526,7 +522,7 @@ 3 - 22 + 23 1 0 0 @@ -538,7 +534,7 @@ 3 - 23 + 24 1 0 0 @@ -550,7 +546,7 @@ 3 - 24 + 25 1 0 0 @@ -562,7 +558,7 @@ 3 - 25 + 26 1 0 0 @@ -574,7 +570,7 @@ 3 - 26 + 27 1 0 0 @@ -586,7 +582,7 @@ 3 - 27 + 28 1 0 0 @@ -598,7 +594,7 @@ 3 - 28 + 29 1 0 0 @@ -610,7 +606,7 @@ 3 - 29 + 30 1 0 0 @@ -622,7 +618,7 @@ 3 - 30 + 31 1 0 0 @@ -634,7 +630,7 @@ 3 - 31 + 32 1 0 0 @@ -646,7 +642,7 @@ 3 - 32 + 33 1 0 0 @@ -658,7 +654,7 @@ 3 - 33 + 34 1 0 0 @@ -670,7 +666,7 @@ 3 - 34 + 35 1 0 0 @@ -690,7 +686,7 @@ 0 4 - 35 + 36 1 0 0 @@ -710,7 +706,7 @@ 0 5 - 36 + 37 1 0 0 @@ -722,7 +718,7 @@ 5 - 37 + 38 1 0 0 @@ -734,7 +730,7 @@ 5 - 38 + 39 2 0 0 @@ -754,7 +750,7 @@ 0 6 - 39 + 40 1 0 0 @@ -766,7 +762,7 @@ 6 - 40 + 41 1 0 0 @@ -778,7 +774,7 @@ 6 - 41 + 42 1 0 0 @@ -790,7 +786,7 @@ 6 - 42 + 43 1 0 0 @@ -802,7 +798,7 @@ 6 - 43 + 44 1 0 0 @@ -814,7 +810,7 @@ 6 - 44 + 45 1 0 0 @@ -826,7 +822,7 @@ 6 - 45 + 46 1 0 0 @@ -838,7 +834,7 @@ 6 - 46 + 47 1 0 0 @@ -850,7 +846,7 @@ 6 - 47 + 48 1 0 0 @@ -862,7 +858,7 @@ 6 - 48 + 49 1 0 0 @@ -874,7 +870,7 @@ 6 - 49 + 50 1 0 0 @@ -886,7 +882,7 @@ 6 - 50 + 51 1 0 0 @@ -898,7 +894,7 @@ 6 - 51 + 52 1 0 0 @@ -910,7 +906,7 @@ 6 - 52 + 53 1 0 0 @@ -922,7 +918,7 @@ 6 - 53 + 54 1 0 0 @@ -934,7 +930,7 @@ 6 - 54 + 55 1 0 0 @@ -946,7 +942,7 @@ 6 - 55 + 56 1 0 0 @@ -958,7 +954,7 @@ 6 - 56 + 57 1 0 0 @@ -970,7 +966,7 @@ 6 - 57 + 58 1 0 0 @@ -982,7 +978,7 @@ 6 - 58 + 59 1 0 0 @@ -994,7 +990,7 @@ 6 - 59 + 60 1 0 0 @@ -1006,7 +1002,7 @@ 6 - 60 + 61 1 0 0 @@ -1018,7 +1014,7 @@ 6 - 61 + 62 1 0 0 @@ -1030,7 +1026,7 @@ 6 - 62 + 63 1 0 0 @@ -1042,7 +1038,7 @@ 6 - 63 + 64 1 0 0 @@ -1054,7 +1050,7 @@ 6 - 64 + 65 1 0 0 @@ -1066,7 +1062,7 @@ 6 - 65 + 66 1 0 0 @@ -1078,7 +1074,7 @@ 6 - 66 + 67 1 0 0 @@ -1098,7 +1094,7 @@ 0 7 - 67 + 68 1 0 0 @@ -1116,18 +1112,6 @@ 0 0 0 - - 8 - 68 - 1 - 0 - 0 - 0 - ..\..\BSP\Src\main.c - main.c - 0 - 0 - 8 69 @@ -1212,7 +1196,7 @@ Hardware - 1 + 0 0 0 0 @@ -1223,8 +1207,8 @@ 0 0 0 - .\App\sensor_parser.c - sensor_parser.c + .\App\pm2d5_parser.c + pm2d5_parser.c 0 0 @@ -1662,6 +1646,78 @@ + + at + 0 + 0 + 0 + 0 + + 17 + 108 + 1 + 0 + 0 + 0 + ..\..\..\..\net\at\src\tos_at.c + tos_at.c + 0 + 0 + + + 17 + 109 + 1 + 0 + 0 + 0 + ..\..\..\..\net\tencent_firmware_module_wrapper\tencent_firmware_module_wrapper.c + tencent_firmware_module_wrapper.c + 0 + 0 + + + + + hal + 0 + 0 + 0 + 0 + + 18 + 110 + 1 + 0 + 0 + 0 + ..\..\..\..\platform\hal\st\stm32wl\src\tos_hal_uart.c + tos_hal_uart.c + 0 + 0 + + + + + devices + 0 + 0 + 0 + 0 + + 19 + 111 + 1 + 0 + 0 + 0 + ..\..\..\..\devices\esp8266_tencent_firmware\esp8266_tencent_firmware.c + esp8266_tencent_firmware.c + 0 + 0 + + + ::CMSIS 0 diff --git a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/TencentOS_Tiny.uvprojx b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/TencentOS_Tiny.uvprojx index 7c4ce239..df24a9ca 100644 --- a/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/TencentOS_Tiny.uvprojx +++ b/board/TencentOS_Tiny_EVB_STM32WL/KEIL/lorawan_pm25/TencentOS_Tiny.uvprojx @@ -10,13 +10,13 @@ TencentOS_Tiny 0x4 ARM-ADS - 6100001::V6.10.1::.\ARMCLANG + 6140000::V6.14::ARMCLANG 1 STM32WLE5JCIx STMicroelectronics - Keil.STM32WLxx_DFP.1.0.2.OEM + Keil.STM32WLxx_DFP.1.0.7 http://www.keil.com/pack IRAM(0x20000000,0x10000) IROM(0x08000000,0x40000) CPUTYPE("Cortex-M4") CLOCK(12000000) ELITTLE @@ -185,6 +185,7 @@ 0 0 0 + 0 0 0 8 @@ -338,7 +339,7 @@ USE_HAL_DRIVER,STM32WLE5xx,CORE_CM4 - ..\..\..\..\platform\vendor_bsp\st\STM32WLxx_HAL_Driver\Inc;..\..\..\..\platform\vendor_bsp\st\STM32WLxx_HAL_Driver\Inc\Legacy;..\..\..\..\platform\vendor_bsp\st\CMSIS\Device\ST\STM32WLxx\Include;..\..\..\..\platform\vendor_bsp\st\CMSIS\Include;..\..\..\..\arch\arm\arm-v7m\common\include;..\..\..\..\arch\arm\arm-v7m\cortex-m4\armcc;..\..\..\..\kernel\core\include;..\..\..\..\kernel\pm\include;..\..\..\..\osal\cmsis_os;..\..\TOS_CONFIG;..\..\BSP\Middlewares\LoRaWAN\Conf;..\..\BSP\Middlewares\LoRaWAN\Crypto;..\..\BSP\Middlewares\LoRaWAN\Mac;..\..\BSP\Middlewares\LoRaWAN\Patterns\Basic;..\..\BSP\Middlewares\LoRaWAN\Utilities;..\..\BSP\Middlewares\SubGHz_Phy;..\..\BSP\Middlewares\SubGHz_Phy\stm32_radio_driver;..\..\BSP\Inc;.\App;.\Target;..\..\BSP\Utilities\lpm\tiny_lpm;..\..\BSP\Utilities\misc + ..\..\..\..\platform\vendor_bsp\st\STM32WLxx_HAL_Driver\Inc;..\..\..\..\platform\vendor_bsp\st\STM32WLxx_HAL_Driver\Inc\Legacy;..\..\..\..\platform\vendor_bsp\st\CMSIS\Device\ST\STM32WLxx\Include;..\..\..\..\platform\vendor_bsp\st\CMSIS\Include;..\..\..\..\arch\arm\arm-v7m\common\include;..\..\..\..\arch\arm\arm-v7m\cortex-m4\armcc;..\..\..\..\kernel\core\include;..\..\..\..\kernel\pm\include;..\..\..\..\kernel\hal\include;..\..\..\..\osal\cmsis_os;..\..\TOS_CONFIG;..\..\BSP\Middlewares\LoRaWAN\Conf;..\..\BSP\Middlewares\LoRaWAN\Crypto;..\..\BSP\Middlewares\LoRaWAN\Mac;..\..\BSP\Middlewares\LoRaWAN\Patterns\Basic;..\..\BSP\Middlewares\LoRaWAN\Utilities;..\..\BSP\Middlewares\SubGHz_Phy;..\..\BSP\Middlewares\SubGHz_Phy\stm32_radio_driver;..\..\BSP\Inc;.\App;.\Target;..\..\BSP\Utilities\lpm\tiny_lpm;..\..\BSP\Utilities\misc;..\..\..\..\net\at\include;..\..\..\..\net\tencent_firmware_module_wrapper;..\..\..\..\devices\esp8266_tencent_firmware @@ -351,7 +352,7 @@ 0 0 0 - 0 + 4 @@ -392,6 +393,11 @@ Application/User + + main.c + 1 + ..\..\BSP\Src\main.c + adc.c 1 @@ -752,11 +758,6 @@ TOS-CONFIG - - main.c - 1 - ..\..\BSP\Src\main.c - tos_config.h 5 @@ -798,9 +799,9 @@ Hardware - sensor_parser.c + pm2d5_parser.c 1 - .\App\sensor_parser.c + .\App\pm2d5_parser.c @@ -994,6 +995,41 @@ + + at + + + tos_at.c + 1 + ..\..\..\..\net\at\src\tos_at.c + + + tencent_firmware_module_wrapper.c + 1 + ..\..\..\..\net\tencent_firmware_module_wrapper\tencent_firmware_module_wrapper.c + + + + + hal + + + tos_hal_uart.c + 1 + ..\..\..\..\platform\hal\st\stm32wl\src\tos_hal_uart.c + + + + + devices + + + esp8266_tencent_firmware.c + 1 + ..\..\..\..\devices\esp8266_tencent_firmware\esp8266_tencent_firmware.c + + + ::CMSIS @@ -1014,4 +1050,19 @@ + + + + <Project Info> + + + + + + 0 + 1 + + + + diff --git a/platform/hal/st/stm32wl/src/tos_hal_uart.c b/platform/hal/st/stm32wl/src/tos_hal_uart.c new file mode 100644 index 00000000..1335077e --- /dev/null +++ b/platform/hal/st/stm32wl/src/tos_hal_uart.c @@ -0,0 +1,93 @@ +#include "tos_k.h" +#include "tos_hal.h" +#include "stm32wlxx_hal.h" +#include "usart.h" + +__API__ int tos_hal_uart_init(hal_uart_t *uart, hal_uart_port_t port) +{ + if (!uart) { + return -1; + } + if (port == HAL_UART_PORT_0) { + uart->private_uart = &hlpuart1; + MX_LPUART1_UART_Init(); + } else if (port == HAL_UART_PORT_1) { + uart->private_uart = &huart1; + MX_USART1_UART_Init(); + } else if (port == HAL_UART_PORT_2) { + uart->private_uart = &huart2; + MX_USART2_UART_Init(); + } + + return 0; +} + +__API__ int tos_hal_uart_write(hal_uart_t *uart, const uint8_t *buf, size_t size, uint32_t timeout) +{ + HAL_StatusTypeDef hal_status; + UART_HandleTypeDef *uart_handle; + + if (!uart || !buf) { + return -1; + } + + if (!uart->private_uart) { + return -1; + } + + uart_handle = (UART_HandleTypeDef *)uart->private_uart; + + hal_status = HAL_UART_Transmit(uart_handle, (uint8_t *)buf, size, timeout); + if (hal_status != HAL_OK) { + return -1; + } + return 0; +} + +__API__ int tos_hal_uart_read(hal_uart_t *uart, const uint8_t *buf, size_t size, uint32_t timeout) +{ + HAL_StatusTypeDef hal_status; + UART_HandleTypeDef *uart_handle; + + if (!uart || !buf) { + return -1; + } + + if (!uart->private_uart) { + return -1; + } + + uart_handle = (UART_HandleTypeDef *)uart->private_uart; + + hal_status = HAL_UART_Receive(uart_handle, (uint8_t *)buf, size, timeout); + if (hal_status != HAL_OK) { + return -1; + } + return 0; +} + +__API__ int tos_hal_uart_deinit(hal_uart_t *uart) +{ + HAL_StatusTypeDef hal_status; + UART_HandleTypeDef *uart_handle; + + if (!uart) { + return -1; + } + + if (!uart->private_uart) { + return -1; + } + + uart_handle = (UART_HandleTypeDef *)uart->private_uart; + + hal_status = HAL_UART_DeInit(uart_handle); + HAL_UART_MspDeInit(uart_handle); + + if (hal_status != HAL_OK) { + return -1; + } + + return 0; +} +