Drive a SSD1306 display with STM32F4 Discovery board and Micropython via SPI

It wasn't trivial to set it up (at least for me), probably it worth to document it, so here is how you can connect and drive an SSD1306 display from Micropython running on an STM32F4 Discovery board:

Wiring

The labeling of the display pins is a bit ambigous,  so here is the pinout i've used:

Pin label  Function Discovery Pin
GND   GND GND
VCC   3V3 or 5V 5V
DO   SPI CLOCK PA5
D1  SPI MOSI PA7
RES   Reset PA3
DC   Data/Command PB1
CS   SPI Chip Select PC5

And this how it looks like when hooked up:

Wiring

Driver

There is already a driver for SSD1306 in the main Micropython source tree which implements both I2C and SPI, but for me the SPI version didn't work, the SPI.init() call missed a parameter:

>>> d = SSD1306_SPI(width, height, spibus, dc, res, cs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ssd1306.py", line 144, in __init__
  File "ssd1306.py", line 49, in __init__
  File "ssd1306.py", line 74, in init_display
  File "ssd1306.py", line 147, in write_cmd
TypeError: 'mode' argument required

After adding the missing parameter and some sanitizing (Pull request sent to upstream https://github.com/micropython/micropython/pull/3385) a simple test script works well:

from ssd1306 import SSD1306_SPI
from pyb import SPI
width = 128
height = 64
spibus = SPI(1, SPI.MASTER, baudrate=600000, polarity=1, phase=0,crc=0x7)
dc = pyb.Pin.board.PB1
res = pyb.Pin.board.PA3
cs =  pyb.Pin.board.PC5


d = SSD1306_SPI(width, height, spibus, dc, res, cs)
d.poweron()
d.text('Hello There!',0,0)
d.show()

The whole source code (with the patched driver) available here: https://git.zgyarmati.de/zgyarmati/micropython-demo. You just need upload the content of the ssd1306 directory  onto your (Micropython flashed) STM32F4 Discovery board to get it running.

Update 2017-11-05: turned out that the driver supposed to be used with the 'new' machine.SPI Micropython API, so my fix in the driver isn't needed. During testing this i found some room for improvements in the I2C variant of the driver, and also added a test script, se a new PR was sent  as https://github.com/micropython/micropython/pull/3411. Also updated the  corresponding files at https://git.zgyarmati.de/zgyarmati/micropython-demo

Currently unrated