First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Committer:
TrebleStick
Date:
Mon Mar 12 15:00:13 2018 +0000
Revision:
6:44c53574bf84
Parent:
5:fe9b21ba2e33
Child:
7:0d6632fba8d4
Max array size accounted for;

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