aboutsummaryrefslogtreecommitdiffstats
path: root/examples/test-spi-roundtrip.cpp
blob: 647e6f18503d54057805c86081a43cf4121e18e6 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Polling SPI loopback test.
 *
 * Bob is nowhere to be found, so Alice decides to talk to herself.
 *
 * Instructions: Connect SPI2 (Alice) to herself (i.e., MISO to MOSI).
 * Connect to Alice via SerialUSB.  Press any key to start.
 *
 * Alice will talk to herself for a little while.  The sketch will
 * report if Alice can't hear anything she says.  She'll then start
 * talking forever at various frequencies, bit orders, and modes.  Use
 * an oscilloscope to make sure she's not trying to lie about any of
 * those things.
 *
 * This file is released into the public domain.
 *
 * Author: Marti Bolivar <mbolivar@leaflabs.com>
 */

#include "wirish.h"

HardwareSPI alice(2);

#define NFREQS 8
const SPIFrequency spi_freqs[] = {
    SPI_140_625KHZ,
    SPI_281_250KHZ,
    SPI_562_500KHZ,
    SPI_1_125MHZ,
    SPI_2_25MHZ,
    SPI_4_5MHZ,
    SPI_9MHZ,
    SPI_18MHZ,
};

#define TEST_BUF_SIZE 10
uint8 test_buf[TEST_BUF_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

void bad_assert(const char* file, int line, const char* exp) {
    SerialUSB.println();
    SerialUSB.print("ERROR: FAILED ASSERT(");
    SerialUSB.print(exp);
    SerialUSB.print("): ");
    SerialUSB.print(file);
    SerialUSB.print(": ");
    SerialUSB.println(line);
    throb();
}

#undef ASSERT
#define ASSERT(exp)                                    \
    if (exp) {                                         \
    } else {                                           \
        bad_assert(__FILE__, __LINE__, #exp);          \
    }

void haveConversation(uint32 bitOrder);
void soliloquies(uint32 bitOrder);

void setup() {
    pinMode(BOARD_LED_PIN, OUTPUT);
    while (!SerialUSB.available())
        ;
    SerialUSB.read();
}

void loop() {
    SerialUSB.println("** Having a conversation, MSB first");
    haveConversation(MSBFIRST);

    SerialUSB.println("** Having a conversation, LSB first");
    haveConversation(LSBFIRST);

    SerialUSB.println();
    SerialUSB.println("*** All done!  It looks like everything worked.");
    SerialUSB.println();

    SerialUSB.println("** Alice will now wax eloquent in various styles. "
                      "Press any key for the next configuration.");
    soliloquies(MSBFIRST);
    soliloquies(LSBFIRST);

    while (true)
        ;
}

void printFrequencyString(SPIFrequency frequency);
void chat(SPIFrequency frequency, uint32 bitOrder, uint32 mode);

void haveConversation(uint32 bitOrder) {
    for (int f = 0; f < NFREQS; f++) {
        for (int mode = 0; mode < 4; mode++) {
            chat(spi_freqs[f], bitOrder, mode);
            delay(10);
        }
    }
}

void chat(SPIFrequency frequency, uint32 bitOrder, uint32 mode) {
    SerialUSB.print("Having a chat.\tFrequency: ");
    printFrequencyString(frequency);
    SerialUSB.print(",\tbitOrder: ");
    SerialUSB.print(bitOrder == MSBFIRST ? "MSB" : "LSB");
    SerialUSB.print(",\tmode: ");
    SerialUSB.print(mode);
    SerialUSB.print(".");

    SerialUSB.print(" [1] ");
    alice.begin(frequency, bitOrder, mode);

    SerialUSB.print(" [2] ");
    uint32 txed = 0;
    while (txed < TEST_BUF_SIZE) {
        ASSERT(alice.transfer(test_buf[txed]) == test_buf[txed]);
        txed++;
    }

    SerialUSB.print(" [3] ");
    alice.end();

    SerialUSB.println(" ok.");
}

void soliloquy(SPIFrequency freq, uint32 bitOrder, uint32 mode);

void soliloquies(uint32 bitOrder) {
    for (int f = 0; f < NFREQS; f++) {
        for (int mode = 0; mode < 4; mode++) {
            soliloquy(spi_freqs[f], bitOrder, mode);
        }
    }
}

void soliloquy(SPIFrequency frequency, uint32 bitOrder, uint32 mode) {
    const uint8 repeat = 0xAE;
    SerialUSB.print("Alice is giving a soliloquy (repeating 0x");
    SerialUSB.print(repeat, HEX);
    SerialUSB.print("). Frequency: ");
    printFrequencyString(frequency);
    SerialUSB.print(", bitOrder: ");
    SerialUSB.print(bitOrder == MSBFIRST ? "big-endian" : "little-endian");
    SerialUSB.print(", SPI mode: ");
    SerialUSB.println(mode);

    alice.begin(frequency, bitOrder, mode);
    while (!SerialUSB.available()) {
        alice.write(repeat);
        delayMicroseconds(200);
    }
    SerialUSB.read();
}

void printFrequencyString(SPIFrequency frequency) {
    switch (frequency) {
    case SPI_18MHZ:
        SerialUSB.print("18 MHz");
        break;
    case SPI_9MHZ:
        SerialUSB.print("9 MHz");
        break;
    case SPI_4_5MHZ:
        SerialUSB.print("4.5 MHz");
        break;
    case SPI_2_25MHZ:
        SerialUSB.print("2.25 MHZ");
        break;
    case SPI_1_125MHZ:
        SerialUSB.print("1.125 MHz");
        break;
    case SPI_562_500KHZ:
        SerialUSB.print("562.500 KHz");
        break;
    case SPI_281_250KHZ:
        SerialUSB.print("281.250 KHz");
        break;
    case SPI_140_625KHZ:
        SerialUSB.print("140.625 KHz");
        break;
    }
}

// Force init to be called *first*, i.e. before static object allocation.
// Otherwise, statically allocated objects that need libmaple may fail.
__attribute__((constructor)) void premain() {
    init();
}

int main(void) {
    setup();
    while (true) {
        loop();
    }
    return 0;
}