First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Revision:
10:cedc98128562
Parent:
8:e7818c369bd3
Child:
11:14ccee7c6b59
--- a/main.cpp	Mon Mar 12 18:57:42 2018 +0000
+++ b/main.cpp	Tue Mar 20 11:25:22 2018 +0000
@@ -6,11 +6,11 @@
 #define I1pin D2
 #define I2pin D11
 #define I3pin D12
- 
+
 //Incremental encoder input pins
 #define CHA   D7
 #define CHB   D8  
- 
+
 //Motor Drive output pins   //Mask in output byte
 #define L1Lpin D4           //0x01
 #define L1Hpin D5           //0x02
@@ -19,17 +19,7 @@
 #define L3Lpin D9           //0x10
 #define L3Hpin D10          //0x20
 
-//Enum for putMessage message types
-#define MSG_HASHCOUNT 0
-#define MSG_NONCE_OK 1
-#define MSG_OVERFLOW 2
-#define INVALID_KEY  3
-#define KEY_UPDATED  4
-//FIFO constant definitions
-#define MAX_ARRAY_SIZE 19 //Max length of input codes
-
-
-
+#define CHAR_ARR_SIZE 18 //Max length of input codes
 
 //Mapping from sequential drive states to motor phase outputs
 /*
@@ -45,41 +35,52 @@
 */
 //Drive state to output table
 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,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
- 
+
 //Phase lead to make motor spin
 const int8_t lead = 2;  //2 for forwards, -2 for backwards
- 
-//Instantiate the serial port, using RawSerial to deal with 
-//serial's undocumented buffering behaviour 
-RawSerial pc(SERIAL_TX, SERIAL_RX); 
+
+//Rotor offset at motor state 0
+int8_t orState = 0;
 
 
-//structure for Mail Class
+enum MSG {MSG_RESET, MSG_HASHCOUNT, MSG_NONCE_OK, 
+            MSG_OVERFLOW, MSG_NEW_KEY, MSG_ASSIGN_KEY};
+
+//Instantiate the serial port
+RawSerial pc(SERIAL_TX, SERIAL_RX);
+
+//Status LED
+DigitalOut led1(LED1);
+
+//Photointerrupter inputs
+InterruptIn I1(I1pin);
+InterruptIn I2(I2pin);
+InterruptIn I3(I3pin);
+
+//Motor Drive outputs
+DigitalOut L1L(L1Lpin);
+DigitalOut L1H(L1Hpin);
+DigitalOut L2L(L2Lpin);
+DigitalOut L2H(L2Hpin);
+DigitalOut L3L(L3Lpin);
+DigitalOut L3H(L3Hpin);
+
+
+
 typedef struct {
      uint8_t code;
      uint32_t data;
 } message_t ;
 
-//Mail class allowing 16 messages to be stored up in the FIFO
 Mail<message_t,16> outMessages;
 
-//Replacement for printf so that notification shortcodes can be sent 
-void putMessage(uint8_t code, uint64_t data)
-{
-    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;
@@ -89,37 +90,41 @@
      }
 }
 
+
+void putMessage(uint8_t code, uint32_t data) {
+    message_t *pMessage = outMessages.alloc();
+    pMessage->code = code;
+    pMessage->data = data;
+    outMessages.put(pMessage);
+}
+
+
+//Instantiate a Queue to buffer incoming characters
+Queue<void, 8> inCharQ;
+//serial port ISR to receive each incoming byte and place into queue
+void serialISR() {
+    uint8_t newChar = pc.getc();
+    inCharQ.put((void*)newChar);
+}
+
+
 //Global varible for the Bitcoin Key
 volatile uint64_t newKey = 0; //check initialise value? ****
-
+ 
 Mutex newKey_mutex; //for mutex locking 
 
-//Queue class
-Queue<void, 8> inCharQ;
-//serial port ISR to take individual chars
-void serialISR(){
- uint8_t newChar = pc.getc();
- inCharQ.put((void*)newChar);
- }
- 
 
-
-//decode commands
 Thread decodeT;
 
