37 #if PERIODIC_TELEMETRY
44 #ifndef SOFTI2C0_DIVIDER
45 #define SOFTI2C0_DIVIDER 1
47 #ifndef SOFTI2C1_DIVIDER
48 #define SOFTI2C1_DIVIDER 1
74 #if USE_SOFTI2C0 && ( \
75 !defined(SOFTI2C0_SDA_GPIO) || !defined(SOFTI2C0_SDA_PIN) || \
76 !defined(SOFTI2C0_SCL_GPIO) || !defined(SOFTI2C0_SCL_PIN))
77 #error "SDA and SCL pins must be configured for SOFTI2C0!"
80 #if USE_SOFTI2C1 && ( \
81 !defined(SOFTI2C1_SDA_GPIO) || !defined(SOFTI2C1_SDA_PIN) || \
82 !defined(SOFTI2C1_SCL_GPIO) || !defined(SOFTI2C1_SCL_PIN))
83 #error "SDA and SCL pins must be configured for SOFTI2C1!"
90 void softi2c0_init(
void) {
94 #if PERIODIC_TELEMETRY
95 void send_softi2c0_err(
struct transport_tx *trans,
struct link_device *
dev)
97 uint16_t i2c0_wd_reset_cnt = softi2c0.errors->wd_reset_cnt;
98 uint16_t i2c0_queue_full_cnt = softi2c0.errors->queue_full_cnt;
99 uint16_t i2c0_ack_fail_cnt = softi2c0.errors->ack_fail_cnt;
100 uint16_t i2c0_miss_start_stop_cnt = softi2c0.errors->miss_start_stop_cnt;
101 uint16_t i2c0_arb_lost_cnt = softi2c0.errors->arb_lost_cnt;
102 uint16_t i2c0_over_under_cnt = softi2c0.errors->over_under_cnt;
103 uint16_t i2c0_pec_recep_cnt = softi2c0.errors->pec_recep_cnt;
104 uint16_t i2c0_timeout_tlow_cnt = softi2c0.errors->timeout_tlow_cnt;
105 uint16_t i2c0_smbus_alert_cnt = softi2c0.errors->smbus_alert_cnt;
106 uint16_t i2c0_unexpected_event_cnt = softi2c0.errors->unexpected_event_cnt;
107 uint32_t i2c0_last_unexpected_event = softi2c0.errors->last_unexpected_event;
109 pprz_msg_send_I2C_ERRORS(trans,
dev, AC_ID,
111 &i2c0_queue_full_cnt,
113 &i2c0_miss_start_stop_cnt,
115 &i2c0_over_under_cnt,
117 &i2c0_timeout_tlow_cnt,
118 &i2c0_smbus_alert_cnt,
119 &i2c0_unexpected_event_cnt,
120 &i2c0_last_unexpected_event,
129 void softi2c1_init(
void) {
133 #if PERIODIC_TELEMETRY
134 void send_softi2c1_err(
struct transport_tx *trans,
struct link_device *
dev)
136 uint16_t i2c1_wd_reset_cnt = softi2c1.errors->wd_reset_cnt;
137 uint16_t i2c1_queue_full_cnt = softi2c1.errors->queue_full_cnt;
138 uint16_t i2c1_ack_fail_cnt = softi2c1.errors->ack_fail_cnt;
139 uint16_t i2c1_miss_start_stop_cnt = softi2c1.errors->miss_start_stop_cnt;
140 uint16_t i2c1_arb_lost_cnt = softi2c1.errors->arb_lost_cnt;
141 uint16_t i2c1_over_under_cnt = softi2c1.errors->over_under_cnt;
142 uint16_t i2c1_pec_recep_cnt = softi2c1.errors->pec_recep_cnt;
143 uint16_t i2c1_timeout_tlow_cnt = softi2c1.errors->timeout_tlow_cnt;
144 uint16_t i2c1_smbus_alert_cnt = softi2c1.errors->smbus_alert_cnt;
145 uint16_t i2c1_unexpected_event_cnt = softi2c1.errors->unexpected_event_cnt;
146 uint32_t i2c1_last_unexpected_event = softi2c1.errors->last_unexpected_event;
148 pprz_msg_send_I2C_ERRORS(trans,
dev, AC_ID,
150 &i2c1_queue_full_cnt,
152 &i2c1_miss_start_stop_cnt,
154 &i2c1_over_under_cnt,
156 &i2c1_timeout_tlow_cnt,
157 &i2c1_smbus_alert_cnt,
158 &i2c1_unexpected_event_cnt,
159 &i2c1_last_unexpected_event,
167 #if defined(CHIBIOS_MCU_ARCH_H) || defined(STM32_MCU_ARCH_H)
197 #ifdef STM32_MCU_ARCH_H
209 void softi2c0_hw_init(
void) {
214 softi2c0.reg_addr = (
void *) &softi2c0_device;
215 softi2c0.errors = &softi2c0_errors;
218 softi2c0_device.periph = &softi2c0;
219 softi2c0_device.sda_port = SOFTI2C0_SDA_GPIO;
220 softi2c0_device.sda_pin = SOFTI2C0_SDA_PIN;
221 softi2c0_device.scl_port = SOFTI2C0_SCL_GPIO;
222 softi2c0_device.scl_pin = SOFTI2C0_SCL_PIN;
226 SOFTI2C0_SDA_GPIO, SOFTI2C0_SDA_PIN,
227 SOFTI2C0_SCL_GPIO, SOFTI2C0_SCL_PIN);
234 void softi2c1_hw_init(
void) {
239 softi2c1.reg_addr = (
void *) &softi2c1_device;
240 softi2c1.errors = &softi2c1_errors;
243 softi2c1_device.periph = &softi2c1;
244 softi2c1_device.sda_port = SOFTI2C1_SDA_GPIO;
245 softi2c1_device.sda_pin = SOFTI2C1_SDA_PIN;
246 softi2c1_device.scl_port = SOFTI2C1_SCL_GPIO;
247 softi2c1_device.scl_pin = SOFTI2C1_SCL_PIN;
251 SOFTI2C1_SDA_GPIO, SOFTI2C1_SDA_PIN,
252 SOFTI2C1_SCL_GPIO, SOFTI2C1_SCL_PIN);
320 default:
return false;
345 default:
return false;
376 default:
return false;
407 default:
return false;
440 default:
return false;
474 default:
return false;
481 #pragma GCC diagnostic push
482 #pragma GCC diagnostic ignored "-Wcast-qual"
603 #pragma GCC diagnostic pop
610 if (
p->trans_insert_idx !=
p->trans_extract_idx) {
626 static int softi2c0_cnt = 1;
635 static int softi2c1_cnt = 1;
651 return (
p->status ==
I2CIdle) && (
p->trans_insert_idx ==
p->trans_extract_idx);
656 if (next_idx ==
p->trans_extract_idx) {
658 p->errors->queue_full_cnt++;
664 p->trans[
p->trans_insert_idx] = t;
665 p->trans_insert_idx = next_idx;
void gpio_setup_input_pullup(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs with pull up resistor enabled.
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
void gpio_setup_input(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs.
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
static void gpio_clear(ioportid_t port, uint16_t pin)
Clear a gpio output to low level.
ioportid_t gpio_port_t
Abstract gpio port type for hardware independent part.
static uint8_t gpio_get(ioportid_t port, uint16_t pin)
Get level of a gpio.
Some architecture independent helper functions for GPIOs.
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
struct i2c_errors * errors
uint16_t len_r
Number of bytes to read/receive.
volatile uint16_t ack_fail_cnt
enum I2CTransactionType type
Transaction type.
enum I2CTransactionStatus status
Transaction status.
uint8_t slave_addr
Slave address.
uint8_t len_w
Number of bytes to write/transmit.
#define ZEROS_ERR_COUNTER(_i2c_err)
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
void i2c_init(struct i2c_periph *p)
Initialize I2C peripheral.
@ I2CTransRunning
transaction is currently ongoing
@ I2CTransSuccess
transaction successfully finished by I2C driver
@ I2CTransFailed
transaction failed
@ I2CTransPending
transaction is pending in queue
@ I2CTransRx
receive only transaction
@ I2CTransTxRx
transmit and receive transaction
I2C transaction structure.
static bool softi2c_write_restart(struct softi2c_device *d)
static void softi2c_device_event(struct softi2c_device *d)
static bool softi2c_submit(struct i2c_periph *periph, struct i2c_transaction *t)
static bool softi2c_idle(struct i2c_periph *periph)
static bool softi2c_read_bit(struct softi2c_device *d, bool *bit)
static void softi2c_setbitrate(struct i2c_periph *periph, int bitrate)
static void softi2c_gpio_drive_low(gpio_port_t port, uint16_t pin)
static void softi2c_gpio_highz(gpio_port_t port, uint16_t pin)
struct i2c_periph * periph
static bool softi2c_gpio_read(gpio_port_t port, uint16_t pin)
static bool softi2c_write_byte(struct softi2c_device *d, uint8_t byte, bool *ack)
static void softi2c_setup_gpio(gpio_port_t sda_port, uint16_t sda_pin, gpio_port_t scl_port, uint16_t scl_pin)
static bool softi2c_write_bit(struct softi2c_device *d, bool bit)
static bool softi2c_process_transaction(struct softi2c_device *d, struct i2c_transaction *t)
static bool softi2c_write_start(struct softi2c_device *d)
static bool softi2c_write_stop(struct softi2c_device *d)
static void softi2c_spin(struct i2c_periph *p)
static bool softi2c_read_byte(struct softi2c_device *d, uint8_t *byte, bool ack)
Platform-independent software I2C implementation.
void gpio_enable_clock(uint32_t port)
Enable the relevant clock.
static const struct usb_device_descriptor dev
Architecture independent timing functions.
Periodic telemetry system header (includes downlink utility and generated code).
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.