测试ST 官网的 FreeRTOS
移植shell
主要是串口对接, 需要重写两个函数
- int fputc(int ch, FILE *f)
- {
- /* Place your implementation of fputc here */
- /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
- HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, UART_TIMEOUT_VALUE);
- return ch;
- }
- // #endif
- char shell_buf[RXBUFFERSIZE]={0};
- char shell_len=0;
- char *shell_buf_head=NULL;
- int fgetc(FILE *f)
- {
- while (0 == shell_len) {
- osDelay(1);
- }
- char ch = *shell_buf_head;
- shell_buf_head ++;
- shell_len --;
- return ch;
- }
复制代码
增加了一个回调函数, 收到数据拷贝到shell_buf, 然后重新初始化串口中断接收参数,指针,size,count
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
- {
- /* Set transmission flag: transfer complete */
- if(UartHandle->Instance == USART1) {
- // UartReady = SET;
- shell_len = UartHandle->pRxBuffPtr - aRxBuffer;
- shell_buf_head = shell_buf;
- memcpy(shell_buf, aRxBuffer, shell_len);
- UartHandle->pRxBuffPtr = aRxBuffer;
- UartHandle->RxXferSize = RXBUFFERSIZE;
- UartHandle->RxXferCount = RXBUFFERSIZE;
- }
- }
复制代码
遇到的问题, 1. fgetc 不能直接调用HAL_UART_Receive, 否则会死等,导致其他任务无法运行。
解决这个问题的方法是,接收采用中断方式。
问题2. STM32CubeMX 生成的代码,串口中断接收是固定长度的方式, 对于shell 不适用。
解决这个问题的方法是,修改中断处理程序,如果是调试用的串口,直接处理接收数据
- static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
- {
- uint16_t uhMask = huart->Mask;
- uint16_t uhdata;
- /* Check that a Rx process is ongoing */
- if (huart->RxState == HAL_UART_STATE_BUSY_RX)
- {
- uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
- *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
- huart->pRxBuffPtr++;
- huart->RxXferCount--;
- if(USART1 == huart->Instance) {
- HAL_UART_RxCpltCallback(huart);
- }
- if (huart->RxXferCount == 0U) {
- /* Disable the UART Parity Error Interrupt and RXNE interrupts */
- CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
- /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
- CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
- /* Rx process is completed, restore huart->RxState to Ready */
- huart->RxState = HAL_UART_STATE_READY;
- /* Clear RxISR function pointer */
- huart->RxISR = NULL;
- #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
- /*Call registered Rx complete callback*/
- huart->RxCpltCallback(huart);
- #else
- /*Call legacy weak Rx complete callback*/
- HAL_UART_RxCpltCallback(huart);
- #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
- }
- }
- else
- {
- /* Clear RXNE interrupt flag */
- __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
- }
- }
复制代码
问题3. shell 放在Mgr task里面,由于内存分配128*4 不够导致死机
解决这个问题的方法是,去掉没有用的shell 指令,限制最多支持64条指令。 任务内存分配加大 256*4
最终效果图
|