/*! * @file apm32f0xx_i2c.h * * @brief This file contains all the functions prototypes for the I2C firmware library * * @version V1.0.1 * * @date 2021-07-01 * */ #ifndef __I2C_H #define __I2C_H #include "apm32f0xx.h" #ifdef __cplusplus extern "C" { #endif /** @addtogroup Peripherals_Library Standard Peripheral Library @{ */ /** @addtogroup I2C_Driver I2C Driver @{ */ /** @addtogroup I2C_Enumerations Enumerations @{ */ /** * @brief I2C Analog noise filter */ typedef enum { I2C_ANALOG_FILTER_ENABLE = ((uint8_t)0), //!< Analog noise filter enabled I2C_ANALOG_FILTER_DISABLE = ((uint8_t)1), //!< Analog noise filter disabled } I2C_ANALOG_FILTER_T; /** * @brief I2C Digital noise filter */ typedef enum { I2C_DIGITAL_FILTER_0 = ((uint8_t)0x00), //!< Digital filter disabled I2C_DIGITAL_FILTER_1 = ((uint8_t)0x01), //!< Digital filter enabled and filtering capability is 1 T_i2cclk I2C_DIGITAL_FILTER_2 = ((uint8_t)0x02), //!< Digital filter enabled and filtering capability is 3 T_i2cclk I2C_DIGITAL_FILTER_3 = ((uint8_t)0x03), //!< Digital filter enabled and filtering capability is 3 T_i2cclk I2C_DIGITAL_FILTER_4 = ((uint8_t)0x04), //!< Digital filter enabled and filtering capability is 4 T_i2cclk I2C_DIGITAL_FILTER_5 = ((uint8_t)0x05), //!< Digital filter enabled and filtering capability is 5 T_i2cclk I2C_DIGITAL_FILTER_6 = ((uint8_t)0x06), //!< Digital filter enabled and filtering capability is 6 T_i2cclk I2C_DIGITAL_FILTER_7 = ((uint8_t)0x07), //!< Digital filter enabled and filtering capability is 7 T_i2cclk I2C_DIGITAL_FILTER_8 = ((uint8_t)0x08), //!< Digital filter enabled and filtering capability is 8 T_i2cclk I2C_DIGITAL_FILTER_9 = ((uint8_t)0x09), //!< Digital filter enabled and filtering capability is 9 T_i2cclk I2C_DIGITAL_FILTER_10 = ((uint8_t)0x0A), //!< Digital filter enabled and filtering capability is 10 T_i2cclk I2C_DIGITAL_FILTER_11 = ((uint8_t)0x0B), //!< Digital filter enabled and filtering capability is 11 T_i2cclk I2C_DIGITAL_FILTER_12 = ((uint8_t)0x0C), //!< Digital filter enabled and filtering capability is 12 T_i2cclk I2C_DIGITAL_FILTER_13 = ((uint8_t)0x0D), //!< Digital filter enabled and filtering capability is 13 T_i2cclk I2C_DIGITAL_FILTER_14 = ((uint8_t)0x0E), //!< Digital filter enabled and filtering capability is 14 T_i2cclk I2C_DIGITAL_FILTER_15 = ((uint8_t)0x0F), //!< Digital filter enabled and filtering capability is 15 T_i2cclk } I2C_DIGITAL_FILTER_T; /** * @brief I2C mode */ typedef enum { I2C_MODE_I2C = ((uint32_t)0x00000000), I2C_MODE_SMBUSDEVICE = ((uint32_t)0x00100000), //!< SMBus Device Default address enable I2C_MODE_SMBUSHOST = ((uint32_t)0x00200000), //!< SMBus Host address enable } I2C_MODE_T; /** * @brief I2C acknowledge */ typedef enum { I2C_ACK_ENABLE = ((uint8_t)0), //!< ACK generation (slave mode) I2C_ACK_DISABLE = ((uint8_t)1), //!< NACK generation (slave mode) } I2C_ACK_T; /** * @brief I2C acknowledge address */ typedef enum { I2C_ACK_ADDRESS_7BIT = ((uint8_t)0), //!< ACK generation (slave mode) I2C_ACK_ADDRESS_10BIT = ((uint8_t)1), //!< NACK generation (slave mode) } I2C_ACK_ADDRESS_T; /** * @brief I2C transfer direction */ typedef enum { I2C_DIRECTION_TX = ((uint8_t)0), //!< Transmission direction I2C_DIRECTION_RX = ((uint8_t)1), //!< Reception direction } I2C_DIRECTION_T; /** * @brief I2C DMA requests */ typedef enum { I2C_DMA_REQ_TX = ((uint8_t)0), //!< DMA transmission requests enable I2C_DMA_REQ_RX = ((uint8_t)1), //!< DMA reception requests enable } I2C_DMA_REQ_T; /** * @brief I2C own address2 mask */ typedef enum { I2C_ADDR2MSK_NOMASK = ((uint8_t)0x00), //!< No masked I2C_ADDR2MSK_MASK01 = ((uint8_t)0x01), //!< Don't care masked ADDR2[1:0] I2C_ADDR2MSK_MASK02 = ((uint8_t)0x02), //!< Don't care masked ADDR2[2:1] I2C_ADDR2MSK_MASK03 = ((uint8_t)0x03), //!< Don't care masked ADDR2[3:1] I2C_ADDR2MSK_MASK04 = ((uint8_t)0x04), //!< Don't care masked ADDR2[4:1] I2C_ADDR2MSK_MASK05 = ((uint8_t)0x05), //!< Don't care masked ADDR2[5:1] I2C_ADDR2MSK_MASK06 = ((uint8_t)0x06), //!< Don't care masked ADDR2[6:1] I2C_ADDR2MSK_MASK07 = ((uint8_t)0x07), //!< Don't care masked ADDR2[7:1] } I2C_ADDR2MSK_T; /** * @brief I2C registers */ typedef enum { I2C_REGISTER_CTRL1 = ((uint8_t)0x00), //!< CTRL1 register I2C_REGISTER_CTRL2 = ((uint8_t)0x04), //!< CTRL2 register I2C_REGISTER_ADDR1 = ((uint8_t)0x08), //!< ADDR1 register I2C_REGISTER_ADDR2 = ((uint8_t)0x0C), //!< ADDR2 register I2C_REGISTER_TIMING = ((uint8_t)0x10), //!< TIMING register I2C_REGISTER_TIMEOUT = ((uint8_t)0x14), //!< TIMEOUT register I2C_REGISTER_STS = ((uint8_t)0x18), //!< STS register I2C_REGISTER_INTFCLR = ((uint8_t)0x1C), //!< INTFCLR register I2C_REGISTER_PEC = ((uint8_t)0x20), //!< PEC register I2C_REGISTER_RXDATA = ((uint8_t)0x24), //!< RXDATA register I2C_REGISTER_TXDATA = ((uint8_t)0x28), //!< TXDATA register } I2C_REGISTER_T; /** * @brief I2C interrupts source */ typedef enum { I2C_INT_TXIE = ((uint8_t)0x02), //!< TX Interrupt enable I2C_INT_RXIE = ((uint8_t)0x04), //!< RX Interrupt enable I2C_INT_ADDRIE = ((uint8_t)0x08), //!< Address match interrupt enable (slave only) I2C_INT_NACKIE = ((uint8_t)0x10), //!< Not acknowledge received interrupt enable I2C_INT_STOPIE = ((uint8_t)0x20), //!< STOP detection Interrupt enable I2C_INT_TXCIE = ((uint8_t)0x40), //!< Transfer complete interrupt enable I2C_INT_ERRIE = ((uint8_t)0x80), //!< Error interrupts enable } I2C_INT_T; /** * @brief I2C Flags */ typedef enum { /** STS1 register flag */ I2C_FLAG_TXBE = ((uint32_t)0x0001), //!< Transmit buffer data register empty flag I2C_FLAG_TXINT = ((uint32_t)0x0002), //!< Transmit interrupt flag I2C_FLAG_RXBNE = ((uint32_t)0x0004), //!< Read buffer data register not empty flag I2C_FLAG_ADDR = ((uint32_t)0x0008), //!< Address Sent/Matched (master/slave) flag I2C_FLAG_NACK = ((uint32_t)0x0010), //!< Not acknowledge received flag I2C_FLAG_STOP = ((uint32_t)0x0020), //!< Stop detected flag I2C_FLAG_TXCF = ((uint32_t)0x0040), //!< Transfer complete flag I2C_FLAG_TCRF = ((uint32_t)0x0080), //!< Transfer complete reload flag I2C_FLAG_BUSERR = ((uint32_t)0x0100), //!< Bus error flag I2C_FLAG_ALF = ((uint32_t)0x0200), //!< Arbitration Loss flag I2C_FLAG_OVR = ((uint32_t)0x0400), //!< Overrun/Underrun flag I2C_FLAG_PECERR = ((uint32_t)0x0800), //!< PEC error flag I2C_FLAG_TIMEOUT = ((uint32_t)0x1000), //!< Timeout or t_low detection flag I2C_FLAG_ALERT = ((uint32_t)0x2000), //!< SMBus alert flag I2C_FLAG_BUSY = ((uint32_t)0x8000), //!< Bus Busy Flag } I2C_FLAG_T; /** * @brief I2C Interrupt flag */ typedef enum { I2C_INT_FLAG_TXINT = ((uint32_t)0x0002), //!< Transmit interrupt flag I2C_INT_FLAG_RXBNE = ((uint32_t)0x0004), //!< Read Buffer Data Register Not Empty interrupt flag I2C_INT_FLAG_ADDR = ((uint32_t)0x0008), //!< Address Sent/Matched (master/slave) interrupt flag I2C_INT_FLAG_NACK = ((uint32_t)0x0010), //!< Not acknowledge received interrupt flag I2C_INT_FLAG_STOP = ((uint32_t)0x0020), //!< Stop detected interrupt flag I2C_INT_FLAG_TXCF = ((uint32_t)0x0040), //!< Transfer complete interrupt flag I2C_INT_FLAG_TCRF = ((uint32_t)0x0080), //!< Transfer Complete Reloadinterrupt flag I2C_INT_FLAG_BUSERR = ((uint32_t)0x0100), //!< Bus error interrupt flag I2C_INT_FLAG_ALF = ((uint32_t)0x0200), //!< Arbitration Loss interrupt flag I2C_INT_FLAG_OVR = ((uint32_t)0x0400), //!< Overrun/Underrun interrupt flag I2C_INT_FLAG_PECERR = ((uint32_t)0x0800), //!< PEC error interrupt flag I2C_INT_FLAG_TIMEOUT = ((uint32_t)0x1000), //!< Timeout or t_low detection interrupt flag I2C_INT_FLAG_ALERT = ((uint32_t)0x2000), //!< SMBus alert interrupt flag } I2C_INT_FLAG_T; /** * @brief I2C Reload End Mode */ typedef enum { I2C_RELOAD_MODE_RELOAD = ((uint32_t)0x01000000), //!< Enable Reload mode I2C_RELOAD_MODE_AUTOEND = ((uint32_t)0x02000000), //!< Enable Automatic end mode I2C_RELOAD_MODE_SOFTEND = ((uint32_t)0x00000000), //!< Enable Software end mode } I2C_RELOAD_MODE_T; /** * @brief I2C Start/Stop Mode */ typedef enum { I2C_GENERATE_NO_STARTSTOP = ((uint32_t)0x00000000), //!< Don't Generate stop and start condition. I2C_GENERATE_START_WRITE = ((uint32_t)0x00002000), //!< Generate Restart for write request I2C_GENERATE_STOP = ((uint32_t)0x00004000), //!< Generate stop condition I2C_GENERATE_START_READ = ((uint32_t)0x00002400), //!< Generate Restart for read request } I2C_GENERATE_T; /**@} end of group I2C_Enumerations*/ /** @addtogroup I2C_Macros Macros @{ */ #define I2C_CTRL2_SADD ((uint32_t)0x000003FF) //