aboutsummaryrefslogtreecommitdiffstats
path: root/keyboards/chidori/board.c
blob: e00156eb9032ef969e8087e6d7be5c858964c002 (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
/* Copyright 2019 ENDO Katsuhiro <ka2hiro@kagizaraya.jp>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdint.h>
#include <stdbool.h>
#include "wait.h"
#include "print.h"
#include "debug.h"
#include "matrix.h"
#include "quantum.h"
#include "board.h"
#include "i2c_master.h"

static board_info_t  boards[NUM_BOARDS] = BOARD_INFOS;
static board_info_t* master_board       = NULL;

static bool          board_is_master(board_info_t* board);
static bool          board_is_initialized(board_info_t* board);
static board_info_t* get_board_by_index(uint8_t board_index);
static uint8_t       board_merge_led_config(board_info_t* board, uint8_t iodir);
static uint8_t       board_merge_led_status(board_info_t* board, uint8_t data);
static void          board_master_init(void);
static void          board_slave_init(void);

//
// board interface
//
static void board_select_master_row(board_info_t* board, uint8_t row);
static void board_unselect_master_row(board_info_t* board, uint8_t row);
static void board_unselect_master_rows(board_info_t* board);
static bool board_read_cols_on_master_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row);
static void board_set_master_led(board_info_t* board, uint8_t led_index, bool status);
static void board_select_slave_row(board_info_t* board, uint8_t row);
static void board_unselect_slave_row(board_info_t* board, uint8_t row);
static void board_unselect_slave_rows(board_info_t* board);
static bool board_read_cols_on_slave_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row);
static void board_set_slave_led(board_info_t* board, uint8_t led_index, bool status);

static board_interface_t master_interface = {board_select_master_row, board_unselect_master_row, board_unselect_master_rows, board_read_cols_on_master_row, board_set_master_led};
static board_interface_t slave_interface  = {board_select_slave_row, board_unselect_slave_row, board_unselect_slave_rows, board_read_cols_on_slave_row, board_set_slave_led};

static board_interface_t* get_interface(board_info_t* board) {
    if (board_is_master(board)) {
        return &master_interface;
    }
    return &slave_interface;
}

static void board_set_master_led(board_info_t* board, uint8_t led_index, bool status) {
    pin_t pin                    = board->led_pins[led_index];
    board->led_status[led_index] = status;
    setPinOutput(pin);
    status ? writePinHigh(pin) : writePinLow(pin);
}

static void board_set_slave_led(board_info_t* board, uint8_t led_index, bool status) {
    board->led_status[led_index] = status;
    uint8_t iodir                = board_merge_led_config(board, 0xff);
    uint8_t data                 = board_merge_led_status(board, 0x00);
    i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
    i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_OLATB, (const uint8_t*)&data, sizeof(data), BOARD_I2C_TIMEOUT);
}

static uint8_t board_merge_led_config(board_info_t* board, uint8_t iodir) {
    for (uint8_t i = 0; i < NUM_LEDS; i++) {
        iodir &= PIN2MASK(board->led_pins[i]);
    }
    return iodir;
}

static bool board_slave_config(board_info_t* board) {
    uint8_t      set   = 0xff;
    uint8_t      clear = 0x00;
    i2c_status_t res   = 0;

    // Set to input
    res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRA, (const uint8_t*)&set, sizeof(set), BOARD_I2C_TIMEOUT);
    if (res < 0) return false;
    // RESTRICTION: LEDs only on PORT B.
    set = board_merge_led_config(board, set);
    res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&set, sizeof(set), BOARD_I2C_TIMEOUT);
    if (res < 0) return false;
    set = 0xff;

    // Pull up for input - enable
    res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPPUA, (const uint8_t*)&set, sizeof(set), BOARD_I2C_TIMEOUT);
    if (res < 0) return false;
    res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPPUB, (const uint8_t*)&set, sizeof(set), BOARD_I2C_TIMEOUT);
    if (res < 0) return false;

    // Disable interrupt
    res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPINTENA, (const uint8_t*)&clear, sizeof(clear), BOARD_I2C_TIMEOUT);
    if (res < 0) return false;
    res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPINTENB, (const uint8_t*)&clear, sizeof(clear), BOARD_I2C_TIMEOUT);
    if (res < 0) return false;

    // Polarity - same logic
    res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IPOLA, (const uint8_t*)&clear, sizeof(clear), BOARD_I2C_TIMEOUT);
    if (res < 0) return false;
    res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IPOLB, (const uint8_t*)&clear, sizeof(clear), BOARD_I2C_TIMEOUT);
    if (res < 0) return false;

    return true;
}

static void board_slave_init(void) {
    i2c_init();
    _delay_ms(500);

    for (uint8_t i = 0; i < NUM_BOARDS; i++) {
        board_info_t* board = &boards[i];
        if (board_is_master(board)) {
            continue;
        }
        if (i2c_start(EXPANDER_ADDR(board->i2c_address), BOARD_I2C_TIMEOUT) != I2C_STATUS_SUCCESS) {
            continue;
        }
        i2c_stop();
        if (board_slave_config(board)) {
            board->initialized = true;
        }
    }
}

inline bool board_is_master(board_info_t* board) {
    if (board) {
        return board->master;
    }
    return false;
}

inline uint8_t matrix2board(uint8_t row) { return row % NUM_ROWS; }

inline uint8_t board_index(uint8_t row) { return row / NUM_ROWS; }

static board_info_t* get_master_board(void) {
    if (master_board == NULL) {
        for (uint8_t i = 0; i < NUM_BOARDS; i++) {
            if (boards[i].master) {
                master_board = &boards[i];
                return master_board;
            }
        }
    }
    return NULL;
}

inline bool board_is_initialized(board_info_t* board) { return board == NULL ? false : board->initialized; }

static board_info_t* get_board_by_index(uint8_t board_index) {
    if (board_index >= 0 && board_index < NUM_BOARDS) {
        if (!board_is_initialized(&boards[board_index])) {
            return NULL;
        }
        return &boards[board_index];
    }
    return NULL;
}

static board_info_t* get_board(uint8_t row) {
    uint8_t idx = board_index(row);
    if (idx >= 0 && idx < NUM_BOARDS) {
        if (!board_is_initialized(&boards[idx])) {
            return NULL;
        }
        return &boards[idx];
    }
    return NULL;
}

static uint8_t board_merge_led_status(board_info_t* board, uint8_t data) {
    if (!board_is_initialized(board)) {
        return data;
    }
    for (uint8_t i = 0; i < NUM_LEDS; i++) {
        bool status = board->led_status[i];
        if (status) {
            data |= (uint8_t)1 << PIN2INDEX(board->led_pins[i]);
        } else {
            data &= PIN2MASK(board->led_pins[i]);
        }
    }
    return data;
}

//
// Functions for slave
//
static uint8_t board_read_slave_cols(board_info_t* board) {
    if (!board_is_initialized(board)) {
        return 0xff;
    }
    uint8_t      data = 0xff;
    i2c_status_t res  = i2c_readReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPIOA, &data, sizeof(data), BOARD_I2C_TIMEOUT);
    return (res < 0) ? 0xff : data;
}

static void board_select_slave_row(board_info_t* board, uint8_t board_row) {
    if (!board_is_initialized(board)) {
        return;
    }
    uint8_t pin    = board->row_pins[board_row];
    uint8_t iodir  = board_merge_led_config(board, PIN2MASK(pin));
    uint8_t status = board_merge_led_status(board, PIN2MASK(pin));
    i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
    i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_OLATB, (const uint8_t*)&status, sizeof(status), BOARD_I2C_TIMEOUT);
}

static void board_unselect_slave_rows(board_info_t* board) {
    if (!board_is_initialized(board)) {
        return;
    }
    uint8_t iodir = board_merge_led_config(board, 0xff);
    uint8_t data  = board_merge_led_status(board, 0x00);
    i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
    i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_OLATB, (const uint8_t*)&data, sizeof(data), BOARD_I2C_TIMEOUT);
}

static void board_unselect_slave_row(board_info_t* board, uint8_t board_row) { board_unselect_slave_rows(board); }

/*
 * row : matrix row (not board row)
 */
static bool board_read_cols_on_slave_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row) {
    matrix_row_t last_row_value = current_matrix[row];
    current_matrix[row]         = 0;

    uint8_t board_row = matrix2board(row);
    board_select_slave_row(board, board_row);
    wait_us(30);

    uint8_t cols = board_read_slave_cols(board);
    for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
        uint8_t pin       = board->col_pins[col_index];
        uint8_t pin_state = cols & PIN2BIT(pin);
        current_matrix[row] |= pin_state ? 0 : (1 << col_index);
    }
    board_unselect_slave_row(board, board_row);

    return (last_row_value != current_matrix[row]);
}

