Page 1 of 2

STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Tue Jul 28, 2015 9:36 pm
by sajulios
Hi guys

I'm new using microphyton, I'm using the STM32F4DISCOVERY board, I hanted to know how can I interact with the different GPIO's on the board, for example toggleling one of the LEDs on the board. the code I have found so far is related to the Micropython board.

Can you gide me on how to set the different IOs.

If I want to set the UART or other module such as timers and ADCs on this board what would be the right way to do it using the already define methods?

Thanks for your help

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Wed Jul 29, 2015 12:17 am
by dhylands
For the STM32F4DISC board, you can determine the pin names in one of a couple of different ways:

1 - Examine the pins.csv file: https://github.com/micropython/micropyt ... C/pins.csv
2 - Use the dir command from the python command line:

Code: Select all

Micro Python v1.3.8-9-g8a2cc1c on 2014-12-29; F4DISC with STM32F407
Type "help()" for more information.
>>> dir(pyb.Pin.board)
['PC0', 'PC1', 'PC2', 'PC3', 'PA0', 'PA1', 'PA2', 'PA3', 'PA4', 'PA5', 'PA6', 'PA7', 'PC4', 'PC5', 'PB0', 'PB1', 'PB2', 'PE7', 'PE8', 'PE9', 'PE10', 'PE11', 'PE12', 'PE13', 'PE14', 'PE15', 'PB10', 'PB11', 'PB12', 'PB13', 'PB14', 'PB15', 'PD8', 'PD9', 'PD10', 'PD11', 'PD12', 'PD13', 'PD14', 'PD15', 'PC6', 'PC7', 'PC8', 'PC9', 'PA8', 'PA9', 'PA10', 'PA13', 'PA14', 'PA15', 'PC10', 'PC11', 'PC12', 'PD0', 'PD1', 'PD2', 'PD3', 'PD4', 'PD5', 'PD6', 'PD7', 'PB4', 'PB5', 'PB6', 'PB7', 'PB8', 'PB9', 'PE0', 'PE1', 'PE2', 'PE3', 'PE4', 'PE5', 'PE6', 'PC13', 'PC14', 'PC15', 'PH0', 'PH1', 'LED_GREEN', 'LED_ORANGE', 'LED_RED', 'LED_BLUE', 'SW']
>>> dir(pyb.Pin.cpu)  
['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A13', 'A14', 'A15', 'B0', 'B1', 'B2', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12', 'B13', 'B14', 'B15', 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'C13', 'C14', 'C15', 'D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'D11', 'D12', 'D13', 'D14', 'D15', 'E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9', 'E10', 'E11', 'E12', 'E13', 'E14', 'E15', 'H0', 'H1']
The board pins typically match the names silscreened on the board, where the CPU pins are always of the form Port Letter followed by pin number within the port. For the STM32F4DISC board, these are almost identical (the board names start with a P).

To determine which functions are available on which pins, you can look at this file: https://github.com/micropython/micropyt ... 405_af.csv which has all of the alternae functions listed for each pin.

There is also a generated python file called pins_af.py which you'll find in build-STM32F4DISC, which looks something like this:

Code: Select all

