76 lines
1.8 KiB
C
76 lines
1.8 KiB
C
|
|
#include <stdbool.h>
|
|
|
|
#include <libopencm3/stm32/rcc.h>
|
|
#include <libopencm3/stm32/spi.h>
|
|
#include <libopencm3/stm32/gpio.h>
|
|
|
|
#include "spi_flash.h"
|
|
|
|
static struct spi_flash_if spif;
|
|
|
|
static void clock_setup(void)
|
|
{
|
|
rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
|
|
rcc_periph_clock_enable(RCC_GPIOA);
|
|
rcc_periph_clock_enable(RCC_GPIOB);
|
|
rcc_periph_clock_enable(RCC_SPI1);
|
|
}
|
|
|
|
static void led_setup(void)
|
|
{
|
|
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO6 | GPIO7);
|
|
}
|
|
|
|
static void spi_flash_if_set_cs(bool val) {
|
|
if (val)
|
|
gpio_set(GPIOB, GPIO0);
|
|
else
|
|
gpio_clear(GPIOB, GPIO0);
|
|
}
|
|
|
|
static void spi_flash_setup(void)
|
|
{
|
|
gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3 | GPIO4 | GPIO5); /* SPI flash SCK/MISO/MOSI */
|
|
gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO0); /* SPI flash CS */
|
|
gpio_set_output_options(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO0 | GPIO3 | GPIO4 | GPIO5);
|
|
gpio_set_af(GPIOB, 5, GPIO3 | GPIO4 | GPIO5);
|
|
|
|
spif_init(&spif, 256, SPI1, &spi_flash_if_set_cs);
|
|
}
|
|
|
|
/*
|
|
void spi_flash_test(void) {
|
|
spif_clear_mem(&spif);
|
|
|
|
uint32_t buf[1024];
|
|
for (size_t addr=0; addr<0x10000; addr += sizeof(buf)) {
|
|
for (size_t i=0; i<sizeof(buf); i+= sizeof(buf[0]))
|
|
buf[i/sizeof(buf[0])] = addr + i;
|
|
|
|
spif_write(&spif, addr, sizeof(buf), (char *)buf);
|
|
}
|
|
|
|
for (size_t i=0; i<sizeof(buf)/sizeof(buf[0]); i++)
|
|
buf[i] = 0;
|
|
spif_read(&spif, 0x1030, sizeof(buf), (char *)buf);
|
|
asm volatile ("bkpt");
|
|
}
|
|
*/
|
|
|
|
int main(void)
|
|
{
|
|
clock_setup();
|
|
led_setup();
|
|
spi_flash_setup();
|
|
|
|
gpio_set(GPIOA, GPIO6);
|
|
|
|
while (1) {
|
|
gpio_toggle(GPIOA, GPIO6 | GPIO7);
|
|
for (int i = 0; i < 6000000; i++)
|
|
__asm__("nop");
|
|
}
|
|
|
|
return 0;
|
|
}
|