stmhal -- how is it used in micropython?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

stmhal -- how is it used in micropython?

Post by jgriessen » Sat Aug 06, 2016 12:54 am

I was searching through the source for timer functions and come across the code st.com contributed
and wonder how it is called.
micropython/stmhal/hal/f4/src/stm32f4xx_hal_tim.c

seems like a giant lookup table.
Where is the contact point with micropython terms like:

t1 = pyb.Timer(1, prescaler=83, period=xfmr_pulse_w, mode=pyb.Timer.UP

t1ch1 = t1.channel(1, pyb.Timer.OC_ACTIVE, polarity= pyb.Timer.HIGH, pin=pyb.Pin.board.JP5)

It must be here in
micropython/stmhal/timer.c
because there I find words like callback and compare and OC_ACTIVE and
oc_config.OCPolarity = TIM_OCPOLARITY_HIGH;

but no one pulse or onepulse anywhere. And no ONE_SHOT constant as mentioned in docs that may be only for the wipy, but are filed under pyboard and stmhal now.
http://docs.micropython.org/en/v1.8.2/p ... t=one_shot

stmhal/timer.c would be where to add one pulse feature, right?
John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: stmhal -- how is it used in micropython?

Post by dhylands » Sat Aug 06, 2016 3:24 am

These 4 files:
stmhal/hal/f4/inc/stm32f4xx_hal_tim.h
stmhal/hal/f4/inc/stm32f4xx_hal_tim_ex.h
stmhal/hal/f4/src/stm32f4xx_hal_tim.c
stmhal/hal/f4/src/stm32f4xx_hal_tim_ex.c
are the HAL files for the timer. They only callable from C.

stmhal/timer.c contains the python interface and it in turn calls into the HAL files.

The one pulse mode constants are defined here:
https://github.com/micropython/micropyt ... #L545-L551
and I see these functions:
https://github.com/micropython/micropyt ... #L196-L201
https://github.com/micropython/micropyt ... l_tim_ex.c (search for OnePulse)

Currently there is no support for this exposed in the python code.

User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: stmhal -- how is it used in micropython?

Post by jgriessen » Tue Aug 09, 2016 3:05 pm

I've been reading in the stmhal/hal/f4/inc/stm32f4xx_hal_tim.h and stmhal/hal/f4/src/stm32f4xx_hal_tim_ex.c
and not figured how to use that yet,

so I'm going to be doing a register write as in your example using stm.mem16 on the OC1M and OC3M control registers.
Any help looking up the part like: stm.mem16[stm.TIM2 + stm.TIM_SMCR]
but for OC1M and OC3M appreciated.
John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: stmhal -- how is it used in micropython?

Post by dhylands » Tue Aug 09, 2016 4:10 pm

jgriessen wrote:I've been reading in the stmhal/hal/f4/inc/stm32f4xx_hal_tim.h and stmhal/hal/f4/src/stm32f4xx_hal_tim_ex.c
and not figured how to use that yet,
You can get more examples by downloading the whole HAL from here: http://www.st.com/content/st_com/en/pro ... ubef4.html

That includes a HAL user manual as well as the HAL source code and a ton of examples.

Note that this is a slightly newer version of the HAL than whats included in MicroPython, but the differences should be minor.

User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: stmhal -- how is it used in micropython?

Post by jgriessen » Tue Aug 09, 2016 4:43 pm

Thanks for the link to the ST docs on HAL. I asked an ST apps engineer and he hasn't even come back with that yet...
Once I have the register write figured out, maybe in a couple weeks I can add it to micropython as a method.

I don't think the current way Timer.OC_FORCED_ACTIVE is implemented is working, or useful, or in line with the STM32F4xx ref manual.
Since it is in channel config, it does not happen at the useful time. It needs to be in a method instead.

I did a test where Timer.OC_FORCED_ACTIVE was followed by Timer.OC_INACTIVE, and it seemed as if the first one was nullified by the second. In the ref manual, forcing an OC output active is something to do on the fly, or in a ISR, not during setup.
John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: stmhal -- how is it used in micropython?

Post by dhylands » Tue Aug 09, 2016 4:59 pm

Well, it can also be used during setup to get the output into a know, but unchanging state.

The HAL code assumes that you'll set the OCMode in one of the init calls and doesn't really provide any mechanism for changing the mode on the fly. I'm not saying that you can't, just saying I that the HAL doesn't really provide any way of doing it.

Doing the direct register writes would be the most efficient (calling any of the TIM_XXX_Init functions seems like way too much overhead to me).

What we've done so far in the hal code is really just to cover off the "typical" cases. When making API changes we also need to consider how those changes will fit into other architectures (like ESP8266, WiPy, teensy, etc).

User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: stmhal -- how is it used in micropython?

Post by jgriessen » Tue Aug 09, 2016 5:59 pm

Doing the direct register writes would be the most efficient (calling any of the TIM_XXX_Init functions seems like way too much overhead to me).
Hmm... i'm just reading the Description of STM32F4xx HAL drivers and see lots of layers of stuff to understand,
and a note that "LL drivers will not be described in this document." so it's hard to trace all the way to registers and
ref manual functions.
For instance disabling, enabling a timer after setting it up:
__HAL_TIM_ENABLE

Enable the TIM peripheral.
Parameters:

__HANDLE__: TIM handle
No example. How do you use the words TIM handle? Looking them up gets me to confusing stuff like lists jammed together almost unreadable. That suggests they made the docs with some code automation, and the whole cross API thing with some document automation that makes it all less readable/understandable. So, back to learning to register write and reading the STM32F4xx ref manual
John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: stmhal -- how is it used in micropython?

Post by dhylands » Tue Aug 09, 2016 9:12 pm

Yeah - that's pretty typical. I think most of the documentation is extracted from the code.

The LL modules only exist for the complicated peripherals (like USB or SD).

All of the peripherals use similar pardigms where there is a handle object for the peripheral and then a bunch of functions that work on it (which is C's version of ADT - Abstract Data Types).

Here's a typical example of using a timer to generate PWM:
https://github.com/micropython/micropyt ... #L140-L149
and just above it you'll see another example of using the HAL to initialize a GPIO pin.

I find that I rely on reading the source a bit more than the docs. So your __HAL_TIM_ENABLE example:
https://github.com/micropython/micropyt ... #L140-L149
takes a handle, which will be a pointer to the TIM_HandleTypeDef object
https://github.com/micropython/micropyt ... #L286-L295
that you passed into the HAL_TIM_XXX_Init function, like this one:
https://github.com/micropython/micropyt ... im.h#L1356

Note that disabling a timer won't change the state of the output, just stop the counter from incrementing/decrementing.

User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: stmhal -- how is it used in micropython?

Post by jgriessen » Wed Aug 10, 2016 8:51 pm

Here's a typical example of using a timer to generate PWM:
https://github.com/micropython/micropyt ... #L140-L149
and just above it you'll see another example of using the HAL to initialize a GPIO pin.
OK, but I still had difficulty figuring where to look next to find how to use such -- for instance
what does pointer s->pulse_cur do?
takes a handle, which will be a pointer to the TIM_HandleTypeDef object
I'm lost. maybe it will soak in later.

HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim); What does this line mean?

Meanwhile, after hearing you say HAL has too much overhead for most uses in micropython, so it was only selectively included
makes me focus on bit twiddled register writes now. I've been getting a little
bit figured out about timers.
John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: stmhal -- how is it used in micropython?

Post by dhylands » Wed Aug 10, 2016 10:43 pm

jgriessen wrote:
Here's a typical example of using a timer to generate PWM:
https://github.com/micropython/micropyt ... #L140-L149
and just above it you'll see another example of using the HAL to initialize a GPIO pin.
OK, but I still had difficulty figuring where to look next to find how to use such -- for instance
what does pointer s->pulse_cur do?
s is declared here are a function argument:
https://github.com/micropython/micropyt ... rvo.c#L120
and it's a pointer to a pyb_servo_ovj_t struct, which is defined here:
https://github.com/micropython/micropyt ... .c#L48-L60

s->pulse_cur returns the pulse_cur member of the pyb_servo_obj_t struct pointed to by s.
takes a handle, which will be a pointer to the TIM_HandleTypeDef object
I'm lost. maybe it will soak in later.

HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim); What does this line mean?
That's a function prototype (the extern is optional and not used here). The function is named HAL_TIM_OC_Init, returns an object of type HAL_StatusTypeDef and takes one argument, which is a pointer to a TIM_HandleTypeDef struct.
Meanwhile, after hearing you say HAL has too much overhead for most uses in micropython, so it was only selectively included
makes me focus on bit twiddled register writes now. I've been getting a little
bit figured out about timers.
I don't know about "most uses", but there are certainly lots of cases where it's definitely overkill (and I think that's certainly the case for what you're trying to do).

Post Reply