PINS_AF = (
  ('PC0', ),
  ('PC1', ),
  ('PC2', (5, 'SPI2_MISO'), (6, 'I2S2_EXTSD'), ),
  ('PC3', (5, 'SPI2_MOSI'), (5, 'I2S2_SD'), ),
  ('PA0', (1, 'TIM2_CH1'), (1, 'TIM2_ETR'), (2, 'TIM5_CH1'), (3, 'TIM8_ETR'), (7, 'USART2_CTS'), (8, 'UART4_TX'), ),
  ('PA1', (1, 'TIM2_CH2'), (2, 'TIM5_CH2'), (7, 'USART2_RTS'), (8, 'UART4_RX'), ),
  ('PA2', (1, 'TIM2_CH3'), (2, 'TIM5_CH3'), (3, 'TIM9_CH1'), (7, 'USART2_TX'), ),
  ('PA3', (1, 'TIM2_CH4'), (2, 'TIM5_CH4'), (3, 'TIM9_CH2'), (7, 'USART2_RX'), ),
  ('PA4', (5, 'SPI1_NSS'), (6, 'SPI3_NSS'), (6, 'I2S3_WS'), (7, 'USART2_CK'), ),
  ('PA5', (1, 'TIM2_CH1'), (1, 'TIM2_ETR'), (3, 'TIM8_CH1N'), (5, 'SPI1_SCK'), ),
 ...
If you copy pins_af.py to your board along with the examples/pins.py file, then you can do:

Code: Select all

>>> import pins
>>> pins.pins()
PC0        IN                  
PC1        IN                  
PC2        IN                  
PC3        IN                  
PA0        IN                  
PA1        IN                  
PA2        IN                  
PA3        IN                  
...
to see how each pin is currently configured. You can run:

Code: Select all

>>> pins.af()
PC0        
PC1        
PC2         5: SPI2_MISO   6: I2S2_EXTSD 
PC3         5: SPI2_MOSI   5: I2S2_SD    
PA0         1: TIM2_CH1    1: TIM2_ETR    2: TIM5_CH1    3: TIM8_ETR    7: USART2_CTS  8: UART4_TX   
PA1         1: TIM2_CH2    2: TIM5_CH2    7: USART2_RTS  8: UART4_RX   
PA2         1: TIM2_CH3    2: TIM5_CH3    3: TIM9_CH1    7: USART2_TX  
...
to see the available functions for each pin in a slightly more readable form than looking at pins_af.py directly.

Most of the pyb functions work the same on the discovery board as they do on the pyboard. The exceptions that I can think of are pyb.Accel and pyb.Servo aren't present. You can see what is present by using:

Code: Select all

>>> dir(pyb)
['__name__', 'bootloader', 'hard_reset', 'info', 'unique_id', 'freq', 'repl_info', 'wfi', 'disable_irq', 'enable_irq', 'stop', 'standby', 'main', 'repl_uart', 'usb_mode', 'hid_mouse', 'hid_keyboard', 'USB_VCP', 'USB_HID', 'have_cdc', 'hid', 'millis', 'elapsed_millis', 'micros', 'elapsed_micros', 'delay', 'udelay', 'sync', 'mount', 'Timer', 'rng', 'RTC', 'Pin', 'ExtInt', 'Switch', 'LED', 'I2C', 'SPI', 'UART', 'CAN', 'ADC', 'ADCAll', 'DAC']
There is an staccel.py in the boards/STMF4DISC directory that is supposed to work with the accelerometer (I haven't used it myself).

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Wed Jul 29, 2015 12:35 pm
by SpotlightKid
dhylands wrote:There is an staccel.py in the boards/STMF4DISC directory that is supposed to work with the accelerometer.
Alas, it doesn't. I have a fixed version here:

https://github.com/SpotlightKid/micropy ... ster/accel

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Fri Jul 31, 2015 6:23 pm
by sajulios
Thanks for your help

So lets say I want to turn the GREE_LED, what would be the right way to do it?

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Fri Jul 31, 2015 8:18 pm
by dhylands
Here are a few (certainly not all) of the ways that you could turn a LED on:

Code: Select all

green_led = pyb.LED(2)
green_led.on()
or

Code: Select all

green_led = pyb.Pin('LED_GREEN', pyb.Pin.OUT_PP)
green_led.value(1)
or

Code: Select all

green_led = pyb.Pin('D12', pyb.Pin.OUT_PP)
green_led.value(1)
You can also assign arbitrary names to pins (a user mapping).

The pyb.LED API has on/off semantics so is probably the most portable. Depending on how the LED is wired, sometimes writing a 0 to a pin will turn the LED on and sometimes writing a 1 to a pin will turn the LED on. The pyb.LED API also has a toggle method.

Using the D12 name is very board specific (i.e. it would probably only work on the STM32F4 Discovery board).

The numbers 1-4 for the LED API are as similar as they can be between the pyboard and the STM32F4DISC.

The pyboard has LED_RED, LED_GREEN, LED_YELLOW, and LED_BLUE. On the STM32F4DISC, LED_YELLOW is LED_ORANGE instead, but the other 3 are the same.

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Fri Jul 31, 2015 10:48 pm
by sajulios
Thanks

Now, I read that to run the script you use pyboard.py but when I try to run it to load test.py to the STM324FDiscovery get the following:

julio@julio-virtual-machine:~/micropython/tools$ python pyboard.py test.py
File "pyboard.py", line 139
ret = self.exec('print({})'.format(expression))
^
SyntaxError: invalid syntax

and when I run it from the micropython folder I get the following:

julio@julio-virtual-machine:~/micropython$ python pyboard.py test.py
python: can't open file 'pyboard.py': [Errno 2] No such file or directory

Note: I have included pyboard.py path to the environment path variables

Am I doing it the right way?

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Sat Aug 01, 2015 12:10 am
by dhylands
sajulios wrote:Thanks

Now, I read that to run the script you use pyboard.py but when I try to run it to load test.py to the STM324FDiscovery get the following:

julio@julio-virtual-machine:~/micropython/tools$ python pyboard.py test.py
File "pyboard.py", line 139
ret = self.exec('print({})'.format(expression))
^
SyntaxError: invalid syntax
IIRC pyboard.py only works properly when run using python3
and when I run it from the micropython folder I get the following:

julio@julio-virtual-machine:~/micropython$ python pyboard.py test.py
python: can't open file 'pyboard.py': [Errno 2] No such file or directory
To run from the micropython folder you'd need to use:

Code: Select all

python tools/pyboard.py path-to-test/test.py
Note: I have included pyboard.py path to the environment path variables
That would only be relevant if you were trying to do:

Code: Select all

pyboard.py ...
rather than

Code: Select all

python pyboard.py ...
(i.e. THE PATH environment variable is only used to locate the program you're trying to run. So when you do:

Code: Select all

python pyboard.py ...
then the PATH is used to locate python, and not to locate pyboard.py

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Sat Aug 01, 2015 1:06 am
by sajulios
Hi I try using python tools/pyboard.py test.py ( since test is in micropython directory)
and I got the following:
julio@julio-virtual-machine:~/micropython$ python tools/pyboard.py test.py File "tools/pyboard.py", line 211
ret = self.exec('print({})'.format(expression))
^
SyntaxError: invalid syntax

when I used python3 tools/pyboard.py test.py, I got the following:
Traceback (most recent call last):
File "tools/pyboard.py", line 282, in <module>
main()
File "tools/pyboard.py", line 251, in main
pyb = Pyboard(args.device, args.baudrate, args.user, args.password)
File "tools/pyboard.py", line 117, in __init__
import serial
ImportError: No module named 'serial'

Do you know what could be causing these error messages? I'm using Ubuntu 14.04
Thanks

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Sat Aug 01, 2015 5:51 am
by pythoncoder
It sounds like you need to install pyserial

Code: Select all

$ sudo pip3 install pyserial

Re: STM32F4DISCOVERY Setting GPIOs and Modules

Posted: Sat Aug 01, 2015 6:25 am
by dhylands
And you may also need to do:

Code: Select all

sudo apt-get install python3-pip