Driver_SPI.c
#include "Driver_SPI.h"
void Driver_SPI_Init(void)
{
RCC->APB2ENR |= (RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPAEN);
GPIOC->CRH &= ~GPIO_CRH_CNF13;
GPIOC->CRH |= GPIO_CRH_MODE13;
GPIOA->CRL &= ~(GPIO_CRL_CNF5 | GPIO_CRL_CNF7);
GPIOA->CRL |= (GPIO_CRL_MODE5 | GPIO_CRL_MODE7);
GPIOA->CRL &= ~(GPIO_CRL_CNF6_1 | GPIO_CRL_MODE6);
GPIOA->CRL |= GPIO_CRL_CNF6_0;
SCK_LOW;
CS_HIGH;
SPI_DELAY;
}
void Driver_SPI_Start(void)
{
CS_LOW;
}
void Driver_SPI_Stop(void)
{
CS_HIGH;
}
uint8_t Driver_SPI_SwapByte(uint8_t byte)
{
uint8_t rByte = 0x00;
for (uint8_t i = 0; i < 8; i++)
{
if (byte & 0x80)
{
MOSI_HIGH;
}
else
{
MOSI_LOW;
}
byte <<= 1;
SCK_HIGH;
rByte <<= 1;
if (MISO_READ)
{
rByte |= 0x01;
}
SCK_LOW;
}
return rByte;
}
Driver_SPI.h
#ifndef __DRIVER_SPI_H
#define __DRIVER_SPI_H
#include "stm32f10x.h"
#include "Delay.h"
#define CS_HIGH (GPIOC->ODR |= GPIO_ODR_ODR13)
#define CS_LOW (GPIOC->ODR &= ~GPIO_ODR_ODR13)
#define SCK_HIGH (GPIOA->ODR |= GPIO_ODR_ODR5)
#define SCK_LOW (GPIOA->ODR &= ~GPIO_ODR_ODR5)
#define MOSI_HIGH (GPIOA->ODR |= GPIO_ODR_ODR7)
#define MOSI_LOW (GPIOA->ODR &= ~GPIO_ODR_ODR7)
#define MISO_READ (GPIOA->IDR & GPIO_IDR_IDR6)
#define SPI_DELAY Delay_us(5)
void Driver_SPI_Init(void);
void Driver_SPI_Start(void);
void Driver_SPI_Stop(void);
uint8_t Driver_SPI_SwapByte(uint8_t byte);
#endif