Tx Polling
该示例通过使用 I2S 外设以轮询的方式实现数据发送功能。
外接逻辑分析仪可以看到发送数据的波形。
环境需求
该示例支持以下开发套件:
Hardware Platforms |
Board Name |
---|---|
RTL87x2G HDK |
RTL87x2G EVB |
更多信息请参考快速入门。
硬件连线
将EVB上的P3_2,P3_3和P4_0 连接逻辑分析仪。 其中P3_2为LRCK,P3_3为BCLK,P4_0为DATA。
编译和下载
该示例的工程路径如下:
Project file: samples\peripheral\i2s\tx_polling\proj\rtl87x2g\mdk
Project file: samples\peripheral\i2s\tx_polling\proj\rtl87x2g\gcc
请按照以下步骤操作构建并运行该示例:
打开工程文件。
按照 快速入门 中 编译APP Image 给出的步骤构建目标文件。
编译成功后,在路径
mdk\bin
或gcc\bin
下会生成 app binapp_MP_xxx.bin
文件。按下复位按键,开始运行。
测试验证
准备阶段
将EVB上的P3_2,P3_3和P4_0 连接逻辑分析仪。
测试阶段
在逻辑分析仪观察I2S输出波形。I2S输出波形如下图所示。

I2S output 波形
代码介绍
该章节分为以下几个部分:
源码路径
工程路径:
sdk\samples\peripheral\i2s\tx_polling\proj
源码路径:
sdk\samples\peripheral\i2s\tx_polling\src
该工程的工程文件代码结构如下:
└── Project: tx_polling
└── secure_only_app
└── Device includes startup code
├── startup_rtl.c
└── system_rtl.c
├── CMSIS includes CMSIS header files
├── CMSE Library Non-secure callable lib
├── Lib includes all binary symbol files that user application is built on
└── rtl87x2g_io.lib
├── Peripheral includes all peripheral drivers and module code used by the application
├── rtl_rcc.c
├── rtl_pinmux.c
└── rtl_i2s.c
└── APP includes the ble_peripheral user application implementation
├── main_ns.c
└── io_i2s.c
初始化
初始化流程包括了 board_i2s_init
和 driver_i2s_init
。
board_i2s_init
中包含了PAD与PINMUX设置。
配置PAD:设置引脚、PINMUX模式、PowerOn、无内部上拉。
配置PINMUX:分配引脚分别为BCLK_SPORT0、LRC_SPORT0、DACDAT_SPORT0功能。
driver_i2s_init
包含了对i2s外设的初始化。
使能PCC时钟。
设置I2S时钟源。
设置
I2S_TxBClockMi
和I2S_TxBClockNi
,确定BCLK和LRCK的频率。设置
I2S_DeviceMode
为主设备模式。设置
I2S_TxChannelType
为双声道模式。设置数据宽度和数据格式。
使能I2S TX模式。
备注
BCLK=I2S_ClockSource*(I2S_BClockNi/I2S_BClockMi)=1024K,LRCK=BCLK/64=16K
RCC_PeriphClockCmd(APB_I2S, APB_I2S_CLOCK, ENABLE);
...
I2S_InitStruct.I2S_ClockSource = I2S_CLK_40M;
I2S_InitStruct.I2S_TxBClockMi = 0x271; /* <!LRCK = 16K */
I2S_InitStruct.I2S_TxBClockNi = 0x10; /* <!BCLK = 1024K */
I2S_InitStruct.I2S_DeviceMode = I2S_DeviceMode_Master;
I2S_InitStruct.I2S_TxChannelType = I2S_Channel_stereo;
I2S_InitStruct.I2S_TxDataWidth = I2S_Width_16Bits;
I2S_InitStruct.I2S_TxDataFormat = I2S_Mode;
I2S_Init(I2S_NUM, &I2S_InitStruct);
I2S_Cmd(I2S_NUM, I2S_MODE_TX, ENABLE);
功能实现
定义发送数据,执行
I2S_SendData()
循环发送数据。
void i2s_senddata(void)
{
uint32_t i = 0x12348800;
while (1)
{
if (I2S_GetTxFIFOFreeLen(I2S_NUM))
{
/* 16bit format, lower half word send first! */
I2S_SendData(I2S_NUM, i++);
}
}
}