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

[gpio] Refactor SAM signals API #482

Merged
merged 2 commits into from
Oct 7, 2020

Conversation

henrikssn
Copy link
Contributor

Goals:

  • Compilaton errors are understandable (contain necessary information about the context that lead to the error).
  • Completely in C++ templates, allows peripherals to perform further validation.
  • Allows the user to select the intended peripheral signal (e.g. TX, SCK, MOSI, SDA)
  • Allows peripherals to take arguments in arbitrary order (my gift to @salkinium )
  • Deduces the pin function at compile time
  • Deduces the Peripheral pad at compile time (e.g. Sercom Pad)
  • As close to the current signals API as possible

Fixes #481.

@salkinium
Copy link
Member

salkinium commented Sep 24, 2020

This looks great on first viewing! The STM32 API was still C++11 based, which didn't have variadic templates, so we generated everything with Python. I've always been busy fixing lbuild/modm-devices/tooling stuff that I don't have enough time to properly engage on the C++17 API front. There's a lot more to improve.

I'm still busy with TinyUSB and then I'd like to review/merge your OTA code, so I'll check this out, but probably not fast.

PS: Please fix your formatting style, or I'm going to have a hard time reviewing this.

@henrikssn
Copy link
Contributor Author

This looks is great on first viewing! The STM32 API was still C++11 based, which didn't have variadic templates, so we generated everything with Python. I've always been busy fixing lbuild/modm-devices/tooling stuff that I don't have enough time to properly engage on the C++17 API front. There's a lot more to improve.

I'm still busy with TinyUSB and then I'd like to review/merge your OTA code, so I'll check this out, but probably not fast.

Yeah I learned a lot about C++17 while doing this, which was my main motivation. It believe it should be fairly straight-forward to make the STM32 API follow the pattern, but I'd like to make that a separate PR.

The OTA code is still WIP and not ready to merge, I would recommend you start here and then look at the fiber PR.

PS: Please fix your formatting style, or I'm going to have a hard time reviewing this.

Fixed, I also added the .clang-format file which I used so that I can stay more consistent in the future.

PS: How I dislike those hard tabs...

Copy link
Member

@salkinium salkinium left a comment

Choose a reason for hiding this comment

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

I like this very, very much! It's so much cleaner and much faster to generate/compile than the STM32 solution, which generates 200+ files just for the GPIOs, which is just dumb.

I added a closer-to-modm-style clang format, but I guess we're going to have to use // clang-format off for some of the short function stuff. You still have to set hardtabs in your editor for the template stuff. Theres a .editorconfig for that purpose, it's supposed to be editor independent.

I would still like to see an alias for using GpioA00 = Gpio<A00>; for backwards compatiblity? Or perhaps just for STM32? I'm not sure I like having a A00 type floating around globally (due to using namespace modm::platform;), so perhaps it should be using GpioA00 = Gpio<GpioConfigA00>;?

using LoggerDevice = modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull >;
using Leds = SoftwareGpioPort< Led >;
using LoggerDevice = modm::IODeviceWrapper<Uart0, modm::IOBuffer::BlockIfFull>;
using Leds = Led;
Copy link
Member

Choose a reason for hiding this comment

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

This does not have the same semantics. Leds::write(uint32_t) accepts a bit vector, Led::write(bool) only accepts a single bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but a bit vector is implicitly convertible to a bool, so it should be ok in all cases.

Here is the line I am trying to make it work with:

Board::Leds::write(1);

Is it expected that it only will blink the first led?

Copy link
Member

Choose a reason for hiding this comment

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

