blog
blog copied to clipboard
[S32K344] Serial Port (LPUART)
1. Overview
演示普通的UART demo可以参考: https://youtu.be/_mwpw3nDrL8 使用LPUART_Flexio demo可以参考: https://youtu.be/VgoZpgZNC0A
S32K344 LPUART的使用。低功耗通用异步收发器(Low Power Universal Asynchronous Receiver/Transmitter, LPUART)支持带有DMA 接口功能的基本UART,和x4 到x32 的过采样波特率,支持LIN 主从操作。该模块在Stop 和VLPS 模式提供的时钟保持启用时,仍可保持功能。 在S32K344 中有如下15个LPUART 模块:

2. 特性
2.1 串口特点
- 全双工,标准的非归零(non-return-to-zero, NRZ)格式
- 可编程的波特率(13 位数模转换器)与可配置的比率从x4 到x32 的过采样
- 发送和接受波特率可以异步操作到总线时钟:
- 波特率可以配置独立的总线时钟频率
- 支持在Stop 模式下操作
- 中断,DMA 或者拉起操作
- 发送数据寄存器为空,发送完成
- 接收数据寄存器满
- 接收溢出,奇偶校验错误,帧错误,噪声干扰
- 闲置接收器检测
- 接受引脚边缘有效
- 支持LIN的中断检测
- 接收数据匹配
- 硬件奇偶校验生成和检查
- 可编程的7-bit,8-bit,9-bit 或10-bit 的字符长度
- 可编程的1-bit 或者2-bit 停止位
- 3 种接收器唤醒方法:
- 闲置线唤醒
- 地址标记唤醒
- 接收数据匹配
- 自动地址匹配,减少中断服务程序(ISR)开销
- 地址标志匹配
- 闲置线地址匹配
- 地址匹配开始和结束
- 可选的13-bit 终止字符生成/11-bit 终止字符检测
- 可配置的空闲长度检测,支持1, 2, 4, 8, 16, 32, 64 或128 空闲字符
- 可选的发送器输出和接收器输入极性
- 硬件流控制支持RTS(request to send)和CTS(clear to send)信号
- 可选的4 归零变换(return-to-zero-inverted, RZI)格式,可编程脉宽
- 独立的FIFO 收发结构:
- 可独立配置水印的收发请求
- 如果接收FIFO 不是空的,在可配置的闲置字符长度后,可以选择让接收器声明请求
2.2 LPUART运行模式
S32K344运行模式 | 工作状态 |
---|---|
Run Mode | 正常工作 |
Stop Mode | 时钟使能则保留功能(可产生中断) |
Wait Mode | 等待模式(DOZEEN 位置1时) |
Debug Mode | 保留功能 |
2.3 收发原理
接收:

发送:

2.4 寄存器映射
参考 S32K344 Reference Manual,Page - 4530
3. Demo
3.1 导入工程

