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:
12:fbb1dd0586e1
Parent:
10:1f96f0cce2a0
Child:
13:f7445faded6a
--- a/BB_Basic.cpp	Tue Apr 17 03:20:57 2018 +0000
+++ b/BB_Basic.cpp	Sat Jul 07 11:45:17 2018 +0000
@@ -18,6 +18,10 @@
 //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
+#define TNF 0x02
+#define TFE 0x01
+#define RNE 0x04
+
 /******************************************************************************
  * Definitiontions
  */
@@ -112,7 +116,13 @@
     return my_spi;
 }
 
-
+/*** My method to mywrite to RAM ***/
+void mywrite (unsigned char data) {
+    // First don't mywrite to the FIFO buffer if it is full
+    while (!(LPC_SSP0->SR & TNF))       // While TNF-Bit = 0 (FIFO full)... 
+        ;                               // Wait
+    LPC_SSP0->DR = data;                // mywrite to FIFO buffer
+}
 /**
  * Setup USB HID, usefull for later not used in this iterarion
  */
@@ -284,6 +294,15 @@
 }
 
 /**
+ * int sendKey(int mod, int key). Sends a keypress to the pc over usb
+ */
+void sendKeyXinda(int keylocation1, int keylocation2, int keylocation3, int keylocation4, int keylocation5, int keylocation6, int keylocation7)
+{
+    int temp[]= {0x01,keylocation1,keylocation2,keylocation3,keylocation4,keylocation5,keylocation6,keylocation7};
+    sendUSB(temp);
+}
+
+/**
  * keyCheck(), scanss the keys and sends the keypress to the pc over usb
  */
 void keyCheck()
@@ -302,6 +321,26 @@
     }
 }
 
+/**
+ * testKey(), a test function from Xinda for debugging the connection between Newreadjson and BB_Basic according to the setting from CtrlCenter
+ */
+void testKey()
+{
+    pc.printf("\ntestKey function simulate keyboard event, starts in 2 sec(s)\n");
+    wait_ms(1000); //give user some time to move the cursor to where they want to type
+    pc.printf("\ntestKey function simulate keyboard event, starts in 1 sec(s)\n");
+    wait_ms(1000);
+    pc.printf("\ntestKey function start! typing...\n");
+    sendKeyXinda(0,80,80,80,80,80,80); // Input Chinese: Francisco is Sonder's mascot
+    wait_ms(10);
+    sendKeyXinda(1,80,80,80,80,80,80); // Ctrl+V according to the Ctrl Center setting
+    wait_ms(10);
+    sendKeyXinda(2,80,80,80,80,80,80); // Open Google Chrome in local
+    wait_ms(10);
+    sendKeyXinda(0,1,80,80,80,80,80);  // Chinese lyric, <Above the Moon> or korean, depends on the layout(cmd.exe or Google chrome)
+    pc.printf("\ntestKey function end!\n");
+}
+
 /******************************************************************************
  * Memory control funtions
  */
@@ -341,7 +380,7 @@
  *and return the start address. tyhe eighteight bit data read from memory is 
  doubled to sixteen bits for the display. eg 0b1010 =>0b11001100
  */
