-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo_spi.c
89 lines (76 loc) · 1.55 KB
/
demo_spi.c
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
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <cortex_m0.h>
#include <delay.h>
#include <random.h>
#include <fifo.h>
#include <spi_master.h>
#define my_spi SPI0
#define spi_pin 29
fifo_t buffer;
fifo_t *spi_buffer;
/**
* Fills the referenced FIFO with <count> random bytes
*/
void generate_random_fifo(fifo_t* buffer, uint8_t count)
{
char c = 0x88;
for (uint8_t i=0; i<count; i++)
fifo_write(buffer, &c);
}
/**
* Initiates transmission of all bytes within the specified FIFO via SPI
*/
void spi_transmit_fifo(fifo_t* buffer)
{
// if (spi_still_transmitting_fifo(my_spi))
// return;
spi_buffer = buffer;
interrupt_enable(INTERRUPT_SPI);
spi_interrupt_upon_READY_enable(my_spi);
spi_enable(my_spi);
}
/**
* Use interrupt to reload out buffer and continue transmission
*/
void SPI0_TWI0_Handler()
{
// event must be cleared
SPI_EVENT_READY(my_spi) = 0;
if (fifo_available(spi_buffer))
{
// enqueue next byte for transmission
char c;
fifo_read(&buffer, &c);
spi_write(my_spi, c);
}
else
{
// transmission sequence completed
spi_disable(my_spi);
}
}
/*
* Arduino-style main functions
*/
void setup()
{
//random_init();
// setup SPI
// pins, frequency, etc.
spi_pin_select(my_spi, SPI_PIN_DISABLED, spi_pin, SPI_PIN_DISABLED);
}
void loop()
{
generate_random_fifo(&buffer, 20);
spi_transmit_fifo(&buffer);
delay_ms(1000);
}
int main()
{
setup();
while (true)
loop();
return 0;
}