|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +// This file is never compiled standalone, it's included directly from |
| 28 | +// extmod/machine_i2c_target.c via MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE. |
| 29 | + |
| 30 | +#include "i2c.h" |
| 31 | + |
| 32 | +#define I2C_IC_CON_RX_FIFO_FULL_HLD_CTRL (1 << 9) |
| 33 | +#define I2C_IC_CON_TX_EMPTY_CTRL (1 << 8) |
| 34 | +#define I2C_IC_CON_STOP_DET_IFADDRESSED (1 << 7) |
| 35 | + |
| 36 | +typedef struct _machine_i2c_target_obj_t { |
| 37 | + mp_obj_base_t base; |
| 38 | + I2C_Type *i2c; |
| 39 | + mp_hal_pin_obj_t scl; |
| 40 | + mp_hal_pin_obj_t sda; |
| 41 | + uint8_t state; |
| 42 | + bool stop_pending; |
| 43 | + bool irq_active; |
| 44 | +} machine_i2c_target_obj_t; |
| 45 | + |
| 46 | +static machine_i2c_target_obj_t machine_i2c_target_obj[] = { |
| 47 | + #if defined(MICROPY_HW_I2C0_SCL) |
| 48 | + [0] = {{&machine_i2c_target_type}, (I2C_Type *)I2C0_BASE, MICROPY_HW_I2C0_SCL, MICROPY_HW_I2C0_SDA}, |
| 49 | + #endif |
| 50 | + #if defined(MICROPY_HW_I2C1_SCL) |
| 51 | + [1] = {{&machine_i2c_target_type}, (I2C_Type *)I2C1_BASE, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA}, |
| 52 | + #endif |
| 53 | + #if defined(MICROPY_HW_I2C2_SCL) |
| 54 | + [2] = {{&machine_i2c_target_type}, (I2C_Type *)I2C2_BASE, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA}, |
| 55 | + #endif |
| 56 | + #if defined(MICROPY_HW_I2C3_SCL) |
| 57 | + [3] = {{&machine_i2c_target_type}, (I2C_Type *)I2C3_BASE, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA}, |
| 58 | + #endif |
| 59 | +}; |
| 60 | + |
| 61 | +/******************************************************************************/ |
| 62 | +// Alif I2C hardware bindings |
| 63 | +// |
| 64 | +// The hardware triggers the following IRQs for the given scenarios: |
| 65 | +// - scan (0-byte write) of another target: START_DET |
| 66 | +// - scan (0-byte write) of us: START_DET STOP_DET |
| 67 | +// - write of n bytes: START_DET RX_FULL*n STOP_DET |
| 68 | +// - write of n bytes then read of m bytes: START_DET RX_FULL*n START_DET RD_REQ*m RX_DONE STOP_DET |
| 69 | + |
| 70 | +static inline unsigned int i2c_reg_base_to_index(I2C_Type *i2c) { |
| 71 | + return ((uintptr_t)i2c - I2C0_BASE) / (I2C1_BASE - I2C0_BASE); |
| 72 | +} |
| 73 | + |
| 74 | +static const uint32_t i2c_irq_num[] = { I2C0_IRQ_IRQn, I2C1_IRQ_IRQn, I2C2_IRQ_IRQn, I2C3_IRQ_IRQn }; |
| 75 | + |
| 76 | +static void check_stop_pending(machine_i2c_target_obj_t *self) { |
| 77 | + if (self->irq_active) { |
| 78 | + return; |
| 79 | + } |
| 80 | + if (self->stop_pending && !(self->i2c->I2C_STATUS & I2C_IC_STATUS_RECEIVE_FIFO_NOT_EMPTY)) { |
| 81 | + unsigned int i2c_id = self - &machine_i2c_target_obj[0]; |
| 82 | + machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id]; |
| 83 | + self->stop_pending = false; |
| 84 | + self->state = STATE_IDLE; |
| 85 | + machine_i2c_target_data_restart_or_stop(data); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +static void i2c_target_irq_handler(machine_i2c_target_obj_t *self) { |
| 90 | + unsigned int i2c_id = self - &machine_i2c_target_obj[0]; |
| 91 | + machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id]; |
| 92 | + I2C_Type *i2c = self->i2c; |
| 93 | + |
| 94 | + self->irq_active = true; |
| 95 | + |
| 96 | + // Get the interrupt status. |
| 97 | + uint32_t intr_stat = i2c->I2C_RAW_INTR_STAT; |
| 98 | + |
| 99 | + if (intr_stat & I2C_IC_INTR_STAT_TX_ABRT) { |
| 100 | + // Clear the TX_ABRT condition. |
| 101 | + (void)i2c->I2C_CLR_TX_ABRT; |
| 102 | + } |
| 103 | + |
| 104 | + if (intr_stat & I2C_IC_INTR_STAT_START_DET) { |
| 105 | + // Controller sent a start condition. |
| 106 | + // Reset all state machines in case something went wrong. |
| 107 | + (void)i2c->I2C_CLR_START_DET; |
| 108 | + if (self->state != STATE_IDLE) { |
| 109 | + machine_i2c_target_data_reset_helper(data); |
| 110 | + self->state = STATE_IDLE; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (intr_stat & I2C_IC_INTR_STAT_RX_FULL) { |
| 115 | + // Data from controller is available for reading. |
| 116 | + // Mask interrupt until I2C_DATA_CMD is read from. |
| 117 | + i2c->I2C_INTR_MASK &= ~I2C_IC_INTR_STAT_RX_FULL; |
| 118 | + if (self->state != STATE_WRITING) { |
| 119 | + machine_i2c_target_data_addr_match(data, false); |
| 120 | + } |
| 121 | + machine_i2c_target_data_write_request(self, data); |
| 122 | + self->state = STATE_WRITING; |
| 123 | + } |
| 124 | + |
| 125 | + if (intr_stat & (I2C_IC_INTR_STAT_RD_REQ | I2C_IC_INTR_STAT_RX_DONE)) { |
| 126 | + // Controller is requesting data. |
| 127 | + // Note: for RX_DONE interrupt, no data needs to be written but this event is |
| 128 | + // needed to match the expected I2CTarget event behaviour. A TX_ABTR interrupt |
| 129 | + // will be fired after the unused byte is written to I2C_DATA_CMD, and clearing |
| 130 | + // that abort is required to reset the hardware I2C state machine. |
| 131 | + (void)i2c->I2C_CLR_RX_DONE; |
| 132 | + (void)i2c->I2C_CLR_RD_REQ; |
| 133 | + i2c->I2C_INTR_MASK &= ~I2C_IC_INTR_STAT_RD_REQ; |
| 134 | + if (self->state != STATE_READING) { |
| 135 | + machine_i2c_target_data_addr_match(data, true); |
| 136 | + } |
| 137 | + machine_i2c_target_data_read_request(self, data); |
| 138 | + self->state = STATE_READING; |
| 139 | + } |
| 140 | + |
| 141 | + if (intr_stat & I2C_IC_INTR_STAT_STOP_DET) { |
| 142 | + // Controller has generated a stop condition. |
| 143 | + (void)i2c->I2C_CLR_STOP_DET; |
| 144 | + if (self->state == STATE_IDLE) { |
| 145 | + machine_i2c_target_data_addr_match(data, false); |
| 146 | + } |
| 147 | + if (i2c->I2C_STATUS & I2C_IC_STATUS_RECEIVE_FIFO_NOT_EMPTY) { |
| 148 | + self->stop_pending = true; |
| 149 | + } else { |
| 150 | + machine_i2c_target_data_restart_or_stop(data); |
| 151 | + self->state = STATE_IDLE; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + self->irq_active = false; |
| 156 | + check_stop_pending(self); |
| 157 | +} |
| 158 | + |
| 159 | +void I2C0_IRQHandler(void) { |
| 160 | + i2c_target_irq_handler(&machine_i2c_target_obj[0]); |
| 161 | +} |
| 162 | + |
| 163 | +void I2C1_IRQHandler(void) { |
| 164 | + i2c_target_irq_handler(&machine_i2c_target_obj[1]); |
| 165 | +} |
| 166 | + |
| 167 | +void I2C2_IRQHandler(void) { |
| 168 | + i2c_target_irq_handler(&machine_i2c_target_obj[2]); |
| 169 | +} |
| 170 | + |
| 171 | +void I2C3_IRQHandler(void) { |
| 172 | + i2c_target_irq_handler(&machine_i2c_target_obj[3]); |
| 173 | +} |
| 174 | + |
| 175 | +static void i2c_target_init(I2C_Type *i2c, uint16_t addr, bool addr_10bit) { |
| 176 | + i2c_disable(i2c); |
| 177 | + |
| 178 | + uint32_t ic_con_reg = 0; |
| 179 | + ic_con_reg |= I2C_IC_CON_RX_FIFO_FULL_HLD_CTRL; |
| 180 | + ic_con_reg |= I2C_IC_CON_STOP_DET_IFADDRESSED; |
| 181 | + if (addr_10bit) { |
| 182 | + ic_con_reg |= I2C_SLAVE_10BIT_ADDR_MODE; |
| 183 | + } |
| 184 | + i2c->I2C_CON = ic_con_reg; |
| 185 | + i2c->I2C_SAR = addr & I2C_IC_SAR_10BIT_ADDR_MASK; |
| 186 | + i2c->I2C_TX_TL = 1; |
| 187 | + i2c->I2C_RX_TL = 0; // interrupt when at least 1 byte is available |
| 188 | + i2c_clear_all_interrupt(i2c); |
| 189 | + |
| 190 | + // Enable interrupts. |
| 191 | + i2c->I2C_INTR_MASK = |
| 192 | + I2C_IC_INTR_STAT_STOP_DET |
| 193 | + | I2C_IC_INTR_STAT_RX_DONE |
| 194 | + | I2C_IC_INTR_STAT_TX_ABRT |
| 195 | + | I2C_IC_INTR_STAT_RD_REQ |
| 196 | + | I2C_IC_INTR_STAT_RX_FULL |
| 197 | + ; |
| 198 | + |
| 199 | + i2c_enable(i2c); |
| 200 | + |
| 201 | + // Enable I2C interrupts. |
| 202 | + uint32_t irq_num = i2c_irq_num[i2c_reg_base_to_index(i2c)]; |
| 203 | + NVIC_ClearPendingIRQ(irq_num); |
| 204 | + NVIC_SetPriority(irq_num, IRQ_PRI_I2C); |
| 205 | + NVIC_EnableIRQ(irq_num); |
| 206 | +} |
| 207 | + |
| 208 | +static void i2c_target_deinit(I2C_Type *i2c) { |
| 209 | + uint32_t irq_num = i2c_irq_num[i2c_reg_base_to_index(i2c)]; |
| 210 | + NVIC_DisableIRQ(irq_num); |
| 211 | + i2c_disable(i2c); |
| 212 | +} |
| 213 | + |
| 214 | +/******************************************************************************/ |
| 215 | +// I2CTarget port implementation |
| 216 | + |
| 217 | +static inline size_t mp_machine_i2c_target_get_index(machine_i2c_target_obj_t *self) { |
| 218 | + return self - &machine_i2c_target_obj[0]; |
| 219 | +} |
| 220 | + |
| 221 | +static inline void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) { |
| 222 | + mp_irq_handler(&irq->base); |
| 223 | +} |
| 224 | + |
| 225 | +static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) { |
| 226 | + I2C_Type *i2c = self->i2c; |
| 227 | + |
| 228 | + // Read from the RX FIFO. |
| 229 | + size_t i = 0; |
| 230 | + while (i < len && (i2c->I2C_STATUS & I2C_IC_STATUS_RECEIVE_FIFO_NOT_EMPTY)) { |
| 231 | + buf[i++] = i2c->I2C_DATA_CMD; |
| 232 | + } |
| 233 | + |
| 234 | + // Re-enable RX_FULL interrupt. |
| 235 | + i2c->I2C_INTR_MASK |= I2C_IC_INTR_STAT_RX_FULL; |
| 236 | + |
| 237 | + check_stop_pending(self); |
| 238 | + |
| 239 | + return i; |
| 240 | +} |
| 241 | + |
| 242 | +static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) { |
| 243 | + // Write to the TX FIFO. |
| 244 | + size_t i = 0; |
| 245 | + while (i < len && (self->i2c->I2C_STATUS & I2C_IC_STATUS_TRANSMIT_FIFO_NOT_FULL)) { |
| 246 | + self->i2c->I2C_DATA_CMD = buf[i++]; |
| 247 | + } |
| 248 | + |
| 249 | + // Re-enable RD_REQ interrupt. |
| 250 | + self->i2c->I2C_INTR_MASK |= I2C_IC_INTR_STAT_RD_REQ; |
| 251 | + |
| 252 | + return 1; |
| 253 | +} |
| 254 | + |
| 255 | +static inline void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) { |
| 256 | + (void)self; |
| 257 | + (void)trigger; |
| 258 | +} |
| 259 | + |
| 260 | +static mp_obj_t mp_machine_i2c_target_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 261 | + enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize, ARG_scl, ARG_sda }; |
| 262 | + static const mp_arg_t allowed_args[] = { |
| 263 | + { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 264 | + { MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT }, |
| 265 | + { MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} }, |
| 266 | + { MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, |
| 267 | + { MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, |
| 268 | + { MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, |
| 269 | + { MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, |
| 270 | + }; |
| 271 | + |
| 272 | + // Parse arguments. |
| 273 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 274 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 275 | + |
| 276 | + int i2c_id = args[ARG_id].u_int; |
| 277 | + |
| 278 | + // Check if the I2C bus is valid |
| 279 | + if (i2c_id < 0 || i2c_id >= MP_ARRAY_SIZE(machine_i2c_target_obj)) { |
| 280 | + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2CTarget(%d) doesn't exist"), i2c_id); |
| 281 | + } |
| 282 | + |
| 283 | + // Get static peripheral object. |
| 284 | + machine_i2c_target_obj_t *self = &machine_i2c_target_obj[i2c_id]; |
| 285 | + |
| 286 | + // Disable I2C controller. |
| 287 | + i2c_disable(self->i2c); |
| 288 | + |
| 289 | + // Initialise data. |
| 290 | + self->state = STATE_IDLE; |
| 291 | + self->stop_pending = false; |
| 292 | + self->irq_active = false; |
| 293 | + MP_STATE_PORT(machine_i2c_target_mem_obj)[i2c_id] = args[ARG_mem].u_obj; |
| 294 | + machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id]; |
| 295 | + machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int); |
| 296 | + |
| 297 | + // Set SCL/SDA pins if given. |
| 298 | + if (args[ARG_scl].u_obj != mp_const_none) { |
| 299 | + self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj); |
| 300 | + } |
| 301 | + if (args[ARG_sda].u_obj != mp_const_none) { |
| 302 | + self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj); |
| 303 | + } |
| 304 | + |
| 305 | + // Configure I2C pins. |
| 306 | + mp_hal_pin_config(self->scl, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, |
| 307 | + MP_HAL_PIN_SPEED_LOW, MP_HAL_PIN_DRIVE_8MA, MP_HAL_PIN_ALT(I2C_SCL, i2c_id), true); |
| 308 | + mp_hal_pin_config(self->sda, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, |
| 309 | + MP_HAL_PIN_SPEED_LOW, MP_HAL_PIN_DRIVE_8MA, MP_HAL_PIN_ALT(I2C_SDA, i2c_id), true); |
| 310 | + |
| 311 | + // Initialise the I2C target. |
| 312 | + i2c_target_init(self->i2c, args[ARG_addr].u_int, args[ARG_addrsize].u_int == 10); |
| 313 | + |
| 314 | + return MP_OBJ_FROM_PTR(self); |
| 315 | +} |
| 316 | + |
| 317 | +static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 318 | + machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 319 | + mp_printf(print, "I2CTarget(%u, addr=%u, scl=" MP_HAL_PIN_FMT ", sda=" MP_HAL_PIN_FMT ")", |
| 320 | + self - &machine_i2c_target_obj[0], self->i2c->I2C_SAR, mp_hal_pin_name(self->scl), mp_hal_pin_name(self->sda)); |
| 321 | +} |
| 322 | + |
| 323 | +static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) { |
| 324 | + i2c_target_deinit(self->i2c); |
| 325 | +} |
0 commit comments