Files
kunlun/app/iot_cus_at_app/driver/app_uart.c

271 lines
7.6 KiB
C
Raw Permalink Normal View History

2024-09-28 14:24:04 +08:00
/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#include "app_main.h"
#include "app_config.h"
#include "app_common.h"
#include "app_uart.h"
#include "app_cus_task.h"
#include "iot_app_pib_api.h"
#include "iot_plc_led_api.h"
#define UART_STOP_BITS_NUM 3
/* global buffer to hold uart half frame. */
uart_handle_buffer g_uart_buffer;
uint8_t app_uart_is_valid(uint32_t baudrate, uint8_t data_bits,
uint8_t stop_bits, uint8_t parity, uint32_t thresholdvalue)
{
switch (baudrate) {
case 3000000:
case 921600:
case 380400:
case 115200:
case 38400:
case 19200:
case 9600:
case 4800:
case 2400:
break;
default:
return false;
}
switch (data_bits) {
case IOT_UART_DLEN_5_BITS:
case IOT_UART_DLEN_6_BITS:
case IOT_UART_DLEN_7_BITS:
case IOT_UART_DLEN_8_BITS:
break;
default:
return false;
}
switch (stop_bits) {
case IOT_UART_STOP_1_BITS:
case IOT_UART_STOP_1_5_BITS:
case IOT_UART_STOP_2_BITS:
break;
default:
return false;
}
switch (parity) {
case IOT_UART_PARITY_NONE:
case IOT_UART_PARITY_ODD:
case IOT_UART_PARITY_EVEN:
break;
default:
return false;
}
if(thresholdvalue > APP_MAX_THDVALUE ||
thresholdvalue < APP_DEFAULT_THDVALUE){
return false;
}
return true;
}
uint8_t app_uart_stop_bits_itoa(uint8_t value, char *str)
{
uint8_t i;
char *stop_bit_symbol[UART_STOP_BITS_NUM] = {"1", "1.5", "2"};
uint8_t stop_bit_option[UART_STOP_BITS_NUM] = {
IOT_UART_STOP_1_BITS,
IOT_UART_STOP_1_5_BITS,
IOT_UART_STOP_2_BITS
};
for (i = 0; i < UART_STOP_BITS_NUM; i++) {
if (stop_bit_option[i] == value) {
os_mem_cpy(str, stop_bit_symbol[i], strlen(stop_bit_symbol[i]));
return true;
}
}
return false;
}
uint8_t app_uart_stop_bits_atoi(char *str, uint8_t *value)
{
uint8_t i;
char *stop_bit_symbol[UART_STOP_BITS_NUM] = {"1", "1.5", "2"};
uint8_t stop_bit_option[UART_STOP_BITS_NUM] = {
IOT_UART_STOP_1_BITS,
IOT_UART_STOP_1_5_BITS,
IOT_UART_STOP_2_BITS
};
for (i = 0; i < UART_STOP_BITS_NUM; i++) {
if (!iot_strcmp((char *)str, stop_bit_symbol[i])) {
*value = stop_bit_option[i];
return true;
}
}
return false;
}
uint16_t app_uart_tx(iot_uart_h hdl, uint8_t * data, uint16_t data_length)
{
iot_pkt_t *uart_pkt;
if (NULL == data || data_length == 0) {
APP_PRINTF("[ERR] %s Data is NULL !!", __FUNCTION__);
return ERR_FAIL;
}
uart_pkt = iot_pkt_alloc(data_length, IOT_APP_DEMO_MID);
if (NULL == uart_pkt) {
APP_PRINTF("[ERR] %s Packet Alloc Failed !!", __FUNCTION__);
return ERR_FAIL;
}
os_mem_cpy(iot_pkt_put(uart_pkt, data_length), (void *)data, data_length);
APP_PRINT_BUF("[UART_TX]", data, data_length);
iot_uart_send(hdl, uart_pkt, NULL);
return ERR_OK;
}
/* Post the data from uart port to the message queue. */
uint32_t app_uart_recv(uint8_t* buffer, uint32_t buffer_len,
bool_t is_full_frame, uint32_t invalid_data_len)
{
iot_pkt_t *pkt;
iot_plc_led_request(IOT_PLC_LED_REQ_PLC_485_RX);
APP_PRINT_BUF("[UART_RX]: ", buffer, buffer_len);
pkt = iot_pkt_alloc(buffer_len, IOT_APP_DEMO_MID);
if (NULL == pkt) {
APP_PRINTF("[ERR] %s Packet Alloc Failed !!", __FUNCTION__);
return ERR_NOMEM;
}
os_mem_cpy(iot_pkt_put(pkt, buffer_len), buffer, buffer_len);
app_cus_task_msg_post(E_CUS_FROM_UART_MSG, APP_CUS_TASK_MSG_ID_UART, (void*)pkt);
return ERR_OK;
}
void app_uart_buf_clear(app_source_e source)
{
/* 之前存在断帧,超时时间已过或者不是串口接收到数据就将之前的断帧数据清除 */
if (g_uart_buffer.buff_len > 0 &&
(g_uart_buffer.boot_tm + UART_RECV_TIMEOUT < os_boot_time32() ||
source != APP_SOURCE_UART)) {
os_mem_set(g_uart_buffer.buff_tmp, 0, g_uart_buffer.buff_len);
g_uart_buffer.buff_len = 0;
}
g_uart_buffer.boot_tm = os_boot_time32();
}
uart_handle_buffer *app_get_uart_buf_info(void)
{
return &g_uart_buffer;
}
uint8_t app_uart_reassign_pin(uint8_t port, uint8_t rx_pin, uint8_t tx_pin)
{
uint8_t ret = ERR_FAIL;
/* chehck uart port is valid */
if (port >= iot_uart_get_max_port_num()) {
goto out;
}
/* set uart rx pin as input */
ret = iot_gpio_open_as_input(rx_pin);
if (ret) {
goto out;
}
/* set uart tx pin as output */
ret = iot_gpio_open_as_output(tx_pin);
if (ret) {
goto out;
}
/* set uart port pin */
iot_uart_set_pin(port, rx_pin, tx_pin);
out:
return ret;
}
uint16_t app_uart_init(iot_uart_h *uart_handle)
{
uint8_t port;
uint8_t port_name = UART_CUS_PORT_0;
uart_cfg_t uart = {0};
char stop_bits[4] = {0};
iot_pkt_t *tmp_pkt;
/* Get uart port for tranceiving data, not for printf() used. */
if ((port = iot_board_get_uart(port_name)) >= iot_uart_get_max_port_num()) {
APP_PRINTF("[ERR] %s Invalid UART#%d !!", __FUNCTION__, port);
return ERR_FAIL;
}
*uart_handle = iot_uart_open(port, (iot_uart_recv_func_t)app_uart_recv,
UART_RCV_BUF_LEN, NULL);
if (NULL == *uart_handle) {
APP_PRINTF("[ERR] %s Open UART#%d Failed !!", __FUNCTION__, port);
return ERR_FAIL;
}
if (iot_app_get_uart_cfg_in_pib(&uart) != ERR_OK
|| 0 == uart.uart_conf_vaild) {
APP_PRINTF("[ERR] %s Get uart config failed, we will use default param!",
__FUNCTION__);
uart.baud_rate = APP_DEFAULT_BAUND_RATE;
uart.data_bits = APP_DEFAULT_DATA_BITS;
uart.stop_bits = APP_DEFAULT_STOP_BITS;
uart.parity = APP_DEFAULT_PARITY;
uart.threshold_value = APP_DEFAULT_THDVALUE;
}
if (false == app_uart_stop_bits_itoa(uart.stop_bits, stop_bits)) {
APP_PRINTF("[ERR]%s invalid uart param", __FUNCTION__);
}
APP_PRINTF("[INF]%s baudrate=%d, data_bits=%d, stop_bits=%s(bits), "
"parity=%d, thresholdvalue=%d", __FUNCTION__, uart.baud_rate,
uart.data_bits, stop_bits, uart.parity, uart.threshold_value);
iot_uart_set_config(*uart_handle, uart.baud_rate, uart.parity,
uart.data_bits, uart.stop_bits);
iot_uart_set_threshold(*uart_handle, UART_THR_RXTIMEOUT,
uart.threshold_value);
iot_uart_set_threshold(*uart_handle, UART_THR_NO_FMT_TIMEOUT, 20);
tmp_pkt = iot_pkt_alloc(UART_BUF_MAX_LEN, IOT_APP_DEMO_MID);
if (NULL == tmp_pkt) {
APP_PRINTF("[ERR] ALLOC PKT FAILED !!");
return ERR_FAIL;
}
/* 创建空间缓存串口数据 */
g_uart_buffer.buff_tmp = (uint8_t*)iot_pkt_data(tmp_pkt);
return ERR_OK;
}