|
| 1 | +/*---------------------------------------------------------------------------- |
| 2 | + * Tencent is pleased to support the open source community by making TencentOS |
| 3 | + * available. |
| 4 | + * |
| 5 | + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. |
| 6 | + * If you have downloaded a copy of the TencentOS binary from Tencent, please |
| 7 | + * note that the TencentOS binary is licensed under the BSD 3-Clause License. |
| 8 | + * |
| 9 | + * If you have downloaded a copy of the TencentOS source code from Tencent, |
| 10 | + * please note that TencentOS source code is licensed under the BSD 3-Clause |
| 11 | + * License, except for the third-party components listed below which are |
| 12 | + * subject to different license terms. Your integration of TencentOS into your |
| 13 | + * own projects may require compliance with the BSD 3-Clause License, as well |
| 14 | + * as the other licenses applicable to the third-party components included |
| 15 | + * within TencentOS. |
| 16 | + *---------------------------------------------------------------------------*/ |
| 17 | +#include "ch20_parser.h" |
| 18 | + |
| 19 | +static ch20_parser_ctrl_t ch20_parser_ctrl; |
| 20 | + |
| 21 | +static k_stack_t ch20_parser_task_stack[CH20_PARSER_TASK_STACK_SIZE]; |
| 22 | + |
| 23 | +static uint8_t ch20_parser_buffer[CH20_PARSER_BUFFER_SIZE]; |
| 24 | + |
| 25 | +/** |
| 26 | + * @brief 向ch20解析器中送入一个字节数据 |
| 27 | + * @param data 送入的数据 |
| 28 | + * @retval none |
| 29 | + * @note 需要用户在串口中断函数中手动调用 |
| 30 | +*/ |
| 31 | +void ch20_parser_input_byte(uint8_t data) |
| 32 | +{ |
| 33 | + if (tos_chr_fifo_push(&ch20_parser_ctrl.parser_rx_fifo, data) == K_ERR_NONE) { |
| 34 | + /* 送入数据成功,释放信号量,计数 */ |
| 35 | + tos_sem_post(&ch20_parser_ctrl.parser_rx_sem); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief ch20解析器从chr fifo中取出一个字节数据 |
| 41 | + * @param none |
| 42 | + * @retval 正常返回读取数据,错误返回-1 |
| 43 | +*/ |
| 44 | +static int ch20_parser_getchar(void) |
| 45 | +{ |
| 46 | + uint8_t chr; |
| 47 | + k_err_t err; |
| 48 | + |
| 49 | + /* 永久等待信号量,信号量为空表示chr fifo中无数据 */ |
| 50 | + if (tos_sem_pend(&ch20_parser_ctrl.parser_rx_sem, TOS_TIME_FOREVER) != K_ERR_NONE) { |
| 51 | + return -1; |
| 52 | + } |
| 53 | + |
| 54 | + /* 从chr fifo中取出数据 */ |
| 55 | + err = tos_chr_fifo_pop(&ch20_parser_ctrl.parser_rx_fifo, &chr); |
| 56 | + |
| 57 | + return err == K_ERR_NONE ? chr : -1; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * @brief ch20读取传感器原始数据并解析 |
| 62 | + * @param void |
| 63 | + * @retval 解析成功返回0,解析失败返回-1 |
| 64 | +*/ |
| 65 | +static int ch20_parser_read_raw_data(ch20_data_t *ch20_data) |
| 66 | +{ |
| 67 | + uint8_t data; |
| 68 | + uint8_t data_h, data_l; |
| 69 | + uint8_t check_sum_cal = 0x17; |
| 70 | + |
| 71 | + /* 读取气体浓度单位 */ |
| 72 | + data = ch20_parser_getchar(); |
| 73 | + if (data != 0x04) { |
| 74 | + return -1; |
| 75 | + } |
| 76 | + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); |
| 77 | + check_sum_cal += data; |
| 78 | + |
| 79 | + /* 读取小数位数 */ |
| 80 | + data = ch20_parser_getchar(); |
| 81 | + if (data != 0x00) { |
| 82 | + return -1; |
| 83 | + } |
| 84 | + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); |
| 85 | + check_sum_cal += data; |
| 86 | + |
| 87 | + /* 读取气体浓度高位 */ |
| 88 | + data = ch20_parser_getchar(); |
| 89 | + if (data == 0xFF) { |
| 90 | + return -1; |
| 91 | + } |
| 92 | + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); |
| 93 | + data_h = data; |
| 94 | + check_sum_cal += data; |
| 95 | + |
| 96 | + /* 读取气体浓度低位 */ |
| 97 | + data = ch20_parser_getchar(); |
| 98 | + if (data == 0xFF) { |
| 99 | + return -1; |
| 100 | + } |
| 101 | + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); |
| 102 | + data_l = data; |
| 103 | + check_sum_cal += data; |
| 104 | + |
| 105 | + /* 读取满量程高位 */ |
| 106 | + data = ch20_parser_getchar(); |
| 107 | + if (data != 0x07) { |
| 108 | + return -1; |
| 109 | + } |
| 110 | + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); |
| 111 | + check_sum_cal += data; |
| 112 | + |
| 113 | + /* 读取满量程低位 */ |
| 114 | + data = ch20_parser_getchar(); |
| 115 | + if (data != 0xD0) { |
| 116 | + return -1; |
| 117 | + } |
| 118 | + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); |
| 119 | + check_sum_cal += data; |
| 120 | + |
| 121 | + /* 和校验 */ |
| 122 | + data = ch20_parser_getchar(); |
| 123 | + CH20_DEBUG_LOG("--->[%#02x]\r\n", data); |
| 124 | + check_sum_cal = ~(check_sum_cal) + 1; |
| 125 | + CH20_DEBUG_LOG("check_sum_cal is 0x%02x \r\n", check_sum_cal); |
| 126 | + if (check_sum_cal != data) { |
| 127 | + return -1; |
| 128 | + } |
| 129 | + |
| 130 | + /* 存储数据 */ |
| 131 | + ch20_data->data = (data_h << 8) + data_l; |
| 132 | + CH20_DEBUG_LOG("ch20_data->data is 0x%04x\r\n", ch20_data->data); |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +extern k_mail_q_t mail_q; |
| 138 | +ch20_data_t ch20_data; |
| 139 | + |
| 140 | +/** |
| 141 | + * @brief ch20解析器任务 |
| 142 | +*/ |
| 143 | +static void ch20_parser_task_entry(void *arg) |
| 144 | +{ |
| 145 | + int chr, last_chr = 0; |
| 146 | + |
| 147 | + while (1) { |
| 148 | + |
| 149 | + chr = ch20_parser_getchar(); |
| 150 | + if (chr < 0) { |
| 151 | + printf("parser task get char fail!\r\n"); |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + if (chr == 0x17 && last_chr == 0xFF) { |
| 156 | + /* 解析到包头 */ |
| 157 | + if (0 == ch20_parser_read_raw_data(&ch20_data)) { |
| 158 | + /* 正常解析之后通过邮箱发送 */ |
| 159 | + tos_mail_q_post(&mail_q, &ch20_data, sizeof(ch20_data_t)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + last_chr = chr; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * @brief 初始化ch20解析器 |
| 169 | + * @param none |
| 170 | + * @retval 全部创建成功返回0,任何一个创建失败则返回-1 |
| 171 | +*/ |
| 172 | +int ch20_parser_init(void) |
| 173 | +{ |
| 174 | + k_err_t ret; |
| 175 | + |
| 176 | + memset((ch20_parser_ctrl_t*)&ch20_parser_ctrl, 0, sizeof(ch20_parser_ctrl)); |
| 177 | + |
| 178 | + /* 创建 chr fifo */ |
| 179 | + ret = tos_chr_fifo_create(&ch20_parser_ctrl.parser_rx_fifo, ch20_parser_buffer, sizeof(ch20_parser_buffer)); |
| 180 | + if (ret != K_ERR_NONE) { |
| 181 | + printf("ch20 parser chr fifo create fail, ret = %d\r\n", ret); |
| 182 | + return -1; |
| 183 | + } |
| 184 | + |
| 185 | + /* 创建信号量 */ |
| 186 | + ret = tos_sem_create(&ch20_parser_ctrl.parser_rx_sem, 0); |
| 187 | + if (ret != K_ERR_NONE) { |
| 188 | + printf("ch20 parser_rx_sem create fail, ret = %d\r\n", ret); |
| 189 | + return -1; |
| 190 | + } |
| 191 | + |
| 192 | + /* 创建线程 */ |
| 193 | + ret = tos_task_create(&ch20_parser_ctrl.parser_task, "ch20_parser_task", |
| 194 | + ch20_parser_task_entry, NULL, CH20_PARSER_TASK_PRIO, |
| 195 | + ch20_parser_task_stack,CH20_PARSER_TASK_STACK_SIZE,0); |
| 196 | + if (ret != K_ERR_NONE) { |
| 197 | + printf("ch20 parser task create fail, ret = %d\r\n", ret); |
| 198 | + return -1; |
| 199 | + } |
| 200 | + |
| 201 | + return 0; |
| 202 | +} |
0 commit comments