Skip to content
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

Driver for AT24CXXX EEPROM #11929

Merged
merged 3 commits into from
Feb 25, 2020
Merged

Conversation

fabian18
Copy link
Contributor

Contribution description

This contribution adds a driver to support the external EEPROM modules
AT24C128 and ATC256.
It allows integration with the existing EEPROM registry, while it includes
a pseudomodule that provides wrappers for the expected EEPROM API.

Testing procedure

Development and testing has been done with the bluepill board.
There is a test application in tests/driver_at24cxxx.

You can also run the existing test for the EEPROM registry.
USEMODULE=at24cxxx_eepreg_wrapper make BOARD=bluepill flash
make BOARD=bluepill term

Issues/PRs references

None

@benpicco benpicco added Area: drivers Area: Device drivers Type: new feature The issue requests / The PR implemements a new feature for RIOT labels Jul 29, 2019
Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can make the driver much simpler by using some existing i2c helper functions.

drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
@fabian18 fabian18 force-pushed the driver-at24cxxx-eeprom branch from 45e89f2 to aa7e32f Compare July 30, 2019 14:10
Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw: If you want to fix a bug in commit with ID , instead of normal commit you can use git commit --fixup=<FOO>. When later the reviewer asks to squash, you can then use git commit --autosquash -i <COMMIT_ID> and git will sort the fixup-commit to be integrated into the commits they fix. This can safe you some work, but has the disadvantage that the commit messages of the fixes no longer make sense. (But eventually those commits will be integrated into the one they fix anyway.)

Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two remarks from me, see inline

drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
drivers/Makefile.dep Outdated Show resolved Hide resolved
@benpicco
Copy link
Contributor

benpicco commented Jul 31, 2019

I'd suggest you drop the 'C' from the name - this family of EEPROMs is commonly known as AT24xxx.
The SPI version will then be AT25xxx.

Linux just calls them at24 & at25.

Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pointed out some minor issues inline.

This is by the way a very nice PR, especially since it is your first :-)

drivers/at24xxx/eeprom_wrapper.c Outdated Show resolved Hide resolved
drivers/Makefile.dep Outdated Show resolved Hide resolved
drivers/at24xxx/at24xxx.c Outdated Show resolved Hide resolved
drivers/at24xxx/include/at24xxx_params.h Outdated Show resolved Hide resolved
drivers/at24xxx/include/at24xxx_params.h Outdated Show resolved Hide resolved
drivers/at24xxx/include/at24xxx_params.h Outdated Show resolved Hide resolved
drivers/at24xxx/include/at24xxx_params.h Outdated Show resolved Hide resolved
drivers/include/at24xxx.h Outdated Show resolved Hide resolved
drivers/include/at24xxx.h Outdated Show resolved Hide resolved
@fabian18
Copy link
Contributor Author

fabian18 commented Aug 5, 2019

I have just seen that there are many different at24xxx. They also vary in page size, so I am going to add the page size to the configuration parameters.
The AT24C1024 has more then 64kB of memory but also uses 2 address bytes, where the device I2C address is different for the lower 64kB and the higher 64kB. So I think the AT24C1024 will not be fully functioning with this driver.

@maribu
Copy link
Member

maribu commented Aug 5, 2019

To me it would also be fine to limit this PR to a subset of devices with the same page size. The AT24C256 and the AT24C128 are the most popular ones anyway, so maybe no one will ever need support for the other chips. In any case, I'm personally a fan of limiting the problem space a PR wants to solve, so that reviewing is easier: Support for more device could be added in a follow-up PR.

@maribu
Copy link
Member

maribu commented Aug 5, 2019

Oh, almost forgot: Could you add at24c256 and at24c128 as (empty) pseudo modules that depend on the driver? That would ease the use as one could add the name of the actually used device.

(Or maybe even at24c256% to match both at24c256 and at24c256c?)

Edit: Here is a backup of the old AT24C256 data sheet, here is the new AT24C256C version on the official Microchip Homepage. The wildcard in at24c256% would likely match also future versions, which likely will remain backward compatible (and therefore compatible with the driver).

@maribu
Copy link
Member

maribu commented Aug 6, 2019

I'd suggest you drop the 'C' from the name - this family of EEPROMs is commonly known as AT24xxx.
The SPI version will then be AT25xxx.

Linux just calls them at24 & at25.

@benpicco: I only just read your comment. Do you still prefer at24xxx? The devices supported by the driver have AT24C as common prefix with three digits following, so the common RIOT naming scheme would be at24cxxx for the driver. Are you fine with at24cxxx?

@benpicco
Copy link
Contributor

benpicco commented Aug 6, 2019

@maribu I'm fine with either. All devices in the kernel follow the AT24Cxxx naming scheme too.
The naming is indicative of capacity/page size and other vendors just produce compatible modules.

Linux also has handling for devices with multiple addresses but I agree that this is out of the scope of this PR since I assume neither of us has the hardware to test this.

@maribu
Copy link
Member

maribu commented Aug 11, 2019

OK. One last thing came to my mind: The indentation is different from RIOT's standard. You can use the command line tool uncrustify with the config in the root of the repo to get code formatting consistent. After that, I'd say squash to three commits: One for the base driver, one for the test and one for the wrapper.

@fabian18 fabian18 force-pushed the driver-at24cxxx-eeprom branch from 09297d7 to 4227796 Compare August 12, 2019 12:04
@maribu maribu added Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines labels Aug 16, 2019
@maribu
Copy link
Member

maribu commented Aug 16, 2019

@fabian18: When I test with no device connected to the I2C bus, it seems the test/driver hangs instead of returning with an error code. Can you reproduce that?

Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See inline comments. I'm not fully sure how to do proper error handling. Maybe it is the best to give up on having the API compatible with the EEPROM API for now, as the API prevents any reasonable error handling :-/

tests/driver_at24cxxx/main.c Outdated Show resolved Hide resolved
drivers/at24cxxx/at24cxxx.c Outdated Show resolved Hide resolved
@fabian18
Copy link
Contributor Author

@maribu
Ain´t your requests for changes fulfilled?

@benpicco
Copy link
Contributor

Thank you for the update! Good to have extended device support.
I just ran this on the AT24MAC402 (I know I know, it's not supported by the driver, but that's what I had at hand), but I got

2020-02-17 16:39:44,116 #  main(): This is RIOT! (Version: 2020.04-devel-522-gc58f8-driver-at24cxxx-eeprom)
2020-02-17 16:39:44,122 # Starting tests for module at24cxxx
2020-02-17 16:39:44,132 # [SUCCESS] at24cxxx_init
2020-02-17 16:39:44,134 # [SUCCESS] at24cxxx_write_byte
2020-02-17 16:39:44,135 # [SUCCESS] at24cxxx_read_byte
2020-02-17 16:39:44,136 # [SUCCESS] write_byte/read_byte
2020-02-17 16:39:44,137 # [SUCCESS] at24cxxx_write
2020-02-17 16:39:44,140 # [SUCCESS] at24cxxx_read
2020-02-17 16:39:44,144 # [FAILURE] write/read: (4FREE != BEER4FREE)
at24mac.patch
diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep
index 423319b245..0b60b0e52c 100644
--- a/drivers/Makefile.dep
+++ b/drivers/Makefile.dep
@@ -36,16 +36,12 @@ ifneq (,$(filter at,$(USEMODULE)))
   USEMODULE += isrpipe_read_timeout
 endif
 
-ifneq (,$(filter at24c%,$(USEMODULE)))
+ifneq (,$(filter at24mac at24c%,$(USEMODULE)))
   FEATURES_REQUIRED += periph_i2c
   USEMODULE += xtimer
   USEMODULE += at24cxxx
 endif
 
-ifneq (,$(filter at24mac,$(USEMODULE)))
-  FEATURES_REQUIRED += periph_i2c
-endif
-
 ifneq (,$(filter at30tse75x,$(USEMODULE)))
   USEMODULE += xtimer
   FEATURES_REQUIRED += periph_i2c
