From a8257723898d147384b59e5da8d5a7f060028a8f Mon Sep 17 00:00:00 2001 From: Alessio Morale Date: Wed, 17 Apr 2013 23:41:25 +0200 Subject: [PATCH] OP-911: Use SPI_ReleaseBusISR within isr, changed a malloc with pvPortMalloc for coherence with the rest of PiOS +review OPReview-44 --- flight/PiOS/Common/pios_mpu6000.c | 40 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/flight/PiOS/Common/pios_mpu6000.c b/flight/PiOS/Common/pios_mpu6000.c index 675b5e600..c90ac43d4 100644 --- a/flight/PiOS/Common/pios_mpu6000.c +++ b/flight/PiOS/Common/pios_mpu6000.c @@ -227,6 +227,7 @@ int32_t PIOS_MPU6000_ConfigureRanges( /** * @brief Claim the SPI bus for the accel communications and select this chip * @return 0 if successful, -1 for invalid device, -2 if unable to claim bus + * @param fromIsr[in] Tells if the function is being called from a ISR or not */ static int32_t PIOS_MPU6000_ClaimBus(bool fromIsr) { @@ -246,15 +247,19 @@ static int32_t PIOS_MPU6000_ClaimBus(bool fromIsr) /** * @brief Release the SPI bus for the accel communications and end the transaction * @return 0 if successful + * @param fromIsr[in] Tells if the function is being called from a ISR or not */ -int32_t PIOS_MPU6000_ReleaseBus() +int32_t PIOS_MPU6000_ReleaseBus(bool fromIsr) { if(PIOS_MPU6000_Validate(dev) != 0) return -1; PIOS_SPI_RC_PinSet(dev->spi_id,dev->slave_num,1); - - return PIOS_SPI_ReleaseBus(dev->spi_id); + if(fromIsr){ + return PIOS_SPI_ReleaseBusISR(dev->spi_id); + } else { + return PIOS_SPI_ReleaseBus(dev->spi_id); + } } /** @@ -272,7 +277,7 @@ static int32_t PIOS_MPU6000_GetReg(uint8_t reg) PIOS_SPI_TransferByte(dev->spi_id,(0x80 | reg) ); // request byte data = PIOS_SPI_TransferByte(dev->spi_id,0 ); // receive response - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(false); return data; } @@ -290,16 +295,16 @@ static int32_t PIOS_MPU6000_SetReg(uint8_t reg, uint8_t data) return -1; if(PIOS_SPI_TransferByte(dev->spi_id, 0x7f & reg) != 0) { - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(false); return -2; } if(PIOS_SPI_TransferByte(dev->spi_id, data) != 0) { - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(false); return -3; } - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(false); return 0; } @@ -321,7 +326,7 @@ int32_t PIOS_MPU6000_ReadGyros(struct pios_mpu6000_data * data) if(PIOS_SPI_TransferBlock(dev->spi_id, &buf[0], &rec[0], sizeof(buf), NULL) < 0) return -2; - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(false); data->gyro_x = rec[1] << 8 | rec[2]; data->gyro_y = rec[3] << 8 | rec[4]; @@ -407,6 +412,7 @@ int32_t PIOS_MPU6000_Test(void) * @brief Run self-test operation. * \return 0 if test succeeded * \return non-zero value if test succeeded + * @param fromIsr[in] Tells if the function is being called from a ISR or not */ static int32_t PIOS_MPU6000_FifoDepth(bool fromIsr) { @@ -417,11 +423,11 @@ static int32_t PIOS_MPU6000_FifoDepth(bool fromIsr) return -1; if(PIOS_SPI_TransferBlock(dev->spi_id, &mpu6000_send_buf[0], &mpu6000_rec_buf[0], sizeof(mpu6000_send_buf), NULL) < 0) { - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(fromIsr); return -1; } - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(fromIsr); return (mpu6000_rec_buf[1] << 8) | mpu6000_rec_buf[2]; } @@ -456,18 +462,18 @@ bool PIOS_MPU6000_IRQHandler(void) if (PIOS_MPU6000_ClaimBus(true) != 0) return false; - uint8_t mpu6000_send_buf[1 + sizeof(struct pios_mpu6000_data) ] = {PIOS_MPU6000_FIFO_REG | 0x80, 0, 0, 0, 0, 0, 0, 0, 0}; - uint8_t mpu6000_rec_buf[1 + sizeof(struct pios_mpu6000_data) ]; + static uint8_t mpu6000_send_buf[1 + sizeof(struct pios_mpu6000_data) ] = {PIOS_MPU6000_FIFO_REG | 0x80, 0, 0, 0, 0, 0, 0, 0, 0}; + static uint8_t mpu6000_rec_buf[1 + sizeof(struct pios_mpu6000_data) ]; if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu6000_send_buf[0], &mpu6000_rec_buf[0], sizeof(mpu6000_send_buf), NULL) < 0) { - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(true); mpu6000_fails++; return false; } - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(true); - struct pios_mpu6000_data data; + static struct pios_mpu6000_data data; // In the case where extras samples backed up grabbed an extra if (mpu6000_count >= (sizeof(data) * 2)) { @@ -476,12 +482,12 @@ bool PIOS_MPU6000_IRQHandler(void) return false; if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu6000_send_buf[0], &mpu6000_rec_buf[0], sizeof(mpu6000_send_buf), NULL) < 0) { - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(true); mpu6000_fails++; return false; } - PIOS_MPU6000_ReleaseBus(); + PIOS_MPU6000_ReleaseBus(true); } // Rotate the sensor to OP convention. The datasheet defines X as towards the right