CC2640R2F&TI-RTOS 拿到 TI CC2640R2F 开发板 第一件事就是移植串口驱动,重定向 printf

/*
 * board_uart.c
 *
 *  Created on: 2018年7月3日
 *      Author: admin
 */


#include "board_uart.h"

#include "board.h"
#include <ti/drivers/uart/UARTCC26XX.h>

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#define UART0_RECEICE_BUFF_SIZE 64

static UART_Handle uart0BoardHandle;

static uint8_t uart0ReceiveBuff[UART0_RECEICE_BUFF_SIZE];

static char debugVsprintfBuff[256] = {0};
void bspDebugPrintf( uint32_t enableFlag, char *str, ... )
{/* 打印调试函数,数据发给串口0,2018年7月2日19:07:33 */

    va_list argptr;
    uint32_t len=0;

    if( enableFlag != true )
            return;

    if(strlen(str) > 128)
            return;

    va_start(argptr, str);
    len = vsprintf(debugVsprintfBuff, str, argptr);
    va_end(argptr);

    UART_write(uart0BoardHandle, debugVsprintfBuff, len);

}

void uart0BoardReciveCallback(UART_Handle handle, void *buf, size_t count)
{/* 串口0接收回调函数,2018年7月3日15:16:30,115200bps */

    bspDebugPrintf( true,"uart0 Recive len:%u,data:%s.\r\n", count, buf );

    //设定接收缓冲区,并开始接收数据
    UART_read( uart0BoardHandle, uart0ReceiveBuff, UART0_RECEICE_BUFF_SIZE );
}

void uart0BoardInit( void )
{/* 初始化串口0,2018年7月2日19:07:33,115200bps */
    UART_Params uart0BoardParams;

    //板载串口0初始化,包括时钟引脚初始化
    Board_initUART();

    /* Create a UART with data processing off. */
    UART_Params_init(&uart0BoardParams);/* 串口各项参数先设置为默认值 */

    uart0BoardParams.baudRate       = 115200;
    uart0BoardParams.readEcho       = UART_ECHO_OFF;
    uart0BoardParams.writeDataMode  = UART_DATA_BINARY;
    uart0BoardParams.readDataMode   = UART_DATA_BINARY;
    uart0BoardParams.readMode       = UART_MODE_CALLBACK;
    uart0BoardParams.readCallback   = uart0BoardReciveCallback;

    uart0BoardHandle = UART_open(Board_UART0, &uart0BoardParams);

    if (uart0BoardHandle == NULL) {
        /* UART_open() failed */
        while (1);
    }
    else
    {
      //使能串口空闲接收中断,即串口接收空闲,进入接收回调函数
      UART_control( uart0BoardHandle, UARTCC26XX_CMD_RETURN_PARTIAL_ENABLE, NULL  );

      //设定接收缓冲区,并开始接收数据
      UART_read( uart0BoardHandle, uart0ReceiveBuff, UART0_RECEICE_BUFF_SIZE );

      bspDebugPrintf( true,"uart0 Board Init end.\r\n" );

    }

}
/*
 * board_uart.h
 *
 *  Created on: 2018年7月3日
 *      Author: admin
 */

#ifndef APPLICATION_BOARD_UART_H_
#define APPLICATION_BOARD_UART_H_

#include <stdint.h>

void bspDebugPrintf( uint32_t enableFlag, char *str, ... );

void uart0BoardInit( void );


#endif /* APPLICATION_BOARD_UART_H_ */

官方资料链接

posted on 2018-07-05 15:14  所长  阅读(2817)  评论(0编辑  收藏  举报

导航