#include "shell_port.h"
#include "usart.h"
Shell shell;
char shellBuffer[512];
#define userShellhuart huart2 //shell 使用到的串口句柄
#define SHELL_UART_REC_LEN_ONCE 1 //串口单次接收的个数
uint8_t uartRecBuffer[SHELL_UART_REC_LEN_ONCE]; //串口接收的buffer
/**
* @brief 用户shell写
*
* @param data 数据
*/
void userShellWrite(char data)
{
HAL_UART_Transmit(&userShellhuart,(uint8_t *)&data, 1,1000);
}
/**
* @brief 用户shell读
*
* @param data 数据
* @return char 状态
*/
signed char userShellRead(char *data)
{
if(HAL_UART_Receive(&userShellhuart,(uint8_t *)data, 1, 0) == HAL_OK)
{
return 0;
}
else
{
return -1;
}
}
/**
* @brief 自定义函数串口接收完成中断 RxCpltCallback
*/
void ShellRxCpltCallback(UART_HandleTypeDef *huart)
{
shellHandler(&shell, uartRecBuffer[0]); //命令行处理函数
HAL_UART_Receive_IT(huart, (uint8_t *)uartRecBuffer, SHELL_UART_REC_LEN_ONCE);//使能串口中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量
}
/**
* @brief 注册串口接收中断到自己的自定义函数
*/
void ShellReceiveCallbackRemap(void)
{
HAL_UART_RegisterCallback(&userShellhuart,HAL_UART_RX_COMPLETE_CB_ID,ShellRxCpltCallback); //注册串口接收中断到自己的自定义函数
HAL_UART_Receive_IT(&userShellhuart, (uint8_t *)uartRecBuffer, SHELL_UART_REC_LEN_ONCE); //使能串口中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量
}
/**
* @brief 用户shell初始化
*
*/
void userShellInit(void)
{
shell.write = userShellWrite; //指定串口写入的函数
shell.read = userShellRead; //指定串口读取的函数
ShellReceiveCallbackRemap(); //注册串口接收中断到自己的自定义函数
shellInit(&shell, shellBuffer, 512);
}
|