1、简介
LPI2C可以用来实现2线或4线I2C串行总线。
2、信号方向
Signal |
Name |
2-Wire Scheme | 4-Wire Scheme |
---|---|---|---|
SCL | LPI2C clock line | SCL | In 4-wire mode, this is the SCL input pin |
SDA | LPI2C data line | SDA | In 4-wire mode, this is the SDA input pin |
HREQ | Host request | if host request is asserted and the I2C bus is idle, then it will initiate an LPI2C master transfer | |
SCLS | Secondary I2C clock line | Not Used | In 4-wire mode, this is the SCLS output pin. If LPI2C master/slave are configured to use separate pins, then this the LPI2C slave SCL pin |
SDAS | Secondary I2C data line | Not Used | In 4-wire mode, this is the SDAS output pin. If LPI2C master/slave are configured to use separate pins, then this the LPI2C slave SDA pin |
2.1、2-wire接法
2.2、4-wire接法
3、寄存器
与SPI极度相似
3.1、Version ID Register (VERID)
版本id寄存器
3.2、Parameter Register (PARAM)
主机接收和发送FIFO大小
3.3、Master Control Register (MCR)
0 MEN |
主机使能 |
1 RST |
软件复位 |
2 DOZEN |
Doze模式使能 |
3 DEGEN |
调试模式使能 |
8 RTF |
复位发送FIFO |
9 RRF |
复位接收FIFO |
3.4、 Master Status Register (MSR)
0 TDF |
发送数据标志 0:没有请求发送数据 1:请求发送数据 |
1 RDF |
接收数据标志 0:没有准备好的接收数据 1:接收数据就绪 |
8 EPF |
结束包标志 |
9 SDF |
停止检测标志 |
10 NDF |
NACK检测标志 |
11 ALF |
仲裁标志 |
12 FEF |
FIFO错误标志 |
13 PLTF |
引脚低电平超时标志 |
14 DMF |
数据匹配标志 |
24 BBF |
主机忙标志 |
25 BBF |
总线忙标志 |
3.5、 Master Interrupt Enable Register (MIER)
0 TDIE |
发送数据中断 |
1 RDIE |
接收数据中断 |
8 EPIE |
结束包中断 |
9 SDIE |
停止检测中断 |
10 NDIE |
NACK检测中断 |
11 ALIE |
仲裁中断 |
12 FEIE |
FIFO错误中断 |
13 PLTIE |
引脚低电平超时中断 |
14 DMIE |
数据匹配中断 |
3.6、 Master DMA Enable Register (MDER)
接收数据DMA使能
3.7、Master Configuration Register 0 (MCFGR0)
0 HREN |
主机请求使能 |
1 HRPOL |
配置主机请求引脚电平 |
2 HRSEL |
主机请求选择 |
8 CIRFIFO |
循环FIFO使能 |
9 RDMO |
只接收数据匹配 |
3.8、 Master Configuration Register 1 (MCFGR1)
0-2 PRESCALE |
分频设置 |
8 AUTOSTOP |
自动停止条件 |
9 IGNACK |
设置是否接收ACK、NACK |
10 TIMECFG |
超时操作 |
16-18 MATCFG |
匹配设置 |
24-26 PINCFG |
引脚设置 |
3.9、Master Configuration Register 2 (MCFGR2)
0-11 BUSIDLE |
总线idle超时 |
16-19 FILTSCL |
故障过滤SCL |
24-27 FILTSDA |
故障过滤SDA |
3.10、 Master Configuration Register 3 (MCFGR3)
8-19 PINLOW |
低电平超时时间 |
3.11、Master Data Match Register (MDMR)
匹配0/1的值
3.12、Master Clock Configuration Register 0 (MCCR0/1)
0-5 CLKLO |
时钟低电平周期 |
0-5 CLKHI |
时钟高电平周期 |
16-21 SETHOLD |
启动延时 |
24-29 DATAVD |
数据有效延时 |
3.13、Master FIFO Control Register (MFCR)
接收和发送的FIFO 水印配置
3.14、Master FIFO Status Register (MFSR)
接收和发送的FIFO计数
3.15、Master Transmit Data Register (MTDR)
0-7 DATA |
发送数据 |
8-10 CMD |
数据命令 |
3.16、Master Receive Data Register (MRDR)
0-7 DATA |
接收数据 |
14 RXEMPTY |
接收FIFO是否为空 |
3.17、Slave Control Register (SCR)
3.18、Slave Status Register (SSR)
3.19、Slave Interrupt Enable Register (SIER)
3.20、Slave Interrupt Enable Register (SIER)
3.21、Slave Configuration Register 1 (SCFGR1)
3.22、Slave Configuration Register 2 (SCFGR2)
3.23、Slave Address Match Register (SAMR)
3.24、Slave Address Status Register (SASR)
3.25、Slave Transmit ACK Register (STAR)
3.26、Slave Transmit Data Register (STDR)
3.27、Slave Receive Data Register (SRDR)
从机与主机基本一样的内容
4、代码搬运工
void LPI2C_init(void)
{
/* Clk src: SIRCDIV2_CLK
* Enable clock for LPI2C0 */
PCC->PCCn[PCC_LPI2C0_INDEX] |= PCC_PCCn_PCS(2)
| PCC_PCCn_CGC_MASK;
/* Prescale = 4
* Ignore NACK */
LPI2C0->MCFGR1 = LPI2C_MCFGR1_PRESCALE(2)|LPI2C_MCFGR1_IGNACK_MASK;
/* SCL_freq = Input_freq / (2^PRESCALER * (CLKLO + CLKHI + 2)) */
LPI2C0->MCCR0 = LPI2C_MCCR0_CLKLO(18)
| LPI2C_MCCR0_CLKHI(6)
| LPI2C_MCCR0_SETHOLD(6)
| LPI2C_MCCR0_DATAVD(3);
/* Transmitter Water mark set to 0
* Receiver Water mark set to 3 */
LPI2C0->MFCR = LPI2C_MFCR_TXWATER(0)
|LPI2C_MFCR_RXWATER(3);
/* Enable LPI2C as master */
LPI2C0->MCR |= LPI2C_MCR_MEN_MASK
| LPI2C_MCR_DBGEN_MASK;
}
void LPI2C_Transmit (void)
{
LPI2C0->MTDR = (0x05<<8)|((0x1E<<1)|0);
}