add sanity check
This commit is contained in:
parent
9c5c535e85
commit
e26b2108ca
19
lib/i2c.c
19
lib/i2c.c
|
@ -16,7 +16,7 @@
|
|||
* @file i2c.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: I2C @ref i2c_i2c, timer @i2c_timer
|
||||
* @note peripherals used: I2C @ref i2c_i2c, timer @ref i2c_timer
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
|
@ -98,13 +98,18 @@ void i2c_master(bool fast)
|
|||
bool i2c_read(uint8_t slave, const uint8_t* address, size_t address_size, uint8_t* data, size_t data_size)
|
||||
{
|
||||
// sanity check
|
||||
if (address==NULL || address_size==0 || data==NULL || data_size==0) {
|
||||
if (address==NULL || address_size==0 || data==NULL || data_size==0) { // input data is erroneous
|
||||
return false;
|
||||
}
|
||||
if (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_BUSY) { // I2C device is busy
|
||||
return false;
|
||||
}
|
||||
if (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL) { // I2C device is already in master mode
|
||||
return false;
|
||||
}
|
||||
|
||||
bool to_return = false; // return if read succeeded
|
||||
|
||||
|
||||
// send start condition
|
||||
i2c_send_start(I2C(I2C_I2C)); // send start condition to start transaction
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
|
@ -216,7 +221,13 @@ error:
|
|||
bool i2c_write(uint8_t slave, const uint8_t* address, size_t address_size, const uint8_t* data, size_t data_size)
|
||||
{
|
||||
// sanity check
|
||||
if (address==NULL || address_size==0 || data==NULL || data_size==0) {
|
||||
if (address==NULL || address_size==0 || data==NULL || data_size==0) { // input data is erroneous
|
||||
return false;
|
||||
}
|
||||
if (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_BUSY) { // I2C device is busy
|
||||
return false;
|
||||
}
|
||||
if (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL) { // I2C device is already in master mode
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue