@@ -288,7 +288,15 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
288
288
port = set_GPIO_Port_Clock (STM_PORT (obj -> pin_sclk ));
289
289
GPIO_InitStruct .Pin = STM_GPIO_PIN (obj -> pin_sclk );
290
290
GPIO_InitStruct .Mode = STM_PIN_MODE (pinmap_function (obj -> pin_sclk ,PinMap_SPI_SCLK ));
291
- GPIO_InitStruct .Pull = STM_PIN_PUPD (pinmap_function (obj -> pin_sclk ,PinMap_SPI_SCLK ));
291
+ /*
292
+ * According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
293
+ * or PULLUP the SCK pin according the polarity used.
294
+ */
295
+ if (handle -> Init .CLKPolarity == SPI_POLARITY_LOW ) {
296
+ GPIO_InitStruct .Pull = GPIO_PULLDOWN ;
297
+ } else {
298
+ GPIO_InitStruct .Pull = GPIO_PULLUP ;
299
+ }
292
300
GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_HIGH ;
293
301
GPIO_InitStruct .Alternate = STM_PIN_AFNUM (pinmap_function (obj -> pin_sclk ,PinMap_SPI_SCLK ));
294
302
HAL_GPIO_Init (port , & GPIO_InitStruct );
@@ -342,6 +350,9 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
342
350
#endif
343
351
344
352
HAL_SPI_Init (handle );
353
+
354
+ /* In order to set correctly the SPI polarity we need to enable the peripheral */
355
+ __HAL_SPI_ENABLE (handle );
345
356
}
346
357
347
358
/**
@@ -358,58 +369,49 @@ void spi_deinit(spi_t *obj)
358
369
SPI_HandleTypeDef * handle = & (obj -> handle );
359
370
360
371
HAL_SPI_DeInit (handle );
361
- }
362
372
363
- /**
364
- * @brief De-Initialize the SPI MSP.
365
- * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
366
- * the configuration information for SPI module.
367
- * @retval None
368
- */
369
- void HAL_SPI_MspDeInit (SPI_HandleTypeDef * hspi )
370
- {
371
373
#if defined SPI1_BASE
372
374
// Reset SPI and disable clock
373
- if (hspi -> Instance == SPI1 ) {
375
+ if (handle -> Instance == SPI1 ) {
374
376
__HAL_RCC_SPI1_FORCE_RESET ();
375
377
__HAL_RCC_SPI1_RELEASE_RESET ();
376
378
__HAL_RCC_SPI1_CLK_DISABLE ();
377
379
}
378
380
#endif
379
381
#if defined SPI2_BASE
380
- if (hspi -> Instance == SPI2 ) {
382
+ if (handle -> Instance == SPI2 ) {
381
383
__HAL_RCC_SPI2_FORCE_RESET ();
382
384
__HAL_RCC_SPI2_RELEASE_RESET ();
383
385
__HAL_RCC_SPI2_CLK_DISABLE ();
384
386
}
385
387
#endif
386
388
387
389
#if defined SPI3_BASE
388
- if (hspi -> Instance == SPI3 ) {
390
+ if (handle -> Instance == SPI3 ) {
389
391
__HAL_RCC_SPI3_FORCE_RESET ();
390
392
__HAL_RCC_SPI3_RELEASE_RESET ();
391
393
__HAL_RCC_SPI3_CLK_DISABLE ();
392
394
}
393
395
#endif
394
396
395
397
#if defined SPI4_BASE
396
- if (hspi -> Instance == SPI4 ) {
398
+ if (handle -> Instance == SPI4 ) {
397
399
__HAL_RCC_SPI4_FORCE_RESET ();
398
400
__HAL_RCC_SPI4_RELEASE_RESET ();
399
401
__HAL_RCC_SPI4_CLK_DISABLE ();
400
402
}
401
403
#endif
402
404
403
405
#if defined SPI5_BASE
404
- if (hspi -> Instance == SPI5 ) {
406
+ if (handle -> Instance == SPI5 ) {
405
407
__HAL_RCC_SPI5_FORCE_RESET ();
406
408
__HAL_RCC_SPI5_RELEASE_RESET ();
407
409
__HAL_RCC_SPI5_CLK_DISABLE ();
408
410
}
409
411
#endif
410
412
411
413
#if defined SPI6_BASE
412
- if (hspi -> Instance == SPI6 ) {
414
+ if (handle -> Instance == SPI6 ) {
413
415
__HAL_RCC_SPI6_FORCE_RESET ();
414
416
__HAL_RCC_SPI6_RELEASE_RESET ();
415
417
__HAL_RCC_SPI6_CLK_DISABLE ();
0 commit comments