diff --git a/drivers/at24cxxx/include/at24cxxx_defines.h b/drivers/at24cxxx/include/at24cxxx_defines.h
index 312ba9d50d..4d2c0e715a 100644
--- a/drivers/at24cxxx/include/at24cxxx_defines.h
+++ b/drivers/at24cxxx/include/at24cxxx_defines.h
@@ -299,6 +299,29 @@ extern "C" {
                                          / AT24CXXX_POLL_DELAY_US))
 /** @} */
 
+/**
+ * @name AT24MAC402/602 constants
+ * @{
+ */
+/**
+ * @brief   2048 byte memory
+ */
+#define AT24MAC_EEPROM_SIZE             (2048U)
+/**
+ * @brief   256 pages of 8 bytes
+ */
+#define AT24MAC_PAGE_SIZE               (8U)
+/**
+ * @brief   Delay to complete write operation
+ */
+#define AT24MAC_PAGE_WRITE_DELAY_US     (5000U)
+/**
+ * @brief   Number of poll attempts
+ */
+#define AT24MAC_MAX_POLLS               (1 + (AT24MAC_PAGE_WRITE_DELAY_US \
+                                         / AT24CXXX_POLL_DELAY_US))
+/** @} */
+
 /**
  * @name Set constants depending on module
  * @{
@@ -347,6 +370,10 @@ extern "C" {
 #define AT24CXXX_EEPROM_SIZE            (AT24C01A_EEPROM_SIZE)
 #define AT24CXXX_PAGE_SIZE              (AT24C01A_PAGE_SIZE)
 #define AT24CXXX_MAX_POLLS              (AT24C01A_MAX_POLLS)
+#elif IS_USED(MODULE_AT24MAC)
+#define AT24CXXX_EEPROM_SIZE            (AT24MAC_EEPROM_SIZE)
+#define AT24CXXX_PAGE_SIZE              (AT24MAC_PAGE_SIZE)
+#define AT24CXXX_MAX_POLLS              (AT24MAC_MAX_POLLS)
 #else /* minimal */
 #define AT24CXXX_EEPROM_SIZE            (128U)  /**< EEPROM size */
 #define AT24CXXX_PAGE_SIZE              (4U)    /**< page size */
same54-xpro.patch
diff --git a/boards/same54-xpro/include/board.h b/boards/same54-xpro/include/board.h
index cde35f1a69..a80f6ebde3 100644
--- a/boards/same54-xpro/include/board.h
+++ b/boards/same54-xpro/include/board.h
@@ -33,6 +33,7 @@ extern "C" {
 #define AT24MAC_PARAM_I2C_DEV   I2C_DEV(0)
 #define AT24MAC_PARAM_I2C_ADDR  (0x5E)
 #define AT24MAC_PARAM_TYPE      AT24MAC4XX
+#define AT24CXXX_PARAM_ADDR     (0x56)
 /** @} */
 
 /**

@fabian18
Copy link
Contributor Author

fabian18 commented Feb 17, 2020

The AT24MAC402 has 2048 Kbit (not Kbyte).
And if I understand correctly, according to the datasheet (p. 8) the memory layout is 16 pages of 16 bytes each. Only the positions ([0] - [127], [144] - [151], [A0] - [FF]) are R/W.
The test writes at position 444, which is out of bounds but it does not complain because you specified the EEPROM size with 2048 bytes.

So maybe try:

/**
 * @name AT24MAC402/602 constants
 * @{
 */
/**
 * @brief   256 byte memory
 */
#define AT24MAC_EEPROM_SIZE             (256U)
/**
 * @brief   16 pages of 16 bytes
 */
#define AT24MAC_PAGE_SIZE               (16U)
/**
 * @brief   Delay to complete write operation
 */
#define AT24MAC_PAGE_WRITE_DELAY_US     (5000U)
/**
 * @brief   Number of poll attempts
 */
#define AT24MAC_MAX_POLLS               (1 + (AT24MAC_PAGE_WRITE_DELAY_US \
                                         / AT24CXXX_POLL_DELAY_US))
