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

Revision:
9:7c0f42f090e8
Parent:
8:3577b060d7af
Child:
10:1f96f0cce2a0
--- a/BB_Basic.cpp	Tue Feb 14 00:31:24 2017 +0000
+++ b/BB_Basic.cpp	Sun Apr 15 23:27:26 2018 +0000
@@ -13,18 +13,25 @@
 #include "Memory.h"
 #include "VKCodes.h"
 
+//Spi port for epd. on black board P0_21, P1_21, P1_20. on QSB P0_21,P0_22,P1_15
+#define EPDSPI P0_21,P0_22,P1_15
+//Spi port for memory. on black board P0_9,P0_8,P0_10, P1_20. on QSB P0_9,P0_8,P0_10
+#define MEMSPI P0_9,P0_8,P0_10
+#define SLOTSIZE 0x50000
 /******************************************************************************
  * Definitiontions
  */
 //Constants for black board
 
-DigitalOut cs_mem(P0_2);        //Set up memory's Chip Select pin
-DigitalOut cs_epd(P1_23);       //Set up epd's Chip Select pin
+DigitalOut cs_mem(P0_16);        //Set up memory's Chip Select pin               BB:P0_2 QSB:P0_16           spi0
+DigitalOut cs_epd(P0_11);       //Set up epd's Chip Select pin                  BB:P1_23 QSB:P0_11          spi1
 DigitalOut TconEn(P0_5);        //Tcon Enable pin, open drain must invert
 DigitalOut start(P0_14);
-DigitalIn TCbusy(P0_4);           //Tcon busy line
+DigitalIn TCbusy(P0_4);           //Tcon busy line                              P0_4
 
-DigitalOut myled(P0_9);
+DigitalOut trigger(P1_19);
+DigitalOut myled(P0_23);
+
 DigitalOut SRclk(P0_15);        //Shift register clk signal
 DigitalOut SRlat (P1_22);       //Shift register output latch
 DigitalOut SRds(P1_14);         //Shiftreg data pin
@@ -35,12 +42,12 @@
 DigitalIn B(P1_31);             //Definne row row D input pin
 DigitalIn A(P1_29);             //Definne row row E input pin
 
-DigitalIn modC(P0_11);          //Definne row mod C input pin
+DigitalIn modC(P0_1);          //Definne row mod C input pin                    BB:P0_11 QSB:P0_1
 DigitalIn modB(P0_12);          //Definne row mod B input pin
 DigitalIn modA(P0_13);          //Definne row mod A input pin
 
 Serial pc (P0_19,P0_18);        //Setup a real serial port
-Memory mem(P0_2);               //Create a new memory manager with chip select on P0_2
+Memory mem(P0_16);               //Create a new memory manager with chip select on P0_2
 
 USBHID hid(64, 8,0x1234,0x3241); //Create a HID conection with and 64 byte input and 8 byte output report, PID0x1234 VID 3241
 
@@ -59,8 +66,8 @@
 char nameList[16][5];                  //A list of all the Layouts in memory, each name is 5 bytes long
 
 //Locations in memory were the 16 layouts can be stored
-const int slots [] = {0,0x20000, 0x40000,0x60000, 0x80000, 0xa0000, 0xc0000, 0xe0000, 0x100000, 0x120000,0x140000,0x160000,0x180000, 0x1a0000,0x1e0000};
-bool erasedSlots[]= {true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
+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};
+bool erasedSlots[]= {true,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
 
 int keybuffer[10][2];   //A buffer for storing key presses (mod key plus key)
 
@@ -72,6 +79,10 @@
     {VK_F1, VK_F2,VK_F3,VK_F4, VK_SPACE, VK_TAB, VK_BACK, VK_RSHIFT,VK_LWIN,VK_RMENU}
 };
 
+/******************************************************************************
+ * prototype Functions
+ */
+short bitDouble(int);
 
 /******************************************************************************
  * Set up Functions
@@ -94,10 +105,10 @@
  */
 SPI setupSPI()
 {
-    SPI my_spi(P0_9,P0_8,P0_10);    // mosi, miso, sclk
+    SPI my_spi(MEMSPI);    // mosi, miso, sclk
     //cs_mem = 1;                         // Chip must be deselected
     my_spi.format(8,3);             // Setup the spi for 8 bit data, low steady state clock,
-    my_spi.frequency(1000000);      // second edge capture, with a 1MHz clock rate, Will work up to 20MHz
+    my_spi.frequency(10000000);      // second edge capture, with a 1MHz clock rate, Will work up to 20MHz
     return my_spi;
 }
 