-//set the global NewKey
-void setNewCmd(char newCmd[MAX_ARRAY_SIZE]){
+void setNewCmd(char newCmd[CHAR_ARR_SIZE]){
     //regex error checking ****
     
     //K 
     if(newCmd[0] == 'K'){
         newKey_mutex.lock();
         sscanf(newCmd, "K%x", &newKey); //Decode the command
-        //error for invalid key
-        //if(newKey != newCmd){
-//            putMessage(INVALID_KEY, newKey);
-//        }
         newKey_mutex.unlock();
+        putMessage(MSG_NEW_KEY, newKey);
     }
     
     //V
@@ -134,89 +139,48 @@
         
 }
 
-//decode char's function
-void decodeFn(){
+
+void decodeFn() {
     pc.attach(&serialISR);
-    char newCmd[MAX_ARRAY_SIZE] = "";
-    uint32_t bufferPosition = 0;                //change this variable type if the max buffer/fifio size is found to be different
-    bool exit = false;
-    while(!exit) { 
-    
-        //get new char
+    char charSeq[CHAR_ARR_SIZE] = "";
+    uint8_t bufPos = 0;
+    while(1) {
+        
+        if(bufPos >= CHAR_ARR_SIZE) {
+            putMessage(MSG_OVERFLOW, bufPos);
+            break;
+        }
+        
         osEvent newEvent = inCharQ.get();
         uint8_t newChar = (uint8_t)newEvent.value.p;
         
-        //check for carriage return "\r"
-        if(newChar == 'r'){
-            if(bufferPosition != 0){
-                if(newCmd[bufferPosition - 1] == '\\'){
-                    //carriage found 
-//                    newChar = '0';  //replace character
-                    
-                    //add to array
-                    newCmd[bufferPosition] = '0'; 
-                    
-                    //reset buffer
-                    bufferPosition = 0;
-                    //send char array to decoder ***
-                    setNewCmd(newCmd);
-                    putMessage(KEY_UPDATED, newKey);
-                    putMessage(KEY_UPDATED, 0x0123456789abcdef);
-                }
-            }
-        }
-        //Add new char to array
-        else{
-            //add character at current position 
-            newCmd[bufferPosition] = newChar; 
-            bufferPosition ++;
-        }
-      
-        //------error for overflow-------------------------// 
-        if(bufferPosition >= MAX_ARRAY_SIZE ){ 
-            exit = true; 
-            putMessage(MSG_OVERFLOW, bufferPosition); //
+        if(newChar == '\r' ) {
+            charSeq[bufPos] = '0';
+            bufPos = 0;
+            setNewCmd(charSeq);
         } 
-        //-------------------------------------------------//
-      
-     }//end of : while(!exit){} 
-        
-        //iii. Test the first character to determine which command was sent.
-        //iv. Decode the rest of the command
-        
-        
-        
-    
-} 
+        else {
+            charSeq[bufPos] = newChar;
+            bufPos++;
+        }      
+    }
+}
 
- 
-//Status LED
-DigitalOut led1(LED1);
- 
-//Photointerrupter inputs
-InterruptIn I1(I1pin);
-InterruptIn I2(I2pin);
-InterruptIn I3(I3pin);
- 
-//Motor Drive outputs
-DigitalOut L1L(L1Lpin);
-DigitalOut L1H(L1Hpin);
-DigitalOut L2L(L2Lpin);
-DigitalOut L2H(L2Hpin);
-DigitalOut L3L(L3Lpin);
-DigitalOut L3H(L3Hpin);
-  
+
+
+
+
+
 volatile uint16_t hashcount = 0;
 
-void do_hashcount() 
-{
+void do_hashcount() {
     putMessage(MSG_HASHCOUNT, hashcount);
     hashcount = 0;
 }
- 
+
+
 //Set a given drive state
-void motorOut(int8_t driveState)
-{
+void motorOut(int8_t driveState){
     
     //Lookup the output byte from the drive state.
     int8_t driveOut = driveTable[driveState & 0x07];
@@ -236,17 +200,15 @@
     if (driveOut & 0x08) L2H = 0;
     if (driveOut & 0x10) L3L = 1;
     if (driveOut & 0x20) L3H = 0;
-}
+    }
     
-//Convert photointerrupter inputs to a rotor state
-inline int8_t readRotorState()
-{
+    //Convert photointerrupter inputs to a rotor state
+inline int8_t readRotorState(){
     return stateMap[I1 + 2*I2 + 4*I3];
-}
- 
+    }
+
 //Basic synchronisation routine    
-int8_t motorHome()
-{
+int8_t motorHome() {
     //Put the motor in drive state 0 and wait for it to stabilise
     motorOut(0);
     wait(2.0);
@@ -257,14 +219,28 @@
 
 void photointerrupter_isr()
 {
-    int8_t orState = motorHome();
     int8_t intState = readRotorState();
     motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
 }
+
     
 //Main
-int main()
-{           
+int main() {
+    
+    putMessage(MSG_RESET, 0);
+
+    
+    commOutT.start(&commOutFn);
+    
+    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);
@@ -273,12 +249,10 @@
     I2.fall(&photointerrupter_isr);
     I3.fall(&photointerrupter_isr);
     
-    Ticker hashcounter;
-    hashcounter.attach(&do_hashcount, 1.0);
+    //Calling the ISR once starts the motor movement
+    photointerrupter_isr();
     
-    commOutT.start(&commOutFn);
-    
-    decodeT.start(&decodeFn); 
+    SHA256 sha256;
     
     uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
                             0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
@@ -288,19 +262,27 @@
                             0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
                             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
-                            
     uint64_t* key = (uint64_t*)((int)sequence + 48);
-    //set key to newKey
-    *key = newKey;
-    putMessage(KEY_UPDATED, *key);
     
+    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) {
-        SHA256::computeHash(hash, sequence, 64);
+        newKey_mutex.lock();
+        *key = newKey;
+        newKey_mutex.unlock();
+        
+        putMessage(MSG_ASSIGN_KEY, newKey);
+   
+        sha256.computeHash(hash, sequence, 64);
         
         if (hash[0] == 0 && hash[1] == 0) {
             putMessage(MSG_NONCE_OK, *nonce);
@@ -308,10 +290,6 @@
 
         (*nonce)++;
         hashcount++;
-        *key = newKey;
-        //putMessage(KEY_UPDATED, *key);
-        
     }
 }
- 
- 
\ No newline at end of file
+