PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
Pokitto
Date:
Fri Dec 29 05:17:38 2017 +0000
Revision:
24:9561281d0378
PokittoLib synced with Github and all Spianl & Hanski contributions so far

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 24:9561281d0378 1 /**************************************************************************/
Pokitto 24:9561281d0378 2 /*!
Pokitto 24:9561281d0378 3 @file HWLCD.h
Pokitto 24:9561281d0378 4 @author Jonne Valola
Pokitto 24:9561281d0378 5
Pokitto 24:9561281d0378 6 @section LICENSE
Pokitto 24:9561281d0378 7
Pokitto 24:9561281d0378 8 Software License Agreement (BSD License)
Pokitto 24:9561281d0378 9
Pokitto 24:9561281d0378 10 Copyright (c) 2016, Jonne Valola
Pokitto 24:9561281d0378 11 All rights reserved.
Pokitto 24:9561281d0378 12
Pokitto 24:9561281d0378 13 Redistribution and use in source and binary forms, with or without
Pokitto 24:9561281d0378 14 modification, are permitted provided that the following conditions are met:
Pokitto 24:9561281d0378 15 1. Redistributions of source code must retain the above copyright
Pokitto 24:9561281d0378 16 notice, this list of conditions and the following disclaimer.
Pokitto 24:9561281d0378 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 24:9561281d0378 18 notice, this list of conditions and the following disclaimer in the
Pokitto 24:9561281d0378 19 documentation and/or other materials provided with the distribution.
Pokitto 24:9561281d0378 20 3. Neither the name of the copyright holders nor the
Pokitto 24:9561281d0378 21 names of its contributors may be used to endorse or promote products
Pokitto 24:9561281d0378 22 derived from this software without specific prior written permission.
Pokitto 24:9561281d0378 23
Pokitto 24:9561281d0378 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 24:9561281d0378 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 24:9561281d0378 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 24:9561281d0378 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 24:9561281d0378 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 24:9561281d0378 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 24:9561281d0378 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 24:9561281d0378 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 24:9561281d0378 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 24:9561281d0378 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 24:9561281d0378 34 */
Pokitto 24:9561281d0378 35 /**************************************************************************/
Pokitto 24:9561281d0378 36
Pokitto 24:9561281d0378 37 #ifndef __HWLCD_H__
Pokitto 24:9561281d0378 38 #define __HWLCD_H__
Pokitto 24:9561281d0378 39
Pokitto 24:9561281d0378 40 #include "mbed.h"
Pokitto 24:9561281d0378 41 #include "gpio_api.h"
Pokitto 24:9561281d0378 42 #include "pinmap.h"
Pokitto 24:9561281d0378 43
Pokitto 24:9561281d0378 44 #define write_command write_command_16
Pokitto 24:9561281d0378 45 #define write_data write_data_16
Pokitto 24:9561281d0378 46
Pokitto 24:9561281d0378 47 namespace Pokitto {
Pokitto 24:9561281d0378 48
Pokitto 24:9561281d0378 49 struct SpriteInfo {
Pokitto 24:9561281d0378 50 const uint8_t* bitmapData;
Pokitto 24:9561281d0378 51 int16_t x;
Pokitto 24:9561281d0378 52 int16_t y;
Pokitto 24:9561281d0378 53 int16_t oldx;
Pokitto 24:9561281d0378 54 int16_t oldy;
Pokitto 24:9561281d0378 55 uint8_t w;
Pokitto 24:9561281d0378 56 uint8_t h;
Pokitto 24:9561281d0378 57 uint16_t palette[4];
Pokitto 24:9561281d0378 58 };
Pokitto 24:9561281d0378 59
Pokitto 24:9561281d0378 60 extern void initBacklight();
Pokitto 24:9561281d0378 61 extern void setBacklight(float);
Pokitto 24:9561281d0378 62 extern void lcdFillSurface(uint16_t);
Pokitto 24:9561281d0378 63 extern void lcdPixel(int16_t x, int16_t y, uint16_t c);
Pokitto 24:9561281d0378 64 extern void setWindow(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2);
Pokitto 24:9561281d0378 65 extern void lcdTile(int16_t x0, int16_t y0, int16_t width, int16_t height, uint16_t* gfx);
Pokitto 24:9561281d0378 66 extern void lcdRectangle(int16_t x, int16_t y,int16_t x2, int16_t y2, uint16_t color);
Pokitto 24:9561281d0378 67 extern void lcdInit();
Pokitto 24:9561281d0378 68 extern void lcdSleep();
Pokitto 24:9561281d0378 69 extern void lcdWakeUp();
Pokitto 24:9561281d0378 70 extern void lcdRefresh(uint8_t *, uint16_t*);
Pokitto 24:9561281d0378 71 extern void lcdRefreshAB(uint8_t *, uint16_t*);
Pokitto 24:9561281d0378 72 extern void lcdRefreshGB(uint8_t *, uint16_t*);
Pokitto 24:9561281d0378 73 extern void lcdRefreshRegionMode1(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint8_t * scrbuf, uint16_t * paletteptr);
Pokitto 24:9561281d0378 74 extern void lcdRefreshMode1(uint8_t *, uint16_t*);
Pokitto 24:9561281d0378 75 extern void lcdRefreshMode1Spr(uint8_t * scrbuf, uint16_t* paletteptr, Pokitto::SpriteInfo* sprites, bool useDirectMode);
Pokitto 24:9561281d0378 76 extern void lcdRefreshMode2(uint8_t *, uint16_t*);
Pokitto 24:9561281d0378 77 extern void lcdRefreshMode3(uint8_t *, uint16_t*);
Pokitto 24:9561281d0378 78 extern void lcdRefreshModeGBC(uint8_t *, uint16_t*);
Pokitto 24:9561281d0378 79 extern void lcdRefreshMode13(uint8_t *, uint16_t*, uint8_t);
Pokitto 24:9561281d0378 80
Pokitto 24:9561281d0378 81 /** Update LCD from 1-bit tile mode */
Pokitto 24:9561281d0378 82 extern void lcdRefreshT1(uint8_t*, uint8_t*, uint8_t*, uint16_t*);
Pokitto 24:9561281d0378 83 extern void lcdClear();
Pokitto 24:9561281d0378 84 extern void lcdFill(uint16_t);
Pokitto 24:9561281d0378 85 /** Blit one word of data*/
Pokitto 24:9561281d0378 86 extern void blitWord(uint16_t);
Pokitto 24:9561281d0378 87
Pokitto 24:9561281d0378 88 /**************************************************************************/
Pokitto 24:9561281d0378 89 /** PINS AND PORTS **/
Pokitto 24:9561281d0378 90 /**************************************************************************/
Pokitto 24:9561281d0378 91
Pokitto 24:9561281d0378 92 #if POK_BOARDREV == 1
Pokitto 24:9561281d0378 93 /** 2-layer board version 1.3 **/
Pokitto 24:9561281d0378 94 #define LCD_CD_PORT 0
Pokitto 24:9561281d0378 95 #define LCD_CD_PIN 2
Pokitto 24:9561281d0378 96 #define LCD_WR_PORT 1
Pokitto 24:9561281d0378 97 #define LCD_WR_PIN 23
Pokitto 24:9561281d0378 98 #define LCD_RD_PORT 1
Pokitto 24:9561281d0378 99 #define LCD_RD_PIN 24
Pokitto 24:9561281d0378 100 #define LCD_RES_PORT 1
Pokitto 24:9561281d0378 101 #define LCD_RES_PIN 28
Pokitto 24:9561281d0378 102 #else
Pokitto 24:9561281d0378 103 /** 4-layer board version 2.1 **/
Pokitto 24:9561281d0378 104 #define LCD_CD_PORT 0
Pokitto 24:9561281d0378 105 #define LCD_CD_PIN 2
Pokitto 24:9561281d0378 106 #define LCD_WR_PORT 1
Pokitto 24:9561281d0378 107 #define LCD_WR_PIN 12
Pokitto 24:9561281d0378 108 #define LCD_RD_PORT 1
Pokitto 24:9561281d0378 109 #define LCD_RD_PIN 24
Pokitto 24:9561281d0378 110 #define LCD_RES_PORT 1
Pokitto 24:9561281d0378 111 #define LCD_RES_PIN 0
Pokitto 24:9561281d0378 112 #endif
Pokitto 24:9561281d0378 113
Pokitto 24:9561281d0378 114 /**************************************************************************/
Pokitto 24:9561281d0378 115 /** LCD CONTROL MACROS **/
Pokitto 24:9561281d0378 116 /**************************************************************************/
Pokitto 24:9561281d0378 117
Pokitto 24:9561281d0378 118 #define CLR_RESET LPC_GPIO_PORT->CLR[LCD_RES_PORT] = 1 << LCD_RES_PIN; //RST = (0); // Clear pin
Pokitto 24:9561281d0378 119 #define SET_RESET LPC_GPIO_PORT->SET[LCD_RES_PORT] = 1 << LCD_RES_PIN; // RST = (1); // Set pin
Pokitto 24:9561281d0378 120
Pokitto 24:9561281d0378 121 #define CLR_CD { LPC_GPIO_PORT->CLR[LCD_CD_PORT] = 1 << LCD_CD_PIN; } // RS = (0); // Clear pin
Pokitto 24:9561281d0378 122 #define SET_CD { LPC_GPIO_PORT->SET[LCD_CD_PORT] = 1 << LCD_CD_PIN; }// RS = (1); // Set pin
Pokitto 24:9561281d0378 123
Pokitto 24:9561281d0378 124 #define CLR_WR { LPC_GPIO_PORT->CLR[LCD_WR_PORT] = 1 << LCD_WR_PIN; __asm("nop");__asm("nop");}//WR = (0); // Clear pin
Pokitto 24:9561281d0378 125 #define SET_WR LPC_GPIO_PORT->SET[LCD_WR_PORT] = 1 << LCD_WR_PIN; //WR = (1); // Set pin
Pokitto 24:9561281d0378 126
Pokitto 24:9561281d0378 127 #define CLR_RD LPC_GPIO_PORT->CLR[LCD_RD_PORT] = 1 << LCD_RD_PIN; //RD = (0); // Clear pin
Pokitto 24:9561281d0378 128 #define SET_RD LPC_GPIO_PORT->SET[LCD_RD_PORT] = 1 << LCD_RD_PIN; //RD = (1); // Set pin
Pokitto 24:9561281d0378 129
Pokitto 24:9561281d0378 130 #define SET_CS //CS tied to ground
Pokitto 24:9561281d0378 131 #define CLR_CS
Pokitto 24:9561281d0378 132
Pokitto 24:9561281d0378 133 #define CLR_CS_CD_SET_RD_WR {CLR_CD; SET_RD; SET_WR;}
Pokitto 24:9561281d0378 134 #define CLR_CS_SET_CD_RD_WR {SET_CD; SET_RD; SET_WR;}
Pokitto 24:9561281d0378 135 #define SET_CD_RD_WR {SET_CD; SET_RD; SET_WR;}
Pokitto 24:9561281d0378 136 #define SET_WR_CS SET_WR;
Pokitto 24:9561281d0378 137
Pokitto 24:9561281d0378 138 #define SET_MASK_P2 LPC_GPIO_PORT->MASK[2] = ~(0x7FFF8); //mask P2_3 ...P2_18
Pokitto 24:9561281d0378 139 #define CLR_MASK_P2 LPC_GPIO_PORT->MASK[2] = 0; // all on
Pokitto 24:9561281d0378 140
Pokitto 24:9561281d0378 141
Pokitto 24:9561281d0378 142
Pokitto 24:9561281d0378 143 /**************************************************************************/
Pokitto 24:9561281d0378 144 /** SETUP GPIO & DATA **/
Pokitto 24:9561281d0378 145 /**************************************************************************/
Pokitto 24:9561281d0378 146
Pokitto 24:9561281d0378 147 static void setup_gpio()
Pokitto 24:9561281d0378 148 {
Pokitto 24:9561281d0378 149 /** control lines **/
Pokitto 24:9561281d0378 150 LPC_GPIO_PORT->DIR[LCD_CD_PORT] |= (1 << LCD_CD_PIN );
Pokitto 24:9561281d0378 151 LPC_GPIO_PORT->DIR[LCD_WR_PORT] |= (1 << LCD_WR_PIN );
Pokitto 24:9561281d0378 152 LPC_GPIO_PORT->DIR[LCD_RD_PORT] |= (1 << LCD_RD_PIN );
Pokitto 24:9561281d0378 153 LPC_GPIO_PORT->DIR[LCD_RES_PORT] |= (1 << LCD_RES_PIN );
Pokitto 24:9561281d0378 154 /** data lines **/
Pokitto 24:9561281d0378 155 LPC_GPIO_PORT->DIR[2] |= (0xFFFF << 3); // P2_3...P2_18 as output
Pokitto 24:9561281d0378 156
Pokitto 24:9561281d0378 157 pin_mode(P2_3,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 158 pin_mode(P2_4,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 159 pin_mode(P2_5,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 160 pin_mode(P2_6,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 161
Pokitto 24:9561281d0378 162 pin_mode(P2_7,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 163 pin_mode(P2_8,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 164 pin_mode(P2_9,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 165 pin_mode(P2_10,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 166
Pokitto 24:9561281d0378 167 pin_mode(P2_11,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 168 pin_mode(P2_12,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 169 pin_mode(P2_13,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 170 pin_mode(P2_14,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 171
Pokitto 24:9561281d0378 172 pin_mode(P2_15,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 173 pin_mode(P2_16,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 174 pin_mode(P2_17,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 175 pin_mode(P2_18,PullNone); // turn off pull-up
Pokitto 24:9561281d0378 176 }
Pokitto 24:9561281d0378 177
Pokitto 24:9561281d0378 178
Pokitto 24:9561281d0378 179
Pokitto 24:9561281d0378 180
Pokitto 24:9561281d0378 181 #define HI_BYTE(d) (LPC_GPIO->MPIN[1]= (d<<13)) //((d>>8)<<21))
Pokitto 24:9561281d0378 182 #define LO_BYTE(d) (LPC_GPIO->MPIN[1]= (d<<21)) //because of mask makes no difference
Pokitto 24:9561281d0378 183
Pokitto 24:9561281d0378 184 // Macros to set data bus direction to input/output
Pokitto 24:9561281d0378 185 #define LCD_GPIO2DATA_SETINPUT GPIO_GPIO2DIR &= ~LCD_DATA_MASK
Pokitto 24:9561281d0378 186 #define LCD_GPIO2DATA_SETOUTPUT GPIO_GPIO2DIR |= LCD_DATA_MASK
Pokitto 24:9561281d0378 187
Pokitto 24:9561281d0378 188
Pokitto 24:9561281d0378 189 // Basic Color definitions
Pokitto 24:9561281d0378 190 #define COLOR_BLACK (uint16_t)(0x0000)
Pokitto 24:9561281d0378 191 #define COLOR_BLUE (uint16_t)(0x001F)
Pokitto 24:9561281d0378 192 #define COLOR_RED (uint16_t)(0xF800)
Pokitto 24:9561281d0378 193 #define COLOR_GREEN (uint16_t)(0x07E0)
Pokitto 24:9561281d0378 194 #define COLOR_CYAN (uint16_t)(0x07FF)
Pokitto 24:9561281d0378 195 #define COLOR_MAGENTA (uint16_t)(0xF81F)
Pokitto 24:9561281d0378 196 #define COLOR_YELLOW (uint16_t)(0xFFE0)
Pokitto 24:9561281d0378 197 #define COLOR_WHITE (uint16_t)(0xFFFF)
Pokitto 24:9561281d0378 198
Pokitto 24:9561281d0378 199 // Grayscale Values
Pokitto 24:9561281d0378 200 #define COLOR_GRAY_15 (uint16_t)(0x0861) // 15 15 15
Pokitto 24:9561281d0378 201 #define COLOR_GRAY_30 (uint16_t)(0x18E3) // 30 30 30
Pokitto 24:9561281d0378 202 #define COLOR_GRAY_50 (uint16_t)(0x3186) // 50 50 50
Pokitto 24:9561281d0378 203 #define COLOR_GRAY_80 (uint16_t)(0x528A) // 80 80 80
Pokitto 24:9561281d0378 204 #define COLOR_GRAY_128 (uint16_t)(0x8410) // 128 128 128
Pokitto 24:9561281d0378 205 #define COLOR_GRAY_200 (uint16_t)(0xCE59) // 200 200 200
Pokitto 24:9561281d0378 206 #define COLOR_GRAY_225 (uint16_t)(0xE71C) // 225 225 225
Pokitto 24:9561281d0378 207
Pokitto 24:9561281d0378 208
Pokitto 24:9561281d0378 209 } // namespace pokitto
Pokitto 24:9561281d0378 210 #endif // __HWLCD_H_
Pokitto 24:9561281d0378 211
Pokitto 24:9561281d0378 212
Pokitto 24:9561281d0378 213