diff --git a/flight/pios/common/pios_ms4525do.c b/flight/pios/common/pios_ms4525do.c index 8ad7ce093..10d124a2d 100644 --- a/flight/pios/common/pios_ms4525do.c +++ b/flight/pios/common/pios_ms4525do.c @@ -60,6 +60,7 @@ int8_t PIOS_MS4525DO_Read(uint16_t *values) int8_t retVal = PIOS_MS4525DO_ReadI2C(data, sizeof(data)); uint8_t status = data[0] & 0xC0; + if (status == 0x80) { /* stale data */ return -5; diff --git a/flight/pios/inc/pios_i2c_priv.h b/flight/pios/inc/pios_i2c_priv.h index b2f340c83..78c9cd0e7 100644 --- a/flight/pios/inc/pios_i2c_priv.h +++ b/flight/pios/inc/pios_i2c_priv.h @@ -92,12 +92,12 @@ struct pios_i2c_adapter { uint8_t busy; #endif - /* variables for transfer timeouts */ - uint32_t transfer_delay_uS; // approx time to transfer one byte, calculated later basen on setting use here time based on 100 kbits/s - uint32_t transfer_timeout_ticks; // take something tha makes sense for small transaction, calculated later based upon transmission desired - - bool bus_error; - bool nack; + /* variables for transfer timeouts */ + uint32_t transfer_delay_uS; // approx time to transfer one byte, calculated later basen on setting use here time based on 100 kbits/s + uint32_t transfer_timeout_ticks; // take something tha makes sense for small transaction, calculated later based upon transmission desired + + bool bus_error; + bool nack; volatile enum i2c_adapter_state curr_state; const struct pios_i2c_txn *first_txn; diff --git a/flight/pios/stm32f4xx/pios_i2c.c b/flight/pios/stm32f4xx/pios_i2c.c index 587afe966..eef03be79 100644 --- a/flight/pios/stm32f4xx/pios_i2c.c +++ b/flight/pios/stm32f4xx/pios_i2c.c @@ -756,9 +756,9 @@ static void i2c_adapter_reset_bus(struct pios_i2c_adapter *i2c_adapter) // for delays during transfer timeouts // one tick correspond to transmission of 1 byte i.e. 9 clock ticks - i2c_adapter->transfer_delay_uS=9000000/(((I2C_InitTypeDef *)&(i2c_adapter->cfg->init))->I2C_ClockSpeed); + i2c_adapter->transfer_delay_uS = 9000000 / (((I2C_InitTypeDef *)&(i2c_adapter->cfg->init))->I2C_ClockSpeed); + - if (i2c_adapter->cfg->regs->SR2 & I2C_FLAG_BUSY) { /* Reset the I2C block */ I2C_SoftwareResetCmd(i2c_adapter->cfg->regs, ENABLE); @@ -796,13 +796,15 @@ static bool i2c_adapter_callback_handler(struct pios_i2c_adapter *i2c_adapter) /* transfer_timeout_ticks is set by PIOS_I2C_Transfer_Callback */ /* Spin waiting for the transfer to finish */ while (!i2c_adapter_fsm_terminated(i2c_adapter)) { - //sleep 1 byte, as FSM can't be faster + // sleep 1 byte, as FSM can't be faster // FIXME: clock delay could make problems, but citing NPX: alsmost no slave device implements clock delay // three times the expected time should cover clock delay PIOS_DELAY_WaituS(i2c_adapter->transfer_delay_uS); - + i2c_adapter->transfer_timeout_ticks--; - if(i2c_adapter->transfer_timeout_ticks==0)break; + if (i2c_adapter->transfer_timeout_ticks == 0) { + break; + } } if (i2c_adapter_wait_for_stopped(i2c_adapter)) { @@ -982,8 +984,10 @@ int32_t PIOS_I2C_Transfer(uint32_t i2c_id, const struct pios_i2c_txn txn_list[], bool valid = PIOS_I2C_validate(i2c_adapter); - if(!valid)return -1; - + if (!valid) { + return -1; + } + PIOS_Assert(valid) PIOS_DEBUG_Assert(txn_list); @@ -1016,21 +1020,21 @@ int32_t PIOS_I2C_Transfer(uint32_t i2c_id, const struct pios_i2c_txn txn_list[], #ifdef USE_FREERTOS /* Make sure the done/ready semaphore is consumed before we start */ - semaphore_success &= (xSemaphoreTake(i2c_adapter->sem_ready, timeout) == pdTRUE); + semaphore_success &= (xSemaphoreTake(i2c_adapter->sem_ready, timeout) == pdTRUE); #endif // used in the i2c_adapter_callback_handler function // Estimate bytes of transmission. Per txns: 1 adress byte + length - i2c_adapter->transfer_timeout_ticks= num_txns; - for(uint32_t i=0;itransfer_timeout_ticks = num_txns; + for (uint32_t i = 0; i < num_txns; i++) { i2c_adapter->transfer_timeout_ticks += txn_list[i].len; } // timeout if it takes three times the expected time - i2c_adapter->transfer_timeout_ticks*=3; - + i2c_adapter->transfer_timeout_ticks *= 3; + i2c_adapter->callback = NULL; i2c_adapter->bus_error = false; - i2c_adapter->nack = false; + i2c_adapter->nack = false; i2c_adapter_inject_event(i2c_adapter, I2C_EVENT_START); /* Wait for the transfer to complete */ @@ -1041,15 +1045,17 @@ int32_t PIOS_I2C_Transfer(uint32_t i2c_id, const struct pios_i2c_txn txn_list[], /* Spin waiting for the transfer to finish */ while (!i2c_adapter_fsm_terminated(i2c_adapter)) { - /* sleep 9 clock ticks (1 byte), because FSM can't be faster than one byte + /* sleep 9 clock ticks (1 byte), because FSM can't be faster than one byte FIXME: clock delay could make problems, but citing NPX: alsmost no slave device implements clock delay three times the expected time should cover clock delay */ PIOS_DELAY_WaituS(i2c_adapter->transfer_delay_uS); - + i2c_adapter->transfer_timeout_ticks--; - if(i2c_adapter->transfer_timeout_ticks==0)break; + if (i2c_adapter->transfer_timeout_ticks == 0) { + break; + } } - + if (i2c_adapter_wait_for_stopped(i2c_adapter)) { i2c_adapter_inject_event(i2c_adapter, I2C_EVENT_STOPPED); } else { @@ -1080,7 +1086,9 @@ int32_t PIOS_I2C_Transfer_Callback(uint32_t i2c_id, const struct pios_i2c_txn tx bool valid = PIOS_I2C_validate(i2c_adapter); - if(!valid)return -1; + if (!valid) { + return -1; + } PIOS_Assert(valid) PIOS_Assert(callback); @@ -1112,18 +1120,18 @@ int32_t PIOS_I2C_Transfer_Callback(uint32_t i2c_id, const struct pios_i2c_txn tx #ifdef USE_FREERTOS /* Make sure the done/ready semaphore is consumed before we start */ - semaphore_success &= (xSemaphoreTake(i2c_adapter->sem_ready, timeout) == pdTRUE); + semaphore_success &= (xSemaphoreTake(i2c_adapter->sem_ready, timeout) == pdTRUE); #endif // used in the i2c_adapter_callback_handler function // Estimate bytes of transmission. Per txns: 1 adress byte + length - i2c_adapter->transfer_timeout_ticks= num_txns; - for(uint32_t i=0;itransfer_timeout_ticks = num_txns; + for (uint32_t i = 0; i < num_txns; i++) { i2c_adapter->transfer_timeout_ticks += txn_list[i].len; } // timeout if it takes three times the expected time - i2c_adapter->transfer_timeout_ticks*=3; - + i2c_adapter->transfer_timeout_ticks *= 3; + i2c_adapter->callback = callback; i2c_adapter->bus_error = false; i2c_adapter_inject_event(i2c_adapter, I2C_EVENT_START); @@ -1137,7 +1145,9 @@ void PIOS_I2C_EV_IRQ_Handler(uint32_t i2c_id) bool valid = PIOS_I2C_validate(i2c_adapter); - if(!valid)return; + if (!valid) { + return; + } PIOS_Assert(valid) @@ -1272,8 +1282,10 @@ void PIOS_I2C_ER_IRQ_Handler(uint32_t i2c_id) bool valid = PIOS_I2C_validate(i2c_adapter); - if(!valid)return; - + if (!valid) { + return; + } + PIOS_Assert(valid) uint32_t event = I2C_GetLastEvent(i2c_adapter->cfg->regs);