Fix for counter overflow in delays based on sdk_system_get_time()

This commit is contained in:
UncleRus 2017-04-21 17:53:13 +05:00
parent b3f658bdbf
commit 55e08de21c
3 changed files with 23 additions and 16 deletions

View file

@ -38,6 +38,8 @@
#define MEASUREMENT_TIMEOUT 6000
#define timeout_expired(start, len) ((uint32_t)(sdk_system_get_time() - (start)) >= (len))
static const float gain_values [] = {
[HMC5883L_GAIN_1370] = 0.73,
[HMC5883L_GAIN_1090] = 0.92,
@ -156,10 +158,10 @@ bool hmc5883l_get_raw_data(hmc5883l_raw_data_t *data)
// overwrite mode register for measurement
hmc5883l_set_operating_mode(current_mode);
// wait for data
uint32_t timeout = sdk_system_get_time() + MEASUREMENT_TIMEOUT;
uint32_t start = sdk_system_get_time();
while (!hmc5883l_data_is_ready())
{
if (sdk_system_get_time() >= timeout)
if (timeout_expired(start, MEASUREMENT_TIMEOUT))
return false;
}
}

View file

@ -141,6 +141,8 @@ static uint16_t crc_ccitt(const uint8_t *data, size_t n)
#define spi_skip_word() do { spi_read_byte(); spi_read_byte(); } while(0)
#define spi_skip_dword() do { spi_read_byte(); spi_read_byte(); spi_read_byte(); spi_read_byte(); } while(0)
#define timeout_expired(start, len) ((uint32_t)(sdk_system_get_time() - (start)) >= (len))
inline static uint16_t spi_write_word(uint16_t word)
{
return (spi_transfer_8(BUS, word >> 8) << 8) | spi_transfer_8(BUS, word);
@ -154,9 +156,10 @@ inline static void spi_read_bytes(uint8_t *dst, size_t size)
static bool wait()
{
uint32_t stop = sdk_system_get_time() + IO_TIMEOUT_US;
uint32_t start = sdk_system_get_time();
while (spi_read_byte() != 0xff)
if (sdk_system_get_time() >= stop)
if (timeout_expired(start, IO_TIMEOUT_US))
return false;
return true;
}
@ -208,11 +211,11 @@ inline static sdio_error_t set_error(sdio_card_t *card, sdio_error_t err)
static sdio_error_t read_data(sdio_card_t *card, uint8_t *dst, size_t size)
{
uint32_t timeout = sdk_system_get_time() + IO_TIMEOUT_US;
uint32_t start = sdk_system_get_time();
while (true)
{
if (sdk_system_get_time() >= timeout)
if (timeout_expired(start, IO_TIMEOUT_US))
return set_error(card, SDIO_ERR_TIMEOUT);
uint8_t b = spi_read_byte();
@ -269,7 +272,7 @@ sdio_error_t sdio_init(sdio_card_t *card, uint8_t cs_pin, uint32_t high_freq_div
spi_set_settings(BUS, &s);
gpio_enable(card->cs_pin, GPIO_OUTPUT);
uint32_t timeout = sdk_system_get_time() + INIT_TIMEOUT_US;
uint32_t start = sdk_system_get_time();
spi_cs_low(card);
spi_cs_high(card);
@ -279,7 +282,7 @@ sdio_error_t sdio_init(sdio_card_t *card, uint8_t cs_pin, uint32_t high_freq_div
// Set card to the SPI idle mode
while (command(card, CMD0, 0) != BV(R1_IDLE_STATE))
{
if (sdk_system_get_time() >= timeout)
if (timeout_expired(start, INIT_TIMEOUT_US))
return set_error(card, SDIO_ERR_TIMEOUT);
}
@ -300,7 +303,7 @@ sdio_error_t sdio_init(sdio_card_t *card, uint8_t cs_pin, uint32_t high_freq_div
break;
}
if (sdk_system_get_time() >= timeout)
if (timeout_expired(start, INIT_TIMEOUT_US))
return set_error(card, SDIO_ERR_TIMEOUT);
}
@ -311,13 +314,13 @@ sdio_error_t sdio_init(sdio_card_t *card, uint8_t cs_pin, uint32_t high_freq_div
{
card->type = SDIO_TYPE_MMC;
while (command(card, CMD1, 0))
if (sdk_system_get_time() >= timeout)
if (timeout_expired(start, INIT_TIMEOUT_US))
return set_error(card, SDIO_ERR_TIMEOUT);
}
else
{
while (app_command(card, ACMD41, 0))
if (sdk_system_get_time() >= timeout)
if (timeout_expired(start, INIT_TIMEOUT_US))
return set_error(card, SDIO_ERR_TIMEOUT);
}
@ -328,7 +331,7 @@ sdio_error_t sdio_init(sdio_card_t *card, uint8_t cs_pin, uint32_t high_freq_div
{
// SD2 or SDHC
while (app_command(card, ACMD41, BV(30)) != 0)
if (sdk_system_get_time() >= timeout)
if (timeout_expired(start, INIT_TIMEOUT_US))
return set_error(card, SDIO_ERR_TIMEOUT);
}
// read OCR

View file

@ -15,6 +15,8 @@
#define PING_TIMEOUT 6000
#define ROUNDTRIP 58
#define timeout_expired(start, len) ((uint32_t)(sdk_system_get_time() - (start)) >= (len))
void ultrasoinc_init(const ultrasonic_sensor_t *dev)
{
gpio_enable(dev->trigger_pin, GPIO_OUTPUT);
@ -36,21 +38,21 @@ int32_t ultrasoinc_measure_cm(const ultrasonic_sensor_t *dev, uint32_t max_dista
return ULTRASONIC_ERROR_PING;
// Wait for echo
uint32_t timeout = sdk_system_get_time() + PING_TIMEOUT;
uint32_t start = sdk_system_get_time();
while (!gpio_read(dev->echo_pin))
{
if (sdk_system_get_time() >= timeout)
if (timeout_expired(start, PING_TIMEOUT))
return ULTRASONIC_ERROR_PING_TIMEOUT;
}
// got echo, measuring
uint32_t echo_start = sdk_system_get_time();
uint32_t time = echo_start;
timeout = echo_start + max_distance * ROUNDTRIP;
uint32_t meas_timeout = echo_start + max_distance * ROUNDTRIP;
while (gpio_read(dev->echo_pin))
{
time = sdk_system_get_time();
if (time >= timeout)
if (timeout_expired(echo_start, meas_timeout))
return ULTRASONIC_ERROR_ECHO_TIMEOUT;
}