@@ -275,19 +286,20 @@
 /**
  * keyCheck(), scanss the keys and sends the keypress to the pc over usb
  */
-void keyCheck(){
+void keyCheck()
+{
     //pc.printf("KeyCheck\n");
     if (keyScan()>0) {                  //check if keyScan returned key presss
-            pc.printf("\nKey scan|n");
-            int countpos =0;
-            while(keybuffer[countpos][1]>0) {   //While there are keypresses in the buffer
-                pc.printf("%i",keybuffer[countpos][1]);
-                pc.printf(" ");
-                sendKey(keybuffer[countpos][0],keybuffer[countpos][1]);//Send key press
-                countpos++;
-                myled=0;
-            }
+        //pc.printf("\nKey scan|n");
+        int countpos =0;
+        while(keybuffer[countpos][1]>0) {   //While there are keypresses in the buffer
+            pc.printf("%i",keybuffer[countpos][1]);
+            pc.printf(" ");
+            sendKey(keybuffer[countpos][0],keybuffer[countpos][1]);//Send key press
+            countpos++;
+            myled=0;
         }
+    }
 }
 
 /******************************************************************************
@@ -304,7 +316,7 @@
     //printf("\n");
     int posIndex=0;
     for(int i = 0; i < 64; i++) {
-            
+
         memoryBuffer[bufferIndex+i]=USBDataBuffer[i];
         posIndex++;
     }
@@ -323,6 +335,39 @@
     }
 }
 
+/**
+ * Reads 'length' elements into a char array starting at the 24bit Address.
+ *If length is greater than BufferSize (3840 bytes) the function will terminate
+ *and return the start address.
+ */
+int readData(SPI my_spi, short value [], int Address, int length, int doubleSelect)
+{
+    //if(length>bufferSize) {
+    //printf("\nLength %i exceeds Max Length\n",length);
+    //return Address;
+    //}
+    int temp = 0;
+    short temp1 = 0;
+    cs_mem = 1;             //Ensure cs is deselected
+    //wait_us(10);
+    cs_mem = 0;                     //memory is selected
+    my_spi.write(0x03);         //Send read command
+    my_spi.write(Address>>16);  //Send high address byte
+    my_spi.write(Address>>8);   //Send mid address byte
+    my_spi.write(Address);      //Send low address byte
+
+
+    for(int i =0; i <length; i++) {
+        temp = my_spi.write(dummy);//Send dummy byte to read out value ate Address
+        temp1 = bitDouble(temp);
+        value[i]= temp1;
+        //printf(" %X",value[i]);
+        Address++;
+    }
+    cs_mem = 1;
+    return Address;     //Return the address of the next unread byte
+}
+
 
 /**
  * receives 64 packets over usb and stores them in memooryBuffer. When memory Buffer is full it is
@@ -338,23 +383,24 @@
     char memoryBuffer [bufferSize];     //Create a memory buffer, to be sent to flash
     int bufferIndex = 0;                //points to the next available possition in memory
     int startAddress = Address;
-    while (Address<startAddress+86272) {
+    int imageSize = 310200;              //The number of bytes to write,  86272 for 9.8"
+    while (Address<startAddress+imageSize) {
         //waitForData(USBDataBuffer);         //Waits untill data is recieved on usb, puts it in USBDataBuffer
         while(bufferIndex<bufferSize) {
             waitForData(USBDataBuffer);
-                bufferIndex=appendBuffer(USBDataBuffer, memoryBuffer, bufferIndex);     //Appends USBDataBuffer onto memoryBuffer
+            bufferIndex=appendBuffer(USBDataBuffer, memoryBuffer, bufferIndex);     //Appends USBDataBuffer onto memoryBuffer
         }
-        bufferIndex=0;          
-
+        bufferIndex=0;
         //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
         //protected. this erases a block before writung to it if nessisary.
-        int currentSlot = Address/0x20000;
+        int currentSlot = Address/SLOTSIZE;
         if (erasedSlots[currentSlot]) {
             Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
         } else {
-            pc.printf("\nCan not write to unerased slot.");
+            pc.printf("\nCan not write to unerased slot. %X", Address);
             Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
-        }        
+        }
+
     }
     pc.printf("\n pinnished writing");
     t.stop();
@@ -389,7 +435,7 @@
 
     //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
     //protected. this erases a block before writung to it if nessisary.
-    int currentSlot = Address/0x20000;
+    int currentSlot = Address/SLOTSIZE;
     pc.printf("\nCurrent slot: %i", currentSlot);
     if (erasedSlots[currentSlot]) {
         pc.printf("\nNE");
@@ -422,7 +468,7 @@
 {
     for(int slot =0; slot< 16; slot++) {
         getLayoutName(slot);
-        }
+    }
 }
 
 /**
@@ -449,7 +495,7 @@
 void populateNameList(SPI my_spi)
 {
     for(int slot=0; slot<16; slot++) {
-        char name[5];
+        short name[5];
         mem.readData(my_spi, name, slots[slot]+0x1FF00, 5); //Read five bytes from the end of a slot into the name array
         for( int i = 0; i<5; i++) {
             nameList[slot][i]=name[i];
@@ -464,7 +510,10 @@
 {
     pc.printf("\nSlot: %i", slots[slot]);
     mem.blockErase(my_spi, slots[slot]);           //erase the bottom block of the slot
-    mem.blockErase(my_spi, slots[slot]+0x10000);   //erase the top block of the slot
+    mem.blockErase(my_spi, slots[slot]+0x10000);   //erase the middle block of the slot
+    mem.blockErase(my_spi, slots[slot]+0x20000);   //erase the top block of the slot
+    mem.blockErase(my_spi, slots[slot]+0x30000);   //erase the top block of the slot
+    mem.blockErase(my_spi, slots[slot]+0x40000);   //erase the top block of the slot
     pc.printf("\nWorking");
     nameBlock(my_spi, name,  slots[slot]/*+0x1FFF9*/); //Write the name of the layout to memory
     erasedSlots[slot]=true;                        //Mark the erased slot as true
