aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/MSP430X/EXP430FR5969/SPI/main.c
blob: 17f5c86de14be5c021ef613d89013a7dd0fd0a2c (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
    ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "ch.h"
#include "hal.h"
#include "hal_dma_lld.h"
#include "string.h"

/* Disable watchdog because of lousy startup code in newlib */
static void __attribute__((naked, section(".crt_0042disable_watchdog"), used))
disable_watchdog(void) {
  WDTCTL = WDTPW | WDTHOLD;
}

const char * start_msg  = "\r\n\r\nExecuting SPI test suite...\r\n";
const char * test_1_msg = "TEST 1: spiStartIgnore, with callback\r\n";
const char * test_2_msg = "TEST 2: spiStartExchange, with callback\r\n";
const char * test_3_msg = "TEST 3: spiStartSend, with callback\r\n";
const char * test_4_msg = "TEST 4: spiStartReceive, with callback\r\n";
const char * test_5_msg = "TEST 5: spiIgnore\r\n";
const char * test_6_msg = "TEST 6: spiExchange\r\n";
const char * test_7_msg = "TEST 7: spiSend\r\n";
const char * test_8_msg = "TEST 8: spiReceive\r\n";
const char * test_9_msg = "TEST 9: spiStartExchange with exclusive DMA\r\n";
const char * test_10_msg =
    "TEST 10: spiStartExchange with exclusive DMA for TX\r\n";
const char * test_11_msg =
    "TEST 11: spiStartExchange with exclusive DMA for RX\r\n";

const char * succeed_string = "SUCCESS\r\n\r\n";
const char * fail_string    = "FAILURE\r\n\r\n";

char instring[256];
char outstring[256];
uint8_t cb_arg = 1;

void spi_callback(SPIDriver * spip) {
  (void)spip;
  cb_arg = 0;
}

SPIConfig SPIDA1_config = {
  spi_callback,         /* callback */
  PAL_NOLINE,           /* hardware slave select line */
  250000,               /* data rate */
  MSP430X_SPI_BO_LSB,   /* bit order */
  MSP430X_SPI_DS_EIGHT, /* data size */
  0,                    /* SPI mode */
  0xFFU,                /* no exclusive TX DMA */
  0xFFU                 /* no exclusive RX DMA */
};

SPIConfig SPIDB0_config = {
  NULL,                 /* callback */
  LINE_LED_G,           /* GPIO slave select line */
  1000,                 /* data rate */
  MSP430X_SPI_BO_MSB,   /* bit order */
  MSP430X_SPI_DS_SEVEN, /* data size */
  3,                    /* SPI mode */
  0xFF,                 /* no exclusive TX DMA */
  0xFF                  /* no exclusive RX DMA */
};

/*
 * Thread 2.
 */
THD_WORKING_AREA(waThread1, 4096);
THD_FUNCTION(Thread1, arg) {

  (void)arg;

  /* Set up loopback mode for testing */
  SPIDA1.regs->statw_a |= UCLISTEN;
  SPIDB0.regs->statw_b |= UCLISTEN;

  /*
   * Activate the serial driver 0 using the driver default configuration.
   */
  sdStart(&SD0, NULL);

  /* Activate the SPI driver A1 using its config */
  spiStart(&SPIDA1, &SPIDA1_config);
  /* Activate the SPI driver B0 using its config */
  spiStart(&SPIDB0, &SPIDB0_config);

  while (chnGetTimeout(&SD0, TIME_INFINITE)) {
    chnWrite(&SD0, (const uint8_t *)start_msg, strlen(start_msg));
    chThdSleepMilliseconds(2000);

    /* Test 1 - spiStartIgnore with callback */
    chnWrite(&SD0, (const uint8_t *)test_1_msg, strlen(test_1_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    cb_arg = 1;
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 1) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDA1);
    spiStartIgnore(&SPIDA1, strlen(outstring));
    while (SPIDA1.state != SPI_READY)
      ; /* wait for transaction to finish */
    spiUnselect(&SPIDA1);
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 0) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Test 2 - spiStartExchange with callback */
    chnWrite(&SD0, (const uint8_t *)test_2_msg, strlen(test_2_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    cb_arg = 1;
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 1) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDA1);
    spiStartExchange(&SPIDA1, strlen(instring), outstring, instring);
    while (SPIDA1.state != SPI_READY)
      ; /* wait for transaction to finish */
    spiUnselect(&SPIDA1);
    if (strcmp("After SPI test  \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 0) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Test 3 - spiStartSend with callback */
    chnWrite(&SD0, (const uint8_t *)test_3_msg, strlen(test_3_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    cb_arg = 1;
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 1) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDA1);
    spiStartSend(&SPIDA1, strlen(outstring), outstring);
    while (SPIDA1.state != SPI_READY)
      ; /* wait for transaction to finish */
    spiUnselect(&SPIDA1);
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 0) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Test 4 - spiStartReceive with callback */
    chnWrite(&SD0, (const uint8_t *)test_4_msg, strlen(test_4_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    cb_arg = 1;
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 1) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDA1);
    chThdSleepMilliseconds(2000);
    spiStartReceive(&SPIDA1, strlen(instring), instring);
    while (SPIDA1.state != SPI_READY)
      ; /* wait for transaction to finish */
    spiUnselect(&SPIDA1);
    if (strcmp("After SPI test  \r\n", outstring) ||
        strcmp("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
               "\xff\xff\xff",
               instring) ||
        cb_arg != 0) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Test 5 - spiIgnore */
    chnWrite(&SD0, (const uint8_t *)test_5_msg, strlen(test_5_msg));
    strcpy(instring, "After SPI test  \r\n");
    strcpy(outstring, "Before SPI test \r\n");
    if (strcmp("Before SPI test \r\n", outstring) ||
        strcmp("After SPI test  \r\n", instring)) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDB0);
    chThdSleepMilliseconds(2000);
    spiIgnore(&SPIDB0, strlen(outstring));
    spiUnselect(&SPIDB0);
    if (strcmp("After SPI test  \r\n", instring) ||
        strcmp("Before SPI test \r\n", outstring)) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Test 6 - spiExchange */
    chnWrite(&SD0, (const uint8_t *)test_6_msg, strlen(test_6_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring)) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDB0);
    spiExchange(&SPIDB0, strlen(outstring), outstring, instring);
    spiUnselect(&SPIDB0);
    if (strcmp("After SPI test  \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring)) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Test 7 - spiSend */
    chnWrite(&SD0, (const uint8_t *)test_7_msg, strlen(test_7_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring)) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDB0);
    spiSend(&SPIDB0, strlen(outstring), outstring);
    spiUnselect(&SPIDB0);
    if (strcmp("After SPI test  \r\n", outstring) ||
        strcmp("Before SPI test \r\n", instring)) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Test 8 - spiReceive */
    chnWrite(&SD0, (const uint8_t *)test_8_msg, strlen(test_8_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring)) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDB0);
    spiReceive(&SPIDB0, strlen(instring), instring);
    spiUnselect(&SPIDB0);
    if (strcmp("After SPI test  \r\n", outstring) ||
        strcmp("\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f"
               "\x7f\x7f\x7f",
               instring)) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Reconfigure SPIDA1 to use exclusive DMA for both */
    spiStop(&SPIDA1);
    SPIDA1_config.dmatx_index = 0;
    SPIDA1_config.dmarx_index = 1;
    SPIDA1_config.spi_mode    = 1; /* because why not get coverage */
    spiStart(&SPIDA1, &SPIDA1_config);

    /* Test 9 - spiStartExchange with exclusive DMA */
    chnWrite(&SD0, (const uint8_t *)test_9_msg, strlen(test_9_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    cb_arg = 1;
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 1) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDA1);
    spiStartExchange(&SPIDA1, strlen(outstring), outstring, instring);
    while (SPIDA1.state != SPI_READY)
      ; /* wait for transaction to finish */
    spiUnselect(&SPIDA1);
    if (strcmp("After SPI test  \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 0) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Reconfigure SPIDA1 to use exclusive DMA for TX only */
    spiStop(&SPIDA1);
    SPIDA1_config.dmatx_index = 0;
    SPIDA1_config.dmarx_index = 0xFFU;
    SPIDA1_config.spi_mode    = 2; /* because why not get coverage */
    spiStart(&SPIDA1, &SPIDA1_config);

    /* Test 10 - spiStartExchange with exclusive DMA for TX */
    chnWrite(&SD0, (const uint8_t *)test_10_msg, strlen(test_10_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    cb_arg = 1;
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 1) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDA1);
    spiStartExchange(&SPIDA1, strlen(outstring), outstring, instring);
    while (SPIDA1.state != SPI_READY)
      ; /* wait for transaction to finish */
    spiUnselect(&SPIDA1);
    if (strcmp("After SPI test  \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 0) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }

    /* Reconfigure SPIDA1 to use exclusive DMA for TX only */
    spiStop(&SPIDA1);
    SPIDA1_config.dmatx_index = 0xFFU;
    SPIDA1_config.dmarx_index = 1;
    SPIDA1_config.spi_mode    = 3; /* because why not get coverage */
    spiStart(&SPIDA1, &SPIDA1_config);

    /* Test 11 - spiStartExchange with exclusive DMA for RX */
    chnWrite(&SD0, (const uint8_t *)test_11_msg, strlen(test_11_msg));
    strcpy(outstring, "After SPI test  \r\n");
    strcpy(instring, "Before SPI test \r\n");
    cb_arg = 1;
    if (strcmp("Before SPI test \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 1) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    spiSelect(&SPIDA1);
    spiStartExchange(&SPIDA1, strlen(outstring), outstring, instring);
    while (SPIDA1.state != SPI_READY)
      ; /* wait for transaction to finish */
    spiUnselect(&SPIDA1);
    if (strcmp("After SPI test  \r\n", instring) ||
        strcmp("After SPI test  \r\n", outstring) || cb_arg != 0) {
      chnWrite(&SD0, (const uint8_t *)fail_string, strlen(fail_string));
    }
    else {
      chnWrite(&SD0, (const uint8_t *)succeed_string, strlen(succeed_string));
    }
  }
}

/*
 * Threads static table, one entry per thread. The number of entries must
 * match NIL_CFG_NUM_THREADS.
 */
THD_TABLE_BEGIN
  THD_TABLE_ENTRY(waThread1, "spi_test", Thread1, NULL)
THD_TABLE_END

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  WDTCTL = WDTPW | WDTHOLD;

  halInit();
  chSysInit();
  dmaInit();

  /* This is now the idle thread loop, you may perform here a low priority
     task but you must never try to sleep or wait in this loop. Note that
     this tasks runs at the lowest priority level so any instruction added
     here will be executed after all other tasks have been started.*/
  while (true) {
  }
}