1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pcf8833.c Fri Nov 22 22:13:07 2013 +0000
1.3 @@ -0,0 +1,247 @@
1.4 +/*
1.5 + * Ben NanoNote communication with the Nu Electronics "Color LCD &
1.6 + * Joystick Shield" featuring a Nokia 6110 and the PCF8833 display
1.7 + * controller.
1.8 + *
1.9 + * http://shieldlist.org/nuelectronics/colorlcd-joystick
1.10 + *
1.11 + * Copyright (C) 2013 Paul Boddie
1.12 + *
1.13 + * This program is free software; you can redistribute it and/or modify
1.14 + * it under the terms of the GNU General Public License as published by
1.15 + * the Free Software Foundation; either version 2 of the License, or
1.16 + * (at your option) any later version.
1.17 + */
1.18 +
1.19 +#include "pcf8833.h"
1.20 +#include <stdio.h>
1.21 +#include <unistd.h>
1.22 +
1.23 +void LCD_send(lcd_sendmode mode, uint8_t data)
1.24 +{
1.25 + spi_begin();
1.26 + spi_send(data, mode);
1.27 + spi_end();
1.28 +}
1.29 +
1.30 +void LCD_send_more_data(uint8_t data)
1.31 +{
1.32 + spi_begin();
1.33 + spi_send(data, LCD_DATA);
1.34 + spi_end();
1.35 +}
1.36 +
1.37 +void spi_init()
1.38 +{
1.39 + OUT(LCD_CS);
1.40 + OUT(LCD_RESET);
1.41 + OUT(LCD_CLK);
1.42 + OUT(LCD_SEND);
1.43 + OUT(LCD_BACKLIGHT);
1.44 +}
1.45 +
1.46 +void spi_begin()
1.47 +{
1.48 + CLR(LCD_CS);
1.49 +}
1.50 +
1.51 +void spi_end()
1.52 +{
1.53 + SET(LCD_CS);
1.54 +}
1.55 +
1.56 +/**
1.57 + * Send the given value via MOSI/SEND.
1.58 + */
1.59 +void spi_send(uint8_t v, lcd_sendmode mode)
1.60 +{
1.61 + uint8_t mask;
1.62 +
1.63 + if (mode == LCD_DATA)
1.64 + {
1.65 + #ifdef DEBUG
1.66 + printf("D");
1.67 + #endif
1.68 + SET(LCD_SEND);
1.69 + }
1.70 + else
1.71 + {
1.72 + #ifdef DEBUG
1.73 + printf("C");
1.74 + #endif
1.75 + CLR(LCD_SEND);
1.76 + }
1.77 +
1.78 + SET(LCD_CLK);
1.79 + CLR(LCD_CLK);
1.80 +
1.81 + for (mask = 0x80; mask; mask >>= 1)
1.82 + {
1.83 + if (v & mask)
1.84 + {
1.85 + #ifdef DEBUG
1.86 + printf("1");
1.87 + #endif
1.88 + SET(LCD_SEND);
1.89 + }
1.90 + else
1.91 + {
1.92 + #ifdef DEBUG
1.93 + printf("0");
1.94 + #endif
1.95 + CLR(LCD_SEND);
1.96 + }
1.97 +
1.98 + SET(LCD_CLK);
1.99 + CLR(LCD_CLK);
1.100 + }
1.101 +
1.102 + #ifdef DEBUG
1.103 + printf("\n");
1.104 + #endif
1.105 +}
1.106 +
1.107 +void LCD_init(void)
1.108 +{
1.109 + /* Perform a hardware reset on the LCD. */
1.110 + /* CS=1, CLK=0, D/C=0 apparently superfluous */
1.111 +
1.112 + SET(LCD_RESET);
1.113 + usleep(50000);
1.114 + CLR(LCD_RESET);
1.115 + usleep(50000);
1.116 + SET(LCD_RESET);
1.117 + usleep(50000);
1.118 +
1.119 + /* CS=1, CLK=1, D/C=1, 10ms, SWRESET, 10ms apparently superfluous */
1.120 +
1.121 + LCD_send(LCD_COMMAND, LCD_SLEEPOUT);
1.122 +
1.123 + /* Configure display memory access. */
1.124 +
1.125 + LCD_send(LCD_COMMAND, LCD_MADCTL);
1.126 + LCD_send(LCD_DATA, LCD_MADCTL_MY_MX);
1.127 +
1.128 + /* Contrast setting (suggested value 0x40), 10ms apparently superfluous */
1.129 +
1.130 + /* Switch the display on. */
1.131 +
1.132 + LCD_send(LCD_COMMAND, LCD_DISPON);
1.133 +
1.134 + /* Configure the display mode/format. */
1.135 + /* 5 == 16bpp */
1.136 +
1.137 + LCD_send(LCD_COMMAND, LCD_COLMOD);
1.138 + LCD_send(LCD_DATA, 0x05);
1.139 +
1.140 + LCD_send(LCD_COMMAND, LCD_NOP);
1.141 +
1.142 + /* Switch the backlight on. */
1.143 +
1.144 + SET(LCD_BACKLIGHT);
1.145 +}
1.146 +
1.147 +void LCD_window(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax)
1.148 +{
1.149 + LCD_send(LCD_COMMAND, LCD_CASET);
1.150 + LCD_send(LCD_DATA, xmin);
1.151 + LCD_send(LCD_DATA, xmax);
1.152 + LCD_send(LCD_COMMAND, LCD_PASET);
1.153 + LCD_send(LCD_DATA, ymin);
1.154 + LCD_send(LCD_DATA, ymax);
1.155 +}
1.156 +
1.157 +void LCD_blit_int(uint16_t colour)
1.158 +{
1.159 + LCD_send_more_data((colour >> 8) & 0xff); /* RGGB -> RG */
1.160 + LCD_send_more_data(colour & 0xff); /* RGGB -> GB */
1.161 +}
1.162 +
1.163 +void LCD_area(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax, uint16_t colour)
1.164 +{
1.165 + uint16_t i, limit = ((xmax - xmin + 1) * (ymax - ymin + 1)) + 1;
1.166 +
1.167 + LCD_window(xmin, ymin, xmax, ymax);
1.168 + LCD_send(LCD_COMMAND, LCD_RAMWR);
1.169 +
1.170 + for (i = 0; i < limit; i++)
1.171 + {
1.172 + LCD_blit_int(colour);
1.173 + }
1.174 +
1.175 + LCD_send(LCD_COMMAND, LCD_NOP);
1.176 +}
1.177 +
1.178 +void LCD_image(int x, int y, uint16_t image[], uint8_t width, uint8_t height)
1.179 +{
1.180 + LCD_image_region(x, y, image, width, height, 0, width, 0, height);
1.181 +}
1.182 +
1.183 +void LCD_image_region(int x, int y, 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)
1.184 +{
1.185 + uint8_t xmin, ymin, xcmax, ycmax, xcmin, xc, yc;
1.186 + uint16_t xmax, ymax, colour;
1.187 +
1.188 + /* Find the screen ranges for blitting. */
1.189 +
1.190 + xmin = (x < 0) ? 0 : x;
1.191 + ymin = (y < 0) ? 0 : y;
1.192 + xmax = (x + span_x > SCREEN_X_MAX) ? SCREEN_X_MAX : x + span_x - 1;
1.193 + ymax = (y + span_y > SCREEN_Y_MAX) ? SCREEN_Y_MAX : y + span_y - 1;
1.194 +
1.195 + if ((xmin > SCREEN_X_MAX) || (ymin > SCREEN_Y_MAX) || (xmax < 0) || (ymax < 0))
1.196 + {
1.197 + return;
1.198 + }
1.199 +
1.200 + /* Find the image ranges for reading data. */
1.201 +
1.202 + xcmin = (x < 0) ? -x + from_x : from_x;
1.203 + yc = (y < 0) ? -y + from_y : from_y;
1.204 + xcmax = from_x + ((x + span_x > SCREEN_WIDTH) ? SCREEN_WIDTH - x : span_x);
1.205 + ycmax = from_y + ((y + span_y > SCREEN_HEIGHT) ? SCREEN_HEIGHT - y : span_y);
1.206 +
1.207 + LCD_window(xmin, ymin, xmax, ymax);
1.208 + LCD_send(LCD_COMMAND, LCD_RAMWR);
1.209 +
1.210 + /* Reading from the appropriate image ranges, blit to the appropriate
1.211 + screen ranges. */
1.212 +
1.213 + xc = xcmin;
1.214 + while (yc < ycmax)
1.215 + {
1.216 + colour = image[yc * width + xc];
1.217 + LCD_blit_int(colour);
1.218 +
1.219 + xc++;
1.220 + if (xc == xcmax)
1.221 + {
1.222 + xc = xcmin;
1.223 + yc++;
1.224 + }
1.225 + }
1.226 +
1.227 + LCD_send(LCD_COMMAND, LCD_NOP);
1.228 +}
1.229 +
1.230 +void LCD_normal(void)
1.231 +{
1.232 + LCD_send(LCD_COMMAND, LCD_NORON);
1.233 +}
1.234 +
1.235 +void LCD_scroll_area(uint8_t top_fixed, uint8_t scrolling, uint8_t bottom_fixed)
1.236 +{
1.237 + LCD_send(LCD_COMMAND, LCD_VSCRDEF);
1.238 + LCD_send(LCD_DATA, top_fixed);
1.239 + LCD_send(LCD_DATA, scrolling);
1.240 + LCD_send(LCD_DATA, bottom_fixed);
1.241 +}
1.242 +
1.243 +void LCD_scroll_start(uint8_t address)
1.244 +{
1.245 + LCD_send(LCD_COMMAND, LCD_SEP);
1.246 + LCD_send(LCD_DATA, address);
1.247 +}
1.248 +
1.249 +/* vim: tabstop=4 expandtab shiftwidth=4
1.250 + */