2014-08-30 00:19:16 +08:00
|
|
|
/**
|
2023-04-05 11:26:18 +08:00
|
|
|
*****************************************************************************
|
|
|
|
* @file cmem7_i2c.h
|
|
|
|
*
|
|
|
|
* @brief CMEM7 I2C header file
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @version V1.0
|
|
|
|
* @date 3. September 2013
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
|
|
|
* @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, CAPITAL-MICRO 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 2013 Capital-micro </center></h2>
|
|
|
|
*****************************************************************************
|
|
|
|
*/
|
|
|
|
|
2014-08-30 00:19:16 +08:00
|
|
|
#ifndef __CMEM7_I2C_H
|
|
|
|
#define __CMEM7_I2C_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cmem7.h"
|
|
|
|
#include "cmem7_conf.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C0) || \
|
|
|
|
((PERIPH) == I2C1))
|
|
|
|
|
|
|
|
/** @defgroup I2C_Mode
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#define I2C_Mode_Slave 0
|
|
|
|
#define I2C_Mode_Master 1
|
|
|
|
#define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_Slave) || \
|
|
|
|
((MODE) == I2C_Mode_Master))
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @defgroup I2C_ADDR_WIDTH
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#define I2C_ADDR_WIDTH_7BIT 0
|
|
|
|
#define I2C_ADDR_WIDTH_10BIT 1
|
|
|
|
#define IS_I2C_ADDR_WIDTH(WIDTH) (((WIDTH) == I2C_ADDR_WIDTH_7BIT) || \
|
|
|
|
((WIDTH) == I2C_ADDR_WIDTH_10BIT))
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @defgroup I2C_INT
|
|
|
|
* @{
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
|
|
|
#define I2C_INT_RX_FIFO_NOT_EMPTY 0x00000004 /*!< Can't be clear but read FIFO */
|
2014-08-30 00:19:16 +08:00
|
|
|
#define I2C_INT_RD_REQUEST 0x00000020 /*!< Slave was requested to send data */
|
|
|
|
#define I2C_INT_TX_ABORT 0x00000040 /*!< Error while sending data */
|
|
|
|
#define I2C_INT_RX_DONE 0x00000080 /*!< Slave sent all requested data */
|
|
|
|
#define I2C_INT_TX_DONE 0x00000100 /*!< Master accomplish to send all data */
|
|
|
|
|
|
|
|
#define I2C_INT_ALL (I2C_INT_RX_FIFO_NOT_EMPTY | \
|
2023-04-05 11:26:18 +08:00
|
|
|
I2C_INT_RD_REQUEST | \
|
|
|
|
I2C_INT_TX_ABORT | \
|
|
|
|
I2C_INT_RX_DONE | \
|
|
|
|
I2C_INT_TX_DONE)
|
2014-08-30 00:19:16 +08:00
|
|
|
|
|
|
|
#define IS_I2C_INT(INT) (((INT) != 0) && (((INT) & ~I2C_INT_ALL) == 0))
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @defgroup I2C_STATUS
|
|
|
|
* @{
|
|
|
|
*/
|
2023-04-05 11:26:18 +08:00
|
|
|
#define I2C_STATUS_RX_FIFO_NOT_EMPTY 0x00200000 /*!< Can't be clear but read FIFO */
|
2014-08-30 00:19:16 +08:00
|
|
|
#define I2C_STATUS_RD_REQUEST 0x01000000 /*!< Slave was requested to send data */
|
|
|
|
#define I2C_STATUS_TX_ABORT 0x02000000 /*!< Error while sending data */
|
|
|
|
#define I2C_STATUS_RX_DONE 0x04000000 /*!< Slave sent all requested data */
|
|
|
|
#define I2C_STATUS_TX_DONE 0x08000000 /*!< Master accomplish to send all data */
|
|
|
|
|
|
|
|
#define I2C_STATUS_ALL (I2C_STATUS_RX_FIFO_NOT_EMPTY | \
|
2023-04-05 11:26:18 +08:00
|
|
|
I2C_STATUS_RD_REQUEST | \
|
|
|
|
I2C_STATUS_TX_ABORT | \
|
|
|
|
I2C_STATUS_RX_DONE | \
|
|
|
|
I2C_STATUS_TX_DONE)
|
2014-08-30 00:19:16 +08:00
|
|
|
|
|
|
|
#define IS_I2C_STATUS(STATUS) (((STATUS) != 0) && (((STATUS) & ~I2C_STATUS_ALL) == 0))
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief I2C timing structure
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint32_t I2C_Freq; /*!< I2C frquency */
|
|
|
|
|
2023-04-05 11:26:18 +08:00
|
|
|
uint16_t I2C_TsuDat; /*!< nano second of TSU:DAT */
|
|
|
|
uint16_t I2C_Tsetup; /*!< nano second of THD:STA and TSU:STO */
|
|
|
|
uint16_t I2C_Tbuf; /*!< nano second of TBUF */
|
|
|
|
uint16_t I2C_TsuSta; /*!< nano second of TSU:STA */
|
|
|
|
|
|
|
|
BOOL I2C_SdaFilterEn; /*!< enabled flag of SDA filter */
|
|
|
|
uint8_t I2C_SdaFilterSpike; /*!< spikes of SDA filter */
|
|
|
|
BOOL I2C_SclFilterEn; /*!< enabled flag of SCL filter */
|
|
|
|
uint8_t I2C_SclFilterSpike; /*!< spikes of SCL filter */
|
|
|
|
|
2014-08-30 00:19:16 +08:00
|
|
|
} I2C_InitTimingDef;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief I2C initialization structure
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t I2C_Mode; /*!< Specifies the I2C mode.
|
|
|
|
This parameter can be a value of @ref I2C_mode */
|
2023-04-05 11:26:18 +08:00
|
|
|
uint8_t I2C_AddressWidth; /*!< 7- or 10-bits width address, ref as @ref I2C_ADDR_WIDTH */
|
|
|
|
uint8_t I2C_Address; /*!< 7- or 10-bits address */
|
|
|
|
I2C_InitTimingDef* timing; /*!< timing structure, null if don't set */
|
2014-08-30 00:19:16 +08:00
|
|
|
} I2C_InitTypeDef;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief I2C initialization
|
|
|
|
* @note This function should be called at first before any other interfaces.
|
2023-04-05 11:26:18 +08:00
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] init A pointer to structure I2C_InitTypeDef
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval None
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
void I2C_Init(I2C0_Type* I2Cx, I2C_InitTypeDef* I2C_Init);
|
|
|
|
|
|
|
|
/**
|
2023-04-05 11:26:18 +08:00
|
|
|
* @brief Enable or disable I2C.
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] Enable The bit indicates if the specific I2C is enable or not
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval None
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
void I2C_Enable(I2C0_Type* I2Cx, BOOL enable);
|
|
|
|
|
|
|
|
/**
|
2023-04-05 11:26:18 +08:00
|
|
|
* @brief Enable or disable I2C interrupt.
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] Int interrupt mask bits, which can be the combination of @ref I2C_Int
|
|
|
|
* @param[in] Enable The bit indicates if specific interrupts are enable or not
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval None
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
void I2C_EnableInt(I2C0_Type* I2Cx, uint32_t Int, BOOL enable);
|
|
|
|
|
|
|
|
/**
|
2023-04-05 11:26:18 +08:00
|
|
|
* @brief Check specific interrupts are set or not
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] Int interrupt mask bits, which can be the combination of @ref I2C_Int
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval BOOL The bit indicates if specific interrupts are enable or not
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
BOOL I2C_GetIntStatus(I2C0_Type* I2Cx, uint32_t Int);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Clear specific interrupts
|
2023-04-05 11:26:18 +08:00
|
|
|
* @note Specific interrupt clear will clear correspective status as well
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] Int interrupt mask bits, which can be the combination of @ref I2C_Int
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval None
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
void I2C_ClearInt(I2C0_Type* I2Cx, uint32_t Int);
|
|
|
|
|
|
|
|
/**
|
2023-04-05 11:26:18 +08:00
|
|
|
* @brief Check specific status are set or not
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] Status Status mask bits, which can be the combination of @ref I2C_STATUS
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval BOOL The bit indicates if specific status are set or not
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
BOOL I2C_GetStatus(I2C0_Type* I2Cx, uint32_t Status);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Clear specific status
|
2023-04-05 11:26:18 +08:00
|
|
|
* @note Specific status clear will clear correspective interrupt as well
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] Status Status mask bits, which can be the combination of @ref I2C_STATUS
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval None
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
void I2C_ClearStatus(I2C0_Type* I2Cx, uint32_t Status);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief I2C send read request in master mode
|
2023-04-05 11:26:18 +08:00
|
|
|
* @note Users must call I2C_StopReq between 2 requests
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] size Expected data size to be read
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval BOOL The bit indicates if read request to be sent is valid
|
2023-04-05 11:26:18 +08:00
|
|
|
* @see I2C_StopReq
|
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
BOOL I2C_MasterReadReq(I2C0_Type* I2Cx, uint8_t size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read data from I2C
|
2023-04-05 11:26:18 +08:00
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] size Expected data size to be read
|
|
|
|
* @param[out] Data A user-allocated buffer to fetch data to be read
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval uint8_t Actual read data size
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
uint8_t I2C_ReadFifo(I2C0_Type* I2Cx, uint8_t size, uint8_t* data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief I2C send write request in master or slave mode
|
2023-04-05 11:26:18 +08:00
|
|
|
* @note Users must call I2C_StopReq between 2 requests
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] size Expected data size to be written, includes the first data
|
|
|
|
* @param[in] firstData The first data to be written
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval BOOL The bit indicates if write request to be sent is valid
|
2023-04-05 11:26:18 +08:00
|
|
|
* @see I2C_StopReq
|
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
BOOL I2C_WriteReq(I2C0_Type* I2Cx, uint8_t size, uint8_t firstData);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write data to I2C
|
2023-04-05 11:26:18 +08:00
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @param[in] size Expected data size to be written
|
|
|
|
* @param[in] Data A pointer to the data to be written
|
2014-08-30 00:19:16 +08:00
|
|
|
* @retval uint8_t Actual written data size
|
2023-04-05 11:26:18 +08:00
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
uint8_t I2C_WriteFifo(I2C0_Type* I2Cx, uint8_t size, uint8_t* data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief I2C stop request
|
2023-04-05 11:26:18 +08:00
|
|
|
* @note Users must call I2C_StopReq between 2 requests
|
|
|
|
* @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
|
|
|
|
* @retval BOOL The bit indicates if request is stopped.
|
|
|
|
* @see I2C_MasterReadReq I2C_WriteReq
|
|
|
|
*/
|
2014-08-30 00:19:16 +08:00
|
|
|
BOOL I2C_StopReq(I2C0_Type* I2Cx);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /*__CMEM7_I2C_H */
|
|
|
|
|