First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Revision:
12:1b2e2540e4e1
Parent:
11:14ccee7c6b59
Child:
13:ecccfc611025
diff -r 14ccee7c6b59 -r 1b2e2540e4e1 main.cpp
--- a/main.cpp	Tue Mar 20 11:27:47 2018 +0000
+++ b/main.cpp	Tue Mar 20 13:03:29 2018 +0000
@@ -9,7 +9,7 @@
 
 //Incremental encoder input pins
 #define CHA   D7
-#define CHB   D8  
+#define CHB   D8
 
 //Motor Drive output pins   //Mask in output byte
 #define L1Lpin D4           //0x01
@@ -37,7 +37,7 @@
 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
 
 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
-const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};  
+const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
 //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
 
 //Phase lead to make motor spin
@@ -47,8 +47,8 @@
 int8_t orState = 0;
 
 
-enum MSG {MSG_RESET, MSG_HASHCOUNT, MSG_NONCE_OK, 
-            MSG_OVERFLOW, MSG_NEW_KEY, MSG_ASSIGN_KEY};
+enum MSG {MSG_RESET, MSG_HASHCOUNT, MSG_NONCE_OK,
+            MSG_OVERFLOW, MSG_NEW_KEY, MSG_ASSIGN_KEY, MSG_TEST};
 
 //Instantiate the serial port
 RawSerial pc(SERIAL_TX, SERIAL_RX);
@@ -78,9 +78,17 @@
 
 Mail<message_t,16> outMessages;
 
+
+void putMessage(uint8_t code, uint32_t data) {//uint64_t
+    message_t *pMessage = outMessages.alloc();
+    pMessage->code = code;
+    pMessage->data = data;
+    outMessages.put(pMessage);
+}
+
 Thread commOutT;
 
-void commOutFn() {  
+void commOutFn() {
     while(1) {
         osEvent newEvent = outMessages.get();
         message_t *pMessage = (message_t*)newEvent.value.p;
@@ -91,13 +99,11 @@
 }
 
 
-void putMessage(uint8_t code, uint32_t data) {
-    message_t *pMessage = outMessages.alloc();
-    pMessage->code = code;
-    pMessage->data = data;
-    outMessages.put(pMessage);
-}
 
+//Global varible for the Bitcoin Key
+volatile uint64_t newKey = 0; //check initialise value? ****
+
+Mutex newKey_mutex; //for mutex locking
 
 //Instantiate a Queue to buffer incoming characters
 Queue<void, 8> inCharQ;
@@ -108,35 +114,31 @@
 }
 
 
-//Global varible for the Bitcoin Key
-volatile uint64_t newKey = 0; //check initialise value? ****
- 
-Mutex newKey_mutex; //for mutex locking 
 
 
 Thread decodeT;
 
 void setNewCmd(char newCmd[CHAR_ARR_SIZE]){
     //regex error checking ****
-    
-    //K 
+
+    //K
     if(newCmd[0] == 'K'){
         newKey_mutex.lock();
         sscanf(newCmd, "K%x", &newKey); //Decode the command
         newKey_mutex.unlock();
         putMessage(MSG_NEW_KEY, newKey);
     }
-    
+
     //V
     if(newCmd[0] == 'V'){
         //set new velocity***
     }
-    
+
     //R
     if(newCmd[0] == 'R'){
         //set new rotation***
     }
-        
+
 }
 
 
@@ -145,24 +147,25 @@
     char charSeq[CHAR_ARR_SIZE] = "";
     uint8_t bufPos = 0;
     while(1) {
-        
+
         if(bufPos >= CHAR_ARR_SIZE) {
             putMessage(MSG_OVERFLOW, bufPos);
+            bufPos = 0;
             break;
         }
-        
+
         osEvent newEvent = inCharQ.get();
         uint8_t newChar = (uint8_t)newEvent.value.p;
-        
-        if(newChar == '\r' ) {
-            charSeq[bufPos] = '0';
+
+        if(newChar == '\r' || newChar == '\n') {
+            charSeq[bufPos] = '\0';
             bufPos = 0;
             setNewCmd(charSeq);
-        } 
+        }
         else {
             charSeq[bufPos] = newChar;
             bufPos++;
-        }      
+        }
     }
 }
 
@@ -181,10 +184,10 @@
 
 //Set a given drive state
 void motorOut(int8_t driveState){
-    
+
     //Lookup the output byte from the drive state.
     int8_t driveOut = driveTable[driveState & 0x07];
-      
+
     //Turn off first
     if (~driveOut & 0x01) L1L = 0;
     if (~driveOut & 0x02) L1H = 1;
@@ -192,7 +195,7 @@
     if (~driveOut & 0x08) L2H = 1;
     if (~driveOut & 0x10) L3L = 0;
     if (~driveOut & 0x20) L3H = 1;
-    
+
     //Then turn on
     if (driveOut & 0x01) L1L = 1;
     if (driveOut & 0x02) L1H = 0;
@@ -201,18 +204,18 @@
     if (driveOut & 0x10) L3L = 1;
     if (driveOut & 0x20) L3H = 0;
     }
-    
+
     //Convert photointerrupter inputs to a rotor state
 inline int8_t readRotorState(){
     return stateMap[I1 + 2*I2 + 4*I3];
     }
 
-//Basic synchronisation routine    
+//Basic synchronisation routine
 int8_t motorHome() {
     //Put the motor in drive state 0 and wait for it to stabilise
     motorOut(0);
     wait(2.0);
-    
+
     //Get the rotor state
     return readRotorState();
 }
@@ -223,37 +226,37 @@
     motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
 }
 
-    
+
 //Main
 int main() {
-    
+
     putMessage(MSG_RESET, 0);
 
-    
+
     commOutT.start(&commOutFn);
-    
-    decodeT.start(&decodeFn); 
-    
+
+    decodeT.start(&decodeFn);
+
 //    pc.printf("Hello\n\r");
-    
+
     //Run the motor synchronisation
     orState = motorHome();
 //    pc.printf("Rotor origin: %x\n\r",orState);
     //orState is subtracted from future rotor state inputs to align rotor and motor states
-    
+
     I1.rise(&photointerrupter_isr);
     I2.rise(&photointerrupter_isr);
     I3.rise(&photointerrupter_isr);
-    
+
     I1.fall(&photointerrupter_isr);
     I2.fall(&photointerrupter_isr);
     I3.fall(&photointerrupter_isr);
-    
+
     //Calling the ISR once starts the motor movement
     photointerrupter_isr();
-    
+
     SHA256 sha256;
-    
+
     uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
                             0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
                             0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
@@ -263,27 +266,21 @@
                             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
     uint64_t* key = (uint64_t*)((int)sequence + 48);
-    
-    newKey_mutex.lock();
-    newKey = *key;
-    newKey_mutex.unlock();
-    
     uint64_t* nonce = (uint64_t*)((int)sequence + 56);
     uint8_t hash[32];
-    
+
     Ticker hashcounter;
     hashcounter.attach(&do_hashcount, 1.0);
-    
+
     //Poll the rotor state and set the motor outputs accordingly to spin the motor
     while (1) {
-        newKey_mutex.lock();
+
         *key = newKey;
-        newKey_mutex.unlock();
-        
-        putMessage(MSG_ASSIGN_KEY, newKey);
-   
+
+        //putMessage(MSG_ASSIGN_KEY, newKey);
+
         sha256.computeHash(hash, sequence, 64);
-        
+
         if (hash[0] == 0 && hash[1] == 0) {
             putMessage(MSG_NONCE_OK, *nonce);
         }
@@ -293,3 +290,6 @@
     }
 }
 
+
+// K12345678\r
+// K12345678