105 lines
2.5 KiB
C
105 lines
2.5 KiB
C
/*
|
|
* (c) danielinux 2019
|
|
*
|
|
* GPLv.2
|
|
*
|
|
* See LICENSE for details
|
|
*/
|
|
#include <stdint.h>
|
|
#include "spi_drv.h"
|
|
#include "system.h"
|
|
|
|
void spi_cs_off(void)
|
|
{
|
|
GPIOA_BSRR |= (1 << SPI_FLASH_PIN);
|
|
DMB();
|
|
while(!(GPIOA_ODR & (1 << SPI_FLASH_PIN)))
|
|
;
|
|
}
|
|
|
|
void spi_cs_on(void)
|
|
{
|
|
volatile int i;
|
|
GPIOA_BSRR |= (1 << (SPI_FLASH_PIN + 16));
|
|
DMB();
|
|
while(GPIOA_ODR & (1 << SPI_FLASH_PIN))
|
|
;
|
|
}
|
|
|
|
|
|
static void spi_flash_pin_setup(void)
|
|
{
|
|
uint32_t reg;
|
|
AHB1_CLOCK_ER |= GPIOA_AHB1_CLOCK_ER;
|
|
reg = GPIOA_MODE & ~ (0x03 << (SPI_FLASH_PIN * 2));
|
|
GPIOA_MODE = reg | (1 << (SPI_FLASH_PIN * 2));
|
|
|
|
reg = GPIOA_PUPD & (0x03 << (SPI_FLASH_PIN * 2));
|
|
GPIOA_PUPD = reg | (0x01 << (SPI_FLASH_PIN * 2));
|
|
|
|
reg = GPIOA_OSPD & ~(0x03 << (SPI_FLASH_PIN * 2));
|
|
GPIOA_OSPD |= (0x03 << (SPI_FLASH_PIN * 2));
|
|
|
|
}
|
|
|
|
static void spi1_pins_setup(void)
|
|
{
|
|
uint32_t reg;
|
|
AHB1_CLOCK_ER |= GPIOA_AHB1_CLOCK_ER;
|
|
/* Set mode = AF */
|
|
reg = GPIOA_MODE & ~ (0x03 << (SPI1_CLOCK_PIN * 2));
|
|
GPIOA_MODE = reg | (2 << (SPI1_CLOCK_PIN * 2));
|
|
reg = GPIOA_MODE & ~ (0x03 << (SPI1_MOSI_PIN * 2));
|
|
GPIOA_MODE = reg | (2 << (SPI1_MOSI_PIN * 2));
|
|
reg = GPIOA_MODE & ~ (0x03 << (SPI1_MISO_PIN * 2));
|
|
GPIOA_MODE = reg | (2 << (SPI1_MISO_PIN * 2));
|
|
|
|
/* Alternate function: use low pins (5,6,7) */
|
|
reg = GPIOA_AFL & ~(0xf << ((SPI1_CLOCK_PIN) * 4));
|
|
GPIOA_AFL = reg | (SPI1_PIN_AF << ((SPI1_CLOCK_PIN) * 4));
|
|
reg = GPIOA_AFL & ~(0xf << ((SPI1_MOSI_PIN) * 4));
|
|
GPIOA_AFL = reg | (SPI1_PIN_AF << ((SPI1_MOSI_PIN) * 4));
|
|
reg = GPIOA_AFL & ~(0xf << ((SPI1_MISO_PIN) * 4));
|
|
GPIOA_AFL = reg | (SPI1_PIN_AF << ((SPI1_MISO_PIN) * 4));
|
|
|
|
}
|
|
|
|
static void spi1_reset(void)
|
|
{
|
|
APB2_CLOCK_RST |= SPI1_APB2_CLOCK_ER_VAL;
|
|
APB2_CLOCK_RST &= ~SPI1_APB2_CLOCK_ER_VAL;
|
|
}
|
|
|
|
uint8_t spi_read(void)
|
|
{
|
|
volatile uint32_t reg;
|
|
do {
|
|
reg = SPI1_SR;
|
|
} while(!(reg & SPI_SR_RX_NOTEMPTY));
|
|
return (uint8_t)SPI1_DR;
|
|
}
|
|
|
|
void spi_write(const char byte)
|
|
{
|
|
int i;
|
|
volatile uint32_t reg;
|
|
do {
|
|
reg = SPI1_SR;
|
|
} while ((reg & SPI_SR_TX_EMPTY) == 0);
|
|
SPI1_DR = byte;
|
|
do {
|
|
reg = SPI1_SR;
|
|
} while ((reg & SPI_SR_TX_EMPTY) == 0);
|
|
}
|
|
|
|
|
|
void spi_init(int polarity, int phase)
|
|
{
|
|
spi1_pins_setup();
|
|
spi_flash_pin_setup();
|
|
APB2_CLOCK_ER |= SPI1_APB2_CLOCK_ER_VAL;
|
|
spi1_reset();
|
|
SPI1_CR1 = SPI_CR1_MASTER | (5 << 3) | (polarity << 1) | (phase << 0);
|
|
SPI1_CR2 |= SPI_CR2_SSOE;
|
|
SPI1_CR1 |= SPI_CR1_SPI_EN;
|
|
}
|