diff options
| author | Ryan <fauxpark@gmail.com> | 2025-01-26 23:48:46 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-26 13:48:46 +0100 |
| commit | ef29a46c87e3a82459b3af4158f7888d55c0b22b (patch) | |
| tree | 9f5ea0f7f46393923e207aeb09dc1242e573be16 /platforms | |
| parent | 291d154d7b13efb83f24c76df26277f32b9f0a58 (diff) | |
Unify i2c_master headers (#24846)
* Unify i2c_master headers
* More documentation improvements
* Reorganise PAL mode defaults
Diffstat (limited to 'platforms')
| -rw-r--r-- | platforms/avr/drivers/i2c_master.h | 41 | ||||
| -rw-r--r-- | platforms/chibios/drivers/i2c_master.c | 43 | ||||
| -rw-r--r-- | platforms/chibios/drivers/i2c_master.h | 42 |
3 files changed, 21 insertions, 105 deletions
diff --git a/platforms/avr/drivers/i2c_master.h b/platforms/avr/drivers/i2c_master.h deleted file mode 100644 index ad92caa55a..0000000000 --- a/platforms/avr/drivers/i2c_master.h +++ /dev/null | |||
| @@ -1,41 +0,0 @@ | |||
| 1 | /* Copyright (C) 2019 Elia Ritterbusch | ||
| 2 | + | ||
| 3 | * This program is free software: you can redistribute it and/or modify | ||
| 4 | * it under the terms of the GNU General Public License as published by | ||
| 5 | * the Free Software Foundation, either version 3 of the License, or | ||
| 6 | * (at your option) any later version. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope that it will be useful, | ||
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | * GNU General Public License for more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | /* Library made by: g4lvanix | ||
| 17 | * GitHub repository: https://github.com/g4lvanix/I2C-master-lib | ||
| 18 | */ | ||
| 19 | |||
| 20 | #pragma once | ||
| 21 | |||
| 22 | #include <stdint.h> | ||
| 23 | |||
| 24 | typedef int16_t i2c_status_t; | ||
| 25 | |||
| 26 | #define I2C_STATUS_SUCCESS (0) | ||
| 27 | #define I2C_STATUS_ERROR (-1) | ||
| 28 | #define I2C_STATUS_TIMEOUT (-2) | ||
| 29 | |||
| 30 | #define I2C_TIMEOUT_IMMEDIATE (0) | ||
| 31 | #define I2C_TIMEOUT_INFINITE (0xFFFF) | ||
| 32 | |||
| 33 | void i2c_init(void); | ||
| 34 | i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 35 | i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 36 | i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 37 | i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 38 | i2c_status_t i2c_write_register16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 39 | i2c_status_t i2c_read_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 40 | i2c_status_t i2c_read_register16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 41 | i2c_status_t i2c_ping_address(uint8_t address, uint16_t timeout); | ||
diff --git a/platforms/chibios/drivers/i2c_master.c b/platforms/chibios/drivers/i2c_master.c index 0d5fb1e985..1d7fe27633 100644 --- a/platforms/chibios/drivers/i2c_master.c +++ b/platforms/chibios/drivers/i2c_master.c | |||
| @@ -29,17 +29,37 @@ | |||
| 29 | #include "i2c_master.h" | 29 | #include "i2c_master.h" |
| 30 | #include "gpio.h" | 30 | #include "gpio.h" |
| 31 | #include "chibios_config.h" | 31 | #include "chibios_config.h" |
| 32 | #include <string.h> | ||
| 33 | #include <ch.h> | 32 | #include <ch.h> |
| 34 | #include <hal.h> | 33 | #include <hal.h> |
| 35 | 34 | ||
| 35 | #ifndef I2C_DRIVER | ||
| 36 | # define I2C_DRIVER I2CD1 | ||
| 37 | #endif | ||
| 38 | |||
| 36 | #ifndef I2C1_SCL_PIN | 39 | #ifndef I2C1_SCL_PIN |
| 37 | # define I2C1_SCL_PIN B6 | 40 | # define I2C1_SCL_PIN B6 |
| 38 | #endif | 41 | #endif |
| 42 | |||
| 43 | #ifndef I2C1_SCL_PAL_MODE | ||
| 44 | # ifdef USE_GPIOV1 | ||
| 45 | # define I2C1_SCL_PAL_MODE PAL_MODE_ALTERNATE_OPENDRAIN | ||
| 46 | # else | ||
| 47 | # define I2C1_SCL_PAL_MODE 4 | ||
| 48 | # endif | ||
| 49 | #endif | ||
| 50 | |||
| 39 | #ifndef I2C1_SDA_PIN | 51 | #ifndef I2C1_SDA_PIN |
| 40 | # define I2C1_SDA_PIN B7 | 52 | # define I2C1_SDA_PIN B7 |
| 41 | #endif | 53 | #endif |
| 42 | 54 | ||
| 55 | #ifndef I2C1_SDA_PAL_MODE | ||
| 56 | # ifdef USE_GPIOV1 | ||
| 57 | # define I2C1_SDA_PAL_MODE PAL_MODE_ALTERNATE_OPENDRAIN | ||
| 58 | # else | ||
| 59 | # define I2C1_SDA_PAL_MODE 4 | ||
| 60 | # endif | ||
| 61 | #endif | ||
| 62 | |||
| 43 | #ifdef USE_I2CV1 | 63 | #ifdef USE_I2CV1 |
| 44 | # ifndef I2C1_OPMODE | 64 | # ifndef I2C1_OPMODE |
| 45 | # define I2C1_OPMODE OPMODE_I2C | 65 | # define I2C1_OPMODE OPMODE_I2C |
| @@ -70,27 +90,6 @@ | |||
| 70 | # endif | 90 | # endif |
| 71 | #endif | 91 | #endif |
| 72 | 92 | ||
| 73 | #ifndef I2C_DRIVER | ||
| 74 | # define I2C_DRIVER I2CD1 | ||
| 75 | #endif | ||
| 76 | |||
| 77 | #ifdef USE_GPIOV1 | ||
| 78 | # ifndef I2C1_SCL_PAL_MODE | ||
| 79 | # define I2C1_SCL_PAL_MODE PAL_MODE_ALTERNATE_OPENDRAIN | ||
| 80 | # endif | ||
| 81 | # ifndef I2C1_SDA_PAL_MODE | ||
| 82 | # define I2C1_SDA_PAL_MODE PAL_MODE_ALTERNATE_OPENDRAIN | ||
| 83 | # endif | ||
| 84 | #else | ||
| 85 | // The default PAL alternate modes are used to signal that the pins are used for I2C | ||
| 86 | # ifndef I2C1_SCL_PAL_MODE | ||
| 87 | # define I2C1_SCL_PAL_MODE 4 | ||
| 88 | # endif | ||
| 89 | # ifndef I2C1_SDA_PAL_MODE | ||
| 90 | # define I2C1_SDA_PAL_MODE 4 | ||
| 91 | # endif | ||
| 92 | #endif | ||
| 93 | |||
| 94 | static const I2CConfig i2cconfig = { | 93 | static const I2CConfig i2cconfig = { |
| 95 | #if defined(USE_I2CV1_CONTRIB) | 94 | #if defined(USE_I2CV1_CONTRIB) |
| 96 | I2C1_CLOCK_SPEED, | 95 | I2C1_CLOCK_SPEED, |
diff --git a/platforms/chibios/drivers/i2c_master.h b/platforms/chibios/drivers/i2c_master.h deleted file mode 100644 index 2215f89ad2..0000000000 --- a/platforms/chibios/drivers/i2c_master.h +++ /dev/null | |||
| @@ -1,42 +0,0 @@ | |||
| 1 | /* Copyright 2018 Jack Humbert | ||
| 2 | * Copyright 2018 Yiancar | ||
| 3 | * | ||
| 4 | * This program is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | /* This library follows the convention of the AVR i2c_master library. | ||
| 19 | * As a result addresses are expected to be already shifted (addr << 1). | ||
| 20 | * I2CD1 is the default driver which corresponds to pins B6 and B7. This | ||
| 21 | * can be changed. | ||
| 22 | * Please ensure that HAL_USE_I2C is TRUE in the halconf.h file and that | ||
| 23 | * STM32_I2C_USE_I2C1 is TRUE in the mcuconf.h file. | ||
| 24 | */ | ||
| 25 | #pragma once | ||
| 26 | |||
| 27 | #include <stdint.h> | ||
| 28 | |||
| 29 | typedef int16_t i2c_status_t; | ||
| 30 | |||
| 31 | #define I2C_STATUS_SUCCESS (0) | ||
| 32 | #define I2C_STATUS_ERROR (-1) | ||
| 33 | #define I2C_STATUS_TIMEOUT (-2) | ||
| 34 | |||
| 35 | void i2c_init(void); | ||
| 36 | i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 37 | i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 38 | i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 39 | i2c_status_t i2c_write_register16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 40 | i2c_status_t i2c_read_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 41 | i2c_status_t i2c_read_register16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 42 | i2c_status_t i2c_ping_address(uint8_t address, uint16_t timeout); | ||
