-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmod_spi.c
105 lines (88 loc) · 2.92 KB
/
pmod_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* MIT License
*
* Copyright (c) 2023 tinyVision.ai
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// libc
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
// pico-sdk
#include "hardware/sync.h"
#include "pico/stdlib.h"
// pico-ice-sdk
#include "boards/pico_ice.h"
// pmod
#include "pmod_spi.h"
#include "pmod.h"
void pmod_spi_init(const pmod_1x_t *pmod) {
gpio_init(pmod->spi.cipo);
gpio_init(pmod->spi.copi);
gpio_put(pmod->spi.copi, true);
gpio_set_dir(pmod->spi.copi, GPIO_OUT);
gpio_init(pmod->spi.clk);
gpio_put(pmod->spi.clk, true);
gpio_set_dir(pmod->spi.clk, GPIO_OUT);
}
static inline void delay(void)
{
sleep_us(1);
}
static uint8_t transfer_byte(const pmod_1x_t *pmod, uint8_t tx) {
uint8_t rx;
for (uint8_t i = 0; i < 8; i++) {
// Update TX and immediately set negative edge.
gpio_put(pmod->spi.clk, false);
gpio_put(pmod->spi.copi, tx >> 7);
tx <<= 1;
// stable for a while with clock low
delay();
// Sample RX as we set positive edge.
rx <<= 1;
rx |= gpio_get(pmod->spi.cipo) & 1;
gpio_put(pmod->spi.clk, true);
// stable for a while with clock high
delay();
}
return rx;
}
void pmod_spi_chip_select(const pmod_1x_t *pmod, uint8_t cs_n_pin) {
gpio_put(cs_n_pin, false);
gpio_set_dir(cs_n_pin, GPIO_OUT);
delay();
}
void pmod_spi_chip_deselect(const pmod_1x_t *pmod, uint8_t cs_n_pin) {
gpio_put(pmod->spi.copi, true);
gpio_put(cs_n_pin, true);
gpio_set_dir(cs_n_pin, GPIO_OUT);
delay();
}
void pmod_spi_write(const pmod_1x_t *pmod, uint8_t const *buf, size_t len) {
for (; len > 0; len--, buf++) {
transfer_byte(pmod, *buf);
}
}
void pmod_spi_read(const pmod_1x_t *pmod, uint8_t tx, uint8_t *buf, size_t len) {
for (; len > 0; len--, buf++) {
*buf = transfer_byte(pmod, tx);
}
}