paul@0 | 1 | /* |
paul@3 | 2 | * Ben NanoNote communication with the Nuelectronics "Color LCD & |
paul@3 | 3 | * Joystick Shield" featuring a Nokia 6100 and the PCF8833 display |
paul@0 | 4 | * controller. |
paul@0 | 5 | * |
paul@0 | 6 | * http://shieldlist.org/nuelectronics/colorlcd-joystick |
paul@0 | 7 | * |
paul@0 | 8 | * Copyright (C) 2013 Paul Boddie |
paul@0 | 9 | * |
paul@3 | 10 | * SPI functions derived from those in lib/atben.c by Werner Almesberger: |
paul@3 | 11 | * |
paul@3 | 12 | * Copyright (C) 2010-2011 Werner Almesberger |
paul@3 | 13 | * |
paul@0 | 14 | * This program is free software; you can redistribute it and/or modify |
paul@0 | 15 | * it under the terms of the GNU General Public License as published by |
paul@0 | 16 | * the Free Software Foundation; either version 2 of the License, or |
paul@0 | 17 | * (at your option) any later version. |
paul@0 | 18 | */ |
paul@0 | 19 | |
paul@0 | 20 | #include "pcf8833.h" |
paul@0 | 21 | #include <stdio.h> |
paul@0 | 22 | #include <unistd.h> |
paul@0 | 23 | |
paul@0 | 24 | void LCD_send(lcd_sendmode mode, uint8_t data) |
paul@0 | 25 | { |
paul@0 | 26 | spi_begin(); |
paul@0 | 27 | spi_send(data, mode); |
paul@0 | 28 | spi_end(); |
paul@0 | 29 | } |
paul@0 | 30 | |
paul@0 | 31 | void LCD_send_more_data(uint8_t data) |
paul@0 | 32 | { |
paul@0 | 33 | spi_begin(); |
paul@0 | 34 | spi_send(data, LCD_DATA); |
paul@0 | 35 | spi_end(); |
paul@0 | 36 | } |
paul@0 | 37 | |
paul@0 | 38 | void spi_init() |
paul@0 | 39 | { |
paul@0 | 40 | OUT(LCD_CS); |
paul@0 | 41 | OUT(LCD_RESET); |
paul@0 | 42 | OUT(LCD_CLK); |
paul@0 | 43 | OUT(LCD_SEND); |
paul@0 | 44 | OUT(LCD_BACKLIGHT); |
paul@2 | 45 | |
paul@2 | 46 | CLR(LCD_RESET); |
paul@2 | 47 | CLR(LCD_BACKLIGHT); |
paul@0 | 48 | } |
paul@0 | 49 | |
paul@0 | 50 | void spi_begin() |
paul@0 | 51 | { |
paul@0 | 52 | CLR(LCD_CS); |
paul@0 | 53 | } |
paul@0 | 54 | |
paul@0 | 55 | void spi_end() |
paul@0 | 56 | { |
paul@0 | 57 | SET(LCD_CS); |
paul@0 | 58 | } |
paul@0 | 59 | |
paul@0 | 60 | /** |
paul@0 | 61 | * Send the given value via MOSI/SEND. |
paul@0 | 62 | */ |
paul@0 | 63 | void spi_send(uint8_t v, lcd_sendmode mode) |
paul@0 | 64 | { |
paul@0 | 65 | uint8_t mask; |
paul@0 | 66 | |
paul@0 | 67 | if (mode == LCD_DATA) |
paul@0 | 68 | { |
paul@0 | 69 | #ifdef DEBUG |
paul@0 | 70 | printf("D"); |
paul@0 | 71 | #endif |
paul@0 | 72 | SET(LCD_SEND); |
paul@0 | 73 | } |
paul@0 | 74 | else |
paul@0 | 75 | { |
paul@0 | 76 | #ifdef DEBUG |
paul@0 | 77 | printf("C"); |
paul@0 | 78 | #endif |
paul@0 | 79 | CLR(LCD_SEND); |
paul@0 | 80 | } |
paul@0 | 81 | |
paul@0 | 82 | SET(LCD_CLK); |
paul@0 | 83 | CLR(LCD_CLK); |
paul@0 | 84 | |
paul@0 | 85 | for (mask = 0x80; mask; mask >>= 1) |
paul@0 | 86 | { |
paul@0 | 87 | if (v & mask) |
paul@0 | 88 | { |
paul@0 | 89 | #ifdef DEBUG |
paul@0 | 90 | printf("1"); |
paul@0 | 91 | #endif |
paul@0 | 92 | SET(LCD_SEND); |
paul@0 | 93 | } |
paul@0 | 94 | else |
paul@0 | 95 | { |
paul@0 | 96 | #ifdef DEBUG |
paul@0 | 97 | printf("0"); |
paul@0 | 98 | #endif |
paul@0 | 99 | CLR(LCD_SEND); |
paul@0 | 100 | } |
paul@0 | 101 | |
paul@0 | 102 | SET(LCD_CLK); |
paul@0 | 103 | CLR(LCD_CLK); |
paul@0 | 104 | } |
paul@0 | 105 | |
paul@0 | 106 | #ifdef DEBUG |
paul@0 | 107 | printf("\n"); |
paul@0 | 108 | #endif |
paul@0 | 109 | } |
paul@0 | 110 | |
paul@2 | 111 | void LCD_init() |
paul@0 | 112 | { |
paul@0 | 113 | /* Perform a hardware reset on the LCD. */ |
paul@0 | 114 | /* CS=1, CLK=0, D/C=0 apparently superfluous */ |
paul@0 | 115 | |
paul@0 | 116 | SET(LCD_RESET); |
paul@0 | 117 | usleep(50000); |
paul@0 | 118 | CLR(LCD_RESET); |
paul@0 | 119 | usleep(50000); |
paul@0 | 120 | SET(LCD_RESET); |
paul@0 | 121 | usleep(50000); |
paul@0 | 122 | |
paul@0 | 123 | /* CS=1, CLK=1, D/C=1, 10ms, SWRESET, 10ms apparently superfluous */ |
paul@0 | 124 | |
paul@0 | 125 | LCD_send(LCD_COMMAND, LCD_SLEEPOUT); |
paul@0 | 126 | |
paul@0 | 127 | /* Configure display memory access. */ |
paul@0 | 128 | |
paul@0 | 129 | LCD_send(LCD_COMMAND, LCD_MADCTL); |
paul@0 | 130 | LCD_send(LCD_DATA, LCD_MADCTL_MY_MX); |
paul@0 | 131 | |
paul@0 | 132 | /* Contrast setting (suggested value 0x40), 10ms apparently superfluous */ |
paul@0 | 133 | |
paul@0 | 134 | /* Switch the display on. */ |
paul@0 | 135 | |
paul@0 | 136 | LCD_send(LCD_COMMAND, LCD_DISPON); |
paul@0 | 137 | |
paul@0 | 138 | /* Configure the display mode/format. */ |
paul@0 | 139 | /* 5 == 16bpp */ |
paul@0 | 140 | |
paul@0 | 141 | LCD_send(LCD_COMMAND, LCD_COLMOD); |
paul@0 | 142 | LCD_send(LCD_DATA, 0x05); |
paul@0 | 143 | |
paul@0 | 144 | LCD_send(LCD_COMMAND, LCD_NOP); |
paul@0 | 145 | |
paul@0 | 146 | /* Switch the backlight on. */ |
paul@0 | 147 | |
paul@0 | 148 | SET(LCD_BACKLIGHT); |
paul@0 | 149 | } |
paul@0 | 150 | |
paul@2 | 151 | void LCD_off() |
paul@2 | 152 | { |
paul@2 | 153 | /* Switch the backlight off. */ |
paul@2 | 154 | |
paul@2 | 155 | CLR(LCD_BACKLIGHT); |
paul@2 | 156 | } |
paul@2 | 157 | |
paul@0 | 158 | void LCD_window(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax) |
paul@0 | 159 | { |
paul@0 | 160 | LCD_send(LCD_COMMAND, LCD_CASET); |
paul@0 | 161 | LCD_send(LCD_DATA, xmin); |
paul@0 | 162 | LCD_send(LCD_DATA, xmax); |
paul@0 | 163 | LCD_send(LCD_COMMAND, LCD_PASET); |
paul@0 | 164 | LCD_send(LCD_DATA, ymin); |
paul@0 | 165 | LCD_send(LCD_DATA, ymax); |
paul@0 | 166 | } |
paul@0 | 167 | |
paul@0 | 168 | void LCD_blit_int(uint16_t colour) |
paul@0 | 169 | { |
paul@0 | 170 | LCD_send_more_data((colour >> 8) & 0xff); /* RGGB -> RG */ |
paul@0 | 171 | LCD_send_more_data(colour & 0xff); /* RGGB -> GB */ |
paul@0 | 172 | } |
paul@0 | 173 | |
paul@0 | 174 | void LCD_area(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax, uint16_t colour) |
paul@0 | 175 | { |
paul@0 | 176 | uint16_t i, limit = ((xmax - xmin + 1) * (ymax - ymin + 1)) + 1; |
paul@0 | 177 | |
paul@0 | 178 | LCD_window(xmin, ymin, xmax, ymax); |
paul@0 | 179 | LCD_send(LCD_COMMAND, LCD_RAMWR); |
paul@0 | 180 | |
paul@0 | 181 | for (i = 0; i < limit; i++) |
paul@0 | 182 | { |
paul@0 | 183 | LCD_blit_int(colour); |
paul@0 | 184 | } |
paul@0 | 185 | |
paul@0 | 186 | LCD_send(LCD_COMMAND, LCD_NOP); |
paul@0 | 187 | } |
paul@0 | 188 | |
paul@1 | 189 | void LCD_image(int x, int y, const uint16_t image[], uint8_t width, uint8_t height) |
paul@0 | 190 | { |
paul@0 | 191 | LCD_image_region(x, y, image, width, height, 0, width, 0, height); |
paul@0 | 192 | } |
paul@0 | 193 | |
paul@1 | 194 | void LCD_image_region(int x, int y, const uint16_t image[], uint8_t width, uint8_t height, uint8_t from_x, uint8_t span_x, uint8_t from_y, uint8_t span_y) |
paul@0 | 195 | { |
paul@0 | 196 | uint8_t xmin, ymin, xcmax, ycmax, xcmin, xc, yc; |
paul@0 | 197 | uint16_t xmax, ymax, colour; |
paul@0 | 198 | |
paul@0 | 199 | /* Find the screen ranges for blitting. */ |
paul@0 | 200 | |
paul@0 | 201 | xmin = (x < 0) ? 0 : x; |
paul@0 | 202 | ymin = (y < 0) ? 0 : y; |
paul@0 | 203 | xmax = (x + span_x > SCREEN_X_MAX) ? SCREEN_X_MAX : x + span_x - 1; |
paul@0 | 204 | ymax = (y + span_y > SCREEN_Y_MAX) ? SCREEN_Y_MAX : y + span_y - 1; |
paul@0 | 205 | |
paul@0 | 206 | if ((xmin > SCREEN_X_MAX) || (ymin > SCREEN_Y_MAX) || (xmax < 0) || (ymax < 0)) |
paul@0 | 207 | { |
paul@0 | 208 | return; |
paul@0 | 209 | } |
paul@0 | 210 | |
paul@0 | 211 | /* Find the image ranges for reading data. */ |
paul@0 | 212 | |
paul@0 | 213 | xcmin = (x < 0) ? -x + from_x : from_x; |
paul@0 | 214 | yc = (y < 0) ? -y + from_y : from_y; |
paul@0 | 215 | xcmax = from_x + ((x + span_x > SCREEN_WIDTH) ? SCREEN_WIDTH - x : span_x); |
paul@0 | 216 | ycmax = from_y + ((y + span_y > SCREEN_HEIGHT) ? SCREEN_HEIGHT - y : span_y); |
paul@0 | 217 | |
paul@0 | 218 | LCD_window(xmin, ymin, xmax, ymax); |
paul@0 | 219 | LCD_send(LCD_COMMAND, LCD_RAMWR); |
paul@0 | 220 | |
paul@0 | 221 | /* Reading from the appropriate image ranges, blit to the appropriate |
paul@0 | 222 | screen ranges. */ |
paul@0 | 223 | |
paul@0 | 224 | xc = xcmin; |
paul@0 | 225 | while (yc < ycmax) |
paul@0 | 226 | { |
paul@0 | 227 | colour = image[yc * width + xc]; |
paul@0 | 228 | LCD_blit_int(colour); |
paul@0 | 229 | |
paul@0 | 230 | xc++; |
paul@0 | 231 | if (xc == xcmax) |
paul@0 | 232 | { |
paul@0 | 233 | xc = xcmin; |
paul@0 | 234 | yc++; |
paul@0 | 235 | } |
paul@0 | 236 | } |
paul@0 | 237 | |
paul@0 | 238 | LCD_send(LCD_COMMAND, LCD_NOP); |
paul@0 | 239 | } |
paul@0 | 240 | |
paul@2 | 241 | void LCD_normal() |
paul@0 | 242 | { |
paul@0 | 243 | LCD_send(LCD_COMMAND, LCD_NORON); |
paul@0 | 244 | } |
paul@0 | 245 | |
paul@0 | 246 | void LCD_scroll_area(uint8_t top_fixed, uint8_t scrolling, uint8_t bottom_fixed) |
paul@0 | 247 | { |
paul@0 | 248 | LCD_send(LCD_COMMAND, LCD_VSCRDEF); |
paul@0 | 249 | LCD_send(LCD_DATA, top_fixed); |
paul@0 | 250 | LCD_send(LCD_DATA, scrolling); |
paul@0 | 251 | LCD_send(LCD_DATA, bottom_fixed); |
paul@0 | 252 | } |
paul@0 | 253 | |
paul@0 | 254 | void LCD_scroll_start(uint8_t address) |
paul@0 | 255 | { |
paul@0 | 256 | LCD_send(LCD_COMMAND, LCD_SEP); |
paul@0 | 257 | LCD_send(LCD_DATA, address); |
paul@0 | 258 | } |
paul@0 | 259 | |
paul@0 | 260 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@0 | 261 | */ |