transport.c (5011B)
1 /* Copyright 2021 QMK 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include <string.h> 18 #include <debug.h> 19 20 #include "compiler_support.h" 21 #include "transactions.h" 22 #include "transport.h" 23 #include "transaction_id_define.h" 24 #include "atomic_util.h" 25 26 #ifdef USE_I2C 27 28 # ifndef SLAVE_I2C_TIMEOUT 29 # define SLAVE_I2C_TIMEOUT 100 30 # endif // SLAVE_I2C_TIMEOUT 31 32 # ifndef SLAVE_I2C_ADDRESS 33 # define SLAVE_I2C_ADDRESS 0x32 34 # endif 35 36 # include "i2c_master.h" 37 # include "i2c_slave.h" 38 39 // Ensure the I2C buffer has enough space 40 STATIC_ASSERT(sizeof(split_shared_memory_t) <= I2C_SLAVE_REG_COUNT, "split_shared_memory_t too large for I2C_SLAVE_REG_COUNT"); 41 42 split_shared_memory_t *const split_shmem = (split_shared_memory_t *)i2c_slave_reg; 43 44 void transport_master_init(void) { 45 i2c_init(); 46 } 47 void transport_slave_init(void) { 48 i2c_slave_init(SLAVE_I2C_ADDRESS); 49 } 50 51 i2c_status_t transport_trigger_callback(int8_t id) { 52 // If there's no callback, indicate that we were successful 53 if (!split_transaction_table[id].slave_callback) { 54 return I2C_STATUS_SUCCESS; 55 } 56 57 // Kick off the "callback executor", now that data has been written to the slave 58 split_shmem->transaction_id = id; 59 split_transaction_desc_t *trans = &split_transaction_table[I2C_EXECUTE_CALLBACK]; 60 return i2c_write_register(SLAVE_I2C_ADDRESS, trans->initiator2target_offset, split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size, SLAVE_I2C_TIMEOUT); 61 } 62 63 bool transport_execute_transaction(int8_t id, const void *initiator2target_buf, uint16_t initiator2target_length, void *target2initiator_buf, uint16_t target2initiator_length) { 64 i2c_status_t status; 65 split_transaction_desc_t *trans = &split_transaction_table[id]; 66 if (initiator2target_length > 0) { 67 size_t len = trans->initiator2target_buffer_size < initiator2target_length ? trans->initiator2target_buffer_size : initiator2target_length; 68 memcpy(split_trans_initiator2target_buffer(trans), initiator2target_buf, len); 69 if ((status = i2c_write_register(SLAVE_I2C_ADDRESS, trans->initiator2target_offset, split_trans_initiator2target_buffer(trans), len, SLAVE_I2C_TIMEOUT)) < 0) { 70 return false; 71 } 72 } 73 74 // If we need to execute a callback on the slave, do so 75 if ((status = transport_trigger_callback(id)) < 0) { 76 return false; 77 } 78 79 if (target2initiator_length > 0) { 80 size_t len = trans->target2initiator_buffer_size < target2initiator_length ? trans->target2initiator_buffer_size : target2initiator_length; 81 if ((status = i2c_read_register(SLAVE_I2C_ADDRESS, trans->target2initiator_offset, split_trans_target2initiator_buffer(trans), len, SLAVE_I2C_TIMEOUT)) < 0) { 82 return false; 83 } 84 memcpy(target2initiator_buf, split_trans_target2initiator_buffer(trans), len); 85 } 86 87 return true; 88 } 89 90 #else // USE_I2C 91 92 # include "serial.h" 93 94 static split_shared_memory_t shared_memory; 95 split_shared_memory_t *const split_shmem = &shared_memory; 96 97 void transport_master_init(void) { 98 soft_serial_initiator_init(); 99 } 100 void transport_slave_init(void) { 101 soft_serial_target_init(); 102 } 103 104 bool transport_execute_transaction(int8_t id, const void *initiator2target_buf, uint16_t initiator2target_length, void *target2initiator_buf, uint16_t target2initiator_length) { 105 split_transaction_desc_t *trans = &split_transaction_table[id]; 106 if (initiator2target_length > 0) { 107 size_t len = trans->initiator2target_buffer_size < initiator2target_length ? trans->initiator2target_buffer_size : initiator2target_length; 108 memcpy(split_trans_initiator2target_buffer(trans), initiator2target_buf, len); 109 } 110 111 if (!soft_serial_transaction(id)) { 112 return false; 113 } 114 115 if (target2initiator_length > 0) { 116 size_t len = trans->target2initiator_buffer_size < target2initiator_length ? trans->target2initiator_buffer_size : target2initiator_length; 117 memcpy(target2initiator_buf, split_trans_target2initiator_buffer(trans), len); 118 } 119 120 return true; 121 } 122 123 #endif // USE_I2C 124 125 bool transport_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { 126 return transactions_master(master_matrix, slave_matrix); 127 } 128 129 void transport_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { 130 transactions_slave(master_matrix, slave_matrix); 131 }