Atmel Software Framework

SSD1306 OLED Controller Low-level driver

This is a low level driver for the SSD1306 OLED controller through 4-wire SPI.

It provides basic functions for initializing and writing to the OLED controller. In addition to hardware control and use of the OLED controller internal functions.

Before writing data to the display call ssd1306_init() which will set up the physical interface and the OLED. A file named conf_ssd1306.h is needed to define which interface to use. For more information see the Interface selection section. In addition one also need to define the pins SSD1306_DC_PIN, SSD1306_CS_PIN and SSD1306_RES_PIN and the display SSD1306_CLOCK_SPEED.

Warning
This driver is not reentrant and can not be used in interrupt\ service routines without extra care.

An example conf_ssd1306.h file could look like

// interface selection
#define SSD1306_USART_SPI_INTERFACE
#define SSD1306_USART_SPI &USARTD0
#define SSD1306_CLOCK_SPEED 1000000
#define SSD1306_DC_PIN UG_2832HSWEG04_DATA_CMD
#define SSD1306_CS_PIN UG_2832HSWEG04_RESET
#define SSD1306_RES_PIN UG_2832HSWEG04_SS

Dependencies

This driver depends on the following modules:

Modules

 

Macros

#define SSD1306_LATENCY   10
 
#define ssd1306_reset_clear()   arch_ioport_set_pin_level(SSD1306_RES_PIN, false)
 
#define ssd1306_reset_set()   arch_ioport_set_pin_level(SSD1306_RES_PIN, true)
 
#define ssd1306_sel_cmd()   arch_ioport_set_pin_level(SSD1306_DC_PIN, false)
 
#define ssd1306_sel_data()   arch_ioport_set_pin_level(SSD1306_DC_PIN, true)
 
#define SSD1306_SERIAL_INTERFACE
 

Fundamental Command defines

#define SSD1306_CMD_SET_LOW_COL(column)   (0x00 | (column))
 
#define SSD1306_CMD_SET_HIGH_COL(column)   (0x10 | (column))
 
#define SSD1306_CMD_SET_MEMORY_ADDRESSING_MODE   0x20
 
#define SSD1306_CMD_SET_COLUMN_ADDRESS   0x21
 
#define SSD1306_CMD_SET_PAGE_ADDRESS   0x22
 
#define SSD1306_CMD_SET_START_LINE(line)   (0x40 | (line))
 
#define SSD1306_CMD_SET_CONTRAST_CONTROL_FOR_BANK0   0x81
 
#define SSD1306_CMD_SET_CHARGE_PUMP_SETTING   0x8D
 
#define SSD1306_CMD_SET_SEGMENT_RE_MAP_COL0_SEG0   0xA0
 
#define SSD1306_CMD_SET_SEGMENT_RE_MAP_COL127_SEG0   0xA1
 
#define SSD1306_CMD_ENTIRE_DISPLAY_AND_GDDRAM_ON   0xA4
 
#define SSD1306_CMD_ENTIRE_DISPLAY_ON   0xA5
 
#define SSD1306_CMD_SET_NORMAL_DISPLAY   0xA6
 
#define SSD1306_CMD_SET_INVERSE_DISPLAY   0xA7
 
#define SSD1306_CMD_SET_MULTIPLEX_RATIO   0xA8
 
#define SSD1306_CMD_SET_DISPLAY_ON   0xAF
 
#define SSD1306_CMD_SET_DISPLAY_OFF   0xAE
 
#define SSD1306_CMD_SET_PAGE_START_ADDRESS(page)   (0xB0 | (page & 0x07))
 
#define SSD1306_CMD_SET_COM_OUTPUT_SCAN_UP   0xC0
 
#define SSD1306_CMD_SET_COM_OUTPUT_SCAN_DOWN   0xC8
 
#define SSD1306_CMD_SET_DISPLAY_OFFSET   0xD3
 
#define SSD1306_CMD_SET_DISPLAY_CLOCK_DIVIDE_RATIO   0xD5
 
#define SSD1306_CMD_SET_PRE_CHARGE_PERIOD   0xD9
 
#define SSD1306_CMD_SET_COM_PINS   0xDA
 
#define SSD1306_CMD_SET_VCOMH_DESELECT_LEVEL   0xDB
 
#define SSD1306_CMD_NOP   0xE3
 

Graphic Acceleration Command defines

#define SSD1306_CMD_SCROLL_H_RIGHT   0x26
 
#define SSD1306_CMD_SCROLL_H_LEFT   0x27
 
#define SSD1306_CMD_CONTINUOUS_SCROLL_V_AND_H_RIGHT   0x29
 
#define SSD1306_CMD_CONTINUOUS_SCROLL_V_AND_H_LEFT   0x2A
 
#define SSD1306_CMD_DEACTIVATE_SCROLL   0x2E
 
#define SSD1306_CMD_ACTIVATE_SCROLL   0x2F
 
#define SSD1306_CMD_SET_VERTICAL_SCROLL_AREA   0xA3
 

Interface selection

The OLED controller support both serial and parallel mode, that means there is a number of possible ways of interfacing the controller using different peripherals.

The different interfaces can be selected using different defines. This driver supports the serial communication mode using an USART in Master SPI mode by defining SSD1306_USART_SPI_INTERFACE, and a normal SPI in Master Mode by defining SSD1306_SPI_INTERFACE.

Note
The current driver only support serial mode.
#define SSD1306_USART_SPI_INTERFACE
 Select the a USART SPI interface. More...
 
#define SSD1306_SPI_INTERFACE
 Select the normal SPI peripheral interface. More...
 

OLED controller write and read functions

static void ssd1306_write_command (uint8_t command)
 Writes a command to the display controller. More...
 
static void ssd1306_write_data (uint8_t data)
 Write data to the display controller. More...
 
static uint8_t ssd1306_read_data (void)
 Read data from the controller. More...
 
static uint8_t ssd1306_get_status (void)
 Read status from the controller. More...
 

OLED Controller reset

static void ssd1306_hard_reset (void)
 Perform a hard reset of the OLED controller. More...
 

Sleep control

static void ssd1306_sleep_enable (void)
 Enable the OLED sleep mode. More...
 
static void ssd1306_sleep_disable (void)
 Disable the OLED sleep mode. More...
 

Address setup for the OLED

static void ssd1306_set_page_address (uint8_t address)
 Set current page in display RAM. More...
 
static void ssd1306_set_column_address (uint8_t address)
 Set current column in display RAM. More...
 
static void ssd1306_set_display_start_line_address (uint8_t address)
 Set the display start draw line address. More...
 

Display hardware control

static void ssd1306_display_on (void)
 Turn the OLED display on. More...
 
static void ssd1306_display_off (void)
 Turn the OLED display off. More...
 
static uint8_t ssd1306_set_contrast (uint8_t contrast)
 Set the OLED contrast level. More...
 
static void ssd1306_display_invert_enable (void)
 Invert all pixels on the device. More...
 
static void ssd1306_display_invert_disable (void)
 Disable invert of all pixels on the device. More...
 
static void ssd1306_clear (void)
 

Initialization

void ssd1306_init (void)
 Initialize the OLED controller. More...
 

Write text routine

void ssd1306_write_text (const char *string)
 Display text on OLED screen. More...
 

#define SSD1306_CMD_ACTIVATE_SCROLL   0x2F
#define SSD1306_CMD_CONTINUOUS_SCROLL_V_AND_H_LEFT   0x2A
#define SSD1306_CMD_CONTINUOUS_SCROLL_V_AND_H_RIGHT   0x29
#define SSD1306_CMD_DEACTIVATE_SCROLL   0x2E
#define SSD1306_CMD_ENTIRE_DISPLAY_AND_GDDRAM_ON   0xA4

Referenced by ssd1306_init().

#define SSD1306_CMD_ENTIRE_DISPLAY_ON   0xA5
#define SSD1306_CMD_NOP   0xE3
#define SSD1306_CMD_SCROLL_H_LEFT   0x27
#define SSD1306_CMD_SCROLL_H_RIGHT   0x26
#define SSD1306_CMD_SET_CHARGE_PUMP_SETTING   0x8D

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_COLUMN_ADDRESS   0x21
#define SSD1306_CMD_SET_COM_OUTPUT_SCAN_DOWN   0xC8

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_COM_OUTPUT_SCAN_UP   0xC0
#define SSD1306_CMD_SET_COM_PINS   0xDA

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_CONTRAST_CONTROL_FOR_BANK0   0x81

Referenced by ssd1306_set_contrast().

#define SSD1306_CMD_SET_DISPLAY_CLOCK_DIVIDE_RATIO   0xD5

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_DISPLAY_OFF   0xAE
#define SSD1306_CMD_SET_DISPLAY_OFFSET   0xD3

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_DISPLAY_ON   0xAF
#define SSD1306_CMD_SET_HIGH_COL (   column)    (0x10 | (column))
#define SSD1306_CMD_SET_INVERSE_DISPLAY   0xA7
#define SSD1306_CMD_SET_LOW_COL (   column)    (0x00 | (column))
#define SSD1306_CMD_SET_MEMORY_ADDRESSING_MODE   0x20
#define SSD1306_CMD_SET_MULTIPLEX_RATIO   0xA8

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_NORMAL_DISPLAY   0xA6
#define SSD1306_CMD_SET_PAGE_ADDRESS   0x22
#define SSD1306_CMD_SET_PAGE_START_ADDRESS (   page)    (0xB0 | (page & 0x07))
#define SSD1306_CMD_SET_PRE_CHARGE_PERIOD   0xD9

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_SEGMENT_RE_MAP_COL0_SEG0   0xA0
#define SSD1306_CMD_SET_SEGMENT_RE_MAP_COL127_SEG0   0xA1

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_START_LINE (   line)    (0x40 | (line))
#define SSD1306_CMD_SET_VCOMH_DESELECT_LEVEL   0xDB