//
// Functions for master board
//
static void board_select_master_row(board_info_t* board, uint8_t board_row) {
    setPinOutput(board->row_pins[board_row]);
    writePinLow(board->row_pins[board_row]);
}

static void board_unselect_master_row(board_info_t* board, uint8_t board_row) { setPinInputHigh(board->row_pins[board_row]); }

static void board_unselect_master_rows(board_info_t* board) {
    if (!board) {
        return;
    }
    for (uint8_t x = 0; x < NUM_ROWS; x++) {
        setPinInput(board->row_pins[x]);
    }
}

/*
 * row : matrix row (not board row)
 */
static bool board_read_cols_on_master_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row) {
    matrix_row_t last_row_value = current_matrix[row];
    current_matrix[row]         = 0;

    uint8_t board_row = matrix2board(row);
    board_select_master_row(board, board_row);
    wait_us(30);

    for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
        uint8_t pin_state = readPin(board->col_pins[col_index]);
        current_matrix[row] |= pin_state ? 0 : (1 << col_index);
    }
    board_unselect_master_row(board, board_row);

    return (last_row_value != current_matrix[row]);
}

static void board_master_init(void) {
    board_info_t* board = get_master_board();
    if (!board) {
        return;
    }
    for (uint8_t x = 0; x < NUM_COLS; x++) {
        setPinInputHigh(board->col_pins[x]);
    }
    board->initialized = true;
}

