Skip to content

Instantly share code, notes, and snippets.

@nickfox-taterli
Created June 28, 2020 16:01
Show Gist options
  • Save nickfox-taterli/50d2b38319662967fee7eed16933b0e4 to your computer and use it in GitHub Desktop.
Save nickfox-taterli/50d2b38319662967fee7eed16933b0e4 to your computer and use it in GitHub Desktop.
IMX RT LPUART 中断+FIFO
#include "board.h"
#include "fsl_lpuart.h"
#include "pin_mux.h"
#include "clock_config.h"
uint8_t txData[] = "Hello,World!";
uint8_t rxData[512];
typedef struct
{
uint8_t *txData;
uint16_t txIndex;
uint16_t txLength;
uint8_t *rxData;
uint16_t rxIndex;
uint16_t rxLength;
} LPUART_DataHandler;
LPUART_DataHandler LPUART1_DataHandler;
void LPUART1_IRQHandler(void)
{
uint8_t count;
if ((kLPUART_TxDataRegEmptyFlag)&LPUART_GetStatusFlags(LPUART1))
{
if (LPUART1_DataHandler.txLength == LPUART1_DataHandler.txIndex)
{
LPUART_DisableInterrupts(LPUART1, kLPUART_TxDataRegEmptyInterruptEnable);
// 全部发送完毕,可以通知用户处理.
__BKPT(0);
}
for (count = 0;
count < MIN(4 - ((LPUART1->WATER >> 8U) & 0x07), LPUART1_DataHandler.txLength - LPUART1_DataHandler.txIndex);
count++)
{
LPUART1->DATA = LPUART1_DataHandler.txData[LPUART1_DataHandler.txIndex++];
}
}
if ((kLPUART_RxDataRegFullFlag)&LPUART_GetStatusFlags(LPUART1))
{
for (count = 0;
count < ((LPUART1->WATER >> 24U) & 0x07) + 1;
count++)
{
LPUART1_DataHandler.rxData[LPUART1_DataHandler.rxIndex] = LPUART1->DATA;
LPUART1_DataHandler.rxIndex++;
LPUART1_DataHandler.rxLength++;
}
}
if ((kLPUART_IdleLineFlag)&LPUART_GetStatusFlags(LPUART1))
{
for (count = 0;
count < ((LPUART1->WATER >> 24U) & 0x07) + 1;
count++)
{
LPUART1_DataHandler.rxData[LPUART1_DataHandler.rxIndex] = LPUART1->DATA;
LPUART1_DataHandler.rxIndex++;
LPUART1_DataHandler.rxLength++;
}
LPUART1_DataHandler.rxIndex = 0;
LPUART_ClearStatusFlags(LPUART1, kLPUART_IdleLineFlag);
// 全部接收完成,可以通知用戶处理.
__BKPT(0);
}
}
void LPUART1_Send(uint8_t *xfer, uint8_t len)
{
LPUART1_DataHandler.txData = xfer;
LPUART1_DataHandler.txIndex = 0;
LPUART1_DataHandler.txLength = len;
LPUART_EnableInterrupts(LPUART1, kLPUART_TxDataRegEmptyInterruptEnable);
}
int main(void)
{
lpuart_config_t config;
BOARD_ConfigMPU();
BOARD_InitPins();
BOARD_BootClockRUN();
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = 115200U;
config.parityMode = kLPUART_ParityDisabled;
config.stopBitCount = kLPUART_OneStopBit;
config.rxFifoWatermark = 0;
config.rxFifoWatermark = 3;
config.enableTx = true;
config.enableRx = true;
LPUART_Init(LPUART1, &config, BOARD_DebugConsoleSrcFreq());
LPUART1_DataHandler.rxData = rxData;
LPUART_EnableInterrupts(LPUART1, kLPUART_RxDataRegFullInterruptEnable);
LPUART_EnableInterrupts(LPUART1, kLPUART_IdleLineInterruptEnable);
EnableIRQ(LPUART1_IRQn);
LPUART1_Send(txData, strlen((const char *)txData));
while (1)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment