Add file
This commit is contained in:
parent
645e976d42
commit
e6d62c90e6
188
softspi.c
Normal file
188
softspi.c
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
/**
|
||||||
|
* @file softspi.c
|
||||||
|
* @author Myth
|
||||||
|
* @version 0.1
|
||||||
|
* @date 2021.10.12
|
||||||
|
* @brief STM32 SoftSPI Library
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "softspi.h"
|
||||||
|
|
||||||
|
SoftSPI_InitTypeDef config;
|
||||||
|
|
||||||
|
uint8_t GPIOx_Pin_Init(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode, uint32_t Pull);
|
||||||
|
|
||||||
|
HAL_StatusTypeDef SoftSPI_Init(SoftSPI_InitTypeDef *SoftSPI_Initure)
|
||||||
|
{
|
||||||
|
config.SCLK_GPIO = SoftSPI_Initure->SCLK_GPIO;
|
||||||
|
config.SCLK_Pin = SoftSPI_Initure->SCLK_Pin;
|
||||||
|
|
||||||
|
if (!GPIOx_Pin_Init(config.SCLK_GPIO, config.SCLK_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
||||||
|
return HAL_ERROR;
|
||||||
|
|
||||||
|
config.MOSI_GPIO = SoftSPI_Initure->MOSI_GPIO;
|
||||||
|
config.MOSI_Pin = SoftSPI_Initure->MOSI_Pin;
|
||||||
|
|
||||||
|
if (!GPIOx_Pin_Init(config.MOSI_GPIO, config.MOSI_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
||||||
|
return HAL_ERROR;
|
||||||
|
|
||||||
|
config.MISO_GPIO = SoftSPI_Initure->MISO_GPIO;
|
||||||
|
config.MISO_Pin = SoftSPI_Initure->MISO_Pin;
|
||||||
|
|
||||||
|
if (!GPIOx_Pin_Init(config.MISO_GPIO, config.MISO_Pin, GPIO_MODE_INPUT, GPIO_PULLUP))
|
||||||
|
return HAL_ERROR;
|
||||||
|
|
||||||
|
config.SS_GPIO = SoftSPI_Initure->SS_GPIO;
|
||||||
|
config.SS_Pin = SoftSPI_Initure->SS_Pin;
|
||||||
|
|
||||||
|
if (!GPIOx_Pin_Init(config.SS_GPIO, config.SS_Pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL))
|
||||||
|
return HAL_ERROR;
|
||||||
|
|
||||||
|
config.Delay_Time = SoftSPI_Initure->Delay_Time;
|
||||||
|
|
||||||
|
return HAL_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t SoftSPI_WriteRead(uint8_t byte)
|
||||||
|
{
|
||||||
|
uint8_t data = 0;
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
// Select Device
|
||||||
|
HAL_GPIO_WritePin(config.SS_GPIO, config.SS_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
|
for (i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
HAL_GPIO_WritePin(config.SCLK_GPIO, config.SCLK_Pin, GPIO_PIN_RESET);
|
||||||
|
SoftSPI_Delay_us(config.Delay_Time);
|
||||||
|
|
||||||
|
if (byte & 0x80)
|
||||||
|
HAL_GPIO_WritePin(config.MOSI_GPIO, config.MOSI_Pin, GPIO_PIN_SET);
|
||||||
|
else
|
||||||
|
HAL_GPIO_WritePin(config.MOSI_GPIO, config.MOSI_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
|
SoftSPI_Delay_us(config.Delay_Time);
|
||||||
|
|
||||||
|
byte <<= 1;
|
||||||
|
HAL_GPIO_WritePin(config.SCLK_GPIO, config.SCLK_Pin, GPIO_PIN_SET);
|
||||||
|
SoftSPI_Delay_us(config.Delay_Time);
|
||||||
|
|
||||||
|
data <<= 1;
|
||||||
|
if (HAL_GPIO_ReadPin(config.MISO_GPIO, config.MISO_Pin) == GPIO_PIN_SET)
|
||||||
|
data |= 0x01;
|
||||||
|
|
||||||
|
SoftSPI_Delay_us(config.Delay_Time);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unselect Device
|
||||||
|
HAL_GPIO_WritePin(config.SS_GPIO, config.SS_Pin, GPIO_PIN_SET);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SoftSPI_WriteReadBuff(uint8_t *pWrite, uint8_t *pRead, uint32_t len)
|
||||||
|
{
|
||||||
|
uint8_t data;
|
||||||
|
uint8_t byte;
|
||||||
|
uint8_t i, j;
|
||||||
|
|
||||||
|
// Select Device
|
||||||
|
HAL_GPIO_WritePin(config.SS_GPIO, config.SS_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
|
for (j = 0; j < len; j++)
|
||||||
|
{
|
||||||
|
data = 0;
|
||||||
|
byte = pWrite[j];
|
||||||
|
|
||||||
|
for (i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
HAL_GPIO_WritePin(config.SCLK_GPIO, config.SCLK_Pin, GPIO_PIN_RESET);
|
||||||
|
SoftSPI_Delay_us(config.Delay_Time);
|
||||||
|
|
||||||
|
if (byte & 0x80)
|
||||||
|
HAL_GPIO_WritePin(config.MOSI_GPIO, config.MOSI_Pin, GPIO_PIN_SET);
|
||||||
|
else
|
||||||
|
HAL_GPIO_WritePin(config.MOSI_GPIO, config.MOSI_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
|
SoftSPI_Delay_us(config.Delay_Time);
|
||||||
|
|
||||||
|
byte <<= 1;
|
||||||
|
HAL_GPIO_WritePin(config.SCLK_GPIO, config.SCLK_Pin, GPIO_PIN_SET);
|
||||||
|
SoftSPI_Delay_us(config.Delay_Time);
|
||||||
|
|
||||||
|
data <<= 1;
|
||||||
|
if (HAL_GPIO_ReadPin(config.MISO_GPIO, config.MISO_Pin) == GPIO_PIN_SET)
|
||||||
|
data |= 0x01;
|
||||||
|
|
||||||
|
SoftSPI_Delay_us(config.Delay_Time);
|
||||||
|
}
|
||||||
|
|
||||||
|
pRead[j] = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unselect Device
|
||||||
|
HAL_GPIO_WritePin(config.SS_GPIO, config.SS_Pin, GPIO_PIN_SET);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t GPIOx_Pin_Init(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode, uint32_t Pull)
|
||||||
|
{
|
||||||
|
switch ((uint32_t)(GPIOx))
|
||||||
|
{
|
||||||
|
case (uint32_t)GPIOA:
|
||||||
|
{
|
||||||
|
GPIO_InitTypeDef GPIO_Initure;
|
||||||
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||||
|
|
||||||
|
GPIO_Initure.Pin = Pin;
|
||||||
|
GPIO_Initure.Mode = Mode;
|
||||||
|
GPIO_Initure.Pull = Pull;
|
||||||
|
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||||
|
HAL_GPIO_Init(GPIOA, &GPIO_Initure);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case (uint32_t)GPIOB:
|
||||||
|
{
|
||||||
|
GPIO_InitTypeDef GPIO_Initure;
|
||||||
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||||
|
|
||||||
|
GPIO_Initure.Pin = Pin;
|
||||||
|
GPIO_Initure.Mode = Mode;
|
||||||
|
GPIO_Initure.Pull = Pull;
|
||||||
|
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||||
|
HAL_GPIO_Init(GPIOB, &GPIO_Initure);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case (uint32_t)GPIOC:
|
||||||
|
{
|
||||||
|
GPIO_InitTypeDef GPIO_Initure;
|
||||||
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||||
|
|
||||||
|
GPIO_Initure.Pin = Pin;
|
||||||
|
GPIO_Initure.Mode = Mode;
|
||||||
|
GPIO_Initure.Pull = Pull;
|
||||||
|
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||||
|
HAL_GPIO_Init(GPIOC, &GPIO_Initure);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case (uint32_t)GPIOD:
|
||||||
|
{
|
||||||
|
GPIO_InitTypeDef GPIO_Initure;
|
||||||
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||||
|
|
||||||
|
GPIO_Initure.Pin = Pin;
|
||||||
|
GPIO_Initure.Mode = Mode;
|
||||||
|
GPIO_Initure.Pull = Pull;
|
||||||
|
GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||||
|
HAL_GPIO_Init(GPIOD, &GPIO_Initure);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
40
softspi.h
Normal file
40
softspi.h
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* @file softspi.h
|
||||||
|
* @author Myth
|
||||||
|
* @version 0.1
|
||||||
|
* @date 2021.10.12
|
||||||
|
* @brief STM32 SoftSPI Library
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __SOFTSPI_H
|
||||||
|
#define __SOFTSPI_H
|
||||||
|
|
||||||
|
#include "softspi_conf.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief SoftSPI Configuration Structure definition
|
||||||
|
*/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
GPIO_TypeDef *SCLK_GPIO;
|
||||||
|
uint32_t SCLK_Pin;
|
||||||
|
|
||||||
|
GPIO_TypeDef *MOSI_GPIO;
|
||||||
|
uint32_t MOSI_Pin;
|
||||||
|
|
||||||
|
GPIO_TypeDef *MISO_GPIO;
|
||||||
|
uint32_t MISO_Pin;
|
||||||
|
|
||||||
|
GPIO_TypeDef *SS_GPIO;
|
||||||
|
uint32_t SS_Pin;
|
||||||
|
|
||||||
|
uint32_t Delay_Time;
|
||||||
|
} SoftSPI_InitTypeDef;
|
||||||
|
|
||||||
|
HAL_StatusTypeDef SoftSPI_Init(SoftSPI_InitTypeDef *SoftSPI_Initure);
|
||||||
|
|
||||||
|
uint8_t SoftSPI_WriteRead(uint8_t byte);
|
||||||
|
|
||||||
|
void SoftSPI_WriteReadBuff(uint8_t *pWrite, uint8_t *pRead, uint32_t len);
|
||||||
|
|
||||||
|
#endif
|
||||||
22
softspi_conf.h
Normal file
22
softspi_conf.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* @file softspi_conf.h
|
||||||
|
* @author Myth
|
||||||
|
* @version 0.1
|
||||||
|
* @date 2021.10.12
|
||||||
|
* @brief STM32 SoftSPI Library Config File
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __SOFTSPI_CONF_H
|
||||||
|
#define __SOFTSPI_CONF_H
|
||||||
|
|
||||||
|
// Set your HAL Library here.
|
||||||
|
|
||||||
|
#include "stm32f1xx_hal.h"
|
||||||
|
|
||||||
|
// Set your owm Delay_us function here.
|
||||||
|
|
||||||
|
#include "systick.h"
|
||||||
|
|
||||||
|
#define SoftSPI_Delay_us(__time__) Delay_us(__time__)
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user