-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.c
204 lines (175 loc) · 5.09 KB
/
main.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
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
/**
* Written by Thomas Roth - [email protected]
*
* Licensed under GPLv3
*
* Based on the spi_flash pico-example, which is:
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
* Also based on stm32-vserprog:
* https://github.com/dword1511/stm32-vserprog
*
*/
#include <stdio.h>
#include <string.h>
#include "pdnd/pdnd.h"
#include "pdnd/pdnd_display.h"
#include "pico/stdlib.h"
#include "pdnd/pio/pio_spi.h"
#include "spi.h"
#define BUS_SPI (1 << 3)
#define S_SUPPORTED_BUS BUS_SPI
#define S_CMD_MAP ( \
(1 << S_CMD_NOP) | \
(1 << S_CMD_Q_IFACE) | \
(1 << S_CMD_Q_CMDMAP) | \
(1 << S_CMD_Q_PGMNAME) | \
(1 << S_CMD_Q_SERBUF) | \
(1 << S_CMD_Q_BUSTYPE) | \
(1 << S_CMD_SYNCNOP) | \
(1 << S_CMD_O_SPIOP) | \
(1 << S_CMD_S_BUSTYPE) | \
(1 << S_CMD_S_SPI_FREQ)| \
(1 << S_CMD_S_PIN_STATE) \
)
static inline void cs_select(uint cs_pin) {
asm volatile("nop \n nop \n nop"); // FIXME
gpio_put(cs_pin, 0);
asm volatile("nop \n nop \n nop"); // FIXME
}
static inline void cs_deselect(uint cs_pin) {
asm volatile("nop \n nop \n nop"); // FIXME
gpio_put(cs_pin, 1);
asm volatile("nop \n nop \n nop"); // FIXME
}
uint32_t getu24() {
uint32_t c1 = getchar();
uint32_t c2 = getchar();
uint32_t c3 = getchar();
return c1 | (c2<<8) | (c3<<16);
}
void putu32(uint32_t d) {
char buf[4];
memcpy(buf, &d, 4);
putchar(buf[0]);
putchar(buf[1]);
putchar(buf[2]);
putchar(buf[3]);
}
unsigned char write_buffer[4096];
void process(pio_spi_inst_t *spi, int command) {
switch(command) {
case S_CMD_NOP:
putchar(S_ACK);
break;
case S_CMD_Q_IFACE:
putchar(S_ACK);
putchar(0x01);
putchar(0x00);
break;
case S_CMD_Q_CMDMAP:
putchar(S_ACK);
putu32(S_CMD_MAP);
for(int i = 0; i < 32 - sizeof(uint32_t); i++) {
putchar(0);
}
break;
case S_CMD_Q_PGMNAME:
putchar(S_ACK);
fwrite("pico-serprog\x0\x0\x0\x0\x0", 1, 16, stdout);
fflush(stdout);
break;
case S_CMD_Q_SERBUF:
putchar(S_ACK);
putchar(0xFF);
putchar(0xFF);
break;
case S_CMD_Q_BUSTYPE:
putchar(S_ACK);
putchar(S_SUPPORTED_BUS);
break;
case S_CMD_SYNCNOP:
putchar(S_NAK);
putchar(S_ACK);
break;
case S_CMD_S_BUSTYPE:
{
int bustype = getchar();
if((bustype | S_SUPPORTED_BUS) == S_SUPPORTED_BUS) {
putchar(S_ACK);
} else {
putchar(S_NAK);
}
}
break;
case S_CMD_O_SPIOP:
{
uint32_t wlen = getu24();
uint32_t rlen = getu24();
cs_select(pdnd_out_pin(PDND_SPI_CS));
fread(write_buffer, 1, wlen, stdin);
pio_spi_write8_blocking(spi, write_buffer, wlen);
putchar(S_ACK);
char buf;
for(uint32_t i = 0; i < rlen; i++) {
pio_spi_read8_blocking(spi, &buf, 1);
putchar(buf);
}
cs_deselect(pdnd_out_pin(PDND_SPI_CS));
}
break;
case S_CMD_S_SPI_FREQ:
// TODO
putchar(S_ACK);
putchar(0x0);
putchar(0x40);
putchar(0x0);
putchar(0x0);
break;
case S_CMD_S_PIN_STATE:
//TODO:
getchar();
putchar(S_ACK);
break;
default:
putchar(S_NAK);
}
}
int main() {
stdio_init_all();
stdio_set_translate_crlf(&stdio_usb, false);
pdnd_initialize();
pdnd_display_initialize();
pprintf("Serprog");
pdnd_enable_buffers(1);
// Initialize CS
gpio_init(pdnd_out_pin(PDND_SPI_CS));
gpio_put(pdnd_out_pin(PDND_SPI_CS), 1);
gpio_set_dir(pdnd_out_pin(PDND_SPI_CS), GPIO_OUT);
gpio_init(pdnd_out_pin(PDND_SPI_HOLD));
gpio_set_dir(pdnd_out_pin(PDND_SPI_HOLD), GPIO_OUT);
gpio_put(pdnd_out_pin(PDND_SPI_HOLD), 1);
gpio_init(pdnd_out_pin(PDND_SPI_WP));
gpio_set_dir(pdnd_out_pin(PDND_SPI_WP), GPIO_OUT);
gpio_put(pdnd_out_pin(PDND_SPI_WP), 1);
// We use PIO 1
pio_spi_inst_t spi = {
.pio = pio1,
.sm = 0,
.cs_pin = pdnd_out_pin(PDND_SPI_CS)
};
uint offset = pio_add_program(spi.pio, &spi_cpha0_program);
pio_spi_init(spi.pio, spi.sm, offset,
8, // 8 bits per SPI frame
31.25f, // 1 MHz @ 125 clk_sys
false, // CPHA = 0
false, // CPOL = 0
pdnd_out_pin(PDND_SPI_CLK),
pdnd_out_pin(PDND_SPI_MOSI),
pdnd_in_pin(PDND_SPI_MISO));
// Command handling
while(1) {
int command = getchar();
process(&spi, command);
}
return 0;
}