Support multiple SoftSPIs
This commit is contained in:
parent
e6d62c90e6
commit
e129b212f5
95
softspi.c
95
softspi.c
@ -1,93 +1,104 @@
|
|||||||
/**
|
/**
|
||||||
* @file softspi.c
|
* @file softspi.c
|
||||||
* @author Myth
|
* @author Myth
|
||||||
* @version 0.1
|
* @version 0.2
|
||||||
* @date 2021.10.12
|
* @date 2021.10.12
|
||||||
* @brief STM32 SoftSPI Library
|
* @brief STM32 SoftSPI Library
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "softspi.h"
|
#include "softspi.h"
|
||||||
|
|
||||||
SoftSPI_InitTypeDef config;
|
#define SCLK_Set HAL_GPIO_WritePin(SoftSPIx->SCLK_GPIO, SoftSPIx->SCLK_Pin, GPIO_PIN_SET)
|
||||||
|
#define SCLK_Clr HAL_GPIO_WritePin(SoftSPIx->SCLK_GPIO, SoftSPIx->SCLK_Pin, GPIO_PIN_RESET)
|
||||||
|
|
||||||
uint8_t GPIOx_Pin_Init(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode, uint32_t Pull);
|
#define MOSI_Set HAL_GPIO_WritePin(SoftSPIx->MOSI_GPIO, SoftSPIx->MOSI_Pin, GPIO_PIN_SET)
|
||||||
|
#define MOSI_Clr HAL_GPIO_WritePin(SoftSPIx->MOSI_GPIO, SoftSPIx->MOSI_Pin, GPIO_PIN_RESET)
|
||||||
|
|
||||||
HAL_StatusTypeDef SoftSPI_Init(SoftSPI_InitTypeDef *SoftSPI_Initure)
|
#define MISO_Read HAL_GPIO_ReadPin(SoftSPIx->MISO_GPIO, SoftSPIx->MISO_Pin)
|
||||||
|
|
||||||
|
#define SS_Set HAL_GPIO_WritePin(SoftSPIx->SS_GPIO, SoftSPIx->SS_Pin, GPIO_PIN_SET)
|
||||||
|
#define SS_Clr HAL_GPIO_WritePin(SoftSPIx->SS_GPIO, SoftSPIx->SS_Pin, GPIO_PIN_RESET)
|
||||||
|
|
||||||
|
#define Delay SoftSPI_Delay_us(SoftSPIx->Delay_Time)
|
||||||
|
|
||||||
|
uint8_t SoftSPI_GPIOx_Pin_Init(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode, uint32_t Pull);
|
||||||
|
|
||||||
|
HAL_StatusTypeDef SoftSPI_Init(SoftSPI_TypeDef *SoftSPIx)
|
||||||
{
|
{
|
||||||
config.SCLK_GPIO = SoftSPI_Initure->SCLK_GPIO;
|
SoftSPIx->SCLK_GPIO = SoftSPIx->SCLK_GPIO;
|
||||||
config.SCLK_Pin = SoftSPI_Initure->SCLK_Pin;
|
SoftSPIx->SCLK_Pin = SoftSPIx->SCLK_Pin;
|
||||||
|
|
||||||
if (!GPIOx_Pin_Init(config.SCLK_GPIO, config.SCLK_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
if (!SoftSPI_GPIOx_Pin_Init(SoftSPIx->SCLK_GPIO, SoftSPIx->SCLK_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
||||||
return HAL_ERROR;
|
return HAL_ERROR;
|
||||||
|
|
||||||
config.MOSI_GPIO = SoftSPI_Initure->MOSI_GPIO;
|
SoftSPIx->MOSI_GPIO = SoftSPIx->MOSI_GPIO;
|
||||||
config.MOSI_Pin = SoftSPI_Initure->MOSI_Pin;
|
SoftSPIx->MOSI_Pin = SoftSPIx->MOSI_Pin;
|
||||||
|
|
||||||
if (!GPIOx_Pin_Init(config.MOSI_GPIO, config.MOSI_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
if (!SoftSPI_GPIOx_Pin_Init(SoftSPIx->MOSI_GPIO, SoftSPIx->MOSI_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
||||||
return HAL_ERROR;
|
return HAL_ERROR;
|
||||||
|
|
||||||
config.MISO_GPIO = SoftSPI_Initure->MISO_GPIO;
|
SoftSPIx->MISO_GPIO = SoftSPIx->MISO_GPIO;
|
||||||
config.MISO_Pin = SoftSPI_Initure->MISO_Pin;
|
SoftSPIx->MISO_Pin = SoftSPIx->MISO_Pin;
|
||||||
|
|
||||||
if (!GPIOx_Pin_Init(config.MISO_GPIO, config.MISO_Pin, GPIO_MODE_INPUT, GPIO_PULLUP))
|
if (!SoftSPI_GPIOx_Pin_Init(SoftSPIx->MISO_GPIO, SoftSPIx->MISO_Pin, GPIO_MODE_INPUT, GPIO_PULLUP))
|
||||||
return HAL_ERROR;
|
return HAL_ERROR;
|
||||||
|
|
||||||
config.SS_GPIO = SoftSPI_Initure->SS_GPIO;
|
SoftSPIx->SS_GPIO = SoftSPIx->SS_GPIO;
|
||||||
config.SS_Pin = SoftSPI_Initure->SS_Pin;
|
SoftSPIx->SS_Pin = SoftSPIx->SS_Pin;
|
||||||
|
|
||||||
if (!GPIOx_Pin_Init(config.SS_GPIO, config.SS_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
if (!SoftSPI_GPIOx_Pin_Init(SoftSPIx->SS_GPIO, SoftSPIx->SS_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
||||||
return HAL_ERROR;
|
return HAL_ERROR;
|
||||||
|
|
||||||
config.Delay_Time = SoftSPI_Initure->Delay_Time;
|
SoftSPIx->Delay_Time = SoftSPIx->Delay_Time;
|
||||||
|
|
||||||
return HAL_OK;
|
return HAL_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t SoftSPI_WriteRead(uint8_t byte)
|
uint8_t SoftSPI_WriteRead(SoftSPI_TypeDef *SoftSPIx, uint8_t byte)
|
||||||
{
|
{
|
||||||
uint8_t data = 0;
|
uint8_t data = 0;
|
||||||
uint8_t i;
|
uint8_t i;
|
||||||
|
|
||||||
// Select Device
|
// Select Device
|
||||||
HAL_GPIO_WritePin(config.SS_GPIO, config.SS_Pin, GPIO_PIN_RESET);
|
SS_Clr;
|
||||||
|
|
||||||
for (i = 0; i < 8; i++)
|
for (i = 0; i < 8; i++)
|
||||||
{
|
{
|
||||||
HAL_GPIO_WritePin(config.SCLK_GPIO, config.SCLK_Pin, GPIO_PIN_RESET);
|
SCLK_Clr;
|
||||||
SoftSPI_Delay_us(config.Delay_Time);
|
Delay;
|
||||||
|
|
||||||
if (byte & 0x80)
|
if (byte & 0x80)
|
||||||
HAL_GPIO_WritePin(config.MOSI_GPIO, config.MOSI_Pin, GPIO_PIN_SET);
|
MOSI_Set;
|
||||||
else
|
else
|
||||||
HAL_GPIO_WritePin(config.MOSI_GPIO, config.MOSI_Pin, GPIO_PIN_RESET);
|
MOSI_Clr;
|
||||||
|
|
||||||
SoftSPI_Delay_us(config.Delay_Time);
|
Delay;
|
||||||
|
|
||||||
byte <<= 1;
|
byte <<= 1;
|
||||||
HAL_GPIO_WritePin(config.SCLK_GPIO, config.SCLK_Pin, GPIO_PIN_SET);
|
SCLK_Set;
|
||||||
SoftSPI_Delay_us(config.Delay_Time);
|
Delay;
|
||||||
|
|
||||||
data <<= 1;
|
data <<= 1;
|
||||||
if (HAL_GPIO_ReadPin(config.MISO_GPIO, config.MISO_Pin) == GPIO_PIN_SET)
|
if (MISO_Read == GPIO_PIN_SET)
|
||||||
data |= 0x01;
|
data |= 0x01;
|
||||||
|
|
||||||
SoftSPI_Delay_us(config.Delay_Time);
|
Delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unselect Device
|
// Unselect Device
|
||||||
HAL_GPIO_WritePin(config.SS_GPIO, config.SS_Pin, GPIO_PIN_SET);
|
SS_Set;
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoftSPI_WriteReadBuff(uint8_t *pWrite, uint8_t *pRead, uint32_t len)
|
void SoftSPI_WriteReadBuff(SoftSPI_TypeDef *SoftSPIx, uint8_t *pWrite, uint8_t *pRead, uint32_t len)
|
||||||
{
|
{
|
||||||
uint8_t data;
|
uint8_t data;
|
||||||
uint8_t byte;
|
uint8_t byte;
|
||||||
uint8_t i, j;
|
uint8_t i, j;
|
||||||
|
|
||||||
// Select Device
|
// Select Device
|
||||||
HAL_GPIO_WritePin(config.SS_GPIO, config.SS_Pin, GPIO_PIN_RESET);
|
SS_Clr;
|
||||||
|
|
||||||
for (j = 0; j < len; j++)
|
for (j = 0; j < len; j++)
|
||||||
{
|
{
|
||||||
@ -96,35 +107,35 @@ void SoftSPI_WriteReadBuff(uint8_t *pWrite, uint8_t *pRead, uint32_t len)
|
|||||||
|
|
||||||
for (i = 0; i < 8; i++)
|
for (i = 0; i < 8; i++)
|
||||||
{
|
{
|
||||||
HAL_GPIO_WritePin(config.SCLK_GPIO, config.SCLK_Pin, GPIO_PIN_RESET);
|
SCLK_Clr;
|
||||||
SoftSPI_Delay_us(config.Delay_Time);
|
Delay;
|
||||||
|
|
||||||
if (byte & 0x80)
|
if (byte & 0x80)
|
||||||
HAL_GPIO_WritePin(config.MOSI_GPIO, config.MOSI_Pin, GPIO_PIN_SET);
|
MOSI_Set;
|
||||||
else
|
else
|
||||||
HAL_GPIO_WritePin(config.MOSI_GPIO, config.MOSI_Pin, GPIO_PIN_RESET);
|
MOSI_Clr;
|
||||||
|
|
||||||
SoftSPI_Delay_us(config.Delay_Time);
|
Delay;
|
||||||
|
|
||||||
byte <<= 1;
|
byte <<= 1;
|
||||||
HAL_GPIO_WritePin(config.SCLK_GPIO, config.SCLK_Pin, GPIO_PIN_SET);
|
SCLK_Set;
|
||||||
SoftSPI_Delay_us(config.Delay_Time);
|
Delay;
|
||||||
|
|
||||||
data <<= 1;
|
data <<= 1;
|
||||||
if (HAL_GPIO_ReadPin(config.MISO_GPIO, config.MISO_Pin) == GPIO_PIN_SET)
|
if (MISO_Read == GPIO_PIN_SET)
|
||||||
data |= 0x01;
|
data |= 0x01;
|
||||||
|
|
||||||
SoftSPI_Delay_us(config.Delay_Time);
|
Delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
pRead[j] = data;
|
pRead[j] = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unselect Device
|
// Unselect Device
|
||||||
HAL_GPIO_WritePin(config.SS_GPIO, config.SS_Pin, GPIO_PIN_SET);
|
SS_Set;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t GPIOx_Pin_Init(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode, uint32_t Pull)
|
uint8_t SoftSPI_GPIOx_Pin_Init(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode, uint32_t Pull)
|
||||||
{
|
{
|
||||||
switch ((uint32_t)(GPIOx))
|
switch ((uint32_t)(GPIOx))
|
||||||
{
|
{
|
||||||
|
|||||||
12
softspi.h
12
softspi.h
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @file softspi.h
|
* @file softspi.h
|
||||||
* @author Myth
|
* @author Myth
|
||||||
* @version 0.1
|
* @version 0.2
|
||||||
* @date 2021.10.12
|
* @date 2021.10.12
|
||||||
* @brief STM32 SoftSPI Library
|
* @brief STM32 SoftSPI Library
|
||||||
*/
|
*/
|
||||||
@ -12,7 +12,7 @@
|
|||||||
#include "softspi_conf.h"
|
#include "softspi_conf.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief SoftSPI Configuration Structure definition
|
* @brief SoftSPI Structure definition
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@ -29,12 +29,12 @@ typedef struct
|
|||||||
uint32_t SS_Pin;
|
uint32_t SS_Pin;
|
||||||
|
|
||||||
uint32_t Delay_Time;
|
uint32_t Delay_Time;
|
||||||
} SoftSPI_InitTypeDef;
|
} SoftSPI_TypeDef;
|
||||||
|
|
||||||
HAL_StatusTypeDef SoftSPI_Init(SoftSPI_InitTypeDef *SoftSPI_Initure);
|
HAL_StatusTypeDef SoftSPI_Init(SoftSPI_TypeDef *SoftSPIx);
|
||||||
|
|
||||||
uint8_t SoftSPI_WriteRead(uint8_t byte);
|
uint8_t SoftSPI_WriteRead(SoftSPI_TypeDef *SoftSPIx, uint8_t byte);
|
||||||
|
|
||||||
void SoftSPI_WriteReadBuff(uint8_t *pWrite, uint8_t *pRead, uint32_t len);
|
void SoftSPI_WriteReadBuff(SoftSPI_TypeDef *SoftSPIx, uint8_t *pWrite, uint8_t *pRead, uint32_t len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @file softspi_conf.h
|
* @file softspi_conf.h
|
||||||
* @author Myth
|
* @author Myth
|
||||||
* @version 0.1
|
* @version 0.2
|
||||||
* @date 2021.10.12
|
* @date 2021.10.12
|
||||||
* @brief STM32 SoftSPI Library Config File
|
* @brief STM32 SoftSPI Library Config File
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user