rt-thread/bsp/swm320/libraries/SWM320_StdPeriph_Driver/SWM320_i2c.c

220 lines
9.3 KiB
C
Raw Normal View History

2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SWM320_i2c.c
* : SWM320单片机的I2C串行接口功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
2021-05-06 10:42:41 +08:00
* :
2021-09-13 21:04:42 +08:00
* : V1.1.0 20171025
* :
2021-02-18 13:29:12 +08:00
*
*
*******************************************************************************************************************************************
* @attention
*
2021-09-13 21:04:42 +08:00
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIES AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIEE. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIES ARISING FROM THE CONTENT
2021-02-18 13:29:12 +08:00
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include "SWM320.h"
#include "SWM320_i2c.h"
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : I2C_Init()
* : I2C初始化
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* I2C_InitStructure * initStruct I2C相关设定值的结构体
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitStructure *initStruct)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
switch ((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_I2C0_Pos);
break;
case ((uint32_t)I2C1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_I2C1_Pos);
break;
}
I2C_Close(I2Cx); //一些关键寄存器只能在I2C关闭时设置
if (initStruct->Master == 1)
{
I2Cx->CLKDIV = SystemCoreClock / 5 / initStruct->MstClk;
I2Cx->MSTCMD = (I2Cx->MSTCMD & (~I2C_MSTCMD_IF_Msk)) | (1 << I2C_MSTCMD_IF_Pos); //使能中断之前先清除中断标志
I2Cx->CTRL &= ~I2C_CTRL_MSTIE_Msk;
I2Cx->CTRL |= (initStruct->MstIEn << I2C_CTRL_MSTIE_Pos);
switch ((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
if (initStruct->MstIEn)
{
NVIC_EnableIRQ(I2C0_IRQn);
}
else
{
NVIC_DisableIRQ(I2C0_IRQn);
}
break;
case ((uint32_t)I2C1):
if (initStruct->MstIEn)
{
NVIC_EnableIRQ(I2C1_IRQn);
}
else
{
NVIC_DisableIRQ(I2C1_IRQn);
}
break;
}
}
else
{
I2Cx->SLVCR |= (1 << I2C_SLVCR_SLAVE_Pos);
I2Cx->SLVCR &= ~(I2C_SLVCR_ADDR7b_Msk | I2C_SLVCR_ADDR_Msk);
I2Cx->SLVCR |= (1 << I2C_SLVCR_ACK_Pos) |
(initStruct->Addr7b << I2C_SLVCR_ADDR7b_Pos) |
(initStruct->SlvAddr << I2C_SLVCR_ADDR_Pos);
I2Cx->SLVIF = I2C_SLVIF_RXEND_Msk | I2C_SLVIF_TXEND_Msk | I2C_SLVIF_STADET_Msk | I2C_SLVIF_STODET_Msk; //清中断标志
I2Cx->SLVCR &= ~(I2C_SLVCR_IM_RXEND_Msk | I2C_SLVCR_IM_TXEND_Msk | I2C_SLVCR_IM_STADET_Msk | I2C_SLVCR_IM_STODET_Msk |
I2C_SLVCR_IM_RDREQ_Msk | I2C_SLVCR_IM_WRREQ_Msk);
I2Cx->SLVCR |= ((initStruct->SlvRxEndIEn ? 0 : 1) << I2C_SLVCR_IM_RXEND_Pos) |
((initStruct->SlvTxEndIEn ? 0 : 1) << I2C_SLVCR_IM_TXEND_Pos) |
((initStruct->SlvSTADetIEn ? 0 : 1) << I2C_SLVCR_IM_STADET_Pos) |
((initStruct->SlvSTODetIEn ? 0 : 1) << I2C_SLVCR_IM_STODET_Pos) |
((initStruct->SlvRdReqIEn ? 0 : 1) << I2C_SLVCR_IM_RDREQ_Pos) |
((initStruct->SlvWrReqIEn ? 0 : 1) << I2C_SLVCR_IM_WRREQ_Pos);
switch ((uint32_t)I2Cx)
{
case ((uint32_t)I2C0):
if (initStruct->SlvRxEndIEn | initStruct->SlvTxEndIEn | initStruct->SlvSTADetIEn |
initStruct->SlvSTODetIEn | initStruct->SlvRdReqIEn | initStruct->SlvWrReqIEn)
{
NVIC_EnableIRQ(I2C0_IRQn);
}
else
{
NVIC_DisableIRQ(I2C0_IRQn);
}
break;
case ((uint32_t)I2C1):
if (initStruct->SlvRxEndIEn | initStruct->SlvTxEndIEn | initStruct->SlvSTADetIEn |
initStruct->SlvSTODetIEn | initStruct->SlvRdReqIEn | initStruct->SlvWrReqIEn)
{
NVIC_EnableIRQ(I2C1_IRQn);
}
else
{
NVIC_DisableIRQ(I2C1_IRQn);
}
break;
}
}
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : I2C_Open()
* : I2C打开
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void I2C_Open(I2C_TypeDef *I2Cx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
I2Cx->CTRL |= (0x01 << I2C_CTRL_EN_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : I2C_Close()
* : I2C关闭
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void I2C_Close(I2C_TypeDef *I2Cx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
I2Cx->CTRL &= ~I2C_CTRL_EN_Msk;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : I2C_Start()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t addr
* : uint8_t 1 ACK 0 NACK
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
uint8_t I2C_Start(I2C_TypeDef *I2Cx, uint8_t addr)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
I2Cx->MSTDAT = addr;
I2Cx->MSTCMD = (1 << I2C_MSTCMD_STA_Pos) |
(1 << I2C_MSTCMD_WR_Pos); //发送起始位和从机地址
while (I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk)
__NOP(); //等待发送完成
return (I2Cx->MSTCMD & I2C_MSTCMD_RXACK_Msk) ? 0 : 1;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : I2C_Stop()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void I2C_Stop(I2C_TypeDef *I2Cx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
I2Cx->MSTCMD = (1 << I2C_MSTCMD_STO_Pos);
while (I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk)
__NOP(); //等待发送完成
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : I2C_Write()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t data
* : uint8_t 1 ACK 0 NACK
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
uint8_t I2C_Write(I2C_TypeDef *I2Cx, uint8_t data)
{
I2Cx->MSTDAT = data;
I2Cx->MSTCMD = (1 << I2C_MSTCMD_WR_Pos);
while (I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk)
__NOP(); //等待发送完成
return (I2Cx->MSTCMD & I2C_MSTCMD_RXACK_Msk) ? 0 : 1;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : I2C_Read()
* :
* : I2C_TypeDef * I2Cx I2CI2C0I2C1
* uint8_t ack 1 ACK 0 NACK
* : uint8_t
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
uint8_t I2C_Read(I2C_TypeDef *I2Cx, uint8_t ack)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
I2Cx->MSTCMD = (1 << I2C_MSTCMD_RD_Pos) |
((ack ? 0 : 1) << I2C_MSTCMD_ACK_Pos);
while (I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk)
__NOP(); //等待接收完成
2021-02-18 13:29:12 +08:00
return I2Cx->MSTDAT;
}