SPI3 support ssd1306 (#309)

SPI3 support, SH1106 port
This commit is contained in:
Zaltora 2016-12-29 15:42:43 +01:00 committed by Ruslan V. Uss
parent 0b063730f3
commit 14c8ff57ca
3 changed files with 208 additions and 36 deletions

View file

@ -9,4 +9,8 @@
#define SSD1306_SPI4_SUPPORT 1
#endif
#ifndef SSD1306_SPI3_SUPPORT
#define SSD1306_SPI3_SUPPORT 1
#endif
#endif /* _EXTRAS_SSD1306_CONFIG_H_ */

View file

@ -14,7 +14,7 @@
#if (SSD1306_I2C_SUPPORT)
#include <i2c/i2c.h>
#endif
#if (SSD1306_SPI4_SUPPORT)
#if (SSD1306_SPI4_SUPPORT) || (SSD1306_SPI3_SUPPORT)
#include <esp/spi.h>
#endif
#include <esp/gpio.h>
@ -70,6 +70,15 @@
#define SSD1306_SCROLL_ENABLE (0x2F)
#define SSD1306_SCROLL_DISABLE (0x2E)
#define SH1106_SET_CHARGE_PUMP (0xAD)
#define SH1106_CHARGE_PUMP_EN (0x8B)
#define SH1106_CHARGE_PUMP_DIS (0x8A)
#define SH1106_CHARGE_PUMP_VALUE (0x30)
#define SH1106_SET_PAGE_ADDRESS (0xB0)
#define SH1106_SET_LOW_COL_ADDR (0x00)
#define SH1106_SET_HIGH_COL_ADDR (0x10)
#ifdef SSD1306_DEBUG
#define debug(fmt, ...) printf("%s: " fmt "\n", "SSD1306", ## __VA_ARGS__)
#else
@ -118,6 +127,15 @@ int ssd1306_command(const ssd1306_t *dev, uint8_t cmd)
spi_transfer_8(SPI_BUS, cmd);
gpio_write(dev->cs_pin, true);
break;
#endif
#if (SSD1306_SPI3_SUPPORT)
case SSD1306_PROTO_SPI3:
gpio_write(dev->cs_pin, false);
spi_set_command(SPI_BUS,1,0); // command mode
spi_transfer_8(SPI_BUS, cmd);
spi_clear_command(SPI_BUS);
gpio_write(dev->cs_pin, true);
break;
#endif
default:
debug("Unsupported protocol");
@ -157,12 +175,20 @@ int ssd1306_init(const ssd1306_t *dev)
gpio_enable(dev->dc_pin, GPIO_OUTPUT);
spi_init(SPI_BUS, SPI_MODE0, SPI_FREQ_DIV_8M, true, SPI_LITTLE_ENDIAN, true);
break;
#endif
#if (SSD1306_SPI3_SUPPORT)
case SSD1306_PROTO_SPI3:
gpio_enable(dev->cs_pin, GPIO_OUTPUT);
gpio_write(dev->cs_pin, true);
spi_init(SPI_BUS, SPI_MODE0, SPI_FREQ_DIV_8M, true, SPI_LITTLE_ENDIAN, true);
break;
#endif
default:
debug("Unsupported protocol");
return -EPROTONOSUPPORT;
}
switch (dev->screen) {
case SSD1306_SCREEN:
if (!ssd1306_display_on(dev, false) &&
!ssd1306_set_osc_freq(dev, 0x80) &&
!ssd1306_set_mux_ratio(dev, dev->height - 1) &&
@ -181,24 +207,59 @@ int ssd1306_init(const ssd1306_t *dev)
!ssd1306_display_on(dev, true)) {
return 0;
}
break;
case SH1106_SCREEN:
if (!ssd1306_display_on(dev, false) &&
!ssd1306_set_charge_pump_enabled(dev, true) &&
!sh1106_set_charge_pump_voltage(dev,SH1106_VOLTAGE_74) &&
!ssd1306_set_osc_freq(dev, 0x80) &&
!ssd1306_set_mux_ratio(dev, dev->height - 1) &&
!ssd1306_set_display_offset(dev, 0x0) &&
!ssd1306_set_display_start_line(dev, 0x0) &&
!ssd1306_set_segment_remapping_enabled(dev, true) &&
!ssd1306_set_scan_direction_fwd(dev, true) &&
!ssd1306_set_com_pin_hw_config(dev, pin_cfg) &&
!ssd1306_set_contrast(dev, 0x9f) &&
!ssd1306_set_precharge_period(dev, 0xf1) &&
!ssd1306_set_deseltct_lvl(dev, 0x40) &&
!ssd1306_set_whole_display_lighting(dev, true) &&
!ssd1306_set_inversion(dev, false) &&
!ssd1306_display_on(dev, true)) {
return 0;
}
break;
}
return -EIO;
}
static int sh1106_go_coordinate(const ssd1306_t *dev, uint8_t x, uint8_t y) {
if (x >= dev->width || y >= (dev->height/8)) return -EINVAL;
int err = 0;
x+=2 ; //offset : panel is 128 ; RAM is 132 for sh1106
if ((err = ssd1306_command(dev, SH1106_SET_PAGE_ADDRESS + y))) // Set row
return err;
if ((err = ssd1306_command(dev, SH1106_SET_LOW_COL_ADDR | (x & 0xf)))) // Set lower column address
return err;
return ssd1306_command(dev, SH1106_SET_HIGH_COL_ADDR | (x >> 4)); //Set higher column address
}
int ssd1306_load_frame_buffer(const ssd1306_t *dev, uint8_t buf[])
{
uint16_t i;
uint8_t j;
size_t len = dev->width * dev->height / 8;
if(dev->screen == SSD1306_SCREEN)
{
ssd1306_set_column_addr(dev, 0, dev->width - 1);
ssd1306_set_page_addr(dev, 0, dev->height / 8 - 1);
size_t len = dev->width * dev->height / 8;
}
switch (dev->protocol) {
#if (SSD1306_I2C_SUPPORT)
case SSD1306_PROTO_I2C:
for (i = 0; i < len; i++) {
if(dev->screen == SH1106_SCREEN) sh1106_go_coordinate(dev,0,i/dev->width);
i2c_start();
if (!i2c_write(dev->addr << 1)) {
debug("Error while xmitting I2C slave address\n");
@ -227,14 +288,56 @@ int ssd1306_load_frame_buffer(const ssd1306_t *dev, uint8_t buf[])
#endif
#if (SSD1306_SPI4_SUPPORT)
case SSD1306_PROTO_SPI4:
gpio_write(dev->dc_pin, true); // data mode
gpio_write(dev->cs_pin, false);
if(dev->screen == SSD1306_SCREEN)
{
gpio_write(dev->dc_pin, true); // data mode
if (buf)
spi_transfer(SPI_BUS, buf, NULL, len, SPI_8BIT);
else
for (i = 0; i < len; i ++) {
spi_transfer_8(SPI_BUS, 0);
spi_repeat_send_8(SPI_BUS,0,len);
}
else
{
for (i = 0 ; i < (dev->height/8) ; i++) {
sh1106_go_coordinate(dev,0,i);
gpio_write(dev->dc_pin, true); // data mode
gpio_write(dev->cs_pin, false);
if (buf)
spi_transfer(SPI_BUS, &buf[dev->width*i], NULL, dev->width, SPI_8BIT);
else
spi_repeat_send_8(SPI_BUS,0,dev->width);
}
}
gpio_write(dev->cs_pin, true);
break;
#endif
#if (SSD1306_SPI3_SUPPORT)
case SSD1306_PROTO_SPI3:
gpio_write(dev->cs_pin, false);
if(dev->screen == SSD1306_SCREEN)
{
spi_set_command(SPI_BUS,1,1); // data mode
if (buf)
spi_transfer(SPI_BUS, buf, NULL, len, SPI_8BIT);
else
spi_repeat_send_8(SPI_BUS,0,len);
}
else
{
for (i = 0 ; i < (dev->height/8) ; i++) {
sh1106_go_coordinate(dev,0,i);
spi_set_command(SPI_BUS,1,1); // data mode
gpio_write(dev->cs_pin, false);
if (buf)
for (j = 0 ; j < dev->width ; j++)
spi_transfer_8(SPI_BUS, buf[dev->width*i+j]);
else
for (j = 0 ; j < dev->width ; j++)
spi_transfer_8(SPI_BUS, buf[dev->width*i+j]);
}
}
spi_clear_command(SPI_BUS);
gpio_write(dev->cs_pin, true);
break;
#endif
@ -271,17 +374,44 @@ int ssd1306_set_display_offset(const ssd1306_t *dev, uint8_t offset)
return ssd1306_command(dev, offset);
}
int sh1106_set_charge_pump_voltage(const ssd1306_t *dev, sh1106_voltage_t select)
{
if (dev->screen == SSD1306_SCREEN)
{
debug("Unsupported screen type");
return -ENOTSUP ;
}
return ssd1306_command(dev, select | SH1106_CHARGE_PUMP_VALUE);
}
int ssd1306_set_charge_pump_enabled(const ssd1306_t *dev, bool enabled)
{
int err = 0;
switch (dev->screen) {
case SH1106_SCREEN:
if ((err = ssd1306_command(dev, SH1106_SET_CHARGE_PUMP)))
return err;
return ssd1306_command(dev, enabled ? SH1106_CHARGE_PUMP_EN : SH1106_CHARGE_PUMP_DIS);
break;
case SSD1306_SCREEN:
if ((err = ssd1306_command(dev, SSD1306_SET_CHARGE_PUMP)))
return err;
return ssd1306_command(dev, enabled ? SSD1306_CHARGE_PUMP_EN : SSD1306_CHARGE_PUMP_DIS);
break;
default:
debug("Unsupported screen type");
return -ENOTSUP;
}
}
int ssd1306_set_mem_addr_mode(const ssd1306_t *dev, ssd1306_mem_addr_mode_t mode)
{
if (dev->screen == SH1106_SCREEN)
{
debug("Unsupported screen type");
return -ENOTSUP ;
}
int err = 0;
if ((err = ssd1306_command(dev, SSD1306_SET_MEM_ADDR_MODE)))
return err;
@ -390,7 +520,6 @@ int ssd1306_set_whole_display_lighting(const ssd1306_t *dev, bool light)
return ssd1306_command(dev, light ? SSD1306_SET_ENTIRE_DISP_ON : SSD1306_SET_ENTIRE_DISP_OFF);
}
/* one byte of xbm - 8 dots in line of picture source
* one byte of fb - 8 rows for 1 column of screen
*/

View file

@ -28,6 +28,17 @@ extern "C"
{
#endif
/**
* SH1106 pump voltage value
*/
typedef enum
{
SH1106_VOLTAGE_74 = 0, // 7.4 Volt
SH1106_VOLTAGE_80, // 8.0 Volt
SH1106_VOLTAGE_84, // 8.4 Volt
SH1106_VOLTAGE_90 // 9.0 Volt
} sh1106_voltage_t;
/**
* I/O protocols
*/
@ -35,20 +46,32 @@ typedef enum
{
SSD1306_PROTO_I2C = 0, //!< I2C
SSD1306_PROTO_SPI4, //!< SPI 8 bits + D/C pin
SSD1306_PROTO_SPI3 //!< SPI 9 bits, currently not supported
SSD1306_PROTO_SPI3 //!< SPI 9 bits
} ssd1306_protocol_t;
/**
* Screen type
*/
typedef enum
{
SSD1306_SCREEN = 0,
SH1106_SCREEN
} ssd1306_screen_t;
/**
* Device descriptor
*/
typedef struct
{
ssd1306_protocol_t protocol;
ssd1306_screen_t screen ;
union {
#if (SSD1306_I2C_SUPPORT)
uint8_t addr ; //!< I2C address, used by SSD1306_PROTO_I2C
#endif
#if (SSD1306_SPI4_SUPPORT)
uint8_t cs_pin ; //!< Chip Select GPIO pin, used by SSD1306_PROTO_SPI3, SSD1306_PROTO_SPI4
} ;
#if (SSD1306_SPI4_SUPPORT)
uint8_t dc_pin; //!< Data/Command GPIO pin, used by SSD1306_PROTO_SPI4
#endif
uint8_t width; //!< Screen width, currently supported 128px, 96px
@ -161,6 +184,22 @@ int ssd1306_set_display_start_line(const ssd1306_t *dev, uint8_t start);
*/
int ssd1306_set_display_offset(const ssd1306_t *dev, uint8_t offset);
/**
* Select charge pump voltage. See value in datasheet.
* @param dev Pointer to device descriptor
* @param select Select charge pump voltage value
* @return Non-zero if error occured
*/
int sh1106_set_charge_pump_voltage(const ssd1306_t *dev, sh1106_voltage_t select);
/**
* Select charge pump voltage. See value in datasheet.
* @param dev Pointer to device descriptor
* @param select Select charge pump voltage value
* @return Non-zero if error occured
*/
int sh1106_set_charge_pump_voltage(const ssd1306_t *dev, sh1106_voltage_t select);
/**
* Enable or disable the charge pump. See application note in datasheet.
* @param dev Pointer to device descriptor