151 lines
5.9 KiB
C
151 lines
5.9 KiB
C
|
/******************************************************************************************************************************************
|
|||
|
* <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: SWM320_i2c.c
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: SWM320<EFBFBD><EFBFBD>Ƭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD>нӿڹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD>: http://www.synwit.com.cn/e/tool/gbook/?bid=1
|
|||
|
* ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
|
|||
|
* <EFBFBD>汾<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: V1.1.0 2017<EFBFBD><EFBFBD>10<EFBFBD><EFBFBD>25<EFBFBD><EFBFBD>
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼:
|
|||
|
*
|
|||
|
*
|
|||
|
*******************************************************************************************************************************************
|
|||
|
* @attention
|
|||
|
*
|
|||
|
* 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
|
|||
|
* 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"
|
|||
|
|
|||
|
/******************************************************************************************************************************************
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Init()
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: I2C<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: I2C_TypeDef * I2Cx ָ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Чֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C0<EFBFBD><EFBFBD>I2C1
|
|||
|
* I2C_InitStructure * initStruct <EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>趨ֵ<EFBFBD>Ľṹ<EFBFBD><EFBFBD>
|
|||
|
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
|
|||
|
* ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ģ<EFBFBD><EFBFBD>ֻ<EFBFBD>ܹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
******************************************************************************************************************************************/
|
|||
|
void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitStructure *initStruct)
|
|||
|
{
|
|||
|
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); //һЩ<D2BB>ؼ<EFBFBD><D8BC>Ĵ<EFBFBD><C4B4><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>I2C<32>ر<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
if (initStruct->Master == 1)
|
|||
|
{
|
|||
|
I2Cx->CLKDIV = SystemCoreClock / 5 / initStruct->MstClk;
|
|||
|
|
|||
|
I2Cx->MSTCMD = (I2Cx->MSTCMD & (~I2C_MSTCMD_IF_Msk)) | (1 << I2C_MSTCMD_IF_Pos); //ʹ<><CAB9><EFBFBD>ж<EFBFBD>֮ǰ<D6AE><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
|
|||
|
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; //<2F><><EFBFBD>жϱ<D0B6>־
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/******************************************************************************************************************************************
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Open()
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD>
|
|||
|
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: I2C_TypeDef * I2Cx ָ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Чֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C0<EFBFBD><EFBFBD>I2C1
|
|||
|
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
|
|||
|
* ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
|
|||
|
******************************************************************************************************************************************/
|
|||
|
void I2C_Open(I2C_TypeDef *I2Cx)
|
|||
|
{
|
|||
|
I2Cx->CTRL |= (0x01 << I2C_CTRL_EN_Pos);
|
|||
|
}
|
|||
|
|
|||
|
/******************************************************************************************************************************************
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Close()
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: I2C<EFBFBD>رգ<EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>շ<EFBFBD>
|
|||
|
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: I2C_TypeDef * I2Cx ָ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Чֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C0<EFBFBD><EFBFBD>I2C1
|
|||
|
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
|
|||
|
* ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
|
|||
|
******************************************************************************************************************************************/
|
|||
|
void I2C_Close(I2C_TypeDef *I2Cx)
|
|||
|
{
|
|||
|
I2Cx->CTRL &= ~I2C_CTRL_EN_Msk;
|
|||
|
}
|