以下是介绍:
1. Example Description
This application demonstrates the usage of a subset of Real Time Drivers that are included in this code drop. It is not part of the production code deliverables.
This is a simple application which shows the usage of Uart driver on S32DS for S32K344 derivative.
1.1 The application software functionality
- Port_Init
Initialize the pins used by the application using the Port driver.
- Mcu_Init, Mcu_InitClock, Mcu_SetMode
Initialize the clock sources for project.
- Mcl_Init
Initialize the Mcl module for Flexio common part.
- Platform_Init
Platform_InstallIrqHandler
Install the ISR for the Uart
- Uart_Init
Initialize the Uart driver
- Uart_Deinit
Deinitialize the Uart driver
- Uart_AsyncSend
Send Uart data
- Uart_AsyncReceive
Receive Uart data
- Uart_GetStatus
Get status of Uart
- Uart_Deinit
Uninitialize the Uart driver
- Mcl_Deinit
Uninitialize the Mcl driver
1.2 The application description
- The first welcome message is sent via UART: "Hello, This message is sent via Uart!"
- The second welcome message is sent via UART: "Have a nice day!"
- User need to check return status of Send_Data() function for verifying example.
2. Installation steps
2.1 Hardware installation
2.1.1 Hardware preparation
- S32K3X4EVB-Q257 47827 PCB RevX1 SCH RevA or S32K3XXEVB-Q172 51972 PCB RevA SCH RevA
- 1 Power Adapters 12V
- 1 microUSB cable
- 1 Personal Computer
2.2.2 Connections
- It uses Lpuart instance 3, comunicates with Flexio instance 0 channel 0 for Tx and channel 1 for Rx
- With S32K3X4EVB-Q257 47827 PCB RevX1 SCH RevA: Connect PTA0(J352.22) - PTD3(J352.10) and PTA1(J352.19) - PTD2(J352.7)
- With S32K3XXEVB-Q172 51972 PCB RevA SCH RevA: Connect PTA0(J2.22) - PTD3(J2.10) and PTA1(J2.19) - PTD2(J2.7)
2.2.3 Debugger
- EVB is debugged through OpenSDA USB connection
2.2 Software installation
2.2.1 Importing the S32 Design Studio project
Make sure that all RTD plugins are already installed in the S32DS plugins directory
3. How to run
3.1 Importing the project into the workspace
After opening S32 Design Studio, go to "File -> New -> S32DS Project From Example" and select "Uart_Example_S32K344". Then click on "Finish".
The project should now be copied into you current workspace.
Wait for the S32 Configuration was initialized and ready.
3.2 Generating the S32 configuration
The example will run without an active configuration, however if any changes are required, a configuration needs to be generated.
The initial configuration will have the same settings as the default example settings.
Right click on the current project, then click "S32 Configuration Tool" choose "Open Clocks".
Pay attention to any error and warning message. If any configured value is invalid, they will be shown for user.
Make the desired changes (if any) then click "Update Code".
3.3 Building the project
Select the configuration to be built: RAM (Debug_RAM), or FLASH (Debug_FLASH) by left clicking on the downward arrow corresponding to the build button in eclipse.
Use Project > Build to build the project.
Wait for the build action to be completed before continuing to the next step. Check the compiler console for error messages; upon completion, the *.elf binary file should be created.
3.4 Running the project
Go to Run and select Debug Configurations. There will be a debug configuration for this project:
Configuration Name Description
------------------------------- -----------------------
Uart_Example_S32K344_Debug_RAM_PNE Debug the RAM configuration using PEmicro probe
Uart_Example_S32K344_Debug_FLASH_PNE Debug the FLASH configuration using PEmicro probe
Select the desired debug configuration and click on Launch. Now the perspective will change to the Debug Perspective.
Use the controls to control the program flow.
3.2 配置工程
S32DS配置









到这里就证明配置都是正确的了。
S32DS配置
根据自己的原理图进行配置。我这里使用的是UART3,通过MAX2323,进行了TTL转RS232电平,需要一个RS232转USB接口。



根据例程进行修改,把串口改为LPUART3。
配置内部internal channel是0了,我们来做一个函数:
#define UART_LPUART_INTERNAL_CHANNEL 0
Std_ReturnType Uart3_Send_Data(const uint8* pBuffer, uint32 length)
{
Std_ReturnType Uart3_Send_Data(const uint8* pBuffer, uint32 length)
{
volatile Std_ReturnType T_Uart_Status;
volatile Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
volatile Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
uint32 T_bytesRemaining;
uint32 T_timeout = 0xFFFFFF;
/* Uart_AsyncSend transmit data */
T_Uart_Status = Uart_AsyncSend(UART_LPUART_INTERNAL_CHANNEL, pBuffer, length);
if (E_OK != T_Uart_Status)
{
return E_NOT_OK;
}
/* Check for no on-going transmission */
do
{
Uart_TransmitStatus = Uart_GetStatus(UART_LPUART_INTERNAL_CHANNEL, &T_bytesRemaining, UART_SEND);
} while (UART_STATUS_NO_ERROR != Uart_TransmitStatus && 0 < T_timeout--);
T_timeout = 0xFFFFFF;
// do
// {
// Uart_ReceiveStatus = Uart_GetStatus(UART_LPUART_INTERNAL_CHANNEL, &T_bytesRemaining, UART_RECEIVE);
// } while (UART_STATUS_NO_ERROR != Uart_ReceiveStatus && 0 < T_timeout--);
// if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus) || (UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
// {
// return E_NOT_OK;
// }
//
// if(User_Str_Cmp(pBuffer, (const uint8 *)Rx_Buffer, length) == FALSE)
// {
// return E_NOT_OK;
// }
return E_OK;
}
编译,测试