Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into cmsis-5
Browse files Browse the repository at this point in the history
  • Loading branch information
concatime committed Mar 3, 2021
2 parents b4db93f + 6c74047 commit 5453e72
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 92 deletions.
1 change: 0 additions & 1 deletion src/drivers/bosch/src/bstdr_comm_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
bstdr_ret_t bstdr_comm_init(void)
{
/**< Communication initialization --Optional!*/
i2cdevInit(I2C1_DEV);
return (bstdr_ret_t)0;
}

Expand Down
129 changes: 56 additions & 73 deletions src/drivers/src/i2c_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "i2c_drv.h"
#include "config.h"
#include "nvicconf.h"
#include "sleepus.h"

//DEBUG
#ifdef I2CDRV_DEBUG_LOG_EVENTS
Expand All @@ -65,23 +66,15 @@
#define I2C_MAX_RETRIES 2
#define I2C_MESSAGE_TIMEOUT M2T(1000)

// Delay is approx 0.06us per loop @168Mhz
#define I2CDEV_LOOPS_PER_US 17
#define I2CDEV_LOOPS_PER_MS (16789) // measured

// Defines to unlock bus
#define I2CDEV_CLK_TS (10 * I2CDEV_LOOPS_PER_US)
#define GPIO_WAIT_FOR_HIGH(gpio, pin, timeoutcycles)\
{\
int i = timeoutcycles;\
while(GPIO_ReadInputDataBit(gpio, pin) == Bit_RESET && i--);\
}

#define GPIO_WAIT_FOR_LOW(gpio, pin, timeoutcycles) \
{\
int i = timeoutcycles;\
while(GPIO_ReadInputDataBit(gpio, pin) == Bit_SET && i--);\
// Helpers to unlock bus
#define I2CDEV_CLK_TS (10)
static void gpioWaitForHigh(GPIO_TypeDef *gpio, uint16_t pin, uint16_t timeout_us)
{
uint64_t start = usecTimestamp();
while (GPIO_ReadInputDataBit(gpio, pin) == Bit_RESET && usecTimestamp() - start <= timeout_us)
{
}
}


#ifdef I2CDRV_DEBUG_LOG_EVENTS
Expand All @@ -107,10 +100,6 @@ static void i2cdrvStartTransfer(I2cDrv *i2c);
* Try to restart a hanged buss
*/
static void i2cdrvTryToRestartBus(I2cDrv* i2c);
/**
* Rough spin loop delay.
*/
static inline void i2cdrvRoughLoopDelay(uint32_t us) __attribute__((optimize("O2")));
/**
* Unlocks the i2c bus if needed.
*/
Expand Down Expand Up @@ -200,12 +189,6 @@ I2cDrv deckBus =
};


static inline void i2cdrvRoughLoopDelay(uint32_t us)
{
volatile uint32_t delay = 0;
for(delay = 0; delay < I2CDEV_LOOPS_PER_US * us; ++delay) { };
}

