@@ -206,12 +206,15 @@ static uint32_t compute_disable_delay(spi_t *obj)
206206 * @param device : spi device mode: master or slave
207207 * @retval None
208208 */
209- void spi_init (spi_t * obj , uint32_t speed , SPIMode mode , uint8_t msb , SPIDeviceMode device )
209+ spi_status_e spi_init (spi_t * obj , uint32_t speed , SPIMode mode , uint8_t msb , SPIDeviceMode device )
210210{
211211 if (obj == NULL ) {
212- return ;
212+ return SPI_ERROR ;
213213 }
214214
215+ // Set the device mode before any other initialization
216+ obj -> mode = device ;
217+
215218 SPI_HandleTypeDef * handle = & (obj -> handle );
216219 uint32_t spi_freq = 0 ;
217220 uint32_t pull = 0 ;
@@ -226,9 +229,11 @@ void spi_init(spi_t *obj, uint32_t speed, SPIMode mode, uint8_t msb, SPIDeviceMo
226229 SPI_TypeDef * spi_ssel = pinmap_peripheral (obj -> pin_ssel , PinMap_SPI_SSEL );
227230
228231 /* Pins MOSI/MISO/SCLK must not be NP. ssel can be NP. */
229- if (spi_mosi == NP || spi_miso == NP || spi_sclk == NP ) {
230- core_debug ("ERROR: at least one SPI pin has no peripheral\n" );
231- return ;
232+ if (spi_mosi == NP || spi_miso == NP || spi_sclk == NP || spi_ssel == NP ) {
233+ if (spi_miso == NP && obj -> duplex ) {
234+ core_debug ("ERROR: at least one SPI pin has no peripheral\n" );
235+ return SPI_ERROR ;
236+ }
232237 }
233238
234239 SPI_TypeDef * spi_data = pinmap_merge_peripheral (spi_mosi , spi_miso );
@@ -239,27 +244,31 @@ void spi_init(spi_t *obj, uint32_t speed, SPIMode mode, uint8_t msb, SPIDeviceMo
239244 // Are all pins connected to the same SPI instance?
240245 if (spi_data == NP || spi_cntl == NP || obj -> spi == NP ) {
241246 core_debug ("ERROR: SPI pins mismatch\n" );
242- return ;
247+ return SPI_ERROR ;
243248 }
244249#if defined(SUBGHZSPI_BASE )
245250 } else {
246251 if (obj -> pin_mosi != NC || obj -> pin_miso != NC || obj -> pin_sclk != NC || obj -> pin_ssel != NC ) {
247252 core_debug ("ERROR: SUBGHZ_SPI cannot define custom pins\n" );
248- return ;
253+ return SPI_ERROR ;
249254 }
250255 }
251256#endif
252257
253258 // Configure the SPI pins
254259 if (obj -> pin_ssel != NC ) {
255- handle -> Init .NSS = SPI_NSS_HARD_OUTPUT ;
260+ if (obj -> mode == SPI_MODE_SLAVE ) {
261+ handle -> Init .NSS = SPI_NSS_HARD_INPUT ;
262+ } else {
263+ handle -> Init .NSS = SPI_NSS_HARD_OUTPUT ;
264+ }
256265 } else {
257266 handle -> Init .NSS = SPI_NSS_SOFT ;
258267 }
259268
260269 /* Fill default value */
261- handle -> Instance = obj -> spi ;
262- handle -> Init .Mode = ( device == SPI_MASTER ) ? SPI_MODE_MASTER : SPI_MODE_SLAVE ;
270+ handle -> Instance = obj -> spi ;
271+ handle -> Init .Mode = obj -> mode ;
263272
264273 spi_freq = spi_getClkFreqInst (obj -> spi );
265274 /* For SUBGHZSPI, 'SPI_BAUDRATEPRESCALER_*' == 'SUBGHZSPI_BAUDRATEPRESCALER_*' */
@@ -290,7 +299,7 @@ void spi_init(spi_t *obj, uint32_t speed, SPIMode mode, uint8_t msb, SPIDeviceMo
290299 obj -> disable_delay = compute_disable_delay (obj );
291300#endif
292301
293- handle -> Init .Direction = SPI_DIRECTION_2LINES ;
302+ handle -> Init .Direction = obj -> direction ;
294303
295304 if ((mode == SPI_MODE0 ) || (mode == SPI_MODE2 )) {
296305 handle -> Init .CLKPhase = SPI_PHASE_1EDGE ;
@@ -325,9 +334,22 @@ void spi_init(spi_t *obj, uint32_t speed, SPIMode mode, uint8_t msb, SPIDeviceMo
325334#if defined(SUBGHZSPI_BASE )
326335 if (handle -> Instance != SUBGHZSPI ) {
327336#endif
328- /* Configure SPI GPIO pins */
329- pinmap_pinout (obj -> pin_mosi , PinMap_SPI_MOSI );
330- pinmap_pinout (obj -> pin_miso , PinMap_SPI_MISO );
337+ /* Configure SPI GPIO pins based on device mode and duplex setting */
338+ if (obj -> mode == SPI_MODE_MASTER ) {
339+ /* Master mode: configure MOSI for output */
340+ pinmap_pinout (obj -> pin_mosi , PinMap_SPI_MOSI );
341+ /* Configure MISO for input if duplex is enabled */
342+ if (obj -> duplex ) {
343+ pinmap_pinout (obj -> pin_miso , PinMap_SPI_MISO );
344+ }
345+ } else {
346+ /* Slave mode: configure MISO for output */
347+ pinmap_pinout (obj -> pin_miso , PinMap_SPI_MISO );
348+ /* Configure MOSI for input if duplex is enabled */
349+ if (obj -> duplex ) {
350+ pinmap_pinout (obj -> pin_mosi , PinMap_SPI_MOSI );
351+ }
352+ }
331353 pinmap_pinout (obj -> pin_sclk , PinMap_SPI_SCLK );
332354 /*
333355 * According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
@@ -396,10 +418,15 @@ void spi_init(spi_t *obj, uint32_t speed, SPIMode mode, uint8_t msb, SPIDeviceMo
396418 }
397419#endif
398420
399- HAL_SPI_Init (handle );
421+ HAL_StatusTypeDef status = HAL_SPI_Init (handle );
422+ if (status != HAL_OK ) {
423+ core_debug ("ERROR: HAL_SPI_Init failed\n" );
424+ return SPI_ERROR ;
425+ }
400426
401427 /* In order to set correctly the SPI polarity we need to enable the peripheral */
402428 __HAL_SPI_ENABLE (handle );
429+ return SPI_OK ;
403430}
404431
405432/**
0 commit comments