From a76880e9c331804e359f80e10cffac64faaba068 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Fri, 26 Jul 2019 21:32:58 +0200
Subject: [PATCH] fix(display): Make use of the display reset pin

---
 lib/{gfx => card10}/display.c | 19 +++++++++++++++++--
 lib/{gfx => card10}/display.h |  0
 lib/card10/meson.build        |  3 ++-
 lib/card10/portexpander.c     | 23 +++++++++++++++++++++--
 lib/card10/portexpander.h     |  1 +
 lib/gfx/GUI_DEV/DEV_Config.h  | 10 ++++------
 lib/gfx/LCD/LCD_Driver.c      |  4 ++--
 lib/gfx/meson.build           |  2 --
 8 files changed, 47 insertions(+), 15 deletions(-)
 rename lib/{gfx => card10}/display.c (83%)
 rename lib/{gfx => card10}/display.h (100%)

diff --git a/lib/gfx/display.c b/lib/card10/display.c
similarity index 83%
rename from lib/gfx/display.c
rename to lib/card10/display.c
index 1caa81c3..1e334202 100644
--- a/lib/gfx/display.c
+++ b/lib/card10/display.c
@@ -3,11 +3,12 @@
 
 #include "gpio.h"
 #include "tmr.h"
+#include "portexpander.h"
+#include "MAX77650-Arduino-Library.h"
 
 #include <stdint.h>
 #include <stdio.h>
 /***** Globals *****/
-//const gpio_cfg_t DEV_RST_PIN = {PORT_0, PIN_28, GPIO_FUNC_OUT, GPIO_PAD_NONE};
 const gpio_cfg_t DEV_DC_PIN = {PORT_1, PIN_6, GPIO_FUNC_OUT, GPIO_PAD_NONE};
 
 // Parameters for PWM output
@@ -70,10 +71,24 @@ void PWM_Output(void)
     printf("PWM started.\n");
 }
 
