-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspi.c
67 lines (54 loc) · 1.14 KB
/
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
#include <stdbool.h>
#include "spi.h"
#include "mackerel.h"
#define GPIO_ON(pin) MEM(DUART1_OPR_RESET) = (1 << pin)
#define GPIO_OFF(pin) MEM(DUART1_OPR) = (1 << pin);
#define MISO_GET() (MEM(DUART1_IP) & (1 << MISO))
void spi_init(uint8_t cs)
{
// Default state of the SPI pins
GPIO_ON(cs);
GPIO_ON(MOSI);
GPIO_OFF(SCLK);
GPIO_OFF(LED);
}
uint8_t spi_transfer(uint8_t cs, uint8_t byte_to_send)
{
uint8_t byte_received = 0;
int i;
GPIO_OFF(SCLK);
// Set chip select (active low)
GPIO_OFF(cs);
for (i = 7; i >= 0; i--)
{
if (byte_to_send & (1 << i))
{
GPIO_ON(MOSI);
}
else
{
GPIO_OFF(MOSI);
}
// Clock high
GPIO_ON(SCLK);
// Shift in a bit
if (MISO_GET())
{
byte_received |= (1 << i);
}
// Clock low
GPIO_OFF(SCLK);
}
// disable SD card
GPIO_ON(cs);
return byte_received;
}
void spi_loop_clk()
{
// Toggle the SPI clock 80 times with CS disabled
for (int i = 0; i < 80; i++)
{
GPIO_ON(SCLK);
GPIO_OFF(SCLK);
}
}