summaryrefslogtreecommitdiffstats
path: root/bsps/arm/lpc24xx/fb/lcd.c
blob: 875d34ae22e6f9868a7719a85f7b81be51c92e04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSBSPsARMLPC24XX_lcd
 *
 * @brief LCD support.
 */

/*
 * Copyright (c) 2010-2012 embedded brains GmbH.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <assert.h>

#include <bsp/lpc24xx.h>
#include <bsp/lcd.h>
#include <bsp/lpc-lcd.h>
#include <bsp/utility.h>
#include <bsp/system-clocks.h>

#ifdef ARM_MULTILIB_ARCH_V4
  #define LCD_ENABLE BSP_BIT32(0)
#endif

rtems_status_code lpc24xx_lcd_set_mode(
  lpc24xx_lcd_mode mode,
  const lpc24xx_pin_range *pins
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  bool enable = false;

  switch (mode) {
    case LCD_MODE_STN_4_BIT:
    case LCD_MODE_STN_8_BIT:
    case LCD_MODE_STN_DUAL_PANEL_4_BIT:
    case LCD_MODE_STN_DUAL_PANEL_8_BIT:
    case LCD_MODE_TFT_12_BIT_4_4_4:
    case LCD_MODE_TFT_16_BIT_5_6_5:
    case LCD_MODE_TFT_16_BIT_1_5_5_5:
    case LCD_MODE_TFT_24_BIT:
      enable = true;
      break;
    case LCD_MODE_DISABLED:
      enable = false;
      break;
    default:
      sc = RTEMS_IO_ERROR;
      break;
  }

  if (sc == RTEMS_SUCCESSFUL) {
    if (enable) {
      sc = lpc24xx_module_enable(LPC24XX_MODULE_LCD, LPC24XX_MODULE_PCLK_DEFAULT);
      assert(sc == RTEMS_SUCCESSFUL);

      #ifdef ARM_MULTILIB_ARCH_V4
        PINSEL11 = BSP_FLD32(mode, 1, 3) | LCD_ENABLE;
      #endif

      sc = lpc24xx_pin_config(pins, LPC24XX_PIN_SET_FUNCTION);
      assert(sc == RTEMS_SUCCESSFUL);
    } else {
      if (lpc24xx_lcd_current_mode() != LCD_MODE_DISABLED) {
        uint32_t lcd_ctrl = LCD_CTRL;

        /* Disable power */
        lcd_ctrl &= ~BSP_BIT32(11);
        LCD_CTRL = lcd_ctrl;

        lpc24xx_micro_seconds_delay(100000);

        /* Disable all signals */
        lcd_ctrl &= ~BSP_BIT32(0);
        LCD_CTRL = lcd_ctrl;
      }

      sc = lpc24xx_pin_config(pins, LPC24XX_PIN_SET_INPUT);
      assert(sc == RTEMS_SUCCESSFUL);

      #ifdef ARM_MULTILIB_ARCH_V4
        PINSEL11 = 0;
      #endif

      sc = lpc24xx_module_disable(LPC24XX_MODULE_LCD);
      assert(sc == RTEMS_SUCCESSFUL);
    }
  }

  return sc;
}

lpc24xx_lcd_mode lpc24xx_lcd_current_mode(void)
{
  #ifdef ARM_MULTILIB_ARCH_V4
    uint32_t pinsel11 = PINSEL11;

    if ((PCONP & BSP_BIT32(20)) != 0 && (pinsel11 & LCD_ENABLE) != 0) {
      return BSP_FLD32GET(pinsel11, 1, 3);
    } else {
      return LCD_MODE_DISABLED;
    }
  #else
    volatile lpc17xx_scb *scb = &LPC17XX_SCB;

    if ((scb->pconp & LPC17XX_SCB_PCONP_LCD) != 0) {
      return LCD_CTRL & 0xae;
    } else {
      return LCD_MODE_DISABLED;
    }
  #endif
}