Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

SPI rework: added SPI1 #5

Merged
merged 2 commits into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 101 additions & 98 deletions cores/arduino/SPI.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "stm32f4xx_hal.h"

}

#include "SPI.h"
Expand All @@ -27,51 +31,81 @@ 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 defined in SPI.h */
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);

/* 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);

/* 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);

/* Put NSS pin as Output pin in order to be used as normal GPIO in Master mode */
GPIO_InitStruct.Pin = SPIx_NSS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(SPIx_NSS_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = SPI2_SCK_AF;
HAL_GPIO_Init(SPI2_SCK_GPIO_PORT, &GPIO_InitStruct);

/* 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 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)
{
GPIO_InitTypeDef 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();

/* SPIx configuration ----------------------------------------------------*/
hSPIx.Instance = SPIx;
/* 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;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = SPI1_SCK_AF;
HAL_GPIO_Init(SPI1_SCK_GPIO_PORT, &GPIO_InitStruct);

/* 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 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;
HAL_GPIO_Init(SPI1_NSS_GPIO_PORT, &GPIO_InitStruct);
}

/* SPI general configuration */
hSPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
hSPIx.Init.Direction = SPI_DIRECTION_2LINES;
hSPIx.Init.CLKPhase = SPI_PHASE_1EDGE;
Expand All @@ -94,30 +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 NSS pin is supported */
switch(slaveSelectPin)
{
case 10:
default:
{
GPIO_InitTypeDef GPIO_InitStruct;

SPIx_NSS_CLK_ENABLE();
GPIO_InitStruct.Pin = SPIx_NSS_PIN;
GPIO_InitStruct.Alternate = SPIx_NSS_AF;
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);
break;
}
}
pinMode(slaveSelectPin, OUTPUT);
}

/**
Expand Down Expand Up @@ -185,7 +198,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;
}
}

/**
Expand All @@ -196,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:
default:
{
HAL_GPIO_WritePin(SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN, 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:
default:
{
HAL_GPIO_WritePin(SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN, GPIO_PIN_SET);
break;
}
}
digitalWrite(slaveSelectPin, HIGH);
}

return rxdata;
Expand Down Expand Up @@ -302,19 +301,33 @@ void SPIClass::setClockDivider(uint8_t clockDiv)
*/
void SPIClass::end()
{
if(hSPIx.Instance == SPIx)
if(hSPIx.Instance == HAL_SPI2)
{
/*##-1- Reset peripherals ##################################################*/
SPI2_FORCE_RESET();
SPI2_RELEASE_RESET();

/*##-2- Disable peripherals and GPIO Clocks ################################*/
/* 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 ##################################################*/
SPIx_FORCE_RESET();
SPIx_RELEASE_RESET();
SPI1_FORCE_RESET();
SPI1_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 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);
Expand All @@ -327,19 +340,9 @@ 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:
default:
{
HAL_GPIO_DeInit(SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN);
break;
}
}

HAL_SPI_DeInit(&hSPIx);
pinMode(slaveSelectPin, INPUT);
end();
}

SPIClass SPI = SPIClass(HAL_SPI2);
SPIClass SPI1 = SPIClass(HAL_SPI1);
76 changes: 44 additions & 32 deletions cores/arduino/SPI.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,48 @@

#include <Arduino.h>

#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
Expand All @@ -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
Expand All @@ -72,7 +85,6 @@ enum SPITransferMode {
SPI_LAST
};


class SPISettings {
public:
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
Expand Down Expand Up @@ -138,7 +150,6 @@ class SPISettings {
friend class SPIClass;
};


class SPIClass {
public:
/* Constructor */
Expand Down Expand Up @@ -182,5 +193,6 @@ class SPIClass {
};

extern SPIClass SPI;
//extern SPISettings SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0);
extern SPIClass SPI1;

#endif