summaryrefslogtreecommitdiff
path: root/platforms/chibios/drivers/serial.c
blob: e5f346ba3372cc04785bcd64187f5b4ee010761f (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * WARNING: be careful changing this code, it is very timing dependent
 */

#include "quantum.h"
#include "serial.h"
#include "wait.h"
#include "synchronization_util.h"

#include <hal.h>

// TODO: resolve/remove build warnings
#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) && defined(PROTOCOL_CHIBIOS) && defined(WS2812_DRIVER_BITBANG)
#    warning "RGBLED_SPLIT not supported with bitbang WS2812 driver"
#endif

// default wait implementation cannot be called within interrupt
//   this method seems to be more accurate than GPT timers
#if PORT_SUPPORTS_RT == FALSE
#    error "chSysPolledDelayX method not supported on this platform"
#else
#    undef wait_us
#    define wait_us(x) chSysPolledDelayX(US2RTC(CPU_CLOCK, x))
#endif

#ifndef SELECT_SOFT_SERIAL_SPEED
#    define SELECT_SOFT_SERIAL_SPEED 1
// TODO: correct speeds...
//  0: about 189kbps (Experimental only)
//  1: about 137kbps (default)
//  2: about 75kbps
//  3: about 39kbps
//  4: about 26kbps
//  5: about 20kbps
#endif

// Serial pulse period in microseconds. At the moment, going lower than 12 causes communication failure
#if SELECT_SOFT_SERIAL_SPEED == 0
#    define SERIAL_DELAY 12
#elif SELECT_SOFT_SERIAL_SPEED == 1
#    define SERIAL_DELAY 16
#elif SELECT_SOFT_SERIAL_SPEED == 2
#    define SERIAL_DELAY 24
#elif SELECT_SOFT_SERIAL_SPEED == 3
#    define SERIAL_DELAY 32
#elif SELECT_SOFT_SERIAL_SPEED == 4
#    define SERIAL_DELAY 48
#elif SELECT_SOFT_SERIAL_SPEED == 5
#    define SERIAL_DELAY 64
#else
#    error invalid SELECT_SOFT_SERIAL_SPEED value
#endif

inline static void serial_delay(void) {
    wait_us(SERIAL_DELAY);
}
inline static void serial_delay_half(void) {
    wait_us(SERIAL_DELAY / 2);
}
inline static void serial_delay_blip(void) {
    wait_us(1);
}
inline static void serial_output(void) {
    setPinOutput(SOFT_SERIAL_PIN);
}
inline static void serial_input(void) {
    setPinInputHigh(SOFT_SERIAL_PIN);
}
inline static bool serial_read_pin(void) {
    return !!readPin(SOFT_SERIAL_PIN);
}
inline static void serial_low(void) {
    writePinLow(SOFT_SERIAL_PIN);
}
inline static void serial_high(void) {
    writePinHigh(SOFT_SERIAL_PIN);
}

void interrupt_handler(void *arg);

// Use thread + palWaitLineTimeout instead of palSetLineCallback
//  - Methods like setPinOutput and palEnableLineEvent/palDisableLineEvent
//    cause the interrupt to lock up, which would limit to only receiving data...
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {
    (void)arg;
    chRegSetThreadName("blinker");
    while (true) {
        palWaitLineTimeout(SOFT_SERIAL_PIN, TIME_INFINITE);

        split_shared_memory_lock();
        interrupt_handler(NULL);
        split_shared_memory_unlock();
    }
}

void soft_serial_initiator_init(void) {
    serial_output();
    serial_high();
}

void soft_serial_target_init(void) {
    serial_input();

    palEnablePadEvent(PAL_PORT(SOFT_SERIAL_PIN), PAL_PAD(SOFT_SERIAL_PIN), PAL_EVENT_MODE_FALLING_EDGE);
    chThdCreateStatic(waThread1, sizeof(waThread1), HIGHPRIO, Thread1, NULL);
}

// Used by the master to synchronize timing with the slave.
static void __attribute__((noinline)) sync_recv(void) {
    serial_input();
    // This shouldn't hang if the slave disconnects because the
    // serial line will float to high if the slave does disconnect.
    while (!serial_read_pin()) {
    }

    serial_delay();
}

// Used by the slave to send a synchronization signal to the master.
static void __attribute__((noinline)) sync_send(void) {
    serial_output();

    serial_low();
    serial_delay();

    serial_high();
}

// Reads a byte from the serial line
static uint8_t __attribute__((noinline)) serial_read_byte(void) {
    uint8_t byte = 0;
    serial_input();
    for (uint8_t i = 0; i < 8; ++i) {
        byte = (byte << 1) | serial_read_pin();
        serial_delay();
    }

    return byte;
}

// Sends a byte with MSB ordering
static void __attribute__((noinline)) serial_write_byte(uint8_t data) {
    uint8_t b = 8;
    serial_output();
    while (b--) {
        if (data & (1 << b)) {
            serial_high();
        } else {
            serial_low();
        }
        serial_delay();
    }
}

// interrupt handle to be used by the slave device
void interrupt_handler(void *arg) {
    chSysLockFromISR();

    sync_send();

    // read mid pulses
    serial_delay_blip();

    uint8_t checksum_computed = 0;
    int     sstd_index        = 0;

    sstd_index = serial_read_byte();
    sync_send();

    split_transaction_desc_t *trans = &split_transaction_table[sstd_index];
    for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
        split_trans_initiator2target_buffer(trans)[i] = serial_read_byte();
        sync_send();
        checksum_computed += split_trans_initiator2target_buffer(trans)[i];
    }
    checksum_computed ^= 7;

    serial_read_byte();
    sync_send();

    // wait for the sync to finish sending
    serial_delay();

    // Allow any slave processing to occur
    if (trans->slave_callback) {
        trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->target2initiator_buffer_size, split_trans_target2initiator_buffer(trans));
    }

    uint8_t checksum = 0;
    for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
        serial_write_byte(split_trans_target2initiator_buffer(trans)[i]);
        sync_send();
        serial_delay_half();
        checksum += split_trans_target2initiator_buffer(trans)[i];
    }
    serial_write_byte(checksum ^ 7);
    sync_send();

    // wait for the sync to finish sending
    serial_delay();

    // end transaction
    serial_input();

    // TODO: remove extra delay between transactions
    serial_delay();

    chSysUnlockFromISR();
}

