Skip to content

Fix NRF52 SPI pin initialization #6717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 43 additions & 22 deletions targets/TARGET_NORDIC/TARGET_NRF5x/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
spi_inst->event = 0;
#endif

/* Configuration has changed, set flag to force update. */
spi_inst->update = true;
/* Configure peripheral. This is called on each init to ensure all pins are set correctly
* according to the SPI mode before calling CS for the first time.
*/
spi_configure_driver_instance(obj);

/* Configure GPIO pin if chip select has been set. */
if (ssel != NC) {
Expand Down Expand Up @@ -223,19 +225,31 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)
struct spi_s *spi_inst = obj;
#endif

nrf_drv_spi_mode_t new_mode = NRF_DRV_SPI_MODE_0;

/* Convert Mbed HAL mode to Nordic mode. */
if(mode == 0) {
spi_inst->config.mode = NRF_DRV_SPI_MODE_0;
new_mode = NRF_DRV_SPI_MODE_0;
} else if(mode == 1) {
spi_inst->config.mode = NRF_DRV_SPI_MODE_1;
new_mode = NRF_DRV_SPI_MODE_1;
} else if(mode == 2) {
spi_inst->config.mode = NRF_DRV_SPI_MODE_2;
new_mode = NRF_DRV_SPI_MODE_2;
} else if(mode == 3) {
spi_inst->config.mode = NRF_DRV_SPI_MODE_3;
new_mode = NRF_DRV_SPI_MODE_3;
}

/* Configuration has changed, set flag to force application. */
spi_inst->update = true;
/* Check if configuration has changed. */
if (spi_inst->config.mode != new_mode) {
spi_inst->config.mode = new_mode;

/* Set flag to force update. */
spi_inst->update = true;
}

/* Configure peripheral if necessary. Must be called on each format to ensure the pins are set
* correctly according to the SPI mode.
*/
spi_configure_driver_instance(obj);
}

/** Set the SPI baud rate
Expand All @@ -253,25 +267,32 @@ void spi_frequency(spi_t *obj, int hz)
struct spi_s *spi_inst = obj;
#endif

nrf_drv_spi_frequency_t new_frequency = NRF_DRV_SPI_FREQ_1M;

/* Convert frequency to Nordic enum type. */
if (hz < 250000) {
spi_inst->config.frequency = NRF_DRV_SPI_FREQ_125K;
new_frequency = NRF_DRV_SPI_FREQ_125K;
} else if (hz < 500000) {
spi_inst->config.frequency = NRF_DRV_SPI_FREQ_250K;
new_frequency = NRF_DRV_SPI_FREQ_250K;
} else if (hz < 1000000) {
spi_inst->config.frequency = NRF_DRV_SPI_FREQ_500K;
new_frequency = NRF_DRV_SPI_FREQ_500K;
} else if (hz < 2000000) {
spi_inst->config.frequency = NRF_DRV_SPI_FREQ_1M;
new_frequency = NRF_DRV_SPI_FREQ_1M;
} else if (hz < 4000000) {
spi_inst->config.frequency = NRF_DRV_SPI_FREQ_2M;
new_frequency = NRF_DRV_SPI_FREQ_2M;
} else if (hz < 8000000) {
spi_inst->config.frequency = NRF_DRV_SPI_FREQ_4M;
new_frequency = NRF_DRV_SPI_FREQ_4M;
} else {
spi_inst->config.frequency = NRF_DRV_SPI_FREQ_8M;
new_frequency = NRF_DRV_SPI_FREQ_8M;
}

/* Configuration has changed, set flag to force application. */
spi_inst->update = true;
/* Check if configuration has changed. */
if (spi_inst->config.frequency != new_frequency) {
spi_inst->config.frequency = new_frequency;

/* Set flag to force update. */
spi_inst->update = true;
}
}

/** Write a byte out in master mode and receive a value
Expand All @@ -290,18 +311,18 @@ int spi_master_write(spi_t *obj, int value)

int instance = spi_inst->instance;

/* Manually clear chip select pin if defined. */
if (spi_inst->cs != NC) {
nrf_gpio_pin_clear(spi_inst->cs);
}

/* Local variables used in transfer. */
const uint8_t tx_buff = (uint8_t) value;
uint8_t rx_buff;

/* Configure peripheral if necessary. */
spi_configure_driver_instance(obj);

/* Manually clear chip select pin if defined. */
if (spi_inst->cs != NC) {
nrf_gpio_pin_clear(spi_inst->cs);
}

/* Transfer 1 byte. */
nrf_drv_spi_transfer(&nordic_nrf5_spi_instance[instance], &tx_buff, 1, &rx_buff, 1);

Expand Down