|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Valentin Milea <[email protected]> |
| 3 | + * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +#include "pico/i2c_slave.h" |
| 9 | +#include "hardware/irq.h" |
| 10 | + |
| 11 | +typedef struct i2c_slave { |
| 12 | + i2c_slave_handler_t handler; |
| 13 | + bool transfer_in_progress; |
| 14 | +} i2c_slave_t; |
| 15 | + |
| 16 | +static i2c_slave_t i2c_slaves[2]; |
| 17 | + |
| 18 | +static inline i2c_inst_t *get_hw_instance(const i2c_slave_t *slave) { |
| 19 | + return i2c_get_instance(slave - i2c_slaves); |
| 20 | +} |
| 21 | + |
| 22 | +static void __isr __not_in_flash_func(i2c_slave_irq_handler)(void) { |
| 23 | + uint i2c_index = __get_current_exception() - VTABLE_FIRST_IRQ - I2C0_IRQ; |
| 24 | + i2c_slave_t *slave = &i2c_slaves[i2c_index]; |
| 25 | + i2c_inst_t *i2c = i2c_get_instance(i2c_index); |
| 26 | + i2c_hw_t *hw = i2c_get_hw(i2c); |
| 27 | + |
| 28 | + uint32_t intr_stat = hw->intr_stat; |
| 29 | + if (intr_stat == 0) { |
| 30 | + return; |
| 31 | + } |
| 32 | + bool do_finish_transfer = false; |
| 33 | + if (intr_stat & I2C_IC_INTR_STAT_R_TX_ABRT_BITS) { |
| 34 | + hw->clr_tx_abrt; |
| 35 | + do_finish_transfer = true; |
| 36 | + } |
| 37 | + if (intr_stat & I2C_IC_INTR_STAT_R_START_DET_BITS) { |
| 38 | + hw->clr_start_det; |
| 39 | + do_finish_transfer = true; |
| 40 | + } |
| 41 | + if (intr_stat & I2C_IC_INTR_STAT_R_STOP_DET_BITS) { |
| 42 | + hw->clr_stop_det; |
| 43 | + do_finish_transfer = true; |
| 44 | + } |
| 45 | + if (do_finish_transfer && slave->transfer_in_progress) { |
| 46 | + slave->handler(i2c, I2C_SLAVE_FINISH); |
| 47 | + slave->transfer_in_progress = false; |
| 48 | + } |
| 49 | + if (intr_stat & I2C_IC_INTR_STAT_R_RX_FULL_BITS) { |
| 50 | + slave->transfer_in_progress = true; |
| 51 | + slave->handler(i2c, I2C_SLAVE_RECEIVE); |
| 52 | + } |
| 53 | + if (intr_stat & I2C_IC_INTR_STAT_R_RD_REQ_BITS) { |
| 54 | + hw->clr_rd_req; |
| 55 | + slave->transfer_in_progress = true; |
| 56 | + slave->handler(i2c, I2C_SLAVE_REQUEST); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void i2c_slave_init(i2c_inst_t *i2c, uint8_t address, i2c_slave_handler_t handler) { |
| 61 | + assert(i2c == i2c0 || i2c == i2c1); |
| 62 | + assert(handler != NULL); |
| 63 | + |
| 64 | + uint i2c_index = i2c_hw_index(i2c); |
| 65 | + i2c_slave_t *slave = &i2c_slaves[i2c_index]; |
| 66 | + slave->handler = handler; |
| 67 | + |
| 68 | + // Note: The I2C slave does clock stretching implicitly after a RD_REQ, while the Tx FIFO is empty. |
| 69 | + // There is also an option to enable clock stretching while the Rx FIFO is full, but we leave it |
| 70 | + // disabled since the Rx FIFO should never fill up (unless slave->handler() is way too slow). |
| 71 | + i2c_set_slave_mode(i2c, true, address); |
| 72 | + |
| 73 | + i2c_hw_t *hw = i2c_get_hw(i2c); |
| 74 | + // unmask necessary interrupts |
| 75 | + hw->intr_mask = |
| 76 | + I2C_IC_INTR_MASK_M_RX_FULL_BITS | I2C_IC_INTR_MASK_M_RD_REQ_BITS | I2C_IC_RAW_INTR_STAT_TX_ABRT_BITS | |
| 77 | + I2C_IC_INTR_MASK_M_STOP_DET_BITS | I2C_IC_INTR_MASK_M_START_DET_BITS; |
| 78 | + |
| 79 | + // enable interrupt for current core |
| 80 | + uint num = I2C0_IRQ + i2c_index; |
| 81 | + irq_set_exclusive_handler(num, i2c_slave_irq_handler); |
| 82 | + irq_set_enabled(num, true); |
| 83 | +} |
| 84 | + |
| 85 | +void i2c_slave_deinit(i2c_inst_t *i2c) { |
| 86 | + assert(i2c == i2c0 || i2c == i2c1); |
| 87 | + |
| 88 | + uint i2c_index = i2c_hw_index(i2c); |
| 89 | + i2c_slave_t *slave = &i2c_slaves[i2c_index]; |
| 90 | + assert(slave->handler); // should be called after i2c_slave_init() |
| 91 | + |
| 92 | + slave->handler = NULL; |
| 93 | + slave->transfer_in_progress = false; |
| 94 | + |
| 95 | + uint num = I2C0_IRQ + i2c_index; |
| 96 | + irq_set_enabled(num, false); |
| 97 | + irq_remove_handler(num, i2c_slave_irq_handler); |
| 98 | + |
| 99 | + i2c_hw_t *hw = i2c_get_hw(i2c); |
| 100 | + hw->intr_mask = I2C_IC_INTR_MASK_RESET; |
| 101 | + |
| 102 | + i2c_set_slave_mode(i2c, false, 0); |
| 103 | +} |
0 commit comments