/
hydrabus_bbio_rawwire.c
240 lines (224 loc) · 6.88 KB
/
hydrabus_bbio_rawwire.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2014-2016 Benjamin VERNOUX
* Copyright (C) 2016 Nicolas OBERLI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common.h"
#include "tokenline.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "hydrabus_bbio.h"
#include "hydrabus_bbio_rawwire.h"
#include "hydrabus_mode_twowire.h"
#include "hydrabus_mode_threewire.h"
#include "hydrabus_bbio_aux.h"
const mode_rawwire_exec_t bbio_twowire = {
.init = &twowire_init_proto_default,
.pin_init = &twowire_pin_init,
.tim_init = &twowire_tim_init,
.tim_update = &twowire_tim_set_prescaler,
.read_u8 = &twowire_read_u8,
.read_bit_clock = &twowire_read_bit_clock,
.read_bit = &twowire_read_bit,
.write_u8 = &twowire_write_u8,
.write_bit = &twowire_send_bit,
.clock = &twowire_clock,
.clock_high = &twowire_clk_high,
.clock_low = &twowire_clk_low,
.data_high = &twowire_sda_high,
.data_low = &twowire_sda_low,
.cleanup = &twowire_cleanup,
};
const mode_rawwire_exec_t bbio_threewire = {
.init = &threewire_init_proto_default,
.pin_init = &threewire_pin_init,
.tim_init = &threewire_tim_init,
.tim_update = &threewire_tim_set_prescaler,
.read_u8 = &threewire_read_u8,
.read_bit_clock = &threewire_read_bit_clock,
.read_bit = &threewire_read_bit,
.write_u8 = &threewire_write_read_u8,
.write_bit = &threewire_send_bit,
.clock = &threewire_clock,
.clock_high = &threewire_clk_high,
.clock_low = &threewire_clk_low,
.data_high = &threewire_sdo_high,
.data_low = &threewire_sdo_low,
.cleanup = &threewire_cleanup,
};
static void bbio_mode_id(t_hydra_console *con)
{
cprint(con, BBIO_RAWWIRE_HEADER, 4);
}
void bbio_mode_rawwire(t_hydra_console *con)
{
uint8_t bbio_subcommand, i;
uint8_t rx_data[16], tx_data[16];
uint8_t data;
mode_rawwire_exec_t curmode = bbio_twowire;
mode_config_proto_t* proto = &con->mode->proto;
curmode.init(con);
curmode.pin_init(con);
curmode.tim_init(con);
curmode.clock_low(con);
curmode.data_low(con);
bbio_mode_id(con);
while (!hydrabus_ubtn()) {
if(chnRead(con->sdu, &bbio_subcommand, 1) == 1) {
switch(bbio_subcommand) {
case BBIO_RESET:
curmode.cleanup(con);
return;
case BBIO_MODE_ID:
bbio_mode_id(con);
break;
case BBIO_RAWWIRE_READ_BYTE:
rx_data[0] = curmode.read_u8(con);
cprint(con, (char *)&rx_data[0], 1);
break;
case BBIO_RAWWIRE_READ_BIT:
rx_data[0] = curmode.read_bit_clock(con);
cprint(con, (char *)&rx_data[0], 1);
break;
case BBIO_RAWWIRE_PEEK_INPUT:
rx_data[0] = curmode.read_bit(con);
cprint(con, (char *)&rx_data[0], 1);
break;
case BBIO_RAWWIRE_CLK_TICK:
curmode.clock(con);
cprint(con, "\x01", 1);
break;
case BBIO_RAWWIRE_CLK_LOW:
curmode.clock_low(con);
cprint(con, "\x01", 1);
break;
case BBIO_RAWWIRE_CLK_HIGH:
curmode.clock_high(con);
cprint(con, "\x01", 1);
break;
case BBIO_RAWWIRE_DATA_LOW:
curmode.data_low(con);
cprint(con, "\x01", 1);
break;
case BBIO_RAWWIRE_DATA_HIGH:
curmode.data_high(con);
cprint(con, "\x01", 1);
break;
default:
if ((bbio_subcommand & BBIO_AUX_MASK) == BBIO_AUX_MASK) {
cprintf(con, "%c", bbio_aux(con, bbio_subcommand));
} else if ((bbio_subcommand & BBIO_RAWWIRE_BULK_BIT) == BBIO_RAWWIRE_BULK_BIT) {
// data contains the number of bits to
// write
data = (bbio_subcommand & 0b1111) + 1;
chnRead(con->sdu, &tx_data[0], 1);
if(proto->config.rawwire.dev_bit_lsb_msb == DEV_FIRSTBIT_MSB) {
tx_data[0] = reverse_u8(tx_data[0]);
}
for (i=0; i<data; i++) {
curmode.write_bit(con, (tx_data[0]>>i) & 1);
}
cprint(con, "\x01", 1);
} else if ((bbio_subcommand & BBIO_RAWWIRE_BULK_TRANSFER) == BBIO_RAWWIRE_BULK_TRANSFER) {
// data contains the number of bytes to
// write
data = (bbio_subcommand & 0b1111) + 1;
chnRead(con->sdu, tx_data, data);
cprint(con, "\x01", 1);
for(i=0; i<data; i++) {
rx_data[i] = curmode.write_u8(con, tx_data[i]);
}
cprint(con, (char *)rx_data, data);
} else if ((bbio_subcommand & BBIO_RAWWIRE_BULK_CLK) == BBIO_RAWWIRE_BULK_CLK) {
// data contains the number of bytes to
// write
data = (bbio_subcommand & 0b1111) + 1;
for(i=0; i<data; i++) {
curmode.clock(con);
}
cprint(con, "\x01", 1);
} else if ((bbio_subcommand & BBIO_SPI_SET_SPEED) == BBIO_SPI_SET_SPEED) {
switch(bbio_subcommand & 0b11){
case 0:
proto->config.rawwire.dev_speed = 5000;
break;
case 1:
proto->config.rawwire.dev_speed = 50000;
break;
case 2:
proto->config.rawwire.dev_speed = 100000;
break;
case 3:
proto->config.rawwire.dev_speed = 1000000;
break;
}
curmode.tim_update(con);
} else if ((bbio_subcommand & BBIO_RAWWIRE_CONFIG) == BBIO_RAWWIRE_CONFIG) {
curmode.cleanup(con);
if(bbio_subcommand & 0b100){
curmode = bbio_threewire;
} else {
curmode = bbio_twowire;
}
curmode.init(con);
if(bbio_subcommand & 0b10){
proto->config.rawwire.dev_bit_lsb_msb = DEV_FIRSTBIT_LSB;
} else {
proto->config.rawwire.dev_bit_lsb_msb = DEV_FIRSTBIT_MSB;
}
if(bbio_subcommand & 0b1000){
proto->config.rawwire.dev_gpio_mode = MODE_CONFIG_DEV_GPIO_OUT_PUSHPULL;
} else {
proto->config.rawwire.dev_gpio_mode = MODE_CONFIG_DEV_GPIO_OUT_OPENDRAIN;
}
if(bbio_subcommand & 0b1){
proto->config.rawwire.clock_polarity = 1;
} else {
proto->config.rawwire.clock_polarity = 0;
}
curmode.pin_init(con);
curmode.tim_init(con);
if (proto->config.rawwire.clock_polarity == 0) {
curmode.clock_low(con);
} else {
curmode.clock_high(con);
}
curmode.data_low(con);
cprint(con, "\x01", 1);
} else if ((bbio_subcommand & BBIO_RAWWIRE_CONFIG_PERIPH) == BBIO_RAWWIRE_CONFIG_PERIPH) {
if(bbio_subcommand & 0b100){
proto->config.rawwire.dev_gpio_pull = MODE_CONFIG_DEV_GPIO_PULLUP;
} else {
proto->config.rawwire.dev_gpio_pull = MODE_CONFIG_DEV_GPIO_NOPULL;
}
//Set AUX[0] (PC4) value
bbio_aux_write((bbio_subcommand & 0b10)>>1);
curmode.pin_init(con);
curmode.tim_init(con);
if (proto->config.rawwire.clock_polarity == 0) {
curmode.clock_low(con);
} else {
curmode.clock_high(con);
}
curmode.data_low(con);
cprint(con, "\x01", 1);
}
}
}
}
curmode.cleanup(con);
}