diff --git a/EPD/EPD_driver.c b/EPD/EPD_driver.c index 74c653e..d55f1c5 100644 --- a/EPD/EPD_driver.c +++ b/EPD/EPD_driver.c @@ -31,8 +31,7 @@ uint32_t EPD_BS_PIN = 13; uint32_t EPD_EN_PIN = 0xFF; uint32_t EPD_LED_PIN = 0xFF; -#define SPI_INSTANCE 0 /**< SPI instance index. */ -static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */ +// Arduino like function wrappers void pinMode(uint32_t pin, uint32_t mode) { @@ -83,6 +82,7 @@ void delay(uint32_t ms) nrf_delay_ms(ms); } +// GPIO void DEV_Module_Init(void) { pinMode(EPD_CS_PIN, OUTPUT); @@ -98,16 +98,6 @@ void DEV_Module_Init(void) pinMode(EPD_BS_PIN, OUTPUT); digitalWrite(EPD_BS_PIN, LOW); - nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; - spi_config.sck_pin = EPD_SCLK_PIN; - spi_config.mosi_pin = EPD_MOSI_PIN; - spi_config.ss_pin = EPD_CS_PIN; -#if defined(S112) - APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL)); -#else - APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL)); -#endif - digitalWrite(EPD_DC_PIN, LOW); digitalWrite(EPD_CS_PIN, LOW); digitalWrite(EPD_RST_PIN, HIGH); @@ -124,21 +114,114 @@ void DEV_Module_Exit(void) digitalWrite(EPD_CS_PIN, LOW); digitalWrite(EPD_RST_PIN, LOW); - nrf_drv_spi_uninit(&spi); + DEV_SPI_Exit(); EPD_LED_OFF(); } +// Hardware SPI (write only) + +#define SPI_INSTANCE 0 /**< SPI instance index. */ +static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */ +static bool spi_initialized = false; + +void DEV_SPI_Init(void) +{ + if (spi_initialized) return; + nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; + spi_config.sck_pin = EPD_SCLK_PIN; + spi_config.mosi_pin = EPD_MOSI_PIN; + spi_config.ss_pin = EPD_CS_PIN; +#if defined(S112) + APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL)); +#else + APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL)); +#endif + spi_initialized = true; +} + +void DEV_SPI_Exit(void) +{ + if (!spi_initialized) return; + nrf_drv_spi_uninit(&spi); + spi_initialized = false; +} + void DEV_SPI_WriteByte(uint8_t value) { + DEV_SPI_Init(); APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &value, 1, NULL, 0)); } void DEV_SPI_WriteBytes(uint8_t *value, uint8_t len) { + DEV_SPI_Init(); APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, value, len, NULL, 0)); } + +// Software SPI (read / write) +void DEV_SPI_WriteByte_SW(uint8_t data) +{ + DEV_SPI_Exit(); + pinMode(EPD_MOSI_PIN, OUTPUT); + digitalWrite(EPD_CS_PIN, LOW); + for (int i = 0; i < 8; i++) + { + if ((data & 0x80) == 0) digitalWrite(EPD_MOSI_PIN, LOW); + else digitalWrite(EPD_MOSI_PIN, HIGH); + + data <<= 1; + digitalWrite(EPD_SCLK_PIN, HIGH); + digitalWrite(EPD_SCLK_PIN, LOW); + } + digitalWrite(EPD_CS_PIN, HIGH); +} + +uint8_t DEV_SPI_ReadByte_SW(void) +{ + DEV_SPI_Exit(); + uint8_t j = 0xff; + pinMode(EPD_MOSI_PIN, INPUT); + digitalWrite(EPD_CS_PIN, LOW); + for (int i = 0; i < 8; i++) + { + j = j << 1; + if (digitalRead(EPD_MOSI_PIN)) j = j | 0x01; + else j = j & 0xfe; + + digitalWrite(EPD_SCLK_PIN, HIGH); + digitalWrite(EPD_SCLK_PIN, LOW); + } + digitalWrite(EPD_CS_PIN, HIGH); + pinMode(EPD_MOSI_PIN, 1); + return j; +} + +void EPD_WriteCommand_SW(uint8_t Reg) +{ + digitalWrite(EPD_DC_PIN, LOW); + digitalWrite(EPD_CS_PIN, LOW); + DEV_SPI_WriteByte_SW(Reg); + digitalWrite(EPD_CS_PIN, HIGH); +} + +void EPD_WriteByte_SW(uint8_t Data) +{ + digitalWrite(EPD_DC_PIN, HIGH); + digitalWrite(EPD_CS_PIN, LOW); + DEV_SPI_WriteByte_SW(Data); + digitalWrite(EPD_CS_PIN, HIGH); +} + +uint8_t EPD_ReadByte_SW(void) +{ + digitalWrite(EPD_DC_PIN, HIGH); + return DEV_SPI_ReadByte_SW(); +} + + +// Hardware SPI void EPD_WriteCommand(uint8_t Reg) { digitalWrite(EPD_DC_PIN, LOW); @@ -170,19 +253,21 @@ void EPD_Reset(uint32_t value, uint16_t duration) void EPD_WaitBusy(uint32_t value, uint16_t timeout) { - NRF_LOG_DEBUG("[EPD]: check busy"); + NRF_LOG_DEBUG("[EPD]: check busy\n"); while (digitalRead(EPD_BUSY_PIN) == value) { if (timeout % 100 == 0) EPD_LED_TOGGLE(); delay(1); timeout--; if (timeout == 0) { - NRF_LOG_DEBUG("[EPD]: busy timeout!"); + NRF_LOG_DEBUG("[EPD]: busy timeout!\n"); break; } } - NRF_LOG_DEBUG("[EPD]: busy release"); + NRF_LOG_DEBUG("[EPD]: busy release\n"); } + +// lED void EPD_LED_ON(void) { if (EPD_LED_PIN != 0xFF) @@ -201,6 +286,7 @@ void EPD_LED_TOGGLE(void) nrf_gpio_pin_toggle(EPD_LED_PIN); } + extern epd_driver_t epd_driver_4in2; extern epd_driver_t epd_driver_4in2bv2; diff --git a/EPD/EPD_driver.h b/EPD/EPD_driver.h index efda620..fe77a0e 100644 --- a/EPD/EPD_driver.h +++ b/EPD/EPD_driver.h @@ -37,12 +37,14 @@ typedef struct uint16_t height; void (*init)(void); /**< Initialize the e-Paper register */ void (*clear)(void); /**< Clear screen */ - void (*send_command)(uint8_t Reg); /**< send command */ - void (*send_byte)(uint8_t Reg); /**< send byte */ - void (*send_data)(uint8_t *Data, uint8_t Len); /**< send data */ + void (*send_command)(uint8_t Reg); /**< send command */ + void (*send_byte)(uint8_t Reg); /**< send byte */ + void (*send_data)(uint8_t *Data, uint8_t Len); /**< send data */ void (*write_image)(uint8_t *black, uint8_t *color, uint16_t x, uint16_t y, uint16_t w, uint16_t h); /**< write image */ void (*refresh)(void); /**< Sends the image buffer in RAM to e-Paper and displays */ void (*sleep)(void); /**< Enter sleep mode */ + int8_t (*read_temp)(void); /**< Read temperature from driver chip */ + void (*force_temp)(int8_t value); /**< Force temperature (will trigger OTP LUT switch) */ } epd_driver_t; extern uint32_t EPD_MOSI_PIN; @@ -69,18 +71,30 @@ void digitalWrite(uint32_t pin, uint32_t value); uint32_t digitalRead(uint32_t pin); void delay(uint32_t ms); +// GPIO void DEV_Module_Init(void); void DEV_Module_Exit(void); +// Software SPI (read / write) +void DEV_SPI_WriteByte_SW(uint8_t data); +uint8_t DEV_SPI_ReadByte_SW(void); +void EPD_WriteCommand_SW(uint8_t Reg); +void EPD_WriteByte_SW(uint8_t Data); +uint8_t EPD_ReadByte_SW(void); + +// Hardware SPI (write only) +void DEV_SPI_Init(void); +void DEV_SPI_Exit(void); void DEV_SPI_WriteByte(uint8_t value); void DEV_SPI_WriteBytes(uint8_t *value, uint8_t len); - void EPD_WriteCommand(uint8_t Reg); void EPD_WriteByte(uint8_t Data); void EPD_WriteData(uint8_t *Data, uint8_t Len); + void EPD_Reset(uint32_t value, uint16_t duration); void EPD_WaitBusy(uint32_t value, uint16_t timeout); +// lED void EPD_LED_ON(void); void EPD_LED_OFF(void); void EPD_LED_TOGGLE(void); diff --git a/EPD/EPD_service.c b/EPD/EPD_service.c index a1506fc..ee14640 100644 --- a/EPD/EPD_service.c +++ b/EPD/EPD_service.c @@ -19,7 +19,8 @@ #include "nrf_log.h" #if defined(S112) -#define EPD_CFG_DEFAULT {0x14, 0x13, 0x06, 0x05, 0x04, 0x03, 0x02, 0x03, 0xFF, 0x12, 0x07} +#define EPD_CFG_DEFAULT {0x14, 0x13, 0x06, 0x05, 0x04, 0x03, 0x02, 0x03, 0xFF, 0x12, 0x07} // 52811 +//#define EPD_CFG_DEFAULT {0x14, 0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x03, 0xFF, 0x0D, 0x02} // 52810 #else #define EPD_CFG_DEFAULT {0x05, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x07} #endif diff --git a/EPD/UC8176.c b/EPD/UC8176.c index bc10ec9..91c69e4 100644 --- a/EPD/UC8176.c +++ b/EPD/UC8176.c @@ -37,13 +37,29 @@ static void EPD_4IN2_PowerOn(void) { EPD_WriteCommand(0x04); - delay(50); + EPD_WaitBusy(LOW, 100); } static void EPD_4IN2_PowerOff(void) { EPD_WriteCommand(0x02); - EPD_WaitBusy(LOW, 50); + EPD_WaitBusy(LOW, 100); +} + +// Read temperature from driver chip +int8_t EPD_4IN2_Read_Temp(void) +{ + EPD_WriteCommand_SW(0x40); // TSC + return (int8_t) EPD_ReadByte_SW(); +} + +// Force temperature (will trigger OTP LUT switch) +void EPD_4IN2_Force_Temp(int8_t value) +{ + EPD_WriteCommand_SW(0xE0); // CCSET + EPD_WriteByte_SW(0x02); + EPD_WriteCommand_SW(0xE5); // TSSET + EPD_WriteByte_SW(value); } /****************************************************************************** @@ -52,11 +68,14 @@ parameter: ******************************************************************************/ void EPD_4IN2_Refresh(void) { + NRF_LOG_DEBUG("[EPD]: refresh begin\n"); EPD_4IN2_PowerOn(); + NRF_LOG_DEBUG("[EPD]: temperature: %d\n", EPD_4IN2_Read_Temp()); EPD_WriteCommand(0x12); delay(100); EPD_WaitBusy(LOW, 20000); EPD_4IN2_PowerOff(); + NRF_LOG_DEBUG("[EPD]: refresh end\n"); } /****************************************************************************** @@ -76,10 +95,10 @@ void EPD_4IN2_Init(void) void EPD_4IN2B_V2_Init(void) { - EPD_Reset(HIGH, 10); + EPD_Reset(HIGH, 200); EPD_WriteCommand(0x00); - EPD_WriteByte(0x0f); + EPD_WriteByte(0x0f); // 400x300 B/W/R mode, LUT from OTP } /****************************************************************************** @@ -191,6 +210,8 @@ const epd_driver_t epd_driver_4in2 = { .write_image = EPD_4IN2_Write_Image, .refresh = EPD_4IN2_Refresh, .sleep = EPD_4IN2_Sleep, + .read_temp = EPD_4IN2_Read_Temp, + .force_temp = EPD_4IN2_Force_Temp, }; const epd_driver_t epd_driver_4in2bv2 = { @@ -205,4 +226,6 @@ const epd_driver_t epd_driver_4in2bv2 = { .write_image = EPD_4IN2B_V2_Write_Image, .refresh = EPD_4IN2_Refresh, .sleep = EPD_4IN2_Sleep, + .read_temp = EPD_4IN2_Read_Temp, + .force_temp = EPD_4IN2_Force_Temp, }; diff --git a/GUI/Calendar.c b/GUI/Calendar.c index d2c745e..546b36c 100644 --- a/GUI/Calendar.c +++ b/GUI/Calendar.c @@ -8,7 +8,7 @@ #if defined(S112) #define PAGE_HEIGHT 150 #else -#define PAGE_HEIGHT 72 +#define PAGE_HEIGHT 64 #endif static void DrawDateHeader(Adafruit_GFX *gfx, int16_t x, int16_t y, tm_t *tm, struct Lunar_Date *Lunar) { diff --git a/SDK/12.3.0_d7731ad/components/toolchain/arm/arm_startup_nrf51.s b/SDK/12.3.0_d7731ad/components/toolchain/arm/arm_startup_nrf51.s index 4ca1bce..4f76255 100644 --- a/SDK/12.3.0_d7731ad/components/toolchain/arm/arm_startup_nrf51.s +++ b/SDK/12.3.0_d7731ad/components/toolchain/arm/arm_startup_nrf51.s @@ -46,7 +46,7 @@ Heap_Size EQU __STARTUP_CONFIG_HEAP_SIZE ELIF :DEF: __HEAP_SIZE Heap_Size EQU __HEAP_SIZE ELSE -Heap_Size EQU 4096 +Heap_Size EQU 3600 ENDIF AREA HEAP, NOINIT, READWRITE, ALIGN=3