Skip to content

Commit 1154c96

Browse files
decsnyjhedberg
authored andcommitted
include: drivers: spi.h: Get params from DT
- Get bit ordering from DT property in SPI_CONFIG_DT. More precisely, set LSB first if set in DT. - Get CS polarity from DT property in SPI_CONFIG_DT. - Add inter-word delay parameter to spi_config struct. If value is 0, the value will be half of the period. Signed-off-by: Declan Snyder <[email protected]>
1 parent 570b445 commit 1154c96

File tree

1 file changed

+32
-1
lines changed
  • include/zephyr/drivers

1 file changed

+32
-1
lines changed

include/zephyr/drivers/spi.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,36 @@ struct spi_config {
466466
* if not used).
467467
*/
468468
struct spi_cs_control cs;
469+
/**
470+
* @brief Delay between SPI words on SCK line in nanoseconds, if supported.
471+
* Value of zero will attempt to use half of the SCK period.
472+
*/
473+
uint16_t word_delay;
469474
};
470475

476+
/** @cond INTERNAL_HIDDEN */
477+
/* converts from the special DT zero value to half of the frequency, for drivers usage mostly */
478+
static inline uint16_t spi_get_word_delay(const struct spi_config *cfg)
479+
{
480+
uint32_t freq = cfg->frequency;
481+
482+
if (cfg->word_delay != 0) {
483+
return cfg->word_delay;
484+
}
485+
486+
if (freq == 0) {
487+
return 0;
488+
}
489+
490+
uint64_t period_ns = NSEC_PER_SEC / freq;
491+
492+
period_ns = MIN(period_ns, UINT16_MAX);
493+
period_ns /= 2;
494+
495+
return (uint16_t)period_ns;
496+
}
497+
/** @endcond */
498+
471499
/**
472500
* @brief Structure initializer for spi_config from devicetree
473501
*
@@ -487,9 +515,12 @@ struct spi_config {
487515
DT_PROP(node_id, frame_format) | \
488516
COND_CODE_1(DT_PROP(node_id, spi_cpol), SPI_MODE_CPOL, (0)) | \
489517
COND_CODE_1(DT_PROP(node_id, spi_cpha), SPI_MODE_CPHA, (0)) | \
490-
COND_CODE_1(DT_PROP(node_id, spi_hold_cs), SPI_HOLD_ON_CS, (0)), \
518+
COND_CODE_1(DT_PROP(node_id, spi_hold_cs), SPI_HOLD_ON_CS, (0)) | \
519+
COND_CODE_1(DT_PROP(node_id, spi_lsb_first), SPI_TRANSFER_LSB, (0)) | \
520+
COND_CODE_1(DT_PROP(node_id, spi_cs_high), SPI_CS_ACTIVE_HIGH, (0)), \
491521
.slave = DT_REG_ADDR(node_id), \
492522
.cs = SPI_CS_CONTROL_INIT(node_id, __VA_ARGS__), \
523+
.word_delay = DT_PROP(node_id, spi_interframe_delay_ns),\
493524
}
494525

495526
/**

0 commit comments

Comments
 (0)