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 23 22:33:40 2016 +0000
Revision:
0:17d169ac117c
Child:
1:1ec8bcd31b27
A first implementation of some complete firmware on the black board. It sends key presses and updates the display as necessary. ATM it writes layouts to the memory extremely slowly.

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