static void board_setup(void) {
    for (uint8_t i = 0; i < NUM_BOARDS; i++) {
        board_info_t* board = &boards[i];
        board->interface    = get_interface(board);
    }
}

//
// Public functions
//

// NOTE: Do not call this while matrix scanning...
void board_set_led_by_index(uint8_t board_index, uint8_t led_index, bool status) {
    board_info_t* board = get_board_by_index(board_index);
    if (!board) return;
    if (led_index < 0 || led_index > NUM_LEDS) return;
    (*board->interface->set_led)(board, led_index, status);
}

bool board_read_cols_on_row(matrix_row_t current_matrix[], uint8_t row) {
    bool          result = false;
    board_info_t* board  = get_board(row);
    if (!board) {
        return false;
    }
    result = (*board->interface->read_cols_on_row)(board, current_matrix, row);
    return result;
}

void board_select_row(uint8_t row) {
    board_info_t* board = get_board(row);
    if (!board) {
        return;
    }
    uint8_t board_row = matrix2board(row);
    (*board->interface->select_row)(board, board_row);
}

void board_unselect_row(uint8_t row) {
    board_info_t* board = get_board(row);
    if (!board) {
        return;
    }
    uint8_t board_row = matrix2board(row);
    (*board->interface->unselect_row)(board, board_row);
}

void board_unselect_rows(void) {
    for (uint8_t i = 0; i < NUM_BOARDS; i++) {
        board_info_t* board = &boards[i];
        (*board->interface->unselect_rows)(board);
    }
}

void board_init(void) {
    board_setup();
    board_master_init();
    board_slave_init();
    board_unselect_rows();
}