Code: Select all
read_timed
Code: Select all
def adjust_tim2(period, width):
stm.mem32[stm.TIM2 + stm.TIM_ARR] = period
stm.mem32[stm.TIM2 + stm.TIM_CCR1] = period - width
Code: Select all
mem16
Thanks in advance!
Code: Select all
read_timed
Code: Select all
def adjust_tim2(period, width):
stm.mem32[stm.TIM2 + stm.TIM_ARR] = period
stm.mem32[stm.TIM2 + stm.TIM_CCR1] = period - width
Code: Select all
mem16
Code: Select all
{ MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
Code: Select all
def adjust_tim2(period, width):
stm.mem32[stm.TIM2 + stm.TIM_ARR] = period
stm.mem32[stm.TIM2 + stm.TIM_CCR1] = period - width
Code: Select all
#include "py/mphal.h"
void adjust_tim2(uint32_t period, uint32_t width) {
TIM2->ARR = period;
TIM2->CCR1 = period - width;
}
Nice, didn't know that. In that case and if the code doesn't have to be generic your 'C way' is way better than what I proposed.
Thanks, your explanation is exactly what I lacked and was hoping after I indeed saw that line in my searching. I haven't had much time to dig deep lately, and honestly didn't really having a feeling on what to do next besides moving up the directory tree (started in ports/stm32), then grepping the macros MP_ROM_QSTR and MP_ROM_PTR. The method/variable subscr is a bit confusing... I was initially reading it as 'subscriber' but now after a minute it's sticking that it means subscript... but now I am not so sure.stijn wrote: ↑Wed May 30, 2018 9:53 amIf you grep for mem16 you'll see it is part of the stm module as defined in modstm.c where it saysThe MP_QSTR_ prefix is an implementation detail because strings are interned in MicroPython, important part is it tells youCode: Select all
{ MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
stm.mem16 returns machine_mem16_obj.
Look for that in turn you'll see it's C definition in machine_mem.c, it's an object of type machine_mem_type. Which has a 'subscr' member pointing to the machine_mem_subscr method which is what gets executed when you run stm.mem16[someIndex] = someValue.
So in your C code you'll have something like machine_mem16_obj.base.type->subscr(machine_mem16_obj, mp_obj_new_int(myIndex), mp_obj_new_int(myValue)); (note: didn't test this, but that should be the principle).
Or if that's still too slow for you ou could copy parts of the machine_mem_subscr implementation to read/write addresses directly.
Awesome, this is pretty much exactly what I want to do in my code. Thanks!dhylands wrote: ↑Wed May 30, 2018 6:08 pmCode: Select all
#include "py/mphal.h" void adjust_tim2(uint32_t period, uint32_t width) { TIM2->ARR = period; TIM2->CCR1 = period - width; }
I assumed that much, but indeed didn't know where they ended up being generated (I was grepping a micropython repo with a build in it).The python stm constants like TIM2 and TIM_ARR are generated by running python script over the C header files
Great, thanks, very valuable and should immediately help me move along.found in the CMSIS library, which you can find in lib/stm32lib/CMSIS/STM32F4xx/Include/stm32f405xx.h (which is #included indirectly thru mphal.h)
There are ways to generalize this even more, but without knowing more about what you're doing, I can't really recommend anything.
You can get the equivalent value by doing:
Code: Select all
uint32_t *tim2_arr = &TIM2->ARR;
Code: Select all
*tim2_arr = val;
Code: Select all
#define TIM2 ((TIM_TypeDef *) TIM2_BASE)
Code: Select all
...
{ MP_ROM_QSTR(MP_QSTR_TIM2), MP_ROM_PTR(&mpz_40000000) },
...
{ MP_ROM_QSTR(MP_QSTR_TIM_ARR), MP_ROM_INT(0x2c) }, // 32-bits, TIM auto-reload register
...
Code: Select all
uint32_t TIM2_value = some-mp-function-to-lookup(stm.TIM2);
uint32_t TIM_ARR_value = some-mp-function-to-lookup(stm.TIM_ARR);
uint32_t *tim2_arr = (uint32_t *)(TIM2_value + TIM_ARR_value);
Code: Select all
uint32_t *tim2_arr = &TIM2->ARR;