41
41
(1 << S_CMD_S_SPI_FREQ) | \
42
42
(1 << S_CMD_S_PIN_STATE) | \
43
43
(1 << S_CMD_S_SPI_CS) | \
44
- (1 << S_CMD_S_SPI_MODE) \
44
+ (1 << S_CMD_S_SPI_MODE) | \
45
+ (1 << S_CMD_S_CS_MODE) \
45
46
)
46
47
47
48
enum spi_mode {
@@ -52,6 +53,15 @@ enum spi_mode {
52
53
53
54
enum spi_mode current_spi_mode = SPI_MODE_HALF_DUPLEX ;
54
55
56
+ enum cs_mode {
57
+ CS_MODE_AUTO = 0 ,
58
+ CS_MODE_SELECTED = 1 ,
59
+ CS_MODE_DESELECTED = 2 ,
60
+ CS_MODE_MAX = CS_MODE_DESELECTED ,
61
+ };
62
+
63
+ enum cs_mode current_cs_mode = CS_MODE_AUTO ;
64
+
55
65
uint active_cs_pin = 0 ;
56
66
#define NUM_CS_AVAILABLE 4 // Number of usable chip selects
57
67
uint8_t cs_pins [NUM_CS_AVAILABLE ] = { PIN_CS_0 , PIN_CS_1 , PIN_CS_2 , PIN_CS_3 };
@@ -200,7 +210,9 @@ void process(const pio_spi_inst_t *spi, int command) {
200
210
uint32_t wlen = getu24 ();
201
211
uint32_t rlen = getu24 ();
202
212
203
- cs_select (active_cs_pin );
213
+ if (current_cs_mode == CS_MODE_AUTO ) {
214
+ cs_select (active_cs_pin );
215
+ }
204
216
switch (current_spi_mode ) {
205
217
case SPI_MODE_HALF_DUPLEX :
206
218
spi_half_duplex (spi , wlen , rlen );
@@ -211,7 +223,9 @@ void process(const pio_spi_inst_t *spi, int command) {
211
223
default :
212
224
break ;
213
225
}
214
- cs_deselect (active_cs_pin );
226
+ if (current_spi_mode == CS_MODE_AUTO ) {
227
+ cs_deselect (active_cs_pin );
228
+ }
215
229
}
216
230
break ;
217
231
case S_CMD_S_SPI_FREQ :
@@ -253,6 +267,27 @@ void process(const pio_spi_inst_t *spi, int command) {
253
267
}
254
268
break ;
255
269
}
270
+ case S_CMD_S_CS_MODE :
271
+ {
272
+ uint8_t cs_mode = getchar ();
273
+ switch (cs_mode ) {
274
+ case CS_MODE_AUTO :
275
+ case CS_MODE_DESELECTED :
276
+ cs_deselect (active_cs_pin );
277
+ current_cs_mode = cs_mode ;
278
+ putchar (S_ACK );
279
+ break ;
280
+ case CS_MODE_SELECTED :
281
+ cs_select (active_cs_pin );
282
+ current_cs_mode = cs_mode ;
283
+ putchar (S_ACK );
284
+ break ;
285
+ default :
286
+ putchar (S_NAK );
287
+ break ;
288
+ }
289
+ break ;
290
+ }
256
291
default :
257
292
putchar (S_NAK );
258
293
}
0 commit comments