defining pin functions

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

defining pin functions

Post by jgriessen » Thu Apr 19, 2018 6:09 pm

If pins are defined to an alt function in the board definition like so:

Code: Select all

#define MICROPY_HW_SPI2_NSS     (pin_B9)
#define MICROPY_HW_SPI2_SCK     (pin_B13)
#define MICROPY_HW_SPI2_MISO    (pin_B14)
#define MICROPY_HW_SPI2_MOSI    (pin_B15)
Is this code below enough to redefine them to do GPIO?

Code: Select all

import machine
import pyb
pinB9 = machine.Pin(machine.Pin.cpu.B9, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
pinB10 = machine.Pin(machine.Pin.cpu.B10, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
pinB13 = machine.Pin(machine.Pin.cpu.B13, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
pinB14 = machine.Pin(machine.Pin.cpu.B14, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
pinB15 = machine.Pin(machine.Pin.cpu.B15, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
count = 0
while ( count < 40):
    pinB9.value(0)
    pyb.delay(100)
    pinB9.value(1)
    pyb.delay(100)
    count = count + 1
John Griessen blog.kitmatic.com

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

Re: defining pin functions

Post by dhylands » Thu Apr 19, 2018 6:28 pm

jgriessen wrote:
Thu Apr 19, 2018 6:09 pm
If pins are defined to an alt function in the board definition like so:

Code: Select all

#define MICROPY_HW_SPI2_NSS     (pin_B9)
#define MICROPY_HW_SPI2_SCK     (pin_B13)
#define MICROPY_HW_SPI2_MISO    (pin_B14)
#define MICROPY_HW_SPI2_MOSI    (pin_B15)
This doesn't really define any alternate function. It tells the SPI module which pins to use when you ask to initialize SPI2. Until you actually initialize SPI2 they don't really do anything. The pins will remain as GPIO inputs (which is pretty much how all of the pins are at power on reset time).
jgriessen wrote:
Thu Apr 19, 2018 6:09 pm
Is this code below enough to redefine them to do GPIO?

Code: Select all

import machine
import pyb
pinB9 = machine.Pin(machine.Pin.cpu.B9, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
pinB10 = machine.Pin(machine.Pin.cpu.B10, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
pinB13 = machine.Pin(machine.Pin.cpu.B13, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
pinB14 = machine.Pin(machine.Pin.cpu.B14, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
pinB15 = machine.Pin(machine.Pin.cpu.B15, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
count = 0
while ( count < 40):
    pinB9.value(0)
    pyb.delay(100)
    pinB9.value(1)
    pyb.delay(100)
    count = count + 1
That will initialize the pins from whatever functionality they were previously setup with (probably GPIO inputs) to be GPIO outputs.

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

Re: defining pin functions

Post by jgriessen » Thu Apr 19, 2018 7:14 pm

So that micropython seems to be good for toggling outputs to confirm the hardware.

I confirmed this hardware using miniblink based on libopencm3 and got the correct pins to toggle and want to confirm that with micropython and can on a G30TH, but not on my board yet. My board gets flashing lights, but at wrong rate and 4 flashes instead of 90, so not sure where that comes from -- not from code in ./modules dir... I can compile two ways with different platform definitions, same code and one LED changes from on to off...Not sure why yet. Thinking of ways to observe serial port output and code to generate output on usart2.
John Griessen blog.kitmatic.com

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

Re: defining pin functions

Post by dhylands » Thu Apr 19, 2018 7:34 pm

The flash_error routine in main.c will flash 4 times:
https://github.com/micropython/micropyt ... ain.c#L605
I'd try chaning the number 4 to something else and see if you get the new number of flashes.

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

Re: defining pin functions

Post by jgriessen » Thu Apr 19, 2018 8:13 pm

Thanks! Will do.

Nope, still flashes 4 times so now I changed the code to 7 flashes for boot.py, and 5 flashes for main.py... in four places, and try again.
John Griessen blog.kitmatic.com

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

Re: defining pin functions

Post by jgriessen » Thu Apr 19, 2018 8:31 pm

I get 6 flashes now. That corresponds to n-1 flashes, so the flash_error(7) was for boot.py

Should I be using openocd differently with frozen code? Different memory addresses? I used:
firmware0.bin 0x08000000 firmware1.bin 0x08020000

Code: Select all

openocd   -f /home/john/Documents/EEProjects/circuitboards/culture_shock/code_PYFLEX_F401/minimodule-swd-on-ch1.cfg  -f target/stm32f4x.cfg -c "program /home/john/micropython/ports/stm32/build-G30FLEX/firmware0.bin 0x08000000 verify; program /home/john/micropython/ports/stm32/build-G30FLEX/firmware1.bin 0x08020000 verify reset exit"
From frozen_mpy.c

Code: Select all

const char mp_frozen_mpy_names[] = {
"main.py\0"
"boot.py\0"
"\0"};
const mp_raw_code_t *const mp_frozen_mpy_content[] = {
    &raw_code_main__lt_module_gt_,
    &raw_code_boot__lt_module_gt_,
};
John Griessen blog.kitmatic.com

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

Re: defining pin functions

Post by dhylands » Thu Apr 19, 2018 9:09 pm

The frozen modules are just compiled into the code like any other C code, so none of the addresses will change. You'll find that the firmware1.bin will be larger with frozen modules rather than without.

For this level of debugging, I normally setup the REPL on a UART by defining MICROPY_HW_UART_REPL and MICROPY_HW_UART_REPL_BAUD. This will cause main.c to start up a UART based REPL here:
https://github.com/micropython/micropyt ... #L539-L547

Assuming your clocks and stuff are right, you can then do printfs after that line of code and the contents of the printf's will show on the UART. If I don't see my output then I connect up a logic anaylzer to the TX line and see what baud rate is being transmitted.

I've never used openocd/debugger with the pyboard (I've been able to get away with printf debugging), but presumably you can set breakpoints and step through the code to see what's failing.

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

Re: defining pin functions

Post by jgriessen » Thu Apr 19, 2018 9:47 pm

Thanks, I'll look up doing printfs to the right place. Sounds much easier than LA. Mine's a big old HP thing.
John Griessen blog.kitmatic.com

Post Reply