steering-wheel/Core/Src/hx8357d.c

101 lines
3.5 KiB
C

#include "hx8357d.h"
#include "main.h"
#include "shorttimer.h"
void HX8357D_WriteData(uint8_t *data, size_t data_len) {
for (size_t byte = 0; byte < data_len; byte++) {
for (size_t bit = 0; bit < 8; bit++) {
GPIO_PinState state =
((data[byte] >> (7 - bit)) & 1) ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(DISPSPI_SCL_GPIO_Port, DISPSPI_SCL_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(DISPSPI_SDA_GPIO_Port, DISPSPI_SDA_Pin, state);
shorttimer_sleep(1);
HAL_GPIO_WritePin(DISPSPI_SCL_GPIO_Port, DISPSPI_SCL_Pin, GPIO_PIN_SET);
shorttimer_sleep(1);
}
}
}
void HX8357D_WriteReg(uint8_t addr, uint8_t *data, size_t data_len) {
HAL_GPIO_WritePin(DISPSPI_CSX_GPIO_Port, DISPSPI_CSX_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(DISPSPI_DCX_GPIO_Port, DISPSPI_DCX_Pin, GPIO_PIN_RESET);
shorttimer_sleep(1);
HX8357D_WriteData(&addr, 1);
HAL_GPIO_WritePin(DISPSPI_DCX_GPIO_Port, DISPSPI_DCX_Pin, GPIO_PIN_SET);
shorttimer_sleep(1);
if (data_len > 0) {
HX8357D_WriteData(data, data_len);
}
HAL_GPIO_WritePin(DISPSPI_CSX_GPIO_Port, DISPSPI_CSX_Pin, GPIO_PIN_SET);
}
void HX8357D_Init() {
HAL_GPIO_WritePin(DISPSPI_CSX_GPIO_Port, DISPSPI_CSX_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(DISPSPI_SCL_GPIO_Port, DISPSPI_SCL_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_RESET);
HAL_Delay(150);
HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_SET);
HAL_Delay(150);
}
void HX8357D_Mode_RGB666() {
// B9h: Enable extended commands
uint8_t data0[] = {0xFF, 0x83, 0x57};
HX8357D_WriteReg(0xB9, data0, sizeof(data0));
// B1h: Set power control
uint8_t data1[] = {0x00, 0x16, 0x1C, 0x1C, 0xC3, 0x5C};
HX8357D_WriteReg(0xB1, data1, sizeof(data1));
// B3h: Set RGB interface
// Disable SDO pin
// Write directly to display (not internal GRAM)
// Data read on rising edge of DCLK
// HSYNC & VSYNC Activ low
// DE Active high
// Horizontal back porch 5
// Vertical back porch 2
uint8_t data2[] = {0x53, 0x00, 0x05, 0x02};
HX8357D_WriteReg(0xB3, data2, sizeof(data2));
// SET cyc
uint8_t data3[] = {0x32, 0x40, 0x00, 0x2A, 0x2A, 0x0D, 0x78};
HX8357D_WriteReg(0xB4, data3, sizeof(data3));
// VCOMDC
uint8_t data4[] = {0x3C};
HX8357D_WriteReg(0xB6, data4, sizeof(data4));
// SET STBA
uint8_t data5[] = {0x70, 0x50, 0x01, 0x3C, 0xC8, 0x08};
HX8357D_WriteReg(0xC0, data5, sizeof(data5));
// SET panel
uint8_t data6[] = {0x0B};
HX8357D_WriteReg(0xCC, data6, sizeof(data6));
// VCOMDC
uint8_t data7[] = {0x40};
HX8357D_WriteReg(0xB6, data7, sizeof(data7));
// Gamma
uint8_t data8[] = {0x02, 0x0A, 0x10, 0x1A, 0x22, 0x34, 0x41, 0x4A, 0x4D,
0x44, 0x3A, 0x23, 0x19, 0x08, 0x09, 0x03, 0x02, 0x0A,
0x10, 0x1A, 0x22, 0x34, 0x41, 0x4A, 0x4D, 0x44, 0x3A,
0x23, 0x19, 0x08, 0x09, 0x03, 0x00, 0x01};
HX8357D_WriteReg(0xE0, data8, sizeof(data8));
// Display cycle register
uint8_t data9[] = {0x00};
HX8357D_WriteReg(0xB4, data9, sizeof(data9));
// SET BGP
uint8_t data10[] = {0x03, 0x03, 0x03};
HX8357D_WriteReg(0xB5, data10, sizeof(data10));
// ?
uint8_t data11[] = {0xB0, 0x22, 0x3B};
HX8357D_WriteReg(0xB6, data11, sizeof(data11));
// COLMOD
uint8_t data12[] = {0x66};
HX8357D_WriteReg(0x3A, data12, sizeof(data12));
// Sleep out
HX8357D_WriteReg(0x11, NULL, 0);
// Normal display mode
HX8357D_WriteReg(0x13, NULL, 0);
// Display on
HX8357D_WriteReg(0x29, NULL, 0);
}