From a2baafe8a7c33245805d2490c34c0c54b0a1227b Mon Sep 17 00:00:00 2001 From: alfran Date: Fri, 17 Feb 2017 18:05:01 +0100 Subject: [PATCH 1/2] SPI rework: added SPI1 --- cores/arduino/SPI.cpp | 148 ++++++++++++++++++++++++++++-------------- cores/arduino/SPI.h | 76 +++++++++++++--------- 2 files changed, 144 insertions(+), 80 deletions(-) mode change 100755 => 100644 cores/arduino/SPI.cpp mode change 100755 => 100644 cores/arduino/SPI.h diff --git a/cores/arduino/SPI.cpp b/cores/arduino/SPI.cpp old mode 100755 new mode 100644 index 9b7b996..c89d9e8 --- a/cores/arduino/SPI.cpp +++ b/cores/arduino/SPI.cpp @@ -11,12 +11,16 @@ * published by the Free Software Foundation. */ + /* + * Arduino srl - www.arduino.org + * 2016 Jun 9: Edited Francesco Alessi (alfran) - francesco@arduino.org + */ + extern "C" { #include #include #include #include "stm32f4xx_hal.h" - } #include "SPI.h" @@ -27,51 +31,80 @@ SPIClass::SPIClass(SPI_TypeDef *spiInstance) } /** - * @brief Configure SPI, configure relatives IOs and enable SPIx + * @brief Configure SPI, configure relatives IOs and enable SPI2 * @param None * @retval None */ void SPIClass::begin() { - if(hSPIx.Instance == SPIx) + if(hSPIx.Instance == HAL_SPI2) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable GPIO clock */ - SPIx_SCK_CLK_ENABLE(); - SPIx_MISO_CLK_ENABLE(); - SPIx_MOSI_CLK_ENABLE(); + SPI2_SCK_CLK_ENABLE(); + SPI2_MISO_CLK_ENABLE(); + SPI2_MOSI_CLK_ENABLE(); - /* Enable SPI clock */ - SPIx_CLK_ENABLE(); + /* Enable SPI2 clock */ + SPI2_CLK_ENABLE(); - /* Configure SPI SCK pin(PB10) as alternate function */ - GPIO_InitStruct.Pin = SPIx_SCK_PIN; + /* Configure SPI2 SCK pin(PD3) as alternate function */ + GPIO_InitStruct.Pin = SPI2_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - GPIO_InitStruct.Alternate = SPIx_SCK_AF; - HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct); + GPIO_InitStruct.Alternate = SPI2_SCK_AF; + HAL_GPIO_Init(SPI2_SCK_GPIO_PORT, &GPIO_InitStruct); + + /* Configure SPI2 MISO pin(PB14) as alternate function */ + GPIO_InitStruct.Pin = SPI2_MISO_PIN; + GPIO_InitStruct.Alternate = SPI2_MISO_AF; + HAL_GPIO_Init(SPI2_MISO_GPIO_PORT, &GPIO_InitStruct); + + /* Configure SPI2 MOSI pin(PC3) as alternate function */ + GPIO_InitStruct.Pin = SPI2_MOSI_PIN; + GPIO_InitStruct.Alternate = SPI2_MOSI_AF; + HAL_GPIO_Init(SPI2_MOSI_GPIO_PORT, &GPIO_InitStruct); + } else if (hSPIx.Instance == HAL_SPI1) + { + GPIO_InitTypeDef GPIO_InitStruct; - /* Configure SPI MISO pin(PB14) as alternate function */ - GPIO_InitStruct.Pin = SPIx_MISO_PIN; - GPIO_InitStruct.Alternate = SPIx_MISO_AF; - HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct); + /* Enable GPIO clock */ + SPI1_SCK_CLK_ENABLE(); + SPI1_MISO_CLK_ENABLE(); + SPI1_MOSI_CLK_ENABLE(); + SPI1_NSS_CLK_ENABLE(); + + /* Enable SPI1 clock */ + SPI1_CLK_ENABLE(); - /* Configure SPI MOSI pin(PB15) as alternate function */ - GPIO_InitStruct.Pin = SPIx_MOSI_PIN; - GPIO_InitStruct.Alternate = SPIx_MOSI_AF; - HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct); + /* Configure SPI1 SCK pin(PB3) as alternate function */ + GPIO_InitStruct.Pin = SPI1_SCK_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Alternate = SPI1_SCK_AF; + HAL_GPIO_Init(SPI1_SCK_GPIO_PORT, &GPIO_InitStruct); - /* Put NSS pin as Output pin in order to be used as normal GPIO in Master mode */ - GPIO_InitStruct.Pin = SPIx_NSS_PIN; + /* Configure SPI1 MISO pin(PB4) as alternate function */ + GPIO_InitStruct.Pin = SPI1_MISO_PIN; + GPIO_InitStruct.Alternate = SPI1_MISO_AF; + HAL_GPIO_Init(SPI1_MISO_GPIO_PORT, &GPIO_InitStruct); + + /* Configure SPI1 MOSI pin(PB5) as alternate function */ + GPIO_InitStruct.Pin = SPI1_MOSI_PIN; + GPIO_InitStruct.Alternate = SPI1_MOSI_AF; + HAL_GPIO_Init(SPI1_MOSI_GPIO_PORT, &GPIO_InitStruct); + + /* Put SPI1 NSS pin as Output pin in order to be used as normal GPIO in Master mode */ + GPIO_InitStruct.Pin = SPI1_NSS_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(SPIx_NSS_GPIO_PORT, &GPIO_InitStruct); + HAL_GPIO_Init(SPI1_NSS_GPIO_PORT, &GPIO_InitStruct); } - /* SPIx configuration ----------------------------------------------------*/ - hSPIx.Instance = SPIx; + /* SPI general configuration ----------------------------------------------------*/ hSPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; hSPIx.Init.Direction = SPI_DIRECTION_2LINES; hSPIx.Init.CLKPhase = SPI_PHASE_1EDGE; @@ -99,22 +132,21 @@ void SPIClass::begin(uint8_t slaveSelectPin) /* TOBEFIXED: The correct way to proceed here it is to map the Arduino slaveSelectPin provided as parameter */ /* with the Cube GPIO port and pin number and then call the HAL_GPIO_Init with the correct values */ - /* At the moment only NSS pin is supported */ + /* At the moment only pin 10 is supported */ switch(slaveSelectPin) { - case 10: + case 10: /* Pin(PA15) */ default: { GPIO_InitTypeDef GPIO_InitStruct; - SPIx_NSS_CLK_ENABLE(); - GPIO_InitStruct.Pin = SPIx_NSS_PIN; - GPIO_InitStruct.Alternate = SPIx_NSS_AF; + __HAL_RCC_GPIOA_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - HAL_GPIO_Init(SPIx_NSS_GPIO_PORT, &GPIO_InitStruct); + HAL_GPIO_Init(HAL_GPIOA, &GPIO_InitStruct); break; } } @@ -185,7 +217,13 @@ uint16_t SPIClass::transfer16(uint16_t data) void SPIClass::transfer(void *buf, size_t count) { uint8_t rxdata; - HAL_SPI_TransmitReceive(&hSPIx, (uint8_t*)buf, (uint8_t*)&rxdata, count,5000); + int i; + + for(i = 0; i < count; i++) + { + HAL_SPI_TransmitReceive(&hSPIx, ((uint8_t*)buf + i), (uint8_t*)&rxdata, 1,5000); + *((uint8_t*)buf + i) = rxdata; + } } /** @@ -201,10 +239,10 @@ uint8_t SPIClass::transfer(uint8_t slaveSelectPin, uint8_t val, SPITransferMode /* with the Cube GPIO port and pin number and then call the HAL_GPIO_WritePin */ switch(slaveSelectPin) { - case 10: + case 10: /* Pin(PA15) */ default: { - HAL_GPIO_WritePin(SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN, GPIO_PIN_RESET); + HAL_GPIO_WritePin(HAL_GPIOA, GPIO_PIN_15, GPIO_PIN_RESET); break; } } @@ -217,10 +255,10 @@ uint8_t SPIClass::transfer(uint8_t slaveSelectPin, uint8_t val, SPITransferMode { switch(slaveSelectPin) { - case 10: + case 10: /* Pin(PA15) */ default: { - HAL_GPIO_WritePin(SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN, GPIO_PIN_SET); + HAL_GPIO_WritePin(HAL_GPIOA, GPIO_PIN_15, GPIO_PIN_SET); break; } } @@ -302,19 +340,32 @@ void SPIClass::setClockDivider(uint8_t clockDiv) */ void SPIClass::end() { - if(hSPIx.Instance == SPIx) + if(hSPIx.Instance == HAL_SPI2) { /*##-1- Reset peripherals ##################################################*/ - SPIx_FORCE_RESET(); - SPIx_RELEASE_RESET(); + SPI2_FORCE_RESET(); + SPI2_RELEASE_RESET(); /*##-2- Disable peripherals and GPIO Clocks ################################*/ - /* Configure SPI SCK as alternate function */ - HAL_GPIO_DeInit(SPIx_SCK_GPIO_PORT, SPIx_SCK_PIN); - /* Configure SPI MISO as alternate function */ - HAL_GPIO_DeInit(SPIx_MISO_GPIO_PORT, SPIx_MISO_PIN); - /* Configure SPI MOSI as alternate function */ - HAL_GPIO_DeInit(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_PIN); + /* Configure SPI2 SCK as alternate function */ + HAL_GPIO_DeInit(SPI2_SCK_GPIO_PORT, SPI2_SCK_PIN); + /* Configure SPI2 MISO as alternate function */ + HAL_GPIO_DeInit(SPI2_MISO_GPIO_PORT, SPI2_MISO_PIN); + /* Configure SPI2 MOSI as alternate function */ + HAL_GPIO_DeInit(SPI2_MOSI_GPIO_PORT, SPI2_MOSI_PIN); + } else if (hSPIx.Instance == HAL_SPI1) + { + /*##-1- Reset peripherals ##################################################*/ + SPI1_FORCE_RESET(); + SPI1_RELEASE_RESET(); + + /*##-2- Disable peripherals and GPIO Clocks ################################*/ + /* Configure SPI1 SCK as alternate function */ + HAL_GPIO_DeInit(SPI1_SCK_GPIO_PORT, SPI1_SCK_PIN); + /* Configure SPI1 MISO as alternate function */ + HAL_GPIO_DeInit(SPI1_MISO_GPIO_PORT, SPI1_MISO_PIN); + /* Configure SPI1 MOSI as alternate function */ + HAL_GPIO_DeInit(SPI1_MOSI_GPIO_PORT, SPI1_MOSI_PIN); } HAL_SPI_DeInit(&hSPIx); @@ -331,15 +382,16 @@ void SPIClass::end(uint8_t slaveSelectPin) /* with the Cube GPIO port and pin number and then call the HAL_GPIO_DeInit with the correct values */ switch(slaveSelectPin) { - case 10: + case 10: /* Pin(PA15) */ default: { - HAL_GPIO_DeInit(SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN); + HAL_GPIO_DeInit(HAL_GPIOA, GPIO_PIN_15); break; } } - HAL_SPI_DeInit(&hSPIx); + end(); } SPIClass SPI = SPIClass(HAL_SPI2); +SPIClass SPI1 = SPIClass(HAL_SPI1); diff --git a/cores/arduino/SPI.h b/cores/arduino/SPI.h old mode 100755 new mode 100644 index ecd9986..332ed59 --- a/cores/arduino/SPI.h +++ b/cores/arduino/SPI.h @@ -16,32 +16,48 @@ #include -#define SPIx HAL_SPI2 -#define SPIx_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE() -#define SPIx_SCK_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE() -#define SPIx_MISO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() -#define SPIx_MOSI_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() -#define SPIx_NSS_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() - -#define SPIx_FORCE_RESET() __HAL_RCC_SPI2_FORCE_RESET() -#define SPIx_RELEASE_RESET() __HAL_RCC_SPI2_RELEASE_RESET() -#define SPIx_NSS_CLK_DISABLE __HAL_RCC_GPIOA_CLK_DISABLE - -/* Definition for SPIx Pins */ -#define SPIx_SCK_PIN GPIO_PIN_3 -#define SPIx_SCK_GPIO_PORT HAL_GPIOD -#define SPIx_SCK_AF GPIO_AF5_SPI2 -#define SPIx_MISO_PIN GPIO_PIN_14 -#define SPIx_MISO_GPIO_PORT HAL_GPIOB -#define SPIx_MISO_AF GPIO_AF5_SPI2 -#define SPIx_MOSI_PIN GPIO_PIN_3 -#define SPIx_MOSI_GPIO_PORT HAL_GPIOC -#define SPIx_MOSI_AF GPIO_AF5_SPI2 - -#define SPIx_NSS_PIN GPIO_PIN_15 -#define SPIx_NSS_GPIO_PORT HAL_GPIOA -#define SPIx_NSS_AF GPIO_AF5_SPI2 - +#define SPI2_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE() +#define SPI2_SCK_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE() +#define SPI2_MISO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() +#define SPI2_MOSI_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE() + +#define SPI2_FORCE_RESET() __HAL_RCC_SPI2_FORCE_RESET() +#define SPI2_RELEASE_RESET() __HAL_RCC_SPI2_RELEASE_RESET() + +/* Definition for SPI2 Pins */ +#define SPI2_SCK_PIN GPIO_PIN_3 +#define SPI2_SCK_GPIO_PORT HAL_GPIOD +#define SPI2_SCK_AF GPIO_AF5_SPI2 +#define SPI2_MISO_PIN GPIO_PIN_14 +#define SPI2_MISO_GPIO_PORT HAL_GPIOB +#define SPI2_MISO_AF GPIO_AF5_SPI2 +#define SPI2_MOSI_PIN GPIO_PIN_3 +#define SPI2_MOSI_GPIO_PORT HAL_GPIOC +#define SPI2_MOSI_AF GPIO_AF5_SPI2 + +#define SPI1_CLK_ENABLE() __HAL_RCC_SPI1_CLK_ENABLE() +#define SPI1_SCK_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() +#define SPI1_MISO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() +#define SPI1_MOSI_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() +#define SPI1_NSS_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() + +#define SPI1_FORCE_RESET() __HAL_RCC_SPI1_FORCE_RESET() +#define SPI1_RELEASE_RESET() __HAL_RCC_SPI1_RELEASE_RESET() + +/* Definition for SPI2 Pins */ +#define SPI1_SCK_PIN GPIO_PIN_3 +#define SPI1_SCK_GPIO_PORT HAL_GPIOB +#define SPI1_SCK_AF GPIO_AF5_SPI1 +#define SPI1_MISO_PIN GPIO_PIN_4 +#define SPI1_MISO_GPIO_PORT HAL_GPIOB +#define SPI1_MISO_AF GPIO_AF5_SPI1 +#define SPI1_MOSI_PIN GPIO_PIN_5 +#define SPI1_MOSI_GPIO_PORT HAL_GPIOB +#define SPI1_MOSI_AF GPIO_AF5_SPI1 + +#define SPI1_NSS_PIN GPIO_PIN_15 +#define SPI1_NSS_GPIO_PORT HAL_GPIOA +#define SPI1_NSS_AF GPIO_AF5_SPI1 #ifndef LSBFIRST #define LSBFIRST 0 @@ -50,9 +66,6 @@ #define MSBFIRST 1 #endif - - - #define SPI_CLOCK_DIV2 SPI_BAUDRATEPRESCALER_2 #define SPI_CLOCK_DIV4 SPI_BAUDRATEPRESCALER_4 #define SPI_CLOCK_DIV8 SPI_BAUDRATEPRESCALER_8 @@ -72,7 +85,6 @@ enum SPITransferMode { SPI_LAST }; - class SPISettings { public: SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) @@ -138,7 +150,6 @@ class SPISettings { friend class SPIClass; }; - class SPIClass { public: /* Constructor */ @@ -182,5 +193,6 @@ class SPIClass { }; extern SPIClass SPI; -//extern SPISettings SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0); +extern SPIClass SPI1; + #endif From ed94251d53c4180a2009ed5c30205059f460c0fb Mon Sep 17 00:00:00 2001 From: alfran Date: Mon, 20 Feb 2017 11:08:45 +0100 Subject: [PATCH 2/2] Improoved SPI, now is possible to use all pins as NSS in SPIx.begin(pin) --- cores/arduino/SPI.cpp | 85 +++++++++---------------------------------- 1 file changed, 18 insertions(+), 67 deletions(-) diff --git a/cores/arduino/SPI.cpp b/cores/arduino/SPI.cpp index c89d9e8..384da68 100644 --- a/cores/arduino/SPI.cpp +++ b/cores/arduino/SPI.cpp @@ -49,7 +49,7 @@ void SPIClass::begin() /* Enable SPI2 clock */ SPI2_CLK_ENABLE(); - /* Configure SPI2 SCK pin(PD3) as alternate function */ + /* Configure SPI2 SCK PIN defined in SPI.h */ GPIO_InitStruct.Pin = SPI2_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; @@ -57,16 +57,17 @@ void SPIClass::begin() GPIO_InitStruct.Alternate = SPI2_SCK_AF; HAL_GPIO_Init(SPI2_SCK_GPIO_PORT, &GPIO_InitStruct); - /* Configure SPI2 MISO pin(PB14) as alternate function */ + /* Configure SPI2 MISO PIN defined in SPI.h */ GPIO_InitStruct.Pin = SPI2_MISO_PIN; GPIO_InitStruct.Alternate = SPI2_MISO_AF; HAL_GPIO_Init(SPI2_MISO_GPIO_PORT, &GPIO_InitStruct); - /* Configure SPI2 MOSI pin(PC3) as alternate function */ + /* Configure SPI2 MOSI PIN defined in SPI.h */ GPIO_InitStruct.Pin = SPI2_MOSI_PIN; GPIO_InitStruct.Alternate = SPI2_MOSI_AF; HAL_GPIO_Init(SPI2_MOSI_GPIO_PORT, &GPIO_InitStruct); - } else if (hSPIx.Instance == HAL_SPI1) + } + else if (hSPIx.Instance == HAL_SPI1) { GPIO_InitTypeDef GPIO_InitStruct; @@ -79,7 +80,7 @@ void SPIClass::begin() /* Enable SPI1 clock */ SPI1_CLK_ENABLE(); - /* Configure SPI1 SCK pin(PB3) as alternate function */ + /* Configure SPI1 SCK PIN defined in SPI.h */ GPIO_InitStruct.Pin = SPI1_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; @@ -87,24 +88,24 @@ void SPIClass::begin() GPIO_InitStruct.Alternate = SPI1_SCK_AF; HAL_GPIO_Init(SPI1_SCK_GPIO_PORT, &GPIO_InitStruct); - /* Configure SPI1 MISO pin(PB4) as alternate function */ + /* Configure SPI1 MISO PIN defined in SPI.h */ GPIO_InitStruct.Pin = SPI1_MISO_PIN; GPIO_InitStruct.Alternate = SPI1_MISO_AF; HAL_GPIO_Init(SPI1_MISO_GPIO_PORT, &GPIO_InitStruct); - /* Configure SPI1 MOSI pin(PB5) as alternate function */ + /* Configure SPI1 MOSI PIN defined in SPI.h */ GPIO_InitStruct.Pin = SPI1_MOSI_PIN; GPIO_InitStruct.Alternate = SPI1_MOSI_AF; HAL_GPIO_Init(SPI1_MOSI_GPIO_PORT, &GPIO_InitStruct); /* Put SPI1 NSS pin as Output pin in order to be used as normal GPIO in Master mode */ GPIO_InitStruct.Pin = SPI1_NSS_PIN; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(SPI1_NSS_GPIO_PORT, &GPIO_InitStruct); } - /* SPI general configuration ----------------------------------------------------*/ + /* SPI general configuration */ hSPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; hSPIx.Init.Direction = SPI_DIRECTION_2LINES; hSPIx.Init.CLKPhase = SPI_PHASE_1EDGE; @@ -127,29 +128,9 @@ void SPIClass::begin() */ void SPIClass::begin(uint8_t slaveSelectPin) { - /* SPIx configuration */ + /* SPIx configuration */ begin(); - - /* TOBEFIXED: The correct way to proceed here it is to map the Arduino slaveSelectPin provided as parameter */ - /* with the Cube GPIO port and pin number and then call the HAL_GPIO_Init with the correct values */ - /* At the moment only pin 10 is supported */ - switch(slaveSelectPin) - { - case 10: /* Pin(PA15) */ - default: - { - GPIO_InitTypeDef GPIO_InitStruct; - - __HAL_RCC_GPIOA_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_15; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - - HAL_GPIO_Init(HAL_GPIOA, &GPIO_InitStruct); - break; - } - } + pinMode(slaveSelectPin, OUTPUT); } /** @@ -234,34 +215,14 @@ void SPIClass::transfer(void *buf, size_t count) uint8_t SPIClass::transfer(uint8_t slaveSelectPin, uint8_t val, SPITransferMode transferMode) { uint8_t rxdata; - - /* TOBEFIXED: The correct way to proceed here it is to map the Arduino pin provided as parameter */ - /* with the Cube GPIO port and pin number and then call the HAL_GPIO_WritePin */ - switch(slaveSelectPin) - { - case 10: /* Pin(PA15) */ - default: - { - HAL_GPIO_WritePin(HAL_GPIOA, GPIO_PIN_15, GPIO_PIN_RESET); - break; - } - } - + digitalWrite(slaveSelectPin,LOW); HAL_SPI_TransmitReceive(&hSPIx, (uint8_t *)&val, (uint8_t*)&rxdata, 1,5000); /* If transferMode is SPI_CONTINUE we need to hold CS GPIO pin low */ /* If transferMode is SPI_LAST we need to put CS GPIO pin high */ if(transferMode == SPI_LAST) { - switch(slaveSelectPin) - { - case 10: /* Pin(PA15) */ - default: - { - HAL_GPIO_WritePin(HAL_GPIOA, GPIO_PIN_15, GPIO_PIN_SET); - break; - } - } + digitalWrite(slaveSelectPin, HIGH); } return rxdata; @@ -353,7 +314,8 @@ void SPIClass::end() HAL_GPIO_DeInit(SPI2_MISO_GPIO_PORT, SPI2_MISO_PIN); /* Configure SPI2 MOSI as alternate function */ HAL_GPIO_DeInit(SPI2_MOSI_GPIO_PORT, SPI2_MOSI_PIN); - } else if (hSPIx.Instance == HAL_SPI1) + } + else if (hSPIx.Instance == HAL_SPI1) { /*##-1- Reset peripherals ##################################################*/ SPI1_FORCE_RESET(); @@ -378,18 +340,7 @@ void SPIClass::end() */ void SPIClass::end(uint8_t slaveSelectPin) { - /* TOBEFIXED: The correct way to proceed here it is to map the Arduino slaveSelectPin provided as parameter */ - /* with the Cube GPIO port and pin number and then call the HAL_GPIO_DeInit with the correct values */ - switch(slaveSelectPin) - { - case 10: /* Pin(PA15) */ - default: - { - HAL_GPIO_DeInit(HAL_GPIOA, GPIO_PIN_15); - break; - } - } - + pinMode(slaveSelectPin, INPUT); end(); }