2019-10-03 03:55 AM
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_spi.h"
void Gpio_Init(void)
{
RCC->AHB1ENR = 1<<0; //Enable GPIOA
RCC->APB2ENR = 1<<12; //Enable SPI1
GPIOA->MODER |= 1<<11; //PA5 AF Function
GPIOA->MODER |= 1<<13; //PA6 AF Function
GPIOA->MODER |= 1<<15; //PA7 AF Function
GPIOA->PUPDR |= 0;
GPIOA->OSPEEDR |= 1<<10; //PA5 High Speed
GPIOA->OSPEEDR |= 1<<12; //PA6 High Speed
GPIOA->OSPEEDR |= 1<<14; //PA7 High Speed
GPIOA->AFR[0] |= 5<<20; //PA5
GPIOA->AFR[0] |= 5<<24; //PA6
GPIOA->AFR[0] |= 5<<28; //PA7
GPIOA->MODER |= 1<<8;
GPIOA->PUPDR |= 1<<8;
GPIOA->OSPEEDR |= 1<<8;
GPIOA->OTYPER |= 0;
}
void Spi_Init(void)
{
SPI1->CR1 = 1<<0; //CPHA is 1
SPI1->CR1 |= 1<<1; //CPOL is 1
SPI1->CR1 |= 3<<8; // Software mode SSM and SSI bit is set
SPI1->CR1 |= 1<<2; // MSTR bit is set
SPI1->CR1 |= 1<<6; //SPE bit is set
}
int Spi_tx(unsigned char data)
{
GPIOA->BSRRH = GPIO_Pin_4;
SPI1->DR = data;
while(!(SPI1->SR & SPI_SR_TXE));
while(!(SPI1->SR & SPI_SR_RXNE));
GPIOA->BSRRL = GPIO_Pin_4;
return SPI1->DR;
}
int main()
{
Gpio_Init();
Spi_Init();
while(1)
{
Spi_tx(0xA);
for (int i = 0; i < 1000000; i++) {};
}
}