diff --git a/firmware/shared_libs/drivers/st7565/st7565.c b/firmware/shared_libs/drivers/st7565/st7565.c new file mode 100644 index 0000000..ac329d8 --- /dev/null +++ b/firmware/shared_libs/drivers/st7565/st7565.c @@ -0,0 +1,90 @@ +#include "main.h" +#include "display_gfx.h" +#include "st7565.h" +// #include "spi.h" +#include "string.h" + +uint8_t st7565_buffor[ST7565_BUFFER_SIZE]; + +void ST7565_SendCommand(uint8_t data) +{ + HAL_GPIO_WritePin(ST7565_A0_GPIO_Port, ST7565_A0_Pin, GPIO_PIN_RESET); + + HAL_SPI_Transmit(&hspi3, &data, 1, HAL_MAX_DELAY); +} + +void ST7565_SendData(uint8_t *data, uint16_t size) +{ + HAL_GPIO_WritePin(ST7565_A0_GPIO_Port, ST7565_A0_Pin, GPIO_PIN_SET); + + HAL_SPI_Transmit(&hspi3, data, size, HAL_MAX_DELAY); +} + +void ST7565_Init(GFX_display_t *disp) +{ + disp->width = ST7565_WIDTH; + disp->height = ST7565_HEIGHT; + disp->buffor = st7565_buffor; + // toggle RST low to reset; CS low so it'll listen to us + HAL_GPIO_WritePin(ST7565_CS_GPIO_Port, ST7565_CS_Pin, GPIO_PIN_RESET); + + HAL_GPIO_WritePin(ST7565_RST_GPIO_Port, ST7565_RST_Pin, GPIO_PIN_RESET); + HAL_Delay(500); + HAL_GPIO_WritePin(ST7565_RST_GPIO_Port, ST7565_RST_Pin, GPIO_PIN_SET); + + // LCD bias select + ST7565_SendCommand(CMD_SET_BIAS_7); + // ADC select + ST7565_SendCommand(CMD_SET_ADC_REVERSE); + // SHL select + ST7565_SendCommand(CMD_SET_COM_NORMAL); + // Initial display line + ST7565_SendCommand(CMD_SET_DISP_START_LINE); + + // turn on voltage converter (VC=1, VR=0, VF=0) + ST7565_SendCommand(CMD_SET_POWER_CONTROL | 0x4); + // wait for 50% rising + HAL_Delay(50); + + // turn on voltage regulator (VC=1, VR=1, VF=0) + ST7565_SendCommand(CMD_SET_POWER_CONTROL | 0x6); + // wait >=50ms + HAL_Delay(50); + + // turn on voltage follower (VC=1, VR=1, VF=1) + ST7565_SendCommand(CMD_SET_POWER_CONTROL | 0x7); + // wait + HAL_Delay(10); + + // set lcd operating voltage (regulator resistor, ref voltage resistor) + ST7565_SendCommand(CMD_SET_RESISTOR_RATIO | 0x6); + + ST7565_SendCommand(CMD_DISPLAY_ON); + ST7565_SendCommand(CMD_SET_ALLPTS_NORMAL); + ST7565_SendCommand(CMD_SET_VOLUME_FIRST); + ST7565_SendCommand(CMD_SET_VOLUME_SECOND | (0x00 & 0x3f)); + + // initial display line + // set page address + // set column address + // write display data + + // set up a bounding box for screen updates + + // updateBoundingBox(0, 0, LCDWIDTH - 1, LCDHEIGHT - 1); + HAL_GPIO_WritePin(ST7565_CS_GPIO_Port, ST7565_CS_Pin, GPIO_PIN_SET); +} + +void ST7565_DisplayAll(void) +{ + HAL_GPIO_WritePin(ST7565_CS_GPIO_Port, ST7565_CS_Pin, GPIO_PIN_RESET); + for (uint8_t p = 0; p < 8; p++) + { + ST7565_SendCommand(CMD_SET_PAGE | p); + ST7565_SendCommand(CMD_SET_COLUMN_UPPER | 0); + ST7565_SendCommand(CMD_SET_COLUMN_LOWER | 0); + + ST7565_SendData(&st7565_buffor[128 * p], 128); + } + HAL_GPIO_WritePin(ST7565_CS_GPIO_Port, ST7565_CS_Pin, GPIO_PIN_SET); +} diff --git a/firmware/shared_libs/drivers/st7565/st7565.h b/firmware/shared_libs/drivers/st7565/st7565.h new file mode 100644 index 0000000..f1e5a3d --- /dev/null +++ b/firmware/shared_libs/drivers/st7565/st7565.h @@ -0,0 +1,47 @@ +#pragma once + +#define ST7565_WIDTH 128 +#define ST7565_HEIGHT 64 +#define ST7565_BUFFER_SIZE ST7565_WIDTH *ST7565_HEIGHT / 8 + +#define CMD_DISPLAY_OFF 0xAE +#define CMD_DISPLAY_ON 0xAF + +#define CMD_SET_DISP_START_LINE 0x40 +#define CMD_SET_PAGE 0xB0 + +#define CMD_SET_COLUMN_UPPER 0x10 +#define CMD_SET_COLUMN_LOWER 0x00 + +#define CMD_SET_ADC_NORMAL 0xA0 +#define CMD_SET_ADC_REVERSE 0xA1 + +#define CMD_SET_DISP_NORMAL 0xA6 +#define CMD_SET_DISP_REVERSE 0xA7 + +#define CMD_SET_ALLPTS_NORMAL 0xA4 +#define CMD_SET_ALLPTS_ON 0xA5 +#define CMD_SET_BIAS_9 0xA2 +#define CMD_SET_BIAS_7 0xA3 + +#define CMD_RMW 0xE0 +#define CMD_RMW_CLEAR 0xEE +#define CMD_INTERNAL_RESET 0xE2 +#define CMD_SET_COM_NORMAL 0xC0 +#define CMD_SET_COM_REVERSE 0xC8 +#define CMD_SET_POWER_CONTROL 0x28 +#define CMD_SET_RESISTOR_RATIO 0x20 +#define CMD_SET_VOLUME_FIRST 0x81 +#define CMD_SET_VOLUME_SECOND 0 +#define CMD_SET_STATIC_OFF 0xAC +#define CMD_SET_STATIC_ON 0xAD +#define CMD_SET_STATIC_REG 0x0 +#define CMD_SET_BOOSTER_FIRST 0xF8 +#define CMD_SET_BOOSTER_234 0 +#define CMD_SET_BOOSTER_5 1 +#define CMD_SET_BOOSTER_6 3 +#define CMD_NOP 0xE3 +#define CMD_TEST 0xF0 + +void ST7565_Init(GFX_display_t *disp); +void ST7565_DisplayAll(void);