Skip to content

Commit 4f20cfc

Browse files
mapellilLMESTM
authored andcommitted
STM32: SPI: SPI3W / SPI_DIRECTION_1LINE management
In case MISO is not passed at SPI init, then we consider a 3 wires SPI configuration is requested, which corresponds to SPI_DIRECTION_1LINE configuration parameter in STM32 HAL layer. We're then handling this specific case of SPI_DIRECTION_1LINE, in spi_master_write or spi_master_block_write, we call to HAL API
1 parent 99a8467 commit 4f20cfc

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

targets/TARGET_STM/stm_spi_api.c

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,13 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
156156
handle->Instance = SPI_INST(obj);
157157
handle->Init.Mode = SPI_MODE_MASTER;
158158
handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
159-
handle->Init.Direction = SPI_DIRECTION_2LINES;
159+
160+
if (miso != NC) {
161+
handle->Init.Direction = SPI_DIRECTION_2LINES;
162+
} else {
163+
handle->Init.Direction = SPI_DIRECTION_1LINE;
164+
}
165+
160166
handle->Init.CLKPhase = SPI_PHASE_1EDGE;
161167
handle->Init.CLKPolarity = SPI_POLARITY_LOW;
162168
handle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
@@ -353,6 +359,10 @@ int spi_master_write(spi_t *obj, int value)
353359
struct spi_s *spiobj = SPI_S(obj);
354360
SPI_HandleTypeDef *handle = &(spiobj->handle);
355361

362+
if (handle->Init.Direction == SPI_DIRECTION_1LINE) {
363+
return HAL_SPI_Transmit(handle, (uint8_t*)&value, 1, 10);
364+
}
365+
356366
#if defined(LL_SPI_RX_FIFO_TH_HALF)
357367
/* Configure the default data size */
358368
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {
@@ -390,13 +400,31 @@ int spi_master_write(spi_t *obj, int value)
390400
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
391401
char *rx_buffer, int rx_length, char write_fill)
392402
{
403+
struct spi_s *spiobj = SPI_S(obj);
404+
SPI_HandleTypeDef *handle = &(spiobj->handle);
393405
int total = (tx_length > rx_length) ? tx_length : rx_length;
394-
395-
for (int i = 0; i < total; i++) {
396-
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
397-
char in = spi_master_write(obj, out);
398-
if (i < rx_length) {
399-
rx_buffer[i] = in;
406+
int i = 0;
407+
if (handle->Init.Direction == SPI_DIRECTION_2LINES) {
408+
for (i = 0; i < total; i++) {
409+
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
410+
char in = spi_master_write(obj, out);
411+
if (i < rx_length) {
412+
rx_buffer[i] = in;
413+
}
414+
}
415+
} else {
416+
/* In case of 1 WIRE only, first handle TX, then Rx */
417+
if (tx_length != 0) {
418+
if (HAL_OK != HAL_SPI_Transmit(handle, (uint8_t*)tx_buffer, tx_length, tx_length*10)) {
419+
/* report an error */
420+
total = 0;
421+
}
422+
}
423+
if (rx_length != 0) {
424+
if (HAL_OK != HAL_SPI_Receive(handle, (uint8_t*)rx_buffer, rx_length, rx_length*10)) {
425+
/* report an error */
426+
total = 0;
427+
}
400428
}
401429
}
402430

0 commit comments

Comments
 (0)