Referenced by ssd1306_init().

#define SSD1306_CMD_SET_VERTICAL_SCROLL_AREA   0xA3
#define SSD1306_LATENCY   10
#define ssd1306_reset_clear ( )    arch_ioport_set_pin_level(SSD1306_RES_PIN, false)
#define ssd1306_reset_set ( )    arch_ioport_set_pin_level(SSD1306_RES_PIN, true)
#define ssd1306_sel_cmd ( )    arch_ioport_set_pin_level(SSD1306_DC_PIN, false)
#define ssd1306_sel_data ( )    arch_ioport_set_pin_level(SSD1306_DC_PIN, true)

Referenced by ssd1306_write_data().

#define SSD1306_SERIAL_INTERFACE
#define SSD1306_SPI_INTERFACE

Select the normal SPI peripheral interface.

#define SSD1306_USART_SPI_INTERFACE

Select the a USART SPI interface.

static void ssd1306_display_invert_disable ( void  )
inlinestatic

Disable invert of all pixels on the device.

This function will disable invert on all pixels on the OLED

References SSD1306_CMD_SET_NORMAL_DISPLAY, and ssd1306_write_command().

Referenced by ssd1306_init().

static void ssd1306_display_invert_enable ( void  )
inlinestatic

Invert all pixels on the device.

This function will invert all pixels on the OLED

References SSD1306_CMD_SET_INVERSE_DISPLAY, and ssd1306_write_command().

static void ssd1306_display_off ( void  )
inlinestatic

Turn the OLED display off.

This function will turn off the OLED.

References SSD1306_CMD_SET_DISPLAY_OFF, and ssd1306_write_command().

static void ssd1306_display_on ( void  )
inlinestatic

Turn the OLED display on.

This function will turn on the OLED.

References SSD1306_CMD_SET_DISPLAY_ON, and ssd1306_write_command().

Referenced by ssd1306_init().

static uint8_t ssd1306_get_status ( void  )
inlinestatic

Read status from the controller.

Note
The controller does not support read in serial mode.
Return values
8bit status read from the controller
static void ssd1306_hard_reset ( void  )
inlinestatic

Perform a hard reset of the OLED controller.

This functions will reset the OLED controller by setting the reset pin low.

Note
this functions should not be confused with the ssd1306_soft_reset() function, this command will control the RST pin.

References arch_ioport_set_pin_level(), delay_us, and SSD1306_LATENCY.

Referenced by ssd1306_init().

static uint8_t ssd1306_read_data ( void  )
inlinestatic

Read data from the controller.

Note
The controller does not support read in serial mode.
Return values
8bit data read from the controller
static uint8_t ssd1306_set_contrast ( uint8_t  contrast)
inlinestatic

Set the OLED contrast level.

Parameters
contrasta number between 0 and 0xFF
Return values
contrastthe contrast value written to the OLED controller

References SSD1306_CMD_SET_CONTRAST_CONTROL_FOR_BANK0, and ssd1306_write_command().

Referenced by ssd1306_init().

static void ssd1306_set_display_start_line_address ( uint8_t  address)
inlinestatic

Set the display start draw line address.

This function will set which line should be the start draw line for the OLED.

References SSD1306_CMD_SET_START_LINE, and ssd1306_write_command().

Referenced by main().

static void ssd1306_set_page_address ( uint8_t  address)
inlinestatic

Set current page in display RAM.

This command is usually followed by the configuration of the column address because this scheme will provide access to all locations in the display RAM.

Parameters
addressthe page address

References SSD1306_CMD_SET_PAGE_START_ADDRESS, and ssd1306_write_command().

Referenced by font_bitmap_display(), main(), multi_language_show_card_size_info(), multi_language_show_end_info(), multi_language_show_file_name(), multi_language_show_sd_info(), multi_language_show_start_info(), multi_language_show_switch_info(), and ssd1306_clear().

static void ssd1306_sleep_disable ( void  )
inlinestatic

Disable the OLED sleep mode.

References SSD1306_CMD_SET_DISPLAY_ON, and ssd1306_write_command().

static void ssd1306_sleep_enable ( void  )
inlinestatic

Enable the OLED sleep mode.

References SSD1306_CMD_SET_DISPLAY_OFF, and ssd1306_write_command().

static void ssd1306_write_command ( uint8_t  command)
static
static void ssd1306_write_data ( uint8_t  data)
inlinestatic

Write data to the display controller.

This functions sets the pin D/C# before writing to the controller. Different data write function is called based on the selected interface.

Parameters
datathe data to write

References arch_ioport_set_pin_level(), delay_us, usart_spi_device::id, spi_device::id, spi_deselect_device(), spi_select_device(), spi_write_single(), SSD1306_LATENCY, ssd1306_sel_cmd, ssd1306_sel_data, usart_spi_deselect_device(), and usart_spi_select_device().

Referenced by font_bitmap_display(), main(), multi_language_show_file_name(), ssd1306_clear(), ssd1306_clear_char(), ssd1306_draw_graph(), and ssd1306_write_text().