MSP432P401R TI Drivers 库函数学习笔记(六)UART 串口

目录

  • API (机翻)
  • 上机实战
    • 配置
  • 初始化和实验
    • 实验结果
    • 整体代码
      • myUart.c
      • myUart.h
      • myTask.c
      • myTask.h
      • main.c
      • main.h

平台:Code Composer Studio 10.4.0
MSP432P401R SimpleLink™ 微控制器 LaunchPad™ 开发套件
(MSP-EXP432P401R)


API (机翻)

官方UART API 手册

void 	UART_close (UART_Handle handle)
关闭由UART句柄指定的UART外围设备

int_fast16_t 	UART_control (UART_Handle handle, uint_fast16_t cmd, void *arg)
在给定的UART_Handle上执行实现特定的特性
 
void 	UART_init (void)
初始化UART模块
 
UART_Handle 	UART_open (uint_least8_t index, UART_Params *params)
初始化给定的UART外围设备
 
void 	UART_Params_init (UART_Params *params)
初始化UART_Params结构体为其默认值
 
int_fast32_t 	UART_write (UART_Handle handle, const void *buffer, size_t size)
用于在启用中断的情况下将数据写入UART
 
int_fast32_t 	UART_writePolling (UART_Handle handle, const void *buffer, size_t size)
向UART写入数据的函数,它轮询外围设备以等待可以写入新数据。此API的使用与UART_write()
的使用是互斥的
 
void 	UART_writeCancel (UART_Handle handle)
取消调用UART_write()
 
int_fast32_t 	UART_read (UART_Handle handle, void *buffer, size_t size)
用于从启用中断的UART读取数据
 
int_fast32_t 	UART_readPolling (UART_Handle handle, void *buffer, size_t size)
从UART读取数据而不中断。此API必须与UART_read()互斥使用
 
void 	UART_readCancel (UART_Handle handle)
取消调用UART_read()

上机实战

配置

MSP432P401R TI Drivers 库函数学习笔记(六)UART 串口_第1张图片
LED1用于提示单片机正常运行
MSP432P401R TI Drivers 库函数学习笔记(六)UART 串口_第2张图片

初始化和实验

/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
     

	My_Task_Init(LED_Task, 1, 1024);
	My_Uart_Init(&huart1, USB_UART, 115200);

    while(1)
    {
     
    	UART_printf(huart1, "MSP432P401R in 115200\r\n");
    	usleep(1000);
    }
}

实验结果

MSP432P401R TI Drivers 库函数学习笔记(六)UART 串口_第3张图片
点击发送
MSP432P401R TI Drivers 库函数学习笔记(六)UART 串口_第4张图片
如图收发实验成功

整体代码

myUart.c

/*
 * myUart.c
 *
 *  Created on: 2021年8月3日
 *      Author: Royic
 */

#include "./inc/myUart.h"

#include 

UART_Handle huart1;

char Uart_Rx_Buffer[Uart_Rx_Buffer_Size] = {
     0};

void Uart_TxCallback_Func(UART_Handle handle, void *buf, size_t count)
{
     

}

void Uart_RxCallback_Func(UART_Handle handle, void *buf, size_t count)
{
     
	UART_read(handle, Uart_Rx_Buffer, Uart_Rx_Buffer_Size);
}

void My_Uart_Init(UART_Handle *huart, uint_least8_t index, uint32_t BaudRate)
{
     
	UART_Params uartParams;
	// Initialize the UART driver.  UART_init() must be called before
	// calling any other UART APIs.
	UART_init();
	// Create a UART with data processing off.
	UART_Params_init(&uartParams);
	uartParams.readMode = UART_MODE_CALLBACK;
//	uartParams.writeMode = UART_MODE_CALLBACK;
	uartParams.writeMode = UART_MODE_BLOCKING;
	uartParams.readCallback = Uart_RxCallback_Func;
	uartParams.writeCallback = Uart_TxCallback_Func;
	uartParams.writeDataMode = UART_DATA_TEXT;
	uartParams.readDataMode = UART_DATA_TEXT;
	uartParams.readReturnMode = UART_RETURN_NEWLINE;
	uartParams.readEcho = UART_ECHO_OFF;
	uartParams.baudRate = BaudRate;
	// Open an instance of the UART drivers
	*huart = UART_open(index, &uartParams);
	if (*huart == NULL)
	{
     
	    // UART_open() failed
	    while (1);
	}
	UART_read(*huart, Uart_Rx_Buffer, Uart_Rx_Buffer_Size);
}

#include 
#include 
#include 
void UART_printf(UART_Handle handle, const char *format,...)
{
     
    uint32_t length;
    va_list args;
    char TxBuffer[Uart_Tx_Buffer_Size] = {
     0};

    va_start(args, format);
    length = vsnprintf((char*)TxBuffer, sizeof(TxBuffer)+1, (char*)format, args);
    va_end(args);

    UART_write(handle, TxBuffer, length);
}

myUart.h

/*
 * myUart.h
 *
 *  Created on: 2021年8月3日
 *      Author: Royic
 */

#ifndef INC_MYUART_H_
#define INC_MYUART_H_

#include "./inc/main.h"

// Import the UART driver definitions
#include 

#define Uart_Rx_Buffer_Size 32
#define Uart_Tx_Buffer_Size 32

extern char Uart_Rx_Buffer[Uart_Rx_Buffer_Size];

void My_Uart_Init(UART_Handle *huart, uint_least8_t index, uint32_t BaudRate);
void UART_printf(UART_Handle handle, const char *format,...);

//Example
//My_Uart_Init(&huart1, USB_UART, 115200);
//UART_write(huart1, "OK\r\n", 5);

extern UART_Handle huart1;

#endif /* INC_MYUART_H_ */

myTask.c

/*
 * myTask.c
 *
 *  Created on: 2021年8月2日
 *      Author: Royic
 */

/* POSIX Header files */
#include 

/* RTOS header files */
#include 

#include "./inc/myTask.h"

/* Driver Header files */
#include 

void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize)
{
     
    pthread_t           thread;
    pthread_attr_t      attrs;
    struct sched_param  priParam;
    int                 retc;

    /* Initialize the attributes structure with default values */
    pthread_attr_init(&attrs);

    /* Set priority, detach state, and stack size attributes */
    priParam.sched_priority = priority;
    retc = pthread_attr_setschedparam(&attrs, &priParam);
    retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
    retc |= pthread_attr_setstacksize(&attrs, stacksize);
    if (retc != 0)
    {
     
        /* failed to set attributes */
        while (1)
        {
     

        }
    }

    retc = pthread_create(&thread, &attrs, startroutine, NULL);
    if (retc != 0)
    {
     
        /* pthread_create() failed */
        while (1)
        {
     

        }
    }
}

void *LED_Task(void *arg0)
{
     
	while(1)
	{
     
		GPIO_toggle(LED1);
		sleep(1);
	}
}


myTask.h

/*
 * myTask.h
 *
 *  Created on: 2021年8月2日
 *      Author: Royic
 */

#ifndef INC_MYTASK_H_
#define INC_MYTASK_H_

#include "./inc/main.h"

void *mainThread(void *arg0);
void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize);

void *LED_Task(void *arg0);

#endif /* INC_MYTASK_H_ */

main.c

/*
 *  ======== main_tirtos.c ========
 */

#include "./inc/main.h"

/* POSIX Header files */
#include 

/* RTOS header files */
#include 

/* Driver configuration */
#include 
#include 

#include "./inc/myTask.h"
#include "./inc/myUart.h"

/*
 *  ======== main ========
 */
int main(void)
{
     
    /* Call driver init functions */
    Board_init();
    GPIO_init();

    My_Task_Init(mainThread, 1, 1024);

    BIOS_start();

    return (0);
}

/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
     

	My_Task_Init(LED_Task, 1, 1024);
	My_Uart_Init(&huart1, USB_UART, 115200);

    while(1)
    {
     
    	UART_printf(huart1, "MSP432P401R in 115200\r\n");
    	usleep(1000);
    }
}

main.h

/*
 * main.h
 *
 *  Created on: 2021年8月2日
 *      Author: Royic
 */

#ifndef INC_MAIN_H_
#define INC_MAIN_H_

/* For usleep() */
#include 
#include 
#include 

/* Driver configuration */
#include "ti_drivers_config.h"

#endif /* INC_MAIN_H_ */

你可能感兴趣的:(MSP432P401R,单片机,串口通信,c语言,mcu,嵌入式硬件)