How to configure pin behavior inside a custom C module?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
JonatanESalinas
Posts: 5
Joined: Wed Feb 24, 2021 7:34 pm
Location: México

How to configure pin behavior inside a custom C module?

Post by JonatanESalinas » Wed Feb 24, 2021 8:18 pm

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!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to configure pin behavior inside a custom C module?

Post by jimmo » Thu Feb 25, 2021 12:35 am

JonatanESalinas wrote:
Wed Feb 24, 2021 8:18 pm
The thing is that I want to turn this Python frozen module file into a C module for Micropython.
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.

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.
JonatanESalinas wrote:
Wed Feb 24, 2021 8:18 pm
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?
But seeing as you asked... :)

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).

JonatanESalinas
Posts: 5
Joined: Wed Feb 24, 2021 7:34 pm
Location: México

Re: How to configure pin behavior inside a custom C module?

Post by JonatanESalinas » Wed Mar 03, 2021 9:13 pm

Thanks for your answer!
jimmo wrote:
Thu Feb 25, 2021 12:35 am
I know this isn't particularly helpful, but other than for educational purposes, I'm not sure why you'd want to do this.
Well yes, I want to do this for educational purposes, I want to learn how to do this.
jimmo wrote:
Thu Feb 25, 2021 12:35 am
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).
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.")
				
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:

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);
    ....
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:

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));
}
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:

Code: Select all

        if sw().sw1() == 0:
            print("Button 1 pressed.")
            delay(500) 
The button 1 don't react nor print anything when I push it. When I try:

Code: Select all

        if sw().sw2() == 0:
            print("Button 2 pressed.")
            delay(500)
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:

Code: Select all

Button 2 pressed.
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.

JonatanESalinas
Posts: 5
Joined: Wed Feb 24, 2021 7:34 pm
Location: México

Re: How to configure pin behavior inside a custom C module?

Post by JonatanESalinas » Thu Mar 04, 2021 11:26 pm

Ok, I solved this issue, I used:

Code: Select all

mp_hal_pin_config(pin_C2, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_UP, 0);
For each of the pins of my buttons, which specifies that I want a PULL_UP configuration, instead of only:

Code: Select all

mp_hal_pin_input(pin_C2);
That solved my problem :) , but thanks anyway!!

Post Reply