summaryrefslogtreecommitdiff
path: root/ucoo/dev/lcd/lcd_spi.cc
blob: f8f05d08aa2f2d6692e569a9b308c8147b4c142b (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2015 Nicolas Schodet
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// }}}
#include "ucoo/dev/lcd/lcd_spi.hh"

#include "ucoo/common.hh"
#include "ucoo/utils/delay.hh"

#include <cstring>

namespace ucoo {

enum
{
    LCD_CLEAR = 0x01,
    LCD_HOME = 0x02,
    LCD_ENTRY_MODE = 0x04,
    LCD_ENTRY_MODE__INCR = 0x02,
    LCD_ENTRY_MODE__SHIFT = 0x01,
    LCD_DISPLAY_ON_OFF = 0x08,
    LCD_DISPLAY_ON_OFF__ON = 0x04,
    LCD_DISPLAY_ON_OFF__CURSOR = 0x02,
    LCD_DISPLAY_ON_OFF__BLINK = 0x01,
    LCD_SHIFT = 0x10,
    LCD_SHIFT__DISPLAY = 0x08,
    LCD_SHIFT__RIGHT = 0x04,
    LCD_FUNCTION_SET = 0x20,
    LCD_FUNCTION_SET__8BIT = 0x10,
    LCD_FUNCTION_SET__2LINES = 0x08,
    LCD_FUNCTION_SET__FONT = 0x04,
    LCD_FUNCTION_SET__INSTRUCTION_TABLE_1 = 0x01,
    LCD_SET_CGRAM_ADDRESS = 0x40,
    LCD_SET_DDRAM_ADDRESS = 0x80,

    LCD_IS1_BIAS_SET = 0x14,
    LCD_IS1_BIAS_SET__1_4_BIAS = 0x08,
    LCD_IS1_BIAS_SET__1_5_BIAS = 0x00,
    LCD_IS1_POWER_CONTROL = 0x50,
    LCD_IS1_POWER_CONTROL__BOOST_ON = 0x04,
    LCD_IS1_POWER_CONTROL__CONTRAST_C5 = 0x02,
    LCD_IS1_POWER_CONTROL__CONTRAST_C4 = 0x01,
    LCD_IS1_FOLLOWER_CONTROL = 0x60,
    LCD_IS1_FOLLOWER_CONTROL__ON = 0x08,
    LCD_IS1_CONTRAST_SET = 0x70,
    LCD_IS1_CONTRAST_SET__CONTRAST_C3 = 0x08,
    LCD_IS1_CONTRAST_SET__CONTRAST_C2 = 0x04,
    LCD_IS1_CONTRAST_SET__CONTRAST_C1 = 0x02,
    LCD_IS1_CONTRAST_SET__CONTRAST_C0 = 0x01,
};

LcdSpi::~LcdSpi ()
{
    disable ();
}

void
LcdSpi::enable ()
{
    enabled_ = true;
    // Enable bus.
    cs_.output ();
    cs_.set ();
    rs_.output ();
    spi_.enable (spi_speed_hz_, SPI_MODE_3);
    // Initialise display.
    char buf[] = {
        LCD_FUNCTION_SET | LCD_FUNCTION_SET__8BIT | LCD_FUNCTION_SET__2LINES
            | LCD_FUNCTION_SET__INSTRUCTION_TABLE_1,
        LCD_IS1_BIAS_SET | LCD_IS1_BIAS_SET__1_5_BIAS,
        LCD_IS1_POWER_CONTROL | LCD_IS1_POWER_CONTROL__BOOST_ON | 0x1,
        LCD_IS1_FOLLOWER_CONTROL | LCD_IS1_FOLLOWER_CONTROL__ON | 5,
        LCD_IS1_CONTRAST_SET | 0x8,
        LCD_FUNCTION_SET | LCD_FUNCTION_SET__8BIT | LCD_FUNCTION_SET__2LINES,
        LCD_DISPLAY_ON_OFF | LCD_DISPLAY_ON_OFF__ON,
        LCD_CLEAR,
        LCD_ENTRY_MODE | LCD_ENTRY_MODE__INCR,
    };
    send (buf, sizeof (buf), true, true);
    cur_line_ = 0;
}

void
LcdSpi::disable ()
{
    if (enabled_)
    {
        enabled_ = false;
        // Turn off display.
        char buf[] = { LCD_DISPLAY_ON_OFF };
        send (buf, sizeof (buf), true);
        // Disable bus.
        spi_.disable ();
        rs_.input ();
        cs_.input ();
    }
}

void
LcdSpi::puts (const char *str)
{
    while (*str)
    {
        const char *end = str + strcspn (str, "\n\r");
        if (end != str)
            send (str, end - str);
        str = end;
        switch (*str)
        {
        case '\n':
            go (cur_line_ + 1, 0);
            str++;
            break;
        case '\r':
            go (cur_line_, 0);
            str++;
            break;
        default:
            break;
        }
    }
}

void
LcdSpi::go (int line, int column)
{
    line = line % lines_;
    int addr = line * 0x40 + column;
    char buf[] = { static_cast<char> (LCD_SET_DDRAM_ADDRESS | addr) };
    send (buf, sizeof (buf), true);
    cur_line_ = line;
}

void
LcdSpi::clear ()
{
    char buf[] = { LCD_CLEAR };
    send (buf, sizeof (buf), true, true);
}

int
LcdSpi::get_lines () const
{
    return lines_;
}

int
LcdSpi::get_columns () const
{
    return columns_;
}

void
LcdSpi::send (const char *buf, int count, bool command, bool long_delay)
{
    assert (enabled_);
    rs_.set (!command);
    cs_.reset ();
    spi_.send (buf, count);
    cs_.set ();
    if (command)
    {
        if (long_delay)
            delay_ms (3);
        else
            delay_us (60);
    }
}

} // namespace ucoo