Skip to content

Commit

Permalink
Fixed some I2C (Wire) examples
Browse files Browse the repository at this point in the history
  • Loading branch information
stevstrong committed Feb 29, 2024
1 parent 8143ad6 commit b358491
Show file tree
Hide file tree
Showing 28 changed files with 228 additions and 227 deletions.
2 changes: 1 addition & 1 deletion STM32F1/cores/maple/boards_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

/* Makes the PIN_MAP rows more human-readable. */
#define PMAP_ROW(gpio_dev, gpio_bit, timer_dev, timer_ch, adc_dev, adc_ch) \
{ gpio_dev, timer_dev, adc_dev, gpio_bit, timer_ch, adc_ch }
{ gpio_dev_t, timer_dev, adc_dev, gpio_bit, timer_ch, adc_ch }

namespace wirish {
namespace priv {
Expand Down
4 changes: 2 additions & 2 deletions STM32F1/cores/maple/libmaple/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ extern void adc_set_prescaler(adc_prescaler pre);
*/
extern void adc_foreach(void (*fn)(adc_dev*));

struct gpio_dev;
struct gpio_dev_t;
/**
* @brief Configure a GPIO pin for ADC conversion.
* @param dev ADC device to use for conversion (currently ignored on
Expand All @@ -289,7 +289,7 @@ struct gpio_dev;
* @param bit Bit on gdev to configure for ADC conversion.
*/
extern void adc_config_gpio(struct adc_dev *dev,
struct gpio_dev *gdev,
struct gpio_dev_t *gdev,
uint8 bit);

/**
Expand Down
2 changes: 1 addition & 1 deletion STM32F1/cores/maple/libmaple/adc_f1.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void adc_foreach(void (*fn)(adc_dev*)) {
#endif
}

void adc_config_gpio(adc_dev *ignored __attribute__((unused)), gpio_dev *gdev, uint8 bit) {
void adc_config_gpio(adc_dev *ignored __attribute__((unused)), gpio_dev_t *gdev, uint8 bit) {
gpio_set_mode(gdev, bit, GPIO_INPUT_ANALOG);
}

Expand Down
20 changes: 10 additions & 10 deletions STM32F1/cores/maple/libmaple/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,58 +37,58 @@
*/

/** GPIO port A device. */
const gpio_dev gpioa = {
const gpio_dev_t gpioa = {
.regs = GPIOA_BASE,
.clk_id = RCC_GPIOA,
.exti_port = EXTI_PA,
};

/** GPIO port B device. */
const gpio_dev gpiob = {
const gpio_dev_t gpiob = {
.regs = GPIOB_BASE,
.clk_id = RCC_GPIOB,
.exti_port = EXTI_PB,
};

/** GPIO port C device. */
const gpio_dev gpioc = {
const gpio_dev_t gpioc = {
.regs = GPIOC_BASE,
.clk_id = RCC_GPIOC,
.exti_port = EXTI_PC,
};

#if STM32_NR_GPIO_PORTS > 3
/** GPIO port D device. */
const gpio_dev gpiod = {
const gpio_dev_t gpiod = {
.regs = GPIOD_BASE,
.clk_id = RCC_GPIOD,
.exti_port = EXTI_PD,
};
#endif
#if STM32_NR_GPIO_PORTS > 4
/** GPIO port E device. */
const gpio_dev gpioe = {
const gpio_dev_t gpioe = {
.regs = GPIOE_BASE,
.clk_id = RCC_GPIOE,
.exti_port = EXTI_PE,
};

/** GPIO port F device. */
const gpio_dev gpiof = {
const gpio_dev_t gpiof = {
.regs = GPIOF_BASE,
.clk_id = RCC_GPIOF,
.exti_port = EXTI_PF,
};

/** GPIO port G device. */
const gpio_dev gpiog = {
const gpio_dev_t gpiog = {
.regs = GPIOG_BASE,
.clk_id = RCC_GPIOG,
.exti_port = EXTI_PG,
};
#endif

const gpio_dev* const gpio_devs[] = {
const gpio_dev_t* const gpio_devs[] = {
GPIOA,
GPIOB,
GPIOC,
Expand Down Expand Up @@ -128,7 +128,7 @@ void gpio_init_all(void) {
* @param mode General purpose or alternate function mode to set the pin to.
* @see gpio_pin_mode
*/
void gpio_set_mode(const gpio_dev *dev, uint8 bit, gpio_pin_mode mode) {
void gpio_set_mode(const gpio_dev_t *dev, uint8 bit, gpio_pin_mode mode) {
gpio_reg_map *regs = dev->regs;
__IO uint32 *cr = &regs->CRL + (bit >> 3);
uint32 shift = (bit & 0x7) * 4;
Expand All @@ -145,7 +145,7 @@ void gpio_set_mode(const gpio_dev *dev, uint8 bit, gpio_pin_mode mode) {
}
}
//-----------------------------------------------------------------------------
gpio_pin_mode gpio_get_mode(const gpio_dev *dev, uint8 pin) {
gpio_pin_mode gpio_get_mode(const gpio_dev_t *dev, uint8 pin) {
gpio_reg_map *regs = dev->regs;
__IO uint32 *cr = &regs->CRL + (pin >> 3);
uint32 shift = (pin & 0x7) * 4;
Expand Down
34 changes: 17 additions & 17 deletions STM32F1/cores/maple/libmaple/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,52 +52,52 @@ extern "C"{
*/

/** GPIO device type */
typedef struct gpio_dev {
typedef struct gpio_dev_t {
gpio_reg_map *regs; /**< Register map */
rcc_clk_id clk_id; /**< RCC clock information */
/**
* @brief (Deprecated) External interrupt port.
* Instead of dev->exti_port, use gpio_exti_port(dev).
*/
exti_cfg exti_port;
} gpio_dev;
} gpio_dev_t;

extern const gpio_dev gpioa;
extern const gpio_dev_t gpioa;
#define GPIOA (&gpioa)
extern const gpio_dev gpiob;
extern const gpio_dev_t gpiob;
#define GPIOB (&gpiob)
extern const gpio_dev gpioc;
extern const gpio_dev_t gpioc;
#define GPIOC (&gpioc)
#if STM32_NR_GPIO_PORTS > 3
extern const gpio_dev gpiod;
extern const gpio_dev_t gpiod;
#define GPIOD (&gpiod)
#endif
#if STM32_NR_GPIO_PORTS > 4
extern const gpio_dev gpioe;
extern const gpio_dev_t gpioe;
#define GPIOE (&gpioe)
extern const gpio_dev gpiof;
extern const gpio_dev_t gpiof;
#define GPIOF (&gpiof)
extern const gpio_dev gpiog;
extern const gpio_dev_t gpiog;
#define GPIOG (&gpiog)
#endif

extern const gpio_dev * const gpio_devs[3];
extern const gpio_dev_t * const gpio_devs[3];

/*
* Portable routines
*/
static inline void enableDebugPorts() { afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY); }
static inline void disableDebugPorts() { afio_cfg_debug_ports(AFIO_DEBUG_NONE); }

inline void gpio_init(const gpio_dev *dev) {
inline void gpio_init(const gpio_dev_t *dev) {
rcc_clk_enable(dev->clk_id);
rcc_reset_dev(dev->clk_id);
}

void gpio_init_all(void);
/* TODO flags argument version? */
void gpio_set_mode(const gpio_dev *dev, uint8 bit, gpio_pin_mode mode);
gpio_pin_mode gpio_get_mode(const gpio_dev *dev, uint8 bit);
void gpio_set_mode(const gpio_dev_t *dev, uint8 bit, gpio_pin_mode mode);
gpio_pin_mode gpio_get_mode(const gpio_dev_t *dev, uint8 bit);

static inline void gpio_set_pin_mode(uint8 pin, gpio_pin_mode mode) {
gpio_set_mode(gpio_devs[pin/16], pin%16, mode);
Expand All @@ -106,7 +106,7 @@ static inline void gpio_set_pin_mode(uint8 pin, gpio_pin_mode mode) {
* @brief Get a GPIO port's corresponding EXTI port configuration.
* @param dev GPIO port whose exti_cfg to return.
*/
static inline exti_cfg gpio_exti_port(gpio_dev *dev) {
static inline exti_cfg gpio_exti_port(gpio_dev_t *dev) {
return (exti_cfg)(EXTI_PA + (dev->clk_id - RCC_GPIOA));
}

Expand All @@ -119,7 +119,7 @@ static inline exti_cfg gpio_exti_port(gpio_dev *dev) {
* @param pin Pin on to set or reset
* @param val If true, set the pin. If false, reset the pin.
*/
static inline void gpio_write_bit(const gpio_dev *dev, uint8 bit, uint8 val) {
static inline void gpio_write_bit(const gpio_dev_t *dev, uint8 bit, uint8 val) {
val = !val; /* "set" bits are lower than "reset" bits */
dev->regs->BSRR = (1U << bit) << (16 * val);
}
Expand All @@ -137,7 +137,7 @@ static inline void gpio_write_pin(uint8 pin, uint8 val) {
* @param pin Pin on dev to test.
* @return True if the pin is set, false otherwise.
*/
static inline uint32 gpio_read_bit(const gpio_dev *dev, uint8 pin) {
static inline uint32 gpio_read_bit(const gpio_dev_t *dev, uint8 pin) {
return dev->regs->IDR & (1U << pin);
}

Expand All @@ -150,7 +150,7 @@ static inline uint32 gpio_read_pin(uint8 pin) {
* @param dev GPIO device.
* @param pin Pin on dev to toggle.
*/
static inline void gpio_toggle_bit(const gpio_dev *dev, uint8 bit) {
static inline void gpio_toggle_bit(const gpio_dev_t *dev, uint8 bit) {
dev->regs->ODR = dev->regs->ODR ^ (1U << bit);
}

Expand Down
38 changes: 19 additions & 19 deletions STM32F1/cores/maple/libmaple/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
* @param addr Slave address
* @param rw Read/write bit
*/
static inline void i2c_send_slave_addr(i2c_dev *dev, uint32 addr, uint32 rw) {
static inline void i2c_send_slave_addr(i2c_dev_t *dev, uint32 addr, uint32 rw) {
dev->regs->DR = (addr << 1) | rw;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ enum {
*
* @param dev I2C device
*/
void i2c_bus_reset(const i2c_dev *dev) {
void i2c_bus_reset(const i2c_dev_t *dev) {
/* Release both lines */
i2c_master_release_bus(dev); // Note: This configures the pins as GPIO instead of AF

Expand Down Expand Up @@ -182,7 +182,7 @@ void i2c_bus_reset(const i2c_dev *dev) {
* this reset logic will also clear other STM32 controllers
* on the bus that are also in this stuck-state.
*/
static void i2c_clear_busy_flag_erratum(const i2c_dev *dev) {
static void i2c_clear_busy_flag_erratum(const i2c_dev_t *dev) {
// 1. Clear PE bit.
dev->regs->CR1 &= ~I2C_CR1_PE;

Expand Down Expand Up @@ -237,7 +237,7 @@ static void i2c_clear_busy_flag_erratum(const i2c_dev *dev) {
* default values.
* @param dev Device to initialize.
*/
void i2c_init(i2c_dev *dev) {
void i2c_init(i2c_dev_t *dev) {
rcc_clk_enable(dev->clk_id); // The device's clock should enabled before we reset it
rcc_reset_dev(dev->clk_id);
_i2c_irq_priority_fixup(dev);
Expand All @@ -264,7 +264,7 @@ void i2c_init(i2c_dev *dev) {
* I2C_SLAVE_GENERAL_CALL: SLA+W broadcast to all general call
* listeners on bus. Addr 0x00
*/
void i2c_master_enable(i2c_dev *dev, uint32 flags, uint32 freq) {
void i2c_master_enable(i2c_dev_t *dev, uint32 flags, uint32 freq) {
/* Remap I2C if needed */
_i2c_handle_remap(dev, flags);

Expand Down Expand Up @@ -340,7 +340,7 @@ void i2c_master_enable(i2c_dev *dev, uint32 flags, uint32 freq) {
* I2C_SLAVE_GENERAL_CALL: SLA+W broadcast to all general call
* listeners on bus. Addr 0x00
*/
void i2c_slave_enable(i2c_dev *dev, uint32 flags, uint32 freq)
void i2c_slave_enable(i2c_dev_t *dev, uint32 flags, uint32 freq)
{
i2c_master_enable(dev, flags | I2C_SLAVE_MODE, freq);
}
Expand All @@ -349,8 +349,8 @@ void i2c_slave_enable(i2c_dev *dev, uint32 flags, uint32 freq)
/**
* @brief Process an i2c transaction.
*
* Transactions are composed of one or more i2c_msg's, and may be read
* or write tranfers. Multiple i2c_msg's will generate a repeated
* Transactions are composed of one or more i2c_msg_t's, and may be read
* or write tranfers. Multiple i2c_msg_t's will generate a repeated
* start in between messages.
*
* @param dev I2C device
Expand All @@ -362,8 +362,8 @@ void i2c_slave_enable(i2c_dev *dev, uint32 flags, uint32 freq)
* I2C_ERROR_PROTOCOL if there was a protocol error,
* I2C_ERROR_TIMEOUT if the transfer timed out.
*/
int8 i2c_master_xfer(i2c_dev *dev,
i2c_msg *msgs,
int8 i2c_master_xfer(i2c_dev_t *dev,
i2c_msg_t *msgs,
uint16 num,
uint32 timeout) {
int32 rc;
Expand Down Expand Up @@ -441,10 +441,10 @@ int8 i2c_master_xfer(i2c_dev *dev,
* @param timeout Timeout, in milliseconds
* @return 0 if target state is reached, a negative value on error.
*/
int8 wait_for_state_change(i2c_dev *dev,
i2c_state state,
int8 wait_for_state_change(i2c_dev_t *dev,
i2c_state_t state,
uint32 timeout) {
volatile i2c_state devState;
volatile i2c_state_t devState;
volatile uint32 devTimestamp;

while (1) {
Expand Down Expand Up @@ -483,7 +483,7 @@ int8 wait_for_state_change(i2c_dev *dev,
* IRQ handler for I2C master and slave.
* Handles transmission/reception.
*/
void _i2c_irq_handler(i2c_dev *dev) {
void _i2c_irq_handler(i2c_dev_t *dev) {
// See Note in ST Specs:
// Reading I2C_SR2 after reading I2C_SR1 clears the ADDR flag, even if the ADDR flag was
// set after reading I2C_SR1. Consequently, I2C_SR2 must be read only when ADDR is found
Expand All @@ -497,7 +497,7 @@ void _i2c_irq_handler(i2c_dev *dev) {

if (!(dev->config_flags & I2C_SLAVE_MODE)) { // Handle Master Mode Here:
int8_t bDone = 0; // Set to true when we're done with this transfer unit
i2c_msg *curMsg = dev->msg;
i2c_msg_t *curMsg = dev->msg;
if (curMsg != NULL) {
int todo = curMsg->length; // Bytes to transfer
if (curMsg->flags & I2C_MSG_READ) { // read transaction:
Expand Down Expand Up @@ -769,7 +769,7 @@ void _i2c_irq_handler(i2c_dev *dev) {
* Interrupt handler for I2C error conditions. Aborts any pending I2C
* transactions.
*/
void _i2c_irq_error_handler(i2c_dev *dev) {
void _i2c_irq_error_handler(i2c_dev_t *dev) {
__IO uint32_t sr1 = dev->regs->SR1;
__IO uint32_t sr2 = dev->regs->SR2;

Expand Down Expand Up @@ -821,7 +821,7 @@ void _i2c_irq_error_handler(i2c_dev *dev) {
/*
* CCR/TRISE configuration helper
*/
void i2c_set_ccr_trise(i2c_dev *dev, uint32 flags, uint32 freq) {
void i2c_set_ccr_trise(i2c_dev_t *dev, uint32 flags, uint32 freq) {
uint32 ccr = 0;
uint32 trise = 0;
uint32 clk_mhz = _i2c_bus_clk(dev);
Expand Down Expand Up @@ -865,7 +865,7 @@ void i2c_set_ccr_trise(i2c_dev *dev, uint32 flags, uint32 freq) {
* @param msg The dev_msg to pass to the slave init code
* @param func The function pointer to call
*/
void i2c_slave_attach_recv_handler(i2c_dev *dev, i2c_msg *msg, i2c_slave_recv_callback_func func) {
void i2c_slave_attach_recv_handler(i2c_dev_t *dev, i2c_msg_t *msg, i2c_slave_recv_callback_func_t func) {
dev->i2c_slave_recv_callback = func;
dev->i2c_slave_recv_msg = msg;
msg->xferred = 0;
Expand All @@ -879,7 +879,7 @@ void i2c_slave_attach_recv_handler(i2c_dev *dev, i2c_msg *msg, i2c_slave_recv_ca
* @param msg The dev_msg to pass to the slave init code
* @param func The function pointer to call
*/
void i2c_slave_attach_transmit_handler(i2c_dev *dev, i2c_msg *msg, i2c_slave_xmit_callback_func func) {
void i2c_slave_attach_transmit_handler(i2c_dev_t *dev, i2c_msg_t *msg, i2c_slave_xmit_callback_func_t func) {
dev->i2c_slave_xmit_callback = func;
dev->i2c_slave_xmit_msg = msg;
msg->xferred = 0;
Expand Down
Loading

0 comments on commit b358491

Please sign in to comment.