diff --git a/components/driver/spi_master.c b/components/driver/spi_master.c index 514a3be683..5976b008ff 100644 --- a/components/driver/spi_master.c +++ b/components/driver/spi_master.c @@ -380,11 +380,14 @@ esp_err_t spi_bus_free(spi_host_device_t host) void spi_get_timing(bool gpio_is_used, int input_delay_ns, int eff_clk, int* dummy_o, int* cycles_remain_o) { const int apbclk_kHz = APB_CLK_FREQ/1000; + //calculate how many apb clocks a period has const int apbclk_n = APB_CLK_FREQ/eff_clk; const int gpio_delay_ns = gpio_is_used ? 25 : 0; - //calculate how many apb clocks a period has, the 1 is to compensate in case ``input_delay_ns`` is rounded off. + //calculate how many apb clocks the delay is, the 1 is to compensate in case ``input_delay_ns`` is rounded off. int apb_period_n = (1 + input_delay_ns + gpio_delay_ns)*apbclk_kHz/1000/1000; + if (apb_period_n < 0) apb_period_n = 0; + int dummy_required = apb_period_n/apbclk_n; int miso_delay = 0; @@ -406,8 +409,10 @@ int spi_get_freq_limit(bool gpio_is_used, int input_delay_ns) const int apbclk_kHz = APB_CLK_FREQ/1000; const int gpio_delay_ns = gpio_is_used ? 25 : 0; - //calculate how many apb clocks a period has, the 1 is to compensate in case ``input_delay_ns`` is rounded off. + //calculate how many apb clocks the delay is, the 1 is to compensate in case ``input_delay_ns`` is rounded off. int apb_period_n = (1 + input_delay_ns + gpio_delay_ns)*apbclk_kHz/1000/1000; + if (apb_period_n < 0) apb_period_n = 0; + return APB_CLK_FREQ/(apb_period_n+1); }