diff --git a/board/TencentOS_tiny_EVB_MX_Plus/BSP/Hardware/PM25/sensor_parser.c b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Hardware/PM25/sensor_parser.c new file mode 100644 index 00000000..675b690c --- /dev/null +++ b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Hardware/PM25/sensor_parser.c @@ -0,0 +1,337 @@ +/*---------------------------------------------------------------------------- + * 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_MX_Plus/BSP/Hardware/PM25/sensor_parser.h b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Hardware/PM25/sensor_parser.h new file mode 100644 index 00000000..b69c8169 --- /dev/null +++ b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Hardware/PM25/sensor_parser.h @@ -0,0 +1,54 @@ +/*---------------------------------------------------------------------------- + * 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_MX_Plus/BSP/LoRaWAN/lora_demo.c b/board/TencentOS_tiny_EVB_MX_Plus/BSP/LoRaWAN/lora_demo.c new file mode 100644 index 00000000..af7281ee --- /dev/null +++ b/board/TencentOS_tiny_EVB_MX_Plus/BSP/LoRaWAN/lora_demo.c @@ -0,0 +1,119 @@ +#include "lora_demo.h" +#include "RHF76.h" +#include "sensor_parser.h" + +/* + ================================================================================== + data template: + + Type Name Token DataType RW Attribute + property temperature temperature integer readonly range: [-100, 155] + initial: 0 + step: 1 + unit: centigrade + + property humidity humidity integer readonly range: [-0, 100] + initial: 0 + step: 1 + unit: % + + property report_period period integer read-write range: [0, 3600] + initial: 0 + step: 1 + unit: second + + ================================================================================== + up-link parser javascript: + + function RawToProtocol(fPort, bytes) { + var data = { + "method": "report", + "clientToken" : new Date(), + "params" : {} + }; + data.params.temperature = bytes[0]; + data.params.humidity = bytes[1]; + data.params.period = bytes[2] | (bytes[3] << 8); + return data; + } + + ================================================================================== + down-link parser javascript: + + function ProtocolToRaw(obj) { + var data = new Array(); + data[0] = 5;// fport=5 + data[1] = 0;// unconfirmed mode + data[2] = obj.params.period & 0x00FF; + data[3] = (obj.params.period >> 8) & 0x00FF; + return data; + } + + */ + +uint16_t report_period = 10; + +k_mail_q_t mail_q; +#define DATA_CNT 26 +uint8_t mail_buf[DATA_CNT]; + +typedef struct device_data_st { + uint8_t data[DATA_CNT]; +} __PACKED__ dev_data_t; + +void recv_callback(uint8_t *data, uint8_t len) +{ + int i = 0; + + printf("len: %d\n", len); + + for (i = 0; i < len; ++i) { + printf("data[%d]: %d\n", i, data[i]); + } + + if (len == 1) { + report_period = data[0]; + } else if (len >= 2) { + report_period = data[0] | (data[1] << 8); + } + printf("report_period: %d\n", report_period); +} + + +#define CMD_LEN_MAX 1024 +char cmd_buf[CMD_LEN_MAX]; + +void uart_output(const char *str) +{ + /* if using c lib printf through uart, a simpler one is: */ + printf(str); +} + +dev_data_t dev_data; +uint8_t pool[DATA_CNT]; + +void application_entry(void *arg) +{ + int i = 0; + + rhf76_lora_init(HAL_UART_PORT_0); + tos_lora_module_recvcb_register(recv_callback); + + tos_lora_module_join_otaa("8cf957200000025a", "8cf957200000025a1e29aaaaad204a72"); + + tos_mail_q_create(&mail_q, pool, DATA_CNT, sizeof(uint8_t)); + tos_shell_init(cmd_buf, sizeof(cmd_buf), uart_output); + + while (1) { + size_t mail_size; + tos_mail_q_pend(&mail_q, &dev_data.data, &mail_size, TOS_TIME_FOREVER); + + for (i = 0; i < mail_size; ++i) { + printf("[%d] %x\n", i, dev_data.data[i]); + } + printf("\n\n"); + + tos_lora_module_send(&dev_data.data, mail_size); + } +} + diff --git a/board/TencentOS_tiny_EVB_MX_Plus/BSP/LoRaWAN/lora_demo.h b/board/TencentOS_tiny_EVB_MX_Plus/BSP/LoRaWAN/lora_demo.h new file mode 100644 index 00000000..fc0cf1e3 --- /dev/null +++ b/board/TencentOS_tiny_EVB_MX_Plus/BSP/LoRaWAN/lora_demo.h @@ -0,0 +1,24 @@ +#ifndef __LORA_DEMO_H__ +#define __LORA_DEMO_H__ + +#include "mcu_init.h" +#include "tos_at.h" +#include "string.h" +#include "tos_k.h" +#include "lora_module_wrapper.h" +#ifdef __cplusplus + extern "C" { +#endif + +typedef struct +{ + char data[32]; +}ReportData_TypeDef; + +void application_entry(void *arg); +#ifdef __cplusplus +} +#endif + +#endif /* __LORA_DEMO_H__ */ + diff --git a/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/stm32l4xx_it_lorawan.c b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/stm32l4xx_it_lorawan.c new file mode 100644 index 00000000..401b6b85 --- /dev/null +++ b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/stm32l4xx_it_lorawan.c @@ -0,0 +1,313 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32l4xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + *