summaryrefslogtreecommitdiffstats
path: root/apps/spi-test/app.c
blob: 006067a48abc6e9ba4a5505c95a1532e960bb829 (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
#include <stdio.h>
#include <string.h>
#include <peripheral_clk_config.h>
#include "watch.h"
#include "spiflash.h"

// this is a very basic app to confirm that SPI is working, tested with board OSO-MISC-21-017 and a GD25Q16C Flash chip.

static bool wait_for_flash_ready(void) {
    watch_set_pin_level(A3, false);
    bool ok = true;
    uint8_t read_status_response[1] = {0x00};
    do {
        ok = spi_flash_read_command(CMD_READ_STATUS, read_status_response, 1);
    } while (ok && (read_status_response[0] & 0x3) != 0);
    watch_set_pin_level(A3, true);
    return ok;
}

void app_init(void) {
    spi_flash_init();
    delay_ms(5000);

    uint8_t buf[256] = {0};
    for(int i = 1; i < 16; i++) {
        wait_for_flash_ready();
        watch_set_pin_level(A3, false);
        spi_flash_command(CMD_ENABLE_WRITE);
        watch_set_pin_level(A3, true);
        wait_for_flash_ready();
        spi_flash_write_data(i * 256, buf, 256);
    }
}

void app_wake_from_backup(void) {
}

void app_setup(void) {
}

void app_prepare_for_standby(void) {
}

void app_wake_from_standby(void) {
}


bool app_loop(void) {
    uint8_t buf[4100] = {0};
    printf("loop\n");

    wait_for_flash_ready();
    spi_flash_read_data(0, buf, 4100);
    for(int i = 0; i < 4100; i++) {
        if (buf[i] > 0) {
            // should break at "byte 4096 is 255"
            printf("    byte %d is %d!\n", i, buf[i]);
            break;
        }
    }

    delay_ms(10000);

    return false;
}