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:
Thu Jun 30 01:08:45 2016 +0000
Revision:
2:8b66a3f7e202
Parent:
1:1ec8bcd31b27
Child:
3:d4e1892846fb
Working mvp firmware. layout transfer still slow. to be used with BB_Basic_Client 27/06/16

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