|
| 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 | +#include "i2cslave.h" |
| 32 | +#include "irq.h" |
| 33 | + |
| 34 | +typedef struct _machine_i2c_target_obj_t { |
| 35 | + mp_obj_base_t base; |
| 36 | + I2C_TypeDef *i2c; |
| 37 | + uint16_t irqn_ev; |
| 38 | + uint16_t irqn_er; |
| 39 | + mp_hal_pin_obj_t scl; |
| 40 | + mp_hal_pin_obj_t sda; |
| 41 | +} machine_i2c_target_obj_t; |
| 42 | + |
| 43 | +static const machine_i2c_target_obj_t machine_i2c_target_obj[] = { |
| 44 | + #if defined(MICROPY_HW_I2C1_SCL) |
| 45 | + {{&machine_i2c_target_type}, I2C1, I2C1_EV_IRQn, I2C1_ER_IRQn, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA}, |
| 46 | + #else |
| 47 | + {{NULL}, NULL, 0, 0, NULL, NULL}, |
| 48 | + #endif |
| 49 | + #if defined(MICROPY_HW_I2C2_SCL) |
| 50 | + {{&machine_i2c_target_type}, I2C2, I2C2_EV_IRQn, I2C2_ER_IRQn, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA}, |
| 51 | + #else |
| 52 | + {{NULL}, NULL, 0, 0, NULL, NULL}, |
| 53 | + #endif |
| 54 | + #if defined(MICROPY_HW_I2C3_SCL) |
| 55 | + {{&machine_i2c_target_type}, I2C3, I2C3_EV_IRQn, I2C3_ER_IRQn, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA}, |
| 56 | + #else |
| 57 | + {{NULL}, NULL, 0, 0, NULL, NULL}, |
| 58 | + #endif |
| 59 | + #if defined(MICROPY_HW_I2C4_SCL) |
| 60 | + {{&machine_i2c_target_type}, I2C4, I2C4_EV_IRQn, I2C4_ER_IRQn, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA}, |
| 61 | + #else |
| 62 | + {{NULL}, NULL, 0, 0, NULL, NULL}, |
| 63 | + #endif |
| 64 | +}; |
| 65 | + |
| 66 | +/******************************************************************************/ |
| 67 | +// stm32 hardware bindings |
| 68 | + |
| 69 | +static machine_i2c_target_obj_t *get_self(i2c_slave_t *i2c) { |
| 70 | + size_t i2c_id = ((uintptr_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE); |
| 71 | + return (machine_i2c_target_obj_t *)&machine_i2c_target_obj[i2c_id]; |
| 72 | +} |
| 73 | + |
| 74 | +static machine_i2c_target_data_t *get_data(i2c_slave_t *i2c) { |
| 75 | + size_t i2c_id = ((uintptr_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE); |
| 76 | + return &machine_i2c_target_data[i2c_id]; |
| 77 | +} |
| 78 | + |
| 79 | +int i2c_slave_process_addr_match(i2c_slave_t *i2c, int rw) { |
| 80 | + machine_i2c_target_data_addr_match(get_data(i2c), rw); |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +int i2c_slave_process_rx_byte(i2c_slave_t *i2c) { |
| 85 | + machine_i2c_target_data_write_request(get_self(i2c), get_data(i2c)); |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +void i2c_slave_process_rx_end(i2c_slave_t *i2c) { |
| 90 | + machine_i2c_target_data_stop(get_data(i2c)); |
| 91 | +} |
| 92 | + |
| 93 | +void i2c_slave_process_tx_byte(i2c_slave_t *i2c) { |
| 94 | + machine_i2c_target_data_read_request(get_self(i2c), get_data(i2c)); |
| 95 | +} |
| 96 | + |
| 97 | +void i2c_slave_process_tx_end(i2c_slave_t *i2c) { |
| 98 | + machine_i2c_target_data_stop(get_data(i2c)); |
| 99 | +} |
| 100 | + |
| 101 | +/******************************************************************************/ |
| 102 | +// I2CTarget port implementation |
| 103 | + |
| 104 | +static inline size_t mp_machine_i2c_target_get_index(machine_i2c_target_obj_t *self) { |
| 105 | + return self - &machine_i2c_target_obj[0]; |
| 106 | +} |
| 107 | + |
| 108 | +static inline void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) { |
| 109 | + mp_irq_handler(&irq->base); |
| 110 | +} |
| 111 | + |
| 112 | +static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) { |
| 113 | + if (len > 0) { |
| 114 | + buf[0] = i2c_slave_read_byte(self->i2c); |
| 115 | + len = 1; |
| 116 | + } |
| 117 | + return len; |
| 118 | +} |
| 119 | + |
| 120 | +static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) { |
| 121 | + if (len > 0) { |
| 122 | + i2c_slave_write_byte(self->i2c, buf[0]); |
| 123 | + len = 1; |
| 124 | + } |
| 125 | + return len; |
| 126 | +} |
| 127 | + |
| 128 | +static inline void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) { |
| 129 | + (void)self; |
| 130 | + (void)trigger; |
| 131 | +} |
| 132 | + |
| 133 | +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) { |
| 134 | + // Parse arguments. |
| 135 | + enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize }; |
| 136 | + static const mp_arg_t allowed_args[] = { |
| 137 | + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 138 | + { MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT }, |
| 139 | + { MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} }, |
| 140 | + { MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, |
| 141 | + { MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, |
| 142 | + }; |
| 143 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 144 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 145 | + |
| 146 | + // Work out I2C bus. |
| 147 | + int i2c_id = i2c_find_peripheral(args[ARG_id].u_obj); |
| 148 | + |
| 149 | + // Get static target object. |
| 150 | + machine_i2c_target_obj_t *self = (machine_i2c_target_obj_t *)&machine_i2c_target_obj[i2c_id - 1]; |
| 151 | + |
| 152 | + // Initialise data. |
| 153 | + MP_STATE_PORT(machine_i2c_target_mem_obj)[i2c_id - 1] = args[ARG_mem].u_obj; |
| 154 | + machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id - 1]; |
| 155 | + machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int); |
| 156 | + |
| 157 | + // Initialise the I2C target. |
| 158 | + mp_hal_pin_config_alt(self->scl, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, AF_FN_I2C, i2c_id); |
| 159 | + mp_hal_pin_config_alt(self->sda, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, AF_FN_I2C, i2c_id); |
| 160 | + i2c_slave_init(self->i2c, self->irqn_ev, IRQ_PRI_I2C, args[ARG_addr].u_int); |
| 161 | + NVIC_SetPriority(self->irqn_er, IRQ_PRI_I2C); |
| 162 | + NVIC_EnableIRQ(self->irqn_er); |
| 163 | + |
| 164 | + i2c_target_enabled |= 1 << (i2c_id - 1); |
| 165 | + |
| 166 | + return MP_OBJ_FROM_PTR(self); |
| 167 | +} |
| 168 | + |
| 169 | +static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 170 | + machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 171 | + mp_printf(print, "I2CTarget(%u, addr=%u)", |
| 172 | + self - &machine_i2c_target_obj[0] + 1, |
| 173 | + (self->i2c->OAR1 >> 1) & 0x7f); |
| 174 | +} |
| 175 | + |
| 176 | +static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) { |
| 177 | + i2c_slave_shutdown(self->i2c, self->irqn_ev); |
| 178 | + NVIC_DisableIRQ(self->irqn_er); |
| 179 | +} |
0 commit comments