Mercurial > public > ostc4
view Small_CPU/Src/i2c.c @ 240:625d20070261 div-fixes-5
Improvement SPI stability/recoverability
The core part of this commit comes from careful code reading. The core is the
swap of Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT) and
SPI_Start_single_TxRx_with_Master(). This code is sitting in an if-clause
that is triggered on SPI comms failure. Instead of blindly trying to
communicate again (which will very likely fail again), first try to reset
the comms link, and then try to communicate again. That simply makes
more sense in this case.
This is heavily tested, on 2 simple dives, and 5 very long deco schedules
from the simulator (10+ hour deco's), and a lot of small simulated dives
(upto 2h runtime). Of all these tests, only one long session failed after
9 out of 11h runtime. Analyzing that one failure, suggests that the
RTE is looping in some error handler, which (obviously) results in
a SPI comms failure as a result. I consider this not part of this change.
Additionally, some more cleanup is done in this code.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Mon, 08 Apr 2019 11:49:13 +0200 |
parents | e4207f0aaa4b |
children | 2b9775f71e30 |
line wrap: on
line source
#include "baseCPU2.h" #include "i2c.h" #include "scheduler.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ // =============================================================================== // I2C addresses - see i2c.h // =============================================================================== I2C_HandleTypeDef I2cHandle; /* static void I2C_Error_Handler(void) { while(1) { } } */ //TODO: remove this function. Why return a global variable? HAL_StatusTypeDef I2C1_Status(void) { return (HAL_StatusTypeDef)global.I2C_SystemStatus; } GPIO_PinState HAL_I2C_Read_Data_PIN(void) { return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN); } void HAL_I2C_Send_One_CLOCK(void) { HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET); HAL_Delay(10); HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET); HAL_Delay(10); } GPIO_PinState MX_I2C1_TestAndClear(void) { I2C_DeInit(); HAL_I2C_ManualControl_MspInit(); for(int i=0; i<9;i++) { if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET) HAL_I2C_Send_One_CLOCK(); else break; } return HAL_I2C_Read_Data_PIN(); } //TODO: make this void. return is never used HAL_StatusTypeDef MX_I2C1_Init(void) { I2cHandle.Instance = I2Cx; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; I2cHandle.Init.OwnAddress1 = 0x01; global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F); if(global.dataSendToSlavePending) { scheduleSpecial_Evaluate_DataSendToSlave(); } return (HAL_StatusTypeDef)global.I2C_SystemStatus; } void I2C_DeInit(void) { HAL_I2C_DeInit(&I2cHandle); } static uint8_t i2c_errors = 0; void I2C_Error_count(void) { i2c_errors++; } HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) { if(global.I2C_SystemStatus != HAL_OK) return global.I2C_SystemStatus; global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2); if(global.I2C_SystemStatus != HAL_OK) { I2C_Error_count(); } return (HAL_StatusTypeDef)global.I2C_SystemStatus; } // TODO: return value never used HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) { if(global.I2C_SystemStatus != HAL_OK) return global.I2C_SystemStatus; global.I2C_SystemStatus = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); if(global.I2C_SystemStatus != HAL_OK) { I2C_Error_count(); } return global.I2C_SystemStatus; }