Hi,
I'm trying to create a custom C module for Micropython, for my custom board. This custom board works with a STM32F407VGT6; and has 4 push buttons on it, attached to specific pins of the MCU.
Right now I have a Python file, which contains a class that allows me to work with the buttons of my board. In this micropython file, I write at the top "from machine import Pin", and then at the bottom there is a class that, depending on the button that the user pressed, a specific function is called and returns the value of the button pressed (High or Low). This allows certain user to import this class in his micropython code, and use the buttons of the boad in an easy way. Actually, I have treated this file as a frozen module, and I have builded my custom firmware for my custom board with that frozen module inside.
The thing is that I want to turn this Python frozen module file into a C module for Micropython.
I've already read some documentation about this, but I can't figure out how to manipulate the pins of the MCU (in order to configure the Pin.IN and PULL_UP stuff for the buttons of my board) inside the "STATIC mp_obj_t example_function_buttons(....){}" function, inside the C module file.
How can I configure certain pins inside the C module file, in order for me to configure their behaviors in order to manipulate the buttons, and then return the value of the button pressed?
Any help would be really appreciated!
How to configure pin behavior inside a custom C module?
-
- Posts: 5
- Joined: Wed Feb 24, 2021 7:34 pm
- Location: México
Re: How to configure pin behavior inside a custom C module?
I know this isn't particularly helpful, but other than for educational purposes, I'm not sure why you'd want to do this. Python bytecode is generally much smaller for simple tasks, so it takes up less ROM (and not to mention easier to write). And it doesn't sound like this is particularly performance critical.JonatanESalinas wrote: ↑Wed Feb 24, 2021 8:18 pmThe thing is that I want to turn this Python frozen module file into a C module for Micropython.
In general, you should only need to drop into C to write highly optimised code (e.g. number crunching, etc) or things that cannot be done in Python.
But seeing as you asked...JonatanESalinas wrote: ↑Wed Feb 24, 2021 8:18 pmHow can I configure certain pins inside the C module file, in order for me to configure their behaviors in order to manipulate the buttons, and then return the value of the button pressed?
It depends a bit based on which board/port you're using. I'm guessing you're talking about a "user c module" (i.e. you C code is built into the firmware) rather than a "dynamic native module" (C code compiled to a .mpy file).
One good place to look to start would be drivers/dht/dht.c -- it uses the mp_hal_get_pin_obj functions (e.g. mp_hal_pin_high, mp_hal_pin_read).
-
- Posts: 5
- Joined: Wed Feb 24, 2021 7:34 pm
- Location: México
Re: How to configure pin behavior inside a custom C module?
Thanks for your answer!
So in order to do this, in my custom "user c module" I created a class and in the "make_new" function I have something like this:
My custom board is using a STM32F407VGT6 MCU and has four buttons, one in each of these pins: C2, D5, D4, D3. To make the buttons work, I need to configure these pins as INPUT and PULL_UP.
Then, I wrote other functions in my user c module "myboard_buttons", like this one:
I have four functions like the one above, one for each button. Then, I build the whole firmware, and I program my custom board with it. But happens that when I want to test if the buttons work, with something like this:
The button 1 don't react nor print anything when I push it. When I try:
I press the button 2, and it prints the message (which is right), but having only that part of code (that "if") I also press the button 3 and it prints: which is wrong. The same happens if I push the button 4. So it seems like it is reading the same pin, or something like that. I don't know if this issue is related with how mp_hal_pin_input() works; Does anyone know how to solve this?
I'm sorry for the long reply, but I got stuck here. Thank you in advance for any help.
Well yes, I want to do this for educational purposes, I want to learn how to do this.
Thank you! This helped me, but now I'm a little bit confused. I want that a person using micropython with my custom board can do something like this (myboard_buttons would be my custom user c module for the buttons of my board):
Code: Select all
from myboard_buttons import sw
[other code]
if sw().sw1() == 0:
print("Button 1 pressed.")
elif sw().sw2() == 0:
print("Button 2 pressed.")
elif sw().sw3() == 0:
print("Button 3 pressed.")
elif sw().sw4() == 0:
print("Button 4 pressed.")
Code: Select all
....
mp_hal_pin_input(pin_C2);
mp_hal_pin_input(pin_D5);
mp_hal_pin_input(pin_D4);
mp_hal_pin_input(pin_D3);
....
Then, I wrote other functions in my user c module "myboard_buttons", like this one:
Code: Select all
STATIC mp_obj_t button0_pressed(mp_obj_t self_in) {
return mp_obj_new_int(mp_hal_pin_read(pin_C2));
}
Code: Select all
if sw().sw1() == 0:
print("Button 1 pressed.")
delay(500)
Code: Select all
if sw().sw2() == 0:
print("Button 2 pressed.")
delay(500)
Code: Select all
Button 2 pressed.
I'm sorry for the long reply, but I got stuck here. Thank you in advance for any help.
-
- Posts: 5
- Joined: Wed Feb 24, 2021 7:34 pm
- Location: México
Re: How to configure pin behavior inside a custom C module?
Ok, I solved this issue, I used:
For each of the pins of my buttons, which specifies that I want a PULL_UP configuration, instead of only:
That solved my problem , but thanks anyway!!
Code: Select all
mp_hal_pin_config(pin_C2, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_UP, 0);
Code: Select all
mp_hal_pin_input(pin_C2);