4D systems Picaso uLCD 32PTU touch display library

Committer:
CaptainR
Date:
Thu Sep 29 19:40:53 2016 +0000
Revision:
27:dbf79d116497
Parent:
20:88e137b9ea46
get word and get string

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CaptainR 12:29f5ad896382 1 //
CaptainR 12:29f5ad896382 2 // Picaso_4DGL-32PTU is a class to drive 4D Systems TFT touch screens with PICASO processor
CaptainR 12:29f5ad896382 3 // Tested with NUCLEO L152RE development board
CaptainR 12:29f5ad896382 4 // Copyright (C) <2016> Rihards Balass <rihards.balass@gmail.com>
CaptainR 12:29f5ad896382 5 //
CaptainR 12:29f5ad896382 6 // Picaso_4DGL-32PTU is free software: you can redistribute it and/or modify
CaptainR 12:29f5ad896382 7 // it under the terms of the GNU General Public License as published by
CaptainR 12:29f5ad896382 8 // the Free Software Foundation, either version 3 of the License, or
CaptainR 12:29f5ad896382 9 // (at your option) any later version.
CaptainR 12:29f5ad896382 10 //
CaptainR 12:29f5ad896382 11 // Picaso_4DGL-32PTU is distributed in the hope that it will be useful,
CaptainR 12:29f5ad896382 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
CaptainR 12:29f5ad896382 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
CaptainR 12:29f5ad896382 14 // GNU General Public License for more details.
CaptainR 12:29f5ad896382 15 //
CaptainR 12:29f5ad896382 16 // You can see GNU General Public License at <http://www.gnu.org/licenses/>.
CaptainR 12:29f5ad896382 17 //
CaptainR 12:29f5ad896382 18
CaptainR 12:29f5ad896382 19 #include "mbed.h"
CaptainR 12:29f5ad896382 20 #include "Picaso_4DGL-32PTU.h"
CaptainR 12:29f5ad896382 21
CaptainR 12:29f5ad896382 22 //**************************************************************************
CaptainR 12:29f5ad896382 23 // The Media Init command initialises a uSD/SD/SDHC memory card for further operations.
CaptainR 12:29f5ad896382 24 // The SD card is connected to the SPI (serial peripheral interface) of the PICASO-GFX2 chip.
CaptainR 12:29f5ad896382 25 //**************************************************************************
CaptainR 12:29f5ad896382 26 short PICASO_4DGL :: media_Init() {
CaptainR 12:29f5ad896382 27
CaptainR 12:29f5ad896382 28 char command[2] = "";
CaptainR 12:29f5ad896382 29
CaptainR 12:29f5ad896382 30 command[0] = (MEDIA_INIT >> (8*1)) & 0xff;
CaptainR 12:29f5ad896382 31 command[1] = (MEDIA_INIT >> (8*0)) & 0xff;
CaptainR 12:29f5ad896382 32
CaptainR 12:29f5ad896382 33 writeCOMMAND(command, 2);
CaptainR 12:29f5ad896382 34 short success = mediaInitResponse();
CaptainR 12:29f5ad896382 35 #ifdef DEBUGMODE
CaptainR 12:29f5ad896382 36 pc.printf("\n\r DEBUG: uSD card INIT: %i\n\r", success);
CaptainR 12:29f5ad896382 37 #endif
CaptainR 12:29f5ad896382 38 return success;
CaptainR 12:29f5ad896382 39 }
CaptainR 12:29f5ad896382 40
CaptainR 12:29f5ad896382 41 //**************************************************************************
CaptainR 12:29f5ad896382 42 // The Set Byte Address command sets the media memory internal
CaptainR 12:29f5ad896382 43 // Address pointer for access at a non-sector aligned byte address.
CaptainR 12:29f5ad896382 44 //**************************************************************************
CaptainR 12:29f5ad896382 45 bool PICASO_4DGL :: media_SetAdd(int address) {
CaptainR 12:29f5ad896382 46
CaptainR 12:29f5ad896382 47 char command[6] = "";
CaptainR 12:29f5ad896382 48
CaptainR 12:29f5ad896382 49 command[0] = (MEDIA_SET_ADD >> (8*1)) & 0xff;
CaptainR 12:29f5ad896382 50 command[1] = (MEDIA_SET_ADD >> (8*0)) & 0xff;
CaptainR 12:29f5ad896382 51 command[2] = (address >> (8*3)) & 0xff;
CaptainR 12:29f5ad896382 52 command[3] = (address >> (8*2)) & 0xff;
CaptainR 12:29f5ad896382 53 command[4] = (address >> (8*1)) & 0xff;
CaptainR 12:29f5ad896382 54 command[5] = (address >> (8*0)) & 0xff;
CaptainR 12:29f5ad896382 55
CaptainR 12:29f5ad896382 56 writeCOMMAND(command, 6);
CaptainR 12:29f5ad896382 57 bool success = getResponse(1);
CaptainR 12:29f5ad896382 58 #ifdef DEBUGMODE
CaptainR 12:29f5ad896382 59 pc.printf("\n\r DEBUG: Set byte address: %i\n\r", success);
CaptainR 12:29f5ad896382 60 #endif
CaptainR 12:29f5ad896382 61 return success;
CaptainR 12:29f5ad896382 62 }
CaptainR 12:29f5ad896382 63
CaptainR 12:29f5ad896382 64 //**************************************************************************
CaptainR 12:29f5ad896382 65 // The Set Sector Address command sets the media memory internal Address pointer for sector access.
CaptainR 12:29f5ad896382 66 //**************************************************************************
CaptainR 12:29f5ad896382 67 bool PICASO_4DGL :: media_SetSector(int address) {
CaptainR 12:29f5ad896382 68
CaptainR 12:29f5ad896382 69 char command[6] = "";
CaptainR 12:29f5ad896382 70
CaptainR 12:29f5ad896382 71 command[0] = (MEDIA_SET_SECTOR >> (8*1)) & 0xff;
CaptainR 12:29f5ad896382 72 command[1] = (MEDIA_SET_SECTOR >> (8*0)) & 0xff;
CaptainR 12:29f5ad896382 73 command[2] = (address >> (8*3)) & 0xff;
CaptainR 12:29f5ad896382 74 command[3] = (address >> (8*2)) & 0xff;
CaptainR 12:29f5ad896382 75 command[4] = (address >> (8*1)) & 0xff;
CaptainR 12:29f5ad896382 76 command[5] = (address >> (8*0)) & 0xff;
CaptainR 12:29f5ad896382 77
CaptainR 12:29f5ad896382 78 writeCOMMAND(command, 6);
CaptainR 12:29f5ad896382 79 bool success = getResponse(1);
CaptainR 12:29f5ad896382 80 #ifdef DEBUGMODE
CaptainR 12:29f5ad896382 81 pc.printf("\n\r DEBUG: Set sector address: %i\n\r", success);
CaptainR 12:29f5ad896382 82 #endif
CaptainR 12:29f5ad896382 83 return success;
CaptainR 12:29f5ad896382 84 }
CaptainR 12:29f5ad896382 85
CaptainR 12:29f5ad896382 86 //**************************************************************************
CaptainR 12:29f5ad896382 87 // The Read Sector command reads and returns 512 bytes (256 words)
CaptainR 12:29f5ad896382 88 // pointed to by the internal Sector pointer, determined by the
CaptainR 12:29f5ad896382 89 // “Set Sector Address” command.
CaptainR 12:29f5ad896382 90 // After the read the Sector pointer is automatically incremented by 1.
CaptainR 12:29f5ad896382 91 // Answer = acknowledge (byte) , status (word), block (sector) = 1 + 2 + 512 = 515 bytes
CaptainR 12:29f5ad896382 92 //**************************************************************************
CaptainR 13:1a0800957412 93 bool PICASO_4DGL :: media_RdSector() {
CaptainR 12:29f5ad896382 94
CaptainR 12:29f5ad896382 95 char command[2] = "";
CaptainR 12:29f5ad896382 96
CaptainR 12:29f5ad896382 97 command[0] = (MEDIA_READ_SECTOR >> (8*1)) & 0xff;
CaptainR 12:29f5ad896382 98 command[1] = (MEDIA_READ_SECTOR >> (8*0)) & 0xff;
CaptainR 12:29f5ad896382 99
CaptainR 12:29f5ad896382 100 writeCOMMAND(command, 2);
CaptainR 12:29f5ad896382 101 bool success = readSectorResponse(515);
CaptainR 12:29f5ad896382 102 #ifdef DEBUGMODE
CaptainR 13:1a0800957412 103 pc.printf("\n\r DEBUG: Read sector: %i\n\r", success);
CaptainR 12:29f5ad896382 104 #endif
CaptainR 12:29f5ad896382 105 return success;
CaptainR 12:29f5ad896382 106 }
CaptainR 12:29f5ad896382 107
CaptainR 12:29f5ad896382 108 //**************************************************************************
CaptainR 12:29f5ad896382 109 // The Write Sector command writes 512 bytes (256 words) from a source memory
CaptainR 12:29f5ad896382 110 // block into the uSD card. After the write the Sect pointer is automatically incremented by 1.
CaptainR 12:29f5ad896382 111 // Response = acknowledge (byte) , status (word)
CaptainR 12:29f5ad896382 112 //**************************************************************************
CaptainR 13:1a0800957412 113 bool PICASO_4DGL :: media_WrSector(char *block) {
CaptainR 12:29f5ad896382 114
CaptainR 12:29f5ad896382 115 char command[514] = "";
CaptainR 14:561cb06a2739 116 bool success = false;
CaptainR 15:86bdf382e6f7 117 int j;
CaptainR 12:29f5ad896382 118
CaptainR 12:29f5ad896382 119 command[0] = (MEDIA_WRITE_SECTOR >> (8*1)) & 0xff;
CaptainR 12:29f5ad896382 120 command[1] = (MEDIA_WRITE_SECTOR >> (8*0)) & 0xff;
CaptainR 12:29f5ad896382 121
CaptainR 15:86bdf382e6f7 122 #if DEBUGMODE
CaptainR 15:86bdf382e6f7 123 pc.printf("\n\r DEBUG: string length = %i\n\r", strlen(block));
CaptainR 15:86bdf382e6f7 124 #endif
CaptainR 14:561cb06a2739 125 if (strlen(block) <= 512) {
CaptainR 15:86bdf382e6f7 126 j = 513 - strlen(block);
CaptainR 14:561cb06a2739 127 for (int i = 0; i < strlen(block); i++) {
CaptainR 14:561cb06a2739 128 command[j++] = block[i];
CaptainR 14:561cb06a2739 129 }
CaptainR 14:561cb06a2739 130 success = media_WrData(command, 514);
CaptainR 14:561cb06a2739 131 }
CaptainR 14:561cb06a2739 132 else { // data is bigger than one block
CaptainR 15:86bdf382e6f7 133 j = 513 - (strlen(block) % 512); // set the first block pointer
CaptainR 14:561cb06a2739 134 for (int i = 0; i < strlen(block); i++) {
CaptainR 14:561cb06a2739 135 if (j == 513) {
CaptainR 14:561cb06a2739 136 success = media_WrData(command, 514);
CaptainR 14:561cb06a2739 137 #if DEBUGMODE
CaptainR 14:561cb06a2739 138 pc.printf("\n\r DEBUG: Block send = %i", success);
CaptainR 15:86bdf382e6f7 139 //if (success) puts("\n\r Sector send: OK");
CaptainR 15:86bdf382e6f7 140 //else puts("\n\r Sector send: FAIL");
CaptainR 14:561cb06a2739 141 #endif
CaptainR 14:561cb06a2739 142 j = 2;
CaptainR 14:561cb06a2739 143 command[j++] = block[i];
CaptainR 14:561cb06a2739 144 }
CaptainR 14:561cb06a2739 145 else command[j++] = block[i];
CaptainR 14:561cb06a2739 146 }
CaptainR 14:561cb06a2739 147 }
CaptainR 14:561cb06a2739 148 return success;
CaptainR 14:561cb06a2739 149 }
CaptainR 14:561cb06a2739 150
CaptainR 14:561cb06a2739 151 bool PICASO_4DGL :: media_WrData(char *block, int size) {
CaptainR 14:561cb06a2739 152
CaptainR 14:561cb06a2739 153 #if DEBUGMODE
CaptainR 14:561cb06a2739 154 pc.printf("\n\r DEBUG: Write block =");
CaptainR 14:561cb06a2739 155 for (int k = 2; k < size; k++) {
CaptainR 14:561cb06a2739 156 pc.printf(" %02X", block[k]);
CaptainR 14:561cb06a2739 157 }
CaptainR 14:561cb06a2739 158 pc.printf("\n\r");
CaptainR 12:29f5ad896382 159 #endif
CaptainR 15:86bdf382e6f7 160 writeCOMMAND_2(block, size);
CaptainR 14:561cb06a2739 161 bool success = writeSectorResponse(3);
CaptainR 12:29f5ad896382 162 return success;
CaptainR 12:29f5ad896382 163 }
CaptainR 12:29f5ad896382 164
CaptainR 16:cb072eea16e9 165 //**************************************************************************
CaptainR 16:cb072eea16e9 166 // The Read Byte command returns the byte value from the current media address,
CaptainR 16:cb072eea16e9 167 // set by the “Set Byte Address” command.
CaptainR 16:cb072eea16e9 168 // The internal byte address will then be internally incremented by one.
CaptainR 16:cb072eea16e9 169 //**************************************************************************
CaptainR 16:cb072eea16e9 170 bool PICASO_4DGL :: media_ReadByte() {
CaptainR 16:cb072eea16e9 171
CaptainR 16:cb072eea16e9 172 char command[2] = "";
CaptainR 16:cb072eea16e9 173
CaptainR 16:cb072eea16e9 174 command[0] = (MEDIA_READ_BYTE >> (8*1)) & 0xff;
CaptainR 16:cb072eea16e9 175 command[1] = (MEDIA_READ_BYTE >> (8*0)) & 0xff;
CaptainR 16:cb072eea16e9 176
CaptainR 16:cb072eea16e9 177 writeCOMMAND(command, 2);
CaptainR 18:829f3e2c064c 178 bool success = readResponse();
CaptainR 16:cb072eea16e9 179 #ifdef DEBUGMODE
CaptainR 16:cb072eea16e9 180 pc.printf("\n\r DEBUG: Read byte: %i\n\r", success);
CaptainR 16:cb072eea16e9 181 #endif
CaptainR 16:cb072eea16e9 182 return success;
CaptainR 16:cb072eea16e9 183 }
CaptainR 12:29f5ad896382 184
CaptainR 18:829f3e2c064c 185 //**************************************************************************
CaptainR 18:829f3e2c064c 186 // The Read Word command returns the word value (2 bytes) from the current media
CaptainR 18:829f3e2c064c 187 // address, set by the “Set Byte Address” command.
CaptainR 18:829f3e2c064c 188 // The internal byte address will then be internally incremented by one.
CaptainR 18:829f3e2c064c 189 // If the address is not aligned, the word will still be read correctly.
CaptainR 18:829f3e2c064c 190 //**************************************************************************
CaptainR 18:829f3e2c064c 191 bool PICASO_4DGL :: media_ReadWord() {
CaptainR 18:829f3e2c064c 192
CaptainR 18:829f3e2c064c 193 char command[2] = "";
CaptainR 18:829f3e2c064c 194
CaptainR 18:829f3e2c064c 195 command[0] = (MEDIA_READ_WORD >> (8*1)) & 0xff;
CaptainR 18:829f3e2c064c 196 command[1] = (MEDIA_READ_WORD >> (8*0)) & 0xff;
CaptainR 18:829f3e2c064c 197
CaptainR 18:829f3e2c064c 198 writeCOMMAND(command, 2);
CaptainR 18:829f3e2c064c 199 bool success = readResponse();
CaptainR 18:829f3e2c064c 200 #ifdef DEBUGMODE
CaptainR 18:829f3e2c064c 201 pc.printf("\n\r DEBUG: Read Word: %i\n\r", success);
CaptainR 18:829f3e2c064c 202 #endif
CaptainR 18:829f3e2c064c 203 return success;
CaptainR 18:829f3e2c064c 204 }
CaptainR 18:829f3e2c064c 205
CaptainR 19:a259bc128867 206 //**************************************************************************
CaptainR 19:a259bc128867 207 // Writes a byte to the current media address that was initially set with the
CaptainR 19:a259bc128867 208 // “Set Sector Address” command.
CaptainR 19:a259bc128867 209 //
CaptainR 19:a259bc128867 210 // Note: Writing bytes or words to a media sector must start from the beginning
CaptainR 19:a259bc128867 211 // of the sector. All writes will be incremental until the “Flush Media” command
CaptainR 19:a259bc128867 212 // is executed, or the sector address rolls over to the next sector.
CaptainR 19:a259bc128867 213 // When the “Flush Media” command is called, any remaining bytes in the sector
CaptainR 19:a259bc128867 214 // will be padded with 0xFF, destroying the previous contents.
CaptainR 19:a259bc128867 215 // An attempt to use the “Set Byte Address” command will result in the
CaptainR 19:a259bc128867 216 // lower 9 bits being interpreted as zero. If the writing rolls over to the
CaptainR 19:a259bc128867 217 // next sector, the “Flush Media” command is issued automatically internally.
CaptainR 19:a259bc128867 218 //**************************************************************************
CaptainR 19:a259bc128867 219 bool PICASO_4DGL :: media_WriteByte(short value) {
CaptainR 19:a259bc128867 220
CaptainR 19:a259bc128867 221 char command[4] = "";
CaptainR 19:a259bc128867 222
CaptainR 19:a259bc128867 223 command[0] = (MEDIA_WRITE_BYTE >> (8*1)) & 0xff;
CaptainR 19:a259bc128867 224 command[1] = (MEDIA_WRITE_BYTE >> (8*0)) & 0xff;
CaptainR 19:a259bc128867 225 command[2] = (value >> (8*1)) & 0xff;
CaptainR 19:a259bc128867 226 command[3] = (value >> (8*0)) & 0xff;
CaptainR 19:a259bc128867 227
CaptainR 19:a259bc128867 228 writeCOMMAND(command, 4);
CaptainR 19:a259bc128867 229 bool success = writeByteResponse();
CaptainR 19:a259bc128867 230 #ifdef DEBUGMODE
CaptainR 19:a259bc128867 231 pc.printf("\n\r DEBUG: Write Byte: %i\n\r", success);
CaptainR 19:a259bc128867 232 #endif
CaptainR 19:a259bc128867 233 return success;
CaptainR 19:a259bc128867 234 }
CaptainR 12:29f5ad896382 235
CaptainR 20:88e137b9ea46 236 //**************************************************************************
CaptainR 20:88e137b9ea46 237 // Writes a word to the current media address that was initially set with the
CaptainR 20:88e137b9ea46 238 // “Set Sector Address” command.
CaptainR 20:88e137b9ea46 239 //
CaptainR 20:88e137b9ea46 240 // Note: Writing bytes or words to a media sector must start from the beginning
CaptainR 20:88e137b9ea46 241 // of the sector. All writes will be incremental until the “Flush Media” command
CaptainR 20:88e137b9ea46 242 // is executed, or the sector address rolls over to the next sector.
CaptainR 20:88e137b9ea46 243 // When the “Flush Media” command is called, any remaining bytes in the sector
CaptainR 20:88e137b9ea46 244 // will be padded with 0xFF, destroying the previous contents.
CaptainR 20:88e137b9ea46 245 // An attempt to use the “Set Byte Address” command will result in the
CaptainR 20:88e137b9ea46 246 // lower 9 bits being interpreted as zero. If the writing rolls over to the
CaptainR 20:88e137b9ea46 247 // next sector, the “Flush Media” command is issued automatically internally.
CaptainR 20:88e137b9ea46 248 //**************************************************************************
CaptainR 20:88e137b9ea46 249 bool PICASO_4DGL :: media_WriteWord(short value) {
CaptainR 20:88e137b9ea46 250
CaptainR 20:88e137b9ea46 251 char command[4] = "";
CaptainR 20:88e137b9ea46 252
CaptainR 20:88e137b9ea46 253 command[0] = (MEDIA_WRITE_WORD >> (8*1)) & 0xff;
CaptainR 20:88e137b9ea46 254 command[1] = (MEDIA_WRITE_WORD >> (8*0)) & 0xff;
CaptainR 20:88e137b9ea46 255 command[2] = (value >> (8*1)) & 0xff;
CaptainR 20:88e137b9ea46 256 command[3] = (value >> (8*0)) & 0xff;
CaptainR 20:88e137b9ea46 257
CaptainR 20:88e137b9ea46 258 writeCOMMAND(command, 4);
CaptainR 20:88e137b9ea46 259 bool success = writeByteResponse();
CaptainR 20:88e137b9ea46 260 #ifdef DEBUGMODE
CaptainR 20:88e137b9ea46 261 pc.printf("\n\r DEBUG: Write Byte: %i\n\r", success);
CaptainR 20:88e137b9ea46 262 #endif
CaptainR 20:88e137b9ea46 263 return success;
CaptainR 20:88e137b9ea46 264 }
CaptainR 20:88e137b9ea46 265
CaptainR 20:88e137b9ea46 266 //**************************************************************************
CaptainR 20:88e137b9ea46 267 // After writing any data to a sector, the Flush Media command should be called
CaptainR 20:88e137b9ea46 268 // to ensure that the current sector that is being written is correctly stored
CaptainR 20:88e137b9ea46 269 // back to the media else write operations may be unpredictable.
CaptainR 20:88e137b9ea46 270 //**************************************************************************
CaptainR 20:88e137b9ea46 271 bool PICASO_4DGL :: media_Flush() {
CaptainR 20:88e137b9ea46 272
CaptainR 20:88e137b9ea46 273 char command[2] = "";
CaptainR 20:88e137b9ea46 274
CaptainR 20:88e137b9ea46 275 command[0] = (MEDIA_FLUSH >> (8*1)) & 0xff;
CaptainR 20:88e137b9ea46 276 command[1] = (MEDIA_FLUSH >> (8*0)) & 0xff;
CaptainR 20:88e137b9ea46 277
CaptainR 20:88e137b9ea46 278 writeCOMMAND(command, 2);
CaptainR 20:88e137b9ea46 279 bool success = writeByteResponse();
CaptainR 20:88e137b9ea46 280 #ifdef DEBUGMODE
CaptainR 20:88e137b9ea46 281 pc.printf("\n\r DEBUG: Read Word: %i\n\r", success);
CaptainR 20:88e137b9ea46 282 #endif
CaptainR 20:88e137b9ea46 283 return success;
CaptainR 20:88e137b9ea46 284 }
CaptainR 12:29f5ad896382 285
CaptainR 12:29f5ad896382 286
CaptainR 12:29f5ad896382 287
CaptainR 12:29f5ad896382 288
CaptainR 12:29f5ad896382 289
CaptainR 12:29f5ad896382 290
CaptainR 12:29f5ad896382 291
CaptainR 12:29f5ad896382 292
CaptainR 12:29f5ad896382 293
CaptainR 12:29f5ad896382 294
CaptainR 12:29f5ad896382 295
CaptainR 12:29f5ad896382 296
CaptainR 12:29f5ad896382 297
CaptainR 12:29f5ad896382 298
CaptainR 12:29f5ad896382 299
CaptainR 12:29f5ad896382 300
CaptainR 12:29f5ad896382 301
CaptainR 12:29f5ad896382 302
CaptainR 12:29f5ad896382 303
CaptainR 12:29f5ad896382 304
CaptainR 12:29f5ad896382 305
CaptainR 12:29f5ad896382 306
CaptainR 12:29f5ad896382 307
CaptainR 12:29f5ad896382 308
CaptainR 12:29f5ad896382 309
CaptainR 12:29f5ad896382 310
CaptainR 12:29f5ad896382 311
CaptainR 12:29f5ad896382 312
CaptainR 16:cb072eea16e9 313
CaptainR 19:a259bc128867 314
CaptainR 20:88e137b9ea46 315