概述
一、实验目的
1.实现串口信息打印。
2.每1s钟打印一次信息。
3.串口接收到数据,然后打印出来
一、实验步骤
1.CubeMax配置
配置PA9,PA10为串口输出,导出工程。
2.usart驱动创建
串口驱动实现原理:
中断接收到数据后通过事件通知应用层。
stm32_usart.c
#include "config.h"
#include "stm32l0xx_hal.h"
#include "cola_device.h"
#include "cola_os.h"
#include "config.h"
#include "usart.h"
#ifdef USING_USART1
static cola_device_t usart1_dev; //串口驱动函数
static task_t * usart1_depen_task = NULL;//当前串口所在的应用任务
static uint8_t data = 0; //用来暂时存放数据的,正常使用缓冲区,后期给出
#ifdef USING_DEBUG
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFF);
return ch;
}
#endif
static void uart1_configuration(uint32_t bund)
{
MX_USART1_UART_Init();
huart1.Init.BaudRate = bund;
HAL_UART_Init(&huart1);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_ERR);
}
static void uart1_sendnbyte(uint8_t *pData, uint16_t Length)
{
HAL_UART_Transmit(&huart1,pData,Length,0xFF);
}
static int uart1_write(cola_device_t *dev,int pos, const void *buffer, int size)
{
uart1_sendnbyte((uint8_t *)buffer,size);
return size;
}
static int uart1_read(cola_device_t *dev,int pos, void *buffer, int size)
{
uint8_t *index = (uint8_t *)buffer;
index[0] = data;
return 1;
}
static void USART1_RECV_IRQHandler(UART_HandleTypeDef *huart)
{
//uint8_t c;
data = (uint8_t)(huart->Instance->RDR);
if(usart1_depen_task)
cola_set_event(usart1_depen_task,SIG_DATA);
}
static int uart1_config(cola_device_t *dev,void *pos,void *args)
{
struct serial_configure *cfg = (struct serial_configure *)args;
uart1_configuration(cfg->baud_rate);
usart1_depen_task = (task_t *)pos;
return 0;
}
static struct cola_device_ops uart1_ops =
{
.write = uart1_write,
.read = uart1_read,
.config = uart1_config,
};
static void USER_UART1_IRQHandler(UART_HandleTypeDef *huart)
{
uint32_t isrflags = READ_REG(huart->Instance->ISR);
uint32_t cr1its = READ_REG(huart->Instance->CR1);
uint32_t cr3its = READ_REG(huart->Instance->CR3);
uint32_t errorflags;
uint32_t errorcode;
/* If no error occurs */
errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
if (errorflags == 0U)
{
/* UART in mode Receiver ---------------------------------------------------*/
if (((isrflags & USART_ISR_RXNE) != 0U)
&& ((cr1its & USART_CR1_RXNEIE) != 0U))
{
if (huart->RxISR != NULL)
{
huart->RxISR(huart);
}
USART1_RECV_IRQHandler(huart);
return;
}
}
/* If some errors occur */
if ((errorflags != 0U)
&& (((cr3its & USART_CR3_EIE) != 0U)
|| ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U)))
{
/* UART parity error interrupt occurred -------------------------------------*/
if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
{
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
huart->ErrorCode |= HAL_UART_ERROR_PE;
}
/* UART frame error interrupt occurred --------------------------------------*/
if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
{
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
huart->ErrorCode |= HAL_UART_ERROR_FE;
}
/* UART noise error interrupt occurred --------------------------------------*/
if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
{
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
huart->ErrorCode |= HAL_UART_ERROR_NE;
}
/* UART Over-Run interrupt occurred -----------------------------------------*/
if (((isrflags & USART_ISR_ORE) != 0U)
&& (((cr1its & USART_CR1_RXNEIE) != 0U) ||
((cr3its & USART_CR3_EIE) != 0U)))
{
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
huart->ErrorCode |= HAL_UART_ERROR_ORE;
}
/* Call UART Error Call back function if need be --------------------------*/
if (huart->ErrorCode != HAL_UART_ERROR_NONE)
{
/* UART in mode Receiver ---------------------------------------------------*/
if (((isrflags & USART_ISR_RXNE) != 0U)
&& ((cr1its & USART_CR1_RXNEIE) != 0U))
{
if (huart->RxISR != NULL)
{
huart->RxISR(huart);
}
}
/* If Overrun error occurs, or if any error occurs in DMA mode reception,
consider error as blocking */
errorcode = huart->ErrorCode;
if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
((errorcode & HAL_UART_ERROR_ORE) != 0U))
{
/* Blocking error : transfer is aborted
Set the UART state ready to be able to start again the process,
Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
//UART_EndRxTransfer(huart);
/* Disable the UART DMA Rx request if enabled */
if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
{
CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
/* Abort the UART DMA Rx channel */
if (huart->hdmarx != NULL)
{
/* Set the UART DMA Abort callback :
will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
// huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
/* Abort DMA RX */
if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
{
/* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
huart->hdmarx->XferAbortCallback(huart->hdmarx);
}
}
else
{
/* Call user error callback */
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
/*Call registered error callback*/
huart->ErrorCallback(huart);
#else
/*Call legacy weak error callback*/
HAL_UART_ErrorCallback(huart);
#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
}
}
else
{
/* Call user error callback */
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
/*Call registered error callback*/
huart->ErrorCallback(huart);
#else
/*Call legacy weak error callback*/
HAL_UART_ErrorCallback(huart);
#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
}
}
else
{
/* Non Blocking error : transfer could go on.
Error is notified to user through user error callback */
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
/*Call registered error callback*/
huart->ErrorCallback(huart);
#else
/*Call legacy weak error callback*/
HAL_UART_ErrorCallback(huart);
#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
huart->ErrorCode = HAL_UART_ERROR_NONE;
}
}
return;
} /* End if some error occurs */
/* UART wakeup from Stop mode interrupt occurred ---------------------------*/
if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U))
{
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF);
/* UART Rx state is not reset as a reception process might be ongoing.
If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
/* Call registered Wakeup Callback */
huart->WakeupCallback(huart);
#else
/* Call legacy weak Wakeup Callback */
HAL_UARTEx_WakeupCallback(huart);
#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
return;
}
/* UART in mode Transmitter ------------------------------------------------*/
if (((isrflags & USART_ISR_TXE) != 0U)
&& ((cr1its & USART_CR1_TXEIE) != 0U))
{
if (huart->TxISR != NULL)
{
huart->TxISR(huart);
}
return;
}
/* UART in mode Transmitter (transmission end) -----------------------------*/
if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
{
// UART_EndTransmit_IT(huart);
return;
}
}
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
USER_UART1_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
void usart_register(void)
{
#ifdef USING_USART1
uart1_configuration(115200);
usart1_dev.name = "usart1";
usart1_dev.dops = &uart1_ops;
cola_device_register(&usart1_dev);
#endif
}
device_initcall(usart_register);
#endif
3.usart应用
#include "cola_device.h"
#include "cola_os.h"
#include "example.h"
#ifdef APP_USART
static task_t timer_1s;
static task_t usart_task;
static cola_device_t *app_usart_dev;
//led每1s状态改变一次
static void timer_1s_cb(uint32_t event)
{
os_log("app_usart running...rn");
}
//usart 主函数任务管理
static void usart_task_event(uint32_t event)
{
uint8_t tmp[32] = {0};
int err = 0;
if(event & SIG_DATA)
{
err = cola_device_read(app_usart_dev,0,tmp,1);
if(err)
{
cola_device_write(app_usart_dev,0,tmp,err);
}
}
}
static void usart_config(void)
{
app_usart_dev = cola_device_find("usart1");
assert(app_usart_dev);
struct serial_configure cfg;
cfg.baud_rate = 9600;
cola_device_config(app_usart_dev,&usart_task,&cfg);
}
void app_usart_init(void)
{
cola_timer_create(&timer_1s,timer_1s_cb);
cola_timer_start(&timer_1s,TIMER_ALWAYS,1000);
cola_task_create(&usart_task,usart_task_event);
usart_config();
}
#endif
四、运行调试
五、总结
串口使用115200时,接收打印时会出现丢包,串口接收最好配合环形缓冲区使用,加缓冲区方式会在后期例程中讲解。
gitHub:https://github.com/sckuck-bit/stm32l0_demo
最后
以上就是舒心乐曲为你收集整理的STM32L例程03--串口输出usart及其printf重定向(基于cola_os)的全部内容,希望文章能够帮你解决STM32L例程03--串口输出usart及其printf重定向(基于cola_os)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复