/** @} */

And change the positions in the test so that you stay in the memory range.

@fabian18
Copy link
Contributor Author

Should I make the positions in the tests configurable too?
Like...

#ifndef WRITE_POSITION
#define WRITE_POSITION (444U)
#endif

@benpicco
Copy link
Contributor

benpicco commented Feb 17, 2020

The AT24MAC402 has 2048 Kbit (not Kbyte).
And if I understand correctly, according to the datasheet (p. 8) the memory layout is 16 pages of 16

You are of course right! Now with that and AT24CDEV also changed to at24mac the test is passing.

Should I make the positions in the tests configurable too?

No - we know the parameters of the EEPROM at compile time, so select some good values for testing, e.g.

#define WRITE_POSITION          (AT24CXXX_EEPROM_SIZE - 3 * AT24CXXX_PAGE_SIZE - 4)

will make sure that we always test writing over the page boundary.

@fabian18
Copy link
Contributor Author

@benpicco Could you please test again? Then I can squash, make Murdock green and maybe we can finally finish this?

@benpicco
Copy link
Contributor

Tried it again, works as expected - please squash.

same54-xpro with at24mac
2020-02-24 11:12:19,109 # Starting tests for module at24cxxx
2020-02-24 11:12:19,110 # EEPROM size: 256 byte
2020-02-24 11:12:19,112 # Page size  : 16 byte
2020-02-24 11:12:19,114 # [SUCCESS] at24cxxx_init
2020-02-24 11:12:19,121 # [SUCCESS] at24cxxx_write_byte
2020-02-24 11:12:19,122 # [SUCCESS] at24cxxx_read_byte
2020-02-24 11:12:19,123 # [SUCCESS] write_byte/read_byte
2020-02-24 11:12:19,128 # [SUCCESS] at24cxxx_write
2020-02-24 11:12:19,136 # [SUCCESS] at24cxxx_read
2020-02-24 11:12:19,137 # [SUCCESS] write/read
2020-02-24 11:12:19,142 # [SUCCESS] at24cxxx_set
2020-02-24 11:12:19,146 # [SUCCESS] set/read
2020-02-24 11:12:19,149 # Finished tests for module at24cxxx

@fabian18 fabian18 force-pushed the driver-at24cxxx-eeprom branch from c4d65c9 to 11f8c05 Compare February 24, 2020 10:59
Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good and works.
We can adapt this to the common EEPROM API later - and finalizing the API might be easier with some existing code to make use of it.

@benpicco
Copy link
Contributor

Mind giving #11945 a look?

@fabian18
Copy link
Contributor Author

I will look at #11945 but I cannot test because I don´t have an SPI EEPROM.

@fabian18
Copy link
Contributor Author

What´s wrong with Murdock?:
--- FAILED build outputs: --- build output of app tests/thread_flags for board native:gnu (raw, runtime=15.6s):
I don´t think I touched this.

@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Feb 24, 2020
@fabian18 fabian18 force-pushed the driver-at24cxxx-eeprom branch from 11f8c05 to 07f7d81 Compare February 24, 2020 13:01
@kaspar030 kaspar030 removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 24, 2020
@kaspar030
Copy link
Contributor

Pausing for #13456

@kaspar030 kaspar030 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 24, 2020
@benpicco benpicco dismissed maribu’s stale review February 25, 2020 14:05

Comments have been addressed.

@benpicco benpicco merged commit aa1c23d into RIOT-OS:master Feb 25, 2020
@fabian18
Copy link
Contributor Author

Thank you @benpicco ☺️

@maribu
Copy link
Member

maribu commented Feb 25, 2020

Nice :-) @fabian18: Thanks for your contribution and @benpicco thanks for reviewing!

@fabian18 fabian18 deleted the driver-at24cxxx-eeprom branch March 2, 2020 11:41
@leandrolanzieri leandrolanzieri added this to the Release 2020.04 milestone Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: drivers Area: Device drivers CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants