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

drivers/mfrc522: add new driver #16782

Merged
merged 2 commits into from
Feb 1, 2023
Merged

Conversation

HendrikVE
Copy link
Contributor

Contribution description

This PR adds support for the MFRC522. It is quite common in the Arduino world and it is quite cheap. The driver connects to the MFRC522 via SPI and is heavily based on the Arduino driver available here. Basically it was ported, but with several improvements in readability and documentation.

Testing procedure

The given (manual) test provides single commands for some driver functions.

@github-actions github-actions bot added Area: doc Area: Documentation Area: drivers Area: Device drivers Area: tests Area: tests and testing framework labels Aug 27, 2021
@HendrikVE HendrikVE added the Type: new feature The issue requests / The PR implemements a new feature for RIOT label Aug 27, 2021
@HendrikVE HendrikVE changed the title Pr/driver/mfrc522 drivers/mfrc522: add new driver Aug 27, 2021
Copy link
Contributor

@fjmolinas fjmolinas left a comment

Choose a reason for hiding this comment

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

Thanks, for this, sole initial comments, can you provide some testing output?

* @param[in] rx_align Defines the bit position in back_data[0] for the first bit received
* @param[in] check_crc True => The last two bytes of the response is assumed to be a CRC_A that must be validated
*
* @return MFRC522_STATUS_OK on success, MFRC522_STATUS_??? otherwise
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use ERROR codes instead according to doc.riot-os.org/driver-guide.html?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure whether I understand the doc correctly. Is there a errno.h where I should take these error codes from or should I declare them by myself but using the given naming scheme (and without an enum)? I doubt there is already a error code for a wrong crc value for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added error codes, namingly -EINVAL and -ENOTSUP to the device_read and device_write functions (they are just passed on from spi).
From the documentation: MUST: all error and status return codes are named, e.g. DEVAB_OK, DEVAB_NOSPI, DEVAB_OVERFLOW, ... I'd say mfrc522_status_code_t is following the rules. I really can't press status codes like MFRC522_STATUS_COLLISION or MFRC522_STATUS_CRC_WRONG into generic error codes. Especially because MFRC522_STATUS_COLLISION is used within the driver.

drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/mfrc522/Makefile.dep Outdated Show resolved Hide resolved
drivers/mfrc522/mfrc522.c Outdated Show resolved Hide resolved
@HendrikVE
Copy link
Contributor Author

Here is some shell output:

2021-09-14 17:20:14,977 # help
2021-09-14 17:20:14,980 # Command              Description
2021-09-14 17:20:14,983 # ---------------------------------------
2021-09-14 17:20:14,986 # reset                Reset the device.
2021-09-14 17:20:14,989 # antenna_on           Turn the antenna on.
2021-09-14 17:20:14,993 # antenna_off          Turn the antenna off.
2021-09-14 17:20:14,996 # set_antenna_gain     Set antenna gain.
2021-09-14 17:20:15,000 # get_antenna_gain     Get antenna gain.
2021-09-14 17:20:15,004 # firmware_version     Print the firmware version.
2021-09-14 17:20:15,009 # picc_dump            Dumps debug info about a scanned PICC.
2021-09-14 17:20:15,015 # scan_uid             Scan repeatedly until a PICC's UID is scanned.
2021-09-14 17:20:15,020 # set_uid              Set the UID of a scanned PICC.
2021-09-14 17:20:15,023 # self_test            Run the self test.
> firmware_version
2021-09-14 17:20:22,460 # firmware_version
2021-09-14 17:20:22,461 # 0x92 = v2.0
> scan_uid
2021-09-14 17:20:30,443 # scan_uid
2021-09-14 17:20:30,467 # No card detected ...
2021-09-14 17:20:30,968 # Repeat scan (2/20) ...
2021-09-14 17:20:30,993 # No card detected ...
2021-09-14 17:20:31,493 # Repeat scan (3/20) ...
2021-09-14 17:20:31,519 # No card detected ...
2021-09-14 17:20:32,019 # Repeat scan (4/20) ...
2021-09-14 17:20:32,022 # Card detected. Read card serial ...
2021-09-14 17:20:32,025 # Scanned PICC's UID: da4ad347
> picc_dump
2021-09-14 17:20:41,805 # picc_dump
2021-09-14 17:20:41,807 # Card detected. Read card serial ...
2021-09-14 17:20:41,810 # Scanned PICC's UID: da4ad347
2021-09-14 17:20:41,812 # Card UID: da 4a d3 47
2021-09-14 17:20:41,813 # Card SAK: 08
2021-09-14 17:20:41,815 # PICC type: MIFARE 1KB
2021-09-14 17:20:41,822 # Sector Block   0  1  2  3   4  5  6  7   8  9 10 11  12 13 14 15  AccessBits
2021-09-14 17:20:41,829 #   15     63   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:41,835 #          62   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,842 #          61   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,849 #          60   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,855 #   14     59   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:41,861 #          58   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,869 #          57   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,875 #          56   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,882 #   13     55   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:41,889 #          54   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,895 #          53   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,902 #          52   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,909 #   12     51   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:41,915 #          50   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,922 #          49   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,929 #          48   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,935 #   11     47   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:41,942 #          46   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,949 #          45   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,955 #          44   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,962 #   10     43   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:41,969 #          42   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,976 #          41   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,982 #          40   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:41,989 #    9     39   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:41,996 #          38   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,002 #          37   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,009 #          36   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,016 #    8     35   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,022 #          34   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,029 #          33   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,036 #          32   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,043 #    7     31   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,049 #          30   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,056 #          29   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,062 #          28   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,069 #    6     27   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,076 #          26   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,083 #          25   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,089 #          24   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,096 #    5     23   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,103 #          22   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,109 #          21   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,116 #          20   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,123 #    4     19   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,129 #          18   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,136 #          17   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,143 #          16   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,149 #    3     15   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,156 #          14   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,163 #          13   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,169 #          12   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,176 #    2     11   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,182 #          10   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,190 #           9   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,196 #           8   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,203 #    1      7   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,210 #           6   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,216 #           5   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,223 #           4   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,230 #    0      3   00 00 00 00  00 00 ff 07  80 69 ff ff  ff ff ff ff  [ 0 0 1 ] 
2021-09-14 17:20:42,236 #           2   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,243 #           1   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
2021-09-14 17:20:42,249 #           0   da 4a d3 47  04 08 04 00  62 63 64 65  66 67 68 69  [ 0 0 0 ] 
2021-09-14 17:20:42,264 # 
> self_test
2021-09-14 17:20:48,808 # self_test
2021-09-14 17:20:48,859 # SUCCESS
scan_uid
2021-09-14 17:21:22,557 # scan_uid
2021-09-14 17:21:22,560 # Card detected. Read card serial ...
2021-09-14 17:21:22,562 # Scanned PICC's UID: 880ae4b
> scan_uid
2021-09-14 17:21:25,611 # scan_uid
2021-09-14 17:21:25,614 # Card detected. Read card serial ...
2021-09-14 17:21:25,617 # Scanned PICC's UID: da4ad347
> scan_uid
2021-09-14 17:21:29,644 # scan_uid
2021-09-14 17:21:29,647 # Card detected. Read card serial ...
2021-09-14 17:21:29,650 # Scanned PICC's UID: 25c6f52a

@benpicco benpicco requested a review from fjmolinas October 14, 2021 21:42
@HendrikVE HendrikVE added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 14, 2022
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.

Thanks for your PR. I very interested in this driver for a personal project that I never found enough time for. I hope I can give you an detailed review next week.

Please check the RIOT driver programming guide that @fjmolinas kindly provided a link for. Also, there are a couple of automatic checks that run in the CI that check e.g. for tabs being used instead of spaces, multiple empty lines, etc. You can run those locally also with make static-test, but result might slightly differ when the versions of the tools used locally and in CI differ.

*
* @return MFRC522_STATUS_OK on success, MFRC522_STATUS_??? otherwise
*/
mfrc522_status_code_t mfrc522_pcd_transceive_data(mfrc522_t *dev,
Copy link
Member

Choose a reason for hiding this comment

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

Please use int as return type and 0 for success. For error codes please use negative errno codes. Refer to the driver guide for reasoning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've read the documentation but I don't quite see how this is supposed to be compatible with some of these return values.

  • Whenever you implement status/error return values in your driver, magic numbers MUST NOT be used.

mfrc522_status_code_t are not magic numbers imo. A magic number would be to return a hardcoded -1, but the enum's members are well documented. To improve things I could replace the MFRC522_STATUS_??? otherwise-part of most functions by explicit naming of the possible return values to fulfill document every single error code that can be return for each function. The doc wants errno but does not give a reasoning for it as far as I can see.

Take MFRC522_STATUS_COLLISION for example. This does not perfectly fit into the scheme of classical errno numbers. Furthermore it is used internally by mfrc522_picc_select and it explicitly checks for the return of mfrc522_pcd_transceive_data which can be MFRC522_STATUS_COLLISION to do some collision specific handling within the driver. Using generic errno codes here does not only remove the context of the error and thus loosing important information for error handling about the result, it also could lead to problems once the same errno is used somewhere else where it does not mean collision but maybe something different. Yes, latter might be prevented by the doc attached to retval saying in which situation a given errno will be used exclusively, but that is still not a nice solution in my opinion. The only way I see at the moment to have both, internal status codes and errno codes for the user, would be to use a facade of functions which maps internal status codes to errno codes and make those functions with status code for internal use only.
After a quick scan of some random header files from drivers/include/ there seem to be way more drivers using custom error codes than doing the opposite, so I wonder how young this guideline is?

Copy link
Member

@maribu maribu Jan 16, 2022

Choose a reason for hiding this comment

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

The doc wants errno but does not give a reasoning for it as far as I can see.

Indeed, the reasoning is only given in the PR: #15018 Likely this also makes sense, as the driver guide purpose is more being a guideline than a reasoning of design decisions.

After a quick scan of some random header files from drivers/include/ there seem to be way more drivers using custom error codes than doing the opposite, so I wonder how young this guideline is?

September 2020, but I don't see how this is relevant. New drivers need to use errno codes, and some old APIs already have been converted. The rest will follow eventually.

Copy link
Contributor Author

@HendrikVE HendrikVE Jan 16, 2022

Choose a reason for hiding this comment

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

Also under the section Additional Sensor Driver Checklist stands MUST: all error and return values are named, e.g. DEVAB_OK, DEVAB_NODEV, .... I'm following this rule with returning values like MFRC522_STATUS_COLLISION. This driver is not really for a typical sensor, but also not that different in its way of working.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is also stated in General Device Driver Checklist:

MUST: all error and status return codes are named, e.g. DEVAB_OK, DEVAB_NOSPI, DEVAB_OVERFLOW, ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I missed the discussion about it in the matrix room but to me it feels like a usecase like this here might have been overlooked? I understand that it is desirable to have a unified result space for all drivers, but it also needs a place for special return values that are unique to a specific driver. In #15018 is stated:

The error classes that device driver have to report are largely the same

"Largely the same" is correct. Some return values simply don't fit perfectly into the scheme.

A lot of code within RIOT and in external code is doing so already. Applying this consistently allows passing error codes through, rather than translating between errno codes and custom enum values

The only difference here would be that I would need to translate it within the driver space instead of the application developer deciding.

Copy link
Member

Choose a reason for hiding this comment

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

Note that while it would be super if applications would do proper error handling. But the reality is different. The majority uses the ostrich algorithm for error handling.

And they do have at least some merit (not at completely abandoning error handling, but at shifting most of the responsibility of error handling to the driver): The developer who is most capable to do error handling is the author of the driver, and the place to best do it is in the driver, as this is where most info about the device and driver state is freely available. Also, it dedulicates code if it is done at one place in the driver. So, IMO all errors that sensibly can be handled in the driver, should be handled there. (Usually that statement is uncontroversial, but what "sensible" is can be quite controversal from time to time.)

So errors that are reported back to the applications are those that cannot be sensible handled to the driver. And for applications it doesn't matter that much what exactly input/output error EIO exactly refers to.

In fact, I would be extremely happy if every application would consistently check every return value for a potential error (where applicable) and at least do some least effort recovery, such as three times resetting the driver and resetting the system if there were no "progress" after each and every of the three resets of the driver. And ideally the application would give proper info to aid debugging (being able to use strerror() makes that more likely to happen.) But most firmwares I have seen either ignore errors altogether, or treat it like being struck by a lighting and just melt down.

In internal APIs (please don't put them in the header in drivers/include but in driver/<module_name>/include ) there is much higher freedom, as you don't write this APIs to best match the needs of applications but rather what your driver needs. If you cannot reasonably map errors to errnos there, than don't.

Copy link
Contributor Author

@HendrikVE HendrikVE Jan 20, 2022

Choose a reason for hiding this comment

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

The point is that it is not as easy to decide which function is internal as one might think. I took the time to sweep over the relations of the functions and following are the results of it:

Functions with no internal use. We could map to errno within these functions.
  • mfrc522_pcd_init
  • mfrc522_pcd_antenna_off
  • mfrc522_pcd_set_antenna_gain
  • mfrc522_pcd_soft_power_down
  • mfrc522_pcd_soft_power_up
  • mfrc522_mifare_ultralight_write
  • mfrc522_mifare_decrement
  • mfrc522_mifare_increment
  • mfrc522_mifare_restore
  • mfrc522_mifare_transfer
  • mfrc522_mifare_get_value
  • mfrc522_mifare_set_value
  • mfrc522_pcd_ntag216_auth
  • mfrc522_mifare_set_uid
  • mfrc522_mifare_unbrick_uid_sector
Functions with internal use. Some of these could map to errno, but not all of them (see further below for examples)
  • mfrc522_pcd_calculate_crc
  • mfrc522_pcd_transceive_data
  • mfrc522_pcd_communicate_with_picc
  • mfrc522_picc_request_a
  • mfrc522_picc_wakeup_a
  • mfrc522_picc_reqa_or_wupa
  • mfrc522_picc_select
  • mfrc522_picc_halt_a
  • mfrc522_pcd_authenticate
  • mfrc522_mifare_read
  • mfrc522_mifare_write
  • mfrc522_pcd_mifare_transceive
  • mfrc522_mifare_open_uid_backdoor
  • mfrc522_picc_is_new_card_present
  • mfrc522_picc_read_card_serial
Use does not matter (no mfrc522_status_code_t return value)
  • mfrc522_pcd_reset
  • mfrc522_pcd_antenna_on
  • mfrc522_pcd_get_antenna_gain
  • mfrc522_pcd_stop_crypto1
  • mfrc522_pcd_dump_version_to_serial
  • mfrc522_picc_dump_to_serial
  • mfrc522_picc_dump_details_to_serial
  • mfrc522_picc_dump_mifare_classic_to_serial
  • mfrc522_picc_dump_mifare_classic_sector_to_serial
  • mfrc522_picc_dump_mifare_ultralight_to_serial
  • mfrc522_pcd_perform_self_test
Pair of functions [returning | checking] MFRC522_STATUS_COLLISION
  • [mfrc522_pcd_transceive_data | mfrc522_picc_select]
  • [frc522_picc_request_a | mfrc522_picc_is_new_card_present]
Functions [returning | checking] MFRC522_STATUS_TIMEOUT
  • [mfrc522_pcd_transceive_data | mfrc522_picc_halt_a]
  • [mfrc522_pcd_communicate_with_picc | mfrc522_pcd_mifare_transceive]
  • [mfrc522_pcd_authenticate | mfrc522_mifare_set_uid]

So if we want to have the functions returning mfrc522_status_code_t to be public (thus returning errno), we would need to write a wrapper for these which maps to errno for external use, while using the original only internally.
Alternatively we replace all status codes by errno (also internally). We replace MFRC522_STATUS_TIMEOUT with ETIMEDOUT and MFRC522_STATUS_COLLISION with whatever value, I really don't have a good candidate for it. And in this process we might combine several errors in to one single errno, because others wont fit to the context of the error. But just because e.g. MFRC522_STATUS_CRC_WRONG is only returned but currently not explicitly checked does not mean it will never be the case.

Copy link
Contributor Author

@HendrikVE HendrikVE Jan 27, 2022

Choose a reason for hiding this comment

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

I added a fixup commit which tries to map to errno. Disadvantages for the driver are:

  • we lose all context information about MFRC522_STATUS_MIFARE_NACK as it is mapped to generic -EIO
  • we lose all context information about MFRC522_STATUS_CRC_WRONG as it is mapped to generic -EIO
  • MFRC522_STATUS_COLLISION was replaced by -ECONNABORTED which does not really fit but comes the closest I think

I don't want to mix status codes and errno, nor do I want to add wrapper functions, so I believe this is the best solution under the given circumstances.

drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
@HendrikVE
Copy link
Contributor Author

Thank you for the future review on my PR btw :) If you want I can already squash my commits to make it easier for you to review?

@maribu
Copy link
Member

maribu commented Jan 16, 2022

From my point of view, you can squash at will. Since github essentially runs a git diff between the current state and that of the last review, I personally don't look at fixup commits anymore. So I don't mind squashing at any point in time.

Copy link
Contributor

@leandrolanzieri leandrolanzieri left a comment

Choose a reason for hiding this comment

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

There seems to be some functions where we could have const, I pointed out what I found but there may be more.

drivers/include/mfrc522.h Show resolved Hide resolved
drivers/include/mfrc522.h Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/include/mfrc522.h Outdated Show resolved Hide resolved
drivers/mfrc522/Makefile.dep Show resolved Hide resolved
drivers/mfrc522/mfrc522.c Outdated Show resolved Hide resolved
drivers/mfrc522/mfrc522.c Show resolved Hide resolved
@github-actions github-actions bot added the Area: Kconfig Area: Kconfig integration label Jan 20, 2022
@HendrikVE
Copy link
Contributor Author

I added a fixup commit which tries to map to errno. Disadvantages for the driver are:

  • we lose all context information about MFRC522_STATUS_MIFARE_NACK as it is mapped to generic -EIO
  • we lose all context information about MFRC522_STATUS_CRC_WRONG as it is mapped to generic -EIO
  • MFRC522_STATUS_COLLISION was replaced by -ECONNABORTED which does not really fit but comes the closest I think

@maribu @fjmolinas Are you ok with the changes in return codes provided by the fixup commit?

@stale
Copy link

stale bot commented Nov 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions.

@stale stale bot added the State: stale State: The issue / PR has no activity for >185 days label Nov 2, 2022
@HendrikVE HendrikVE added State: don't stale State: Tell state-bot to ignore this issue and removed State: stale State: The issue / PR has no activity for >185 days labels Nov 2, 2022
@riot-ci
Copy link

riot-ci commented Jan 31, 2023

Murdock results

✔️ PASSED

dcc8edd tests/driver_mfrc522: add test for mfrc522 driver

Success Failures Total Runtime
6807 0 6807 09m:17s

Artifacts

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.

looks good to me

@benpicco
Copy link
Contributor

bors merge

@bors
Copy link
Contributor

bors bot commented Feb 1, 2023

Build succeeded:

@bors bors bot merged commit 718e4a8 into RIOT-OS:master Feb 1, 2023
@HendrikVE
Copy link
Contributor Author

Thank you all!

@HendrikVE HendrikVE deleted the pr/driver/mfrc522 branch February 1, 2023 17:01
@MrKevinWeiss MrKevinWeiss added this to the Release 2023.04 milestone Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: doc Area: Documentation Area: drivers Area: Device drivers Area: Kconfig Area: Kconfig integration Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR State: don't stale State: Tell state-bot to ignore this issue 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.

7 participants