summaryrefslogtreecommitdiff
path: root/controller/fw/main.c
blob: 3131df160f8a1b6f7e207c762205cf6d7698e6a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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;
}