一、硬件连接确认
串口选择开发板默认调试串口为 LPUART0,通过板载调试器连接USB虚拟串口。
查看原理图确认目标串口引脚。


PC端接入开发板J21,设备管理器会弹出端口。

二、核心代码实现
串口初始化代码
void BOARD_InitPins(void)
{
/* PORT0: Peripheral clock is enabled */
CLOCK_EnableClock(kCLOCK_GatePORT0);
/* LPUART0 peripheral is released from reset */
RESET_ReleasePeripheralReset(kLPUART0_RST_SHIFT_RSTn);
/* PORT0 peripheral is released from reset */
RESET_ReleasePeripheralReset(kPORT0_RST_SHIFT_RSTn);
const port_pin_config_t port0_2_pin78_config = {/* Internal pull-up resistor is enabled */
kPORT_PullUp,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as LPUART0_RXD */
kPORT_MuxAlt2,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT0_2 (pin 78) is configured as LPUART0_RXD */
PORT_SetPinConfig(PORT0, 2U, &port0_2_pin78_config);
const port_pin_config_t port0_3_pin79_config = {/* Internal pull-up resistor is enabled */
kPORT_PullUp,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as LPUART0_TXD */
kPORT_MuxAlt2,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT0_3 (pin 79) is configured as LPUART0_TXD */
PORT_SetPinConfig(PORT0, 3U, &port0_3_pin79_config);
}
status_t DbgConsole_Init(uint8_t instance, uint32_t baudRate, serial_port_type_t device, uint32_t clkSrcFreq)
{
hal_uart_config_t usrtConfig;
if (kSerialPort_Uart != device)
{
return kStatus_Fail;
}
/* Set debug console to initialized to avoid duplicated initialized operation. */
s_debugConsole.serial_port_type = device;
usrtConfig.srcClock_Hz = clkSrcFreq;
usrtConfig.baudRate_Bps = baudRate;
usrtConfig.parityMode = kHAL_UartParityDisabled;
usrtConfig.stopBitCount = kHAL_UartOneStopBit;
usrtConfig.enableRx = 1U;
usrtConfig.enableTx = 1U;
usrtConfig.enableRxRTS = 0U;
usrtConfig.enableTxCTS = 0U;
usrtConfig.instance = instance;
#if (defined(HAL_UART_ADAPTER_FIFO) && (HAL_UART_ADAPTER_FIFO > 0u))
usrtConfig.txFifoWatermark = 0U;
usrtConfig.rxFifoWatermark = 0U;
#endif
/* Enable clock and initial UART module follow user configure structure. */
(void)HAL_UartInit((hal_uart_handle_t)&s_debugConsole.uartHandleBuffer[0], &usrtConfig);
/* Set the function pointer for send and receive for this kind of device. */
s_debugConsole.putChar = HAL_UartSendBlocking;
s_debugConsole.getChar = HAL_UartReceiveBlocking;
return kStatus_Success;
}
int main(void)
{
char ch;
/* Init board hardware. */
BOARD_InitPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
PRINTF("hello eeworld.\r\n");
while (1)
{
ch = GETCHAR();
PUTCHAR(ch);
}
}
Building target: frdmmcxa156_hello_world.axf
Invoking: MCU Linker
arm-none-eabi-gcc -nostdlib -Xlinker -no-warn-rwx-segments -Xlinker -Map="frdmmcxa156_hello_world.map" -Xlinker --gc-sections -Xlinker -print-memory-usage -Xlinker --sort-section=alignment -Xlinker --cref -mcpu=cortex-m33 -mfpu=fpv5-sp-d16 -mfloat-abi=hard -mthumb -T frdmmcxa156_hello_world_Debug.ld -o "frdmmcxa156_hello_world.axf" ./utilities/fsl_assert.o ./utilities/fsl_debug_console.o ./utilities/fsl_memcpy.o ./utilities/fsl_str.o ./startup/startup_mcxa156.o ./source/hello_world.o ./source/semihost_hardfault.o ./drivers/fsl_clock.o ./drivers/fsl_common.o ./drivers/fsl_common_arm.o ./drivers/fsl_gpio.o ./drivers/fsl_lpuart.o ./drivers/fsl_reset.o ./drivers/fsl_spc.o ./device/system_MCXA156.o ./component/uart/fsl_adapter_lpuart.o ./component/lists/fsl_component_generic_list.o ./board/board.o ./board/clock_config.o ./board/pin_mux.o
Memory region Used Size Region Size %age Used
PROGRAM_FLASH: 11820 B 1 MB 1.13%
SRAM: 8444 B 120 KB 6.87%
SRAMX: 0 GB 12 KB 0.00%
Finished building target: frdmmcxa156_hello_world.axf
Performing post-build steps
arm-none-eabi-size 'frdmmcxa156_hello_world.axf'; arm-none-eabi-objcopy -v -O binary 'frdmmcxa156_hello_world.axf' 'frdmmcxa156_hello_world.bin'
text data bss dec hex filename
11812 8 8436 20256 4f20 frdmmcxa156_hello_world.axf
copy from `frdmmcxa156_hello_world.axf' [elf32-littlearm] to `frdmmcxa156_hello_world.bin' [binary]
22:04:59 Build Finished. 0 errors, 0 warnings. (took 10s.918ms)三.配置

引脚功能

四.接收效果

我要赚赏金
