summaryrefslogtreecommitdiff
path: root/code/fe310/eos/net.c
blob: b6f5722a138ce5d1b3d63c582f81c3606ada11bc (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <stdlib.h>
#include <stdint.h>

#include "encoding.h"
#include "platform.h"

#include "eos.h"
#include "msgq.h"
#include "interrupt.h"
#include "event.h"

#include "spi.h"
#include "spi_def.h"

#include "net.h"
#include "net_def.h"
#include "irq_def.h"

#define MIN(X, Y)               (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y)               (((X) > (Y)) ? (X) : (Y))
#define NET_BUFQ_IDX_MASK(IDX)  ((IDX) & (NET_SIZE_BUFQ - 1))

typedef struct EOSNetBufQ {
    uint8_t idx_r;
    uint8_t idx_w;
    unsigned char *array[NET_SIZE_BUFQ];
} EOSNetBufQ;

static EOSMsgQ net_send_q;
static EOSMsgItem net_sndq_array[NET_SIZE_BUFQ];

static EOSNetBufQ net_buf_q;
static unsigned char net_bufq_array[NET_SIZE_BUFQ][NET_SIZE_BUF];

static uint8_t net_state_flags = 0;
static unsigned char net_state_type = 0;
static uint32_t net_state_len_tx = 0;
static uint32_t net_state_len_rx = 0;

static uint8_t net_state_next_cnt = 0;
static unsigned char *net_state_next_buf = NULL;

static eos_evt_fptr_t evt_handler[EOS_NET_MAX_MTYPE];
static uint16_t evt_handler_flags_buf_free = 0;
static uint16_t evt_handler_flags_buf_acq = 0;

extern EOSMsgQ _eos_event_q;
extern uint32_t _eos_spi_state_len;
extern uint32_t _eos_spi_state_idx_tx;
extern uint32_t _eos_spi_state_idx_rx;
extern unsigned char *_eos_spi_state_buf;

static void net_bufq_init(void) {
    int i;

    net_buf_q.idx_r = 0;
    net_buf_q.idx_w = NET_SIZE_BUFQ;
    for (i=0; i<NET_SIZE_BUFQ; i++) {
        net_buf_q.array[i] = net_bufq_array[i];
    }
}

static int net_bufq_push(unsigned char *buffer) {
    net_buf_q.array[NET_BUFQ_IDX_MASK(net_buf_q.idx_w++)] = buffer;
    return EOS_OK;
}

static unsigned char *net_bufq_pop(void) {
    if (net_buf_q.idx_r == net_buf_q.idx_w) return NULL;
    return net_buf_q.array[NET_BUFQ_IDX_MASK(net_buf_q.idx_r++)];
}

static void net_xchg_reset(void) {
    net_state_flags &= ~NET_STATE_FLAG_CTS;
    net_state_flags |= NET_STATE_FLAG_RST;

    // before starting a transaction, set SPI peripheral to desired mode
    SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD;

    SPI1_REG(SPI_REG_TXFIFO) = 0;

    SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(0);
    SPI1_REG(SPI_REG_IE) = SPI_IP_RXWM;
}

static void net_xchg_start(unsigned char type, unsigned char *buffer, uint16_t len) {
    net_state_flags &= ~NET_STATE_FLAG_CTS;
    net_state_flags |= NET_STATE_FLAG_INIT;

    if (net_state_next_cnt && (net_state_next_buf == NULL)) type |= NET_MTYPE_FLAG_ONEW;
    if (type & NET_MTYPE_FLAG_ONEW) net_state_flags |= NET_STATE_FLAG_ONEW;

    net_state_type = type;
    net_state_len_tx = len;
    net_state_len_rx = 0;

    _eos_spi_state_buf = buffer;

    // before starting a transaction, set SPI peripheral to desired mode
    SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD;

    SPI1_REG(SPI_REG_TXFIFO) = ((type << 3) | (len >> 8)) & 0xFF;
    SPI1_REG(SPI_REG_TXFIFO) = len & 0xFF;

    SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(1);
    SPI1_REG(SPI_REG_IE) = SPI_IP_RXWM;
}

static void net_xchg_handler(void) {
    volatile uint32_t r1, r2;

    if (net_state_flags & NET_STATE_FLAG_RST) {
        net_state_flags &= ~NET_STATE_FLAG_RST;

        r1 = SPI1_REG(SPI_REG_RXFIFO);
        SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
        SPI1_REG(SPI_REG_IE) = 0x0;

        return;
    } else if (net_state_flags & NET_STATE_FLAG_INIT) {
        net_state_flags &= ~NET_STATE_FLAG_INIT;
        SPI1_REG(SPI_REG_TXCTRL) = SPI_TXWM(SPI_SIZE_WM);
        SPI1_REG(SPI_REG_IE) = SPI_IP_TXWM;

        r1 = SPI1_REG(SPI_REG_RXFIFO);
        r2 = SPI1_REG(SPI_REG_RXFIFO);

        if (net_state_flags & NET_STATE_FLAG_ONEW) {
            r1 = 0;
            r2 = 0;
        }

        net_state_type = ((r1 & 0xFF) >> 3);
        net_state_len_rx = ((r1 & 0x07) << 8);
        net_state_len_rx |= (r2 & 0xFF);
        _eos_spi_state_len = MAX(net_state_len_tx, net_state_len_rx);
        _eos_spi_state_idx_tx = 0;
        _eos_spi_state_idx_rx = 0;

        // Work around esp32 bug
        if (_eos_spi_state_len < 6) {
            _eos_spi_state_len = 6;
        } else if ((_eos_spi_state_len + 2) % 4 != 0) {
            _eos_spi_state_len = ((_eos_spi_state_len + 2)/4 + 1) * 4 - 2;
        }

        if (_eos_spi_state_len > NET_SIZE_BUF) {
            SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
            SPI1_REG(SPI_REG_IE) = 0x0;
        }

        return;
    }

    eos_spi_xchg_handler();
}

static int net_xchg_next(unsigned char *_buffer) {
    unsigned char type;
    unsigned char *buffer = NULL;
    uint16_t len;

    eos_msgq_pop(&net_send_q, &type, &buffer, &len);
    if (type) {
        net_xchg_start(type, buffer, len);
    } else if (net_state_flags & NET_STATE_FLAG_RTS) {
        if (_buffer == NULL) _buffer = net_bufq_pop();
        if (_buffer) {
            net_xchg_start(0, _buffer, 0);
            return 0;
        }
    }
    return 1;
}

void eos_net_xchg_done(void) {
    if (net_state_type) {
        int r = eos_msgq_push(&_eos_event_q, EOS_EVT_NET | net_state_type, _eos_spi_state_buf, net_state_len_rx);
        if (r) net_bufq_push(_eos_spi_state_buf);
    } else if (((net_state_flags & NET_STATE_FLAG_ONEW) || net_state_next_cnt) && (net_state_next_buf == NULL)) {
        net_state_next_buf = _eos_spi_state_buf;
        net_state_flags &= ~NET_STATE_FLAG_ONEW;
    } else {
        net_bufq_push(_eos_spi_state_buf);
    }
}

static void net_handler_cts(void) {
    GPIO_REG(GPIO_RISE_IP) = (1 << NET_PIN_CTS);
    net_state_flags |= NET_STATE_FLAG_CTS;

    if (net_state_flags & NET_STATE_FLAG_RUN) {
        net_xchg_next(NULL);
    }
}

static void net_handler_rts(void) {
    uint32_t rts_offset = (1 << NET_PIN_RTS);
    if (GPIO_REG(GPIO_RISE_IP) & rts_offset) {
        GPIO_REG(GPIO_RISE_IP) = rts_offset;
        net_state_flags |= NET_STATE_FLAG_RTS;
        if ((net_state_flags & NET_STATE_FLAG_RUN) && (net_state_flags & NET_STATE_FLAG_CTS)) net_xchg_reset();
    } else if (GPIO_REG(GPIO_FALL_IP) & rts_offset) {
        GPIO_REG(GPIO_FALL_IP) = rts_offset;
        net_state_flags &= ~NET_STATE_FLAG_RTS;
    }
}

static void net_handler_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
    unsigned char idx = (type & ~EOS_EVT_MASK) - 1;
    uint16_t buf_free = ((uint16_t)1 << idx) & evt_handler_flags_buf_free;
    uint16_t buf_acq = ((uint16_t)1 << idx) & evt_handler_flags_buf_acq;

    if (idx >= EOS_NET_MAX_MTYPE) {
        eos_evtq_bad_handler(type, buffer, len);
        eos_net_free(buffer, 0);
        return;
    }
    if (buf_free) {
        eos_net_free(buffer, buf_acq);
        buffer = NULL;
        len = 0;
    }

    evt_handler[idx](type, buffer, len);

    if (buf_free && buf_acq) eos_net_release();
}

