lcd.c 12.5 KB
Newer Older
1
#include <stdio.h>
Damien George's avatar
Damien George committed
2
3
4
#include <string.h>
#include <stm32f4xx_hal.h>

5
#include "mpconfig.h"
Damien George's avatar
Damien George committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "nlr.h"
#include "misc.h"

#if MICROPY_HW_HAS_LCD

#include "qstr.h"
#include "parse.h"
#include "obj.h"
#include "runtime.h"

#include "systick.h"
#include "font_petme128_8x8.h"
#include "lcd.h"

20
#if defined(PYBV3)
Damien George's avatar
Damien George committed
21
22
23
24
25
26
#define PYB_LCD_PORT        (GPIOA)
#define PYB_LCD_CS1_PIN     (GPIO_PIN_0)
#define PYB_LCD_RST_PIN     (GPIO_PIN_1)
#define PYB_LCD_A0_PIN      (GPIO_PIN_2)
#define PYB_LCD_SCL_PIN     (GPIO_PIN_3)
#define PYB_LCD_SI_PIN      (GPIO_PIN_4)
27
#elif defined(PYBV4) || defined(PYBV10)
Damien George's avatar
Damien George committed
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
// X position
#define PYB_LCD_PORT       (GPIOA)
#define PYB_LCD_CS1_PIN    (GPIO_PIN_2) // X3
#define PYB_LCD_RST_PIN    (GPIO_PIN_3) // X4
#define PYB_LCD_A0_PIN     (GPIO_PIN_4) // X5
#define PYB_LCD_SCL_PIN    (GPIO_PIN_5) // X6
#define PYB_LCD_SI_PIN     (GPIO_PIN_7) // X8
#define PYB_LCD_BL_PORT    (GPIOC)
#define PYB_LCD_BL_PIN     (GPIO_PIN_5) // X12
/*
// Y position
#define PYB_LCD_PORT       (GPIOB)
#define PYB_LCD_CS1_PIN    (GPIO_PIN_8) // Y3 = PB8
#define PYB_LCD_RST_PIN    (GPIO_PIN_9) // Y4 = PB9
#define PYB_LCD_A0_PIN     (GPIO_PIN_12) // Y5 = PB12
#define PYB_LCD_SCL_PIN    (GPIO_PIN_13) // Y6 = PB13
#define PYB_LCD_SI_PIN     (GPIO_PIN_15) // Y8 = PB15
#define PYB_LCD_BL_PORT    (GPIOB)
#define PYB_LCD_BL_PIN     (GPIO_PIN_1) // Y12 = PB1
*/
#elif defined(STM32F4DISC)
/* Configure if needed */
#define PYB_LCD_PORT       (GPIOA)
#define PYB_LCD_CS1_PIN    (GPIO_PIN_2) // X3
#define PYB_LCD_RST_PIN    (GPIO_PIN_3) // X4
#define PYB_LCD_A0_PIN     (GPIO_PIN_4) // X5
#define PYB_LCD_SCL_PIN    (GPIO_PIN_5) // X6
#define PYB_LCD_SI_PIN     (GPIO_PIN_7) // X8
#define PYB_LCD_BL_PORT    (GPIOC)
#define PYB_LCD_BL_PIN     (GPIO_PIN_5) // X12
#endif

#define LCD_INSTR (0)
#define LCD_DATA (1)

63
64
65
66
static void lcd_delay(void) {
    __asm volatile ("nop\nnop");
}

Damien George's avatar
Damien George committed
67
static void lcd_out(int instr_data, uint8_t i) {
68
    lcd_delay();
Damien George's avatar
Damien George committed
69
70
71
72
73
74
75
76
    PYB_LCD_PORT->BSRRH = PYB_LCD_CS1_PIN; // CS=0; enable
    if (instr_data == LCD_INSTR) {
        PYB_LCD_PORT->BSRRH = PYB_LCD_A0_PIN; // A0=0; select instr reg
    } else {
        PYB_LCD_PORT->BSRRL = PYB_LCD_A0_PIN; // A0=1; select data reg
    }
    // send byte bigendian, latches on rising clock
    for (uint32_t n = 0; n < 8; n++) {
77
        lcd_delay();
Damien George's avatar
Damien George committed
78
79
80
81
82
83
84
        PYB_LCD_PORT->BSRRH = PYB_LCD_SCL_PIN; // SCL=0
        if ((i & 0x80) == 0) {
            PYB_LCD_PORT->BSRRH = PYB_LCD_SI_PIN; // SI=0
        } else {
            PYB_LCD_PORT->BSRRL = PYB_LCD_SI_PIN; // SI=1
        }
        i <<= 1;
85
        lcd_delay();
Damien George's avatar
Damien George committed
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
        PYB_LCD_PORT->BSRRL = PYB_LCD_SCL_PIN; // SCL=1
    }
    PYB_LCD_PORT->BSRRL = PYB_LCD_CS1_PIN; // CS=1; disable

    /*
    in Python, native types:
    CS1_PIN(const) = 0
    n = int(0)
    delay_ms(0)
    PORT[word:BSRRH] = 1 << CS1_PIN
    for n in range(0, 8):
        delay_ms(0)
        PORT[word:BSRRH] = 1 << SCL_PIN
        if i & 0x80 == 0:
            PORT[word:BSRRH] = 1 << SI_PIN
        else:
            PORT[word:BSRRL] = 1 << SI_PIN
        i <<= 1
        delay_ms(0)
        PORT[word:BSRRL] = 1 << SCL_PIN
    */
}

