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

SYSTIMER peripheral #76

Merged
merged 7 commits into from
Jun 10, 2022
Merged

SYSTIMER peripheral #76

merged 7 commits into from
Jun 10, 2022

Conversation

MabezDev
Copy link
Member

@MabezDev MabezDev commented Jun 8, 2022

Opening in draft form early to bikeshed the API.

Limitations of this implementation:

  • Period alarms are not supported
  • Only unit0 of the systimer is used

This is a key part of embassy integration, so I thought I'd upstream this early :).

TODO

  • Implement Delay on Alarm
    • Make delay::Delay take an alarm instead of the whole SYSTIMER
  • Periodic (might not get to this in this PR)
    • Implement e-h timer traits on Alarm

@MabezDev
Copy link
Member Author

MabezDev commented Jun 9, 2022

To add some reasoning behind the current API:

  • The SYSTIMER peripheral isn't really the singleton here, it's the three alarms so I chose to make them singletons inside the SystemTimer struct.

  • Embassy currently requires that alarms are configurable in an immutable way (see the driver trait). So we can't have &mut in our Alarm signatures - this should be okay though because each alarm struct is a singleton, it should have unique access anyway.

  • Why const generic channel? It's an attempt to reduce code repetition, I feel its relatively clean, the only bit I don't really like is the unreachable! macros

@bjoernQ
Copy link
Contributor

bjoernQ commented Jun 9, 2022

Why const generic channel? It's an attempt to reduce code repetition, I feel its relatively clean, the only bit I don't really like is the unreachable! macros

I like it, too. Really looking forward to have const generics for enums. There is a nightly feature but don't think it's really worth to force nighly and also that feature is very experimental (warning: the feature adt_const_params is incomplete and may not be safe to use and/or cause compiler crashes 👻 ). I think the unreachable! is kind of okay since the user can't create arbitrary Alarms

I'm a bit more concerned about the unwrapping needed on alarmX() results - no idea how but it would be nice if the compiler could know if an alarm is already taken or not

@MabezDev
Copy link
Member Author

MabezDev commented Jun 9, 2022

I'm a bit more concerned about the unwrapping needed on alarmX() results - no idea how but it would be nice if the compiler could know if an alarm is already taken or not

Maybe it might be better for SysTimer to own the alarms (outright, not in an option) and let the compiler move the fields out. Much like the Peripherals struct. Only issue with that, is that SystemTimer cannot have any other self methods, so I suppose now() would have to be available on the alarms, which would be kinda weird. Actually, we can just make now an associated method.

Example:

pub struct SystemTimer {
    inner: SYSTIMER,
    pub alrm0: Alarm<Target, 0>,
    pub alrm1: Alarm<Target, 1>,
    pub alrm2: Alarm<Target, 2>,
}

// in main
let s = SystemTimer::new(p.SYSTIMER);

let alarm0 = s.alarm0; // move out of SystemTimer struct

s.alarm0.enable_interrupt() // compiler error, use of moved value, alarm is now in alarm0

@bjoernQ
Copy link
Contributor

bjoernQ commented Jun 9, 2022

Yes, I thought of something like this

@MabezDev MabezDev marked this pull request as ready for review June 9, 2022 14:34
@MabezDev
Copy link
Member Author

MabezDev commented Jun 9, 2022

Should now be ready for review. I had a branch with esp32c3 delay implemented with an alarm here: https://github.com/esp-rs/esp-hal/tree/feature/delay-alarm, but it introduced a generic parameter on Delay and instead chose just to implement with the new SystemTimer::now() method. I won't get to periodic alarms in this PR, I'll open an issue tracking that instead.

Copy link
Contributor

@bjoernQ bjoernQ left a comment

Choose a reason for hiding this comment

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

Looks great to me

@jessebraham
Copy link
Member

The ESP32-S2 and ESP32-S3 have this peripheral as well.

@MabezDev
Copy link
Member Author

MabezDev commented Jun 9, 2022

The ESP32-S2 and ESP32-S3 have this peripheral as well.

Ah, good point! I'll add support for those (hopefully its the same 😅).

@MabezDev
Copy link
Member Author

MabezDev commented Jun 9, 2022

S2 compiles, but it looks like SYSTIMER is missing from the S3 PAC, I filed esp-rs/esp-pacs#20.

@jessebraham
Copy link
Member

The SYSTIMER peripheral is now present for the ESP32-S3 in esp-pacs.

@MabezDev
Copy link
Member Author

MabezDev commented Jun 9, 2022

So the C3 & S3 version is identical and works great (yay!). However, the S2 has some changes, see the CI. I see a few options:

  • punt implementing SystemTimer for the S2 down the road
  • cfg all the s2 specific stuff (ew)
  • 'fix' the SVD to be more consistent with the other chip implementations?

What do we think?

@jessebraham
Copy link
Member

Thanks for making those changes!

I would say "fixing" the SVD would probably be my preferred approach, however if you just don't want to deal with the S2 right now that's fine too.

@MabezDev
Copy link
Member Author

So I needed a few cfg's but it's not too bad. Yet one more difference is that the S2 system timer does not run at fixed 16MHZ, but is variable. I've fixed it to 40mhz (can't go any lower) for now.

Copy link
Member

@jessebraham jessebraham 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 this is looking pretty good, thanks for all your work!

@jessebraham jessebraham merged commit 4acdf25 into main Jun 10, 2022
@MabezDev MabezDev self-assigned this Jun 10, 2022
@MabezDev MabezDev changed the title [esp32c3] SYSTIMER peripheral SYSTIMER peripheral Jun 10, 2022
@jessebraham jessebraham deleted the feature/systimer branch June 13, 2022 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants