[stm32][i2c] replace stm32_udelay as rt_hw_us_delay

This commit is contained in:
Meco Man 2023-06-27 18:07:57 -04:00 committed by Man, Jianting (Meco)
parent 4cbc1893bd
commit 34bc78331c
1 changed files with 4 additions and 38 deletions

View File

@ -6,9 +6,9 @@
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2018-11-08 balanceTWK first version * 2018-11-08 balanceTWK first version
* 2023-06-27 Meco Man replace stm32_udelay as rt_hw_us_delay
*/ */
#include <board.h>
#include "drv_soft_i2c.h" #include "drv_soft_i2c.h"
#include "drv_config.h" #include "drv_config.h"
@ -116,40 +116,6 @@ static rt_int32_t stm32_get_scl(void *data)
struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data; struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
return rt_pin_read(cfg->scl); return rt_pin_read(cfg->scl);
} }
/**
* The time delay function.
*
* @param microseconds.
*/
static void stm32_udelay(rt_uint32_t us)
{
rt_uint32_t ticks;
rt_uint32_t told, tnow, tcnt = 0;
rt_uint32_t reload = SysTick->LOAD;
ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
told = SysTick->VAL;
while (1)
{
tnow = SysTick->VAL;
if (tnow != told)
{
if (tnow < told)
{
tcnt += told - tnow;
}
else
{
tcnt += reload - tnow + told;
}
told = tnow;
if (tcnt >= ticks)
{
break;
}
}
}
}
static const struct rt_i2c_bit_ops stm32_bit_ops_default = static const struct rt_i2c_bit_ops stm32_bit_ops_default =
{ {
@ -158,7 +124,7 @@ static const struct rt_i2c_bit_ops stm32_bit_ops_default =
.set_scl = stm32_set_scl, .set_scl = stm32_set_scl,
.get_sda = stm32_get_sda, .get_sda = stm32_get_sda,
.get_scl = stm32_get_scl, .get_scl = stm32_get_scl,
.udelay = stm32_udelay, .udelay = rt_hw_us_delay,
.delay_us = 1, .delay_us = 1,
.timeout = 100 .timeout = 100
}; };
@ -179,9 +145,9 @@ static rt_err_t stm32_i2c_bus_unlock(const struct stm32_soft_i2c_config *cfg)
while (i++ < 9) while (i++ < 9)
{ {
rt_pin_write(cfg->scl, PIN_HIGH); rt_pin_write(cfg->scl, PIN_HIGH);
stm32_udelay(100); rt_hw_us_delay(100);
rt_pin_write(cfg->scl, PIN_LOW); rt_pin_write(cfg->scl, PIN_LOW);
stm32_udelay(100); rt_hw_us_delay(100);
} }
} }
if (PIN_LOW == rt_pin_read(cfg->sda)) if (PIN_LOW == rt_pin_read(cfg->sda))