/**
******************************************************************************
* @file stm8s_i2c.c
* @author MCD Application Team
* @version V2.1.0
* @date 18-November-2011
* @brief This file contains all the functions for the I2C peripheral.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s_i2c.h"
/** @addtogroup STM8S_StdPeriph_Driver
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/** @defgroup I2C_Private_Defines
* @{
*/
/* I2C register mask */
#define REGISTER_Mask ((uint16_t)0x3000)
#define REGISTER_SR1_Index ((uint16_t)0x0100)
#define REGISTER_SR2_Index ((uint16_t)0x0200)
/* I2C Interrupt Enable mask */
#define ITEN_Mask ((uint16_t)0x0700)
/* I2C FLAG mask */
#define FLAG_Mask ((uint16_t)0x00FF)
/**
* @}
*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @addtogroup I2C_Public_Functions
* @{
*/
/**
* @brief Deinitializes the I2C peripheral registers to their default reset values.
* @param None
* @retval None
*/
void I2C_DeInit(void)
{
I2C->CR1 = I2C_CR1_RESET_VALUE;
I2C->CR2 = I2C_CR2_RESET_VALUE;
I2C->FREQR = I2C_FREQR_RESET_VALUE;
I2C->OARL = I2C_OARL_RESET_VALUE;
I2C->OARH = I2C_OARH_RESET_VALUE;
I2C->ITR = I2C_ITR_RESET_VALUE;
I2C->CCRL = I2C_CCRL_RESET_VALUE;
I2C->CCRH = I2C_CCRH_RESET_VALUE;
I2C->TRISER = I2C_TRISER_RESET_VALUE;
}
/**
* @brief Initializes the I2C according to the specified parameters in standard
* or fast mode.
* @param OutputClockFrequencyHz : Specifies the output clock frequency in Hz.
* @param OwnAddress : Specifies the own address.
* @param I2C_DutyCycle : Specifies the duty cycle to apply in fast mode.
* This parameter can be any of the @ref I2C_DutyCycle_TypeDef enumeration.
* @note This parameter don't have impact when the OutputClockFrequency lower
* than 100KHz.
* @param Ack : Specifies the acknowledge mode to apply.
* This parameter can be any of the @ref I2C_Ack_TypeDef enumeration.
* @param AddMode : Specifies the acknowledge address to apply.
* This parameter can be any of the @ref I2C_AddMode_TypeDef enumeration.
* @param InputClockFrequencyMHz : Specifies the input clock frequency in MHz.
* @retval None
*/
void I2C_Init(uint32_t OutputClockFrequencyHz, uint16_t OwnAddress,
I2C_DutyCycle_TypeDef I2C_DutyCycle, I2C_Ack_TypeDef Ack,
I2C_AddMode_TypeDef AddMode, uint8_t InputClockFrequencyMHz )
{
uint16_t result = 0x0004;
uint16_t tmpval = 0;
uint8_t tmpccrh = 0;
/*------------------------- I2C FREQ Configuration ------------------------*/
/* Clear frequency bits */
I2C->FREQR &= (uint8_t)(~I2C_FREQR_FREQ);
/* Write new value */
I2C->FREQR |= InputClockFrequencyMHz;
/*--------------------------- I2C CCR Configuration ------------------------*/
/* Disable I2C to configure TRISER */
I2C->CR1 &= (uint8_t)(~I2C_CR1_PE);
/* Clear CCRH & CCRL */
I2C->CCRH &= (uint8_t)(~(I2C_CCRH_FS | I2C_CCRH_DUTY | I2C_CCRH_CCR));
I2C->CCRL &= (uint8_t)(~I2C_CCRL_CCR);
/* Detect Fast or Standard mode depending on the Output clock frequency selected */
if (OutputClockFrequencyHz > I2C_MAX_STANDARD_FREQ) /* FAST MODE */
{
/* Set F/S bit for fast mode */
tmpccrh = I2C_CCRH_FS;
if (I2C_DutyCycle == I2C_DUTYCYCLE_2)
{
/* Fast mode speed calculate: Tlow/Thigh = 2 */
result = (uint16_t) ((InputClockFrequencyMHz * 1000000) / (OutputClockFrequencyHz * 3));
}
else /* I2C_DUTYCYCLE_16_9 */
{
/* Fast mode speed calculate: Tlow/Thigh = 16/9 */
result = (uint16_t) ((InputClockFrequencyMHz * 1000000) / (OutputClockFrequencyHz * 25));
/* Set DUTY bit */
tmpccrh |= I2C_CCRH_DUTY;
}
/* Verify and correct CCR value if below minimum value */
if (result < (uint16_t)0x01)
{
/* Set the minimum allowed value */
result = (uint16_t)0x0001;
}
/* Set Maximum Rise Time: 300ns max in Fast Mode
= [300ns/(1/InputClockFrequencyMHz.10e6)]+1
= [(InputClockFrequencyMHz * 3)/10]+1 */
tmpval = ((InputClockFrequencyMHz * 3) / 10) + 1;
I2C->TRISER = (uint8_t)tmpval;
}
else /* STANDARD MODE */
{
/* Calculate standard mode speed */
result = (uint16_t)((InputClockFrequencyMHz * 1000000) / (OutputClockFrequencyHz << (uint8_t)1));
/* Verify and correct CCR value if below minimum value */
if (result < (uint16_t)0x0004)
{
/* Set the minimum allowed value */
result = (uint16_t)0x0004;
}
/* Set Maximum Rise Time: 1000ns max in Standard Mode
= [1000ns/(1/InputClockFrequencyMHz.10e6)]+1
= InputClockFrequencyMHz+1 */
I2C->TRISER = (uint8_t)(InputClockFrequencyMHz + (uint8_t)1);
}
/* Write CCR with new calculated value */
I2C->CCRL = (uint8_t)result;
I2C->CCRH = (uint8_t)((uint8_t)((uint8_t)(result >> 8) & I2C_CCRH_CCR) | tmpccrh);
/* Enable I2C */ // 广播呼叫使能
I2C->CR1 |= I2C_CR1_PE|I2C_CR1_ENGC/*|I2C_CR1_NOSTRETCH*/;
/* Configure I2C acknowledgement */
I2C_AcknowledgeConfig(Ack);
/*--------------------------- I2C OAR Configuration ------------------------*/
I2C->OARL = (uint8_t)(OwnAddress);
I2C->OARH = (uint8_t)((uint8_t)(AddMode | I2C_OARH_ADDCONF) |
(uint8_t)((OwnAddress & (uint16_t)0x0300) >> (uint8_t)7));
}
/**
* @brief Enables or disables the I2C peripheral.
* @param NewState : Indicate the new I2C peripheral state.
* This parameter can be any of the @ref FunctionalState enumeration.
* @retval None
*/
void I2C_Cmd(bool NewState)
{
/* Check function parameters */
//assert_param(IS_FUNCTIONALSTATE_OK(NewState));
if (NewState != DISABLE)
{
/* Enable I2C peripheral */
I2C->CR1 |= I2C_CR1_PE;
}
else /* NewState == DISABLE */
{
/* Disable I2C peripheral */
I2C->CR1 &= (uint8_t)(~I2C_CR1_PE);
}
}
/**
* @brief Enables or disables the I2C General Call feature.
* @param NewState : State of the General Call feature.
* This parameter can be any of the @ref FunctionalState enumeration.
* @retval None
*/
void I2C_GeneralCallCmd(bool NewState)
{
/* Check function parameters */
// assert_param(IS_FUNCTIONALSTATE_OK(NewState));
if (NewState != DISABLE)
{
/* Enable General Call */
I2C->CR1 |= I2C_CR1_ENGC;
}
else /* NewState == DISABLE */
{
/* Disable General Call */
I2C->CR1 &= (uint8_t)(~I2C_CR1_ENGC);
}
}
/**
* @brief Generates I2C communication START condition.
* @note CCR must be programmed, i.e. I2C_Init function must have been called
* wi