cpu: efm32: adapt to new I2C interface#9208
Conversation
aabadie
left a comment
There was a problem hiding this comment.
Now there's a common implementation is drivers/periph_common/i2c.c where default i2c_read_byte, i2c_write_byte, i2c_read_reg, i2c_write_reg, i2c_read_regs and i2c_write_regs/ are already available it the CPU implementation can work with them.
In short, if the CPU supports it, it can only provide i2c_read_bytes and i2c_write_bytes function and reuse the others from the driver.
cpu/efm32/periph/i2c.c
Outdated
| } | ||
|
|
||
| int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, void *data) | ||
| int i2c_read_reg(i2c_t dev, uint16_t address, uint16_t reg, void *data, uint8_t flags) |
There was a problem hiding this comment.
i2c_read_reg and i2c_write_reg are now provided by the common driver in drivers/periph_common. Just define PERIPH_I2C_NEED_READ_REG and PERIPH_I2C_NEED_WRITE_REG in the periph_cpu.h file of your CPU type.
For example in #9202, I added the following in cpu/stm32_common/periph_cpu_common.h:
/**
* @name Use the shared I2C functions
* @{
*/
/** Use read regs function from periph common */
#define PERIPH_I2C_NEED_READ_REG
/** Use write regs function from periph common */
#define PERIPH_I2C_NEED_WRITE_REG
/** @} */Other defines exist for PERIPH_I2C_NEED_READ_REGS and PERIPH_I2C_NEED_WRITE_REGS if your CPU driver implementation works with them.
There was a problem hiding this comment.
@aabadie I don't want to use the common implementations, because I then have to use the optional I2C flags, which I cannot use with emlib. Emlib has its own way to provide I2C_START and I2C_STOP.
There was a problem hiding this comment.
I see. Why not converting RIOT common flags to emlib ones (using a switch) ? This way you can get rid of these functions.
There was a problem hiding this comment.
I can see if that works, but it would require me to briefly patch em_i2c.c (which I don't mind, since Gecko-SDK is already prepared for RIOT-OS).
cpu/efm32/periph/i2c.c
Outdated
| } | ||
|
|
||
| int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data) | ||
| int i2c_write_byte(i2c_t dev, uint16_t address, uint8_t data, uint8_t flags) |
There was a problem hiding this comment.
You don't need this function, it's already provided by the common i2c driver (in drivers/periph_common/)
cpu/efm32/periph/i2c.c
Outdated
| } | ||
|
|
||
| int i2c_read_byte(i2c_t dev, uint8_t address, void *data) | ||
| int i2c_read_byte(i2c_t dev, uint16_t address, void *data, uint8_t flags) |
There was a problem hiding this comment.
You also don't need this function
pkg/gecko_sdk/Makefile
Outdated
| PKG_NAME=gecko_sdk | ||
| PKG_URL=https://github.com/basilfx/RIOT-gecko-sdk | ||
| PKG_VERSION=d381e526d68a2d0c951f37040c1c2e168ac66cd6 | ||
| PKG_VERSION=a5e7be18ef3d7947ab6f04f9cd74f8808da7aece |
There was a problem hiding this comment.
Why do you need to update ?
There was a problem hiding this comment.
It includes an update for the I2C driver.
cpu/efm32/periph/i2c.c
Outdated
| /* transfer finished, interpret the result */ | ||
| switch (i2c_progress[dev]) { | ||
| case i2cTransferDone: | ||
| return I2C_ACK; |
There was a problem hiding this comment.
We were wondering last week if it's better to return I2C_ACK when everything is fine or the number of bytes written on the bus.
What do you think ?
There was a problem hiding this comment.
I don't think it really matters. I2C_ACK would imply all bytes are transferred (in either direction), otherwise you would receive a different error (e.g. bus arbitration lost, NACK).
There was a problem hiding this comment.
I agree but we need to get a consensus here so all CPUs will behave in the same way :)
|
@basilfx Any progress on this ? |
|
ping @basilfx |
|
@dylad Sorry, I haven't found the time to fix this yet. |
|
@basilfx no problem, do you think you can fix it before the new release ? |
aabadie
left a comment
There was a problem hiding this comment.
@basilfx, this PR is the last one remaining to have all CPUs drivers adapted. It would be great to have it merged soon.
Do you think you'll have time to work on it before mid-july (and even before, so we can test the whole feature branch) ?
cpu/efm32/periph/i2c.c
Outdated
| } | ||
|
|
||
| int i2c_init_master(i2c_t dev, i2c_speed_t speed) | ||
| int i2c_init(i2c_t dev) |
There was a problem hiding this comment.
Should be void i2c_init(i2c_t dev), according to the new i2c api
cpu/efm32/periph/i2c.c
Outdated
| assert(I2C_NUMOF <= (sizeof(i2c_lock) / sizeof(i2c_lock[0]))); | ||
|
|
||
| /* check if device is valid */ | ||
| if (dev >= I2C_NUMOF) { |
There was a problem hiding this comment.
There's a consensus on using assert:
assert(dev < I2C_NUMOF);|
I talked with @basilfx on IRC. He'll try to adapt the EFM32 this week-end if he find some time. |
cpu/efm32/periph/i2c.c
Outdated
| * @brief Start and track an I2C transfer. | ||
| */ | ||
| static void _transfer(i2c_t dev, I2C_TransferSeq_TypeDef *transfer) | ||
| int _transfer(i2c_t dev, I2C_TransferSeq_TypeDef *transfer) |
There was a problem hiding this comment.
This function should remain static or maybe even become static inline, otherwise it's exposed publicly.
5c4ea66 to
f12267a
Compare
|
I have fixed all the comments. Sorry for the delay, as I have been very busy with other things. I also thought it would be more work, but I opted for not supporting This version won't compile, because it depends on current I tested STK3600 with U8g2, SLTB001A with Si7021 and BMP280 and the SLSTK3401a with Si7021. |
smlng
left a comment
There was a problem hiding this comment.
when compiling I get the following error on macOS:
/RIOT/cpu/efm32/periph/i2c.c:86:14: error: 'i2cTransferAddrNack' undeclared (first use in this function); did you mean 'i2cTransferNack'?
case i2cTransferAddrNack:
^~~~~~~~~~~~~~~~~~~
i2cTransferNack
/RIOT/cpu/efm32/periph/i2c.c:86:14: note: each undeclared identifier is reported only once for each function it appears in
|
@smlng Please see my comment :-) |
|
oops ... but yes with the new commit its works. |
|
@aabadie are you happy with the changes ? |
|
well then please squash to make travis happy |
f12267a to
10339e2
Compare
|
Travis is Happy! |
|
and ping @aabadie, let's get over with this. |
|
we can merge this one as soon as @aabadie dismiss his review if he think his comments were addressed ! |
|
Here we go ! thanks @basilfx ! |
cpu: efm32: adapt to new I2C interface
cpu: efm32: adapt to new I2C interface
cpu: efm32: adapt to new I2C interface
Contribution description
This PR updates the EFM32 port to work with the new I2C peripheral interface. I've tested this on the SLTB001A (which has Si7021, BMP280 + I2C power controller), SLSTK3401a (Si7021) and STK3600 (for #9207).
I don't have 10-bit I2C device, nor have I access to something to test 16 bit registers.
Issues/PRs references
#6577, depends on
#9205and#9206(or#8383).