static inline bool initiate_transaction(uint8_t sstd_index) {
    if (sstd_index > NUM_TOTAL_TRANSACTIONS) return false;

    split_transaction_desc_t *trans = &split_transaction_table[sstd_index];

    // TODO: remove extra delay between transactions
    serial_delay();

    // this code is very time dependent, so we need to disable interrupts
    chSysLock();

    // signal to the slave that we want to start a transaction
    serial_output();
    serial_low();
    serial_delay_blip();

    // wait for the slaves response
    serial_input();
    serial_high();
    serial_delay();

    // check if the slave is present
    if (serial_read_pin()) {
        // slave failed to pull the line low, assume not present
        serial_dprintf("serial::NO_RESPONSE\n");
        chSysUnlock();
        return false;
    }

    // if the slave is present synchronize with it
    uint8_t checksum = 0;
    // send data to the slave
    serial_write_byte(sstd_index); // first chunk is transaction id
    sync_recv();

    for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
        serial_write_byte(split_trans_initiator2target_buffer(trans)[i]);
        sync_recv();
        checksum += split_trans_initiator2target_buffer(trans)[i];
    }
    serial_write_byte(checksum ^ 7);
    sync_recv();

    serial_delay();
    serial_delay(); // read mid pulses

    // receive data from the slave
    uint8_t checksum_computed = 0;
    for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
        split_trans_target2initiator_buffer(trans)[i] = serial_read_byte();
        sync_recv();
        checksum_computed += split_trans_target2initiator_buffer(trans)[i];
    }
    checksum_computed ^= 7;
    uint8_t checksum_received = serial_read_byte();

    sync_recv();
    serial_delay();

    if ((checksum_computed) != (checksum_received)) {
        serial_dprintf("serial::FAIL[%u,%u,%u]\n", checksum_computed, checksum_received, sstd_index);
        serial_output();
        serial_high();

        chSysUnlock();
        return false;
    }

    // always, release the line when not in use
    serial_high();
    serial_output();

    chSysUnlock();
    return true;
}

/////////
//  start transaction by initiator
//
// bool  soft_serial_transaction(int sstd_index)
//
// this code is very time dependent, so we need to disable interrupts
bool soft_serial_transaction(int sstd_index) {
    split_shared_memory_lock();
    bool result = initiate_transaction((uint8_t)sstd_index);
    split_shared_memory_unlock();
    return result;
}