+void display_set_reset_pin(uint8_t state)
+{
+    if(!portexpander_detected()) {
+        MAX77650_setDO(state ? true:false);
+    } else {
+        portexpander_set(4, state);
+    }
+}
 
 void display_init(void)
 {
-    //GPIO_Config(&DEV_RST_PIN);
+    if(!portexpander_detected()) {
+        // Open-drain
+        MAX77650_setDRV(false);
+        // Output
+        MAX77650_setDIR(false);
+    }
+
     GPIO_Config(&DEV_DC_PIN);
 
     PWM_Output();
diff --git a/lib/gfx/display.h b/lib/card10/display.h
similarity index 100%
rename from lib/gfx/display.h
rename to lib/card10/display.h
diff --git a/lib/card10/meson.build b/lib/card10/meson.build
index 5c51f5f1..5cde6614 100644
--- a/lib/card10/meson.build
+++ b/lib/card10/meson.build
@@ -8,7 +8,8 @@ sources = files(
   'leds.c',
   'pmic.c',
   'portexpander.c',
-  'pb.c'
+  'pb.c',
+  'display.c'
 )
 
 deps = [
diff --git a/lib/card10/portexpander.c b/lib/card10/portexpander.c
index 554d618a..67192cc0 100644
--- a/lib/card10/portexpander.c
+++ b/lib/card10/portexpander.c
@@ -8,6 +8,7 @@
 #include <stdbool.h>
 
 static bool detected = false;
+static uint8_t output_state;
 
 void portexpander_init(void)
 {
@@ -29,12 +30,13 @@ void portexpander_init(void)
     command[0] = 0x4F; command[1] = 0x01;
     I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
 
-    // Enable outputs for transistors and the LED
+    // Enable outputs for the transistors, the LED and the LCD reset
     command[0] = 0x03; command[1] = 0x68;
     I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
 
     // Set outputs to high (i.e. open-drain)
-    command[0] = 0x01; command[1] = 0x97;
+    output_state = 0x97;
+    command[0] = 0x01; command[1] = output_state;
     I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
 
     // Turn on LEDs
@@ -62,3 +64,20 @@ bool portexpander_detected(void)
 {
     return detected;
 }
+
+void portexpander_set(uint8_t pin, uint8_t value)
+{
+    uint8_t addr = 0x21;
+    uint8_t command[2];
+
+    if(detected && pin < 8) {
+        if(value) {
+            output_state |= (1 << pin);
+        } else {
+            output_state &= ~(1 << pin);
+        }
+
+        command[0] = 0x01; command[1] = output_state;
+        I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
+    }
+}
diff --git a/lib/card10/portexpander.h b/lib/card10/portexpander.h
index 66937c29..b81c7070 100644
--- a/lib/card10/portexpander.h
+++ b/lib/card10/portexpander.h
@@ -6,6 +6,7 @@
 
 void portexpander_init(void);
 uint8_t portexpander_get(void);
+void portexpander_set(uint8_t pin, uint8_t value);
 bool portexpander_detected(void);
 
 #endif
diff --git a/lib/gfx/GUI_DEV/DEV_Config.h b/lib/gfx/GUI_DEV/DEV_Config.h
index 94eaa7ae..64487005 100644
--- a/lib/gfx/GUI_DEV/DEV_Config.h
+++ b/lib/gfx/GUI_DEV/DEV_Config.h
@@ -46,24 +46,22 @@
 /**
  * GPIO config
 **/
-//extern const gpio_cfg_t DEV_RST_PIN;
 extern const gpio_cfg_t DEV_DC_PIN;
-//extern const gpio_cfg_t DEV_CS_PIN;
-//extern const gpio_cfg_t DEV_BL_PIN;
+
 /**
  * GPIO read and write
 **/
 #define DEV_Digital_Write(_pin, _value) GPIO_OutPut(&_pin, _value == 0? 0 : _pin.mask)
-//#define DEV_Digital_Read(_pin) HAL_GPIO_ReadPin(_pin)
 
 
 /**
  * SPI
 **/
 void lcd_write(uint8_t* data, int size);
-//#define DEV_SPI_WRITE(_dat)  HAL_SPI_Transmit(&hspi1, (uint8_t *)&_dat, 1, 500);
+void display_set_reset_pin(uint8_t state);
 #define DEV_SPI_WRITE(_dat) lcd_write(&_dat, 1)
-
+#define DEV_RESET_LOW() display_set_reset_pin(0)
+#define DEV_RESET_HIGH() display_set_reset_pin(1)
 /**
  * delay x ms
 **/
diff --git a/lib/gfx/LCD/LCD_Driver.c b/lib/gfx/LCD/LCD_Driver.c
index 20f12e58..7005fda5 100644
--- a/lib/gfx/LCD/LCD_Driver.c
+++ b/lib/gfx/LCD/LCD_Driver.c
@@ -38,9 +38,9 @@ static void LCD_Reset(void)
 {
 	//DEV_Digital_Write(DEV_CS_PIN,0);
 	DEV_Delay_ms(20);
-	//DEV_Digital_Write(DEV_RST_PIN,0);
+    DEV_RESET_LOW();
 	DEV_Delay_ms(20);
-	//DEV_Digital_Write(DEV_RST_PIN,1);
+    DEV_RESET_HIGH();
 	DEV_Delay_ms(20);
 }
 
diff --git a/lib/gfx/meson.build b/lib/gfx/meson.build
index a29e5678..41b377b1 100644
--- a/lib/gfx/meson.build
+++ b/lib/gfx/meson.build
@@ -9,8 +9,6 @@ sources = files(
   './GUI_DEV/DEV_Config.c',
   './GUI_DEV/GUI_Paint.c',
   './LCD/LCD_Driver.c',
-  './display.c',
-
   './Fonts/font8.c',
   './Fonts/font12.c',
   './Fonts/font12CN.c',
-- 
GitLab