First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Committer:
TrebleStick
Date:
Mon Mar 12 15:31:44 2018 +0000
Revision:
7:0d6632fba8d4
Parent:
6:44c53574bf84
Child:
8:e7818c369bd3
Decode set up, only newKey implemented so far

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TrebleStick 0:88c3d6c8a4eb 1 #include "mbed.h"
andrebharath 3:2e32d7974962 2 #include "Crypto_light/hash/SHA256.h"
andrebharath 3:2e32d7974962 3 #include "mbed-rtos/rtos/rtos.h"
TrebleStick 0:88c3d6c8a4eb 4
TrebleStick 0:88c3d6c8a4eb 5 //Photointerrupter input pins
TrebleStick 0:88c3d6c8a4eb 6 #define I1pin D2
TrebleStick 0:88c3d6c8a4eb 7 #define I2pin D11
TrebleStick 0:88c3d6c8a4eb 8 #define I3pin D12
andrebharath 3:2e32d7974962 9
TrebleStick 0:88c3d6c8a4eb 10 //Incremental encoder input pins
TrebleStick 0:88c3d6c8a4eb 11 #define CHA D7
TrebleStick 0:88c3d6c8a4eb 12 #define CHB D8
andrebharath 3:2e32d7974962 13
TrebleStick 0:88c3d6c8a4eb 14 //Motor Drive output pins //Mask in output byte
TrebleStick 0:88c3d6c8a4eb 15 #define L1Lpin D4 //0x01
TrebleStick 0:88c3d6c8a4eb 16 #define L1Hpin D5 //0x02
TrebleStick 0:88c3d6c8a4eb 17 #define L2Lpin D3 //0x04
TrebleStick 0:88c3d6c8a4eb 18 #define L2Hpin D6 //0x08
TrebleStick 0:88c3d6c8a4eb 19 #define L3Lpin D9 //0x10
TrebleStick 0:88c3d6c8a4eb 20 #define L3Hpin D10 //0x20
TrebleStick 0:88c3d6c8a4eb 21
andrebharath 3:2e32d7974962 22 //Enum for putMessage message types
andrebharath 3:2e32d7974962 23 #define MSG_HASHCOUNT 0
andrebharath 3:2e32d7974962 24 #define MSG_NONCE_OK 1
TrebleStick 4:e1141c1d8b19 25 #define MSG_OVERFLOW 2
TrebleStick 7:0d6632fba8d4 26
TrebleStick 4:e1141c1d8b19 27 //FIFO constant definitions
TrebleStick 6:44c53574bf84 28 #define MAX_ARRAY_SIZE 19 //Max length of input codes
TrebleStick 4:e1141c1d8b19 29
andrebharath 3:2e32d7974962 30
TrebleStick 7:0d6632fba8d4 31
TrebleStick 7:0d6632fba8d4 32
TrebleStick 0:88c3d6c8a4eb 33 //Mapping from sequential drive states to motor phase outputs
TrebleStick 0:88c3d6c8a4eb 34 /*
TrebleStick 0:88c3d6c8a4eb 35 State L1 L2 L3
TrebleStick 0:88c3d6c8a4eb 36 0 H - L
TrebleStick 0:88c3d6c8a4eb 37 1 - H L
TrebleStick 0:88c3d6c8a4eb 38 2 L H -
TrebleStick 0:88c3d6c8a4eb 39 3 L - H
TrebleStick 0:88c3d6c8a4eb 40 4 - L H
TrebleStick 0:88c3d6c8a4eb 41 5 H L -
TrebleStick 0:88c3d6c8a4eb 42 6 - - -
TrebleStick 0:88c3d6c8a4eb 43 7 - - -
TrebleStick 0:88c3d6c8a4eb 44 */
TrebleStick 0:88c3d6c8a4eb 45 //Drive state to output table
TrebleStick 0:88c3d6c8a4eb 46 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
andrebharath 3:2e32d7974962 47
TrebleStick 0:88c3d6c8a4eb 48 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
TrebleStick 0:88c3d6c8a4eb 49 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
TrebleStick 0:88c3d6c8a4eb 50 //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
andrebharath 3:2e32d7974962 51
TrebleStick 0:88c3d6c8a4eb 52 //Phase lead to make motor spin
TrebleStick 0:88c3d6c8a4eb 53 const int8_t lead = 2; //2 for forwards, -2 for backwards
andrebharath 3:2e32d7974962 54
TrebleStick 4:e1141c1d8b19 55 //Instantiate the serial port, using RawSerial to deal with
TrebleStick 4:e1141c1d8b19 56 //serial's undocumented buffering behaviour
TrebleStick 4:e1141c1d8b19 57 RawSerial pc(SERIAL_TX, SERIAL_RX);
TrebleStick 4:e1141c1d8b19 58
TrebleStick 0:88c3d6c8a4eb 59
TrebleStick 4:e1141c1d8b19 60 //structure for Mail Class
andrebharath 3:2e32d7974962 61 typedef struct {
andrebharath 3:2e32d7974962 62 uint8_t code;
andrebharath 3:2e32d7974962 63 uint32_t data;
andrebharath 3:2e32d7974962 64 } message_t ;
andrebharath 3:2e32d7974962 65
TrebleStick 4:e1141c1d8b19 66 //Mail class allowing 16 messages to be stored up in the FIFO
andrebharath 3:2e32d7974962 67 Mail<message_t,16> outMessages;
andrebharath 3:2e32d7974962 68
TrebleStick 4:e1141c1d8b19 69 //Replacement for printf so that notification shortcodes can be sent
andrebharath 3:2e32d7974962 70 void putMessage(uint8_t code, uint32_t data)
andrebharath 3:2e32d7974962 71 {
andrebharath 3:2e32d7974962 72 message_t *pMessage = outMessages.alloc();
andrebharath 3:2e32d7974962 73 pMessage->code = code;
andrebharath 3:2e32d7974962 74 pMessage->data = data;
andrebharath 3:2e32d7974962 75 outMessages.put(pMessage);
andrebharath 3:2e32d7974962 76 }
andrebharath 3:2e32d7974962 77
andrebharath 3:2e32d7974962 78 Thread commOutT;
andrebharath 3:2e32d7974962 79
andrebharath 3:2e32d7974962 80 void commOutFn()
andrebharath 3:2e32d7974962 81 {
andrebharath 3:2e32d7974962 82 while(1) {
andrebharath 3:2e32d7974962 83 osEvent newEvent = outMessages.get();
andrebharath 3:2e32d7974962 84 message_t *pMessage = (message_t*)newEvent.value.p;
andrebharath 3:2e32d7974962 85 pc.printf("Message %d with data 0x%016x\r\n",
andrebharath 3:2e32d7974962 86 pMessage->code,pMessage->data);
andrebharath 3:2e32d7974962 87 outMessages.free(pMessage);
andrebharath 3:2e32d7974962 88 }
andrebharath 3:2e32d7974962 89 }
andrebharath 3:2e32d7974962 90
TrebleStick 7:0d6632fba8d4 91 //Global varible for the Bitcoin Key
TrebleStick 7:0d6632fba8d4 92 volatile uint64_t newKey = 0; //check initialise value? ****
TrebleStick 7:0d6632fba8d4 93
TrebleStick 7:0d6632fba8d4 94 Mutex newKey_mutex //for mutex locking
TrebleStick 7:0d6632fba8d4 95
TrebleStick 5:fe9b21ba2e33 96 //Queue class
TrebleStick 5:fe9b21ba2e33 97 Queue<void, 8> inCharQ;
TrebleStick 5:fe9b21ba2e33 98 //serial port ISR to take individual chars
TrebleStick 5:fe9b21ba2e33 99 void serialISR(){
TrebleStick 5:fe9b21ba2e33 100 uint8_t newChar = pc.getc();
TrebleStick 5:fe9b21ba2e33 101 inCharQ.put((void*)newChar);
TrebleStick 5:fe9b21ba2e33 102 }
TrebleStick 7:0d6632fba8d4 103
TrebleStick 7:0d6632fba8d4 104
TrebleStick 5:fe9b21ba2e33 105 //decode commands
TrebleStick 5:fe9b21ba2e33 106 Thread decodeT;
TrebleStick 5:fe9b21ba2e33 107
TrebleStick 7:0d6632fba8d4 108 //set the global NewKey
TrebleStick 7:0d6632fba8d4 109 void setNewCmd(char newCmd[MAX_ARRAY_SIZE]){
TrebleStick 7:0d6632fba8d4 110 //find prefix ***
TrebleStick 7:0d6632fba8d4 111
TrebleStick 7:0d6632fba8d4 112 //K
TrebleStick 7:0d6632fba8d4 113 if newCmd[0] == 'K'{
TrebleStick 7:0d6632fba8d4 114 newKey_mutex.lock();
TrebleStick 7:0d6632fba8d4 115 sscanf(newCmd, "K%x", &newKey); //Decode the command
TrebleStick 7:0d6632fba8d4 116 newKey_mutex.unlock();
TrebleStick 7:0d6632fba8d4 117 }
TrebleStick 7:0d6632fba8d4 118 //V
TrebleStick 7:0d6632fba8d4 119
TrebleStick 7:0d6632fba8d4 120 //R
TrebleStick 7:0d6632fba8d4 121
TrebleStick 7:0d6632fba8d4 122
TrebleStick 7:0d6632fba8d4 123 }
TrebleStick 7:0d6632fba8d4 124
TrebleStick 7:0d6632fba8d4 125 //decode char's function
TrebleStick 5:fe9b21ba2e33 126 void decodeFn(){
TrebleStick 5:fe9b21ba2e33 127 pc.attach(&serialISR);
TrebleStick 7:0d6632fba8d4 128 char newCmd[MAX_ARRAY_SIZE] = "";
TrebleStick 5:fe9b21ba2e33 129 uint32_t bufferPosition = 0; //change this variable type if the max buffer/fifio size is found to be different
TrebleStick 5:fe9b21ba2e33 130 bool exit = false;
TrebleStick 5:fe9b21ba2e33 131 while(!exit) {
TrebleStick 5:fe9b21ba2e33 132
TrebleStick 5:fe9b21ba2e33 133 //get new char
TrebleStick 5:fe9b21ba2e33 134 osEvent newEvent = inCharQ.get();
TrebleStick 5:fe9b21ba2e33 135 uint8_t newChar = (uint8_t)newEvent.value.p;
TrebleStick 5:fe9b21ba2e33 136
TrebleStick 5:fe9b21ba2e33 137 //check for carriage return "\r"
TrebleStick 5:fe9b21ba2e33 138 if(newChar == 'r'){
TrebleStick 5:fe9b21ba2e33 139 if(bufferPosition != 0){
TrebleStick 7:0d6632fba8d4 140 if(newCmd[bufferPosition - 1] == '\\'){
TrebleStick 5:fe9b21ba2e33 141 //carriage found
TrebleStick 5:fe9b21ba2e33 142 newChar = '0'; //replace character
TrebleStick 5:fe9b21ba2e33 143
TrebleStick 5:fe9b21ba2e33 144 //add to array
TrebleStick 7:0d6632fba8d4 145 newCmd[bufferPosition] = newChar;
TrebleStick 5:fe9b21ba2e33 146
TrebleStick 5:fe9b21ba2e33 147 //reset buffer
TrebleStick 5:fe9b21ba2e33 148 bufferPosition = 0;
TrebleStick 5:fe9b21ba2e33 149 //send char array to decoder ***
TrebleStick 7:0d6632fba8d4 150 setNewCmd(newCmd);
TrebleStick 5:fe9b21ba2e33 151 }
TrebleStick 5:fe9b21ba2e33 152 }
TrebleStick 5:fe9b21ba2e33 153 }
TrebleStick 5:fe9b21ba2e33 154 //Add new char to array
TrebleStick 5:fe9b21ba2e33 155 else{
TrebleStick 5:fe9b21ba2e33 156 //add character at current position
TrebleStick 7:0d6632fba8d4 157 newCmd[bufferPosition] = newChar;
TrebleStick 5:fe9b21ba2e33 158 bufferPosition ++;
TrebleStick 5:fe9b21ba2e33 159 }
TrebleStick 5:fe9b21ba2e33 160
TrebleStick 6:44c53574bf84 161 //------error for overflow-------------------------//
TrebleStick 6:44c53574bf84 162 if(bufferPosition >= MAX_ARRAY_SIZE ){
TrebleStick 6:44c53574bf84 163 exit = true;
TrebleStick 6:44c53574bf84 164 putMessage(MSG_OVERFLOW, bufferPosition); //
TrebleStick 6:44c53574bf84 165 }
TrebleStick 6:44c53574bf84 166 //-------------------------------------------------//
TrebleStick 6:44c53574bf84 167
TrebleStick 5:fe9b21ba2e33 168 }//end of : while(!exit){}
TrebleStick 5:fe9b21ba2e33 169
TrebleStick 5:fe9b21ba2e33 170 //iii. Test the first character to determine which command was sent.
TrebleStick 5:fe9b21ba2e33 171 //iv. Decode the rest of the command
TrebleStick 6:44c53574bf84 172
TrebleStick 6:44c53574bf84 173
TrebleStick 6:44c53574bf84 174
TrebleStick 5:fe9b21ba2e33 175
TrebleStick 5:fe9b21ba2e33 176 }
TrebleStick 5:fe9b21ba2e33 177
andrebharath 3:2e32d7974962 178
TrebleStick 0:88c3d6c8a4eb 179 //Status LED
TrebleStick 0:88c3d6c8a4eb 180 DigitalOut led1(LED1);
andrebharath 3:2e32d7974962 181
TrebleStick 0:88c3d6c8a4eb 182 //Photointerrupter inputs
andrebharath 3:2e32d7974962 183 InterruptIn I1(I1pin);
andrebharath 3:2e32d7974962 184 InterruptIn I2(I2pin);
andrebharath 3:2e32d7974962 185 InterruptIn I3(I3pin);
andrebharath 3:2e32d7974962 186
TrebleStick 0:88c3d6c8a4eb 187 //Motor Drive outputs
TrebleStick 0:88c3d6c8a4eb 188 DigitalOut L1L(L1Lpin);
TrebleStick 0:88c3d6c8a4eb 189 DigitalOut L1H(L1Hpin);
TrebleStick 0:88c3d6c8a4eb 190 DigitalOut L2L(L2Lpin);
TrebleStick 0:88c3d6c8a4eb 191 DigitalOut L2H(L2Hpin);
TrebleStick 0:88c3d6c8a4eb 192 DigitalOut L3L(L3Lpin);
TrebleStick 0:88c3d6c8a4eb 193 DigitalOut L3H(L3Hpin);
andrebharath 3:2e32d7974962 194
andrebharath 3:2e32d7974962 195 volatile uint16_t hashcount = 0;
TrebleStick 0:88c3d6c8a4eb 196
andrebharath 3:2e32d7974962 197 void do_hashcount()
andrebharath 3:2e32d7974962 198 {
andrebharath 3:2e32d7974962 199 putMessage(MSG_HASHCOUNT, hashcount);
andrebharath 3:2e32d7974962 200 hashcount = 0;
andrebharath 3:2e32d7974962 201 }
andrebharath 3:2e32d7974962 202
TrebleStick 0:88c3d6c8a4eb 203 //Set a given drive state
andrebharath 3:2e32d7974962 204 void motorOut(int8_t driveState)
andrebharath 3:2e32d7974962 205 {
TrebleStick 0:88c3d6c8a4eb 206
TrebleStick 0:88c3d6c8a4eb 207 //Lookup the output byte from the drive state.
TrebleStick 0:88c3d6c8a4eb 208 int8_t driveOut = driveTable[driveState & 0x07];
TrebleStick 0:88c3d6c8a4eb 209
TrebleStick 0:88c3d6c8a4eb 210 //Turn off first
TrebleStick 0:88c3d6c8a4eb 211 if (~driveOut & 0x01) L1L = 0;
TrebleStick 0:88c3d6c8a4eb 212 if (~driveOut & 0x02) L1H = 1;
TrebleStick 0:88c3d6c8a4eb 213 if (~driveOut & 0x04) L2L = 0;
TrebleStick 0:88c3d6c8a4eb 214 if (~driveOut & 0x08) L2H = 1;
TrebleStick 0:88c3d6c8a4eb 215 if (~driveOut & 0x10) L3L = 0;
TrebleStick 0:88c3d6c8a4eb 216 if (~driveOut & 0x20) L3H = 1;
TrebleStick 0:88c3d6c8a4eb 217
TrebleStick 0:88c3d6c8a4eb 218 //Then turn on
TrebleStick 0:88c3d6c8a4eb 219 if (driveOut & 0x01) L1L = 1;
TrebleStick 0:88c3d6c8a4eb 220 if (driveOut & 0x02) L1H = 0;
TrebleStick 0:88c3d6c8a4eb 221 if (driveOut & 0x04) L2L = 1;
TrebleStick 0:88c3d6c8a4eb 222 if (driveOut & 0x08) L2H = 0;
TrebleStick 0:88c3d6c8a4eb 223 if (driveOut & 0x10) L3L = 1;
TrebleStick 0:88c3d6c8a4eb 224 if (driveOut & 0x20) L3H = 0;
andrebharath 3:2e32d7974962 225 }
TrebleStick 0:88c3d6c8a4eb 226
andrebharath 3:2e32d7974962 227 //Convert photointerrupter inputs to a rotor state
andrebharath 3:2e32d7974962 228 inline int8_t readRotorState()
andrebharath 3:2e32d7974962 229 {
TrebleStick 0:88c3d6c8a4eb 230 return stateMap[I1 + 2*I2 + 4*I3];
andrebharath 3:2e32d7974962 231 }
andrebharath 3:2e32d7974962 232
TrebleStick 0:88c3d6c8a4eb 233 //Basic synchronisation routine
andrebharath 3:2e32d7974962 234 int8_t motorHome()
andrebharath 3:2e32d7974962 235 {
TrebleStick 0:88c3d6c8a4eb 236 //Put the motor in drive state 0 and wait for it to stabilise
TrebleStick 0:88c3d6c8a4eb 237 motorOut(0);
andrebharath 3:2e32d7974962 238 wait(2.0);
TrebleStick 0:88c3d6c8a4eb 239
TrebleStick 0:88c3d6c8a4eb 240 //Get the rotor state
TrebleStick 0:88c3d6c8a4eb 241 return readRotorState();
TrebleStick 0:88c3d6c8a4eb 242 }
andrebharath 3:2e32d7974962 243
andrebharath 3:2e32d7974962 244 void photointerrupter_isr()
andrebharath 3:2e32d7974962 245 {
andrebharath 3:2e32d7974962 246 int8_t orState = motorHome();
andrebharath 3:2e32d7974962 247 int8_t intState = readRotorState();
andrebharath 3:2e32d7974962 248 motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
andrebharath 3:2e32d7974962 249 }
TrebleStick 0:88c3d6c8a4eb 250
TrebleStick 0:88c3d6c8a4eb 251 //Main
andrebharath 3:2e32d7974962 252 int main()
andrebharath 3:2e32d7974962 253 {
andrebharath 3:2e32d7974962 254 I1.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 255 I2.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 256 I3.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 257
andrebharath 3:2e32d7974962 258 I1.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 259 I2.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 260 I3.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 261
andrebharath 3:2e32d7974962 262 Ticker hashcounter;
andrebharath 3:2e32d7974962 263 hashcounter.attach(&do_hashcount, 1.0);
TrebleStick 0:88c3d6c8a4eb 264
andrebharath 3:2e32d7974962 265 commOutT.start(&commOutFn);
TrebleStick 0:88c3d6c8a4eb 266
TrebleStick 5:fe9b21ba2e33 267 decodeT.start(&decodeFn);
TrebleStick 4:e1141c1d8b19 268
andrebharath 3:2e32d7974962 269 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
andrebharath 3:2e32d7974962 270 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
andrebharath 3:2e32d7974962 271 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
andrebharath 3:2e32d7974962 272 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
andrebharath 3:2e32d7974962 273 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
andrebharath 3:2e32d7974962 274 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
andrebharath 3:2e32d7974962 275 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
andrebharath 3:2e32d7974962 276 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
andrebharath 3:2e32d7974962 277 uint64_t* key = (uint64_t*)((int)sequence + 48);
andrebharath 3:2e32d7974962 278 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
andrebharath 3:2e32d7974962 279 uint8_t hash[32];
TrebleStick 0:88c3d6c8a4eb 280
TrebleStick 0:88c3d6c8a4eb 281 //Poll the rotor state and set the motor outputs accordingly to spin the motor
TrebleStick 0:88c3d6c8a4eb 282 while (1) {
andrebharath 3:2e32d7974962 283 SHA256::computeHash(hash, sequence, 64);
TrebleStick 0:88c3d6c8a4eb 284
andrebharath 3:2e32d7974962 285 if (hash[0] == 0 && hash[1] == 0) {
andrebharath 3:2e32d7974962 286 putMessage(MSG_NONCE_OK, *nonce);
andrebharath 3:2e32d7974962 287 }
andrebharath 3:2e32d7974962 288
andrebharath 3:2e32d7974962 289 (*nonce)++;
andrebharath 3:2e32d7974962 290 hashcount++;
TrebleStick 1:a530f6235850 291
TrebleStick 0:88c3d6c8a4eb 292 }
TrebleStick 0:88c3d6c8a4eb 293 }
andrebharath 3:2e32d7974962 294
andrebharath 3:2e32d7974962 295