|
| 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 | + * Copyright (c) 2025 Robert Hammelrath |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +// This file is never compiled standalone, it's included directly from |
| 29 | +// extmod/machine_i2c_target.c via MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE. |
| 30 | + |
| 31 | +#include "fsl_lpi2c.h" |
| 32 | +#include "machine_i2c.h" |
| 33 | +#include CLOCK_CONFIG_H |
| 34 | + |
| 35 | +typedef struct _machine_i2c_target_obj_t { |
| 36 | + mp_obj_base_t base; |
| 37 | + LPI2C_Type *i2c_inst; |
| 38 | + uint8_t i2c_id; |
| 39 | + uint8_t addr; |
| 40 | + lpi2c_slave_config_t slave_config; |
| 41 | + lpi2c_slave_handle_t handle; |
| 42 | +} machine_i2c_target_obj_t; |
| 43 | + |
| 44 | +static void lpi2c_slave_callback(LPI2C_Type *base, lpi2c_slave_transfer_t *xfer, void *param) { |
| 45 | + machine_i2c_target_obj_t *self = (machine_i2c_target_obj_t *)param; |
| 46 | + machine_i2c_target_data_t *data = &machine_i2c_target_data[self->i2c_id]; |
| 47 | + |
| 48 | + switch (xfer->event) { |
| 49 | + case kLPI2C_SlaveAddressMatchEvent: |
| 50 | + // Controller addressed us. |
| 51 | + machine_i2c_target_data_addr_match(data, xfer->receivedAddress & 1); |
| 52 | + break; |
| 53 | + case kLPI2C_SlaveReceiveEvent: |
| 54 | + // Data from controller is available for reading. |
| 55 | + machine_i2c_target_data_write_request(self, data); |
| 56 | + break; |
| 57 | + case kLPI2C_SlaveTransmitEvent: |
| 58 | + // Controller is requesting data. |
| 59 | + machine_i2c_target_data_read_request(self, data); |
| 60 | + break; |
| 61 | + case kLPI2C_SlaveCompletionEvent: |
| 62 | + // Transfer done. |
| 63 | + machine_i2c_target_data_stop(data); |
| 64 | + break; |
| 65 | + default: |
| 66 | + break; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/******************************************************************************/ |
| 71 | +// I2CTarget port implementation |
| 72 | + |
| 73 | +static inline size_t mp_machine_i2c_target_get_index(machine_i2c_target_obj_t *self) { |
| 74 | + return self->i2c_id; |
| 75 | +} |
| 76 | + |
| 77 | +static inline void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) { |
| 78 | + mp_irq_handler(&irq->base); |
| 79 | +} |
| 80 | + |
| 81 | +static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) { |
| 82 | + // LPI2C_Type *i2c_inst = self->i2c_inst; |
| 83 | + // mp_int_t i = 0; |
| 84 | + // mp_int_t val = 0; |
| 85 | + // while (i < len && !((val = i2c_inst->SRDR) & LPI2C_SRDR_RXEMPTY_MASK)) { |
| 86 | + // buf[i++] = (uint8_t)(val & 0xff); |
| 87 | + // } |
| 88 | + // return i; |
| 89 | + // Simple and fast version for len == 1 |
| 90 | + buf[0] = (uint8_t)(self->i2c_inst->SRDR); |
| 91 | + return 1; |
| 92 | +} |
| 93 | + |
| 94 | +static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) { |
| 95 | + self->i2c_inst->STDR = buf[0]; |
| 96 | + return 1; |
| 97 | +} |
| 98 | + |
| 99 | +static inline void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) { |
| 100 | + (void)self; |
| 101 | + (void)trigger; |
| 102 | +} |
| 103 | + |
| 104 | +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) { |
| 105 | + enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize, ARG_drive }; |
| 106 | + static const mp_arg_t allowed_args[] = { |
| 107 | + { MP_QSTR_id, MP_ARG_INT, {.u_int = DEFAULT_I2C_ID} }, |
| 108 | + { MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT }, |
| 109 | + { MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} }, |
| 110 | + { MP_QSTR_mem, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, |
| 111 | + { MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, |
| 112 | + { MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_I2C_DRIVE} }, |
| 113 | + }; |
| 114 | + |
| 115 | + // Parse arguments. |
| 116 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 117 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 118 | + |
| 119 | + // Get I2C bus. |
| 120 | + int i2c_id = args[ARG_id].u_int; |
| 121 | + if (i2c_id < 0 || i2c_id >= micropy_hw_i2c_num || i2c_index_table[i2c_id] == 0) { |
| 122 | + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id); |
| 123 | + } |
| 124 | + int i2c_hw_id = i2c_index_table[i2c_id]; // the hw i2c number 1..n |
| 125 | + |
| 126 | + // Get I2C Object. |
| 127 | + machine_i2c_target_obj_t *self = mp_obj_malloc_with_finaliser(machine_i2c_target_obj_t, &machine_i2c_target_type); |
| 128 | + self->i2c_id = i2c_id; |
| 129 | + self->i2c_inst = i2c_base_ptr_table[i2c_hw_id]; |
| 130 | + uint8_t drive = args[ARG_drive].u_int; |
| 131 | + if (drive < 1 || drive > 7) { |
| 132 | + drive = DEFAULT_I2C_DRIVE; |
| 133 | + } |
| 134 | + // Set the target address. |
| 135 | + self->addr = args[ARG_addr].u_int; |
| 136 | + // Initialise data. |
| 137 | + MP_STATE_PORT(machine_i2c_target_mem_obj)[i2c_id] = args[ARG_mem].u_obj; |
| 138 | + machine_i2c_target_data_t *data = &machine_i2c_target_data[self->i2c_id]; |
| 139 | + machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int); |
| 140 | + |
| 141 | + // Initialise the GPIO pins |
| 142 | + lpi2c_set_iomux(i2c_hw_id, drive); |
| 143 | + // Initialise the I2C peripheral |
| 144 | + LPI2C_SlaveGetDefaultConfig(&self->slave_config); |
| 145 | + self->slave_config.address0 = self->addr; |
| 146 | + self->slave_config.sdaGlitchFilterWidth_ns = DEFAULT_I2C_FILTER_NS; |
| 147 | + self->slave_config.sclGlitchFilterWidth_ns = DEFAULT_I2C_FILTER_NS; |
| 148 | + LPI2C_SlaveInit(self->i2c_inst, &self->slave_config, BOARD_BOOTCLOCKRUN_LPI2C_CLK_ROOT); |
| 149 | + // Create the LPI2C handle for the non-blocking transfer |
| 150 | + LPI2C_SlaveTransferCreateHandle(self->i2c_inst, &self->handle, lpi2c_slave_callback, self); |
| 151 | + // Start accepting I2C transfers on the LPI2C slave peripheral |
| 152 | + status_t reVal = LPI2C_SlaveTransferNonBlocking(self->i2c_inst, &self->handle, |
| 153 | + kLPI2C_SlaveAddressMatchEvent | kLPI2C_SlaveTransmitEvent | kLPI2C_SlaveReceiveEvent | kLPI2C_SlaveCompletionEvent); |
| 154 | + if (reVal != kStatus_Success) { |
| 155 | + mp_raise_ValueError(MP_ERROR_TEXT("cannot start I2C")); |
| 156 | + } |
| 157 | + return MP_OBJ_FROM_PTR(self); |
| 158 | +} |
| 159 | + |
| 160 | +static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 161 | + machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 162 | + mp_printf(print, "I2CTarget(%u, addr=%u)", self->i2c_id, self->addr); |
| 163 | +} |
| 164 | + |
| 165 | +// Stop the Slave transfer and free the memory objects. |
| 166 | +static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) { |
| 167 | + LPI2C_SlaveDeinit(self->i2c_inst); |
| 168 | +} |
0 commit comments