6.2 Designing the Display Driver State Machine

The state machine orchestrates display initialization, touch processing and backlight control, with error handling and delays.

The following figure shows the key states and their logic as implemented in the code.

Figure 6-1. Key States
  1. DISP_STATE_INIT:
    • Opens I2C clients for ATtiny (0x45) and GT911 (0x5d) using DRV_I2C_Open.
    • Initiates touch ID read (0x8140) and power-on command (REG_POWERON, 0x3).
    • Transitions to DISP_STATE_GET_TOUCH_ID after 100 ms via DISP_DelayNextState.
    • Error: moves to DISP_STATE_ERROR if I2C handles are invalid.
  2. DISP_STATE_GET_TOUCH_ID:
    • Waits for touch ID read completion (0x8140, 4 bytes).
    • If no NACK, transitions to DISP_STATE_VERIFY_TOUCH_ID.
    • If NACK, retries read after 5 ms.
    • Ensures GT911 communication is established.
  3. DISP_STATE_VERIFY_TOUCH_ID:
    • Verifies the GT911 chip ID ('9', '1', '1') against read data.
    • Reads touch firmware (0x8047, 186 bytes) for resolution data.
    • Transitions to DISP_STATE_PROCESS_TOUCH_FW after 5 ms.
    • Error: moves to DISP_STATE_ERROR if ID mismatches.
  4. DISP_STATE_PROCESS_TOUCH_FW:
    • Processes touch firmware data, validating checksum.
    • Extracts touch resolution (touchXRes, touchYRes) from bytes 1–4.
    • Transitions to DISP_STATE_POWER_ON on success.
    • Error: moves to DISP_STATE_ERROR if checksum fails.
  5. DISP_STATE_POWER_ON:
    • Waits for REG_POWERON write completion.
    • If no NACK, delays 100 ms, reads touch status (0x8140) and transitions to DISP_STATE_CONFIG_MIPI_HOST after 100 ms.
    • If NACK, retries REG_POWERON after 60 ms, checks FFC cable and power if persistent.
  6. DISP_STATE_CONFIG_MIPI_HOST:
    • Switches to DSI command mode (DSI_CommandMode).
    • Sends DSI commands via DSI_DCS_WriteBuffer and DSI_DCS_WritePage for display initialization.
    • Switches to video mode (DSI_VideoMode) for rendering.
  7. DISP_STATE_SET_BRIGHTNESS:
    • Applies a 50 ms delay, then writes REG_PWM (0x9f) to set backlight brightness.
    • Transitions to DISP_STATE_IDLE if there is no NACK.
  8. DISP_STATE_PROCESS_TOUCH:
    • Reads touch data (0x814E, 41 bytes for up to 5 points).
    • Processes touch points (1–5), extracting ID, X/Y coordinates and status.
    • Transitions to DISP_STATE_IDLE after 1 ms, or retries read after 10 ms if no touch data.
  9. DISP_STATE_IDLE:
    • Polls for touch updates by reading 0x814E every 10 ms.
    • Transitions to DISP_STATE_PROCESS_TOUCH if not busy.
  10. DISP_STATE_DELAY:
    • Waits for SYS_TIME_DelayIsComplete to complete timed delays.
    • Transitions to stateNext (e.g., CONFIG_MIPI_HOST, SET_BRIGHTNESS).
  11. DISP_STATE_ERROR:
    • Halts on critical errors (e.g., invalid I2C handles, touch ID mismatch, checksum failure).