-
-
Notifications
You must be signed in to change notification settings - Fork 40k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify i2c_master API #14337
Unify i2c_master API #14337
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,11 +112,7 @@ static uint8_t micro_oled_screen_buffer[LCDWIDTH * LCDHEIGHT / 8] = {0}; | |
void micro_oled_init(void) { | ||
i2c_init(); | ||
|
||
#ifdef __AVR__ | ||
i2c_start(I2C_ADDRESS_SA0_1, 100); | ||
#else | ||
i2c_start(I2C_ADDRESS_SA0_1); | ||
#endif | ||
Comment on lines
-115
to
-119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this driver uses the high level I2C API ( |
||
|
||
// Display Init sequence for 64x48 OLED module | ||
send_command(DISPLAYOFF); // 0xAE | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,11 @@ | |
# pragma message("ChibiOS is currently 'best effort' and might not report accurate results") | ||
|
||
i2c_status_t i2c_start_bodge(uint8_t address, uint16_t timeout) { | ||
i2c_start(address); | ||
i2c_start(address, timeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can just be removed, because the existing ChibiOS implementation of |
||
|
||
// except on ChibiOS where the only way is do do "something" | ||
uint8_t data = 0; | ||
return i2c_readReg(address, 0, &data, sizeof(data), TIMEOUT); | ||
return i2c_readReg(address, 0, &data, sizeof(data), timeout); | ||
} | ||
|
||
# define i2c_start i2c_start_bodge | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,8 +27,80 @@ | |
#include "quantum.h" | ||
#include "i2c_master.h" | ||
#include <string.h> | ||
#include <ch.h> | ||
#include <hal.h> | ||
|
||
#ifdef I2C1_BANK | ||
# define I2C1_SCL_BANK I2C1_BANK | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is moved out of the header file, something like this will start failing: See keyboards/matrix/m20add/m20add.c line 67 |
||
# define I2C1_SDA_BANK I2C1_BANK | ||
#endif | ||
|
||
#ifndef I2C1_SCL_BANK | ||
# define I2C1_SCL_BANK GPIOB | ||
#endif | ||
|
||
#ifndef I2C1_SDA_BANK | ||
# define I2C1_SDA_BANK GPIOB | ||
#endif | ||
|
||
#ifndef I2C1_SCL | ||
# define I2C1_SCL 6 | ||
#endif | ||
#ifndef I2C1_SDA | ||
# define I2C1_SDA 7 | ||
#endif | ||
|
||
#ifdef USE_I2CV1 | ||
# ifndef I2C1_OPMODE | ||
# define I2C1_OPMODE OPMODE_I2C | ||
# endif | ||
# ifndef I2C1_CLOCK_SPEED | ||
# define I2C1_CLOCK_SPEED 100000 /* 400000 */ | ||
# endif | ||
# ifndef I2C1_DUTY_CYCLE | ||
# define I2C1_DUTY_CYCLE STD_DUTY_CYCLE /* FAST_DUTY_CYCLE_2 */ | ||
# endif | ||
#else | ||
// The default timing values below configures the I2C clock to 400khz assuming a 72Mhz clock | ||
// For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html | ||
# ifndef I2C1_TIMINGR_PRESC | ||
# define I2C1_TIMINGR_PRESC 0U | ||
# endif | ||
# ifndef I2C1_TIMINGR_SCLDEL | ||
# define I2C1_TIMINGR_SCLDEL 7U | ||
# endif | ||
# ifndef I2C1_TIMINGR_SDADEL | ||
# define I2C1_TIMINGR_SDADEL 0U | ||
# endif | ||
# ifndef I2C1_TIMINGR_SCLH | ||
# define I2C1_TIMINGR_SCLH 38U | ||
# endif | ||
# ifndef I2C1_TIMINGR_SCLL | ||
# define I2C1_TIMINGR_SCLL 129U | ||
# endif | ||
#endif | ||
|
||
#ifndef I2C_DRIVER | ||
# define I2C_DRIVER I2CD1 | ||
#endif | ||
|
||
#ifdef USE_GPIOV1 | ||
# ifndef I2C1_SCL_PAL_MODE | ||
# define I2C1_SCL_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN | ||
# endif | ||
# ifndef I2C1_SDA_PAL_MODE | ||
# define I2C1_SDA_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN | ||
# endif | ||
#else | ||
// The default PAL alternate modes are used to signal that the pins are used for I2C | ||
# ifndef I2C1_SCL_PAL_MODE | ||
# define I2C1_SCL_PAL_MODE 4 | ||
# endif | ||
# ifndef I2C1_SDA_PAL_MODE | ||
# define I2C1_SDA_PAL_MODE 4 | ||
# endif | ||
#endif | ||
|
||
static uint8_t i2c_address; | ||
|
||
static const I2CConfig i2cconfig = { | ||
|
@@ -77,7 +149,8 @@ __attribute__((weak)) void i2c_init(void) { | |
} | ||
} | ||
|
||
i2c_status_t i2c_start(uint8_t address) { | ||
i2c_status_t i2c_start(uint8_t address, uint16_t timeout) { | ||
(void)timeout; | ||
i2c_address = address; | ||
i2cStart(&I2C_DRIVER, &i2cconfig); | ||
return I2C_STATUS_SUCCESS; | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that
i2c_start()
andi2c_stop()
should also be moved into the#if defined(__AVR__)
block — the existing ChibiOS implementation of these functions does something completely different.i2c_start()
andi2c_stop()
in the ChibiOS driver can be dropped completely — I don't think that there is any code that actually wants to use these functions in their current form (some code may be using them by mistake, like the QWIIK OLED driver).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not worried about the usage of
i2c_start
right now, as that's going to have to be tackled as part of the rework for #7967. That change would potentially introduce further platform specific ifdefs, which is what I want to avoid long term. Given the vast mess in this area.... theres going to be a fair few iterations to get things truly uniform.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is, there is a low level API where you have functions that actually cause I2C start and stop signaling on the bus, and a high level API where all I2C details like start/stop are handled internally and not exposed to the API users. That high level API may need functions like
i2c_lock()
andi2c_unlock()
, which is what #7967 tried to implement, but those functions should not be calledi2c_start()
andi2c_stop()
, if we want to use those names for the low level I2C start/stop functions.And maybe there should be a header file provided by the actual driver implementation, because there is also #12616 with the bitbang I2C implementation, and that implementation can also provide the low level API. So that
#if defined(__AVR__)
may become#if defined(I2C_RAW_API_AVAILABLE)
or something like that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its not that I dont get your point, its that I dont care for this phase of the work. The plan is to defer some of the API decisions till when they are reliant to the ongoing work. The
i2c_start
changes that are within this PR are limited to having the same function declaration. I know the functionality is not the same (i was the one who added the i2c_scanner keymap), and that will need some further work.The ifdef is just there while I refactor, its not planned to have any platform specific code within the
i2c_master.h
file. The consumer should not care what is implementing the functionallity. The only reason that is done in a few places right now is down to short term goals. Both the existing bitbang PR proposals need some work, as i wouldnt be happy if they merged in their current form.