Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | #include "libCharLCD/lcdInterface.h" |
2 | |||
3 | /// \brief Initialise : call after ChibiOS is running . |
||
4 | void lcdInterface_t::init() |
||
5 | { |
||
6 | } |
||
7 | |||
8 | void lcdInterface_t::setLineMode(ioline_t line, unsigned mode) |
||
9 | { |
||
10 | GPIO_InitTypeDef GPIO_InitStruct; |
||
11 | GPIO_InitStruct.Pin = line.pin; |
||
12 | GPIO_InitStruct.Mode = mode; |
||
13 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
14 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
15 | HAL_GPIO_Init(line.gpio, &GPIO_InitStruct); |
||
16 | } |
||
17 | |||
18 | void lcdInterface_t::polledDelay(unsigned microsec) |
||
19 | { |
||
20 | |||
21 | uint16_t tval = __HAL_TIM_GET_COUNTER(LCD_DELAY_CNT); |
||
22 | |||
23 | // need to determine timer is "after" |
||
24 | |||
25 | while ((__HAL_TIM_GET_COUNTER(LCD_DELAY_CNT) - tval) < microsec) |
||
26 | { |
||
27 | }; |
||
28 | }; |
||
29 | |||
30 | void lcdInterface_t::writeData4(uint8_t data_) |
||
31 | { |
||
32 | // set write bit to low = write |
||
33 | HAL_GPIO_WritePin(m_wrBit.gpio, m_wrBit.pin, GPIO_PIN_RESET); |
||
34 | |||
35 | for (int i = 0; i < 4; i++) |
||
36 | setLineMode(m_dBits[i], GPIO_MODE_OUTPUT_PP); |
||
37 | |||
38 | for (int i = 0; i < 4; i++) |
||
39 | HAL_GPIO_WritePin(m_dBits[i].gpio, m_dBits[i].pin, (data_ & (1 << i)) ? GPIO_PIN_SET : GPIO_PIN_RESET); |
||
40 | |||
41 | polledDelay(2); |
||
42 | // pulse enable line high |
||
43 | HAL_GPIO_WritePin(m_eBit.gpio, m_eBit.pin, GPIO_PIN_SET); |
||
44 | polledDelay(2); |
||
45 | HAL_GPIO_WritePin(m_eBit.gpio, m_eBit.pin, GPIO_PIN_RESET); |
||
46 | } |
||
47 | void lcdInterface_t::writeData8(uint8_t data_) |
||
48 | { |
||
49 | writeData4((data_ & 0xF0) >> 4); |
||
50 | writeData4(data_ & 0x0F); |
||
51 | } |
||
52 | |||
53 | uint8_t lcdInterface_t::readData4() |
||
54 | { |
||
55 | // clear write bit to high = read |
||
56 | HAL_GPIO_WritePin(m_wrBit.gpio, m_wrBit.pin, GPIO_PIN_SET); |
||
57 | for (int i = 0; i < 4; i++) |
||
58 | setLineMode(m_dBits[i], GPIO_MODE_INPUT); |
||
59 | HAL_GPIO_WritePin(m_eBit.gpio, m_eBit.pin, GPIO_PIN_SET); |
||
60 | polledDelay(2); |
||
61 | uint8_t dat = 0; |
||
62 | for (int i = 0; i < 4; i++) |
||
63 | { |
||
64 | if (HAL_GPIO_ReadPin(m_dBits[i].gpio, m_dBits[i].pin) == GPIO_PIN_SET) |
||
65 | dat |= 1 << i; |
||
66 | } |
||
67 | HAL_GPIO_WritePin(m_eBit.gpio, m_eBit.pin, GPIO_PIN_RESET); |
||
68 | return dat; |
||
69 | } |
||
70 | |||
71 | uint8_t lcdInterface_t::readData8() |
||
72 | { |
||
73 | uint8_t dat = readData4() << 4; |
||
74 | dat |= readData4(); |
||
75 | return dat; |
||
76 | } |
||
77 | |||
78 | /// \brief Return the X size of this display |
||
79 | uint8_t lcdInterface_t::displayXsize() const |
||
80 | { |
||
81 | switch (m_lcdType) |
||
82 | { |
||
83 | case LCD_8X1: |
||
84 | case LCD_8X2: |
||
85 | return 8; |
||
86 | case LCD_10X1: |
||
87 | case LCD_10X2: |
||
88 | return 10; |
||
89 | case LCD_16X1: |
||
90 | case LCD_16X2: |
||
91 | return 16; |
||
92 | case LCD_20X1: |
||
93 | case LCD_20X2: |
||
94 | return 20; |
||
95 | case LCD_40X1: |
||
96 | case LCD_40X2: |
||
97 | return 40; |
||
98 | } |
||
99 | return 0; |
||
100 | } |
||
101 | |||
102 | /// \brief Return the Y size (1 or 2) of this display |
||
103 | uint8_t lcdInterface_t::displayYsize() const |
||
104 | { |
||
105 | switch (m_lcdType) |
||
106 | { |
||
107 | case LCD_8X1: |
||
108 | case LCD_10X1: |
||
109 | case LCD_16X1: |
||
110 | case LCD_20X1: |
||
111 | case LCD_40X1: |
||
112 | return 1; |
||
113 | case LCD_8X2: |
||
114 | case LCD_10X2: |
||
115 | case LCD_16X2: |
||
116 | case LCD_20X2: |
||
117 | case LCD_40X2: |
||
118 | return 2; |
||
119 | } |
||
120 | return 0; |
||
121 | } |
||
122 | |||
123 | void lcdInterface_t::writeData(uint8_t data_) |
||
124 | { |
||
125 | HAL_GPIO_WritePin(m_rsBit.gpio, m_rsBit.pin, GPIO_PIN_SET); |
||
126 | writeData8(data_); |
||
127 | pollBusy(); |
||
128 | } |
||
129 | |||
130 | void lcdInterface_t::writeCtrl(uint8_t data_) |
||
131 | { |
||
132 | HAL_GPIO_WritePin(m_rsBit.gpio, m_rsBit.pin, GPIO_PIN_RESET); |
||
133 | writeData8(data_); |
||
134 | } |
||
135 | |||
136 | void lcdInterface_t::writeCtrl4(uint8_t data_) |
||
137 | { |
||
138 | HAL_GPIO_WritePin(m_rsBit.gpio, m_rsBit.pin, GPIO_PIN_RESET); |
||
139 | writeData4(data_ >> 4); |
||
140 | } |
||
141 | |||
142 | void lcdInterface_t::pollBusy() |
||
143 | { |
||
144 | // read from the status bit |
||
145 | HAL_GPIO_WritePin(m_rsBit.gpio, m_rsBit.pin, GPIO_PIN_RESET); |
||
146 | // limit to a certain number of loops |
||
147 | int tries = 1000; |
||
148 | while (--tries) |
||
149 | { |
||
150 | if ((readData8() & 0x80) == 0) |
||
151 | return; |
||
152 | } |
||
153 | m_timeouts++; |
||
154 | // fall out as it failed . |
||
155 | } |
||
156 | |||
157 | void lcdInterface_t::writeCtrlWait(uint8_t data_) |
||
158 | { |
||
159 | writeCtrl(data_); |
||
160 | pollBusy(); |
||
161 | } |
||
162 | |||
163 | uint8_t lcdInterface_t::readData() |
||
164 | { |
||
165 | HAL_GPIO_WritePin(m_rsBit.gpio, m_rsBit.pin, GPIO_PIN_SET); |
||
166 | return readData8(); |
||
167 | } |
||
168 | |||
169 | uint8_t lcdInterface_t::readCtrl() |
||
170 | { |
||
171 | HAL_GPIO_WritePin(m_rsBit.gpio, m_rsBit.pin, GPIO_PIN_RESET); |
||
172 | return readData8(); |
||
173 | } |
||
174 | |||
175 | void lcdInterface_t::pulseE() |
||
176 | { |
||
177 | // pulse enable line high |
||
178 | polledDelay(2); |
||
179 | HAL_GPIO_WritePin(m_eBit.gpio, m_eBit.pin, GPIO_PIN_SET); |
||
180 | polledDelay(2); |
||
181 | HAL_GPIO_WritePin(m_eBit.gpio, m_eBit.pin, GPIO_PIN_RESET); |
||
182 | } |