@@ -482,36 +531,495 @@
 {
     pc.printf("\nEpd setup");
     /////Setup the spi link with the EDP////
-    SPI epd_spi(P0_21, P1_21, P1_20);    //SPI setup: mosi, miso, sclk
-    // Setup the spi for 8 bit data, high steady state clock,
+    SPI epd_spi(EPDSPI);    //SPI setup: mosi, miso, sclk
+    // Setup the spi for 16 bit data, low steady state clock,
     // second edge capture, with a 5MHz clock rate
-    epd_spi.format(8,3);
-    epd_spi.frequency(5000000);
+    epd_spi.format(16,0);
+    epd_spi.frequency(10000000);
     return epd_spi;
 }
 
+/******************************************************************************
+ * ITE driver Functions
+ */
+
+/*###### Wait for LCD ######*/
+//delay until the hardware ready pin on LCD is set
+void waitForLCD()
+{
+    myled=1;
+
+    while(TCbusy==0) {
+        wait_us(1);
+    }
+    myled=0;
+}
+
+/*###### Mirror the bits in a int ######*/
+int reverse(int in)
+{
+    int out=0;
+    out|=in&128;
+    out|=in&64;
+    out|=in&32;
+    out|=in&16;
+    out|=in&8;
+    out|=in&4;
+    out|=in&2;
+    out|=in&1;
+    return out;
+}
+
+/*###### Double the number of bits (up to 16)in an int eg: 1010 => 11001100 ######*/
+/*short bitDouble(int in)
+{
+    short out=0;
+    int y = 0;
+    for(int i =0; i<8; i++) {
+         y = 1<<i;
+        if((in&y)>0) {
+        out=out|(3<<(2*i));
+        }
+    }
+    return out;
+}*/
+
+short bitDouble(int in)
+{
+    short out=0;
+    //1
+    out=out|(3*(in&1));
+    //2
+    out=out|((in&2)*6);
+    //4
+    out=out|((in&4)*12);
+    //8
+    out=out|((in&8)*24);
+    //16
+    out=out|((in&16)*48);
+    //32
+    out=out|((in&32)*96);
+    //64
+    out=out|((in&64)*192);
+    //128
+    out=out|((in&128)*384);
+
+    return out;
+}
+
 
+/*###### Send Comands ######*/
+//send a comand to the LCD
+void sendComand(SPI my_spi, int comand)
+{
+    waitForLCD();
+    cs_epd = 0;                     // Bring chip select low
+    //Send write commandpreamble  0x6000
+    my_spi.write(0x6000);
+    //Send   command
+    my_spi.write(comand);
+    cs_epd = 1;
+}
+
+/*###### Send N Data ######*/
+//send some 16 bit words to the LCD
+//data[] is an int array containg wordsN number of words
+void sendNData(SPI my_spi,int data[], int wordsN) //for ints
+{
+    waitForLCD();
+    cs_epd = 0;                     // Bring chip select low
+    //Send write data preamble  0x0000
+    my_spi.write(0x0000);
+    //Send data
+    for (int i =0; i<wordsN; i++) {
+        my_spi.write(data[i]);
+        //pc.printf("%x ",data[i]);
+        waitForLCD();
+    }
+    cs_epd = 1;                     // Bring chip select high to stop writing
+}
+
+/*###### Send N Data ######*/
+//send some 16 bit words to the LCD
+//data[] is an short array containg wordsN number of 16 bit words
+void sendNData(SPI my_spi, short data[], int wordsN) //for shorts
+{
+    waitForLCD();
+    cs_epd = 0;                     // Bring chip select low
+    //Send write data preamble  0x0000
+    my_spi.write(0x0000);
+    //Send data
+    for (int i =0; i<wordsN; i++) {
+        my_spi.write(data[i]);
+        //pc.printf("%x ",data[i]);
+        waitForLCD();
+    }
+    cs_epd = 1;                     // Bring chip select high to stop writing
+}
+
+/////////////////////////////////////////////////Register comands
+
+
+//Read one register value
+// reg_Add: address of the register you want to read.
+//
+int* readReg(SPI my_spi, int reg_Add)
+{
+    int myval[3];               //Read data buffer
+
+    cs_epd = 0;                     // Bring chip select low
+    //Send write commandpreamble  0x6000
+    my_spi.write(0x6000);
+    //Send read reg command 0x0010
+    my_spi.write(0x0010);
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+    waitForLCD();
+
+    cs_epd = 0;                     // Bring chip select low
+    //Send write data preamble  0x0000
+    my_spi.write(0x0000);
+    //Send register address  0x1100
+    my_spi.write(reg_Add);
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+    waitForLCD();
+
+    cs_epd=0;
+    //Send read data preamble  0x0000 ?0x0010?
+    my_spi.write(0x1000);//endian swap
+
+    myval[0]=my_spi.write(00);
+    myval[1]=my_spi.write(00);
+    myval[2]=my_spi.write(00);
+
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+
+    pc.printf("\n\rRef:%x %x %x %x",reg_Add,myval[0],myval[1],myval[2]);
+    //=====Print data=======
+    return myval;
+}
+
+/*###### Write Register ######*/
+//Writes to one register
+//wirtes reg_data to the registor at adress reg_Add
+void writeReg(SPI my_spi, int reg_Add, int reg_data)
+{
+    cs_epd = 0;                     // Bring chip select low
+    //Send write commandpreamble  0x6000
+    my_spi.write(0x6000);
+    //Send write reg command 0x0011
+    my_spi.write(0x0011);
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+    waitForLCD();
+
+    cs_epd = 0;                     // Bring chip select low
+    //Send write data preamble  0x0000
+    my_spi.write(0x0000);
+    //Send register address
+    my_spi.write(reg_Add);
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+    waitForLCD();
+
+    cs_epd = 0;                     // Bring chip select low
+    //Send write data preamble  0x0000
+    my_spi.write(0x0000);
+    //Send register data
+    my_spi.write(reg_data);
+    cs_epd = 1;                     // Bring chip select high to stop writing
+}
+
+/*###### Read Status ######*/
+//Reads the staus register of the LCD
+//returns 20 16bit words stored in the myval array.
+void readStatus(SPI my_spi, int* myval)
+{
+    //my_spi.format(16,0);            // Setup the spi for 16 bit data, low steady state clock,
+    //my_spi.frequency(1000000);      // second edge capture, with a 5Hz clock rate
+    //int myval[20];               //Read data buffer
+
+    cs_epd = 0;                     // Bring chip select low
+    pc.printf("chec ");
+    waitForLCD();
+    pc.printf("check1 ");
+    //Send write commandpreamble  0x6000
+    my_spi.write(0x6000);
+    pc.printf("check2 ");
+    waitForLCD();
+    //Send read status comand 0x0302 Section 2.1 in programing guide
+    my_spi.write(0x0302);
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+    waitForLCD();
+
+    cs_epd=0;
+    //Send read data preamble  0x0000 ?0x0010?
+    my_spi.write(0x1000);//endian swap
+    waitForLCD();
+    //Read the result into the data buffer
+    for(int i = 0; i<20; i++) {
+        waitForLCD();
+        myval[i]=my_spi.write(00);      //send Dummy Byte to rede out data
+    }
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+
+    //=====Print data to the terminal=======
+
+    pc.printf("\n\rStatus: ");
+    for(int i = 0; i< 20; i++) {
+        pc.printf("\n\r %x", myval[i]);
+    }
+    int imHi = myval[3];
+    imHi = imHi<<16;
+    int imaddress = myval[4]|imHi;
+    pc.printf("Panel(W,H) = (%d,%d)\r\nImage Buffer Address = %X\r\n",myval[1], myval[2],imaddress);
+
+    //pc.printf("Image Buffer Address = %X\r\n",imaddress);
+
+
+}
+
+/*###### Set Image buffer adress ######*/
+//sets the bottom adress where new image buffer will be written to and the display will be read from
+//imBufAddress is a 16bit address
+void setImBufAddress(SPI my_spi, int imBufAddress)
+{
+    int ImBufAdReg = 0x208;     //Image buffer maddress register
+    int addL = imBufAddress&0xffff;
+    writeReg(my_spi, ImBufAdReg, addL);   //Write the low side of the address
+    int addH = (imBufAddress >> 16)&0xffff;
+    writeReg(my_spi, ImBufAdReg+2, addH);   //Write the high side of the address
+}
+
+/*###### Burst write Memory ######*/
+//Fast write comand to send data to the LCD buffer
+//writeBuffer: short buffer contsaning data to write
+//mem_Add: address on the TCON to save the datat to
+//write_Size: size of the writeBuffer
+void burst_write_Memory(SPI my_spi, short *writeBuffer, int mem_Add, int write_Size)
+{
+    sendComand(my_spi,0x0014); //Send bst write
+
+    //int params []= {mem_Add&0xFFFF,(mem_Add>>16)&0xFFFF,write_Size&0xFFFF,(write_Size>>16)&0xFFFF,}; //Split the parameters into 16bit words
+    //sendNData(params, 4); //Send paramerters
+    waitForLCD();
+    cs_epd=0;
+    my_spi.write(0x0000);
+    waitForLCD();
+    my_spi.write(mem_Add&0xFFFF);
+    cs_epd=1;
+    waitForLCD();
+    cs_epd=0;
+    my_spi.write(0x0000);
+    waitForLCD();
+    my_spi.write((mem_Add>>16)&0xFFFF);
+    cs_epd=1;
+    waitForLCD();
+    cs_epd=0;
+    my_spi.write(0x0000);
+    waitForLCD();
+    my_spi.write(write_Size&0xFFFF);
+    cs_epd=1;
+    waitForLCD();
+    cs_epd=0;
+    my_spi.write(0x0000);
+    waitForLCD();
+    my_spi.write((write_Size>>16)&0xFFFF);
+    cs_epd=1;
+
+
+
+
+    waitForLCD();
+    cs_epd = 0;                     // Bring chip select low
+
+    //Send write data preamble  0x0000
+    my_spi.write(0x0000);
+    //Send data
+    for(int i = 0; i< 100; i++) {
+        for(int h = 0; h< write_Size; h++) {
+            waitForLCD();
+            my_spi.write(writeBuffer[h]);
+            pc.printf(".");
+        }
+    }
+    sendComand(my_spi, 0x0015); //Send burst access stop comand
+}
+
+/*###### Burst Read Memory ######*/
+//Fast write comand to read data from the TCON
+//readBuffer: short buffer contsaning data that was read
+//mem_Add: address on the TCON to read the data from
+//read_Size: size of the readBuffer
+void burst_read_Memory(SPI my_spi, int *readBuffer, int mem_Add, int read_Size)
+{
+    pc.printf("check1");
+    //Send burst reag trigger comand 0x0012
+    sendComand(my_spi, 0x0012);                // Bring chip select high to stop writing
+
+    waitForLCD();
+    pc.printf("Check4");
+
+    cs_epd = 0;                     // Bring chip select low
+    //Send write data preamble  0x0000
+    my_spi.write(0x0000);
+    //Send memory address  low side
+    my_spi.write(mem_Add);
+    //Send memory address  high side
+    my_spi.write(mem_Add>>16);
+    cs_epd = 1;
+
+
+    waitForLCD();
+
+    cs_epd = 0;                     // Bring chip select low
+    //Send write data preamble  0x0000
+    my_spi.write(0x0000);
+    //Send memory size  low side
+    my_spi.write(read_Size);
+    //Send memory address  high side
+    my_spi.write(read_Size>>16);
+    cs_epd = 1;
+
+    waitForLCD();
+
+    cs_epd = 0;                     // Bring chip select low
+    //Send write commandpreamble  0x6000
+    my_spi.write(0x6000);
+    waitForLCD();
+    //Send burst read start comand 0x0013
+    my_spi.write(0x0013);
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+    waitForLCD();
+
+    cs_epd=0;
+    //Send read data preamble  0x0000 ?0x0010?
+    my_spi.write(0x1000);//endian swap
+    waitForLCD();
+    //Read the result into the data buffer
+    for(int i = 0; i<read_Size; i++) {
+        waitForLCD();
+        readBuffer[i]=my_spi.write(00);      //send Dummy Byte to rede out data
+    }
+    cs_epd = 1;                     // Bring chip select high to stop writing
+
+    sendComand(my_spi, 0x0015); //Send burst access stop comand
+
+}
+
+/////////////////////////////////////////////////////////image comands
+//
+/**
+ * loads image data into the tcon
+ * ediantype 0/1 xxxxxxx1
+ * pixel format bits per pixel 00=2 11=8 xxxxxx01
+ * rotate 0,90, 270 xxxxxx10
+ * im_buffer start of the local image buffer. data to send to the display
+ * imbuffer size, lenght of the buffer
+ * imPosX, imPosY, the position on the display to place the image
+ * height, width, the size in pixels of the image.
+ */
+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)
+{
+
+    //Write image buffer address
+    //setImBufAddress(0x3dbe90);   //adress from readdata() dat[2] and data[3]
+
+    //pc.printf("\nwidth: %d\nHeight: %d", width, height);
+
+    //Write load image  comand 0x0020
+    sendComand(my_spi, 0x0020);
+    int param[] = {(endianType<<8)|(pixFormat<<4)|rotate};
+    sendNData(my_spi,param,1);
+
+
+    //Write load image AREA comand 0x0021
+    //sendComand(0x0021);
+    //Send load parameters
+    //int img_param[] = {(endianType<<8)|(pixFormat<<4)|rotate, imPosX, imPosY, width, height};
+    //sendNData(img_param,5);
+
+    int count =0;
+    int line=0;
+
+
+    //send image data one word at a time
+    waitForLCD();
+    //cs_epd = 0;                     // Bring chip select low
+
+    //Send write data preamble  0x0000
+    //my_spi.write(0x0000);
+    //Send data
+    sendNData(my_spi,im_buffer, imbufferSize);
+
+    cs_epd=1;
+    //pc.printf("\ncheck1");
+    //send load image end coand
+    sendComand(my_spi, 0x0022);
+}
+
+//Display   bitmap on EPD
+/**
+ *  * xstart, ystart, the position on the display to place the image
+ * height, width, the size in pixels of the image.
+ * mode: bits per pixel 0 =2bbp, 1=3bpp, 2=4bpp, 3=5-8bpp
+ */
+void displayIamge(SPI my_spi, int xstart, int ystart, int height, int width, int mode)
+{
+    waitForLCD();
+    //Write display image comand 0x0034
+    sendComand(my_spi, 0x0034);
+    //Send parameters
+    //t.attach_us(&ledon,5000);
+
+    int params[] = {xstart, ystart, width, height, mode};
+    trigger = 1;
+    sendNData(my_spi, params,5);
+}
+
+/*###### Write Line ######*/
+//Write a single line To the TCON buffer
+void writeLine(SPI my_spi, int lineNum, short lineBuffer[], int size)//Writes one line to the display memory.
+{
+    int displayAddress = lineNum*0x960+0x3DBE90;    //set the address that the line will be stored in
+    //pc.printf("\n\rline: %X Line Nume: %d",  displayAddress, lineNum);
+    setImBufAddress(my_spi, displayAddress);   //0x3DBE90 adress from readdata() dat[2] and data[3]
+    load_full_image(my_spi, 0,0,0, lineBuffer, size, 0,0, size,1);
+    //readReg(my_spi, 0x208); //read the image base adress
+}
+
 /*###### EPD Write ######*/
-//Update the whole display with data from memory, starting at the 
+//Update the whole display with data from memory, starting at the
+//address: the adress in eeprom to read from
 int EPD_Write(SPI mem_spi, int address)
 {
+    Timer t;
     pc.printf("\nAddress: %i", address);
+    t.start();          //Start the timer
     //Setup the SPIs
     SPI epd_spi = setupEPD();       //Creat a new SPI object to comunicate with the EPD
+    int vaule[20];
+    readStatus(epd_spi, vaule);
 
     //Create local variables
-    int lineLengh = 60;            //the length of the display in bytes
-    int imageLength = 1440;
-    char sixtyBytes[lineLengh];        //Create a line buffer
-    
+    int lineLengh = 300;            //the length of the display in 16 bit wordswords,
+    int imageLength = 1034;
+    short sixtyBytes[lineLengh*2];        //Create a line buffer
+
     //int line = 0;               //counter to keep track of the current line
     //int frameLine = 0;          //counter to keep track of the line in the current frame
-    
+
 
     //led=!led;
-    
+
     //Begin SPI comunication
-    cs_epd=1;           //EPD chip deselected
+    // for old Tcon
+    /*cs_epd=1;           //EPD chip deselected
     TconEn =1;          //Tcon ON set High
     wait_ms(120);       //delay 120ms
     cs_epd=0;           //Select the Tcon chip
@@ -519,42 +1027,51 @@
     //Write Header
     epd_spi.write(0x06);
     epd_spi.write(0xa0);
-    wait_ms(120);   //delay 120ms
+    wait_ms(120);   //delay 120ms*/
 
-    //loop throug 1440 lines
+    //loop throug all lines lines
     for(int j=0; j<imageLength; j++) {
+
         //pc.printf("\n");
-        mem.readData(mem_spi, sixtyBytes, address, lineLengh);    //Read the line, putt them in buffer return the next address to read from
-        for(int i =0; i<lineLengh; i++) {             //Read one byte from the buffer
-            epd_spi.write(sixtyBytes[i]);      //and write it to the display
-            //pc.printf("%i%i%i%i%i%i%i%i",sixtyBytes[i]>>7&&1,sixtyBytes[i]>>6&&1,sixtyBytes[i]>>5&&1,sixtyBytes[i]>>4&&1,sixtyBytes[i]>>3&&1,sixtyBytes[i]>>2&&1,sixtyBytes[i]>>1&&1,sixtyBytes[i]&&1);
-            address++;
-            //byteCounter++;
-        }
-        
-        epd_spi.write(00);      //Write a 0 at the end of each line
-        if(j%100==0){
+        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
+
+        //Halve the resolution by doubling bit length
+        /*for(int i=0; i<lineLengh; i++) {
+            int low = sixtyBytes[i];
+
+            low = bitDouble(low);
+
+            sixtyBytes[i]=(low);
+        }*/
+
+
+        //pc.printf("ÿup");
+        writeLine(epd_spi,j,sixtyBytes,lineLengh);
+        if(j%100==0) {
             keyCheck();
-            }
-        //wait_us(10000);
+        }
+
     }
-    wait_us(1000);
-    DigitalOut sclk(P0_13);   //serial clk
-    sclk = 0;
-    cs_epd=1;       //Deselct the chip
-    Timer t;
-    t.start();          //Start the timer
-     while(TCbusy)
-    {
-        keyCheck();
-        //wait_ms
-        }
-        t.stop();
-    TconEn=0;       //Deassert Tcon ON
-    cs_epd=0;       //Deassert chip select
+    t.stop();
+    displayIamge(epd_spi,0,0,1034,2400,2);
+
+    for(int i = 0; i<1034; i++) {
+        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
+
+        //Halve the resolution by doubling bit length
+        /*for(int i=0; i<lineLengh; i++) {
+            int low = sixtyBytes[i];
+            low = bitDouble(low);
+            sixtyBytes[i]=(low);
+        }*/
+        writeLine(epd_spi,i,sixtyBytes,lineLengh-250);
+    }
+    displayIamge(epd_spi,0,0,1034,400,2);
+
 
     printf("\ntime = %i ", t.read_ms());
     t.reset();
+    printf("\ndone = ");
     return 1;
 }
 
@@ -571,45 +1088,48 @@
 {
     Timer t;
 
+
+    pc.baud(115200);
     int USBDataBuffer[64];
-    SPI my_spi = setupSPI();        //Creates an SPI object to comunicate with the external memory
-    populateNameList(my_spi);       //Reads the names  of layouts stored in external memory into RAM
+    SPI mem_spi = setupSPI();        //Creates an SPI object to comunicate with the external memory
+    populateNameList(mem_spi);       //Reads the names  of layouts stored in external memory into RAM
 
     while(1) {
         //pc.printf("Loop");
+        trigger=0;
+
+        sendUSB(readyForComand);    //Tell pc that read for data
+        keyCheck();                 //Scan the key matrix for key press and send to the pc
 
         sendUSB(readyForComand);
-        keyCheck();        
-
-        sendUSB(readyForComand);
-        if (readUSB(USBDataBuffer)>0) {
+        if (readUSB(USBDataBuffer)>0) {             //If a coamd received
             pc.printf("\nSwitch %i",USBDataBuffer[1]);
             char temp[] = {USBDataBuffer[3],USBDataBuffer[4],USBDataBuffer[5],USBDataBuffer[6],USBDataBuffer[7]};
             switch(USBDataBuffer[1]) {
                 case 0x10:          //If the recieved data is a write instruction
-                    GlobalAddress = writeFrame(my_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);       //Write the following data to the memory at 24 bit address usbDatabuffer[2-4]
+                    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]
                     break;
                 case 0x11:
                     pc.printf("\nPrep write");
-                    prepImage(my_spi, USBDataBuffer[2], temp);//Prepare a slot in memory for an image
+                    prepImage(mem_spi, USBDataBuffer[2], temp);//Prepare a slot in memory for an image
                     break;
                 case 0x12:
                     pc.printf("\nImage write");
-                    writeImage(my_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);//Write the next 86400 bytes of data to the memory starting at 24 bit address usbDatabuffer[2-4]
+                    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]
                     break;
                 case 0x20:          //If the recieved comand is a read instruction
-                    //mem.readData(my_spi, memoryBuffer, USBDataBuffer[2], USBDataBuffer[3]);//read(spi, destination[], address, length)
+                    //mem.readData(mem_spi, memoryBuffer, USBDataBuffer[2], USBDataBuffer[3]);//read(spi, destination[], address, length)
                     //pc.printf(" \n---EPD UPDATE--- %i",USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);
-                    EPD_Write(my_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);
+                    EPD_Write(mem_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);
                     break;
                 case 0x21:          //If the recieved comand is a swap instruction
-                    EPD_Swap(my_spi, USBDataBuffer[2]);
-                    break;    
+                    EPD_Swap(mem_spi, USBDataBuffer[2]);
+                    break;
                 case 0x30:          //If the recieved comand is a request for the cutrrent name
-                    populateNameList(my_spi);
+                    populateNameList(mem_spi);
                     getNameList();
                     break;
-                case 0x35:          //If the recieved comand is a request for all the names 
+                case 0x35:          //If the recieved comand is a request for all the names
                     getCurrentLayout();
                     break;
                 default: