STM8S/A Standard Peripherals Drivers: stm8s_i2c.c Source File

STM8S/A

stm8s_i2c.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8s_i2c.c
00004   * @author  MCD Application Team
00005   * @version V2.3.0
00006   * @date    16-June-2017
00007   * @brief   This file contains all the functions for the I2C peripheral.
00008    ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "stm8s_i2c.h"
00030 
00031 /** @addtogroup STM8S_StdPeriph_Driver
00032   * @{
00033   */
00034 /* Private typedef -----------------------------------------------------------*/
00035 
00036 /** @addtogroup I2C_Private_Defines
00037   * @{
00038   */
00039 /* I2C register mask */
00040 #define REGISTER_Mask               ((uint16_t)0x3000)
00041 #define REGISTER_SR1_Index          ((uint16_t)0x0100)
00042 #define REGISTER_SR2_Index          ((uint16_t)0x0200)
00043 /* I2C Interrupt Enable mask */
00044 #define ITEN_Mask                   ((uint16_t)0x0700)
00045 /* I2C FLAG mask */
00046 #define FLAG_Mask                   ((uint16_t)0x00FF)
00047 /**
00048   * @}
00049   */
00050 
00051 /* Private macro -------------------------------------------------------------*/
00052 /* Private variables ---------------------------------------------------------*/
00053 /* Private function prototypes -----------------------------------------------*/
00054 /* Private functions ---------------------------------------------------------*/
00055 
00056 
00057 /**
00058   * @addtogroup I2C_Public_Functions
00059   * @{
00060   */
00061 
00062 /**
00063   * @brief  Deinitializes the I2C peripheral registers to their default reset values.
00064   * @param  None
00065   * @retval None
00066   */
00067 void I2C_DeInit(void)
00068 {
00069   I2C->CR1 = I2C_CR1_RESET_VALUE;
00070   I2C->CR2 = I2C_CR2_RESET_VALUE;
00071   I2C->FREQR = I2C_FREQR_RESET_VALUE;
00072   I2C->OARL = I2C_OARL_RESET_VALUE;
00073   I2C->OARH = I2C_OARH_RESET_VALUE;
00074   I2C->ITR = I2C_ITR_RESET_VALUE;
00075   I2C->CCRL = I2C_CCRL_RESET_VALUE;
00076   I2C->CCRH = I2C_CCRH_RESET_VALUE;
00077   I2C->TRISER = I2C_TRISER_RESET_VALUE;
00078 }
00079 
00080 /**
00081   * @brief  Initializes the I2C according to the specified parameters in standard
00082   *         or fast mode.
00083   * @param  OutputClockFrequencyHz : Specifies the output clock frequency in Hz.
00084   * @param  OwnAddress : Specifies the own address.
00085   * @param  I2C_DutyCycle : Specifies the duty cycle to apply in fast mode.
00086   *         This parameter can be any of the  @ref I2C_DutyCycle_TypeDef enumeration.
00087   * @note   This parameter don't have impact when the OutputClockFrequency lower
00088   *         than 100KHz.
00089   * @param  Ack : Specifies the acknowledge mode to apply.
00090   *         This parameter can be any of the  @ref I2C_Ack_TypeDef enumeration.
00091   * @param  AddMode : Specifies the acknowledge address to apply.
00092   *         This parameter can be any of the  @ref I2C_AddMode_TypeDef enumeration.
00093   * @param  InputClockFrequencyMHz : Specifies the input clock frequency in MHz.
00094   * @retval None
00095   */
00096 void I2C_Init(uint32_t OutputClockFrequencyHz, uint16_t OwnAddress, 
00097               I2C_DutyCycle_TypeDef I2C_DutyCycle, I2C_Ack_TypeDef Ack, 
00098               I2C_AddMode_TypeDef AddMode, uint8_t InputClockFrequencyMHz )
00099 {
00100   uint16_t result = 0x0004;
00101   uint16_t tmpval = 0;
00102   uint8_t tmpccrh = 0;
00103 
00104   /* Check the parameters */
00105   assert_param(IS_I2C_ACK_OK(Ack));
00106   assert_param(IS_I2C_ADDMODE_OK(AddMode));
00107   assert_param(IS_I2C_OWN_ADDRESS_OK(OwnAddress));
00108   assert_param(IS_I2C_DUTYCYCLE_OK(I2C_DutyCycle));  
00109   assert_param(IS_I2C_INPUT_CLOCK_FREQ_OK(InputClockFrequencyMHz));
00110   assert_param(IS_I2C_OUTPUT_CLOCK_FREQ_OK(OutputClockFrequencyHz));
00111 
00112 
00113   /*------------------------- I2C FREQ Configuration ------------------------*/
00114   /* Clear frequency bits */
00115   I2C->FREQR &= (uint8_t)(~I2C_FREQR_FREQ);
00116   /* Write new value */
00117   I2C->FREQR |= InputClockFrequencyMHz;
00118 
00119   /*--------------------------- I2C CCR Configuration ------------------------*/
00120   /* Disable I2C to configure TRISER */
00121   I2C->CR1 &= (uint8_t)(~I2C_CR1_PE);
00122 
00123   /* Clear CCRH & CCRL */
00124   I2C->CCRH &= (uint8_t)(~(I2C_CCRH_FS | I2C_CCRH_DUTY | I2C_CCRH_CCR));
00125   I2C->CCRL &= (uint8_t)(~I2C_CCRL_CCR);
00126 
00127   /* Detect Fast or Standard mode depending on the Output clock frequency selected */
00128   if (OutputClockFrequencyHz > I2C_MAX_STANDARD_FREQ) /* FAST MODE */
00129   {
00130     /* Set F/S bit for fast mode */
00131     tmpccrh = I2C_CCRH_FS;
00132 
00133     if (I2C_DutyCycle == I2C_DUTYCYCLE_2)
00134     {
00135       /* Fast mode speed calculate: Tlow/Thigh = 2 */
00136       result = (uint16_t) ((InputClockFrequencyMHz * 1000000) / (OutputClockFrequencyHz * 3));
00137     }
00138     else /* I2C_DUTYCYCLE_16_9 */
00139     {
00140       /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
00141       result = (uint16_t) ((InputClockFrequencyMHz * 1000000) / (OutputClockFrequencyHz * 25));
00142       /* Set DUTY bit */
00143       tmpccrh |= I2C_CCRH_DUTY;
00144     }
00145 
00146     /* Verify and correct CCR value if below minimum value */
00147     if (result < (uint16_t)0x01)
00148     {
00149       /* Set the minimum allowed value */
00150       result = (uint16_t)0x0001;
00151     }
00152 
00153     /* Set Maximum Rise Time: 300ns max in Fast Mode
00154     = [300ns/(1/InputClockFrequencyMHz.10e6)]+1
00155     = [(InputClockFrequencyMHz * 3)/10]+1 */
00156     tmpval = ((InputClockFrequencyMHz * 3) / 10) + 1;
00157     I2C->TRISER = (uint8_t)tmpval;
00158 
00159   }
00160   else /* STANDARD MODE */
00161   {
00162 
00163     /* Calculate standard mode speed */
00164     result = (uint16_t)((InputClockFrequencyMHz * 1000000) / (OutputClockFrequencyHz << (uint8_t)1));
00165 
00166     /* Verify and correct CCR value if below minimum value */
00167     if (result < (uint16_t)0x0004)
00168     {
00169       /* Set the minimum allowed value */
00170       result = (uint16_t)0x0004;
00171     }
00172 
00173     /* Set Maximum Rise Time: 1000ns max in Standard Mode
00174     = [1000ns/(1/InputClockFrequencyMHz.10e6)]+1
00175     = InputClockFrequencyMHz+1 */
00176     I2C->TRISER = (uint8_t)(InputClockFrequencyMHz + (uint8_t)1);
00177 
00178   }
00179 
00180   /* Write CCR with new calculated value */
00181   I2C->CCRL = (uint8_t)result;
00182   I2C->CCRH = (uint8_t)((uint8_t)((uint8_t)(result >> 8) & I2C_CCRH_CCR) | tmpccrh);
00183 
00184   /* Enable I2C */
00185   I2C->CR1 |= I2C_CR1_PE;
00186 
00187   /* Configure I2C acknowledgement */
00188   I2C_AcknowledgeConfig(Ack);
00189 
00190   /*--------------------------- I2C OAR Configuration ------------------------*/
00191   I2C->OARL = (uint8_t)(OwnAddress);
00192   I2C->OARH = (uint8_t)((uint8_t)(AddMode | I2C_OARH_ADDCONF) |
00193                    (uint8_t)((OwnAddress & (uint16_t)0x0300) >> (uint8_t)7));
00194 }
00195 
00196 /**
00197   * @brief  Enables or disables the I2C peripheral.
00198   * @param  NewState : Indicate the new I2C peripheral state.
00199   *         This parameter can be any of the @ref FunctionalState enumeration.
00200   * @retval None
00201   */
00202 void I2C_Cmd(FunctionalState NewState)
00203 {
00204   /* Check function parameters */
00205   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00206 
00207   if (NewState != DISABLE)
00208   {
00209     /* Enable I2C peripheral */
00210     I2C->CR1 |= I2C_CR1_PE;
00211   }
00212   else /* NewState == DISABLE */
00213   {
00214     /* Disable I2C peripheral */
00215     I2C->CR1 &= (uint8_t)(~I2C_CR1_PE);
00216   }
00217 }
00218 
00219 /**
00220   * @brief  Enables or disables the I2C General Call feature.
00221   * @param  NewState : State of the General Call feature.
00222   *         This parameter can be any of the @ref FunctionalState enumeration.
00223   * @retval None
00224   */
00225 void I2C_GeneralCallCmd(FunctionalState NewState)
00226 {
00227   /* Check function parameters */
00228   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00229 
00230   if (NewState != DISABLE)
00231   {
00232     /* Enable General Call */
00233     I2C->CR1 |= I2C_CR1_ENGC;
00234   }
00235   else /* NewState == DISABLE */
00236   {
00237     /* Disable General Call */
00238     I2C->CR1 &= (uint8_t)(~I2C_CR1_ENGC);
00239   }
00240 }
00241 
00242 /**
00243   * @brief  Generates I2C communication START condition.
00244   * @note   CCR must be programmed, i.e. I2C_Init function must have been called
00245   *         with a valid I2C_ClockSpeed
00246   * @param  NewState : Enable or disable the start condition.
00247   *         This parameter can be any of the @ref FunctionalState enumeration.
00248   * @retval None
00249   */
00250 void I2C_GenerateSTART(FunctionalState NewState)
00251 {
00252   /* Check function parameters */
00253   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00254 
00255   if (NewState != DISABLE)
00256   {
00257     /* Generate a START condition */
00258     I2C->CR2 |= I2C_CR2_START;
00259   }
00260   else /* NewState == DISABLE */
00261   {
00262     /* Disable the START condition generation */
00263     I2C->CR2 &= (uint8_t)(~I2C_CR2_START);
00264   }
00265 }
00266 
00267 /**
00268   * @brief  Generates I2C communication STOP condition.
00269   * @param   NewState : Enable or disable the stop condition.
00270   *          This parameter can be any of the @ref FunctionalState enumeration.
00271   * @retval None
00272   */
00273 void I2C_GenerateSTOP(FunctionalState NewState)
00274 {
00275   /* Check function parameters */
00276   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00277 
00278   if (NewState != DISABLE)
00279   {
00280     /* Generate a STOP condition */
00281     I2C->CR2 |= I2C_CR2_STOP;
00282   }
00283   else /* NewState == DISABLE */
00284   {
00285     /* Disable the STOP condition generation */
00286     I2C->CR2 &= (uint8_t)(~I2C_CR2_STOP);
00287   }
00288 }
00289 
00290 /**
00291   * @brief  Enables or disables I2C software reset.
00292   * @param  NewState : Specifies the new state of the I2C software reset.
00293   *         This parameter can be any of the @ref FunctionalState enumeration.
00294   * @retval None
00295   */
00296 void I2C_SoftwareResetCmd(FunctionalState NewState)
00297 {
00298   /* Check function parameters */
00299   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00300 
00301   if (NewState != DISABLE)
00302   {
00303     /* Peripheral under reset */
00304     I2C->CR2 |= I2C_CR2_SWRST;
00305   }
00306   else /* NewState == DISABLE */
00307   {
00308     /* Peripheral not under reset */
00309     I2C->CR2 &= (uint8_t)(~I2C_CR2_SWRST);
00310   }
00311 }
00312 
00313 /**
00314   * @brief  Enables or disables the I2C clock stretching.
00315   * @param  NewState : Specifies the new state of the I2C Clock stretching.
00316   *         This parameter can be any of the @ref FunctionalState enumeration.
00317   * @retval None
00318   */
00319 
00320 void I2C_StretchClockCmd(FunctionalState NewState)
00321 {
00322   /* Check function parameters */
00323   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00324 
00325   if (NewState != DISABLE)
00326   {
00327     /* Clock Stretching Enable */
00328     I2C->CR1 &= (uint8_t)(~I2C_CR1_NOSTRETCH);
00329 
00330   }
00331   else /* NewState == DISABLE */
00332   {
00333     /* Clock Stretching Disable (Slave mode) */
00334     I2C->CR1 |= I2C_CR1_NOSTRETCH;
00335   }
00336 }
00337 
00338 /**
00339   * @brief  Enable or Disable the I2C acknowledge and position acknowledge feature.
00340   * @note   This function must be called before data reception start
00341   * @param  Ack : Specifies the acknowledge mode to apply.
00342   *         This parameter can be any of the  @ref I2C_Ack_TypeDef enumeration.
00343   * @retval None
00344   */
00345 void I2C_AcknowledgeConfig(I2C_Ack_TypeDef Ack)
00346 {
00347   /* Check function parameters */
00348   assert_param(IS_I2C_ACK_OK(Ack));
00349 
00350   if (Ack == I2C_ACK_NONE)
00351   {
00352     /* Disable the acknowledgement */
00353     I2C->CR2 &= (uint8_t)(~I2C_CR2_ACK);
00354   }
00355   else
00356   {
00357     /* Enable the acknowledgement */
00358     I2C->CR2 |= I2C_CR2_ACK;
00359 
00360     if (Ack == I2C_ACK_CURR)
00361     {
00362       /* Configure (N)ACK on current byte */
00363       I2C->CR2 &= (uint8_t)(~I2C_CR2_POS);
00364     }
00365     else
00366     {
00367       /* Configure (N)ACK on next byte */
00368       I2C->CR2 |= I2C_CR2_POS;
00369     }
00370   }
00371 }
00372 
00373 /**
00374   * @brief  Enables or disables the specified I2C interrupt.
00375   * @param  I2C_IT : Name of the interrupt to enable or disable.
00376   *         This parameter can be any of the  @ref I2C_IT_TypeDef enumeration.
00377   * @param  NewState : State of the interrupt to apply.
00378   *         This parameter can be any of the @ref FunctionalState enumeration.
00379   * @retval None
00380   */
00381 void I2C_ITConfig(I2C_IT_TypeDef I2C_IT, FunctionalState NewState)
00382 {
00383   /* Check functions parameters */
00384   assert_param(IS_I2C_INTERRUPT_OK(I2C_IT));
00385   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00386 
00387   if (NewState != DISABLE)
00388   {
00389     /* Enable the selected I2C interrupts */
00390     I2C->ITR |= (uint8_t)I2C_IT;
00391   }
00392   else /* NewState == DISABLE */
00393   {
00394     /* Disable the selected I2C interrupts */
00395     I2C->ITR &= (uint8_t)(~(uint8_t)I2C_IT);
00396   }
00397 }
00398 
00399 /**
00400   * @brief  Selects the specified I2C fast mode duty cycle.
00401   * @param  I2C_DutyCycle : Specifies the duty cycle to apply.
00402   *         This parameter can be any of the @ref I2C_DutyCycle_TypeDef enumeration.
00403   * @retval None
00404   */
00405 void I2C_FastModeDutyCycleConfig(I2C_DutyCycle_TypeDef I2C_DutyCycle)
00406 {
00407   /* Check function parameters */
00408   assert_param(IS_I2C_DUTYCYCLE_OK(I2C_DutyCycle));
00409 
00410   if (I2C_DutyCycle == I2C_DUTYCYCLE_16_9)
00411   {
00412     /* I2C fast mode Tlow/Thigh = 16/9 */
00413     I2C->CCRH |= I2C_CCRH_DUTY;
00414   }
00415   else /* I2C_DUTYCYCLE_2 */
00416   {
00417     /* I2C fast mode Tlow/Thigh = 2 */
00418     I2C->CCRH &= (uint8_t)(~I2C_CCRH_DUTY);
00419   }
00420 }
00421 
00422 /**
00423   * @brief  Returns the most recent received data.
00424   * @param  None
00425   * @retval uint8_t : The value of the received byte data.
00426   */
00427 uint8_t I2C_ReceiveData(void)
00428 {
00429   /* Return the data present in the DR register */
00430   return ((uint8_t)I2C->DR);
00431 }
00432 
00433 /**
00434   * @brief  Transmits the 7-bit address (to select the) slave device.
00435   * @param   Address : Specifies the slave address which will be transmitted.
00436   * @param   Direction : Specifies whether the I2C device will be a Transmitter or a Receiver.
00437   * This parameter can be any of the @ref I2C_Direction_TypeDef enumeration.
00438   * @retval None
00439   */
00440 void I2C_Send7bitAddress(uint8_t Address, I2C_Direction_TypeDef Direction)
00441 {
00442   /* Check function parameters */
00443   assert_param(IS_I2C_ADDRESS_OK(Address));
00444   assert_param(IS_I2C_DIRECTION_OK(Direction));
00445 
00446   /* Clear bit0 (direction) just in case */
00447   Address &= (uint8_t)0xFE;
00448 
00449   /* Send the Address + Direction */
00450   I2C->DR = (uint8_t)(Address | (uint8_t)Direction);
00451 }
00452 
00453 /**
00454   * @brief  Send a byte by writing in the DR register.
00455   * @param   Data : Byte to be sent.
00456   * @retval None
00457   */
00458 void I2C_SendData(uint8_t Data)
00459 {
00460   /* Write in the DR register the data to be sent */
00461   I2C->DR = Data;
00462 }
00463 
00464 /**
00465  * @brief
00466  ****************************************************************************************
00467  *
00468  *                         I2C State Monitoring Functions
00469  *
00470  ****************************************************************************************
00471  * This I2C driver provides three different ways for I2C state monitoring
00472  *  depending on the application requirements and constraints:
00473  *
00474  *
00475  * 1) Basic state monitoring:
00476  *    Using I2C_CheckEvent() function:
00477  *    It compares the status registers (SR1, SR2 and SR3) content to a given event
00478  *    (can be the combination of one or more flags).
00479  *    It returns SUCCESS if the current status includes the given flags
00480  *    and returns ERROR if one or more flags are missing in the current status.
00481  *    - When to use:
00482  *      - This function is suitable for most applications as well as for startup
00483  *      activity since the events are fully described in the product reference manual
00484  *      (RM0016).
00485  *      - It is also suitable for users who need to define their own events.
00486  *    - Limitations:
00487  *      - If an error occurs (ie. error flags are set besides to the monitored flags),
00488  *        the I2C_CheckEvent() function may return SUCCESS despite the communication
00489  *        hold or corrupted real state.
00490  *        In this case, it is advised to use error interrupts to monitor the error
00491  *        events and handle them in the interrupt IRQ handler.
00492  *
00493  *        @note
00494  *        For error management, it is advised to use the following functions:
00495  *          - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
00496  *          - I2C_IRQHandler() which is called when the I2C interrupts occur.
00497  *          - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the
00498  *           I2C_IRQHandler() function in order to determine which error occurred.
00499  *          - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
00500  *            and/or I2C_GenerateStop() in order to clear the error flag and
00501  *            source and return to correct communication status.
00502  *
00503  *
00504  *  2) Advanced state monitoring:
00505  *     Using the function I2C_GetLastEvent() which returns the image of both SR1
00506  *     & SR3 status registers in a single word (uint16_t) (Status Register 3 value
00507  *     is shifted left by 8 bits and concatenated to Status Register 1).
00508  *     - When to use:
00509  *       - This function is suitable for the same applications above but it allows to
00510  *         overcome the limitations of I2C_GetFlagStatus() function (see below).
00511  *         The returned value could be compared to events already defined in the
00512  *         library (stm8s_i2c.h) or to custom values defined by user.
00513  *       - This function is suitable when multiple flags are monitored at the same time.
00514  *       - At the opposite of I2C_CheckEvent() function, this function allows user to
00515  *         choose when an event is accepted (when all events flags are set and no
00516  *         other flags are set or just when the needed flags are set like
00517  *         I2C_CheckEvent() function).
00518  *     - Limitations:
00519  *       - User may need to define his own events.
00520  *       - Same remark concerning the error management is applicable for this
00521  *         function if user decides to check only regular communication flags (and
00522  *         ignores error flags).
00523  *
00524  *
00525  *  3) Flag-based state monitoring:
00526  *     Using the function I2C_GetFlagStatus() which simply returns the status of
00527  *     one single flag (ie. I2C_FLAG_RXNE ...).
00528  *     - When to use:
00529  *        - This function could be used for specific applications or in debug phase.
00530  *        - It is suitable when only one flag checking is needed (most I2C events
00531  *          are monitored through multiple flags).
00532  *     - Limitations:
00533  *        - When calling this function, the Status register is accessed. Some flags are
00534  *          cleared when the status register is accessed. So checking the status
00535  *          of one Flag, may clear other ones.
00536  *        - Function may need to be called twice or more in order to monitor one
00537  *          single event.
00538  *
00539  *  For detailed description of Events, please refer to section I2C_Events in
00540  *  stm8s_i2c.h file.
00541  *
00542  */
00543 /**
00544  *
00545  *  1) Basic state monitoring
00546  *******************************************************************************
00547  */
00548 
00549 /**
00550   * @brief  Checks whether the last I2C Event is equal to the one passed
00551   *   as parameter.
00552   * @param  I2C_Event: specifies the event to be checked.
00553   *   This parameter can be one of the following values:
00554   *     @arg I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED           : EV1
00555   *     @arg I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED              : EV1
00556   *     @arg I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED            : EV1
00557   *     @arg I2C_EVENT_SLAVE_BYTE_RECEIVED                         : EV2
00558   *     @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)    : EV2
00559   *     @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED                      : EV3
00560   *     @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL) : EV3
00561   *     @arg I2C_EVENT_SLAVE_ACK_FAILURE                           : EV3_2
00562   *     @arg I2C_EVENT_SLAVE_STOP_DETECTED                         : EV4
00563   *     @arg I2C_EVENT_MASTER_MODE_SELECT                          : EV5
00564   *     @arg I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED            : EV6
00565   *     @arg I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED               : EV6
00566   *     @arg I2C_EVENT_MASTER_BYTE_RECEIVED                        : EV7
00567   *     @arg I2C_EVENT_MASTER_BYTE_TRANSMITTING                    : EV8
00568   *     @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED                     : EV8_2
00569   *     @arg I2C_EVENT_MASTER_MODE_ADDRESS10                       : EV9
00570   *
00571   * @note: For detailed description of Events, please refer to section
00572   *    I2C_Events in stm8s_i2c.h file.
00573   *
00574   * @retval An ErrorStatus enumeration value:
00575   * - SUCCESS: Last event is equal to the I2C_EVENT
00576   * - ERROR: Last event is different from the I2C_EVENT
00577   */
00578 ErrorStatus I2C_CheckEvent(I2C_Event_TypeDef I2C_Event)
00579 {
00580   __IO uint16_t lastevent = 0x00;
00581   uint8_t flag1 = 0x00 ;
00582   uint8_t flag2 = 0x00;
00583   ErrorStatus status = ERROR;
00584 
00585   /* Check the parameters */
00586   assert_param(IS_I2C_EVENT_OK(I2C_Event));
00587 
00588   if (I2C_Event == I2C_EVENT_SLAVE_ACK_FAILURE)
00589   {
00590     lastevent = I2C->SR2 & I2C_SR2_AF;
00591   }
00592   else
00593   {
00594     flag1 = I2C->SR1;
00595     flag2 = I2C->SR3;
00596     lastevent = ((uint16_t)((uint16_t)flag2 << (uint16_t)8) | (uint16_t)flag1);
00597   }
00598   /* Check whether the last event is equal to I2C_EVENT */
00599   if (((uint16_t)lastevent & (uint16_t)I2C_Event) == (uint16_t)I2C_Event)
00600   {
00601     /* SUCCESS: last event is equal to I2C_EVENT */
00602     status = SUCCESS;
00603   }
00604   else
00605   {
00606     /* ERROR: last event is different from I2C_EVENT */
00607     status = ERROR;
00608   }
00609 
00610   /* Return status */
00611   return status;
00612 }
00613 
00614 /**
00615  *
00616  *  2) Advanced state monitoring
00617  *******************************************************************************
00618  */
00619 /**
00620   * @brief  Returns the last I2C Event.
00621   *
00622   * @note: For detailed description of Events, please refer to section
00623   *    I2C_Events in stm8s_i2c.h file.
00624   *
00625   * @retval The last event
00626   *   This parameter can be any of the  @ref I2C_Event_TypeDef enumeration.
00627   */
00628 I2C_Event_TypeDef I2C_GetLastEvent(void)
00629 {
00630   __IO uint16_t lastevent = 0;
00631   uint16_t flag1 = 0;
00632   uint16_t flag2 = 0;
00633 
00634   if ((I2C->SR2 & I2C_SR2_AF) != 0x00)
00635   {
00636     lastevent = I2C_EVENT_SLAVE_ACK_FAILURE;
00637   }
00638   else
00639   {
00640     /* Read the I2C status register */
00641     flag1 = I2C->SR1;
00642     flag2 = I2C->SR3;
00643 
00644     /* Get the last event value from I2C status register */
00645     lastevent = ((uint16_t)((uint16_t)flag2 << 8) | (uint16_t)flag1);
00646   }
00647   /* Return status */
00648   return (I2C_Event_TypeDef)lastevent;
00649 }
00650 
00651 /**
00652  *
00653  *  3) Flag-based state monitoring
00654  *******************************************************************************
00655  */
00656 /**
00657   * @brief  Checks whether the specified I2C flag is set or not.
00658   * @param  I2C_Flag: specifies the flag to check.
00659   *   This parameter can be one of the following values:
00660   *     @arg I2C_FLAG_GENERALCALL: General call header flag (Slave mode)
00661   *     @arg I2C_FLAG_TRANSMITTERRECEIVER: Transmitter/Receiver flag
00662   *     @arg I2C_FLAG_BUSBUSY: Bus busy flag
00663   *     @arg I2C_FLAG_MASTERSLAVE: Master/Slave flag
00664   *     @arg I2C_FLAG_WAKEUPFROMHALT: Wake up from HALT flag  
00665   *     @arg I2C_FLAG_OVERRUNUNDERRUN: Overrun/Underrun flag (Slave mode)
00666   *     @arg I2C_FLAG_ACKNOWLEDGEFAILURE: Acknowledge failure flag
00667   *     @arg I2C_FLAG_ARBITRATIONLOSS: Arbitration lost flag (Master mode)
00668   *     @arg I2C_FLAG_BUSERROR: Bus error flag
00669   *     @arg I2C_FLAG_TXEMPTY: Data register empty flag (Transmitter)
00670   *     @arg I2C_FLAG_RXNOTEMPTY: Data register not empty (Receiver) flag
00671   *     @arg I2C_FLAG_STOPDETECTION: Stop detection flag (Slave mode)
00672   *     @arg I2C_FLAG_HEADERSENT: 10-bit header sent flag (Master mode)
00673   *     @arg I2C_FLAG_TRANSFERFINISHED: Byte transfer finished flag
00674   *     @arg I2C_FLAG_ADDRESSSENTMATCHED: Address sent flag (Master mode) �ADSL�
00675   *   Address matched flag (Slave mode)�ENDAD�
00676   *     @arg I2C_FLAG_STARTDETECTION: Start bit flag (Master mode)
00677   * @retval The new state of I2C_FLAG (SET or RESET).
00678   */
00679 FlagStatus I2C_GetFlagStatus(I2C_Flag_TypeDef I2C_Flag)
00680 {
00681   uint8_t tempreg = 0;
00682   uint8_t regindex = 0;
00683   FlagStatus bitstatus = RESET;
00684 
00685   /* Check the parameters */
00686   assert_param(IS_I2C_FLAG_OK(I2C_Flag));
00687 
00688   /* Read flag register index */
00689   regindex = (uint8_t)((uint16_t)I2C_Flag >> 8);
00690   /* Check SRx index */
00691   switch (regindex)
00692   {
00693       /* Returns whether the status register to check is SR1 */
00694     case 0x01:
00695       tempreg = (uint8_t)I2C->SR1;
00696       break;
00697 
00698       /* Returns whether the status register to check is SR2 */
00699     case 0x02:
00700       tempreg = (uint8_t)I2C->SR2;
00701       break;
00702 
00703       /* Returns whether the status register to check is SR3 */
00704     case 0x03:
00705       tempreg = (uint8_t)I2C->SR3;
00706       break;
00707 
00708     default:
00709       break;
00710   }
00711 
00712   /* Check the status of the specified I2C flag */
00713   if ((tempreg & (uint8_t)I2C_Flag ) != 0)
00714   {
00715     /* Flag is set */
00716     bitstatus = SET;
00717   }
00718   else
00719   {
00720     /* Flag is reset */
00721     bitstatus = RESET;
00722   }
00723   /* Return the flag status */
00724   return bitstatus;
00725 }
00726 
00727 /**
00728   * @brief  Clear flags
00729   * @param  I2C_FLAG : Specifies the flag to clear
00730   *   This parameter can be any combination of the following values:
00731   *                       - I2C_FLAG_WAKEUPFROMHALT: Wakeup from Halt
00732   *                       - I2C_FLAG_OVERRUNUNDERRUN: Overrun/Underrun flag (Slave mode)
00733   *                       - I2C_FLAG_ACKNOWLEDGEFAILURE: Acknowledge failure flag
00734   *                       - I2C_FLAG_ARBITRATIONLOSS: Arbitration lost flag (Master mode)
00735   *                       - I2C_FLAG_BUSERROR: Bus error flag.
00736   * @note Notes:
00737   *                       - STOPF (STOP detection) is cleared by software
00738   *                         sequence: a read operation to I2C_SR1 register
00739   *                         (I2C_GetFlagStatus()) followed by a write operation
00740   *                         to I2C_CR2 register.
00741   *                       - ADD10 (10-bit header sent) is cleared by software
00742   *                         sequence: a read operation to I2C_SR1
00743   *                         (I2C_GetFlagStatus()) followed by writing the
00744   *                         second byte of the address in DR register.
00745   *                       - BTF (Byte Transfer Finished) is cleared by software
00746   *                         sequence: a read operation to I2C_SR1 register
00747   *                         (I2C_GetFlagStatus()) followed by a read/write to
00748   *                         I2C_DR register (I2C_SendData()).
00749   *                       - ADDR (Address sent) is cleared by software sequence:
00750   *                         a read operation to I2C_SR1 register
00751   *                         (I2C_GetFlagStatus()) followed by a read operation to
00752   *                         I2C_SR3 register ((void)(I2C->SR3)).
00753   *                       - SB (Start Bit) is cleared software sequence: a read
00754   *                         operation to I2C_SR1 register (I2C_GetFlagStatus())
00755   *                         followed by a write operation to I2C_DR register
00756   *                         (I2C_SendData()).
00757   * @retval None
00758   */
00759 void I2C_ClearFlag(I2C_Flag_TypeDef I2C_FLAG)
00760 {
00761   uint16_t flagpos = 0;
00762   /* Check the parameters */
00763   assert_param(IS_I2C_CLEAR_FLAG_OK(I2C_FLAG));
00764 
00765   /* Get the I2C flag position */
00766   flagpos = (uint16_t)I2C_FLAG & FLAG_Mask;
00767   /* Clear the selected I2C flag */
00768   I2C->SR2 = (uint8_t)((uint16_t)(~flagpos));
00769 }
00770 
00771 /**
00772   * @brief  Checks whether the specified I2C interrupt has occurred or not.
00773   * @param  I2C_ITPendingBit: specifies the interrupt source to check.
00774   *            This parameter can be one of the following values:
00775   *               - I2C_ITPENDINGBIT_WAKEUPFROMHALT: Wakeup from Halt
00776   *               - I2C_ITPENDINGBIT_OVERRUNUNDERRUN: Overrun/Underrun flag (Slave mode)
00777   *               - I2C_ITPENDINGBIT_ACKNOWLEDGEFAILURE: Acknowledge failure flag
00778   *               - I2C_ITPENDINGBIT_ARBITRATIONLOSS: Arbitration lost flag (Master mode)
00779   *               - I2C_ITPENDINGBIT_BUSERROR: Bus error flag
00780   *               - I2C_ITPENDINGBIT_TXEMPTY: Data register empty flag (Transmitter)
00781   *               - I2C_ITPENDINGBIT_RXNOTEMPTY: Data register not empty (Receiver) flag
00782   *               - I2C_ITPENDINGBIT_STOPDETECTION: Stop detection flag (Slave mode)
00783   *               - I2C_ITPENDINGBIT_HEADERSENT: 10-bit header sent flag (Master mode)
00784   *               - I2C_ITPENDINGBIT_TRANSFERFINISHED: Byte transfer finished flag
00785   *               - I2C_ITPENDINGBIT_ADDRESSSENTMATCHED: Address sent flag (Master mode) �ADSL�
00786   *                              Address matched flag (Slave mode)�ENDAD�
00787   *               - I2C_ITPENDINGBIT_STARTDETECTION: Start bit flag (Master mode)
00788   * @retval The new state of I2C_ITPendingBit
00789   *   This parameter can be any of the @ref ITStatus enumeration.
00790   */
00791 ITStatus I2C_GetITStatus(I2C_ITPendingBit_TypeDef I2C_ITPendingBit)
00792 {
00793   ITStatus bitstatus = RESET;
00794   __IO uint8_t enablestatus = 0;
00795   uint16_t tempregister = 0;
00796 
00797     /* Check the parameters */
00798     assert_param(IS_I2C_ITPENDINGBIT_OK(I2C_ITPendingBit));
00799 
00800   tempregister = (uint8_t)( ((uint16_t)((uint16_t)I2C_ITPendingBit & ITEN_Mask)) >> 8);
00801 
00802   /* Check if the interrupt source is enabled or not */
00803   enablestatus = (uint8_t)(I2C->ITR & ( uint8_t)tempregister);
00804 
00805   if ((uint16_t)((uint16_t)I2C_ITPendingBit & REGISTER_Mask) == REGISTER_SR1_Index)
00806   {
00807     /* Check the status of the specified I2C flag */
00808     if (((I2C->SR1 & (uint8_t)I2C_ITPendingBit) != RESET) && enablestatus)
00809     {
00810       /* I2C_IT is set */
00811       bitstatus = SET;
00812     }
00813     else
00814     {
00815       /* I2C_IT is reset */
00816       bitstatus = RESET;
00817     }
00818   }
00819   else
00820   {
00821     /* Check the status of the specified I2C flag */
00822     if (((I2C->SR2 & (uint8_t)I2C_ITPendingBit) != RESET) && enablestatus)
00823     {
00824       /* I2C_IT is set */
00825       bitstatus = SET;
00826     }
00827     else
00828     {
00829       /* I2C_IT is reset */
00830       bitstatus = RESET;
00831     }
00832   }
00833   /* Return the I2C_IT status */
00834   return  bitstatus;
00835 }
00836 
00837 /**
00838   * @brief  Clear IT pending bit
00839   * @param  I2C_ITPendingBit : specifies the interrupt pending bit to clear.
00840   *            This parameter can be any combination of the following values:
00841   *                 - I2C_ITPENDINGBIT_WAKEUPFROMHALT: Wakeup from Halt
00842   *                 - I2C_ITPENDINGBIT_OVERRUNUNDERRUN: Overrun/Underrun interrupt (Slave mode)
00843   *                 - I2C_ITPENDINGBIT_ACKNOWLEDGEFAILURE: Acknowledge failure interrupt
00844   *                 - I2C_ITPENDINGBIT_ARBITRATIONLOSS: Arbitration lost interrupt (Master mode)
00845   *                 - I2C_ITPENDINGBIT_BUSERROR: Bus error interrupt
00846   *
00847   *             Notes:
00848   *                  - STOPF (STOP detection) is cleared by software
00849   *                    sequence: a read operation to I2C_SR1 register
00850   *                    (I2C_GetITStatus()) followed by a write operation to
00851   *                    I2C_CR2 register (I2C_AcknowledgeConfig() to configure
00852   *                    the I2C peripheral Acknowledge).
00853   *                  - ADD10 (10-bit header sent) is cleared by software
00854   *                    sequence: a read operation to I2C_SR1
00855   *                    (I2C_GetITStatus()) followed by writing the second
00856   *                    byte of the address in I2C_DR register.
00857   *                  - BTF (Byte Transfer Finished) is cleared by software
00858   *                    sequence: a read operation to I2C_SR1 register
00859   *                    (I2C_GetITStatus()) followed by a read/write to
00860   *                    I2C_DR register (I2C_SendData()).
00861   *                  - ADDR (Address sent) is cleared by software sequence:
00862   *                    a read operation to I2C_SR1 register (I2C_GetITStatus())
00863   *                    followed by a read operation to I2C_SR3 register
00864   *                    ((void)(I2C->SR3)).
00865   *                  - SB (Start Bit) is cleared by software sequence: a
00866   *                    read operation to I2C_SR1 register (I2C_GetITStatus())
00867   *                    followed by a write operation to I2C_DR register
00868   *                    (I2C_SendData()).
00869   * @retval None
00870   */
00871 void I2C_ClearITPendingBit(I2C_ITPendingBit_TypeDef I2C_ITPendingBit)
00872 {
00873   uint16_t flagpos = 0;
00874 
00875   /* Check the parameters */
00876   assert_param(IS_I2C_CLEAR_ITPENDINGBIT_OK(I2C_ITPendingBit));
00877 
00878   /* Get the I2C flag position */
00879   flagpos = (uint16_t)I2C_ITPendingBit & FLAG_Mask;
00880 
00881   /* Clear the selected I2C flag */
00882   I2C->SR2 = (uint8_t)((uint16_t)~flagpos);
00883 }
00884 
00885 /**
00886   * @}
00887   */
00888 
00889 /**
00890   * @}
00891   */
00892 
00893 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com