Sonder Design Team / Mbed 2 deprecated BlackBoard_Firmware_Fast_read_not_test

Dependencies:   Memory25L16_fast USBDevice mbed

Fork of BlackBoard_Firmware_MVP by Sonder Design Team

Committer:
ThomasSonderDesign
Date:
Tue Feb 14 00:31:24 2017 +0000
Revision:
8:3577b060d7af
Parent:
7:b30c411a6d36
Child:
9:7c0f42f090e8
Same as before but with the name of each layout stored in its slot.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThomasSonderDesign 0:17d169ac117c 1 /******************************************************************************
ThomasSonderDesign 0:17d169ac117c 2 * S O N D E R
ThomasSonderDesign 0:17d169ac117c 3 * Copyright 2016 (C) Sonder Design Pty Ltd - All Rights Reserved
ThomasSonderDesign 0:17d169ac117c 4 * Unauthorised copying of this file, via any medium is strictly prohibited
ThomasSonderDesign 0:17d169ac117c 5 * Proprietary and confidential
ThomasSonderDesign 0:17d169ac117c 6 *****************************************************************************/
ThomasSonderDesign 0:17d169ac117c 7 /*
ThomasSonderDesign 0:17d169ac117c 8 This is the first implementation of a functional firmware. It allows the user to change the Display layout and type. Improvements for later
ThomasSonderDesign 0:17d169ac117c 9 versions will include usb keyboard support
ThomasSonderDesign 0:17d169ac117c 10 */
ThomasSonderDesign 0:17d169ac117c 11 #include "mbed.h"
ThomasSonderDesign 0:17d169ac117c 12 #include "USBHID.h"
ThomasSonderDesign 0:17d169ac117c 13 #include "Memory.h"
ThomasSonderDesign 0:17d169ac117c 14 #include "VKCodes.h"
ThomasSonderDesign 0:17d169ac117c 15
ThomasSonderDesign 0:17d169ac117c 16 /******************************************************************************
ThomasSonderDesign 0:17d169ac117c 17 * Definitiontions
ThomasSonderDesign 0:17d169ac117c 18 */
ThomasSonderDesign 0:17d169ac117c 19 //Constants for black board
ThomasSonderDesign 0:17d169ac117c 20
ThomasSonderDesign 0:17d169ac117c 21 DigitalOut cs_mem(P0_2); //Set up memory's Chip Select pin
ThomasSonderDesign 0:17d169ac117c 22 DigitalOut cs_epd(P1_23); //Set up epd's Chip Select pin
ThomasSonderDesign 0:17d169ac117c 23 DigitalOut TconEn(P0_5); //Tcon Enable pin, open drain must invert
ThomasSonderDesign 0:17d169ac117c 24 DigitalOut start(P0_14);
ThomasSonderDesign 7:b30c411a6d36 25 DigitalIn TCbusy(P0_4); //Tcon busy line
ThomasSonderDesign 0:17d169ac117c 26
ThomasSonderDesign 0:17d169ac117c 27 DigitalOut myled(P0_9);
ThomasSonderDesign 0:17d169ac117c 28 DigitalOut SRclk(P0_15); //Shift register clk signal
ThomasSonderDesign 0:17d169ac117c 29 DigitalOut SRlat (P1_22); //Shift register output latch
ThomasSonderDesign 0:17d169ac117c 30 DigitalOut SRds(P1_14); //Shiftreg data pin
ThomasSonderDesign 0:17d169ac117c 31
ThomasSonderDesign 0:17d169ac117c 32 DigitalIn E(P1_13); //Definne row row A input pin
ThomasSonderDesign 0:17d169ac117c 33 DigitalIn D(P0_7); //Definne row row B input pin
ThomasSonderDesign 0:17d169ac117c 34 DigitalIn C(P1_28); //Definne row row C input pin
ThomasSonderDesign 0:17d169ac117c 35 DigitalIn B(P1_31); //Definne row row D input pin
ThomasSonderDesign 0:17d169ac117c 36 DigitalIn A(P1_29); //Definne row row E input pin
ThomasSonderDesign 0:17d169ac117c 37
ThomasSonderDesign 0:17d169ac117c 38 DigitalIn modC(P0_11); //Definne row mod C input pin
ThomasSonderDesign 0:17d169ac117c 39 DigitalIn modB(P0_12); //Definne row mod B input pin
ThomasSonderDesign 0:17d169ac117c 40 DigitalIn modA(P0_13); //Definne row mod A input pin
ThomasSonderDesign 0:17d169ac117c 41
ThomasSonderDesign 0:17d169ac117c 42 Serial pc (P0_19,P0_18); //Setup a real serial port
ThomasSonderDesign 0:17d169ac117c 43 Memory mem(P0_2); //Create a new memory manager with chip select on P0_2
ThomasSonderDesign 0:17d169ac117c 44
ThomasSonderDesign 0:17d169ac117c 45 USBHID hid(64, 8,0x1234,0x3241); //Create a HID conection with and 64 byte input and 8 byte output report, PID0x1234 VID 3241
ThomasSonderDesign 0:17d169ac117c 46
ThomasSonderDesign 0:17d169ac117c 47 volatile int *PIN39IOREG = (int *)0x4004403C;
ThomasSonderDesign 0:17d169ac117c 48 int reportLength = 64; //The length of the report recieved
ThomasSonderDesign 0:17d169ac117c 49
ThomasSonderDesign 0:17d169ac117c 50 //Codes to send to the pc
ThomasSonderDesign 0:17d169ac117c 51 int readyAndWaiting []= {2,0x25,0,0,0,0,0,0};
ThomasSonderDesign 0:17d169ac117c 52 int busy []= {2,0x50,0,0,0,0,0,0};
ThomasSonderDesign 0:17d169ac117c 53 int readyForComand []= {2,0x75,0,0,0,0,0,0};
ThomasSonderDesign 0:17d169ac117c 54
ThomasSonderDesign 0:17d169ac117c 55
ThomasSonderDesign 0:17d169ac117c 56 int GlobalAddress = 0; //The last adress written to plus one
ThomasSonderDesign 0:17d169ac117c 57 int lastErasedBlock =0; //The Adress of the last erases block
ThomasSonderDesign 0:17d169ac117c 58 int currentName = 0; //The position in the nameList of the curret layout
ThomasSonderDesign 1:1ec8bcd31b27 59 char nameList[16][5]; //A list of all the Layouts in memory, each name is 5 bytes long
ThomasSonderDesign 1:1ec8bcd31b27 60
ThomasSonderDesign 1:1ec8bcd31b27 61 //Locations in memory were the 16 layouts can be stored
ThomasSonderDesign 1:1ec8bcd31b27 62 const int slots [] = {0,0x20000, 0x40000,0x60000, 0x80000, 0xa0000, 0xc0000, 0xe0000, 0x100000, 0x120000,0x140000,0x160000,0x180000, 0x1a0000,0x1e0000};
ThomasSonderDesign 4:e6e1642724d4 63 bool erasedSlots[]= {true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
ThomasSonderDesign 0:17d169ac117c 64
ThomasSonderDesign 0:17d169ac117c 65 int keybuffer[10][2]; //A buffer for storing key presses (mod key plus key)
ThomasSonderDesign 0:17d169ac117c 66
ThomasSonderDesign 0:17d169ac117c 67 int scanCode [5][14]= { //This 2D array maps key scan codes to letter codes
ThomasSonderDesign 0:17d169ac117c 68 {VK_OEM_3, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, VK_OEM_MINUS, VK_OEM_PLUS, VK_BACK},
ThomasSonderDesign 0:17d169ac117c 69 {VK_TAB, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P},
ThomasSonderDesign 0:17d169ac117c 70 {KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, VK_OEM_1},
ThomasSonderDesign 0:17d169ac117c 71 {KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, VK_LSHIFT,VK_CONTROL,VK_LMENU},
ThomasSonderDesign 0:17d169ac117c 72 {VK_F1, VK_F2,VK_F3,VK_F4, VK_SPACE, VK_TAB, VK_BACK, VK_RSHIFT,VK_LWIN,VK_RMENU}
ThomasSonderDesign 0:17d169ac117c 73 };
ThomasSonderDesign 0:17d169ac117c 74
ThomasSonderDesign 0:17d169ac117c 75
ThomasSonderDesign 0:17d169ac117c 76 /******************************************************************************
ThomasSonderDesign 0:17d169ac117c 77 * Set up Functions
ThomasSonderDesign 0:17d169ac117c 78 */
ThomasSonderDesign 0:17d169ac117c 79 char check(int depth) //Check availible memory
ThomasSonderDesign 0:17d169ac117c 80 {
ThomasSonderDesign 0:17d169ac117c 81 char c;
ThomasSonderDesign 0:17d169ac117c 82 char *ptr = new char;
ThomasSonderDesign 0:17d169ac117c 83 //pc.printf("stack at %p, heap at %p\n", &c, ptr);
ThomasSonderDesign 0:17d169ac117c 84 //pc.printf("sh,%i,%i\n", &c, ptr);
ThomasSonderDesign 0:17d169ac117c 85 pc.printf("mem size = %p\n", &c-ptr);
ThomasSonderDesign 0:17d169ac117c 86 //if (depth <= 0) return;
ThomasSonderDesign 0:17d169ac117c 87 // check(depth-1);
ThomasSonderDesign 0:17d169ac117c 88 free(ptr);
ThomasSonderDesign 0:17d169ac117c 89 return 1;
ThomasSonderDesign 0:17d169ac117c 90 }
ThomasSonderDesign 0:17d169ac117c 91
ThomasSonderDesign 0:17d169ac117c 92 /**
ThomasSonderDesign 0:17d169ac117c 93 * Set up the memory SPI
ThomasSonderDesign 0:17d169ac117c 94 */
ThomasSonderDesign 0:17d169ac117c 95 SPI setupSPI()
ThomasSonderDesign 0:17d169ac117c 96 {
ThomasSonderDesign 0:17d169ac117c 97 SPI my_spi(P0_9,P0_8,P0_10); // mosi, miso, sclk
ThomasSonderDesign 0:17d169ac117c 98 //cs_mem = 1; // Chip must be deselected
ThomasSonderDesign 0:17d169ac117c 99 my_spi.format(8,3); // Setup the spi for 8 bit data, low steady state clock,
ThomasSonderDesign 1:1ec8bcd31b27 100 my_spi.frequency(1000000); // second edge capture, with a 1MHz clock rate, Will work up to 20MHz
ThomasSonderDesign 0:17d169ac117c 101 return my_spi;
ThomasSonderDesign 0:17d169ac117c 102 }
ThomasSonderDesign 0:17d169ac117c 103
ThomasSonderDesign 0:17d169ac117c 104
ThomasSonderDesign 0:17d169ac117c 105 /**
ThomasSonderDesign 0:17d169ac117c 106 * Setup USB HID, usefull for later not used in this iterarion
ThomasSonderDesign 0:17d169ac117c 107 */
ThomasSonderDesign 0:17d169ac117c 108 /*USBHID setupUSB()
ThomasSonderDesign 0:17d169ac117c 109 {
ThomasSonderDesign 0:17d169ac117c 110 USBHID hid(64, 8,0x1234,0x3241); //Create a HID conection with and 64 byte input and 8 byte output report, PID0x1234 VID 3241
ThomasSonderDesign 0:17d169ac117c 111 return hid;
ThomasSonderDesign 0:17d169ac117c 112 }*/
ThomasSonderDesign 0:17d169ac117c 113
ThomasSonderDesign 0:17d169ac117c 114
ThomasSonderDesign 0:17d169ac117c 115 /******************************************************************************
ThomasSonderDesign 0:17d169ac117c 116 * USB Functions
ThomasSonderDesign 0:17d169ac117c 117 */
ThomasSonderDesign 0:17d169ac117c 118
ThomasSonderDesign 0:17d169ac117c 119 /**
ThomasSonderDesign 0:17d169ac117c 120 * checks the usb recive buffer, if it contains data it is coppired to USBDataBuffer and returns 1
ThomasSonderDesign 0:17d169ac117c 121 * if the empty return -1;
ThomasSonderDesign 0:17d169ac117c 122 */
ThomasSonderDesign 0:17d169ac117c 123 int readUSB(int USBDataBuffer[])
ThomasSonderDesign 0:17d169ac117c 124 {
ThomasSonderDesign 0:17d169ac117c 125 HID_REPORT recv_report;
ThomasSonderDesign 0:17d169ac117c 126 if(hid.readNB(&recv_report)) { //Check if there is a received report
ThomasSonderDesign 0:17d169ac117c 127 //printf("\n");
ThomasSonderDesign 0:17d169ac117c 128 for(int i = 0; i<recv_report.length; i++) { //Store the report in a locat array
ThomasSonderDesign 0:17d169ac117c 129 //printf(" %x", recv_report.data[i]);
ThomasSonderDesign 0:17d169ac117c 130 USBDataBuffer[i] = recv_report.data[i];
ThomasSonderDesign 0:17d169ac117c 131 }
ThomasSonderDesign 0:17d169ac117c 132 return 1;
ThomasSonderDesign 0:17d169ac117c 133 } else {
ThomasSonderDesign 0:17d169ac117c 134 return -1;
ThomasSonderDesign 0:17d169ac117c 135 }
ThomasSonderDesign 0:17d169ac117c 136 }
ThomasSonderDesign 0:17d169ac117c 137
ThomasSonderDesign 0:17d169ac117c 138
ThomasSonderDesign 0:17d169ac117c 139 /**
ThomasSonderDesign 0:17d169ac117c 140 * usbSend, send an eight byte report over usb
ThomasSonderDesign 0:17d169ac117c 141 */
ThomasSonderDesign 0:17d169ac117c 142 void sendUSB(int usbReport[])
ThomasSonderDesign 0:17d169ac117c 143 {
ThomasSonderDesign 0:17d169ac117c 144 //pc.printf("\nS: %x",usbReport[1]);
ThomasSonderDesign 0:17d169ac117c 145 HID_REPORT send_report;
ThomasSonderDesign 0:17d169ac117c 146 send_report.length = 8;
ThomasSonderDesign 0:17d169ac117c 147
ThomasSonderDesign 0:17d169ac117c 148 send_report.data[0] = usbReport[0];
ThomasSonderDesign 0:17d169ac117c 149 send_report.data[1] = usbReport[1]; //comand byte
ThomasSonderDesign 0:17d169ac117c 150 send_report.data[2] = usbReport[2];
ThomasSonderDesign 0:17d169ac117c 151 send_report.data[3] = usbReport[3];
ThomasSonderDesign 0:17d169ac117c 152 send_report.data[4] = usbReport[4];
ThomasSonderDesign 0:17d169ac117c 153 send_report.data[5] = usbReport[5];
ThomasSonderDesign 0:17d169ac117c 154 send_report.data[6] = usbReport[6];
ThomasSonderDesign 0:17d169ac117c 155 send_report.data[7] = usbReport[7];
ThomasSonderDesign 0:17d169ac117c 156 hid.send(&send_report);
ThomasSonderDesign 0:17d169ac117c 157 }
ThomasSonderDesign 0:17d169ac117c 158
ThomasSonderDesign 0:17d169ac117c 159
ThomasSonderDesign 0:17d169ac117c 160
ThomasSonderDesign 7:b30c411a6d36 161
ThomasSonderDesign 0:17d169ac117c 162 /******************************************************************************
ThomasSonderDesign 0:17d169ac117c 163 * Keyboard Functions
ThomasSonderDesign 0:17d169ac117c 164 */
ThomasSonderDesign 0:17d169ac117c 165
ThomasSonderDesign 0:17d169ac117c 166 /**
ThomasSonderDesign 0:17d169ac117c 167 * keyScan(int keyBuffer[][]). Sacns the keyboard matrix and stores any keypresses in the 2D keyBuffer
ThomasSonderDesign 0:17d169ac117c 168 * If a key press was detected it returns the most recent key press, otherwise it returns 0
ThomasSonderDesign 0:17d169ac117c 169 */
ThomasSonderDesign 0:17d169ac117c 170 int keyScan()
ThomasSonderDesign 0:17d169ac117c 171 {
ThomasSonderDesign 0:17d169ac117c 172 //Initialise and set Up code
ThomasSonderDesign 0:17d169ac117c 173 int counter=0; //Keubuffer index counter
ThomasSonderDesign 0:17d169ac117c 174 int rowNum = 0;
ThomasSonderDesign 0:17d169ac117c 175 int code =0; //the return value, the last key pushed
ThomasSonderDesign 0:17d169ac117c 176
ThomasSonderDesign 0:17d169ac117c 177 /*Clear the keybuffer*/
ThomasSonderDesign 0:17d169ac117c 178 for(int i = 0; i<10; i++) {
ThomasSonderDesign 0:17d169ac117c 179 keybuffer[i][0]=0;
ThomasSonderDesign 0:17d169ac117c 180 keybuffer[i][1]=0;
ThomasSonderDesign 0:17d169ac117c 181 }
ThomasSonderDesign 0:17d169ac117c 182
ThomasSonderDesign 0:17d169ac117c 183 int noCols = 16; //number of bits to be shifted
ThomasSonderDesign 0:17d169ac117c 184 int t = 1000; //wait time in uS. This is the period in us of the shift register clock
ThomasSonderDesign 0:17d169ac117c 185 SRclk=0;
ThomasSonderDesign 0:17d169ac117c 186 SRlat = 0;
ThomasSonderDesign 0:17d169ac117c 187 SRds =1; //Set first bit high
ThomasSonderDesign 0:17d169ac117c 188
ThomasSonderDesign 0:17d169ac117c 189
ThomasSonderDesign 0:17d169ac117c 190 for (int col = 0; col<noCols; col++) { //Loop through one shifit reg
ThomasSonderDesign 0:17d169ac117c 191 SRclk = 1; //clock in current bit // __ __
ThomasSonderDesign 0:17d169ac117c 192 wait_us(t/2); //SRclk__| |_..._| |_
ThomasSonderDesign 0:17d169ac117c 193 SRds = 0; //set dext bit low // ___
ThomasSonderDesign 0:17d169ac117c 194 wait_us(t/2); //SRds | |___..._____
ThomasSonderDesign 0:17d169ac117c 195 SRclk=0; // _ _
ThomasSonderDesign 0:17d169ac117c 196 SRlat = 1; //latch all data out //SRlat____| |_...___| |_
ThomasSonderDesign 0:17d169ac117c 197 wait_us(t/2); //
ThomasSonderDesign 0:17d169ac117c 198 SRlat=0;
ThomasSonderDesign 0:17d169ac117c 199
ThomasSonderDesign 0:17d169ac117c 200 /*
ThomasSonderDesign 0:17d169ac117c 201 Check if a button has been pressed by scaning the rows
ThomasSonderDesign 0:17d169ac117c 202 Generates a unique code depending on which button has been pressed
ThomasSonderDesign 0:17d169ac117c 203 The code is looked up in the scanCode Array
ThomasSonderDesign 0:17d169ac117c 204 */
ThomasSonderDesign 0:17d169ac117c 205 if(A>0) {
ThomasSonderDesign 0:17d169ac117c 206 //pc.printf("A\n");
ThomasSonderDesign 0:17d169ac117c 207 rowNum = 0;
ThomasSonderDesign 0:17d169ac117c 208 code = scanCode[rowNum][col];
ThomasSonderDesign 0:17d169ac117c 209 keybuffer[counter][1]= code;
ThomasSonderDesign 0:17d169ac117c 210 counter++;
ThomasSonderDesign 0:17d169ac117c 211 }
ThomasSonderDesign 0:17d169ac117c 212 if(B>0) {
ThomasSonderDesign 0:17d169ac117c 213 //pc.printf("B\n");
ThomasSonderDesign 0:17d169ac117c 214 rowNum = 1;
ThomasSonderDesign 0:17d169ac117c 215 code = scanCode[rowNum][col];
ThomasSonderDesign 0:17d169ac117c 216 keybuffer[counter][1]= code;
ThomasSonderDesign 0:17d169ac117c 217 counter++;
ThomasSonderDesign 0:17d169ac117c 218 }
ThomasSonderDesign 0:17d169ac117c 219 if(C>0) {
ThomasSonderDesign 0:17d169ac117c 220 //pc.printf("C\n");
ThomasSonderDesign 0:17d169ac117c 221 rowNum = 2;
ThomasSonderDesign 0:17d169ac117c 222 code = scanCode[rowNum][col];
ThomasSonderDesign 0:17d169ac117c 223 keybuffer[counter][1]= code;
ThomasSonderDesign 0:17d169ac117c 224 counter++;
ThomasSonderDesign 0:17d169ac117c 225 }
ThomasSonderDesign 0:17d169ac117c 226 if(D>0) {
ThomasSonderDesign 0:17d169ac117c 227 //pc.printf("D\n");
ThomasSonderDesign 0:17d169ac117c 228 rowNum = 3;
ThomasSonderDesign 0:17d169ac117c 229 code = scanCode[rowNum][col];
ThomasSonderDesign 0:17d169ac117c 230 keybuffer[counter][1]= code;
ThomasSonderDesign 0:17d169ac117c 231 counter++;
ThomasSonderDesign 0:17d169ac117c 232 }
ThomasSonderDesign 0:17d169ac117c 233 if(E>0) {
ThomasSonderDesign 0:17d169ac117c 234 //pc.printf("E\n");
ThomasSonderDesign 0:17d169ac117c 235 rowNum = 4;
ThomasSonderDesign 0:17d169ac117c 236 code = scanCode[rowNum][col];
ThomasSonderDesign 0:17d169ac117c 237 keybuffer[counter][1]= code;
ThomasSonderDesign 0:17d169ac117c 238 counter++;
ThomasSonderDesign 0:17d169ac117c 239 }
ThomasSonderDesign 0:17d169ac117c 240
ThomasSonderDesign 0:17d169ac117c 241 /*Scan the mod keys and assign them to the mod column of key buffer in the from zero up*/
ThomasSonderDesign 0:17d169ac117c 242 if(col>0) {
ThomasSonderDesign 0:17d169ac117c 243 counter=0;
ThomasSonderDesign 0:17d169ac117c 244
ThomasSonderDesign 0:17d169ac117c 245 if(modC>0) {
ThomasSonderDesign 0:17d169ac117c 246 rowNum = 0;
ThomasSonderDesign 0:17d169ac117c 247 code = scanCode[rowNum][col];
ThomasSonderDesign 0:17d169ac117c 248 keybuffer[counter][0]= code;
ThomasSonderDesign 0:17d169ac117c 249 }
ThomasSonderDesign 0:17d169ac117c 250 if(modB>0) {
ThomasSonderDesign 0:17d169ac117c 251 rowNum = 0;
ThomasSonderDesign 0:17d169ac117c 252 code = scanCode[rowNum][col];
ThomasSonderDesign 0:17d169ac117c 253 keybuffer[counter][0]= code;
ThomasSonderDesign 0:17d169ac117c 254 }
ThomasSonderDesign 0:17d169ac117c 255 if(modA>0) {
ThomasSonderDesign 0:17d169ac117c 256 rowNum = 0;
ThomasSonderDesign 0:17d169ac117c 257 code = scanCode[rowNum][col];
ThomasSonderDesign 0:17d169ac117c 258 keybuffer[counter][0]= code;
ThomasSonderDesign 0:17d169ac117c 259 }
ThomasSonderDesign 0:17d169ac117c 260 }
ThomasSonderDesign 0:17d169ac117c 261 }
ThomasSonderDesign 0:17d169ac117c 262 return code;
ThomasSonderDesign 0:17d169ac117c 263 }
ThomasSonderDesign 0:17d169ac117c 264
ThomasSonderDesign 0:17d169ac117c 265
ThomasSonderDesign 0:17d169ac117c 266 /**
ThomasSonderDesign 0:17d169ac117c 267 * int sendKey(int mod, int key). Sends a keypress to the pc over usb
ThomasSonderDesign 0:17d169ac117c 268 */
ThomasSonderDesign 0:17d169ac117c 269 void sendKey(int mod, int key)
ThomasSonderDesign 0:17d169ac117c 270 {
ThomasSonderDesign 0:17d169ac117c 271 int temp[]= {0x01,mod,key,0,0,0,0,0};
ThomasSonderDesign 0:17d169ac117c 272 sendUSB(temp);
ThomasSonderDesign 0:17d169ac117c 273 }
ThomasSonderDesign 0:17d169ac117c 274
ThomasSonderDesign 7:b30c411a6d36 275 /**
ThomasSonderDesign 7:b30c411a6d36 276 * keyCheck(), scanss the keys and sends the keypress to the pc over usb
ThomasSonderDesign 7:b30c411a6d36 277 */
ThomasSonderDesign 7:b30c411a6d36 278 void keyCheck(){
ThomasSonderDesign 8:3577b060d7af 279 //pc.printf("KeyCheck\n");
ThomasSonderDesign 7:b30c411a6d36 280 if (keyScan()>0) { //check if keyScan returned key presss
ThomasSonderDesign 7:b30c411a6d36 281 pc.printf("\nKey scan|n");
ThomasSonderDesign 7:b30c411a6d36 282 int countpos =0;
ThomasSonderDesign 7:b30c411a6d36 283 while(keybuffer[countpos][1]>0) { //While there are keypresses in the buffer
ThomasSonderDesign 7:b30c411a6d36 284 pc.printf("%i",keybuffer[countpos][1]);
ThomasSonderDesign 7:b30c411a6d36 285 pc.printf(" ");
ThomasSonderDesign 7:b30c411a6d36 286 sendKey(keybuffer[countpos][0],keybuffer[countpos][1]);//Send key press
ThomasSonderDesign 7:b30c411a6d36 287 countpos++;
ThomasSonderDesign 7:b30c411a6d36 288 myled=0;
ThomasSonderDesign 7:b30c411a6d36 289 }
ThomasSonderDesign 7:b30c411a6d36 290 }
ThomasSonderDesign 7:b30c411a6d36 291 }
ThomasSonderDesign 0:17d169ac117c 292
ThomasSonderDesign 0:17d169ac117c 293 /******************************************************************************
ThomasSonderDesign 1:1ec8bcd31b27 294 * Memory control funtions
ThomasSonderDesign 0:17d169ac117c 295 */
ThomasSonderDesign 0:17d169ac117c 296
ThomasSonderDesign 0:17d169ac117c 297
ThomasSonderDesign 0:17d169ac117c 298 /**
ThomasSonderDesign 0:17d169ac117c 299 * Writes the contense of USBDataBuffer to memoryBuffer, starting at bufferIndex
ThomasSonderDesign 0:17d169ac117c 300 * returns the index of the last written byte+1;
ThomasSonderDesign 0:17d169ac117c 301 */
ThomasSonderDesign 0:17d169ac117c 302 int appendBuffer(int USBDataBuffer[], char memoryBuffer[], int bufferIndex)
ThomasSonderDesign 0:17d169ac117c 303 {
ThomasSonderDesign 5:07113abf18c0 304 //printf("\n");
ThomasSonderDesign 0:17d169ac117c 305 int posIndex=0;
ThomasSonderDesign 0:17d169ac117c 306 for(int i = 0; i < 64; i++) {
ThomasSonderDesign 5:07113abf18c0 307
ThomasSonderDesign 0:17d169ac117c 308 memoryBuffer[bufferIndex+i]=USBDataBuffer[i];
ThomasSonderDesign 0:17d169ac117c 309 posIndex++;
ThomasSonderDesign 0:17d169ac117c 310 }
ThomasSonderDesign 0:17d169ac117c 311 return (bufferIndex+posIndex);
ThomasSonderDesign 0:17d169ac117c 312 }
ThomasSonderDesign 0:17d169ac117c 313
ThomasSonderDesign 0:17d169ac117c 314 /**
ThomasSonderDesign 0:17d169ac117c 315 *Sends the ready and waiting signall and loops untill data is recieved.
ThomasSonderDesign 0:17d169ac117c 316 *Recieved data is stored in the USBDataBuffer
ThomasSonderDesign 0:17d169ac117c 317 */
ThomasSonderDesign 0:17d169ac117c 318 void waitForData(int USBDataBuffer[])
ThomasSonderDesign 0:17d169ac117c 319 {
ThomasSonderDesign 0:17d169ac117c 320 sendUSB(readyAndWaiting);
ThomasSonderDesign 0:17d169ac117c 321 while (readUSB(USBDataBuffer)<1) {
ThomasSonderDesign 0:17d169ac117c 322 sendUSB(readyAndWaiting);
ThomasSonderDesign 0:17d169ac117c 323 }
ThomasSonderDesign 0:17d169ac117c 324 }
ThomasSonderDesign 0:17d169ac117c 325
ThomasSonderDesign 4:e6e1642724d4 326
ThomasSonderDesign 4:e6e1642724d4 327 /**
ThomasSonderDesign 4:e6e1642724d4 328 * receives 64 packets over usb and stores them in memooryBuffer. When memory Buffer is full it is
ThomasSonderDesign 4:e6e1642724d4 329 * written to memory at address. This is repeated 336 times to write a whole image to memory.
ThomasSonderDesign 4:e6e1642724d4 330 * returns the next availible adsress;
ThomasSonderDesign 4:e6e1642724d4 331 */
ThomasSonderDesign 4:e6e1642724d4 332 int writeImage(SPI my_spi,int Address)
ThomasSonderDesign 4:e6e1642724d4 333 {
ThomasSonderDesign 4:e6e1642724d4 334 Timer t;
ThomasSonderDesign 4:e6e1642724d4 335 t.start(); //Start the timer
ThomasSonderDesign 5:07113abf18c0 336 pc.printf("\nAddress: %i\n", Address);
ThomasSonderDesign 4:e6e1642724d4 337 int USBDataBuffer [64]; //Creat a buffer for recived data
ThomasSonderDesign 4:e6e1642724d4 338 char memoryBuffer [bufferSize]; //Create a memory buffer, to be sent to flash
ThomasSonderDesign 4:e6e1642724d4 339 int bufferIndex = 0; //points to the next available possition in memory
ThomasSonderDesign 4:e6e1642724d4 340 int startAddress = Address;
ThomasSonderDesign 4:e6e1642724d4 341 while (Address<startAddress+86272) {
ThomasSonderDesign 4:e6e1642724d4 342 //waitForData(USBDataBuffer); //Waits untill data is recieved on usb, puts it in USBDataBuffer
ThomasSonderDesign 4:e6e1642724d4 343 while(bufferIndex<bufferSize) {
ThomasSonderDesign 4:e6e1642724d4 344 waitForData(USBDataBuffer);
ThomasSonderDesign 4:e6e1642724d4 345 bufferIndex=appendBuffer(USBDataBuffer, memoryBuffer, bufferIndex); //Appends USBDataBuffer onto memoryBuffer
ThomasSonderDesign 4:e6e1642724d4 346 }
ThomasSonderDesign 4:e6e1642724d4 347 bufferIndex=0;
ThomasSonderDesign 4:e6e1642724d4 348
ThomasSonderDesign 4:e6e1642724d4 349 //Checks if the block has been erased. only virgin bytes can be written to, so if a block has not been errased it is write
ThomasSonderDesign 4:e6e1642724d4 350 //protected. this erases a block before writung to it if nessisary.
ThomasSonderDesign 4:e6e1642724d4 351 int currentSlot = Address/0x20000;
ThomasSonderDesign 4:e6e1642724d4 352 if (erasedSlots[currentSlot]) {
ThomasSonderDesign 4:e6e1642724d4 353 Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
ThomasSonderDesign 4:e6e1642724d4 354 } else {
ThomasSonderDesign 4:e6e1642724d4 355 pc.printf("\nCan not write to unerased slot.");
ThomasSonderDesign 5:07113abf18c0 356 Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
ThomasSonderDesign 4:e6e1642724d4 357 }
ThomasSonderDesign 4:e6e1642724d4 358 }
ThomasSonderDesign 4:e6e1642724d4 359 pc.printf("\n pinnished writing");
ThomasSonderDesign 4:e6e1642724d4 360 t.stop();
ThomasSonderDesign 4:e6e1642724d4 361 pc.printf("\nwrite image %i", t.read_ms());
ThomasSonderDesign 4:e6e1642724d4 362 t.reset();
ThomasSonderDesign 4:e6e1642724d4 363 return Address;
ThomasSonderDesign 4:e6e1642724d4 364 }
ThomasSonderDesign 4:e6e1642724d4 365
ThomasSonderDesign 0:17d169ac117c 366 /**
ThomasSonderDesign 5:07113abf18c0 367 * Writes a single memorybuffer to memory
ThomasSonderDesign 0:17d169ac117c 368 * receives 64 packets over usb and stores them in memooryBuffer. When memory Buffer is full it is
ThomasSonderDesign 0:17d169ac117c 369 * written to memory at address.
ThomasSonderDesign 0:17d169ac117c 370 * returns the next availible adsress;
ThomasSonderDesign 0:17d169ac117c 371 */
ThomasSonderDesign 0:17d169ac117c 372 int writeFrame(SPI my_spi,int Address)
ThomasSonderDesign 0:17d169ac117c 373 {
ThomasSonderDesign 0:17d169ac117c 374 int USBDataBuffer [64]; //Creat a buffer for recived data
ThomasSonderDesign 0:17d169ac117c 375 char memoryBuffer [bufferSize]; //Create a memory buffer, to be sent to flash
ThomasSonderDesign 0:17d169ac117c 376 int bufferIndex = 0; //points to the next available possition in memory
ThomasSonderDesign 3:d4e1892846fb 377 Timer t;
ThomasSonderDesign 3:d4e1892846fb 378 t.start(); //Start the timer
ThomasSonderDesign 0:17d169ac117c 379
ThomasSonderDesign 0:17d169ac117c 380 waitForData(USBDataBuffer); //Waits untill data is recieved on usb, puts it in USBDataBuffer
ThomasSonderDesign 3:d4e1892846fb 381 t.stop();
ThomasSonderDesign 4:e6e1642724d4 382
ThomasSonderDesign 0:17d169ac117c 383 while(bufferIndex<bufferSize) {
ThomasSonderDesign 0:17d169ac117c 384 bufferIndex=appendBuffer(USBDataBuffer, memoryBuffer, bufferIndex); //Appends USBDataBuffer onto memoryBuffer
ThomasSonderDesign 3:d4e1892846fb 385 t.start();
ThomasSonderDesign 0:17d169ac117c 386 waitForData(USBDataBuffer);
ThomasSonderDesign 3:d4e1892846fb 387 t.stop();
ThomasSonderDesign 0:17d169ac117c 388 }
ThomasSonderDesign 0:17d169ac117c 389
ThomasSonderDesign 0:17d169ac117c 390 //Checks if the block has been erased. only virgin bytes can be written to, so if a block has not been errased it is write
ThomasSonderDesign 0:17d169ac117c 391 //protected. this erases a block before writung to it if nessisary.
ThomasSonderDesign 2:8b66a3f7e202 392 int currentSlot = Address/0x20000;
ThomasSonderDesign 2:8b66a3f7e202 393 pc.printf("\nCurrent slot: %i", currentSlot);
ThomasSonderDesign 2:8b66a3f7e202 394 if (erasedSlots[currentSlot]) {
ThomasSonderDesign 1:1ec8bcd31b27 395 pc.printf("\nNE");
ThomasSonderDesign 0:17d169ac117c 396 Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
ThomasSonderDesign 0:17d169ac117c 397 } else {
ThomasSonderDesign 2:8b66a3f7e202 398 pc.printf("\nCan not write to unerased slot.");
ThomasSonderDesign 4:e6e1642724d4 399 }
ThomasSonderDesign 3:d4e1892846fb 400 //t.stop();
ThomasSonderDesign 3:d4e1892846fb 401 pc.printf("\nWait1 Wate2-4 %i", t.read_ms());
ThomasSonderDesign 3:d4e1892846fb 402 t.reset();
ThomasSonderDesign 0:17d169ac117c 403 return Address;
ThomasSonderDesign 0:17d169ac117c 404 }
ThomasSonderDesign 0:17d169ac117c 405
ThomasSonderDesign 1:1ec8bcd31b27 406
ThomasSonderDesign 1:1ec8bcd31b27 407 /**
ThomasSonderDesign 1:1ec8bcd31b27 408 * Sends a report containing a the name of the layout stored at a given slot
ThomasSonderDesign 1:1ec8bcd31b27 409 */
ThomasSonderDesign 2:8b66a3f7e202 410 void getLayoutName(int slot)
ThomasSonderDesign 1:1ec8bcd31b27 411 {
ThomasSonderDesign 1:1ec8bcd31b27 412 int temp [] = {0x31,slot,nameList[slot][0],nameList[slot][1],nameList[slot][2],nameList[slot][3],nameList[slot][4],0};
ThomasSonderDesign 1:1ec8bcd31b27 413 sendUSB(temp);
ThomasSonderDesign 8:3577b060d7af 414 pc.printf("\nSlot %d = %c%c%c%c%c",slot, nameList[slot][0],nameList[slot][1],nameList[slot][2],nameList[slot][3],nameList[slot][4]);
ThomasSonderDesign 1:1ec8bcd31b27 415 }
ThomasSonderDesign 1:1ec8bcd31b27 416
ThomasSonderDesign 1:1ec8bcd31b27 417
ThomasSonderDesign 1:1ec8bcd31b27 418 /**
ThomasSonderDesign 1:1ec8bcd31b27 419 * Sends three reports containing the list of layout names stored in memory.
ThomasSonderDesign 1:1ec8bcd31b27 420 */
ThomasSonderDesign 1:1ec8bcd31b27 421 void getNameList()
ThomasSonderDesign 1:1ec8bcd31b27 422 {
ThomasSonderDesign 1:1ec8bcd31b27 423 for(int slot =0; slot< 16; slot++) {
ThomasSonderDesign 2:8b66a3f7e202 424 getLayoutName(slot);
ThomasSonderDesign 8:3577b060d7af 425 }
ThomasSonderDesign 1:1ec8bcd31b27 426 }
ThomasSonderDesign 0:17d169ac117c 427
ThomasSonderDesign 0:17d169ac117c 428 /**
ThomasSonderDesign 0:17d169ac117c 429 * Sends the name of the Current layout
ThomasSonderDesign 0:17d169ac117c 430 */
ThomasSonderDesign 1:1ec8bcd31b27 431 void getCurrentLayout()
ThomasSonderDesign 0:17d169ac117c 432 {
ThomasSonderDesign 2:8b66a3f7e202 433 getLayoutName(currentName);
ThomasSonderDesign 0:17d169ac117c 434 }
ThomasSonderDesign 0:17d169ac117c 435
ThomasSonderDesign 0:17d169ac117c 436 /**
ThomasSonderDesign 0:17d169ac117c 437 * Writes the name of a given layout to the top memory address in its reserved block
ThomasSonderDesign 1:1ec8bcd31b27 438 * name[] is a 5 bytes char array with the most significant byte at name[0]
ThomasSonderDesign 0:17d169ac117c 439 */
ThomasSonderDesign 2:8b66a3f7e202 440 void nameBlock(SPI my_spi, char name[], int slot)
ThomasSonderDesign 0:17d169ac117c 441 {
ThomasSonderDesign 1:1ec8bcd31b27 442 //char temp[]= {name};
ThomasSonderDesign 8:3577b060d7af 443 mem.writeData(my_spi, name, slots[slot]+0x1ff00, 5);
ThomasSonderDesign 2:8b66a3f7e202 444 }
ThomasSonderDesign 2:8b66a3f7e202 445
ThomasSonderDesign 2:8b66a3f7e202 446 /**
ThomasSonderDesign 2:8b66a3f7e202 447 * Reads memory to find the names of the layouts stored. puts the names into nameList
ThomasSonderDesign 2:8b66a3f7e202 448 */
ThomasSonderDesign 2:8b66a3f7e202 449 void populateNameList(SPI my_spi)
ThomasSonderDesign 2:8b66a3f7e202 450 {
ThomasSonderDesign 4:e6e1642724d4 451 for(int slot=0; slot<16; slot++) {
ThomasSonderDesign 4:e6e1642724d4 452 char name[5];
ThomasSonderDesign 8:3577b060d7af 453 mem.readData(my_spi, name, slots[slot]+0x1FF00, 5); //Read five bytes from the end of a slot into the name array
ThomasSonderDesign 4:e6e1642724d4 454 for( int i = 0; i<5; i++) {
ThomasSonderDesign 4:e6e1642724d4 455 nameList[slot][i]=name[i];
ThomasSonderDesign 4:e6e1642724d4 456 }
ThomasSonderDesign 4:e6e1642724d4 457 }
ThomasSonderDesign 2:8b66a3f7e202 458 }
ThomasSonderDesign 2:8b66a3f7e202 459
ThomasSonderDesign 2:8b66a3f7e202 460 /**
ThomasSonderDesign 2:8b66a3f7e202 461 * Prepares the memory for a new Layout, writes the layout's name, and erases the blocks
ThomasSonderDesign 2:8b66a3f7e202 462 */
ThomasSonderDesign 4:e6e1642724d4 463 void prepImage(SPI my_spi, int slot, char name[])
ThomasSonderDesign 2:8b66a3f7e202 464 {
ThomasSonderDesign 5:07113abf18c0 465 pc.printf("\nSlot: %i", slots[slot]);
ThomasSonderDesign 4:e6e1642724d4 466 mem.blockErase(my_spi, slots[slot]); //erase the bottom block of the slot
ThomasSonderDesign 4:e6e1642724d4 467 mem.blockErase(my_spi, slots[slot]+0x10000); //erase the top block of the slot
ThomasSonderDesign 8:3577b060d7af 468 pc.printf("\nWorking");
ThomasSonderDesign 7:b30c411a6d36 469 nameBlock(my_spi, name, slots[slot]/*+0x1FFF9*/); //Write the name of the layout to memory
ThomasSonderDesign 4:e6e1642724d4 470 erasedSlots[slot]=true; //Mark the erased slot as true
ThomasSonderDesign 8:3577b060d7af 471 populateNameList(my_spi);
ThomasSonderDesign 0:17d169ac117c 472 }
ThomasSonderDesign 0:17d169ac117c 473
ThomasSonderDesign 1:1ec8bcd31b27 474
ThomasSonderDesign 1:1ec8bcd31b27 475 /******************************************************************************
ThomasSonderDesign 1:1ec8bcd31b27 476 * Display control funtions
ThomasSonderDesign 1:1ec8bcd31b27 477 */
ThomasSonderDesign 1:1ec8bcd31b27 478
ThomasSonderDesign 0:17d169ac117c 479 /*###### EPD Set Up ######*/
ThomasSonderDesign 0:17d169ac117c 480 //Sets up the EPD spi pins
ThomasSonderDesign 0:17d169ac117c 481 SPI setupEPD()
ThomasSonderDesign 0:17d169ac117c 482 {
ThomasSonderDesign 0:17d169ac117c 483 pc.printf("\nEpd setup");
ThomasSonderDesign 0:17d169ac117c 484 /////Setup the spi link with the EDP////
ThomasSonderDesign 0:17d169ac117c 485 SPI epd_spi(P0_21, P1_21, P1_20); //SPI setup: mosi, miso, sclk
ThomasSonderDesign 0:17d169ac117c 486 // Setup the spi for 8 bit data, high steady state clock,
ThomasSonderDesign 0:17d169ac117c 487 // second edge capture, with a 5MHz clock rate
ThomasSonderDesign 0:17d169ac117c 488 epd_spi.format(8,3);
ThomasSonderDesign 0:17d169ac117c 489 epd_spi.frequency(5000000);
ThomasSonderDesign 0:17d169ac117c 490 return epd_spi;
ThomasSonderDesign 0:17d169ac117c 491 }
ThomasSonderDesign 0:17d169ac117c 492
ThomasSonderDesign 0:17d169ac117c 493
ThomasSonderDesign 0:17d169ac117c 494 /*###### EPD Write ######*/
ThomasSonderDesign 5:07113abf18c0 495 //Update the whole display with data from memory, starting at the
ThomasSonderDesign 0:17d169ac117c 496 int EPD_Write(SPI mem_spi, int address)
ThomasSonderDesign 0:17d169ac117c 497 {
ThomasSonderDesign 5:07113abf18c0 498 pc.printf("\nAddress: %i", address);
ThomasSonderDesign 0:17d169ac117c 499 //Setup the SPIs
ThomasSonderDesign 0:17d169ac117c 500 SPI epd_spi = setupEPD(); //Creat a new SPI object to comunicate with the EPD
ThomasSonderDesign 0:17d169ac117c 501
ThomasSonderDesign 0:17d169ac117c 502 //Create local variables
ThomasSonderDesign 5:07113abf18c0 503 int lineLengh = 60; //the length of the display in bytes
ThomasSonderDesign 6:2f4a272ab299 504 int imageLength = 1440;
ThomasSonderDesign 5:07113abf18c0 505 char sixtyBytes[lineLengh]; //Create a line buffer
ThomasSonderDesign 5:07113abf18c0 506
ThomasSonderDesign 0:17d169ac117c 507 //int line = 0; //counter to keep track of the current line
ThomasSonderDesign 0:17d169ac117c 508 //int frameLine = 0; //counter to keep track of the line in the current frame
ThomasSonderDesign 5:07113abf18c0 509
ThomasSonderDesign 0:17d169ac117c 510
ThomasSonderDesign 0:17d169ac117c 511 //led=!led;
ThomasSonderDesign 7:b30c411a6d36 512
ThomasSonderDesign 0:17d169ac117c 513 //Begin SPI comunication
ThomasSonderDesign 0:17d169ac117c 514 cs_epd=1; //EPD chip deselected
ThomasSonderDesign 0:17d169ac117c 515 TconEn =1; //Tcon ON set High
ThomasSonderDesign 0:17d169ac117c 516 wait_ms(120); //delay 120ms
ThomasSonderDesign 0:17d169ac117c 517 cs_epd=0; //Select the Tcon chip
ThomasSonderDesign 0:17d169ac117c 518 wait_ms(1); //delay 1ms
ThomasSonderDesign 0:17d169ac117c 519 //Write Header
ThomasSonderDesign 0:17d169ac117c 520 epd_spi.write(0x06);
ThomasSonderDesign 0:17d169ac117c 521 epd_spi.write(0xa0);
ThomasSonderDesign 0:17d169ac117c 522 wait_ms(120); //delay 120ms
ThomasSonderDesign 0:17d169ac117c 523
ThomasSonderDesign 0:17d169ac117c 524 //loop throug 1440 lines
ThomasSonderDesign 6:2f4a272ab299 525 for(int j=0; j<imageLength; j++) {
ThomasSonderDesign 5:07113abf18c0 526 //pc.printf("\n");
ThomasSonderDesign 0:17d169ac117c 527 mem.readData(mem_spi, sixtyBytes, address, lineLengh); //Read the line, putt them in buffer return the next address to read from
ThomasSonderDesign 5:07113abf18c0 528 for(int i =0; i<lineLengh; i++) { //Read one byte from the buffer
ThomasSonderDesign 0:17d169ac117c 529 epd_spi.write(sixtyBytes[i]); //and write it to the display
ThomasSonderDesign 5:07113abf18c0 530 //pc.printf("%i%i%i%i%i%i%i%i",sixtyBytes[i]>>7&&1,sixtyBytes[i]>>6&&1,sixtyBytes[i]>>5&&1,sixtyBytes[i]>>4&&1,sixtyBytes[i]>>3&&1,sixtyBytes[i]>>2&&1,sixtyBytes[i]>>1&&1,sixtyBytes[i]&&1);
ThomasSonderDesign 0:17d169ac117c 531 address++;
ThomasSonderDesign 0:17d169ac117c 532 //byteCounter++;
ThomasSonderDesign 0:17d169ac117c 533 }
ThomasSonderDesign 6:2f4a272ab299 534
ThomasSonderDesign 0:17d169ac117c 535 epd_spi.write(00); //Write a 0 at the end of each line
ThomasSonderDesign 7:b30c411a6d36 536 if(j%100==0){
ThomasSonderDesign 7:b30c411a6d36 537 keyCheck();
ThomasSonderDesign 7:b30c411a6d36 538 }
ThomasSonderDesign 0:17d169ac117c 539 //wait_us(10000);
ThomasSonderDesign 0:17d169ac117c 540 }
ThomasSonderDesign 0:17d169ac117c 541 wait_us(1000);
ThomasSonderDesign 0:17d169ac117c 542 DigitalOut sclk(P0_13); //serial clk
ThomasSonderDesign 0:17d169ac117c 543 sclk = 0;
ThomasSonderDesign 0:17d169ac117c 544 cs_epd=1; //Deselct the chip
ThomasSonderDesign 7:b30c411a6d36 545 Timer t;
ThomasSonderDesign 7:b30c411a6d36 546 t.start(); //Start the timer
ThomasSonderDesign 7:b30c411a6d36 547 while(TCbusy)
ThomasSonderDesign 7:b30c411a6d36 548 {
ThomasSonderDesign 7:b30c411a6d36 549 keyCheck();
ThomasSonderDesign 7:b30c411a6d36 550 //wait_ms
ThomasSonderDesign 7:b30c411a6d36 551 }
ThomasSonderDesign 7:b30c411a6d36 552 t.stop();
ThomasSonderDesign 0:17d169ac117c 553 TconEn=0; //Deassert Tcon ON
ThomasSonderDesign 0:17d169ac117c 554 cs_epd=0; //Deassert chip select
ThomasSonderDesign 0:17d169ac117c 555
ThomasSonderDesign 7:b30c411a6d36 556 printf("\ntime = %i ", t.read_ms());
ThomasSonderDesign 7:b30c411a6d36 557 t.reset();
ThomasSonderDesign 0:17d169ac117c 558 return 1;
ThomasSonderDesign 0:17d169ac117c 559 }
ThomasSonderDesign 0:17d169ac117c 560
ThomasSonderDesign 2:8b66a3f7e202 561 /*###### EPD Write ######*/
ThomasSonderDesign 2:8b66a3f7e202 562 //Update the whole display with data from a reserved slot
ThomasSonderDesign 2:8b66a3f7e202 563 int EPD_Swap(SPI mem_spi, int slot)
ThomasSonderDesign 2:8b66a3f7e202 564 {
ThomasSonderDesign 2:8b66a3f7e202 565 return EPD_Write(mem_spi, slots[slot]);
ThomasSonderDesign 2:8b66a3f7e202 566 }
ThomasSonderDesign 0:17d169ac117c 567 /*******************************************************
ThomasSonderDesign 0:17d169ac117c 568 * Main Function
ThomasSonderDesign 0:17d169ac117c 569 */
ThomasSonderDesign 0:17d169ac117c 570 int main()
ThomasSonderDesign 0:17d169ac117c 571 {
ThomasSonderDesign 1:1ec8bcd31b27 572 Timer t;
ThomasSonderDesign 1:1ec8bcd31b27 573
ThomasSonderDesign 0:17d169ac117c 574 int USBDataBuffer[64];
ThomasSonderDesign 0:17d169ac117c 575 SPI my_spi = setupSPI(); //Creates an SPI object to comunicate with the external memory
ThomasSonderDesign 2:8b66a3f7e202 576 populateNameList(my_spi); //Reads the names of layouts stored in external memory into RAM
ThomasSonderDesign 4:e6e1642724d4 577
ThomasSonderDesign 0:17d169ac117c 578 while(1) {
ThomasSonderDesign 8:3577b060d7af 579 //pc.printf("Loop");
ThomasSonderDesign 1:1ec8bcd31b27 580
ThomasSonderDesign 0:17d169ac117c 581 sendUSB(readyForComand);
ThomasSonderDesign 7:b30c411a6d36 582 keyCheck();
ThomasSonderDesign 1:1ec8bcd31b27 583
ThomasSonderDesign 0:17d169ac117c 584 sendUSB(readyForComand);
ThomasSonderDesign 0:17d169ac117c 585 if (readUSB(USBDataBuffer)>0) {
ThomasSonderDesign 4:e6e1642724d4 586 pc.printf("\nSwitch %i",USBDataBuffer[1]);
ThomasSonderDesign 3:d4e1892846fb 587 char temp[] = {USBDataBuffer[3],USBDataBuffer[4],USBDataBuffer[5],USBDataBuffer[6],USBDataBuffer[7]};
ThomasSonderDesign 0:17d169ac117c 588 switch(USBDataBuffer[1]) {
ThomasSonderDesign 0:17d169ac117c 589 case 0x10: //If the recieved data is a write instruction
ThomasSonderDesign 3:d4e1892846fb 590 GlobalAddress = writeFrame(my_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]); //Write the following data to the memory at 24 bit address usbDatabuffer[2-4]
ThomasSonderDesign 0:17d169ac117c 591 break;
ThomasSonderDesign 3:d4e1892846fb 592 case 0x11:
ThomasSonderDesign 5:07113abf18c0 593 pc.printf("\nPrep write");
ThomasSonderDesign 4:e6e1642724d4 594 prepImage(my_spi, USBDataBuffer[2], temp);//Prepare a slot in memory for an image
ThomasSonderDesign 4:e6e1642724d4 595 break;
ThomasSonderDesign 4:e6e1642724d4 596 case 0x12:
ThomasSonderDesign 5:07113abf18c0 597 pc.printf("\nImage write");
ThomasSonderDesign 4:e6e1642724d4 598 writeImage(my_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);//Write the next 86400 bytes of data to the memory starting at 24 bit address usbDatabuffer[2-4]
ThomasSonderDesign 3:d4e1892846fb 599 break;
ThomasSonderDesign 0:17d169ac117c 600 case 0x20: //If the recieved comand is a read instruction
ThomasSonderDesign 0:17d169ac117c 601 //mem.readData(my_spi, memoryBuffer, USBDataBuffer[2], USBDataBuffer[3]);//read(spi, destination[], address, length)
ThomasSonderDesign 1:1ec8bcd31b27 602 //pc.printf(" \n---EPD UPDATE--- %i",USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);
ThomasSonderDesign 0:17d169ac117c 603 EPD_Write(my_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);
ThomasSonderDesign 0:17d169ac117c 604 break;
ThomasSonderDesign 8:3577b060d7af 605 case 0x21: //If the recieved comand is a swap instruction
ThomasSonderDesign 8:3577b060d7af 606 EPD_Swap(my_spi, USBDataBuffer[2]);
ThomasSonderDesign 8:3577b060d7af 607 break;
ThomasSonderDesign 0:17d169ac117c 608 case 0x30: //If the recieved comand is a request for the cutrrent name
ThomasSonderDesign 8:3577b060d7af 609 populateNameList(my_spi);
ThomasSonderDesign 8:3577b060d7af 610 getNameList();
ThomasSonderDesign 0:17d169ac117c 611 break;
ThomasSonderDesign 7:b30c411a6d36 612 case 0x35: //If the recieved comand is a request for all the names
ThomasSonderDesign 0:17d169ac117c 613 getCurrentLayout();
ThomasSonderDesign 0:17d169ac117c 614 break;
ThomasSonderDesign 0:17d169ac117c 615 default:
ThomasSonderDesign 4:e6e1642724d4 616 pc.printf("\nfail! [%x %x %x %x %x %x %x %x]",USBDataBuffer[0],USBDataBuffer[1],USBDataBuffer[2],USBDataBuffer[3],USBDataBuffer[4],USBDataBuffer[5],USBDataBuffer[6],USBDataBuffer[7]);
ThomasSonderDesign 0:17d169ac117c 617 }
ThomasSonderDesign 0:17d169ac117c 618 }
ThomasSonderDesign 1:1ec8bcd31b27 619
ThomasSonderDesign 3:d4e1892846fb 620 //pc.printf("\n%d", t.read_us());
ThomasSonderDesign 1:1ec8bcd31b27 621 t.reset();
ThomasSonderDesign 0:17d169ac117c 622 }
ThomasSonderDesign 0:17d169ac117c 623 }