-int readData(SPI my_spi, short value [], int Address, int length, int doubleSelect)
+int readData(short value [], int Address, int length, int doubleSelect)
 {
     //if(length>bufferSize) {
     //printf("\nLength %i exceeds Max Length\n",length);
@@ -352,14 +391,16 @@
     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
+    mywrite(0x03);         //Send read command
+    mywrite(Address>>16);  //Send high address byte
+    mywrite(Address>>8);   //Send mid address byte
+    mywrite(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
+        mywrite(dummy);//Send dummy byte to read out value ate Address
+        while (LPC_SSP0->SR & RNE)
+            temp = LPC_SSP0->DR;
         //pc.printf("Temp: %X, ", temp);
         temp1 = bitDouble(temp);
         value[i]= temp1;
@@ -397,10 +438,10 @@
         //protected. this erases a block before writung to it if nessisary.
         int currentSlot = Address/SLOTSIZE;
         if (erasedSlots[currentSlot]) {
-            Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
+            Address = mem.writeData(memoryBuffer, Address, bufferSize);
         } else {
             pc.printf("\nCan not write to unerased slot. %X", Address);
-            Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
+            Address = mem.writeData(memoryBuffer, Address, bufferSize);
         }
 
     }
@@ -441,7 +482,7 @@
     pc.printf("\nCurrent slot: %i", currentSlot);
     if (erasedSlots[currentSlot]) {
         pc.printf("\nNE");
-        Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
+        Address = mem.writeData(memoryBuffer, Address, bufferSize);
     } else {
         pc.printf("\nCan not write to unerased slot.");
     }
@@ -485,22 +526,22 @@
  * Writes the name of a given layout to the top memory address in its reserved block
  * name[] is a 5 bytes char array with the most significant byte at name[0]
  */
-void nameBlock(SPI my_spi, char name[], int slot)
+void nameBlock(char name[], int slot)
 {
     //char temp[]= {name};
     pc.printf("write name");
-    mem.writeData(my_spi, name, slots[slot]+0x1ff00, 5);
+    mem.writeData(name, slots[slot]+0x1ff00, 5);
     pc.printf("\nName Done");
 }
 
 /**
  * Reads memory to find the names of the layouts stored. puts the names into nameList
  */
-void populateNameList(SPI my_spi)
+void populateNameList()
 {
     for(int slot=0; slot<16; slot++) {
         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
+        mem.readData(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];
         }
@@ -510,19 +551,19 @@
 /**
  * Prepares the memory for a new Layout, writes the layout's name, and erases the blocks
  */
-void prepImage(SPI my_spi, int slot, char name[])
+void prepImage(int slot, char name[])
 {
     pc.printf("\nSlot: %i, %i", slot, slots[slot]);
-    mem.blockErase(my_spi, slots[slot]);           //erase the bottom 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
+    mem.blockErase(slots[slot]);           //erase the bottom block of the slot
+    mem.blockErase(slots[slot]+0x10000);   //erase the middle block of the slot
+    mem.blockErase(slots[slot]+0x20000);   //erase the top block of the slot
+    mem.blockErase(slots[slot]+0x30000);   //erase the top block of the slot
+    mem.blockErase(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
+    //nameBlock(name,  slots[slot]/*+0x1FFF9*/); //Write the name of the layout to memory
     erasedSlots[slot]=true;                        //Mark the erased slot as true
     pc.printf("\nFin Working");
-    populateNameList(my_spi);
+    populateNameList();
     
 }
 
@@ -635,10 +676,10 @@
     waitForLCD();
     cs_epd = 0;                     // Bring chip select low
     //Send write data preamble  0x0000
-    my_spi.write(0x0000);
+    mywrite(0x0000);
     //Send data
     for (int i =0; i<wordsN; i++) {
-        my_spi.write(data[i]);
+        mywrite(data[i]);
         //pc.printf("%x ",data[i]);
         waitForLCD();
     }
@@ -648,15 +689,15 @@
 /*###### 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
+void sendNData(short data[], int wordsN) //for shorts
 {
     waitForLCD();
     cs_epd = 0;                     // Bring chip select low
     //Send write data preamble  0x0000
-    my_spi.write(0x0000);
+    mywrite(0x0000);
     //Send data
     for (int i =0; i<wordsN; i++) {
-        my_spi.write(data[i]);
+        mywrite(data[i]);
         //pc.printf("%x ",data[i]);
         waitForLCD();
     }
@@ -768,7 +809,9 @@
     //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
+        mywrite(00);//Send dummy byte to read out value ate Address
+        while (LPC_SSP0->SR & RNE)
+            myval[i]= LPC_SSP0->DR;//Read from data register
     }
     cs_epd = 1;                     // Bring chip select high to stop writing
 
@@ -808,33 +851,33 @@
 //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
+    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);
+    mywrite(0x0000);
     waitForLCD();
-    my_spi.write(mem_Add&0xFFFF);
+    mywrite(mem_Add&0xFFFF);
     cs_epd=1;
     waitForLCD();
     cs_epd=0;
-    my_spi.write(0x0000);
+    mywrite(0x0000);
     waitForLCD();
-    my_spi.write((mem_Add>>16)&0xFFFF);
+    mywrite((mem_Add>>16)&0xFFFF);
     cs_epd=1;
     waitForLCD();
     cs_epd=0;
-    my_spi.write(0x0000);
+    mywrite(0x0000);
     waitForLCD();
-    my_spi.write(write_Size&0xFFFF);
+    mywrite(write_Size&0xFFFF);
     cs_epd=1;
     waitForLCD();
     cs_epd=0;
-    my_spi.write(0x0000);
+    mywrite(0x0000);
     waitForLCD();
-    my_spi.write((write_Size>>16)&0xFFFF);
+    mywrite((write_Size>>16)&0xFFFF);
     cs_epd=1;
 
 
@@ -844,12 +887,12 @@
     cs_epd = 0;                     // Bring chip select low
 
     //Send write data preamble  0x0000
-    my_spi.write(0x0000);
+    mywrite(0x0000);
     //Send data
     for(int i = 0; i< 100; i++) {
         for(int h = 0; h< write_Size; h++) {
             waitForLCD();
-            my_spi.write(writeBuffer[h]);
+            mywrite(writeBuffer[h]);
             pc.printf(".");
         }
     }
@@ -941,7 +984,7 @@
     //Write load image  comand 0x0020
     sendComand(my_spi, 0x0020);
     int param[] = {(endianType<<8)|(pixFormat<<4)|rotate};
-    sendNData(my_spi,param,1);
+    sendNData(my_spi, param,1);
 
 
     //Write load image AREA comand 0x0021
@@ -959,9 +1002,9 @@
     //cs_epd = 0;                     // Bring chip select low
 
     //Send write data preamble  0x0000
-    //my_spi.write(0x0000);
+    //mywrite(0x0000);
     //Send data
-    sendNData(my_spi,im_buffer, imbufferSize);
+    sendNData(im_buffer, imbufferSize);
 
     cs_epd=1;
     //pc.printf("\ncheck1");
@@ -1002,7 +1045,7 @@
 /*###### EPD Write ######*/
 //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)
+int EPD_Write(int address)
 {
     Timer t;
     pc.printf("\nAddress: %i", address);
@@ -1039,7 +1082,7 @@
     for(int j=0; j<imageLength; j++) {
 
         //pc.printf("\n");
-        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
+        readData(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++) {
@@ -1062,7 +1105,7 @@
     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
+        readData(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++) {
@@ -1083,10 +1126,10 @@
 
 /*###### EPD Write ######*/
 //Update the whole display with data from a reserved slot
-int EPD_Swap(SPI mem_spi, int slot)
+int EPD_Swap(int slot)
 {
     pc.printf("\nslots: %i, %X", slot,slots[slot]);  
-    return EPD_Write(mem_spi, slots[slot]);
+    return EPD_Write(slots[slot]);
 }
 /*******************************************************
  * Main Function
@@ -1099,7 +1142,7 @@
     pc.baud(115200);
     int USBDataBuffer[64];
     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
+    populateNameList();       //Reads the names  of layouts stored in external memory into RAM
 
     while(1) {
         //pc.printf("Loop");
@@ -1118,7 +1161,7 @@
                     break;
                 case 0x11:
                     pc.printf("\nPrep write");
-                    prepImage(mem_spi, USBDataBuffer[2], temp);//Prepare a slot in memory for an image
+                    prepImage(USBDataBuffer[2], temp);//Prepare a slot in memory for an image
                     break;
                 case 0x12:
                     pc.printf("\nImage write");
@@ -1127,18 +1170,21 @@
                 case 0x20:          //If the recieved comand is a read instruction
                     //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(mem_spi, USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);
+                    EPD_Write(USBDataBuffer[2]*65536+USBDataBuffer[3]*256+USBDataBuffer[4]);
                     break;
                 case 0x21:          //If the recieved comand is a swap instruction
-                    EPD_Swap(mem_spi, USBDataBuffer[2]);
+                    EPD_Swap(USBDataBuffer[2]);
                     break;
                 case 0x30:          //If the recieved comand is a request for the cutrrent name
-                    populateNameList(mem_spi);
+                    populateNameList();
                     getNameList();
                     break;
                 case 0x35:          //If the recieved comand is a request for all the names
                     getCurrentLayout();
                     break;
+                case 0xF0:
+                    testKey();  // If received command is a request of keypress debugging. Xinda Debug version of keyCheck
+                    break;
                 default:
                     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]);
             }