Skip to content

Commit 5f4cbdd

Browse files
cparatafpistm
authored andcommitted
Fix SPI clock polarity issue
PULLDOWN or PULLUP the SCK pin according the polarity used. Fix #24 Signed-off-by: Carlo.Parata <[email protected]>
1 parent 5989d3e commit 5f4cbdd

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

Diff for: cores/arduino/stm32/spi_com.c

+18-16
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,15 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
288288
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_sclk));
289289
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_sclk);
290290
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+
}
292300
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
293301
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK));
294302
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)
342350
#endif
343351

344352
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);
345356
}
346357

347358
/**
@@ -358,58 +369,49 @@ void spi_deinit(spi_t *obj)
358369
SPI_HandleTypeDef *handle = &(obj->handle);
359370

360371
HAL_SPI_DeInit(handle);
361-
}
362372

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-
{
371373
#if defined SPI1_BASE
372374
// Reset SPI and disable clock
373-
if (hspi->Instance == SPI1) {
375+
if (handle->Instance == SPI1) {
374376
__HAL_RCC_SPI1_FORCE_RESET();
375377
__HAL_RCC_SPI1_RELEASE_RESET();
376378
__HAL_RCC_SPI1_CLK_DISABLE();
377379
}
378380
#endif
379381
#if defined SPI2_BASE
380-
if (hspi->Instance == SPI2) {
382+
if (handle->Instance == SPI2) {
381383
__HAL_RCC_SPI2_FORCE_RESET();
382384
__HAL_RCC_SPI2_RELEASE_RESET();
383385
__HAL_RCC_SPI2_CLK_DISABLE();
384386
}
385387
#endif
386388

387389
#if defined SPI3_BASE
388-
if (hspi->Instance == SPI3) {
390+
if (handle->Instance == SPI3) {
389391
__HAL_RCC_SPI3_FORCE_RESET();
390392
__HAL_RCC_SPI3_RELEASE_RESET();
391393
__HAL_RCC_SPI3_CLK_DISABLE();
392394
}
393395
#endif
394396

395397
#if defined SPI4_BASE
396-
if (hspi->Instance == SPI4) {
398+
if (handle->Instance == SPI4) {
397399
__HAL_RCC_SPI4_FORCE_RESET();
398400
__HAL_RCC_SPI4_RELEASE_RESET();
399401
__HAL_RCC_SPI4_CLK_DISABLE();
400402
}
401403
#endif
402404

403405
#if defined SPI5_BASE
404-
if (hspi->Instance == SPI5) {
406+
if (handle->Instance == SPI5) {
405407
__HAL_RCC_SPI5_FORCE_RESET();
406408
__HAL_RCC_SPI5_RELEASE_RESET();
407409
__HAL_RCC_SPI5_CLK_DISABLE();
408410
}
409411
#endif
410412

411413
#if defined SPI6_BASE
412-
if (hspi->Instance == SPI6) {
414+
if (handle->Instance == SPI6) {
413415
__HAL_RCC_SPI6_FORCE_RESET();
414416
__HAL_RCC_SPI6_RELEASE_RESET();
415417
__HAL_RCC_SPI6_CLK_DISABLE();

0 commit comments

Comments
 (0)