What is the benefit of removing the SoftwareGpioPort<> here?
I would like to keep it in order to have the same board.hpp files for further automation and testing in the future (#359).

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 wasn't implemented on SAM properly and the new Gpio<> is drop-in compatible with a SoftwareGpioPort<> that only contains one port (which is the case for all SAM boards at the moment).

Maybe we can move the SoftwareGpioPort<> to a common library after stm32/avr supports the same signals API?

src/modm/board/feather_m0/board.hpp Outdated Show resolved Hide resolved
src/modm/board/feather_m0/board.hpp Outdated Show resolved Hide resolved
src/modm/platform/gpio/sam/config.hpp.in Outdated Show resolved Hide resolved
src/modm/platform/gpio/sam/pin.hpp Show resolved Hide resolved
Copy link
Contributor Author

@henrikssn henrikssn left a comment

Choose a reason for hiding this comment

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

I like this very, very much! It's so much cleaner and much faster to generate/compile than the STM32 solution, which generates 200+ files just for the GPIOs, which is just dumb.

Thanks for the review!

I added a closer-to-modm-style clang format, but I guess we're going to have to use // clang-format off for some of the short function stuff. You still have to set hardtabs in your editor for the template stuff. Theres a .editorconfig for that purpose, it's supposed to be editor independent.

I disagree your clang-format is substantially closer to modm-style than the one I added. I force-pushed the branch back to where it originally was, in the future, please make changes in commits so that we can review the differences. I spent multiple hours aligning it with the modm style so it would be good to compare our efforts...

Also feel free to comment on the style in this PR, then we can see how we can fix that in the clang-format file.

I used this command to assess quality of the clang-format file: find src/modm | grep -e "\.\(c\|h\|hpp\|cpp\)\$" | xargs clang-format -n 2>&1 | wc -l (lower is better)

Your config: 143759
My config: 143800

Note that my config does allow the short functions, so no need for the // clang-format off.

I would still like to see an alias for using GpioA00 = Gpio<A00>; for backwards compatiblity? Or perhaps just for STM32?

I'm fine with adding the using declarations, but it should be a separate templated header file (which we could probably share between the platforms).

I'm not sure I like having a A00 type floating around globally (due to using namespace modm::platform;), so perhaps it should be using GpioA00 = Gpio<GpioConfigA00>;

How about PinA01 then? GpioConfigA00 is too long for my taste.

src/modm/board/feather_m0/board.hpp Outdated Show resolved Hide resolved

%% endfor

} // namespace modm::platform
Copy link
Member

Choose a reason for hiding this comment

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

