|
@@ -17,25 +17,6 @@ static i2c_status_t st_Read = I2C_Ret_OK;
|
|
|
/* Private function prototypes */
|
|
|
static i2c_status_t i2c_check_err(void);
|
|
|
|
|
|
-static inline void i2c_start(void) {
|
|
|
- // Send 'Start' condition, and wait for acknowledge.
|
|
|
- I2C1->CR2 |= (I2C_CR2_START);
|
|
|
- while ((I2C1->CR2 & I2C_CR2_START)) {}
|
|
|
-}
|
|
|
-
|
|
|
-static inline void i2c_stop(void) {
|
|
|
- // Send 'Stop' condition, and wait for acknowledge.
|
|
|
- I2C1->CR2 |= (I2C_CR2_STOP);
|
|
|
- while ((I2C1->CR2 & I2C_CR2_STOP)) {}
|
|
|
- // Reset the ICR ('Interrupt Clear Register') event flag.
|
|
|
- I2C1->ICR |= (I2C_ICR_STOPCF);
|
|
|
- while ((I2C1->ICR & I2C_ICR_STOPCF)) {}
|
|
|
-}
|
|
|
-
|
|
|
-static void i2c_write_byte(uint8_t dat);
|
|
|
-static uint8_t i2c_read_byte(void);
|
|
|
-static uint8_t i2c_read_register(uint8_t reg_addr);
|
|
|
-
|
|
|
/**
|
|
|
* @brief Check I2C bus for errors.
|
|
|
* @retval I2C return code
|
|
@@ -81,7 +62,8 @@ i2c_status_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *da
|
|
|
/* prepare i2c for sending reg addr */
|
|
|
I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
|
|
|
I2C1->CR2 |= (id | (1 << I2C_CR2_NBYTES_Pos));
|
|
|
- i2c_start();
|
|
|
+ I2C1->CR2 |= (I2C_CR2_START);
|
|
|
+ while ((I2C1->CR2 & I2C_CR2_START)) {}
|
|
|
|
|
|
/* wait for byte request or any error */
|
|
|
while ((I2C1->ISR & (I2C_ISR_TIMEOUT | I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) {
|
|
@@ -111,7 +93,8 @@ i2c_status_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *da
|
|
|
I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
|
|
|
I2C1->CR2 |= (id | (len << I2C_CR2_NBYTES_Pos) | I2C_CR2_RD_WRN);
|
|
|
/* launch receiving */
|
|
|
- i2c_start();
|
|
|
+ I2C1->CR2 |= (I2C_CR2_START);
|
|
|
+ while ((I2C1->CR2 & I2C_CR2_START)) {}
|
|
|
|
|
|
/* receiving data */
|
|
|
while (len) {
|
|
@@ -146,12 +129,10 @@ i2c_status_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *d
|
|
|
|
|
|
I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
|
|
|
I2C1->CR2 |= (id | ((len + 1) << I2C_CR2_NBYTES_Pos ));
|
|
|
- i2c_start();
|
|
|
-
|
|
|
- while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) {
|
|
|
- __NOP();
|
|
|
- }
|
|
|
+ I2C1->CR2 |= (I2C_CR2_START);
|
|
|
+ while ((I2C1->CR2 & I2C_CR2_START)) {}
|
|
|
|
|
|
+ while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); }
|
|
|
if ((I2C1->ISR & I2C_ISR_TXE) != 0) {
|
|
|
I2C1->TXDR = reg_addr;
|
|
|
} else {
|
|
@@ -178,47 +159,3 @@ i2c_status_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *d
|
|
|
Flag.I2C_TX_End = 1;
|
|
|
return r;
|
|
|
}
|
|
|
-
|
|
|
-static void i2c_write_byte(uint8_t dat) {
|
|
|
- I2C1->TXDR = (I2C1->TXDR & 0xFFFFFF00) | dat;
|
|
|
- // Wait for one of these ISR bits:
|
|
|
- // 'TXIS' ("ready for next byte")
|
|
|
- // 'TC' ("transfer complete")
|
|
|
- while (!(I2C1->ISR & (I2C_ISR_TXIS | I2C_ISR_TC))) {}
|
|
|
- // (Also of interest: 'TXE' ("TXDR register is empty") and
|
|
|
- // 'TCR' ("transfer complete, and 'RELOAD' is set."))
|
|
|
-}
|
|
|
-
|
|
|
-static uint8_t i2c_read_byte(void) {
|
|
|
- // Wait for a byte of data to be available, then read it.
|
|
|
- while (!(I2C1->ISR & I2C_ISR_RXNE)) {}
|
|
|
- return (I2C1->RXDR & 0xFF);
|
|
|
-}
|
|
|
-
|
|
|
-static uint8_t i2c_read_register(uint8_t reg_addr) {
|
|
|
- // Set '1 byte to send.'
|
|
|
- I2C1->CR2 &= ~(I2C_CR2_NBYTES);
|
|
|
- I2C1->CR2 |= (0x01 << I2C_CR2_NBYTES_Pos);
|
|
|
- // Start the I2C write transmission.
|
|
|
- i2c_start();
|
|
|
- // Send the register address.
|
|
|
- i2c_write_byte(reg_addr);
|
|
|
- // Stop the I2C write transmission.
|
|
|
- i2c_stop();
|
|
|
- // Set '1 byte to receive.'
|
|
|
- I2C1->CR2 &= ~(I2C_CR2_NBYTES);
|
|
|
- I2C1->CR2 |= (0x01 << I2C_CR2_NBYTES_Pos);
|
|
|
- // Set 'read' I2C direction.
|
|
|
- I2C1->CR2 |= (I2C_CR2_RD_WRN);
|
|
|
- // Start the I2C read transmission.
|
|
|
- i2c_start();
|
|
|
- // Read the transmitted data.
|
|
|
- uint8_t read_result = i2c_read_byte();
|
|
|
- // Stop the I2C read transmission.
|
|
|
- i2c_stop();
|
|
|
- // Set 'write' I2C direction again.
|
|
|
- I2C1->CR2 &= ~(I2C_CR2_RD_WRN);
|
|
|
-
|
|
|
- // Return the read value.
|
|
|
- return read_result;
|
|
|
-}
|