重玩LPC1768——Uart串口输出
串口做为最古老的调试信息输出方式就是本次实验的目的。LPCopen库中将Uart的驱动与RingBuffer数据结构相结合,坦白讲,我还是不太喜欢。原因是其会影响到我的上层应用数据的缓冲处理。换句话说,LPCopen库自己增加的这层缓存将会导致我多一层数据拷贝。
不过,本次实现仍然使用了官方的库,自己增加了一些封装而已。
因为本次实验主要是实现uart0的输出,并且之后将使用该接口打印调试信息,所以将其定位为log输出功能。
硬件上面,根据原理图所示,使用Uart0外设,引脚为P0.2与P0.3两个引脚,均为Func01功能。配置串口通讯协议参数为115200bps。
uart0外设初始化源代码如下:
- void uart_init(void)
- {
- Chip_GPIO_Init(LPC_GPIO);
- Chip_IOCON_PinMux(LPC_IOCON, 0, 2, IOCON_MODE_INACT, IOCON_FUNC1);
- Chip_IOCON_PinMux(LPC_IOCON, 0, 3, IOCON_MODE_INACT, IOCON_FUNC1);
- /* Setup UART for 115.2K8N1 */
- Chip_UART_Init(LPC_UART0);
- Chip_UART_SetBaud(LPC_UART0, 115200);
- Chip_UART_ConfigData(LPC_UART0, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT));
- Chip_UART_SetupFIFOS(LPC_UART0, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2));
- Chip_UART_TXEnable(LPC_UART0);
- /* Reset and enable FIFOs, FIFO trigger level 3 (14 chars) */
- Chip_UART_SetupFIFOS(LPC_UART0, (UART_FCR_FIFO_EN | UART_FCR_RX_RS |
- UART_FCR_TX_RS | UART_FCR_TRG_LEV3));
- /* Enable receive data and line status interrupt */
- Chip_UART_IntEnable(LPC_UART0, (UART_IER_RBRINT | UART_IER_RLSINT));
- /* preemption = 1, sub-priority = 1 */
- NVIC_SetPriority(UART0_IRQn, 1);
- NVIC_EnableIRQ(UART0_IRQn);
- }
复制代码
相对来讲,依托于LPCOpen库的一层封装,程序在操控外设上面还是容易了不少,有效缩短了工程师在软件开发上的时间与精力。
最后,附上log.c的接口设计源代码:
- /**
- ******************************************************************************
- * @file jlog.c
- * @author jobs
- * @version v0.00
- * @date 2020-08-30
- * @brief using Uart0 to output the debug info.
- * @note
- *
- ******************************************************************************
- */
- #include <string.h>
- #include "jlog.h"
- #include "ring_buffer.h"
- #include "bspuart.h"
- /* Transmit and receive ring buffers */
- STATIC RINGBUFF_T txring, rxring;
- /* Transmit and receive ring buffer sizes */
- #define UART_SRB_SIZE 128 /* Send */
- #define UART_RRB_SIZE 32 /* Receive */
- /* Transmit and receive buffers */
- static uint8_t rxbuff[UART_RRB_SIZE], txbuff[UART_SRB_SIZE];
- void jlog_init(void)
- {
- uart_init();
- /* Before using the ring buffers, initialize them using the ring
- buffer init function */
- RingBuffer_Init(&rxring, rxbuff, 1, UART_RRB_SIZE);
- RingBuffer_Init(&txring, txbuff, 1, UART_SRB_SIZE);
- }
- void jlog_handler(void)
- {
- /* Use default ring buffer handler. Override this with your own
- code if you need more capability. */
- uart0_irqb_handler(&rxring, &txring);
- }
- int32_t jlog_out(const char *mbuf, int16_t len)
- {
- uart0_send_rb(&txring, mbuf, len);
- }
- /******************************** END OF FILE *********************************/
复制代码
|