第一次提交
This commit is contained in:
471
interface_uart.c
Normal file
471
interface_uart.c
Normal file
@@ -0,0 +1,471 @@
|
||||
|
||||
|
||||
|
||||
#include "interface_uart.h"
|
||||
|
||||
|
||||
#define BUFFER_SIZE 256
|
||||
|
||||
#define SBUS_UART UART3
|
||||
|
||||
|
||||
UartFaultInfo uart_fault_info = {
|
||||
.remote_count = 0,
|
||||
.remote_state = 0,
|
||||
};
|
||||
|
||||
|
||||
|
||||
// <20><><EFBFBD>岢<EFBFBD><E5B2A2>ʼ<EFBFBD><CABC> SBus <20><><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>
|
||||
typedef struct {
|
||||
uint16_t channels[16]; // <20>洢16<31><36>ͨ<EFBFBD><CDA8><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t flags; // <20><>־<EFBFBD>ֽ<EFBFBD>
|
||||
} SBusData;
|
||||
|
||||
|
||||
SBusData sbus_data = {
|
||||
.channels = {0},
|
||||
.flags = 0
|
||||
};
|
||||
|
||||
///* Buffer used to receive data from the console */
|
||||
typedef struct user_data {
|
||||
uint8_t user_buffer[BUFFER_SIZE];
|
||||
uint8_t *buff_ptr;
|
||||
uint32_t buff_remain;
|
||||
} user_data_t;
|
||||
static user_data_t user_data;
|
||||
///* Buffer used to transmit welcome message */
|
||||
|
||||
|
||||
sdrv_uart_t uart_ctrl_ptr;
|
||||
|
||||
/* uart config structures */
|
||||
//sbus Ĭ<><C4AC>Ϊ100k
|
||||
static sdrv_uart_config_t g_uart_config = {
|
||||
.base = DEVICE_BASE(SBUS_UART),
|
||||
.irq = DEVICE_INTR(SBUS_UART),
|
||||
.baud = 100000,
|
||||
.data_bits = SDRV_UART_CHAR_8_BITS,
|
||||
.stop_bits = SDRV_UART_STOP_2_BIT,
|
||||
.parity = SDRV_UART_EVEN_PARITY,
|
||||
};
|
||||
|
||||
|
||||
// SBus <20><><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
SBusData* parseSBusData(const uint8_t *input_data)
|
||||
{
|
||||
|
||||
for(uint8_t i = 0; i < 25; i++)
|
||||
{
|
||||
test_app[i] = input_data[i];
|
||||
}
|
||||
test_app[25] = 0x44;
|
||||
|
||||
float sbus_temp = 0.0;
|
||||
// <20><><EFBFBD><EFBFBD>16<31><36>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ÿ<EFBFBD><C3BF>ͨ<EFBFBD><CDA8>Ϊ11λ 0xFΪ֡ͷ<D6A1><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
sbus_data.channels[0] = (input_data[1] | input_data[2] << 8) & 0x07FF;
|
||||
sbus_data.channels[1] = (input_data[2] >> 3 | input_data[3] << 5) & 0x07FF;
|
||||
sbus_data.channels[2] = (input_data[3] >> 6 | input_data[4] << 2 | input_data[5] << 10) & 0x07FF;
|
||||
sbus_data.channels[3] = (input_data[5] >> 1 | input_data[6] << 7) & 0x07FF;
|
||||
sbus_data.channels[4] = (input_data[6] >> 4 | input_data[7] << 4) & 0x07FF;
|
||||
sbus_data.channels[5] = (input_data[7] >> 7 | input_data[8] << 1 | input_data[9] << 9) & 0x07FF;
|
||||
sbus_data.channels[6] = (input_data[9] >> 2 | input_data[10] << 6) & 0x07FF;
|
||||
sbus_data.channels[7] = (input_data[10] >> 5 | input_data[11] << 3) & 0x07FF;
|
||||
sbus_data.channels[8] = (input_data[12] | input_data[13] << 8) & 0x07FF;
|
||||
sbus_data.channels[9] = (input_data[13] >> 3 | input_data[14] << 5) & 0x07FF;
|
||||
sbus_data.channels[10] = (input_data[14] >> 6 | input_data[15] << 2 | input_data[16] << 10) & 0x07FF;
|
||||
sbus_data.channels[11] = (input_data[16] >> 1 | input_data[17] << 7) & 0x07FF;
|
||||
sbus_data.channels[12] = (input_data[17] >> 4 | input_data[18] << 4) & 0x07FF;
|
||||
sbus_data.channels[13] = (input_data[18] >> 7 | input_data[19] << 1 | input_data[20] << 9) & 0x07FF;
|
||||
sbus_data.channels[14] = (input_data[20] >> 2 | input_data[21] << 6) & 0x07FF;
|
||||
sbus_data.channels[15] = (input_data[21] >> 5 | input_data[22] << 3) & 0x07FF;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ֽ<EFBFBD>
|
||||
sbus_data.flags = input_data[23];
|
||||
|
||||
//ת<><D7AA>
|
||||
sbus_temp = (float)(sbus_data.channels[0]);
|
||||
sbus_temp = (ZERO_VALUE - sbus_temp)*23;//<2F><>Ҫ<EFBFBD>任һ<E4BBBB>·<EFBFBD><C2B7>ţ<EFBFBD><C5A3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
remote_curvature = (uint16_t)((int16_t)sbus_temp);//<2F><><EFBFBD><EFBFBD>
|
||||
|
||||
//ת<><D7AA>
|
||||
sbus_temp = (float)(sbus_data.channels[1]);
|
||||
sbus_temp = (ZERO_VALUE - sbus_temp)*2.3;//<2F><>Ҫ<EFBFBD>任һ<E4BBBB>·<EFBFBD><C2B7>ţ<EFBFBD><C5A3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
remote_speed = (uint16_t)((int16_t)sbus_temp);//ת<><D7AA>
|
||||
|
||||
remote_Reserve = sbus_data.channels[7];
|
||||
|
||||
//SwA
|
||||
if(sbus_data.channels[3] >= 1700)//<2F><><EFBFBD><EFBFBD>
|
||||
{
|
||||
emergency_stop = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
emergency_stop = 0;
|
||||
}
|
||||
|
||||
//SwB
|
||||
if(sbus_data.channels[4] >= 1700)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
remote_switch_b = 2;
|
||||
}
|
||||
else if(sbus_data.channels[6] <= 250)//<2F>ڶ<EFBFBD><DAB6><EFBFBD>
|
||||
{
|
||||
remote_switch_b = 0;
|
||||
}
|
||||
else//<2F><>һ<EFBFBD><D2BB>
|
||||
{
|
||||
remote_switch_b = 1;
|
||||
}
|
||||
|
||||
//SwC
|
||||
if(sbus_data.channels[5] >= 1700)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
remote_switch_c = 2;
|
||||
}
|
||||
else if(sbus_data.channels[4] <= 250)//<2F>ڶ<EFBFBD><DAB6><EFBFBD>
|
||||
{
|
||||
remote_switch_c = 0;
|
||||
}
|
||||
else//<2F><>һ<EFBFBD><D2BB>
|
||||
{
|
||||
remote_switch_c = 1;
|
||||
}
|
||||
|
||||
//SwD
|
||||
if(sbus_data.channels[6] >= 1700)//<2F><><EFBFBD><EFBFBD>
|
||||
{
|
||||
remote_switch_d = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
remote_switch_d = 0;
|
||||
}
|
||||
|
||||
if(0 == sbus_data.flags)//<2F>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>߹<EFBFBD><DFB9><EFBFBD><EFBFBD>ж<EFBFBD> //ͨ<><CDA8><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD>жϣ<D0B6> Ϊ0<CEAA><30>ʾ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
remote_enable = 1;//<2F>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
else
|
||||
{
|
||||
remote_enable = 0;//<2F>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
ssdk_printf(SSDK_NOTICE, "sta\r\n");
|
||||
for(int i=0;i<16;i++)
|
||||
{
|
||||
ssdk_printf(SSDK_EMERG, "%04x\r\n",sbus_data.channels[i]);
|
||||
}
|
||||
ssdk_printf(SSDK_NOTICE, "end\r\n");
|
||||
return &sbus_data;
|
||||
}
|
||||
|
||||
|
||||
void callback(sdrv_uart_t *ctrl, sdrv_uart_callback_status_e status,
|
||||
void *userData)
|
||||
{
|
||||
uint8_t char_data[25] = {0};//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽڻ<D6BD><DABB><EFBFBD>
|
||||
static uint8_t cnt_sbus = 0;
|
||||
static uint8_t state = 0;
|
||||
static uint8_t receive = 0;
|
||||
static uint8_t sbus_buff[25];
|
||||
SBusData *tmp_sbus_data;
|
||||
|
||||
// user_data_t *user_data = (user_data_t *)userData;
|
||||
|
||||
// ssdk_printf(SSDK_NOTICE, "123456789\r\n");
|
||||
|
||||
/* Uart receive data. */
|
||||
if (SDRV_UART_RxFWF == status)
|
||||
{
|
||||
uart_fault_info.remote_count ++;
|
||||
|
||||
size_t size = sdrv_uart_get_rxfifodata(ctrl, char_data,1);
|
||||
|
||||
// for(uint8_t i = 0; i < 25; i++)
|
||||
// {
|
||||
// test_app[i] = char_data[i];
|
||||
//
|
||||
// }
|
||||
|
||||
// sdrv_uart_sync_transmit(&g_console_uart, char_data,
|
||||
// 25, NULL,
|
||||
// 0Xffff);
|
||||
|
||||
// for(uint8_t i=0;i<25;i++)
|
||||
// {
|
||||
// ssdk_printf(SSDK_INFO,"char_data: %x \r\n", char_data[i]);
|
||||
// }
|
||||
//
|
||||
|
||||
// for(int i=0;i<size;i++)
|
||||
// {
|
||||
switch(state)
|
||||
{
|
||||
case 0:
|
||||
if(0x0f == char_data[0])
|
||||
{
|
||||
receive = 0;
|
||||
cnt_sbus = 0;
|
||||
sbus_buff[cnt_sbus] = char_data[0];
|
||||
cnt_sbus++;
|
||||
state = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
state = 0;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if(cnt_sbus >= 24)
|
||||
{
|
||||
for(uint8_t i = 0; i < 25; i++)
|
||||
{
|
||||
test_app[i] = sbus_buff[i];
|
||||
}
|
||||
test_app[25] = 0x55;
|
||||
// ssdk_printf(SSDK_INFO,"char_data24: %x \r\n", char_data[0]);
|
||||
if(0x0 == char_data[0])
|
||||
{
|
||||
cnt_sbus = 0;
|
||||
receive = 0;
|
||||
sbus_buff[cnt_sbus] = char_data[0];
|
||||
state = 0;
|
||||
tmp_sbus_data = parseSBusData(sbus_buff);
|
||||
|
||||
// sdrv_uart_sync_transmit(ctrl, "AA\r\n", strlen("AA\r\n"), NULL, 0xFFFF);
|
||||
|
||||
// sdrv_uart_sync_transmit(ctrl, ( const uint8_t *)&tmp_sbus_data->channels[0],
|
||||
// 32, NULL,
|
||||
// 0Xffff);
|
||||
// ssdk_printf(SSDK_EMERG, "AAAr\n");
|
||||
}
|
||||
}
|
||||
else if(0x0f == char_data[0])
|
||||
{
|
||||
receive = 0;
|
||||
cnt_sbus = 0;
|
||||
sbus_buff[cnt_sbus] = char_data[0];
|
||||
cnt_sbus++;
|
||||
state = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sbus_buff[cnt_sbus] = char_data[0];
|
||||
cnt_sbus ++;
|
||||
state = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// }
|
||||
}
|
||||
else if (SDRV_UART_RxFifoOverFlow == status)
|
||||
{
|
||||
// if (0 != user_data->buff_remain) {
|
||||
// size_t size = sdrv_uart_get_rxfifodata(ctrl, user_data->user_buffer,
|
||||
// user_data->buff_remain);
|
||||
// }
|
||||
// sdrv_uart_sync_transmit(ctrl, "\r\n", strlen("\r\n"), NULL, TIMES_OUT);
|
||||
// sdrv_uart_sync_transmit(ctrl, user_data->user_buffer,
|
||||
// BUFFER_SIZE - user_data->buff_remain, NULL,
|
||||
// TIMES_OUT);
|
||||
// sdrv_uart_sync_transmit(ctrl, "\r\n", strlen("\r\n"), NULL, TIMES_OUT);
|
||||
//
|
||||
// memset(user_data->user_buffer, '\0', BUFFER_SIZE);
|
||||
// user_data->buff_ptr = user_data->user_buffer;
|
||||
// user_data->buff_remain = BUFFER_SIZE;
|
||||
//
|
||||
// sdrv_uart_sync_transmit(
|
||||
// ctrl, "\r\ncallback: SDRV_UART_RxFifoOverFlow!\r\n",
|
||||
// strlen("\r\ncallback: SDRV_UART_RxFifoOverFlow!\r\n"), NULL,
|
||||
// TIMES_OUT);
|
||||
//
|
||||
// /* User can stop or reset realtime receive if you want, when transfer
|
||||
// * occur errors */
|
||||
// sdrv_uart_stop_realtime_receive(ctrl);
|
||||
// sdrv_uart_start_realtime_receive(ctrl);
|
||||
|
||||
}
|
||||
else if (SDRV_UART_ParityError == status)
|
||||
{
|
||||
// sdrv_uart_sync_transmit(
|
||||
// ctrl, "\r\ncallback: SDRV_UART_ParityError!\r\n",
|
||||
// strlen("\r\ncallback: SDRV_UART_ParityError!\r\n"), NULL,
|
||||
// TIMES_OUT);
|
||||
}
|
||||
else if (SDRV_UART_BaudrateError == status)
|
||||
{
|
||||
// sdrv_uart_sync_transmit(
|
||||
// ctrl, "\r\ncallback: SDRV_UART_BaudrateError!\r\n",
|
||||
// strlen("\r\ncallback: SDRV_UART_BaudrateError!\r\n"), NULL,
|
||||
// TIMES_OUT);
|
||||
}
|
||||
else if (SDRV_UART_NoiseError == status)
|
||||
{
|
||||
// sdrv_uart_sync_transmit(
|
||||
// ctrl, "\r\ncallback: SDRV_UART_NoiseError!\r\n",
|
||||
// strlen("\r\ncallback: SDRV_UART_NoiseError!\r\n"), NULL, TIMES_OUT);
|
||||
}
|
||||
else if (SDRV_UART_FramingError == status)
|
||||
{
|
||||
// sdrv_uart_sync_transmit(
|
||||
// ctrl, "\r\ncallback: SDRV_UART_FramingError!\r\n",
|
||||
// strlen("\r\ncallback: SDRV_UART_FramingError!\r\n"), NULL,
|
||||
// TIMES_OUT);
|
||||
}
|
||||
else
|
||||
{
|
||||
// ssdk_printf(SSDK_NOTICE, "234567891\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void uart_Initialize(void)
|
||||
{
|
||||
/* Get uart clk Config. */
|
||||
#if ((CONFIG_E3210) || (CONFIG_E3110))
|
||||
if (g_uart_config.irq <= UART8_INTR_NUM) {
|
||||
g_uart_config.clk_freq =
|
||||
sdrv_ckgen_get_rate(CLK_NODE(g_ckgen_ip_uart_sf_1_to_8));
|
||||
} else {
|
||||
g_uart_config.clk_freq =
|
||||
sdrv_ckgen_get_rate(CLK_NODE(g_ckgen_ip_uart_sf_9_to_16));
|
||||
}
|
||||
#else
|
||||
if (g_uart_config.irq <= UART6_INTR_NUM) {
|
||||
g_uart_config.clk_freq =
|
||||
sdrv_ckgen_get_rate(CLK_NODE(g_ckgen_ip_uart_sf_1_to_6));
|
||||
} else {
|
||||
g_uart_config.clk_freq =
|
||||
sdrv_ckgen_get_rate(CLK_NODE(g_ckgen_ip_uart_sf_7_to_12));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Initializes sdrv uart controller. */
|
||||
|
||||
user_data.buff_ptr = user_data.user_buffer;
|
||||
user_data.buff_remain = BUFFER_SIZE;
|
||||
|
||||
ssdk_printf(SSDK_NOTICE, "uart3 init\r\n");
|
||||
sdrv_uart_controller_init(&uart_ctrl_ptr, &g_uart_config, callback,
|
||||
&user_data);
|
||||
|
||||
sdrv_uart_start_realtime_receive(&uart_ctrl_ptr);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void uartTimerProcess(void)
|
||||
{
|
||||
static uint8_t uart_timer = 0;
|
||||
static uint8_t uart_temp[1] = {2};//<2F>м<EFBFBD><D0BC>ж<EFBFBD>ֵ
|
||||
//----------------------------------------------------------------------------
|
||||
uart_timer ++;
|
||||
if(uart_timer >= 1)//500ms<6D>ж<EFBFBD>һ<EFBFBD><D2BB>
|
||||
{
|
||||
uart_timer = 0;
|
||||
if(uart_fault_info.remote_count == uart_temp[0])//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
||||
{
|
||||
uart_fault_info.remote_state = FAULT;
|
||||
}
|
||||
else
|
||||
{
|
||||
uart_fault_info.remote_state = NORMAL;
|
||||
uart_temp[0] = uart_fault_info.remote_count;//<2F><><EFBFBD>ݸ<EFBFBD><DDB8><EFBFBD>
|
||||
}
|
||||
|
||||
if(uart_fault_info.remote_state != uart_temp[1])
|
||||
{
|
||||
// publishMessage(&uart_fault_info.remote_state, 1);// ״̬<D7B4>仯 <20><><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
|
||||
uart_temp[1] = uart_fault_info.remote_state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//Timer uart_timer_interface;
|
||||
|
||||
// APPģ<50><C4A3><EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC>
|
||||
void uartAppInit(void)
|
||||
{
|
||||
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> brake_timer <20>ĵ<EFBFBD>ַ<EFBFBD><D6B7>Ϊ<EFBFBD>ź<EFBFBD>ID
|
||||
// timerInit(&uart_timer_interface, 100);
|
||||
// subscribe(&uart_timer_interface, uartTimerProcess);
|
||||
|
||||
// timerStart(&uart_timer_interface);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user