/*
static void lcd_data_out(uint8_t i) {
    delay_ms(0);
    PYB_LCD_PORT->BSRRH = PYB_LCD_CS1_PIN; // CS=0; enable
    PYB_LCD_PORT->BSRRL = PYB_LCD_A0_PIN; // A0=1; select data reg
    // send byte bigendian, latches on rising clock
    for (uint32_t n = 0; n < 8; n++) {
        delay_ms(0);
        PYB_LCD_PORT->BSRRH = PYB_LCD_SCL_PIN; // SCL=0
        if ((i & 0x80) == 0) {
            PYB_LCD_PORT->BSRRH = PYB_LCD_SI_PIN; // SI=0
        } else {
            PYB_LCD_PORT->BSRRL = PYB_LCD_SI_PIN; // SI=1
        }
        i <<= 1;
        delay_ms(0);
        PYB_LCD_PORT->BSRRL = PYB_LCD_SCL_PIN; // SCL=1
    }
    PYB_LCD_PORT->BSRRL = PYB_LCD_CS1_PIN; // CS=1; disable
}
*/

// writes 8 vertical pixels
// pos 0 is upper left, pos 1 is 8 pixels to right of that, pos 128 is 8 pixels below that
mp_obj_t lcd_draw_pixel_8(mp_obj_t mp_pos, mp_obj_t mp_val) {
    int pos = mp_obj_get_int(mp_pos);
    int val = mp_obj_get_int(mp_val);
    int page = pos / 128;
    int offset = pos - (page * 128);
    lcd_out(LCD_INSTR, 0xb0 | page); // page address set
    lcd_out(LCD_INSTR, 0x10 | ((offset >> 4) & 0x0f)); // column address set upper
    lcd_out(LCD_INSTR, 0x00 | (offset & 0x0f)); // column address set lower
    lcd_out(LCD_DATA, val); // write data
    return mp_const_none;
}

#define LCD_BUF_W (16)
#define LCD_BUF_H (4)

char lcd_char_buffer[LCD_BUF_W * LCD_BUF_H];
int lcd_line;
int lcd_column;
int lcd_next_line;

#define LCD_PIX_BUF_SIZE (128 * 32 / 8)
byte lcd_pix_buf[LCD_PIX_BUF_SIZE];
byte lcd_pix_buf2[LCD_PIX_BUF_SIZE];

mp_obj_t lcd_pix_clear(void) {
    memset(lcd_pix_buf, 0, LCD_PIX_BUF_SIZE);
    memset(lcd_pix_buf2, 0, LCD_PIX_BUF_SIZE);
    return mp_const_none;
}

mp_obj_t lcd_pix_get(mp_obj_t mp_x, mp_obj_t mp_y) {
    int x = mp_obj_get_int(mp_x);
    int y = mp_obj_get_int(mp_y);
    if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
        uint byte_pos = x + 128 * ((uint)y >> 3);
        if (lcd_pix_buf[byte_pos] & (1 << (y & 7))) {
            return mp_obj_new_int(1);
        }
    }
    return mp_obj_new_int(0);
}

mp_obj_t lcd_pix_set(mp_obj_t mp_x, mp_obj_t mp_y) {
    int x = mp_obj_get_int(mp_x);
    int y = mp_obj_get_int(mp_y);
    if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
        uint byte_pos = x + 128 * ((uint)y >> 3);
        lcd_pix_buf2[byte_pos] |= 1 << (y & 7);
    }
    return mp_const_none;
}

