mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-23 00:05:33 +03:00
Adding RP2350 SDK and target framework (#13988)
* Adding RP2350 SDK and target framework * Spacing * Removing board definitions
This commit is contained in:
parent
462cb05930
commit
2dd6f95aad
576 changed files with 435012 additions and 0 deletions
98
lib/main/pico-sdk/rp2_common/pico_i2c_slave/i2c_slave.c
Normal file
98
lib/main/pico-sdk/rp2_common/pico_i2c_slave/i2c_slave.c
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Valentin Milea <valentin.milea@gmail.com>
|
||||
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "pico/i2c_slave.h"
|
||||
#include "hardware/irq.h"
|
||||
|
||||
typedef struct i2c_slave {
|
||||
i2c_slave_handler_t handler;
|
||||
bool transfer_in_progress;
|
||||
} i2c_slave_t;
|
||||
|
||||
static i2c_slave_t i2c_slaves[2];
|
||||
|
||||
static void __isr __not_in_flash_func(i2c_slave_irq_handler)(void) {
|
||||
uint i2c_index = __get_current_exception() - VTABLE_FIRST_IRQ - I2C0_IRQ;
|
||||
i2c_slave_t *slave = &i2c_slaves[i2c_index];
|
||||
i2c_inst_t *i2c = i2c_get_instance(i2c_index);
|
||||
i2c_hw_t *hw = i2c_get_hw(i2c);
|
||||
|
||||
uint32_t intr_stat = hw->intr_stat;
|
||||
if (intr_stat == 0) {
|
||||
return;
|
||||
}
|
||||
bool do_finish_transfer = false;
|
||||
if (intr_stat & I2C_IC_INTR_STAT_R_TX_ABRT_BITS) {
|
||||
hw->clr_tx_abrt;
|
||||
do_finish_transfer = true;
|
||||
}
|
||||
if (intr_stat & I2C_IC_INTR_STAT_R_START_DET_BITS) {
|
||||
hw->clr_start_det;
|
||||
do_finish_transfer = true;
|
||||
}
|
||||
if (intr_stat & I2C_IC_INTR_STAT_R_STOP_DET_BITS) {
|
||||
hw->clr_stop_det;
|
||||
do_finish_transfer = true;
|
||||
}
|
||||
if (do_finish_transfer && slave->transfer_in_progress) {
|
||||
slave->handler(i2c, I2C_SLAVE_FINISH);
|
||||
slave->transfer_in_progress = false;
|
||||
}
|
||||
if (intr_stat & I2C_IC_INTR_STAT_R_RX_FULL_BITS) {
|
||||
slave->transfer_in_progress = true;
|
||||
slave->handler(i2c, I2C_SLAVE_RECEIVE);
|
||||
}
|
||||
if (intr_stat & I2C_IC_INTR_STAT_R_RD_REQ_BITS) {
|
||||
hw->clr_rd_req;
|
||||
slave->transfer_in_progress = true;
|
||||
slave->handler(i2c, I2C_SLAVE_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
void i2c_slave_init(i2c_inst_t *i2c, uint8_t address, i2c_slave_handler_t handler) {
|
||||
assert(i2c == i2c0 || i2c == i2c1);
|
||||
assert(handler != NULL);
|
||||
|
||||
uint i2c_index = i2c_hw_index(i2c);
|
||||
i2c_slave_t *slave = &i2c_slaves[i2c_index];
|
||||
slave->handler = handler;
|
||||
|
||||
// Note: The I2C slave does clock stretching implicitly after a RD_REQ, while the Tx FIFO is empty.
|
||||
// Clock stretching while the Rx FIFO is full is also enabled by default.
|
||||
i2c_set_slave_mode(i2c, true, address);
|
||||
|
||||
i2c_hw_t *hw = i2c_get_hw(i2c);
|
||||
// unmask necessary interrupts
|
||||
hw->intr_mask =
|
||||
I2C_IC_INTR_MASK_M_RX_FULL_BITS | I2C_IC_INTR_MASK_M_RD_REQ_BITS | I2C_IC_INTR_MASK_M_TX_ABRT_BITS |
|
||||
I2C_IC_INTR_MASK_M_STOP_DET_BITS | I2C_IC_INTR_MASK_M_START_DET_BITS;
|
||||
|
||||
// enable interrupt for current core
|
||||
uint num = I2C0_IRQ + i2c_index;
|
||||
irq_set_exclusive_handler(num, i2c_slave_irq_handler);
|
||||
irq_set_enabled(num, true);
|
||||
}
|
||||
|
||||
void i2c_slave_deinit(i2c_inst_t *i2c) {
|
||||
assert(i2c == i2c0 || i2c == i2c1);
|
||||
|
||||
uint i2c_index = i2c_hw_index(i2c);
|
||||
i2c_slave_t *slave = &i2c_slaves[i2c_index];
|
||||
assert(slave->handler); // should be called after i2c_slave_init()
|
||||
|
||||
slave->handler = NULL;
|
||||
slave->transfer_in_progress = false;
|
||||
|
||||
uint num = I2C0_IRQ + i2c_index;
|
||||
irq_set_enabled(num, false);
|
||||
irq_remove_handler(num, i2c_slave_irq_handler);
|
||||
|
||||
i2c_hw_t *hw = i2c_get_hw(i2c);
|
||||
hw->intr_mask = I2C_IC_INTR_MASK_RESET;
|
||||
|
||||
i2c_set_slave_mode(i2c, false, 0);
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Valentin Milea <valentin.milea@gmail.com>
|
||||
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _PICO_I2C_SLAVE_H
|
||||
#define _PICO_I2C_SLAVE_H
|
||||
|
||||
#include "hardware/i2c.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \file pico/i2c_slave.h
|
||||
* \defgroup pico_i2c_slave pico_i2c_slave
|
||||
*
|
||||
* \brief Functions providing an interrupt driven I2C slave interface
|
||||
*
|
||||
* This I2C slave helper library configures slave mode and hooks the relevant I2C IRQ
|
||||
* so that a user supplied handler is called with enumerated I2C events.
|
||||
*
|
||||
* An example application \c slave_mem_i2c, which makes use of this library, can be found in
|
||||
* <a href="https://github.com/raspberrypi/pico-examples/blob/master/i2c/slave_mem_i2c/slave_mem_i2c.c">pico_examples</a>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief I2C slave event types.
|
||||
* \ingroup pico_i2c_slave
|
||||
*/
|
||||
typedef enum i2c_slave_event_t
|
||||
{
|
||||
I2C_SLAVE_RECEIVE, ///< Data from master is available for reading. Slave must read from Rx FIFO.
|
||||
I2C_SLAVE_REQUEST, ///< Master is requesting data. Slave must write into Tx FIFO.
|
||||
I2C_SLAVE_FINISH, ///< Master has sent a Stop or Restart signal. Slave may prepare for the next transfer.
|
||||
} i2c_slave_event_t;
|
||||
|
||||
/**
|
||||
* \brief I2C slave event handler
|
||||
* \ingroup pico_i2c_slave
|
||||
*
|
||||
* The event handler will run from the I2C ISR, so it should return quickly (under 25 us at 400 kb/s).
|
||||
* Avoid blocking inside the handler and split large data transfers across multiple calls for best results.
|
||||
* When sending data to master, up to \ref i2c_get_write_available() bytes can be written without blocking.
|
||||
* When receiving data from master, up to \ref i2c_get_read_available() bytes can be read without blocking.
|
||||
*
|
||||
* \param i2c Either \ref i2c0 or \ref i2c1
|
||||
* \param event Event type.
|
||||
*/
|
||||
typedef void (*i2c_slave_handler_t)(i2c_inst_t *i2c, i2c_slave_event_t event);
|
||||
|
||||
/**
|
||||
* \brief Configure an I2C instance for slave mode.
|
||||
* \ingroup pico_i2c_slave
|
||||
* \param i2c I2C instance.
|
||||
* \param address 7-bit slave address.
|
||||
* \param handler Callback for events from I2C master. It will run from the I2C ISR, on the CPU core
|
||||
* where the slave was initialised.
|
||||
*/
|
||||
void i2c_slave_init(i2c_inst_t *i2c, uint8_t address, i2c_slave_handler_t handler);
|
||||
|
||||
/**
|
||||
* \brief Restore an I2C instance to master mode.
|
||||
* \ingroup pico_i2c_slave
|
||||
* \param i2c Either \ref i2c0 or \ref i2c1
|
||||
*/
|
||||
void i2c_slave_deinit(i2c_inst_t *i2c);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _PICO_I2C_SLAVE_H_
|
Loading…
Add table
Add a link
Reference in a new issue