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

cpu/atmega_common: Fix atmega_port_addr() #18265

Merged
merged 1 commit into from
Jun 28, 2022

Conversation

maribu
Copy link
Member

@maribu maribu commented Jun 27, 2022

Contribution description

In 04ab5a7 a bug was introduced in
the calculation of the GPIO port address by refactoring code. This
fixes the issue by extracting the GPIO port first from the pin.

Testing procedure

See #18245 (comment)

Issues/PRs references

Reported in #18245 (comment)

@maribu maribu added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Platform: AVR Platform: This PR/issue effects AVR-based platforms labels Jun 27, 2022
@maribu maribu requested a review from kYc0o as a code owner June 27, 2022 14:05
@github-actions github-actions bot added the Area: cpu Area: CPU/MCU ports label Jun 27, 2022
@maribu
Copy link
Member Author

maribu commented Jun 27, 2022

@valentinpi: Care to test?

@krzysztof-cabaj
Copy link
Contributor

Hi! I confirm that now everything works well!

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jun 27, 2022
@maribu
Copy link
Member Author

maribu commented Jun 27, 2022

It seems that the compiler's optimizer trips over the volatile qualifier in the GPIO struct, despite the struct only being used to calculate address offsets. The new version has the same .text size as the old gpio_init() (the only difference to the stable branch left is from a change in assert()).

For me, the code still works. Care to test it again?

@maribu maribu removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jun 27, 2022
@maribu
Copy link
Member Author

maribu commented Jun 27, 2022

For reference: This models the memory layout of the three registers per GPIO port:

/**
* @brief Structure describing the memory layout of the registers of a
* GPIO port on ATmega MCUs.
*/
typedef struct {
/**
* @brief Toggle bits in the port register
* @note The bits in the port register will be also toggled for inputs.
* This can be both a footgun as well as an efficient way to toggle
* the pull up resistor on inputs
*
* Referred to as "Input Pins Address" in the datasheet.
*/
volatile uint8_t pin;
/**
* @brief Configure pins as output (1) or input (0) using the Data
* Direction Register
*/
volatile uint8_t ddr;
/**
* @brief Read/write the state of GPIO pins using the Port Data Register
*
* @note When in input mode (see @ref atmega_gpio_port_t::ddr) writing a
* 1 will enable the pull up resistor, writing a 0 will put the
* pin in floating mode.
*/
volatile uint8_t port;
} atmega_gpio_port_t;

That is why I reordered the functions now to match the order they come in memory. Interestingly, the datasheet lists the registers in reverse order (in regard to their address) as well:

excerpt from ATmega328p datasheet showing GPIO regs

@valentinpi
Copy link

The current version is working for me.

@krzysztof-cabaj
Copy link
Contributor

The current version is working for me.

For me too. This probably resolve these BUGs.

@benpicco
Copy link
Contributor

So then squash to get this through CI

In 04ab5a7 a bug was introduced in
the calculation of the GPIO port address by refactoring code. This
fixes the issue by extracting the GPIO port first from the pin.
@maribu maribu force-pushed the cpu/atmega_common/atmega_gpio branch from cb661e2 to b72cafb Compare June 27, 2022 20:16
@maribu maribu added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jun 27, 2022
@maribu maribu enabled auto-merge June 27, 2022 20:16
@maribu maribu merged commit 1d25475 into RIOT-OS:master Jun 28, 2022
@maribu maribu deleted the cpu/atmega_common/atmega_gpio branch June 28, 2022 07:39
@maribu
Copy link
Member Author

maribu commented Jun 28, 2022

@krzysztof-cabaj and @valentinpi: Thanks for reporting the issue and testing!

@benpicco: Thx for the quick review :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: cpu Area: CPU/MCU ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: AVR Platform: This PR/issue effects AVR-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants