Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Memory25L16_fast USBDevice mbed
Fork of BlackBoard_Firmware_MVP by
BB_Basic.cpp@10:1f96f0cce2a0, 2018-04-17 (annotated)
- Committer:
- ThomasSonderDesign
- Date:
- Tue Apr 17 03:16:23 2018 +0000
- Revision:
- 10:1f96f0cce2a0
- Parent:
- 9:7c0f42f090e8
- Child:
- 12:fbb1dd0586e1
Now working with blacjkboard
Who changed what in which revision?
User | Revision | Line number | New 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 | 9:7c0f42f090e8 | 16 | //Spi port for epd. on black board P0_21, P1_21, P1_20. on QSB P0_21,P0_22,P1_15 |
ThomasSonderDesign | 10:1f96f0cce2a0 | 17 | #define EPDSPI P0_21,P0_22,P1_20 |
ThomasSonderDesign | 9:7c0f42f090e8 | 18 | //Spi port for memory. on black board P0_9,P0_8,P0_10, P1_20. on QSB P0_9,P0_8,P0_10 |
ThomasSonderDesign | 9:7c0f42f090e8 | 19 | #define MEMSPI P0_9,P0_8,P0_10 |
ThomasSonderDesign | 9:7c0f42f090e8 | 20 | #define SLOTSIZE 0x50000 |
ThomasSonderDesign | 0:17d169ac117c | 21 | /****************************************************************************** |
ThomasSonderDesign | 0:17d169ac117c | 22 | * Definitiontions |
ThomasSonderDesign | 0:17d169ac117c | 23 | */ |
ThomasSonderDesign | 0:17d169ac117c | 24 | //Constants for black board |
ThomasSonderDesign | 0:17d169ac117c | 25 | |
ThomasSonderDesign | 10:1f96f0cce2a0 | 26 | DigitalOut cs_mem(P0_2); //Set up memory's Chip Select pin BB:P0_2 QSB:P0_16 spi0 |
ThomasSonderDesign | 10:1f96f0cce2a0 | 27 | DigitalOut cs_epd(P1_23); //Set up epd's Chip Select pin BB:P1_23 QSB:P0_11 spi1 |
ThomasSonderDesign | 0:17d169ac117c | 28 | DigitalOut TconEn(P0_5); //Tcon Enable pin, open drain must invert |
ThomasSonderDesign | 0:17d169ac117c | 29 | DigitalOut start(P0_14); |
ThomasSonderDesign | 9:7c0f42f090e8 | 30 | DigitalIn TCbusy(P0_4); //Tcon busy line P0_4 |
ThomasSonderDesign | 0:17d169ac117c | 31 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 32 | DigitalOut trigger(P1_19); |
ThomasSonderDesign | 9:7c0f42f090e8 | 33 | DigitalOut myled(P0_23); |
ThomasSonderDesign | 9:7c0f42f090e8 | 34 | |
ThomasSonderDesign | 0:17d169ac117c | 35 | DigitalOut SRclk(P0_15); //Shift register clk signal |
ThomasSonderDesign | 0:17d169ac117c | 36 | DigitalOut SRlat (P1_22); //Shift register output latch |
ThomasSonderDesign | 0:17d169ac117c | 37 | DigitalOut SRds(P1_14); //Shiftreg data pin |
ThomasSonderDesign | 0:17d169ac117c | 38 | |
ThomasSonderDesign | 0:17d169ac117c | 39 | DigitalIn E(P1_13); //Definne row row A input pin |
ThomasSonderDesign | 0:17d169ac117c | 40 | DigitalIn D(P0_7); //Definne row row B input pin |
ThomasSonderDesign | 0:17d169ac117c | 41 | DigitalIn C(P1_28); //Definne row row C input pin |
ThomasSonderDesign | 0:17d169ac117c | 42 | DigitalIn B(P1_31); //Definne row row D input pin |
ThomasSonderDesign | 0:17d169ac117c | 43 | DigitalIn A(P1_29); //Definne row row E input pin |
ThomasSonderDesign | 0:17d169ac117c | 44 | |
ThomasSonderDesign | 10:1f96f0cce2a0 | 45 | DigitalIn modC(P0_11); //Definne row mod C input pin BB:P0_11 QSB:P0_1 |
ThomasSonderDesign | 0:17d169ac117c | 46 | DigitalIn modB(P0_12); //Definne row mod B input pin |
ThomasSonderDesign | 0:17d169ac117c | 47 | DigitalIn modA(P0_13); //Definne row mod A input pin |
ThomasSonderDesign | 0:17d169ac117c | 48 | |
ThomasSonderDesign | 0:17d169ac117c | 49 | Serial pc (P0_19,P0_18); //Setup a real serial port |
ThomasSonderDesign | 10:1f96f0cce2a0 | 50 | Memory mem(P0_2); //Create a new memory manager with chip select on P0_2 |
ThomasSonderDesign | 0:17d169ac117c | 51 | |
ThomasSonderDesign | 0:17d169ac117c | 52 | 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 | 53 | |
ThomasSonderDesign | 0:17d169ac117c | 54 | volatile int *PIN39IOREG = (int *)0x4004403C; |
ThomasSonderDesign | 0:17d169ac117c | 55 | int reportLength = 64; //The length of the report recieved |
ThomasSonderDesign | 0:17d169ac117c | 56 | |
ThomasSonderDesign | 0:17d169ac117c | 57 | //Codes to send to the pc |
ThomasSonderDesign | 0:17d169ac117c | 58 | int readyAndWaiting []= {2,0x25,0,0,0,0,0,0}; |
ThomasSonderDesign | 0:17d169ac117c | 59 | int busy []= {2,0x50,0,0,0,0,0,0}; |
ThomasSonderDesign | 0:17d169ac117c | 60 | int readyForComand []= {2,0x75,0,0,0,0,0,0}; |
ThomasSonderDesign | 0:17d169ac117c | 61 | |
ThomasSonderDesign | 0:17d169ac117c | 62 | |
ThomasSonderDesign | 0:17d169ac117c | 63 | int GlobalAddress = 0; //The last adress written to plus one |
ThomasSonderDesign | 0:17d169ac117c | 64 | int lastErasedBlock =0; //The Adress of the last erases block |
ThomasSonderDesign | 0:17d169ac117c | 65 | int currentName = 0; //The position in the nameList of the curret layout |
ThomasSonderDesign | 1:1ec8bcd31b27 | 66 | char nameList[16][5]; //A list of all the Layouts in memory, each name is 5 bytes long |
ThomasSonderDesign | 1:1ec8bcd31b27 | 67 | |
ThomasSonderDesign | 1:1ec8bcd31b27 | 68 | //Locations in memory were the 16 layouts can be stored |
ThomasSonderDesign | 9:7c0f42f090e8 | 69 | const int slots [] = {0*SLOTSIZE,1*SLOTSIZE, 2*SLOTSIZE,3*SLOTSIZE, 4*SLOTSIZE, 5*SLOTSIZE, 6*SLOTSIZE, 7*SLOTSIZE, 8*SLOTSIZE, 9*SLOTSIZE,10*SLOTSIZE,11*SLOTSIZE,12*SLOTSIZE}; |
ThomasSonderDesign | 9:7c0f42f090e8 | 70 | bool erasedSlots[]= {true,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false}; |
ThomasSonderDesign | 0:17d169ac117c | 71 | |
ThomasSonderDesign | 0:17d169ac117c | 72 | int keybuffer[10][2]; //A buffer for storing key presses (mod key plus key) |
ThomasSonderDesign | 0:17d169ac117c | 73 | |
ThomasSonderDesign | 0:17d169ac117c | 74 | int scanCode [5][14]= { //This 2D array maps key scan codes to letter codes |
ThomasSonderDesign | 0:17d169ac117c | 75 | {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 | 76 | {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 | 77 | {KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, VK_OEM_1}, |
ThomasSonderDesign | 0:17d169ac117c | 78 | {KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, VK_LSHIFT,VK_CONTROL,VK_LMENU}, |
ThomasSonderDesign | 0:17d169ac117c | 79 | {VK_F1, VK_F2,VK_F3,VK_F4, VK_SPACE, VK_TAB, VK_BACK, VK_RSHIFT,VK_LWIN,VK_RMENU} |
ThomasSonderDesign | 0:17d169ac117c | 80 | }; |
ThomasSonderDesign | 0:17d169ac117c | 81 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 82 | /****************************************************************************** |
ThomasSonderDesign | 9:7c0f42f090e8 | 83 | * prototype Functions |
ThomasSonderDesign | 9:7c0f42f090e8 | 84 | */ |
ThomasSonderDesign | 9:7c0f42f090e8 | 85 | short bitDouble(int); |
ThomasSonderDesign | 0:17d169ac117c | 86 | |
ThomasSonderDesign | 0:17d169ac117c | 87 | /****************************************************************************** |
ThomasSonderDesign | 0:17d169ac117c | 88 | * Set up Functions |
ThomasSonderDesign | 0:17d169ac117c | 89 | */ |
ThomasSonderDesign | 0:17d169ac117c | 90 | char check(int depth) //Check availible memory |
ThomasSonderDesign | 0:17d169ac117c | 91 | { |
ThomasSonderDesign | 0:17d169ac117c | 92 | char c; |
ThomasSonderDesign | 0:17d169ac117c | 93 | char *ptr = new char; |
ThomasSonderDesign | 0:17d169ac117c | 94 | //pc.printf("stack at %p, heap at %p\n", &c, ptr); |
ThomasSonderDesign | 0:17d169ac117c | 95 | //pc.printf("sh,%i,%i\n", &c, ptr); |
ThomasSonderDesign | 0:17d169ac117c | 96 | pc.printf("mem size = %p\n", &c-ptr); |
ThomasSonderDesign | 0:17d169ac117c | 97 | //if (depth <= 0) return; |
ThomasSonderDesign | 0:17d169ac117c | 98 | // check(depth-1); |
ThomasSonderDesign | 0:17d169ac117c | 99 | free(ptr); |
ThomasSonderDesign | 0:17d169ac117c | 100 | return 1; |
ThomasSonderDesign | 0:17d169ac117c | 101 | } |
ThomasSonderDesign | 0:17d169ac117c | 102 | |
ThomasSonderDesign | 0:17d169ac117c | 103 | /** |
ThomasSonderDesign | 0:17d169ac117c | 104 | * Set up the memory SPI |
ThomasSonderDesign | 0:17d169ac117c | 105 | */ |
ThomasSonderDesign | 0:17d169ac117c | 106 | SPI setupSPI() |
ThomasSonderDesign | 0:17d169ac117c | 107 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 108 | SPI my_spi(MEMSPI); // mosi, miso, sclk |
ThomasSonderDesign | 0:17d169ac117c | 109 | //cs_mem = 1; // Chip must be deselected |
ThomasSonderDesign | 0:17d169ac117c | 110 | my_spi.format(8,3); // Setup the spi for 8 bit data, low steady state clock, |
ThomasSonderDesign | 9:7c0f42f090e8 | 111 | my_spi.frequency(10000000); // second edge capture, with a 1MHz clock rate, Will work up to 20MHz |
ThomasSonderDesign | 0:17d169ac117c | 112 | return my_spi; |
ThomasSonderDesign | 0:17d169ac117c | 113 | } |
ThomasSonderDesign | 0:17d169ac117c | 114 | |
ThomasSonderDesign | 0:17d169ac117c | 115 | |
ThomasSonderDesign | 0:17d169ac117c | 116 | /** |
ThomasSonderDesign | 0:17d169ac117c | 117 | * Setup USB HID, usefull for later not used in this iterarion |
ThomasSonderDesign | 0:17d169ac117c | 118 | */ |
ThomasSonderDesign | 0:17d169ac117c | 119 | /*USBHID setupUSB() |
ThomasSonderDesign | 0:17d169ac117c | 120 | { |
ThomasSonderDesign | 0:17d169ac117c | 121 | 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 | 122 | return hid; |
ThomasSonderDesign | 0:17d169ac117c | 123 | }*/ |
ThomasSonderDesign | 0:17d169ac117c | 124 | |
ThomasSonderDesign | 0:17d169ac117c | 125 | |
ThomasSonderDesign | 0:17d169ac117c | 126 | /****************************************************************************** |
ThomasSonderDesign | 0:17d169ac117c | 127 | * USB Functions |
ThomasSonderDesign | 0:17d169ac117c | 128 | */ |
ThomasSonderDesign | 0:17d169ac117c | 129 | |
ThomasSonderDesign | 0:17d169ac117c | 130 | /** |
ThomasSonderDesign | 0:17d169ac117c | 131 | * checks the usb recive buffer, if it contains data it is coppired to USBDataBuffer and returns 1 |
ThomasSonderDesign | 0:17d169ac117c | 132 | * if the empty return -1; |
ThomasSonderDesign | 0:17d169ac117c | 133 | */ |
ThomasSonderDesign | 0:17d169ac117c | 134 | int readUSB(int USBDataBuffer[]) |
ThomasSonderDesign | 0:17d169ac117c | 135 | { |
ThomasSonderDesign | 0:17d169ac117c | 136 | HID_REPORT recv_report; |
ThomasSonderDesign | 0:17d169ac117c | 137 | if(hid.readNB(&recv_report)) { //Check if there is a received report |
ThomasSonderDesign | 0:17d169ac117c | 138 | //printf("\n"); |
ThomasSonderDesign | 0:17d169ac117c | 139 | for(int i = 0; i<recv_report.length; i++) { //Store the report in a locat array |
ThomasSonderDesign | 0:17d169ac117c | 140 | //printf(" %x", recv_report.data[i]); |
ThomasSonderDesign | 0:17d169ac117c | 141 | USBDataBuffer[i] = recv_report.data[i]; |
ThomasSonderDesign | 0:17d169ac117c | 142 | } |
ThomasSonderDesign | 0:17d169ac117c | 143 | return 1; |
ThomasSonderDesign | 0:17d169ac117c | 144 | } else { |
ThomasSonderDesign | 0:17d169ac117c | 145 | return -1; |
ThomasSonderDesign | 0:17d169ac117c | 146 | } |
ThomasSonderDesign | 0:17d169ac117c | 147 | } |
ThomasSonderDesign | 0:17d169ac117c | 148 | |
ThomasSonderDesign | 0:17d169ac117c | 149 | |
ThomasSonderDesign | 0:17d169ac117c | 150 | /** |
ThomasSonderDesign | 0:17d169ac117c | 151 | * usbSend, send an eight byte report over usb |
ThomasSonderDesign | 0:17d169ac117c | 152 | */ |
ThomasSonderDesign | 0:17d169ac117c | 153 | void sendUSB(int usbReport[]) |
ThomasSonderDesign | 0:17d169ac117c | 154 | { |
ThomasSonderDesign | 0:17d169ac117c | 155 | //pc.printf("\nS: %x",usbReport[1]); |
ThomasSonderDesign | 0:17d169ac117c | 156 | HID_REPORT send_report; |
ThomasSonderDesign | 0:17d169ac117c | 157 | send_report.length = 8; |
ThomasSonderDesign | 0:17d169ac117c | 158 | |
ThomasSonderDesign | 0:17d169ac117c | 159 | send_report.data[0] = usbReport[0]; |
ThomasSonderDesign | 0:17d169ac117c | 160 | send_report.data[1] = usbReport[1]; //comand byte |
ThomasSonderDesign | 0:17d169ac117c | 161 | send_report.data[2] = usbReport[2]; |
ThomasSonderDesign | 0:17d169ac117c | 162 | send_report.data[3] = usbReport[3]; |
ThomasSonderDesign | 0:17d169ac117c | 163 | send_report.data[4] = usbReport[4]; |
ThomasSonderDesign | 0:17d169ac117c | 164 | send_report.data[5] = usbReport[5]; |
ThomasSonderDesign | 0:17d169ac117c | 165 | send_report.data[6] = usbReport[6]; |
ThomasSonderDesign | 0:17d169ac117c | 166 | send_report.data[7] = usbReport[7]; |
ThomasSonderDesign | 0:17d169ac117c | 167 | hid.send(&send_report); |
ThomasSonderDesign | 0:17d169ac117c | 168 | } |
ThomasSonderDesign | 0:17d169ac117c | 169 | |
ThomasSonderDesign | 0:17d169ac117c | 170 | |
ThomasSonderDesign | 0:17d169ac117c | 171 | |
ThomasSonderDesign | 7:b30c411a6d36 | 172 | |
ThomasSonderDesign | 0:17d169ac117c | 173 | /****************************************************************************** |
ThomasSonderDesign | 0:17d169ac117c | 174 | * Keyboard Functions |
ThomasSonderDesign | 0:17d169ac117c | 175 | */ |
ThomasSonderDesign | 0:17d169ac117c | 176 | |
ThomasSonderDesign | 0:17d169ac117c | 177 | /** |
ThomasSonderDesign | 0:17d169ac117c | 178 | * keyScan(int keyBuffer[][]). Sacns the keyboard matrix and stores any keypresses in the 2D keyBuffer |
ThomasSonderDesign | 0:17d169ac117c | 179 | * If a key press was detected it returns the most recent key press, otherwise it returns 0 |
ThomasSonderDesign | 0:17d169ac117c | 180 | */ |
ThomasSonderDesign | 0:17d169ac117c | 181 | int keyScan() |
ThomasSonderDesign | 0:17d169ac117c | 182 | { |
ThomasSonderDesign | 0:17d169ac117c | 183 | //Initialise and set Up code |
ThomasSonderDesign | 0:17d169ac117c | 184 | int counter=0; //Keubuffer index counter |
ThomasSonderDesign | 0:17d169ac117c | 185 | int rowNum = 0; |
ThomasSonderDesign | 0:17d169ac117c | 186 | int code =0; //the return value, the last key pushed |
ThomasSonderDesign | 0:17d169ac117c | 187 | |
ThomasSonderDesign | 0:17d169ac117c | 188 | /*Clear the keybuffer*/ |
ThomasSonderDesign | 0:17d169ac117c | 189 | for(int i = 0; i<10; i++) { |
ThomasSonderDesign | 0:17d169ac117c | 190 | keybuffer[i][0]=0; |
ThomasSonderDesign | 0:17d169ac117c | 191 | keybuffer[i][1]=0; |
ThomasSonderDesign | 0:17d169ac117c | 192 | } |
ThomasSonderDesign | 0:17d169ac117c | 193 | |
ThomasSonderDesign | 0:17d169ac117c | 194 | int noCols = 16; //number of bits to be shifted |
ThomasSonderDesign | 0:17d169ac117c | 195 | int t = 1000; //wait time in uS. This is the period in us of the shift register clock |
ThomasSonderDesign | 0:17d169ac117c | 196 | SRclk=0; |
ThomasSonderDesign | 0:17d169ac117c | 197 | SRlat = 0; |
ThomasSonderDesign | 0:17d169ac117c | 198 | SRds =1; //Set first bit high |
ThomasSonderDesign | 0:17d169ac117c | 199 | |
ThomasSonderDesign | 0:17d169ac117c | 200 | |
ThomasSonderDesign | 0:17d169ac117c | 201 | for (int col = 0; col<noCols; col++) { //Loop through one shifit reg |
ThomasSonderDesign | 0:17d169ac117c | 202 | SRclk = 1; //clock in current bit // __ __ |
ThomasSonderDesign | 0:17d169ac117c | 203 | wait_us(t/2); //SRclk__| |_..._| |_ |
ThomasSonderDesign | 0:17d169ac117c | 204 | SRds = 0; //set dext bit low // ___ |
ThomasSonderDesign | 0:17d169ac117c | 205 | wait_us(t/2); //SRds | |___..._____ |
ThomasSonderDesign | 0:17d169ac117c | 206 | SRclk=0; // _ _ |
ThomasSonderDesign | 0:17d169ac117c | 207 | SRlat = 1; //latch all data out //SRlat____| |_...___| |_ |
ThomasSonderDesign | 0:17d169ac117c | 208 | wait_us(t/2); // |
ThomasSonderDesign | 0:17d169ac117c | 209 | SRlat=0; |
ThomasSonderDesign | 0:17d169ac117c | 210 | |
ThomasSonderDesign | 0:17d169ac117c | 211 | /* |
ThomasSonderDesign | 0:17d169ac117c | 212 | Check if a button has been pressed by scaning the rows |
ThomasSonderDesign | 0:17d169ac117c | 213 | Generates a unique code depending on which button has been pressed |
ThomasSonderDesign | 0:17d169ac117c | 214 | The code is looked up in the scanCode Array |
ThomasSonderDesign | 0:17d169ac117c | 215 | */ |
ThomasSonderDesign | 0:17d169ac117c | 216 | if(A>0) { |
ThomasSonderDesign | 0:17d169ac117c | 217 | //pc.printf("A\n"); |
ThomasSonderDesign | 0:17d169ac117c | 218 | rowNum = 0; |
ThomasSonderDesign | 0:17d169ac117c | 219 | code = scanCode[rowNum][col]; |
ThomasSonderDesign | 0:17d169ac117c | 220 | keybuffer[counter][1]= code; |
ThomasSonderDesign | 0:17d169ac117c | 221 | counter++; |
ThomasSonderDesign | 0:17d169ac117c | 222 | } |
ThomasSonderDesign | 0:17d169ac117c | 223 | if(B>0) { |
ThomasSonderDesign | 0:17d169ac117c | 224 | //pc.printf("B\n"); |
ThomasSonderDesign | 0:17d169ac117c | 225 | rowNum = 1; |
ThomasSonderDesign | 0:17d169ac117c | 226 | code = scanCode[rowNum][col]; |
ThomasSonderDesign | 0:17d169ac117c | 227 | keybuffer[counter][1]= code; |
ThomasSonderDesign | 0:17d169ac117c | 228 | counter++; |
ThomasSonderDesign | 0:17d169ac117c | 229 | } |
ThomasSonderDesign | 0:17d169ac117c | 230 | if(C>0) { |
ThomasSonderDesign | 0:17d169ac117c | 231 | //pc.printf("C\n"); |
ThomasSonderDesign | 0:17d169ac117c | 232 | rowNum = 2; |
ThomasSonderDesign | 0:17d169ac117c | 233 | code = scanCode[rowNum][col]; |
ThomasSonderDesign | 0:17d169ac117c | 234 | keybuffer[counter][1]= code; |
ThomasSonderDesign | 0:17d169ac117c | 235 | counter++; |
ThomasSonderDesign | 0:17d169ac117c | 236 | } |
ThomasSonderDesign | 0:17d169ac117c | 237 | if(D>0) { |
ThomasSonderDesign | 0:17d169ac117c | 238 | //pc.printf("D\n"); |
ThomasSonderDesign | 0:17d169ac117c | 239 | rowNum = 3; |
ThomasSonderDesign | 0:17d169ac117c | 240 | code = scanCode[rowNum][col]; |
ThomasSonderDesign | 0:17d169ac117c | 241 | keybuffer[counter][1]= code; |
ThomasSonderDesign | 0:17d169ac117c | 242 | counter++; |
ThomasSonderDesign | 0:17d169ac117c | 243 | } |
ThomasSonderDesign | 0:17d169ac117c | 244 | if(E>0) { |
ThomasSonderDesign | 0:17d169ac117c | 245 | //pc.printf("E\n"); |
ThomasSonderDesign | 0:17d169ac117c | 246 | rowNum = 4; |
ThomasSonderDesign | 0:17d169ac117c | 247 | code = scanCode[rowNum][col]; |
ThomasSonderDesign | 0:17d169ac117c | 248 | keybuffer[counter][1]= code; |
ThomasSonderDesign | 0:17d169ac117c | 249 | counter++; |
ThomasSonderDesign | 0:17d169ac117c | 250 | } |
ThomasSonderDesign | 0:17d169ac117c | 251 | |
ThomasSonderDesign | 0:17d169ac117c | 252 | /*Scan the mod keys and assign them to the mod column of key buffer in the from zero up*/ |
ThomasSonderDesign | 0:17d169ac117c | 253 | if(col>0) { |
ThomasSonderDesign | 0:17d169ac117c | 254 | counter=0; |
ThomasSonderDesign | 0:17d169ac117c | 255 | |
ThomasSonderDesign | 0:17d169ac117c | 256 | if(modC>0) { |
ThomasSonderDesign | 0:17d169ac117c | 257 | rowNum = 0; |
ThomasSonderDesign | 0:17d169ac117c | 258 | code = scanCode[rowNum][col]; |
ThomasSonderDesign | 0:17d169ac117c | 259 | keybuffer[counter][0]= code; |
ThomasSonderDesign | 0:17d169ac117c | 260 | } |
ThomasSonderDesign | 0:17d169ac117c | 261 | if(modB>0) { |
ThomasSonderDesign | 0:17d169ac117c | 262 | rowNum = 0; |
ThomasSonderDesign | 0:17d169ac117c | 263 | code = scanCode[rowNum][col]; |
ThomasSonderDesign | 0:17d169ac117c | 264 | keybuffer[counter][0]= code; |
ThomasSonderDesign | 0:17d169ac117c | 265 | } |
ThomasSonderDesign | 0:17d169ac117c | 266 | if(modA>0) { |
ThomasSonderDesign | 0:17d169ac117c | 267 | rowNum = 0; |
ThomasSonderDesign | 0:17d169ac117c | 268 | code = scanCode[rowNum][col]; |
ThomasSonderDesign | 0:17d169ac117c | 269 | keybuffer[counter][0]= code; |
ThomasSonderDesign | 0:17d169ac117c | 270 | } |
ThomasSonderDesign | 0:17d169ac117c | 271 | } |
ThomasSonderDesign | 0:17d169ac117c | 272 | } |
ThomasSonderDesign | 0:17d169ac117c | 273 | return code; |
ThomasSonderDesign | 0:17d169ac117c | 274 | } |
ThomasSonderDesign | 0:17d169ac117c | 275 | |
ThomasSonderDesign | 0:17d169ac117c | 276 | |
ThomasSonderDesign | 0:17d169ac117c | 277 | /** |
ThomasSonderDesign | 0:17d169ac117c | 278 | * int sendKey(int mod, int key). Sends a keypress to the pc over usb |
ThomasSonderDesign | 0:17d169ac117c | 279 | */ |
ThomasSonderDesign | 0:17d169ac117c | 280 | void sendKey(int mod, int key) |
ThomasSonderDesign | 0:17d169ac117c | 281 | { |
ThomasSonderDesign | 0:17d169ac117c | 282 | int temp[]= {0x01,mod,key,0,0,0,0,0}; |
ThomasSonderDesign | 0:17d169ac117c | 283 | sendUSB(temp); |
ThomasSonderDesign | 0:17d169ac117c | 284 | } |
ThomasSonderDesign | 0:17d169ac117c | 285 | |
ThomasSonderDesign | 7:b30c411a6d36 | 286 | /** |
ThomasSonderDesign | 7:b30c411a6d36 | 287 | * keyCheck(), scanss the keys and sends the keypress to the pc over usb |
ThomasSonderDesign | 7:b30c411a6d36 | 288 | */ |
ThomasSonderDesign | 9:7c0f42f090e8 | 289 | void keyCheck() |
ThomasSonderDesign | 9:7c0f42f090e8 | 290 | { |
ThomasSonderDesign | 8:3577b060d7af | 291 | //pc.printf("KeyCheck\n"); |
ThomasSonderDesign | 7:b30c411a6d36 | 292 | if (keyScan()>0) { //check if keyScan returned key presss |
ThomasSonderDesign | 9:7c0f42f090e8 | 293 | //pc.printf("\nKey scan|n"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 294 | int countpos =0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 295 | while(keybuffer[countpos][1]>0) { //While there are keypresses in the buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 296 | pc.printf("%i",keybuffer[countpos][1]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 297 | pc.printf(" "); |
ThomasSonderDesign | 9:7c0f42f090e8 | 298 | sendKey(keybuffer[countpos][0],keybuffer[countpos][1]);//Send key press |
ThomasSonderDesign | 9:7c0f42f090e8 | 299 | countpos++; |
ThomasSonderDesign | 9:7c0f42f090e8 | 300 | myled=0; |
ThomasSonderDesign | 7:b30c411a6d36 | 301 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 302 | } |
ThomasSonderDesign | 7:b30c411a6d36 | 303 | } |
ThomasSonderDesign | 0:17d169ac117c | 304 | |
ThomasSonderDesign | 0:17d169ac117c | 305 | /****************************************************************************** |
ThomasSonderDesign | 1:1ec8bcd31b27 | 306 | * Memory control funtions |
ThomasSonderDesign | 0:17d169ac117c | 307 | */ |
ThomasSonderDesign | 0:17d169ac117c | 308 | |
ThomasSonderDesign | 0:17d169ac117c | 309 | |
ThomasSonderDesign | 0:17d169ac117c | 310 | /** |
ThomasSonderDesign | 0:17d169ac117c | 311 | * Writes the contense of USBDataBuffer to memoryBuffer, starting at bufferIndex |
ThomasSonderDesign | 0:17d169ac117c | 312 | * returns the index of the last written byte+1; |
ThomasSonderDesign | 0:17d169ac117c | 313 | */ |
ThomasSonderDesign | 0:17d169ac117c | 314 | int appendBuffer(int USBDataBuffer[], char memoryBuffer[], int bufferIndex) |
ThomasSonderDesign | 0:17d169ac117c | 315 | { |
ThomasSonderDesign | 5:07113abf18c0 | 316 | //printf("\n"); |
ThomasSonderDesign | 0:17d169ac117c | 317 | int posIndex=0; |
ThomasSonderDesign | 0:17d169ac117c | 318 | for(int i = 0; i < 64; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 319 | |
ThomasSonderDesign | 0:17d169ac117c | 320 | memoryBuffer[bufferIndex+i]=USBDataBuffer[i]; |
ThomasSonderDesign | 0:17d169ac117c | 321 | posIndex++; |
ThomasSonderDesign | 0:17d169ac117c | 322 | } |
ThomasSonderDesign | 0:17d169ac117c | 323 | return (bufferIndex+posIndex); |
ThomasSonderDesign | 0:17d169ac117c | 324 | } |
ThomasSonderDesign | 0:17d169ac117c | 325 | |
ThomasSonderDesign | 0:17d169ac117c | 326 | /** |
ThomasSonderDesign | 0:17d169ac117c | 327 | *Sends the ready and waiting signall and loops untill data is recieved. |
ThomasSonderDesign | 0:17d169ac117c | 328 | *Recieved data is stored in the USBDataBuffer |
ThomasSonderDesign | 0:17d169ac117c | 329 | */ |
ThomasSonderDesign | 0:17d169ac117c | 330 | void waitForData(int USBDataBuffer[]) |
ThomasSonderDesign | 0:17d169ac117c | 331 | { |
ThomasSonderDesign | 0:17d169ac117c | 332 | sendUSB(readyAndWaiting); |
ThomasSonderDesign | 0:17d169ac117c | 333 | while (readUSB(USBDataBuffer)<1) { |
ThomasSonderDesign | 0:17d169ac117c | 334 | sendUSB(readyAndWaiting); |
ThomasSonderDesign | 0:17d169ac117c | 335 | } |
ThomasSonderDesign | 0:17d169ac117c | 336 | } |
ThomasSonderDesign | 0:17d169ac117c | 337 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 338 | /** |
ThomasSonderDesign | 10:1f96f0cce2a0 | 339 | * Reads 'length' elements into a short array starting at the 24bit Address. |
ThomasSonderDesign | 9:7c0f42f090e8 | 340 | *If length is greater than BufferSize (3840 bytes) the function will terminate |
ThomasSonderDesign | 10:1f96f0cce2a0 | 341 | *and return the start address. tyhe eighteight bit data read from memory is |
ThomasSonderDesign | 10:1f96f0cce2a0 | 342 | doubled to sixteen bits for the display. eg 0b1010 =>0b11001100 |
ThomasSonderDesign | 9:7c0f42f090e8 | 343 | */ |
ThomasSonderDesign | 9:7c0f42f090e8 | 344 | int readData(SPI my_spi, short value [], int Address, int length, int doubleSelect) |
ThomasSonderDesign | 9:7c0f42f090e8 | 345 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 346 | //if(length>bufferSize) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 347 | //printf("\nLength %i exceeds Max Length\n",length); |
ThomasSonderDesign | 9:7c0f42f090e8 | 348 | //return Address; |
ThomasSonderDesign | 9:7c0f42f090e8 | 349 | //} |
ThomasSonderDesign | 9:7c0f42f090e8 | 350 | int temp = 0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 351 | short temp1 = 0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 352 | cs_mem = 1; //Ensure cs is deselected |
ThomasSonderDesign | 9:7c0f42f090e8 | 353 | //wait_us(10); |
ThomasSonderDesign | 9:7c0f42f090e8 | 354 | cs_mem = 0; //memory is selected |
ThomasSonderDesign | 9:7c0f42f090e8 | 355 | my_spi.write(0x03); //Send read command |
ThomasSonderDesign | 9:7c0f42f090e8 | 356 | my_spi.write(Address>>16); //Send high address byte |
ThomasSonderDesign | 9:7c0f42f090e8 | 357 | my_spi.write(Address>>8); //Send mid address byte |
ThomasSonderDesign | 9:7c0f42f090e8 | 358 | my_spi.write(Address); //Send low address byte |
ThomasSonderDesign | 9:7c0f42f090e8 | 359 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 360 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 361 | for(int i =0; i <length; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 362 | temp = my_spi.write(dummy);//Send dummy byte to read out value ate Address |
ThomasSonderDesign | 10:1f96f0cce2a0 | 363 | //pc.printf("Temp: %X, ", temp); |
ThomasSonderDesign | 9:7c0f42f090e8 | 364 | temp1 = bitDouble(temp); |
ThomasSonderDesign | 9:7c0f42f090e8 | 365 | value[i]= temp1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 366 | //printf(" %X",value[i]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 367 | Address++; |
ThomasSonderDesign | 9:7c0f42f090e8 | 368 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 369 | cs_mem = 1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 370 | return Address; //Return the address of the next unread byte |
ThomasSonderDesign | 9:7c0f42f090e8 | 371 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 372 | |
ThomasSonderDesign | 4:e6e1642724d4 | 373 | |
ThomasSonderDesign | 4:e6e1642724d4 | 374 | /** |
ThomasSonderDesign | 4:e6e1642724d4 | 375 | * receives 64 packets over usb and stores them in memooryBuffer. When memory Buffer is full it is |
ThomasSonderDesign | 4:e6e1642724d4 | 376 | * written to memory at address. This is repeated 336 times to write a whole image to memory. |
ThomasSonderDesign | 4:e6e1642724d4 | 377 | * returns the next availible adsress; |
ThomasSonderDesign | 4:e6e1642724d4 | 378 | */ |
ThomasSonderDesign | 4:e6e1642724d4 | 379 | int writeImage(SPI my_spi,int Address) |
ThomasSonderDesign | 4:e6e1642724d4 | 380 | { |
ThomasSonderDesign | 4:e6e1642724d4 | 381 | Timer t; |
ThomasSonderDesign | 4:e6e1642724d4 | 382 | t.start(); //Start the timer |
ThomasSonderDesign | 5:07113abf18c0 | 383 | pc.printf("\nAddress: %i\n", Address); |
ThomasSonderDesign | 4:e6e1642724d4 | 384 | int USBDataBuffer [64]; //Creat a buffer for recived data |
ThomasSonderDesign | 4:e6e1642724d4 | 385 | char memoryBuffer [bufferSize]; //Create a memory buffer, to be sent to flash |
ThomasSonderDesign | 4:e6e1642724d4 | 386 | int bufferIndex = 0; //points to the next available possition in memory |
ThomasSonderDesign | 4:e6e1642724d4 | 387 | int startAddress = Address; |
ThomasSonderDesign | 9:7c0f42f090e8 | 388 | int imageSize = 310200; //The number of bytes to write, 86272 for 9.8" |
ThomasSonderDesign | 9:7c0f42f090e8 | 389 | while (Address<startAddress+imageSize) { |
ThomasSonderDesign | 4:e6e1642724d4 | 390 | //waitForData(USBDataBuffer); //Waits untill data is recieved on usb, puts it in USBDataBuffer |
ThomasSonderDesign | 4:e6e1642724d4 | 391 | while(bufferIndex<bufferSize) { |
ThomasSonderDesign | 4:e6e1642724d4 | 392 | waitForData(USBDataBuffer); |
ThomasSonderDesign | 9:7c0f42f090e8 | 393 | bufferIndex=appendBuffer(USBDataBuffer, memoryBuffer, bufferIndex); //Appends USBDataBuffer onto memoryBuffer |
ThomasSonderDesign | 4:e6e1642724d4 | 394 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 395 | bufferIndex=0; |
ThomasSonderDesign | 4:e6e1642724d4 | 396 | //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 | 397 | //protected. this erases a block before writung to it if nessisary. |
ThomasSonderDesign | 9:7c0f42f090e8 | 398 | int currentSlot = Address/SLOTSIZE; |
ThomasSonderDesign | 4:e6e1642724d4 | 399 | if (erasedSlots[currentSlot]) { |
ThomasSonderDesign | 4:e6e1642724d4 | 400 | Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize); |
ThomasSonderDesign | 4:e6e1642724d4 | 401 | } else { |
ThomasSonderDesign | 9:7c0f42f090e8 | 402 | pc.printf("\nCan not write to unerased slot. %X", Address); |
ThomasSonderDesign | 5:07113abf18c0 | 403 | Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize); |
ThomasSonderDesign | 9:7c0f42f090e8 | 404 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 405 | |
ThomasSonderDesign | 4:e6e1642724d4 | 406 | } |
ThomasSonderDesign | 4:e6e1642724d4 | 407 | pc.printf("\n pinnished writing"); |
ThomasSonderDesign | 4:e6e1642724d4 | 408 | t.stop(); |
ThomasSonderDesign | 4:e6e1642724d4 | 409 | pc.printf("\nwrite image %i", t.read_ms()); |
ThomasSonderDesign | 4:e6e1642724d4 | 410 | t.reset(); |
ThomasSonderDesign | 4:e6e1642724d4 | 411 | return Address; |
ThomasSonderDesign | 4:e6e1642724d4 | 412 | } |
ThomasSonderDesign | 4:e6e1642724d4 | 413 | |
ThomasSonderDesign | 0:17d169ac117c | 414 | /** |
ThomasSonderDesign | 5:07113abf18c0 | 415 | * Writes a single memorybuffer to memory |
ThomasSonderDesign | 0:17d169ac117c | 416 | * receives 64 packets over usb and stores them in memooryBuffer. When memory Buffer is full it is |
ThomasSonderDesign | 0:17d169ac117c | 417 | * written to memory at address. |
ThomasSonderDesign | 0:17d169ac117c | 418 | * returns the next availible adsress; |
ThomasSonderDesign | 0:17d169ac117c | 419 | */ |
ThomasSonderDesign | 0:17d169ac117c | 420 | int writeFrame(SPI my_spi,int Address) |
ThomasSonderDesign | 0:17d169ac117c | 421 | { |
ThomasSonderDesign | 0:17d169ac117c | 422 | int USBDataBuffer [64]; //Creat a buffer for recived data |
ThomasSonderDesign | 0:17d169ac117c | 423 | char memoryBuffer [bufferSize]; //Create a memory buffer, to be sent to flash |
ThomasSonderDesign | 0:17d169ac117c | 424 | int bufferIndex = 0; //points to the next available possition in memory |
ThomasSonderDesign | 3:d4e1892846fb | 425 | Timer t; |
ThomasSonderDesign | 3:d4e1892846fb | 426 | t.start(); //Start the timer |
ThomasSonderDesign | 0:17d169ac117c | 427 | |
ThomasSonderDesign | 0:17d169ac117c | 428 | waitForData(USBDataBuffer); //Waits untill data is recieved on usb, puts it in USBDataBuffer |
ThomasSonderDesign | 3:d4e1892846fb | 429 | t.stop(); |
ThomasSonderDesign | 4:e6e1642724d4 | 430 | |
ThomasSonderDesign | 0:17d169ac117c | 431 | while(bufferIndex<bufferSize) { |
ThomasSonderDesign | 0:17d169ac117c | 432 | bufferIndex=appendBuffer(USBDataBuffer, memoryBuffer, bufferIndex); //Appends USBDataBuffer onto memoryBuffer |
ThomasSonderDesign | 3:d4e1892846fb | 433 | t.start(); |
ThomasSonderDesign | 0:17d169ac117c | 434 | waitForData(USBDataBuffer); |
ThomasSonderDesign | 3:d4e1892846fb | 435 | t.stop(); |
ThomasSonderDesign | 0:17d169ac117c | 436 | } |
ThomasSonderDesign | 0:17d169ac117c | 437 | |
ThomasSonderDesign | 0:17d169ac117c | 438 | //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 | 439 | //protected. this erases a block before writung to it if nessisary. |
ThomasSonderDesign | 9:7c0f42f090e8 | 440 | int currentSlot = Address/SLOTSIZE; |
ThomasSonderDesign | 2:8b66a3f7e202 | 441 | pc.printf("\nCurrent slot: %i", currentSlot); |
ThomasSonderDesign | 2:8b66a3f7e202 | 442 | if (erasedSlots[currentSlot]) { |
ThomasSonderDesign | 1:1ec8bcd31b27 | 443 | pc.printf("\nNE"); |
ThomasSonderDesign | 0:17d169ac117c | 444 | Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize); |
ThomasSonderDesign | 0:17d169ac117c | 445 | } else { |
ThomasSonderDesign | 2:8b66a3f7e202 | 446 | pc.printf("\nCan not write to unerased slot."); |
ThomasSonderDesign | 4:e6e1642724d4 | 447 | } |
ThomasSonderDesign | 3:d4e1892846fb | 448 | //t.stop(); |
ThomasSonderDesign | 3:d4e1892846fb | 449 | pc.printf("\nWait1 Wate2-4 %i", t.read_ms()); |
ThomasSonderDesign | 3:d4e1892846fb | 450 | t.reset(); |
ThomasSonderDesign | 0:17d169ac117c | 451 | return Address; |
ThomasSonderDesign | 0:17d169ac117c | 452 | } |
ThomasSonderDesign | 0:17d169ac117c | 453 | |
ThomasSonderDesign | 1:1ec8bcd31b27 | 454 | |
ThomasSonderDesign | 1:1ec8bcd31b27 | 455 | /** |
ThomasSonderDesign | 1:1ec8bcd31b27 | 456 | * Sends a report containing a the name of the layout stored at a given slot |
ThomasSonderDesign | 1:1ec8bcd31b27 | 457 | */ |
ThomasSonderDesign | 2:8b66a3f7e202 | 458 | void getLayoutName(int slot) |
ThomasSonderDesign | 1:1ec8bcd31b27 | 459 | { |
ThomasSonderDesign | 1:1ec8bcd31b27 | 460 | int temp [] = {0x31,slot,nameList[slot][0],nameList[slot][1],nameList[slot][2],nameList[slot][3],nameList[slot][4],0}; |
ThomasSonderDesign | 1:1ec8bcd31b27 | 461 | sendUSB(temp); |
ThomasSonderDesign | 8:3577b060d7af | 462 | 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 | 463 | } |
ThomasSonderDesign | 1:1ec8bcd31b27 | 464 | |
ThomasSonderDesign | 1:1ec8bcd31b27 | 465 | |
ThomasSonderDesign | 1:1ec8bcd31b27 | 466 | /** |
ThomasSonderDesign | 1:1ec8bcd31b27 | 467 | * Sends three reports containing the list of layout names stored in memory. |
ThomasSonderDesign | 1:1ec8bcd31b27 | 468 | */ |
ThomasSonderDesign | 1:1ec8bcd31b27 | 469 | void getNameList() |
ThomasSonderDesign | 1:1ec8bcd31b27 | 470 | { |
ThomasSonderDesign | 1:1ec8bcd31b27 | 471 | for(int slot =0; slot< 16; slot++) { |
ThomasSonderDesign | 2:8b66a3f7e202 | 472 | getLayoutName(slot); |
ThomasSonderDesign | 9:7c0f42f090e8 | 473 | } |
ThomasSonderDesign | 1:1ec8bcd31b27 | 474 | } |
ThomasSonderDesign | 0:17d169ac117c | 475 | |
ThomasSonderDesign | 0:17d169ac117c | 476 | /** |
ThomasSonderDesign | 0:17d169ac117c | 477 | * Sends the name of the Current layout |
ThomasSonderDesign | 0:17d169ac117c | 478 | */ |
ThomasSonderDesign | 1:1ec8bcd31b27 | 479 | void getCurrentLayout() |
ThomasSonderDesign | 0:17d169ac117c | 480 | { |
ThomasSonderDesign | 2:8b66a3f7e202 | 481 | getLayoutName(currentName); |
ThomasSonderDesign | 0:17d169ac117c | 482 | } |
ThomasSonderDesign | 0:17d169ac117c | 483 | |
ThomasSonderDesign | 0:17d169ac117c | 484 | /** |
ThomasSonderDesign | 0:17d169ac117c | 485 | * Writes the name of a given layout to the top memory address in its reserved block |
ThomasSonderDesign | 1:1ec8bcd31b27 | 486 | * name[] is a 5 bytes char array with the most significant byte at name[0] |
ThomasSonderDesign | 0:17d169ac117c | 487 | */ |
ThomasSonderDesign | 2:8b66a3f7e202 | 488 | void nameBlock(SPI my_spi, char name[], int slot) |
ThomasSonderDesign | 0:17d169ac117c | 489 | { |
ThomasSonderDesign | 1:1ec8bcd31b27 | 490 | //char temp[]= {name}; |
ThomasSonderDesign | 10:1f96f0cce2a0 | 491 | pc.printf("write name"); |
ThomasSonderDesign | 8:3577b060d7af | 492 | mem.writeData(my_spi, name, slots[slot]+0x1ff00, 5); |
ThomasSonderDesign | 10:1f96f0cce2a0 | 493 | pc.printf("\nName Done"); |
ThomasSonderDesign | 2:8b66a3f7e202 | 494 | } |
ThomasSonderDesign | 2:8b66a3f7e202 | 495 | |
ThomasSonderDesign | 2:8b66a3f7e202 | 496 | /** |
ThomasSonderDesign | 2:8b66a3f7e202 | 497 | * Reads memory to find the names of the layouts stored. puts the names into nameList |
ThomasSonderDesign | 2:8b66a3f7e202 | 498 | */ |
ThomasSonderDesign | 2:8b66a3f7e202 | 499 | void populateNameList(SPI my_spi) |
ThomasSonderDesign | 2:8b66a3f7e202 | 500 | { |
ThomasSonderDesign | 4:e6e1642724d4 | 501 | for(int slot=0; slot<16; slot++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 502 | short name[5]; |
ThomasSonderDesign | 8:3577b060d7af | 503 | 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 | 504 | for( int i = 0; i<5; i++) { |
ThomasSonderDesign | 4:e6e1642724d4 | 505 | nameList[slot][i]=name[i]; |
ThomasSonderDesign | 4:e6e1642724d4 | 506 | } |
ThomasSonderDesign | 4:e6e1642724d4 | 507 | } |
ThomasSonderDesign | 2:8b66a3f7e202 | 508 | } |
ThomasSonderDesign | 2:8b66a3f7e202 | 509 | |
ThomasSonderDesign | 2:8b66a3f7e202 | 510 | /** |
ThomasSonderDesign | 2:8b66a3f7e202 | 511 | * Prepares the memory for a new Layout, writes the layout's name, and erases the blocks |
ThomasSonderDesign | 2:8b66a3f7e202 | 512 | */ |
ThomasSonderDesign | 4:e6e1642724d4 | 513 | void prepImage(SPI my_spi, int slot, char name[]) |
ThomasSonderDesign | 2:8b66a3f7e202 | 514 | { |
ThomasSonderDesign | 10:1f96f0cce2a0 | 515 | pc.printf("\nSlot: %i, %i", slot, slots[slot]); |
ThomasSonderDesign | 4:e6e1642724d4 | 516 | mem.blockErase(my_spi, slots[slot]); //erase the bottom block of the slot |
ThomasSonderDesign | 9:7c0f42f090e8 | 517 | mem.blockErase(my_spi, slots[slot]+0x10000); //erase the middle block of the slot |
ThomasSonderDesign | 9:7c0f42f090e8 | 518 | mem.blockErase(my_spi, slots[slot]+0x20000); //erase the top block of the slot |
ThomasSonderDesign | 9:7c0f42f090e8 | 519 | mem.blockErase(my_spi, slots[slot]+0x30000); //erase the top block of the slot |
ThomasSonderDesign | 9:7c0f42f090e8 | 520 | mem.blockErase(my_spi, slots[slot]+0x40000); //erase the top block of the slot |
ThomasSonderDesign | 8:3577b060d7af | 521 | pc.printf("\nWorking"); |
ThomasSonderDesign | 10:1f96f0cce2a0 | 522 | //nameBlock(my_spi, name, slots[slot]/*+0x1FFF9*/); //Write the name of the layout to memory |
ThomasSonderDesign | 4:e6e1642724d4 | 523 | erasedSlots[slot]=true; //Mark the erased slot as true |
ThomasSonderDesign | 10:1f96f0cce2a0 | 524 | pc.printf("\nFin Working"); |
ThomasSonderDesign | 8:3577b060d7af | 525 | populateNameList(my_spi); |
ThomasSonderDesign | 10:1f96f0cce2a0 | 526 | |
ThomasSonderDesign | 0:17d169ac117c | 527 | } |
ThomasSonderDesign | 0:17d169ac117c | 528 | |
ThomasSonderDesign | 1:1ec8bcd31b27 | 529 | |
ThomasSonderDesign | 1:1ec8bcd31b27 | 530 | /****************************************************************************** |
ThomasSonderDesign | 1:1ec8bcd31b27 | 531 | * Display control funtions |
ThomasSonderDesign | 1:1ec8bcd31b27 | 532 | */ |
ThomasSonderDesign | 1:1ec8bcd31b27 | 533 | |
ThomasSonderDesign | 0:17d169ac117c | 534 | /*###### EPD Set Up ######*/ |
ThomasSonderDesign | 0:17d169ac117c | 535 | //Sets up the EPD spi pins |
ThomasSonderDesign | 0:17d169ac117c | 536 | SPI setupEPD() |
ThomasSonderDesign | 0:17d169ac117c | 537 | { |
ThomasSonderDesign | 0:17d169ac117c | 538 | pc.printf("\nEpd setup"); |
ThomasSonderDesign | 0:17d169ac117c | 539 | /////Setup the spi link with the EDP//// |
ThomasSonderDesign | 9:7c0f42f090e8 | 540 | SPI epd_spi(EPDSPI); //SPI setup: mosi, miso, sclk |
ThomasSonderDesign | 9:7c0f42f090e8 | 541 | // Setup the spi for 16 bit data, low steady state clock, |
ThomasSonderDesign | 0:17d169ac117c | 542 | // second edge capture, with a 5MHz clock rate |
ThomasSonderDesign | 9:7c0f42f090e8 | 543 | epd_spi.format(16,0); |
ThomasSonderDesign | 9:7c0f42f090e8 | 544 | epd_spi.frequency(10000000); |
ThomasSonderDesign | 0:17d169ac117c | 545 | return epd_spi; |
ThomasSonderDesign | 0:17d169ac117c | 546 | } |
ThomasSonderDesign | 0:17d169ac117c | 547 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 548 | /****************************************************************************** |
ThomasSonderDesign | 9:7c0f42f090e8 | 549 | * ITE driver Functions |
ThomasSonderDesign | 9:7c0f42f090e8 | 550 | */ |
ThomasSonderDesign | 9:7c0f42f090e8 | 551 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 552 | /*###### Wait for LCD ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 553 | //delay until the hardware ready pin on LCD is set |
ThomasSonderDesign | 9:7c0f42f090e8 | 554 | void waitForLCD() |
ThomasSonderDesign | 9:7c0f42f090e8 | 555 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 556 | myled=1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 557 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 558 | while(TCbusy==0) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 559 | wait_us(1); |
ThomasSonderDesign | 9:7c0f42f090e8 | 560 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 561 | myled=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 562 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 563 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 564 | /*###### Mirror the bits in a int ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 565 | int reverse(int in) |
ThomasSonderDesign | 9:7c0f42f090e8 | 566 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 567 | int out=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 568 | out|=in&128; |
ThomasSonderDesign | 9:7c0f42f090e8 | 569 | out|=in&64; |
ThomasSonderDesign | 9:7c0f42f090e8 | 570 | out|=in&32; |
ThomasSonderDesign | 9:7c0f42f090e8 | 571 | out|=in&16; |
ThomasSonderDesign | 9:7c0f42f090e8 | 572 | out|=in&8; |
ThomasSonderDesign | 9:7c0f42f090e8 | 573 | out|=in&4; |
ThomasSonderDesign | 9:7c0f42f090e8 | 574 | out|=in&2; |
ThomasSonderDesign | 9:7c0f42f090e8 | 575 | out|=in&1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 576 | return out; |
ThomasSonderDesign | 9:7c0f42f090e8 | 577 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 578 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 579 | /*###### Double the number of bits (up to 16)in an int eg: 1010 => 11001100 ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 580 | /*short bitDouble(int in) |
ThomasSonderDesign | 9:7c0f42f090e8 | 581 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 582 | short out=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 583 | int y = 0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 584 | for(int i =0; i<8; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 585 | y = 1<<i; |
ThomasSonderDesign | 9:7c0f42f090e8 | 586 | if((in&y)>0) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 587 | out=out|(3<<(2*i)); |
ThomasSonderDesign | 9:7c0f42f090e8 | 588 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 589 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 590 | return out; |
ThomasSonderDesign | 9:7c0f42f090e8 | 591 | }*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 592 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 593 | short bitDouble(int in) |
ThomasSonderDesign | 9:7c0f42f090e8 | 594 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 595 | short out=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 596 | //1 |
ThomasSonderDesign | 9:7c0f42f090e8 | 597 | out=out|(3*(in&1)); |
ThomasSonderDesign | 9:7c0f42f090e8 | 598 | //2 |
ThomasSonderDesign | 9:7c0f42f090e8 | 599 | out=out|((in&2)*6); |
ThomasSonderDesign | 9:7c0f42f090e8 | 600 | //4 |
ThomasSonderDesign | 9:7c0f42f090e8 | 601 | out=out|((in&4)*12); |
ThomasSonderDesign | 9:7c0f42f090e8 | 602 | //8 |
ThomasSonderDesign | 9:7c0f42f090e8 | 603 | out=out|((in&8)*24); |
ThomasSonderDesign | 9:7c0f42f090e8 | 604 | //16 |
ThomasSonderDesign | 9:7c0f42f090e8 | 605 | out=out|((in&16)*48); |
ThomasSonderDesign | 9:7c0f42f090e8 | 606 | //32 |
ThomasSonderDesign | 9:7c0f42f090e8 | 607 | out=out|((in&32)*96); |
ThomasSonderDesign | 9:7c0f42f090e8 | 608 | //64 |
ThomasSonderDesign | 9:7c0f42f090e8 | 609 | out=out|((in&64)*192); |
ThomasSonderDesign | 9:7c0f42f090e8 | 610 | //128 |
ThomasSonderDesign | 9:7c0f42f090e8 | 611 | out=out|((in&128)*384); |
ThomasSonderDesign | 9:7c0f42f090e8 | 612 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 613 | return out; |
ThomasSonderDesign | 9:7c0f42f090e8 | 614 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 615 | |
ThomasSonderDesign | 0:17d169ac117c | 616 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 617 | /*###### Send Comands ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 618 | //send a comand to the LCD |
ThomasSonderDesign | 9:7c0f42f090e8 | 619 | void sendComand(SPI my_spi, int comand) |
ThomasSonderDesign | 9:7c0f42f090e8 | 620 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 621 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 622 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 623 | //Send write commandpreamble 0x6000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 624 | my_spi.write(0x6000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 625 | //Send command |
ThomasSonderDesign | 9:7c0f42f090e8 | 626 | my_spi.write(comand); |
ThomasSonderDesign | 9:7c0f42f090e8 | 627 | cs_epd = 1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 628 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 629 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 630 | /*###### Send N Data ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 631 | //send some 16 bit words to the LCD |
ThomasSonderDesign | 9:7c0f42f090e8 | 632 | //data[] is an int array containg wordsN number of words |
ThomasSonderDesign | 9:7c0f42f090e8 | 633 | void sendNData(SPI my_spi,int data[], int wordsN) //for ints |
ThomasSonderDesign | 9:7c0f42f090e8 | 634 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 635 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 636 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 637 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 638 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 639 | //Send data |
ThomasSonderDesign | 9:7c0f42f090e8 | 640 | for (int i =0; i<wordsN; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 641 | my_spi.write(data[i]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 642 | //pc.printf("%x ",data[i]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 643 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 644 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 645 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 646 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 647 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 648 | /*###### Send N Data ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 649 | //send some 16 bit words to the LCD |
ThomasSonderDesign | 9:7c0f42f090e8 | 650 | //data[] is an short array containg wordsN number of 16 bit words |
ThomasSonderDesign | 9:7c0f42f090e8 | 651 | void sendNData(SPI my_spi, short data[], int wordsN) //for shorts |
ThomasSonderDesign | 9:7c0f42f090e8 | 652 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 653 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 654 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 655 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 656 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 657 | //Send data |
ThomasSonderDesign | 9:7c0f42f090e8 | 658 | for (int i =0; i<wordsN; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 659 | my_spi.write(data[i]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 660 | //pc.printf("%x ",data[i]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 661 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 662 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 663 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 664 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 665 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 666 | /////////////////////////////////////////////////Register comands |
ThomasSonderDesign | 9:7c0f42f090e8 | 667 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 668 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 669 | //Read one register value |
ThomasSonderDesign | 9:7c0f42f090e8 | 670 | // reg_Add: address of the register you want to read. |
ThomasSonderDesign | 9:7c0f42f090e8 | 671 | // |
ThomasSonderDesign | 9:7c0f42f090e8 | 672 | int* readReg(SPI my_spi, int reg_Add) |
ThomasSonderDesign | 9:7c0f42f090e8 | 673 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 674 | int myval[3]; //Read data buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 675 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 676 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 677 | //Send write commandpreamble 0x6000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 678 | my_spi.write(0x6000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 679 | //Send read reg command 0x0010 |
ThomasSonderDesign | 9:7c0f42f090e8 | 680 | my_spi.write(0x0010); |
ThomasSonderDesign | 9:7c0f42f090e8 | 681 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 682 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 683 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 684 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 685 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 686 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 687 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 688 | //Send register address 0x1100 |
ThomasSonderDesign | 9:7c0f42f090e8 | 689 | my_spi.write(reg_Add); |
ThomasSonderDesign | 9:7c0f42f090e8 | 690 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 691 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 692 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 693 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 694 | cs_epd=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 695 | //Send read data preamble 0x0000 ?0x0010? |
ThomasSonderDesign | 9:7c0f42f090e8 | 696 | my_spi.write(0x1000);//endian swap |
ThomasSonderDesign | 9:7c0f42f090e8 | 697 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 698 | myval[0]=my_spi.write(00); |
ThomasSonderDesign | 9:7c0f42f090e8 | 699 | myval[1]=my_spi.write(00); |
ThomasSonderDesign | 9:7c0f42f090e8 | 700 | myval[2]=my_spi.write(00); |
ThomasSonderDesign | 9:7c0f42f090e8 | 701 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 702 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 703 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 704 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 705 | pc.printf("\n\rRef:%x %x %x %x",reg_Add,myval[0],myval[1],myval[2]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 706 | //=====Print data======= |
ThomasSonderDesign | 9:7c0f42f090e8 | 707 | return myval; |
ThomasSonderDesign | 9:7c0f42f090e8 | 708 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 709 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 710 | /*###### Write Register ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 711 | //Writes to one register |
ThomasSonderDesign | 9:7c0f42f090e8 | 712 | //wirtes reg_data to the registor at adress reg_Add |
ThomasSonderDesign | 9:7c0f42f090e8 | 713 | void writeReg(SPI my_spi, int reg_Add, int reg_data) |
ThomasSonderDesign | 9:7c0f42f090e8 | 714 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 715 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 716 | //Send write commandpreamble 0x6000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 717 | my_spi.write(0x6000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 718 | //Send write reg command 0x0011 |
ThomasSonderDesign | 9:7c0f42f090e8 | 719 | my_spi.write(0x0011); |
ThomasSonderDesign | 9:7c0f42f090e8 | 720 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 721 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 722 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 723 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 724 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 725 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 726 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 727 | //Send register address |
ThomasSonderDesign | 9:7c0f42f090e8 | 728 | my_spi.write(reg_Add); |
ThomasSonderDesign | 9:7c0f42f090e8 | 729 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 730 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 731 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 732 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 733 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 734 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 735 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 736 | //Send register data |
ThomasSonderDesign | 9:7c0f42f090e8 | 737 | my_spi.write(reg_data); |
ThomasSonderDesign | 9:7c0f42f090e8 | 738 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 739 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 740 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 741 | /*###### Read Status ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 742 | //Reads the staus register of the LCD |
ThomasSonderDesign | 9:7c0f42f090e8 | 743 | //returns 20 16bit words stored in the myval array. |
ThomasSonderDesign | 9:7c0f42f090e8 | 744 | void readStatus(SPI my_spi, int* myval) |
ThomasSonderDesign | 9:7c0f42f090e8 | 745 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 746 | //my_spi.format(16,0); // Setup the spi for 16 bit data, low steady state clock, |
ThomasSonderDesign | 9:7c0f42f090e8 | 747 | //my_spi.frequency(1000000); // second edge capture, with a 5Hz clock rate |
ThomasSonderDesign | 9:7c0f42f090e8 | 748 | //int myval[20]; //Read data buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 749 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 750 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 751 | pc.printf("chec "); |
ThomasSonderDesign | 9:7c0f42f090e8 | 752 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 753 | pc.printf("check1 "); |
ThomasSonderDesign | 9:7c0f42f090e8 | 754 | //Send write commandpreamble 0x6000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 755 | my_spi.write(0x6000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 756 | pc.printf("check2 "); |
ThomasSonderDesign | 9:7c0f42f090e8 | 757 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 758 | //Send read status comand 0x0302 Section 2.1 in programing guide |
ThomasSonderDesign | 9:7c0f42f090e8 | 759 | my_spi.write(0x0302); |
ThomasSonderDesign | 9:7c0f42f090e8 | 760 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 761 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 762 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 763 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 764 | cs_epd=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 765 | //Send read data preamble 0x0000 ?0x0010? |
ThomasSonderDesign | 9:7c0f42f090e8 | 766 | my_spi.write(0x1000);//endian swap |
ThomasSonderDesign | 9:7c0f42f090e8 | 767 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 768 | //Read the result into the data buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 769 | for(int i = 0; i<20; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 770 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 771 | myval[i]=my_spi.write(00); //send Dummy Byte to rede out data |
ThomasSonderDesign | 9:7c0f42f090e8 | 772 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 773 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 774 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 775 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 776 | //=====Print data to the terminal======= |
ThomasSonderDesign | 9:7c0f42f090e8 | 777 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 778 | pc.printf("\n\rStatus: "); |
ThomasSonderDesign | 9:7c0f42f090e8 | 779 | for(int i = 0; i< 20; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 780 | pc.printf("\n\r %x", myval[i]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 781 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 782 | int imHi = myval[3]; |
ThomasSonderDesign | 9:7c0f42f090e8 | 783 | imHi = imHi<<16; |
ThomasSonderDesign | 9:7c0f42f090e8 | 784 | int imaddress = myval[4]|imHi; |
ThomasSonderDesign | 9:7c0f42f090e8 | 785 | pc.printf("Panel(W,H) = (%d,%d)\r\nImage Buffer Address = %X\r\n",myval[1], myval[2],imaddress); |
ThomasSonderDesign | 9:7c0f42f090e8 | 786 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 787 | //pc.printf("Image Buffer Address = %X\r\n",imaddress); |
ThomasSonderDesign | 9:7c0f42f090e8 | 788 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 789 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 790 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 791 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 792 | /*###### Set Image buffer adress ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 793 | //sets the bottom adress where new image buffer will be written to and the display will be read from |
ThomasSonderDesign | 9:7c0f42f090e8 | 794 | //imBufAddress is a 16bit address |
ThomasSonderDesign | 9:7c0f42f090e8 | 795 | void setImBufAddress(SPI my_spi, int imBufAddress) |
ThomasSonderDesign | 9:7c0f42f090e8 | 796 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 797 | int ImBufAdReg = 0x208; //Image buffer maddress register |
ThomasSonderDesign | 9:7c0f42f090e8 | 798 | int addL = imBufAddress&0xffff; |
ThomasSonderDesign | 9:7c0f42f090e8 | 799 | writeReg(my_spi, ImBufAdReg, addL); //Write the low side of the address |
ThomasSonderDesign | 9:7c0f42f090e8 | 800 | int addH = (imBufAddress >> 16)&0xffff; |
ThomasSonderDesign | 9:7c0f42f090e8 | 801 | writeReg(my_spi, ImBufAdReg+2, addH); //Write the high side of the address |
ThomasSonderDesign | 9:7c0f42f090e8 | 802 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 803 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 804 | /*###### Burst write Memory ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 805 | //Fast write comand to send data to the LCD buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 806 | //writeBuffer: short buffer contsaning data to write |
ThomasSonderDesign | 9:7c0f42f090e8 | 807 | //mem_Add: address on the TCON to save the datat to |
ThomasSonderDesign | 9:7c0f42f090e8 | 808 | //write_Size: size of the writeBuffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 809 | void burst_write_Memory(SPI my_spi, short *writeBuffer, int mem_Add, int write_Size) |
ThomasSonderDesign | 9:7c0f42f090e8 | 810 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 811 | sendComand(my_spi,0x0014); //Send bst write |
ThomasSonderDesign | 9:7c0f42f090e8 | 812 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 813 | //int params []= {mem_Add&0xFFFF,(mem_Add>>16)&0xFFFF,write_Size&0xFFFF,(write_Size>>16)&0xFFFF,}; //Split the parameters into 16bit words |
ThomasSonderDesign | 9:7c0f42f090e8 | 814 | //sendNData(params, 4); //Send paramerters |
ThomasSonderDesign | 9:7c0f42f090e8 | 815 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 816 | cs_epd=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 817 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 818 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 819 | my_spi.write(mem_Add&0xFFFF); |
ThomasSonderDesign | 9:7c0f42f090e8 | 820 | cs_epd=1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 821 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 822 | cs_epd=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 823 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 824 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 825 | my_spi.write((mem_Add>>16)&0xFFFF); |
ThomasSonderDesign | 9:7c0f42f090e8 | 826 | cs_epd=1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 827 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 828 | cs_epd=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 829 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 830 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 831 | my_spi.write(write_Size&0xFFFF); |
ThomasSonderDesign | 9:7c0f42f090e8 | 832 | cs_epd=1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 833 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 834 | cs_epd=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 835 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 836 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 837 | my_spi.write((write_Size>>16)&0xFFFF); |
ThomasSonderDesign | 9:7c0f42f090e8 | 838 | cs_epd=1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 839 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 840 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 841 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 842 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 843 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 844 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 845 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 846 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 847 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 848 | //Send data |
ThomasSonderDesign | 9:7c0f42f090e8 | 849 | for(int i = 0; i< 100; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 850 | for(int h = 0; h< write_Size; h++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 851 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 852 | my_spi.write(writeBuffer[h]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 853 | pc.printf("."); |
ThomasSonderDesign | 9:7c0f42f090e8 | 854 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 855 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 856 | sendComand(my_spi, 0x0015); //Send burst access stop comand |
ThomasSonderDesign | 9:7c0f42f090e8 | 857 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 858 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 859 | /*###### Burst Read Memory ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 860 | //Fast write comand to read data from the TCON |
ThomasSonderDesign | 9:7c0f42f090e8 | 861 | //readBuffer: short buffer contsaning data that was read |
ThomasSonderDesign | 9:7c0f42f090e8 | 862 | //mem_Add: address on the TCON to read the data from |
ThomasSonderDesign | 9:7c0f42f090e8 | 863 | //read_Size: size of the readBuffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 864 | void burst_read_Memory(SPI my_spi, int *readBuffer, int mem_Add, int read_Size) |
ThomasSonderDesign | 9:7c0f42f090e8 | 865 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 866 | pc.printf("check1"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 867 | //Send burst reag trigger comand 0x0012 |
ThomasSonderDesign | 9:7c0f42f090e8 | 868 | sendComand(my_spi, 0x0012); // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 869 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 870 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 871 | pc.printf("Check4"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 872 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 873 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 874 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 875 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 876 | //Send memory address low side |
ThomasSonderDesign | 9:7c0f42f090e8 | 877 | my_spi.write(mem_Add); |
ThomasSonderDesign | 9:7c0f42f090e8 | 878 | //Send memory address high side |
ThomasSonderDesign | 9:7c0f42f090e8 | 879 | my_spi.write(mem_Add>>16); |
ThomasSonderDesign | 9:7c0f42f090e8 | 880 | cs_epd = 1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 881 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 882 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 883 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 884 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 885 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 886 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 887 | my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 888 | //Send memory size low side |
ThomasSonderDesign | 9:7c0f42f090e8 | 889 | my_spi.write(read_Size); |
ThomasSonderDesign | 9:7c0f42f090e8 | 890 | //Send memory address high side |
ThomasSonderDesign | 9:7c0f42f090e8 | 891 | my_spi.write(read_Size>>16); |
ThomasSonderDesign | 9:7c0f42f090e8 | 892 | cs_epd = 1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 893 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 894 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 895 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 896 | cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 897 | //Send write commandpreamble 0x6000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 898 | my_spi.write(0x6000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 899 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 900 | //Send burst read start comand 0x0013 |
ThomasSonderDesign | 9:7c0f42f090e8 | 901 | my_spi.write(0x0013); |
ThomasSonderDesign | 9:7c0f42f090e8 | 902 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 903 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 904 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 905 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 906 | cs_epd=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 907 | //Send read data preamble 0x0000 ?0x0010? |
ThomasSonderDesign | 9:7c0f42f090e8 | 908 | my_spi.write(0x1000);//endian swap |
ThomasSonderDesign | 9:7c0f42f090e8 | 909 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 910 | //Read the result into the data buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 911 | for(int i = 0; i<read_Size; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 912 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 913 | readBuffer[i]=my_spi.write(00); //send Dummy Byte to rede out data |
ThomasSonderDesign | 9:7c0f42f090e8 | 914 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 915 | cs_epd = 1; // Bring chip select high to stop writing |
ThomasSonderDesign | 9:7c0f42f090e8 | 916 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 917 | sendComand(my_spi, 0x0015); //Send burst access stop comand |
ThomasSonderDesign | 9:7c0f42f090e8 | 918 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 919 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 920 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 921 | /////////////////////////////////////////////////////////image comands |
ThomasSonderDesign | 9:7c0f42f090e8 | 922 | // |
ThomasSonderDesign | 9:7c0f42f090e8 | 923 | /** |
ThomasSonderDesign | 9:7c0f42f090e8 | 924 | * loads image data into the tcon |
ThomasSonderDesign | 9:7c0f42f090e8 | 925 | * ediantype 0/1 xxxxxxx1 |
ThomasSonderDesign | 9:7c0f42f090e8 | 926 | * pixel format bits per pixel 00=2 11=8 xxxxxx01 |
ThomasSonderDesign | 9:7c0f42f090e8 | 927 | * rotate 0,90, 270 xxxxxx10 |
ThomasSonderDesign | 9:7c0f42f090e8 | 928 | * im_buffer start of the local image buffer. data to send to the display |
ThomasSonderDesign | 9:7c0f42f090e8 | 929 | * imbuffer size, lenght of the buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 930 | * imPosX, imPosY, the position on the display to place the image |
ThomasSonderDesign | 9:7c0f42f090e8 | 931 | * height, width, the size in pixels of the image. |
ThomasSonderDesign | 9:7c0f42f090e8 | 932 | */ |
ThomasSonderDesign | 9:7c0f42f090e8 | 933 | void load_full_image(SPI my_spi, int endianType, int pixFormat, int rotate, short im_buffer[], int imbufferSize, int imPosX, int imPosY, int width, int height) |
ThomasSonderDesign | 9:7c0f42f090e8 | 934 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 935 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 936 | //Write image buffer address |
ThomasSonderDesign | 9:7c0f42f090e8 | 937 | //setImBufAddress(0x3dbe90); //adress from readdata() dat[2] and data[3] |
ThomasSonderDesign | 9:7c0f42f090e8 | 938 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 939 | //pc.printf("\nwidth: %d\nHeight: %d", width, height); |
ThomasSonderDesign | 9:7c0f42f090e8 | 940 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 941 | //Write load image comand 0x0020 |
ThomasSonderDesign | 9:7c0f42f090e8 | 942 | sendComand(my_spi, 0x0020); |
ThomasSonderDesign | 9:7c0f42f090e8 | 943 | int param[] = {(endianType<<8)|(pixFormat<<4)|rotate}; |
ThomasSonderDesign | 9:7c0f42f090e8 | 944 | sendNData(my_spi,param,1); |
ThomasSonderDesign | 9:7c0f42f090e8 | 945 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 946 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 947 | //Write load image AREA comand 0x0021 |
ThomasSonderDesign | 9:7c0f42f090e8 | 948 | //sendComand(0x0021); |
ThomasSonderDesign | 9:7c0f42f090e8 | 949 | //Send load parameters |
ThomasSonderDesign | 9:7c0f42f090e8 | 950 | //int img_param[] = {(endianType<<8)|(pixFormat<<4)|rotate, imPosX, imPosY, width, height}; |
ThomasSonderDesign | 9:7c0f42f090e8 | 951 | //sendNData(img_param,5); |
ThomasSonderDesign | 9:7c0f42f090e8 | 952 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 953 | int count =0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 954 | int line=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 955 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 956 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 957 | //send image data one word at a time |
ThomasSonderDesign | 9:7c0f42f090e8 | 958 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 959 | //cs_epd = 0; // Bring chip select low |
ThomasSonderDesign | 9:7c0f42f090e8 | 960 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 961 | //Send write data preamble 0x0000 |
ThomasSonderDesign | 9:7c0f42f090e8 | 962 | //my_spi.write(0x0000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 963 | //Send data |
ThomasSonderDesign | 9:7c0f42f090e8 | 964 | sendNData(my_spi,im_buffer, imbufferSize); |
ThomasSonderDesign | 9:7c0f42f090e8 | 965 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 966 | cs_epd=1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 967 | //pc.printf("\ncheck1"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 968 | //send load image end coand |
ThomasSonderDesign | 9:7c0f42f090e8 | 969 | sendComand(my_spi, 0x0022); |
ThomasSonderDesign | 9:7c0f42f090e8 | 970 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 971 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 972 | //Display bitmap on EPD |
ThomasSonderDesign | 9:7c0f42f090e8 | 973 | /** |
ThomasSonderDesign | 9:7c0f42f090e8 | 974 | * * xstart, ystart, the position on the display to place the image |
ThomasSonderDesign | 9:7c0f42f090e8 | 975 | * height, width, the size in pixels of the image. |
ThomasSonderDesign | 9:7c0f42f090e8 | 976 | * mode: bits per pixel 0 =2bbp, 1=3bpp, 2=4bpp, 3=5-8bpp |
ThomasSonderDesign | 9:7c0f42f090e8 | 977 | */ |
ThomasSonderDesign | 9:7c0f42f090e8 | 978 | void displayIamge(SPI my_spi, int xstart, int ystart, int height, int width, int mode) |
ThomasSonderDesign | 9:7c0f42f090e8 | 979 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 980 | waitForLCD(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 981 | //Write display image comand 0x0034 |
ThomasSonderDesign | 9:7c0f42f090e8 | 982 | sendComand(my_spi, 0x0034); |
ThomasSonderDesign | 9:7c0f42f090e8 | 983 | //Send parameters |
ThomasSonderDesign | 9:7c0f42f090e8 | 984 | //t.attach_us(&ledon,5000); |
ThomasSonderDesign | 9:7c0f42f090e8 | 985 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 986 | int params[] = {xstart, ystart, width, height, mode}; |
ThomasSonderDesign | 9:7c0f42f090e8 | 987 | trigger = 1; |
ThomasSonderDesign | 9:7c0f42f090e8 | 988 | sendNData(my_spi, params,5); |
ThomasSonderDesign | 9:7c0f42f090e8 | 989 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 990 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 991 | /*###### Write Line ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 992 | //Write a single line To the TCON buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 993 | void writeLine(SPI my_spi, int lineNum, short lineBuffer[], int size)//Writes one line to the display memory. |
ThomasSonderDesign | 9:7c0f42f090e8 | 994 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 995 | int displayAddress = lineNum*0x960+0x3DBE90; //set the address that the line will be stored in |
ThomasSonderDesign | 9:7c0f42f090e8 | 996 | //pc.printf("\n\rline: %X Line Nume: %d", displayAddress, lineNum); |
ThomasSonderDesign | 9:7c0f42f090e8 | 997 | setImBufAddress(my_spi, displayAddress); //0x3DBE90 adress from readdata() dat[2] and data[3] |
ThomasSonderDesign | 9:7c0f42f090e8 | 998 | load_full_image(my_spi, 0,0,0, lineBuffer, size, 0,0, size,1); |
ThomasSonderDesign | 9:7c0f42f090e8 | 999 | //readReg(my_spi, 0x208); //read the image base adress |
ThomasSonderDesign | 9:7c0f42f090e8 | 1000 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 1001 | |
ThomasSonderDesign | 0:17d169ac117c | 1002 | /*###### EPD Write ######*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 1003 | //Update the whole display with data from memory, starting at the |
ThomasSonderDesign | 9:7c0f42f090e8 | 1004 | //address: the adress in eeprom to read from |
ThomasSonderDesign | 0:17d169ac117c | 1005 | int EPD_Write(SPI mem_spi, int address) |
ThomasSonderDesign | 0:17d169ac117c | 1006 | { |
ThomasSonderDesign | 9:7c0f42f090e8 | 1007 | Timer t; |
ThomasSonderDesign | 5:07113abf18c0 | 1008 | pc.printf("\nAddress: %i", address); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1009 | t.start(); //Start the timer |
ThomasSonderDesign | 0:17d169ac117c | 1010 | //Setup the SPIs |
ThomasSonderDesign | 0:17d169ac117c | 1011 | SPI epd_spi = setupEPD(); //Creat a new SPI object to comunicate with the EPD |
ThomasSonderDesign | 9:7c0f42f090e8 | 1012 | int vaule[20]; |
ThomasSonderDesign | 9:7c0f42f090e8 | 1013 | readStatus(epd_spi, vaule); |
ThomasSonderDesign | 0:17d169ac117c | 1014 | |
ThomasSonderDesign | 0:17d169ac117c | 1015 | //Create local variables |
ThomasSonderDesign | 9:7c0f42f090e8 | 1016 | int lineLengh = 300; //the length of the display in 16 bit wordswords, |
ThomasSonderDesign | 9:7c0f42f090e8 | 1017 | int imageLength = 1034; |
ThomasSonderDesign | 9:7c0f42f090e8 | 1018 | short sixtyBytes[lineLengh*2]; //Create a line buffer |
ThomasSonderDesign | 9:7c0f42f090e8 | 1019 | |
ThomasSonderDesign | 0:17d169ac117c | 1020 | //int line = 0; //counter to keep track of the current line |
ThomasSonderDesign | 0:17d169ac117c | 1021 | //int frameLine = 0; //counter to keep track of the line in the current frame |
ThomasSonderDesign | 9:7c0f42f090e8 | 1022 | |
ThomasSonderDesign | 0:17d169ac117c | 1023 | |
ThomasSonderDesign | 0:17d169ac117c | 1024 | //led=!led; |
ThomasSonderDesign | 9:7c0f42f090e8 | 1025 | |
ThomasSonderDesign | 0:17d169ac117c | 1026 | //Begin SPI comunication |
ThomasSonderDesign | 9:7c0f42f090e8 | 1027 | // for old Tcon |
ThomasSonderDesign | 9:7c0f42f090e8 | 1028 | /*cs_epd=1; //EPD chip deselected |
ThomasSonderDesign | 0:17d169ac117c | 1029 | TconEn =1; //Tcon ON set High |
ThomasSonderDesign | 0:17d169ac117c | 1030 | wait_ms(120); //delay 120ms |
ThomasSonderDesign | 0:17d169ac117c | 1031 | cs_epd=0; //Select the Tcon chip |
ThomasSonderDesign | 0:17d169ac117c | 1032 | wait_ms(1); //delay 1ms |
ThomasSonderDesign | 0:17d169ac117c | 1033 | //Write Header |
ThomasSonderDesign | 0:17d169ac117c | 1034 | epd_spi.write(0x06); |
ThomasSonderDesign | 0:17d169ac117c | 1035 | epd_spi.write(0xa0); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1036 | wait_ms(120); //delay 120ms*/ |
ThomasSonderDesign | 0:17d169ac117c | 1037 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1038 | //loop throug all lines lines |
ThomasSonderDesign | 6:2f4a272ab299 | 1039 | for(int j=0; j<imageLength; j++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 1040 | |
ThomasSonderDesign | 5:07113abf18c0 | 1041 | //pc.printf("\n"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1042 | readData(mem_spi, sixtyBytes, address+(j*lineLengh), lineLengh,0); //Read the line, putt them in buffer return the next address to read from. Since mem reads 8bit data, we need double line length for 16bit display |
ThomasSonderDesign | 9:7c0f42f090e8 | 1043 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1044 | //Halve the resolution by doubling bit length |
ThomasSonderDesign | 9:7c0f42f090e8 | 1045 | /*for(int i=0; i<lineLengh; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 1046 | int low = sixtyBytes[i]; |
ThomasSonderDesign | 9:7c0f42f090e8 | 1047 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1048 | low = bitDouble(low); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1049 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1050 | sixtyBytes[i]=(low); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1051 | }*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 1052 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1053 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1054 | //pc.printf("ÿup"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1055 | writeLine(epd_spi,j,sixtyBytes,lineLengh); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1056 | if(j%100==0) { |
ThomasSonderDesign | 7:b30c411a6d36 | 1057 | keyCheck(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1058 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 1059 | |
ThomasSonderDesign | 0:17d169ac117c | 1060 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 1061 | t.stop(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1062 | displayIamge(epd_spi,0,0,1034,2400,2); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1063 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1064 | for(int i = 0; i<1034; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 1065 | readData(mem_spi, sixtyBytes, address+(i*lineLengh), lineLengh-250,0); //Read the line, putt them in buffer return the next address to read from. Since mem reads 8bit data, we need double line length for 16bit display |
ThomasSonderDesign | 9:7c0f42f090e8 | 1066 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1067 | //Halve the resolution by doubling bit length |
ThomasSonderDesign | 9:7c0f42f090e8 | 1068 | /*for(int i=0; i<lineLengh; i++) { |
ThomasSonderDesign | 9:7c0f42f090e8 | 1069 | int low = sixtyBytes[i]; |
ThomasSonderDesign | 9:7c0f42f090e8 | 1070 | low = bitDouble(low); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1071 | sixtyBytes[i]=(low); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1072 | }*/ |
ThomasSonderDesign | 9:7c0f42f090e8 | 1073 | writeLine(epd_spi,i,sixtyBytes,lineLengh-250); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1074 | } |
ThomasSonderDesign | 9:7c0f42f090e8 | 1075 | displayIamge(epd_spi,0,0,1034,400,2); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1076 | |
ThomasSonderDesign | 0:17d169ac117c | 1077 | |
ThomasSonderDesign | 7:b30c411a6d36 | 1078 | printf("\ntime = %i ", t.read_ms()); |
ThomasSonderDesign | 7:b30c411a6d36 | 1079 | t.reset(); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1080 | printf("\ndone = "); |
ThomasSonderDesign | 0:17d169ac117c | 1081 | return 1; |
ThomasSonderDesign | 0:17d169ac117c | 1082 | } |
ThomasSonderDesign | 0:17d169ac117c | 1083 | |
ThomasSonderDesign | 2:8b66a3f7e202 | 1084 | /*###### EPD Write ######*/ |
ThomasSonderDesign | 2:8b66a3f7e202 | 1085 | //Update the whole display with data from a reserved slot |
ThomasSonderDesign | 2:8b66a3f7e202 | 1086 | int EPD_Swap(SPI mem_spi, int slot) |
ThomasSonderDesign | 2:8b66a3f7e202 | 1087 | { |
ThomasSonderDesign | 10:1f96f0cce2a0 | 1088 | pc.printf("\nslots: %i, %X", slot,slots[slot]); |
ThomasSonderDesign | 2:8b66a3f7e202 | 1089 | return EPD_Write(mem_spi, slots[slot]); |
ThomasSonderDesign | 2:8b66a3f7e202 | 1090 | } |
ThomasSonderDesign | 0:17d169ac117c | 1091 | /******************************************************* |
ThomasSonderDesign | 0:17d169ac117c | 1092 | * Main Function |
ThomasSonderDesign | 0:17d169ac117c | 1093 | */ |
ThomasSonderDesign | 0:17d169ac117c | 1094 | int main() |
ThomasSonderDesign | 0:17d169ac117c | 1095 | { |
ThomasSonderDesign | 1:1ec8bcd31b27 | 1096 | Timer t; |
ThomasSonderDesign | 1:1ec8bcd31b27 | 1097 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1098 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1099 | pc.baud(115200); |
ThomasSonderDesign | 0:17d169ac117c | 1100 | int USBDataBuffer[64]; |
ThomasSonderDesign | 9:7c0f42f090e8 | 1101 | SPI mem_spi = setupSPI(); //Creates an SPI object to comunicate with the external memory |
ThomasSonderDesign | 9:7c0f42f090e8 | 1102 | populateNameList(mem_spi); //Reads the names of layouts stored in external memory into RAM |
ThomasSonderDesign | 4:e6e1642724d4 | 1103 | |
ThomasSonderDesign | 0:17d169ac117c | 1104 | while(1) { |
ThomasSonderDesign | 8:3577b060d7af | 1105 | //pc.printf("Loop"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1106 | trigger=0; |
ThomasSonderDesign | 9:7c0f42f090e8 | 1107 | |
ThomasSonderDesign | 9:7c0f42f090e8 | 1108 | sendUSB(readyForComand); //Tell pc that read for data |
ThomasSonderDesign | 9:7c0f42f090e8 | 1109 | keyCheck(); //Scan the key matrix for key press and send to the pc |
ThomasSonderDesign | 10:1f96f0cce2a0 | 1110 | pc.printf("."); |
ThomasSonderDesign | 0:17d169ac117c | 1111 | sendUSB(readyForComand); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1112 | if (readUSB(USBDataBuffer)>0) { //If a coamd received |
ThomasSonderDesign | 4:e6e1642724d4 | 1113 | pc.printf("\nSwitch %i",USBDataBuffer[1]); |
ThomasSonderDesign | 3:d4e1892846fb | 1114 | char temp[] = {USBDataBuffer[3],USBDataBuffer[4],USBDataBuffer[5],USBDataBuffer[6],USBDataBuffer[7]}; |
ThomasSonderDesign | 0:17d169ac117c | 1115 | switch(USBDataBuffer[1]) { |
ThomasSonderDesign | 0:17d169ac117c | 1116 | case 0x10: //If the recieved data is a write instruction |
ThomasSonderDesign | 9:7c0f42f090e8 | 1117 | GlobalAddress = writeFrame(mem_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 | 1118 | break; |
ThomasSonderDesign | 3:d4e1892846fb | 1119 | case 0x11: |
ThomasSonderDesign | 5:07113abf18c0 | 1120 | pc.printf("\nPrep write"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1121 | prepImage(mem_spi, USBDataBuffer[2], temp);//Prepare a slot in memory for an image |
ThomasSonderDesign | 4:e6e1642724d4 | 1122 | break; |
ThomasSonderDesign | 4:e6e1642724d4 | 1123 | case 0x12: |
ThomasSonderDesign | 5:07113abf18c0 | 1124 | pc.printf("\nImage write"); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1125 | writeImage(mem_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 | 1126 | break; |
ThomasSonderDesign | 0:17d169ac117c | 1127 | case 0x20: //If the recieved comand is a read instruction |
ThomasSonderDesign | 9:7c0f42f090e8 | 1128 | //mem.readData(mem_spi, memoryBuffer, USBDataBuffer[2], USBDataBuffer[3]);//read(spi, destination[], address, length) |
ThomasSonderDesign | 1:1ec8bcd31b27 | 1129 | //pc.printf(" \n---EPD UPDATE--- %i",USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1130 | EPD_Write(mem_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]); |
ThomasSonderDesign | 0:17d169ac117c | 1131 | break; |
ThomasSonderDesign | 8:3577b060d7af | 1132 | case 0x21: //If the recieved comand is a swap instruction |
ThomasSonderDesign | 9:7c0f42f090e8 | 1133 | EPD_Swap(mem_spi, USBDataBuffer[2]); |
ThomasSonderDesign | 9:7c0f42f090e8 | 1134 | break; |
ThomasSonderDesign | 0:17d169ac117c | 1135 | case 0x30: //If the recieved comand is a request for the cutrrent name |
ThomasSonderDesign | 9:7c0f42f090e8 | 1136 | populateNameList(mem_spi); |
ThomasSonderDesign | 8:3577b060d7af | 1137 | getNameList(); |
ThomasSonderDesign | 0:17d169ac117c | 1138 | break; |
ThomasSonderDesign | 9:7c0f42f090e8 | 1139 | case 0x35: //If the recieved comand is a request for all the names |
ThomasSonderDesign | 0:17d169ac117c | 1140 | getCurrentLayout(); |
ThomasSonderDesign | 0:17d169ac117c | 1141 | break; |
ThomasSonderDesign | 0:17d169ac117c | 1142 | default: |
ThomasSonderDesign | 4:e6e1642724d4 | 1143 | 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 | 1144 | } |
ThomasSonderDesign | 0:17d169ac117c | 1145 | } |
ThomasSonderDesign | 1:1ec8bcd31b27 | 1146 | |
ThomasSonderDesign | 3:d4e1892846fb | 1147 | //pc.printf("\n%d", t.read_us()); |
ThomasSonderDesign | 1:1ec8bcd31b27 | 1148 | t.reset(); |
ThomasSonderDesign | 0:17d169ac117c | 1149 | } |
ThomasSonderDesign | 0:17d169ac117c | 1150 | } |