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

Committer:
Pokitto
Date:
Tue Jan 30 10:41:47 2018 +0000
Revision:
31:f4b9b85c7b62
Sound output improvements added:  louder, clearer, faster!

Who changed what in which revision?

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