2022-10-19 22:44:15 +08:00

216 lines
8.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/******************************************************************************************************************************************
* <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>: SWM320_i2c.c
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: SWM320<32><30>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD>I2C<32><43><EFBFBD>нӿڹ<D3BF><DAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>: http://www.synwit.com.cn/e/tool/gbook/?bid=1
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
* <20><EFBFBD><E6B1BE><EFBFBD><EFBFBD>: V1.1.0 2017<31><37>10<31><30>25<32><35>
* <20><><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"
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Init()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: I2C<32><43>ʼ<EFBFBD><CABC>
* <20><> <20><>: I2C_TypeDef * I2Cx ָ<><D6B8>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<32><43><EFBFBD><EFBFBD>Чֵ<D0A7><D6B5><EFBFBD><EFBFBD>I2C0<43><30>I2C1
* I2C_InitStructure * initStruct <09><><EFBFBD><EFBFBD>I2C<32><43><EFBFBD><EFBFBD><EFBFBD>趨ֵ<E8B6A8>Ľṹ<C4BD><E1B9B9>
* <20><> <20><>: <20><>
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ģ<><C4A3>ֻ<EFBFBD>ܹ<EFBFBD><DCB9><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;
}
}
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Open()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: I2C<32>򿪣<EFBFBD><F2BFAAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD>
* <20><> <20><>: I2C_TypeDef * I2Cx ָ<><D6B8>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<32><43><EFBFBD><EFBFBD>Чֵ<D0A7><D6B5><EFBFBD><EFBFBD>I2C0<43><30>I2C1
* <20><> <20><>: <20><>
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
void I2C_Open(I2C_TypeDef * I2Cx)
{
I2Cx->CTRL |= (0x01 << I2C_CTRL_EN_Pos);
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Close()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: I2C<32>رգ<D8B1><D5A3><EFBFBD>ֹ<EFBFBD>շ<EFBFBD>
* <20><> <20><>: I2C_TypeDef * I2Cx ָ<><D6B8>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<32><43><EFBFBD><EFBFBD>Чֵ<D0A7><D6B5><EFBFBD><EFBFBD>I2C0<43><30>I2C1
* <20><> <20><>: <20><>
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
void I2C_Close(I2C_TypeDef * I2Cx)
{
I2Cx->CTRL &= ~I2C_CTRL_EN_Msk;
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Start()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>źŲ<C5BA><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8>ַ
* <20><> <20><>: I2C_TypeDef * I2Cx ָ<><D6B8>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<32><43><EFBFBD><EFBFBD>Чֵ<D0A7><D6B5><EFBFBD><EFBFBD>I2C0<43><30>I2C1
* uint8_t addr <09><EFBFBD><E8B1B8>ַ
* <20><> <20><>: uint8_t 1 <20><><EFBFBD>յ<EFBFBD>ACK 0 <20><><EFBFBD>յ<EFBFBD>NACK
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
uint8_t I2C_Start(I2C_TypeDef * I2Cx, uint8_t addr)
{
I2Cx->MSTDAT = addr;
I2Cx->MSTCMD = (1 << I2C_MSTCMD_STA_Pos) |
(1 << I2C_MSTCMD_WR_Pos); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC>ʹӻ<CDB4><D3BB><EFBFBD>ַ
while(I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk) __NOP(); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return (I2Cx->MSTCMD & I2C_MSTCMD_RXACK_Msk) ? 0 : 1;
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Stop()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: <09><><EFBFBD><EFBFBD>ֹͣ<CDA3>ź<EFBFBD>
* <20><> <20><>: I2C_TypeDef * I2Cx ָ<><D6B8>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<32><43><EFBFBD><EFBFBD>Чֵ<D0A7><D6B5><EFBFBD><EFBFBD>I2C0<43><30>I2C1
* <20><> <20><>: <20><>
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
void I2C_Stop(I2C_TypeDef * I2Cx)
{
I2Cx->MSTCMD = (1 << I2C_MSTCMD_STO_Pos);
while(I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk) __NOP(); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Write()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: д<><D0B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <20><> <20><>: I2C_TypeDef * I2Cx ָ<><D6B8>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<32><43><EFBFBD><EFBFBD>Чֵ<D0A7><D6B5><EFBFBD><EFBFBD>I2C0<43><30>I2C1
* uint8_t data Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <20><> <20><>: uint8_t 1 <20><><EFBFBD>յ<EFBFBD>ACK 0 <20><><EFBFBD>յ<EFBFBD>NACK
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
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(); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return (I2Cx->MSTCMD & I2C_MSTCMD_RXACK_Msk) ? 0 : 1;
}
/******************************************************************************************************************************************
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: I2C_Read()
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>: <09><>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <20><> <20><>: I2C_TypeDef * I2Cx ָ<><D6B8>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>I2C<32><43><EFBFBD><EFBFBD>Чֵ<D0A7><D6B5><EFBFBD><EFBFBD>I2C0<43><30>I2C1
* uint8_t ack 1 <20><><EFBFBD><EFBFBD>ACK 0 <20><><EFBFBD><EFBFBD>NACK
* <20><> <20><>: uint8_t <09><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>
******************************************************************************************************************************************/
uint8_t I2C_Read(I2C_TypeDef * I2Cx, uint8_t ack)
{
I2Cx->MSTCMD = (1 << I2C_MSTCMD_RD_Pos) |
((ack ? 0 : 1) << I2C_MSTCMD_ACK_Pos);
while(I2Cx->MSTCMD & I2C_MSTCMD_TIP_Msk) __NOP(); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return I2Cx->MSTDAT;
}