Improve SSD1306 spi example

Don't acquire bus for every command
Use SPI driver to control chip select
Use SPI driver start/stop only when bus acquired for lower power usage
Throttle SPI speed based on processor clock
This commit is contained in:
ergosys 2014-12-11 23:34:08 -08:00
parent 3b12678e6e
commit cb115186c5

View file

@ -16,65 +16,60 @@
#ifndef _GDISP_LLD_BOARD_H #ifndef _GDISP_LLD_BOARD_H
#define _GDISP_LLD_BOARD_H #define _GDISP_LLD_BOARD_H
// The command byte to put on the front of each page line // Pin & SPI setup
#define SSD1306_PAGE_PREFIX 0x40 // Co = 0, D/C = 1
// For a multiple display configuration we would put all this in a structure and then #define SPI_DRIVER (&SPID2)
// set g->board to that structure. #define SPI_PORT GPIOB
#define SSD1306_RESET_PORT GPIOB #define SCK_PAD 13
#define SSD1306_RESET_PIN 5 #define MISO_PAD 14
#define SSD1306_MISO_PORT GPIOB #define MOSI_PAD 15
#define SSD1306_MISO_PIN 8
#define SSD1306_MOSI_PORT GPIOB
#define SSD1306_MOSI_PIN 7
#define SSD1306_SCK_PORT GPIOB
#define SSD1306_SCK_PIN 6
#define SSD1306_CS_PORT GPIOB
#define SSD1306_CS_PIN 5
#define SET_RST palSetPad(SSD1306_RESET_PORT, SSD1306_RESET_PIN);
#define CLR_RST palClearPad(SSD1306_RESET_PORT, SSD1306_RESET_PIN);
/* #define CS_PORT GPIOC
* SPI1 configuration structure. #define RESET_PORT GPIOC
* Speed 42MHz, CPHA=0, CPOL=0, 8bits frames, MSb transmitted first. #define DNC_PORT GPIOC
* The slave select line is the pin 4 on the port GPIOA. #define CS_PAD 7 // 0 = chip selected
*/ #define RESET_PAD 8 // 0 = reset
static const SPIConfig spi1config = { #define DNC_PAD 9 // control=0, data=1
0,
/* HW dependent part.*/
SSD1306_MISO_PORT,
SSD1306_MISO_PIN,
0
//SPI_CR1_BR_0
};
#if GFX_USE_OS_CHIBIOS static SPIConfig spi_cfg = { NULL, CS_PORT, CS_PAD, 0 };
static int32_t thdPriority = 0;
#endif
static inline void init_board(GDisplay *g) { static inline void init_board(GDisplay *g) {
unsigned i; (void) g;
// As we are not using multiple displays we set g->board to NULL as we don't use it.
g->board = 0; g->board = 0;
// Maximum speed of SSD1306 is 10Mhz, so set SPI speed less or = to that.
//
// STM32 specific setup
// STM32_PCLK1 is APB1 frequence in hertz.
// STM32_PCLK2 is APB2 frequence in hertz.
// See manual clock diagram to determine APB1 or APB2 for spi in use.
// SPI2 uses APB1 clock on stm32151
// BR bits divide PCLK as follows
// 000 /2 = 16 MHz
// 001 /4 = 8 MHz
// 010 /8 = 4 MHz
// 011 /16 = 2 MHz
// 100 /32 = 1 MHz
// 101 /64 = 500 kHz
// 110 /128 = 250 kHz
// 111 /256 = 125 kHz
unsigned long spi_clk = STM32_PCLK1 / 2;
unsigned code = 0;
while (spi_clk > 10000000) {
code++;
spi_clk /= 2;
}
spi_cfg.cr1 |= (code << 3);
if (g->controllerdisplay == 0) {
switch(g->controllerdisplay) { palSetPadMode(SPI_PORT, SCK_PAD, PAL_MODE_ALTERNATE(5)|PAL_STM32_OTYPE_PUSHPULL|PAL_STM32_OSPEED_MID2);
case 0: // Set up for Display 0 palSetPadMode(SPI_PORT, MOSI_PAD, PAL_MODE_ALTERNATE(5)|PAL_STM32_OTYPE_PUSHPULL|PAL_STM32_OSPEED_MID2);
// RESET pin. palSetPadMode(SPI_PORT, MISO_PAD, PAL_MODE_ALTERNATE(5));
palSetPadMode(SSD1306_RESET_PORT, SSD1306_RESET_PIN, PAL_MODE_OUTPUT_PUSHPULL); palSetPadMode(RESET_PORT, RESET_PAD, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(CS_PORT, CS_PAD, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(SSD1306_MISO_PORT, SSD1306_MISO_PIN, PAL_MODE_ALTERNATE(1)| palSetPadMode(DNC_PORT, DNC_PAD, PAL_MODE_OUTPUT_PUSHPULL);
PAL_STM32_OSPEED_HIGHEST); palSetPad(CS_PORT, CS_PAD);
palSetPadMode(SSD1306_MOSI_PORT, SSD1306_MOSI_PIN, PAL_MODE_ALTERNATE(1)| palSetPad(RESET_PORT, RESET_PAD);
PAL_STM32_OSPEED_HIGHEST); palClearPad(DNC_PORT, DNC_PAD);
palSetPadMode(SSD1306_SCK_PORT, SSD1306_SCK_PIN, PAL_MODE_ALTERNATE(1)|
PAL_STM32_OSPEED_HIGHEST);
palSetPad(SSD1306_CS_PORT, SSD1306_CS_PIN);
palSetPadMode(SSD1306_CS_PORT, SSD1306_CS_PIN, PAL_MODE_ALTERNATE(1)|
PAL_STM32_OSPEED_HIGHEST);
spiInit();
break;
} }
} }
@ -82,55 +77,39 @@ static inline void post_init_board(GDisplay *g) {
(void) g; (void) g;
} }
static inline void setpin_reset(GDisplay *g, bool_t state) { static inline void setpin_reset(GDisplay *g, bool_t state) {
(void) g; (void) g;
if(state) palWritePad(RESET_PORT, RESET_PAD, !state);
CLR_RST
else
SET_RST
} }
static inline void acquire_bus(GDisplay *g) { static inline void acquire_bus(GDisplay *g) {
(void) g; (void) g;
#if GFX_USE_OS_CHIBIOS spiAcquireBus(SPI_DRIVER);
thdPriority = (int32_t)chThdGetPriority(); spiStart(SPI_DRIVER, &spi_cfg);
chThdSetPriority(HIGHPRIO); spiSelect(SPI_DRIVER);
#endif
spiAcquireBus(&SPID1);
} }
static inline void release_bus(GDisplay *g) { static inline void release_bus(GDisplay *g) {
(void) g; (void) g;
#if GFX_USE_OS_CHIBIOS spiUnselect(SPI_DRIVER);
chThdSetPriority(thdPriority); spiStop(SPI_DRIVER);
#endif spiReleaseBus(SPI_DRIVER);
spiReleaseBus(&SPID1);
} }
static inline void write_cmd(GDisplay *g, uint8_t cmd) { static inline void write_cmd(GDisplay *g, uint8_t cmd) {
uint8_t command[2];
(void) g; (void) g;
static uint8_t buf;
command[0] = 0x00; // Co = 0, D/C = 0 palClearPad(DNC_PORT, DNC_PAD);
command[1] = cmd; buf = cmd;
spiSend(SPI_DRIVER, 1, &buf);
spiStart(&SPID1, &spi1config);
spiSelect(&SPID1);
spiStartSend(&SPID1, 2, command);
spiUnselect(&SPID1);
spiStop(&SPID1);
} }
static inline void write_data(GDisplay *g, uint8_t* data, uint16_t length) { static inline void write_data(GDisplay *g, uint8_t* data, uint16_t length) {
(void) g; (void) g;
palSetPad(DNC_PORT, DNC_PAD);
spiStart(&SPID1, &spi1config); spiSend(SPI_DRIVER, length, data);
spiSelect(&SPID1);
spiStartSend(&SPID1, length, data);
spiUnselect(&SPID1);
spiStop(&SPID1);
} }
#endif /* _GDISP_LLD_BOARD_H */ #endif /* _GDISP_LLD_BOARD_H */