void eos_net_set_handler(unsigned char mtype, eos_evt_fptr_t handler, uint8_t flags) {
    if (mtype && (mtype <= EOS_NET_MAX_MTYPE)) {
        mtype--;
    } else {
        return;
    }
    if (flags) {
        uint16_t flag = (uint16_t)1 << mtype;
        if (flags & EOS_NET_FLAG_BUF_FREE) evt_handler_flags_buf_free |= flag;
        if (flags & EOS_NET_FLAG_BUF_ACQ) evt_handler_flags_buf_acq |= flag;
    }
    evt_handler[mtype] = handler;
}

void eos_net_init(void) {
    int i;

    GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << NET_PIN_CTS);
    GPIO_REG(GPIO_PULLUP_EN) |=  (1 << NET_PIN_CTS);
    GPIO_REG(GPIO_INPUT_EN)  |=  (1 << NET_PIN_CTS);
    GPIO_REG(GPIO_RISE_IE)   |=  (1 << NET_PIN_CTS);
    eos_intr_set(INT_GPIO_BASE + NET_PIN_CTS, IRQ_PRIORITY_NET_CTS, net_handler_cts);

    GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << NET_PIN_RTS);
    GPIO_REG(GPIO_PULLUP_EN) |=  (1 << NET_PIN_RTS);
    GPIO_REG(GPIO_INPUT_EN)  |=  (1 << NET_PIN_RTS);
    GPIO_REG(GPIO_RISE_IE)   |=  (1 << NET_PIN_RTS);
    GPIO_REG(GPIO_FALL_IE)   |=  (1 << NET_PIN_RTS);
    eos_intr_set(INT_GPIO_BASE + NET_PIN_RTS, IRQ_PRIORITY_NET_RTS, net_handler_rts);

    net_bufq_init();
    eos_msgq_init(&net_send_q, net_sndq_array, NET_SIZE_BUFQ);
    for (i=0; i<EOS_NET_MAX_MTYPE; i++) {
        evt_handler[i] = eos_evtq_bad_handler;
    }
    eos_evtq_set_handler(EOS_EVT_NET, net_handler_evt);
}

void eos_net_start(void) {
    eos_intr_set_handler(INT_SPI1_BASE, net_xchg_handler);
    SPI1_REG(SPI_REG_SCKDIV) = SPI_DIV_NET;
    SPI1_REG(SPI_REG_CSID) = SPI_CS_IDX_NET;

    net_state_flags |= NET_STATE_FLAG_RUN;
    if (net_state_flags & NET_STATE_FLAG_CTS) net_xchg_next(NULL);
}

void eos_net_stop(void) {
    volatile uint8_t done = 0;

    while (!done) {
        clear_csr(mstatus, MSTATUS_MIE);
        net_state_flags &= ~NET_STATE_FLAG_RUN;
        done = net_state_flags & NET_STATE_FLAG_CTS;
        if (!done) asm volatile ("wfi");
        set_csr(mstatus, MSTATUS_MIE);
    }
}

int eos_net_acquire(unsigned char reserved) {
    int ret = 0;

    if (reserved) {
        while (!ret) {
            clear_csr(mstatus, MSTATUS_MIE);
            if (net_state_next_buf) {
                ret = 1;
                net_state_next_cnt--;
            } else {
                asm volatile ("wfi");
            }
            set_csr(mstatus, MSTATUS_MIE);
        }
    } else {
        clear_csr(mstatus, MSTATUS_MIE);
        if (net_state_next_buf == NULL) net_state_next_buf = net_bufq_pop();
        ret = (net_state_next_buf != NULL);
        if (!ret) net_state_next_cnt++;
        set_csr(mstatus, MSTATUS_MIE);
    }
    return ret;
}

void eos_net_release(void) {
    clear_csr(mstatus, MSTATUS_MIE);
    if (!net_state_next_cnt && net_state_next_buf) {
        net_bufq_push(net_state_next_buf);
        net_state_next_buf = NULL;
    }
    set_csr(mstatus, MSTATUS_MIE);
}

unsigned char *eos_net_alloc(void) {
    unsigned char *ret = NULL;

    while (ret == NULL) {
        clear_csr(mstatus, MSTATUS_MIE);
        if (net_state_next_buf) {
            ret = net_state_next_buf;
            net_state_next_buf = NULL;
        } else {
            asm volatile ("wfi");
        }
        set_csr(mstatus, MSTATUS_MIE);
    }

    return ret;
}

void eos_net_free(unsigned char *buffer, unsigned char more) {
    uint8_t do_release = 1;

    clear_csr(mstatus, MSTATUS_MIE);
    if ((more || net_state_next_cnt) && (net_state_next_buf == NULL)) {
        net_state_next_buf = buffer;
    } else {
        if ((net_state_flags & NET_STATE_FLAG_RUN) && (net_state_flags & NET_STATE_FLAG_CTS)) do_release = net_xchg_next(buffer);
        if (do_release) net_bufq_push(buffer);
    }
    set_csr(mstatus, MSTATUS_MIE);
}

int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char more) {
    int rv = EOS_OK;

    if (more) {
        type |= NET_MTYPE_FLAG_ONEW;
    }
    clear_csr(mstatus, MSTATUS_MIE);
    if ((net_state_flags & NET_STATE_FLAG_RUN) && (net_state_flags & NET_STATE_FLAG_CTS)) {
        net_xchg_start(type, buffer, len);
    } else {
        rv = eos_msgq_push(&net_send_q, type, buffer, len);
        if (rv) net_bufq_push(buffer);
    }
    set_csr(mstatus, MSTATUS_MIE);

    return rv;
}