"Free" pins to use for GPIO purposes

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
justatoms
Posts: 2
Joined: Sat May 17, 2014 10:37 pm

"Free" pins to use for GPIO purposes

Post by justatoms » Sun May 18, 2014 1:25 am

Hi forum members,

I plan to use PyBoards as the brains for future (mostly ham radio) projects. The ability to program an MCU directly in a true high level language has made me really enthusiastic about micro python and the PyBoard. I recently got my 2 kick-starter boards and have started some initial programming. I'm now at a point where I want to connect a 2x8 character LCD to the PyBoard. These LCDs are controlled through a simple parallel interface (HD44780 style) requiring some 12 digital output pins. I thought of using some of the many IO pins that the STM chip and PyBoard provides, but have difficulty understanding the concept of pins having "alternate functions". Looking at PYBv10b.pdf and the PyBoard silk screen all pins seem to have some dedicated functions other than GPIO. (CAN, I2C, SPI, DAC, ADC, UART, etc.)

What determines the alternative function of a pin? According to STM datasheet there may be as many as 15 different alternate functions. Is this something set by the mcu firmware at mcu start-up or are there (non-volatile) mcu registers whose content determine function of a pin? What is the function of the pins when the PyBoard boots with the micro python firmware? If I don't need any of the CAN, SPI, I2C, DAC, ADC functionality, can I make all PyBoard pins (except supply voltage pins) become digital GPIO pins with pyb.Pin(pin_id, mode, pull) object instantiations?

In short; what PyBoard pins (not) to use for driving my LCD and how to set them?

Thanks!

fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: "Free" pins to use for GPIO purposes

Post by fma » Sun May 18, 2014 9:11 am

As you said, all GPIO pins have alternate functions. If you only plan to use digital IOs, you can choose any pin you want, for easy routing, for example.

But if you need a UART, or a timer output, or a SPI bus, then, you will have to find which pins can be use for that (not all), and use the remainings for your LCD.

The GPIO alternate functions are defines using the Pin class, with Pin.AF_xx flag:

http://micropython.org/doc/module/pyb/Pin

Hope this helps.

BTW,such LCD can be driven with only 8 pins, using the 4-bit data bus feature.
Frédéric

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

Re: "Free" pins to use for GPIO purposes

Post by dhylands » Sun May 18, 2014 5:57 pm

I have some reference code for driving those types of LCDs.

I did some python code for the beaglebone talking to an LCD with an i2c backpack:
https://github.com/dhylands/python_lcd

It has a "core" part in the https://github.com/dhylands/python_lcd/ ... lcd/lcd.py
and then the part that knows how the LCD is connected is here:
https://github.com/dhylands/python_lcd/ ... i2c_lcd.py

This would probably need to be tweaked for MicroPython, and you'd need to create a gpio_lcd.py piece

The python code was inspired by some C code that I had previously written:
https://github.com/dhylands/projects/bl ... mmon/lcd.h
https://github.com/dhylands/projects/bl ... /lcd-hal.h
https://github.com/dhylands/projects/bl ... /lcd-api.c
https://github.com/dhylands/projects/bl ... -hal-avr.c

Other versions of the code I've looked at always intermingle the layer that knows stuff about the LCD with the layer that knows how to talk to the LCD (i.e. i2c, gpio)

fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: "Free" pins to use for GPIO purposes

Post by fma » Sun May 18, 2014 6:05 pm

Very usefull! Thanks for sharing :)
Frédéric

justatoms
Posts: 2
Joined: Sat May 17, 2014 10:37 pm

Re: "Free" pins to use for GPIO purposes

Post by justatoms » Mon May 19, 2014 7:57 am

Thanks Frédéric and Dave!

Frédéric, you speak of a class named Pin.AF_xx. I checked the pyb module docs, but could not find it. Could you provide me with some more info on this class or a pointer where to find?

Dave, checked your code and it looks like the programming work I had anticipated is mostly done already (!). (Generic class to output to LCD as well as the lower level I2C interfacing.)

Although my initial thought was that there are more than enough IO pins available to implement a fully parallel pyb->LCD connection, it seems that a serial link (I2C) is the smarter way. So, I'm reading up on the PCF8574/5.

Anne.

fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: "Free" pins to use for GPIO purposes

Post by fma » Mon May 19, 2014 9:47 pm

AF_PP and AF_OD flags are Pin class constant attributes. See:

http://micropython.org/doc/module/pyb/Pin

(scroll down to init() method description).

I didn't check that these constants exist in the current implementation, but they are described in the documentation, so...
Frédéric

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

Re: "Free" pins to use for GPIO purposes

Post by dhylands » Tue May 20, 2014 12:26 am

Hi Anne,
justatoms wrote:Thanks Frédéric and Dave!

Dave, checked your code and it looks like the programming work I had anticipated is mostly done already (!). (Generic class to output to LCD as well as the lower level I2C interfacing.)

Although my initial thought was that there are more than enough IO pins available to implement a fully parallel pyb->LCD connection, it seems that a serial link (I2C) is the smarter way. So, I'm reading up on the PCF8574/5.

Anne.
There are several places you can get i2c backpacks for LCD displays.
AdaFruit sells one: http://www.adafruit.com/products/772 although it uses the MCP23017 chip for i2c to parallel conversion, which has slightly different commands that the PCF8574.

I purchased my i2c backpack off of eBay: http://www.ebay.com/itm/IIC-I2C-Serial- ... 0802573051 - I'm pretty sure that this is the module the code was written for. You need to be careful since there are several different styles of 8574 based backpacks and several have different pin outs (the mapping of the pins from the 8574 to the LCD).

I have one of the adafruit backpacks as well - just haven't gotten around to writing any code for it yet.

Post Reply