static void i2cdrvStartTransfer(I2cDrv *i2c)
{
ASSERT_DMA_SAFE(i2c->txMessage.buffer);
Expand Down Expand Up @@ -238,43 +221,7 @@ static void i2cNotifyClient(I2cDrv* i2c)

static void i2cdrvTryToRestartBus(I2cDrv* i2c)
{
i2cdrvInitBus(i2c);
}

static void i2cdrvDmaSetupBus(I2cDrv* i2c)
{

NVIC_InitTypeDef NVIC_InitStructure;

RCC_AHB1PeriphClockCmd(i2c->def->dmaPerif, ENABLE);

// RX DMA Channel Config
i2c->DMAStruct.DMA_Channel = i2c->def->dmaChannel;
i2c->DMAStruct.DMA_PeripheralBaseAddr = (uint32_t)&i2c->def->i2cPort->DR;
i2c->DMAStruct.DMA_Memory0BaseAddr = 0;
i2c->DMAStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
i2c->DMAStruct.DMA_BufferSize = 0;
i2c->DMAStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
i2c->DMAStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
i2c->DMAStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
i2c->DMAStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
i2c->DMAStruct.DMA_Mode = DMA_Mode_Normal;
i2c->DMAStruct.DMA_Priority = DMA_Priority_High;
i2c->DMAStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
i2c->DMAStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
i2c->DMAStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
i2c->DMAStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

NVIC_InitStructure.NVIC_IRQChannel = i2c->def->dmaRxIRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_HIGH_PRI;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

static void i2cdrvInitBus(I2cDrv* i2c)
{
I2C_InitTypeDef I2C_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

Expand Down Expand Up @@ -329,6 +276,42 @@ static void i2cdrvInitBus(I2cDrv* i2c)
NVIC_Init(&NVIC_InitStructure);

i2cdrvDmaSetupBus(i2c);
}

static void i2cdrvDmaSetupBus(I2cDrv* i2c)
{

NVIC_InitTypeDef NVIC_InitStructure;

RCC_AHB1PeriphClockCmd(i2c->def->dmaPerif, ENABLE);

// RX DMA Channel Config
i2c->DMAStruct.DMA_Channel = i2c->def->dmaChannel;
i2c->DMAStruct.DMA_PeripheralBaseAddr = (uint32_t)&i2c->def->i2cPort->DR;
i2c->DMAStruct.DMA_Memory0BaseAddr = 0;
i2c->DMAStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
i2c->DMAStruct.DMA_BufferSize = 0;
i2c->DMAStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
i2c->DMAStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
i2c->DMAStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
i2c->DMAStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
i2c->DMAStruct.DMA_Mode = DMA_Mode_Normal;
i2c->DMAStruct.DMA_Priority = DMA_Priority_High;
i2c->DMAStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
i2c->DMAStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
i2c->DMAStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
i2c->DMAStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

NVIC_InitStructure.NVIC_IRQChannel = i2c->def->dmaRxIRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_HIGH_PRI;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

static void i2cdrvInitBus(I2cDrv* i2c)
{
i2cdrvTryToRestartBus(i2c);

i2c->isBusFreeSemaphore = xSemaphoreCreateBinaryStatic(&i2c->isBusFreeSemaphoreBuffer);
i2c->isBusFreeMutex = xSemaphoreCreateMutexStatic(&i2c->isBusFreeMutexBuffer);
Expand All @@ -343,30 +326,30 @@ static void i2cdrvdevUnlockBus(GPIO_TypeDef* portSCL, GPIO_TypeDef* portSDA, uin
/* Set clock high */
GPIO_SetBits(portSCL, pinSCL);
/* Wait for any clock stretching to finish. */
GPIO_WAIT_FOR_HIGH(portSCL, pinSCL, 10 * I2CDEV_LOOPS_PER_MS);
i2cdrvRoughLoopDelay(I2CDEV_CLK_TS);
gpioWaitForHigh(portSCL, pinSCL, 10 * 1000);
sleepus(I2CDEV_CLK_TS);

/* Generate a clock cycle */
GPIO_ResetBits(portSCL, pinSCL);
i2cdrvRoughLoopDelay(I2CDEV_CLK_TS);
sleepus(I2CDEV_CLK_TS);
GPIO_SetBits(portSCL, pinSCL);
i2cdrvRoughLoopDelay(I2CDEV_CLK_TS);
sleepus(I2CDEV_CLK_TS);
}

/* Generate a start then stop condition */
GPIO_SetBits(portSCL, pinSCL);
i2cdrvRoughLoopDelay(I2CDEV_CLK_TS);
GPIO_ResetBits(portSDA, pinSDA);
i2cdrvRoughLoopDelay(I2CDEV_CLK_TS);
sleepus(I2CDEV_CLK_TS);
GPIO_ResetBits(portSDA, pinSDA);
i2cdrvRoughLoopDelay(I2CDEV_CLK_TS);
sleepus(I2CDEV_CLK_TS);
GPIO_ResetBits(portSCL, pinSCL);
sleepus(I2CDEV_CLK_TS);

/* Set data and clock high and wait for any clock stretching to finish. */
GPIO_SetBits(portSDA, pinSDA);
GPIO_SetBits(portSCL, pinSCL);
GPIO_WAIT_FOR_HIGH(portSCL, pinSCL, 10 * I2CDEV_LOOPS_PER_MS);
gpioWaitForHigh(portSCL, pinSCL, 10 * 1000);
/* Wait for data to be high */
GPIO_WAIT_FOR_HIGH(portSDA, pinSDA, 10 * I2CDEV_LOOPS_PER_MS);
gpioWaitForHigh(portSDA, pinSDA, 10 * 1000);
}

//-----------------------------------------------------------
Expand Down
4 changes: 0 additions & 4 deletions src/drivers/src/pca9685.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ static uint16_t dutyToDuration(float duty)

bool pca9685init(int addr, float pwmFreq)
{
if (!i2cdevInit(I2C1_DEV)) {
return false;
}

// initial state is sleeping.
// must be asleep to set PWM freq.

Expand Down
1 change: 0 additions & 1 deletion src/drivers/src/vl53l0x.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ bool vl53l0xInit(VL53L0xDev* dev, I2C_Dev *I2Cx, bool io_2V8)
{
dev->I2Cx = I2Cx;
dev->devAddr = VL53L0X_DEFAULT_ADDRESS;
i2cdevInit(dev->I2Cx);

dev->io_timeout = 0;
dev->did_timeout = 0;
Expand Down
1 change: 0 additions & 1 deletion src/drivers/src/vl53l1x.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ bool vl53l1xInit(VL53L1_Dev_t *pdev, I2C_Dev *I2Cx)

pdev->I2Cx = I2Cx;
pdev->devAddr = VL53L1X_DEFAULT_ADDRESS;
i2cdevInit(pdev->I2Cx);

/* Move initialized sensor to a new I2C address */
int newAddress;
Expand Down
3 changes: 1 addition & 2 deletions src/hal/src/amg8833.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ bool begin(AMG8833_Dev_t *dev, I2C_Dev *I2Cx)
// Set I2C parameters
dev->I2Cx = I2Cx;
dev->devAddr = AMG88xx_ADDRESS;
bool i2c_complete = i2cdevInit(dev->I2Cx);
// Enter normal mode
bool mode_selected = write8(dev, AMG88xx_PCTL, AMG88xx_NORMAL_MODE);
// Software reset
Expand All @@ -53,7 +52,7 @@ bool begin(AMG8833_Dev_t *dev, I2C_Dev *I2Cx)
//set to 10 FPS
bool fps_set = write8(dev, AMG88xx_FPSC, (AMG88xx_FPS_10 & 0x01));
vTaskDelay(M2T(10));
return i2c_complete && mode_selected && software_resetted &&
return mode_selected && software_resetted &&
interrupts_set && fps_set;
}

Expand Down
1 change: 0 additions & 1 deletion src/hal/src/pca9555.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ static I2C_Dev *I2Cx;

void pca9555Init()
{
i2cdevInit(I2C1_DEV);
I2Cx = I2C1_DEV;
devAddr = PCA9555_DEFAULT_ADDRESS;
}
Expand Down
1 change: 0 additions & 1 deletion src/hal/src/pca95x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ static I2C_Dev *I2Cx;

void pca95x4Init()
{
i2cdevInit(I2C1_DEV);
I2Cx = I2C1_DEV;
devAddr = PCA95X4_DEFAULT_ADDRESS;
}
Expand Down
2 changes: 0 additions & 2 deletions src/hal/src/sensors_bmi088_bmp388.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,6 @@ void sensorsBmi088Bmp388Init(void)
return;
}

i2cdevInit(I2C3_DEV);

sensorsBiasObjInit(&gyroBiasRunning);
sensorsDeviceInit();
sensorsInterruptInit();
Expand Down
1 change: 0 additions & 1 deletion src/hal/src/sensors_bmi088_spi_bmp388.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,6 @@ void sensorsBmi088SpiBmp388Init(void)
return;
}

i2cdevInit(I2C3_DEV);
spiInit();
spiDMAInit();

Expand Down
1 change: 0 additions & 1 deletion src/hal/src/sensors_mpu9250_lps25h.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ static void sensorsDeviceInit(void)
// Wait for sensors to startup
while (xTaskGetTickCount() < 1000);

i2cdevInit(I2C3_DEV);
mpu6500Init(I2C3_DEV);
if (mpu6500TestConnection() == true)
{
Expand Down
4 changes: 1 addition & 3 deletions src/hal/src/usb_bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE);

/* Configure SOF ID DM DP Pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |
GPIO_Pin_11 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 |
GPIO_Pin_12;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
Expand All @@ -123,7 +122,6 @@ void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)

// AF10

GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ;
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ;
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ;

Expand Down
5 changes: 5 additions & 0 deletions src/modules/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "static_mem.h"
#include "peer_localization.h"
#include "cfassert.h"
#include "i2cdev.h"

#ifndef START_DISARMED
#define ARM_INIT true
Expand Down Expand Up @@ -175,6 +176,10 @@ void systemTask(void *arg)
uart2Init(115200);
#endif

initUsecTimer();
i2cdevInit(I2C3_DEV);
i2cdevInit(I2C1_DEV);

//Init the high-levels modules
systemInit();
commInit();
Expand Down
1 change: 0 additions & 1 deletion src/utils/src/configblockeeprom.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ int configblockInit(void)
if(isInit)
return 0;

i2cdevInit(I2C1_DEV);
eepromInit(I2C1_DEV);

// Because of strange behavior from I2C device during expansion port test
Expand Down

0 comments on commit 5453e72

Please sign in to comment.