mp_obj_t lcd_pix_reset(mp_obj_t mp_x, mp_obj_t mp_y) {
    int x = mp_obj_get_int(mp_x);
    int y = mp_obj_get_int(mp_y);
    if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
        uint byte_pos = x + 128 * ((uint)y >> 3);
        lcd_pix_buf2[byte_pos] &= ~(1 << (y & 7));
    }
    return mp_const_none;
}

mp_obj_t lcd_pix_show(void) {
    memcpy(lcd_pix_buf, lcd_pix_buf2, LCD_PIX_BUF_SIZE);
    for (uint page = 0; page < 4; page++) {
        lcd_out(LCD_INSTR, 0xb0 | page); // page address set
        lcd_out(LCD_INSTR, 0x10); // column address set upper; 0
        lcd_out(LCD_INSTR, 0x00); // column address set lower; 0
        for (uint i = 0; i < 128; i++) {
            lcd_out(LCD_DATA, lcd_pix_buf[i + 128 * page]);
        }
    }
    return mp_const_none;
}

mp_obj_t lcd_print(mp_obj_t text) {
    uint len;
    const char *data = mp_obj_str_get_data(text, &len);
    lcd_print_strn(data, len);
    return mp_const_none;
}

mp_obj_t lcd_light(mp_obj_t value) {
#if defined(PYB_LCD_BL_PORT)
Damien George's avatar
Damien George committed
217
    if (mp_obj_is_true(value)) {
Damien George's avatar
Damien George committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
        PYB_LCD_BL_PORT->BSRRL = PYB_LCD_BL_PIN; // set pin high to turn backlight on
    } else {
        PYB_LCD_BL_PORT->BSRRH = PYB_LCD_BL_PIN; // set pin low to turn backlight off
    }
#endif
    return mp_const_none;
}

static mp_obj_t mp_lcd = MP_OBJ_NULL;

static mp_obj_t pyb_lcd_init(void) {
    if (mp_lcd != MP_OBJ_NULL) {
        // already init'd
        return mp_lcd;
    }

    // set the outputs high
    PYB_LCD_PORT->BSRRL = PYB_LCD_CS1_PIN;
    PYB_LCD_PORT->BSRRL = PYB_LCD_RST_PIN;
    PYB_LCD_PORT->BSRRL = PYB_LCD_A0_PIN;
    PYB_LCD_PORT->BSRRL = PYB_LCD_SCL_PIN;
    PYB_LCD_PORT->BSRRL = PYB_LCD_SI_PIN;

    // make them push/pull outputs
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Pin = PYB_LCD_CS1_PIN | PYB_LCD_RST_PIN | PYB_LCD_A0_PIN | PYB_LCD_SCL_PIN | PYB_LCD_SI_PIN;
    HAL_GPIO_Init(PYB_LCD_PORT, &GPIO_InitStructure);

#if defined(PYB_LCD_BL_PORT)
    // backlight drive pin, starts low (off)
    PYB_LCD_BL_PORT->BSRRH = PYB_LCD_BL_PIN;
    GPIO_InitStructure.Pin = PYB_LCD_BL_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(PYB_LCD_BL_PORT, &GPIO_InitStructure);
#endif

    // init the LCD
    HAL_Delay(1); // wait a bit
    PYB_LCD_PORT->BSRRH = PYB_LCD_RST_PIN; // RST=0; reset
    HAL_Delay(1); // wait for reset; 2us min
    PYB_LCD_PORT->BSRRL = PYB_LCD_RST_PIN; // RST=1; enable
    HAL_Delay(1); // wait for reset; 2us min
    lcd_out(LCD_INSTR, 0xa0); // ADC select, normal
    lcd_out(LCD_INSTR, 0xc8); // common output mode select, reverse
    lcd_out(LCD_INSTR, 0xa2); // LCD bias set, 1/9 bias
    lcd_out(LCD_INSTR, 0x2f); // power control set, 0b111=(booster on, vreg on, vfollow on)
    lcd_out(LCD_INSTR, 0x21); // v0 voltage regulator internal resistor ratio set, 0b001=small
    lcd_out(LCD_INSTR, 0x81); // electronic volume mode set
    lcd_out(LCD_INSTR, 0x34); // electronic volume register set, 0b110100
    lcd_out(LCD_INSTR, 0x40); // display start line set, 0
    lcd_out(LCD_INSTR, 0xaf); // LCD display, on

    // clear display
    for (int page = 0; page < 4; page++) {
        lcd_out(LCD_INSTR, 0xb0 | page); // page address set
        lcd_out(LCD_INSTR, 0x10); // column address set upper
        lcd_out(LCD_INSTR, 0x00); // column address set lower
        for (int i = 0; i < 128; i++) {
            lcd_out(LCD_DATA, 0x00);
        }
    }

    for (int i = 0; i < LCD_BUF_H * LCD_BUF_W; i++) {
        lcd_char_buffer[i] = ' ';
    }
    lcd_line = 0;
    lcd_column = 0;
    lcd_next_line = 0;

    // Micro Python interface
    mp_obj_t o = mp_obj_new_type(MP_QSTR_LCD, mp_const_empty_tuple, mp_obj_new_dict(0));
Damien George's avatar
Damien George committed
294
295
296
297
298
299
300
301
    mp_store_attr(o, qstr_from_str("lcd8"), mp_make_function_n(2, lcd_draw_pixel_8));
    mp_store_attr(o, qstr_from_str("clear"), mp_make_function_n(0, lcd_pix_clear));
    mp_store_attr(o, qstr_from_str("get"), mp_make_function_n(2, lcd_pix_get));
    mp_store_attr(o, qstr_from_str("set"), mp_make_function_n(2, lcd_pix_set));
    mp_store_attr(o, qstr_from_str("reset"), mp_make_function_n(2, lcd_pix_reset));
    mp_store_attr(o, qstr_from_str("show"), mp_make_function_n(0, lcd_pix_show));
    mp_store_attr(o, qstr_from_str("text"), mp_make_function_n(1, lcd_print));
    mp_store_attr(o, qstr_from_str("light"), mp_make_function_n(1, lcd_light));
Damien George's avatar
Damien George committed
302
303
304
305
306
307
308
309
    mp_lcd = o;
    return o;
}

