?
?
?
?串口初始化完成后增加两行代码:
? ? DEMO_LPUART->MODIR |= LPUART_MODIR_TXRTSE_MASK; ? ? DEMO_LPUART->MODIR |= LPUART_MODIR_TXRTSPOL_MASK;
即可使用RTS引脚直接控制RS485收发器收发使能。
/*
* Copyright 2017 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_lpuart.h"
#include "fsl_debug_console.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define DEMO_LPUART LPUART2
#define DEMO_LPUART_CLK_FREQ BOARD_DebugConsoleSrcFreq()
#define DELAY_TIME 100000U
#define TRANSFER_SIZE 256U /*! Transfer dataSize */
#define TRANSFER_BAUDRATE 115200U /*! Transfer baudrate - 115200 */
/*******************************************************************************
* Prototypes
******************************************************************************/
/* LPUART user callback */
void LPUART_UserCallback(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData);
/*******************************************************************************
* Variables
******************************************************************************/
uint8_t transferRxData[TRANSFER_SIZE] = {0U};
uint8_t transferTxData[TRANSFER_SIZE] = {0U};
lpuart_handle_t g_lpuartHandle;
volatile bool isTransferCompleted = false;
/*******************************************************************************
* Code
******************************************************************************/
/* LPUART user callback */
void LPUART_UserCallback(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
{
if (kStatus_LPUART_TxIdle == status)
{
isTransferCompleted = true;
}
}
/*!
* @brief Main function
*/
int main(void)
{
uint32_t i = 0U, errCount = 0U;
status_t status = 0;
lpuart_config_t config;
lpuart_transfer_t sendXfer;
BOARD_ConfigMPU();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
PRINTF("This is LPUART hardware flow control example on one board.\r\n");
PRINTF("This example will send data to itself and will use hardware flow control to avoid the overflow.\r\n");
PRINTF("Please make sure you make the correct line connection. Basically, the connection is: \r\n");
PRINTF(" LPUART_TX -- LPUART_RX \r\n");
PRINTF(" LPUART_RTS -- LPUART_CTS \r\n");
/*
* config.baudRate_Bps = 115200U;
* config.parityMode = kLPUART_ParityDisabled;
* config.stopBitCount = kLPUART_OneStopBit;
* config.txFifoWatermark = 0;
* config.rxFifoWatermark = 0;
* config.enableTx = false;
* config.enableRx = false;
*/
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
config.enableTx = true;
config.enableRx = true;
//config.enableRxRTS = true;
//config.enableTxCTS = true;
LPUART_Init(DEMO_LPUART, &config, DEMO_LPUART_CLK_FREQ);
DEMO_LPUART->MODIR |= LPUART_MODIR_TXRTSE_MASK;
DEMO_LPUART->MODIR |= LPUART_MODIR_TXRTSPOL_MASK;
LPUART_TransferCreateHandle(DEMO_LPUART, &g_lpuartHandle, LPUART_UserCallback, NULL);
while(1){
/* Set up the transfer data */
for (i = 0U; i < TRANSFER_SIZE; i++)
{
transferTxData[i] = i % 256U;
transferRxData[i] = 0U;
}
sendXfer.data = (uint8_t *)transferTxData;
sendXfer.dataSize = TRANSFER_SIZE;
LPUART_TransferSendNonBlocking(DEMO_LPUART, &g_lpuartHandle, &sendXfer);
/* Delay for some time to let the RTS pin dessart. */
for (i = 0U; i < DELAY_TIME; i++)
{
__NOP();
}
}
/* Set up the transfer data */
for (i = 0U; i < TRANSFER_SIZE; i++)
{
transferTxData[i] = i % 256U;
transferRxData[i] = 0U;
}
sendXfer.data = (uint8_t *)transferTxData;
sendXfer.dataSize = TRANSFER_SIZE;
LPUART_TransferSendNonBlocking(DEMO_LPUART, &g_lpuartHandle, &sendXfer);
/* Delay for some time to let the RTS pin dessart. */
for (i = 0U; i < DELAY_TIME; i++)
{
__NOP();
}
status = LPUART_ReadBlocking(DEMO_LPUART, transferRxData, TRANSFER_SIZE);
if (kStatus_Success != status)
{
PRINTF(" Error occurred when LPUART receiving data.\r\n");
}
/* Wait for the transmit complete. */
while (!isTransferCompleted)
{
}
for (i = 0U; i < TRANSFER_SIZE; i++)
{
if (transferTxData[i] != transferRxData[i])
{
errCount++;
}
}
if (errCount)
{
PRINTF("Data not matched! Transfer error.\r\n");
}
else
{
PRINTF("Data matched! Transfer successfully.\r\n");
}
/* Deinit the LPUART. */
LPUART_Deinit(DEMO_LPUART);
while (1)
{
}
}
?
|