This file needs to be converted to tabs and the { put on a newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Braces done, I still need to figure out how to configure my editor (VSCode) for properly emitting tabs

Copy link
Member

Choose a reason for hiding this comment

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

I've added a .editorconfig to make GitHub render tabs as 4 spaces. Perhaps you can use the same mechanism. Something like this perhaps:
https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig

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 tried it but it makes it impossible to edit the python files, as there we use spaces and python considers the difference important

Copy link
Member

@salkinium salkinium Oct 5, 2020

Choose a reason for hiding this comment

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

Then we need to add a filter for python files to the config.

Copy link
Member

Choose a reason for hiding this comment

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

Try adding this to .editorconfig, that should make the tabs go away for the Python files.

[*.{lb,py}]
indent_style = space
indent_size = 4

examples/samd/interrupt/main.cpp Outdated Show resolved Hide resolved
@salkinium
Copy link
Member

I'm currently super busy with manufacturing 100 railway signals, so I'm AFK for a few weeks.
If you can fix the { on the newline, then I'm happy to merge this.

Copy link
Member

@salkinium salkinium left a comment

Choose a reason for hiding this comment

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

Need to make the CI pass and rebase and squash onto develop please.

@salkinium
Copy link
Member

Perhaps using clang format is ok on new files, but we're not reformatting entire swathes of modm, just because one line changes. I'm not wasting my free time fighting an autoformatter.

@salkinium
Copy link
Member

salkinium commented Oct 5, 2020

I'm sorry I'm exploding at you, but these are exactly the same issues that I first encountered ~3 years ago when investigating using clang format for modm (during the porting from xpcc) and it's very frustating to see all the failing corner cases. I like the idea, but I'm going to have to deal with the fallout of this decision for many, many years going forward and I don't want to burn out on this project from preventable causes.

@salkinium
Copy link
Member

salkinium commented Oct 5, 2020

And why did the STM32 CMSIS header and FreeRTOS submodules change?

@salkinium
Copy link
Member

*challening the inner grumpy old maintainer* get off my lawn!1!!

@henrikssn
Copy link
Contributor Author

Rebased + fixed CI

@henrikssn
Copy link
Contributor Author

I'm sorry I'm exploding at you, but these are exactly the same issues that I first encountered ~3 years ago when investigating using clang format for modm (during the porting from xpcc) and it's very frustating to see all the failing corner cases. I like the idea, but I'm going to have to deal with the fallout of this decision for many, many years going forward and I don't want to burn out on this project from preventable causes.

If I look at the changes, there are more cases where the existing style is inconsistent with the modm style than there are cases where clang-format IMO is wrong.

For example, the contribution guide specifies no space after template keyword, yet that is the case in all of the bitbang implementations. Also, brace wrapping of if/else conditions is done very inconsistently (and this one isn't well defined in the style guide). At some point, you need to bite the apple and decide which one is actually correct.

Obviously, if the goal is to have inconsistent style everywhere, then we should not use clang format [grumpy new contributor mode on] but then I expect no more comments on the style of my commits... [/grumpy]

@salkinium
Copy link
Member

if the goal is to have inconsistent style everywhere

The goals are in order:

  1. Don't burn out the maintainer.
  2. Have somewhat structured, reviewable PRs, so we don't burn out the maintainer.
  3. Have somewhat consistent, readable code, so we don't burn out the maintainer.
  4. Don't burn out the maintainer.

All are pretty subjective, but forcing me to search the diff in a reformatted file is not acceptable. I also contribute to other projects and I don't reformat entirely unrelated lines of code because I want my changes merged and not bikeshed about code formatting.

Our formatting guide is basically about quickly grokking code, by visually matching vertically scoped code elements, like most control structured and function declarations.

  1. Use tabs, yes we hate it too, but it's too late to change.
  2. Put braces in a new line unless it becomes an unreadable mess.
  3. Put the function name on a newline unless it becomes an unreadable mess.
  4. Limit yourself to <100 chars unless it becomes an unreadable mess.

This is a cooperation between you and me, not a negotiation. You adapt your style a little and I look the other way and then we can both be maximize our happiness. This has worked quite well for most contributors and it can work well for us too.

the contribution guide specifies no space after template keyword, yet that is the case in all of the bitbang implementations.

The bit-bang implementation is adapted from code from 2006 I believe. Much modm code is over a decade old. When we ported xpcc to modm, we experimented with clang-format to change the tabs to spaces, but it was just too many changes it would have double the repository size and made it impossible to use git blame for anything anymore.

Which is why using clang-format on purely added files is very acceptable to me, but let's not plough over the entire repo on principle.

@henrikssn
Copy link
Contributor Author

I reverted the style changes to the bitbang* files and rebased/squashed against develop.

Copy link
Member

@salkinium salkinium left a comment

Choose a reason for hiding this comment

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

Excellent.

Copy link
Member

@rleh rleh left a comment

Choose a reason for hiding this comment

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

Nice work! 👍

I have commented a few remarks and questions inline.

.editorconfig Show resolved Hide resolved
src/modm/board/feather_m0/board.hpp Outdated Show resolved Hide resolved
using LoggerDevice = modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull >;
using Leds = SoftwareGpioPort< Led >;
using LoggerDevice = modm::IODeviceWrapper<Uart0, modm::IOBuffer::BlockIfFull>;
using Leds = Led;
Copy link
Member

Choose a reason for hiding this comment

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

What is the benefit of removing the SoftwareGpioPort<> here?
I would like to keep it in order to have the same board.hpp files for further automation and testing in the future (#359).

src/modm/platform/gpio/sam/module.md Outdated Show resolved Hide resolved
src/modm/platform/gpio/sam/unused.hpp Show resolved Hide resolved
src/modm/platform/uart/sam/uart.hpp.in Show resolved Hide resolved
src/modm/platform/usb/sam/usb.hpp Outdated Show resolved Hide resolved
Copy link
Contributor Author

@henrikssn henrikssn 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 the review!

src/modm/board/feather_m0/board.hpp Outdated Show resolved Hide resolved
using LoggerDevice = modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull >;
using Leds = SoftwareGpioPort< Led >;
using LoggerDevice = modm::IODeviceWrapper<Uart0, modm::IOBuffer::BlockIfFull>;
using Leds = Led;
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 wasn't implemented on SAM properly and the new Gpio<> is drop-in compatible with a SoftwareGpioPort<> that only contains one port (which is the case for all SAM boards at the moment).

Maybe we can move the SoftwareGpioPort<> to a common library after stm32/avr supports the same signals API?

src/modm/platform/gpio/sam/module.md Outdated Show resolved Hide resolved
src/modm/platform/usb/sam/usb.hpp Outdated Show resolved Hide resolved
@rleh
Copy link
Member

rleh commented Oct 6, 2020

Merge @salkinium?

Copy link
Member

@salkinium salkinium left a comment

Choose a reason for hiding this comment

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

Squashed and rebased, I'll merge once the CI passes.

# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#----------------------------------------------------------------------------

git diff --name-only --diff-filter=A -C -M develop | grep -e "\.\(c\|h\|hpp\|cpp\)\$" | xargs clang-format -i "$@"
Copy link
Member

Choose a reason for hiding this comment

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

Added a filter so that only newly added files get formatted, otherwise I go crazy as discussed ;-P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I realized this command is even better, as it reformats only changed lines: git clang-format upstream/develop

@salkinium salkinium merged commit debb430 into modm-io:develop Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Discussion about Signals API
3 participants