static MP_DEFINE_CONST_FUN_OBJ_0(pyb_lcd_init_obj, pyb_lcd_init);

void lcd_init(void) {
    mp_lcd = MP_OBJ_NULL;
Damien George's avatar
Damien George committed
310
    mp_store_name(qstr_from_str("LCD"), (mp_obj_t)&pyb_lcd_init_obj);
Damien George's avatar
Damien George committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
}

void lcd_print_str(const char *str) {
    lcd_print_strn(str, strlen(str));
}

void lcd_print_strn(const char *str, unsigned int len) {
    int redraw_min = lcd_line * LCD_BUF_W + lcd_column;
    int redraw_max = redraw_min;
    int did_new_line = 0;
    for (; len > 0; len--, str++) {
        // move to next line if needed
        if (lcd_next_line) {
            if (lcd_line + 1 < LCD_BUF_H) {
                lcd_line += 1;
            } else {
                lcd_line = LCD_BUF_H - 1;
                for (int i = 0; i < LCD_BUF_W * (LCD_BUF_H - 1); i++) {
                    lcd_char_buffer[i] = lcd_char_buffer[i + LCD_BUF_W];
                }
                for (int i = 0; i < LCD_BUF_W; i++) {
                    lcd_char_buffer[LCD_BUF_W * (LCD_BUF_H - 1) + i] = ' ';
                }
                redraw_min = 0;
                redraw_max = LCD_BUF_W * LCD_BUF_H;
            }
            lcd_next_line = 0;
            lcd_column = 0;
            did_new_line = 1;
        }
        if (*str == '\n') {
            lcd_next_line = 1;
        } else if (*str == '\r') {
            lcd_column = 0;
        } else if (*str == '\b') {
            if (lcd_column > 0) {
                lcd_column--;
            }
        } else if (lcd_column >= LCD_BUF_W) {
            lcd_next_line = 1;
            str -= 1;
            len += 1;
        } else {
            lcd_char_buffer[lcd_line * LCD_BUF_W + lcd_column] = *str;
            lcd_column += 1;
            int max = lcd_line * LCD_BUF_W + lcd_column;
            if (max > redraw_max) {
                redraw_max = max;
            }
        }
    }

    int last_page = -1;
    for (int i = redraw_min; i < redraw_max; i++) {
        int page = i / LCD_BUF_W;
        if (page != last_page) {
            int offset = 8 * (i - (page * LCD_BUF_W));
            lcd_out(LCD_INSTR, 0xb0 | page); // page address set
            lcd_out(LCD_INSTR, 0x10 | ((offset >> 4) & 0x0f)); // column address set upper
            lcd_out(LCD_INSTR, 0x00 | (offset & 0x0f)); // column address set lower
            last_page = page;
        }
        int chr = lcd_char_buffer[i];
        if (chr < 32 || chr > 126) {
            chr = 127;
        }
        const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
        for (int j = 0; j < 8; j++) {
            lcd_out(LCD_DATA, chr_data[j]);
        }
    }

    if (did_new_line) {
        HAL_Delay(50);
    }
}

#endif // MICROPY_HW_HAS_LCD