code style fix

pull/14030/head
pennam 2021-02-03 14:18:07 +01:00
parent 506390bcf8
commit d968b17e76
1 changed files with 243 additions and 270 deletions

View File

@ -47,8 +47,7 @@
/** @defgroup I2C_DEVICE_Private_Types I2C_DEVICE Private Types /** @defgroup I2C_DEVICE_Private_Types I2C_DEVICE Private Types
* @{ * @{
*/ */
typedef struct typedef struct {
{
uint32_t freq; /* Frequency in Hz */ uint32_t freq; /* Frequency in Hz */
uint32_t freq_min; /* Minimum frequency in Hz */ uint32_t freq_min; /* Minimum frequency in Hz */
uint32_t freq_max; /* Maximum frequency in Hz */ uint32_t freq_max; /* Maximum frequency in Hz */
@ -62,8 +61,7 @@ typedef struct
uint32_t dnf; /* Digital noise filter coefficient */ uint32_t dnf; /* Digital noise filter coefficient */
} I2C_Charac_t; } I2C_Charac_t;
typedef struct typedef struct {
{
uint32_t presc; /* Timing prescaler */ uint32_t presc; /* Timing prescaler */
uint32_t tscldel; /* SCL delay */ uint32_t tscldel; /* SCL delay */
uint32_t tsdadel; /* SDA delay */ uint32_t tsdadel; /* SDA delay */
@ -77,8 +75,7 @@ typedef struct
/** @defgroup I2C_DEVICE_Private_Constants I2C_DEVICE Private Constants /** @defgroup I2C_DEVICE_Private_Constants I2C_DEVICE Private Constants
* @{ * @{
*/ */
static const I2C_Charac_t I2C_Charac[] = static const I2C_Charac_t I2C_Charac[] = {
{
[I2C_SPEED_FREQ_STANDARD] = [I2C_SPEED_FREQ_STANDARD] =
{ {
.freq = 100000, .freq = 100000,
@ -173,42 +170,33 @@ static void I2C_Compute_PRESC_SCLDEL_SDADEL(uint32_t clock_src_freq, uint32_t I2
/* {[tr+ tSU;DAT(min)] / [tPRESC]} - 1 <= SCLDEL */ /* {[tr+ tSU;DAT(min)] / [tPRESC]} - 1 <= SCLDEL */
tscldel_min = (int32_t)I2C_Charac[I2C_speed].trise + (int32_t)I2C_Charac[I2C_speed].sudat_min; tscldel_min = (int32_t)I2C_Charac[I2C_speed].trise + (int32_t)I2C_Charac[I2C_speed].sudat_min;
if (tsdadel_min <= 0) if (tsdadel_min <= 0) {
{
tsdadel_min = 0; tsdadel_min = 0;
} }
if (tsdadel_max <= 0) if (tsdadel_max <= 0) {
{
tsdadel_max = 0; tsdadel_max = 0;
} }
for (presc = 0; presc < I2C_PRESC_MAX; presc++) for (presc = 0; presc < I2C_PRESC_MAX; presc++) {
{ for (scldel = 0; scldel < I2C_SCLDEL_MAX; scldel++) {
for (scldel = 0; scldel < I2C_SCLDEL_MAX; scldel++)
{
/* TSCLDEL = (SCLDEL+1) * (PRESC+1) * TI2CCLK */ /* TSCLDEL = (SCLDEL+1) * (PRESC+1) * TI2CCLK */
uint32_t tscldel = (scldel + 1U) * (presc + 1U) * ti2cclk; uint32_t tscldel = (scldel + 1U) * (presc + 1U) * ti2cclk;
if (tscldel >= (uint32_t)tscldel_min) if (tscldel >= (uint32_t)tscldel_min) {
{ for (sdadel = 0; sdadel < I2C_SDADEL_MAX; sdadel++) {
for (sdadel = 0; sdadel < I2C_SDADEL_MAX; sdadel++)
{
/* TSDADEL = SDADEL * (PRESC+1) * TI2CCLK */ /* TSDADEL = SDADEL * (PRESC+1) * TI2CCLK */
uint32_t tsdadel = (sdadel * (presc + 1U)) * ti2cclk; uint32_t tsdadel = (sdadel * (presc + 1U)) * ti2cclk;
if ((tsdadel >= (uint32_t)tsdadel_min) && (tsdadel <= (uint32_t)tsdadel_max)) if ((tsdadel >= (uint32_t)tsdadel_min) && (tsdadel <= (uint32_t)tsdadel_max)) {
{ if (presc != prev_presc) {
if(presc != prev_presc)
{
I2c_valid_timing[I2c_valid_timing_nbr].presc = presc; I2c_valid_timing[I2c_valid_timing_nbr].presc = presc;
I2c_valid_timing[I2c_valid_timing_nbr].tscldel = scldel; I2c_valid_timing[I2c_valid_timing_nbr].tscldel = scldel;
I2c_valid_timing[I2c_valid_timing_nbr].tsdadel = sdadel; I2c_valid_timing[I2c_valid_timing_nbr].tsdadel = sdadel;
prev_presc = presc; prev_presc = presc;
I2c_valid_timing_nbr ++; I2c_valid_timing_nbr ++;
if(I2c_valid_timing_nbr >= I2C_VALID_TIMING_NBR) if (I2c_valid_timing_nbr >= I2C_VALID_TIMING_NBR) {
{
return; return;
} }
} }
@ -249,41 +237,34 @@ static uint32_t I2C_Compute_SCLL_SCLH (uint32_t clock_src_freq, uint32_t I2C_spe
prev_error = ti2cspeed; prev_error = ti2cspeed;
for (uint32_t count = 0; count < I2c_valid_timing_nbr; count++) for (uint32_t count = 0; count < I2c_valid_timing_nbr; count++) {
{
/* tPRESC = (PRESC+1) x tI2CCLK*/ /* tPRESC = (PRESC+1) x tI2CCLK*/
uint32_t tpresc = (I2c_valid_timing[count].presc + 1U) * ti2cclk; uint32_t tpresc = (I2c_valid_timing[count].presc + 1U) * ti2cclk;
for (scll = 0; scll < I2C_SCLL_MAX; scll++) for (scll = 0; scll < I2C_SCLL_MAX; scll++) {
{
/* tLOW(min) <= tAF(min) + tDNF + 2 x tI2CCLK + [(SCLL+1) x tPRESC ] */ /* tLOW(min) <= tAF(min) + tDNF + 2 x tI2CCLK + [(SCLL+1) x tPRESC ] */
uint32_t tscl_l = tafdel_min + dnf_delay + (2U * ti2cclk) + ((scll + 1U) * tpresc); uint32_t tscl_l = tafdel_min + dnf_delay + (2U * ti2cclk) + ((scll + 1U) * tpresc);
/* The I2CCLK period tI2CCLK must respect the following conditions: /* The I2CCLK period tI2CCLK must respect the following conditions:
tI2CCLK < (tLOW - tfilters) / 4 and tI2CCLK < tHIGH */ tI2CCLK < (tLOW - tfilters) / 4 and tI2CCLK < tHIGH */
if ((tscl_l > I2C_Charac[I2C_speed].lscl_min) && (ti2cclk < ((tscl_l - tafdel_min - dnf_delay) / 4U))) if ((tscl_l > I2C_Charac[I2C_speed].lscl_min) && (ti2cclk < ((tscl_l - tafdel_min - dnf_delay) / 4U))) {
{ for (sclh = 0; sclh < I2C_SCLH_MAX; sclh++) {
for (sclh = 0; sclh < I2C_SCLH_MAX; sclh++)
{
/* tHIGH(min) <= tAF(min) + tDNF + 2 x tI2CCLK + [(SCLH+1) x tPRESC] */ /* tHIGH(min) <= tAF(min) + tDNF + 2 x tI2CCLK + [(SCLH+1) x tPRESC] */
uint32_t tscl_h = tafdel_min + dnf_delay + (2U * ti2cclk) + ((sclh + 1U) * tpresc); uint32_t tscl_h = tafdel_min + dnf_delay + (2U * ti2cclk) + ((sclh + 1U) * tpresc);
/* tSCL = tf + tLOW + tr + tHIGH */ /* tSCL = tf + tLOW + tr + tHIGH */
uint32_t tscl = tscl_l + tscl_h + I2C_Charac[I2C_speed].trise + I2C_Charac[I2C_speed].tfall; uint32_t tscl = tscl_l + tscl_h + I2C_Charac[I2C_speed].trise + I2C_Charac[I2C_speed].tfall;
if ((tscl >= clk_min) && (tscl <= clk_max) && (tscl_h >= I2C_Charac[I2C_speed].hscl_min) && (ti2cclk < tscl_h)) if ((tscl >= clk_min) && (tscl <= clk_max) && (tscl_h >= I2C_Charac[I2C_speed].hscl_min) && (ti2cclk < tscl_h)) {
{
int32_t error = (int32_t)tscl - (int32_t)ti2cspeed; int32_t error = (int32_t)tscl - (int32_t)ti2cspeed;
if (error < 0) if (error < 0) {
{
error = -error; error = -error;
} }
/* look for the timings with the lowest clock error */ /* look for the timings with the lowest clock error */
if ((uint32_t)error < prev_error) if ((uint32_t)error < prev_error) {
{
prev_error = (uint32_t)error; prev_error = (uint32_t)error;
I2c_valid_timing[count].scll = scll; I2c_valid_timing[count].scll = scll;
I2c_valid_timing[count].sclh = sclh; I2c_valid_timing[count].sclh = sclh;
@ -311,18 +292,14 @@ static uint32_t I2C_ComputeTiming(uint32_t clock_src_freq, uint32_t i2c_freq)
uint32_t idx; uint32_t idx;
if((clock_src_freq != 0U) && (i2c_freq != 0U)) if ((clock_src_freq != 0U) && (i2c_freq != 0U)) {
{ for (speed = 0 ; speed <= (uint32_t)I2C_SPEED_FREQ_FAST_PLUS ; speed++) {
for ( speed = 0 ; speed <= (uint32_t)I2C_SPEED_FREQ_FAST_PLUS ; speed++)
{
if ((i2c_freq >= I2C_Charac[speed].freq_min) && if ((i2c_freq >= I2C_Charac[speed].freq_min) &&
(i2c_freq <= I2C_Charac[speed].freq_max)) (i2c_freq <= I2C_Charac[speed].freq_max)) {
{
I2C_Compute_PRESC_SCLDEL_SDADEL(clock_src_freq, speed); I2C_Compute_PRESC_SCLDEL_SDADEL(clock_src_freq, speed);
idx = I2C_Compute_SCLL_SCLH(clock_src_freq, speed); idx = I2C_Compute_SCLL_SCLH(clock_src_freq, speed);
if (idx < I2C_VALID_TIMING_NBR) if (idx < I2C_VALID_TIMING_NBR) {
{
ret = ((I2c_valid_timing[idx].presc & 0x0FU) << 28) | \ ret = ((I2c_valid_timing[idx].presc & 0x0FU) << 28) | \
((I2c_valid_timing[idx].tscldel & 0x0FU) << 20) | \ ((I2c_valid_timing[idx].tscldel & 0x0FU) << 20) | \
((I2c_valid_timing[idx].tsdadel & 0x0FU) << 16) | \ ((I2c_valid_timing[idx].tsdadel & 0x0FU) << 16) | \
@ -350,8 +327,7 @@ static uint32_t I2C_GetPclk(I2CName i2c)
if (i2c == I2C_1 || i2c == I2C_2 || i2c == I2C_3) { if (i2c == I2C_1 || i2c == I2C_2 || i2c == I2C_3) {
clocksource = __HAL_RCC_GET_I2C123_SOURCE(); clocksource = __HAL_RCC_GET_I2C123_SOURCE();
switch(clocksource) switch (clocksource) {
{
case RCC_I2C123CLKSOURCE_D2PCLK1: case RCC_I2C123CLKSOURCE_D2PCLK1:
pclk = HAL_RCC_GetPCLK1Freq(); pclk = HAL_RCC_GetPCLK1Freq();
break; break;
@ -370,11 +346,9 @@ static uint32_t I2C_GetPclk(I2CName i2c)
error("I2C123: Invalid clock source"); error("I2C123: Invalid clock source");
break; break;
} }
} } else if (i2c == I2C_4) {
else if(i2c == I2C_4) {
clocksource = __HAL_RCC_GET_I2C4_SOURCE(); clocksource = __HAL_RCC_GET_I2C4_SOURCE();
switch(clocksource) switch (clocksource) {
{
case RCC_I2C4CLKSOURCE_D3PCLK1: case RCC_I2C4CLKSOURCE_D3PCLK1:
pclk = HAL_RCCEx_GetD3PCLK1Freq(); pclk = HAL_RCCEx_GetD3PCLK1Freq();
break; break;
@ -393,8 +367,7 @@ static uint32_t I2C_GetPclk(I2CName i2c)
error("I2C4: Invalid clock source"); error("I2C4: Invalid clock source");
break; break;
} }
} } else {
else {
// should not happend // should not happend
error("I2C: unknown instance"); error("I2C: unknown instance");
} }