First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Committer:
TrebleStick
Date:
Mon Mar 12 14:54:18 2018 +0000
Revision:
5:fe9b21ba2e33
Parent:
4:e1141c1d8b19
Child:
6:44c53574bf84
Bug fixes;

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 4:e1141c1d8b19 27 #define MAX_ARRAY_SIZE 256 //check the variable fifo position if this is changed
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 //------error for overflow-------------------------//
TrebleStick 5:fe9b21ba2e33 110 if(bufferPosition >= MAX_ARRAY_SIZE - 1){
TrebleStick 5:fe9b21ba2e33 111 exit = true;
TrebleStick 5:fe9b21ba2e33 112 putMessage(MSG_OVERFLOW, bufferPosition); //
TrebleStick 5:fe9b21ba2e33 113 }
TrebleStick 5:fe9b21ba2e33 114 //-------------------------------------------------//
TrebleStick 5:fe9b21ba2e33 115
TrebleStick 5:fe9b21ba2e33 116
TrebleStick 5:fe9b21ba2e33 117
TrebleStick 5:fe9b21ba2e33 118 //check for carriage return "\r"
TrebleStick 5:fe9b21ba2e33 119 if(newChar == 'r'){
TrebleStick 5:fe9b21ba2e33 120 if(bufferPosition != 0){
TrebleStick 5:fe9b21ba2e33 121 if(charArray[bufferPosition - 1] == '\\'){
TrebleStick 5:fe9b21ba2e33 122 //carriage found
TrebleStick 5:fe9b21ba2e33 123 newChar = '0'; //replace character
TrebleStick 5:fe9b21ba2e33 124
TrebleStick 5:fe9b21ba2e33 125 //add to array
TrebleStick 5:fe9b21ba2e33 126 charArray[bufferPosition] = newChar;
TrebleStick 5:fe9b21ba2e33 127
TrebleStick 5:fe9b21ba2e33 128 //reset buffer
TrebleStick 5:fe9b21ba2e33 129 bufferPosition = 0;
TrebleStick 5:fe9b21ba2e33 130 //send char array to decoder ***
TrebleStick 5:fe9b21ba2e33 131 }
TrebleStick 5:fe9b21ba2e33 132 }
TrebleStick 5:fe9b21ba2e33 133 }
TrebleStick 5:fe9b21ba2e33 134 //Add new char to array
TrebleStick 5:fe9b21ba2e33 135 else{
TrebleStick 5:fe9b21ba2e33 136 //add character at current position
TrebleStick 5:fe9b21ba2e33 137 charArray[bufferPosition] = newChar;
TrebleStick 5:fe9b21ba2e33 138 bufferPosition ++;
TrebleStick 5:fe9b21ba2e33 139 }
TrebleStick 5:fe9b21ba2e33 140
TrebleStick 5:fe9b21ba2e33 141 }//end of : while(!exit){}
TrebleStick 5:fe9b21ba2e33 142
TrebleStick 5:fe9b21ba2e33 143 //iii. Test the first character to determine which command was sent.
TrebleStick 5:fe9b21ba2e33 144 //iv. Decode the rest of the command
TrebleStick 5:fe9b21ba2e33 145
TrebleStick 5:fe9b21ba2e33 146 }
TrebleStick 5:fe9b21ba2e33 147
andrebharath 3:2e32d7974962 148
TrebleStick 0:88c3d6c8a4eb 149 //Status LED
TrebleStick 0:88c3d6c8a4eb 150 DigitalOut led1(LED1);
andrebharath 3:2e32d7974962 151
TrebleStick 0:88c3d6c8a4eb 152 //Photointerrupter inputs
andrebharath 3:2e32d7974962 153 InterruptIn I1(I1pin);
andrebharath 3:2e32d7974962 154 InterruptIn I2(I2pin);
andrebharath 3:2e32d7974962 155 InterruptIn I3(I3pin);
andrebharath 3:2e32d7974962 156
TrebleStick 0:88c3d6c8a4eb 157 //Motor Drive outputs
TrebleStick 0:88c3d6c8a4eb 158 DigitalOut L1L(L1Lpin);
TrebleStick 0:88c3d6c8a4eb 159 DigitalOut L1H(L1Hpin);
TrebleStick 0:88c3d6c8a4eb 160 DigitalOut L2L(L2Lpin);
TrebleStick 0:88c3d6c8a4eb 161 DigitalOut L2H(L2Hpin);
TrebleStick 0:88c3d6c8a4eb 162 DigitalOut L3L(L3Lpin);
TrebleStick 0:88c3d6c8a4eb 163 DigitalOut L3H(L3Hpin);
andrebharath 3:2e32d7974962 164
andrebharath 3:2e32d7974962 165 volatile uint16_t hashcount = 0;
TrebleStick 0:88c3d6c8a4eb 166
andrebharath 3:2e32d7974962 167 void do_hashcount()
andrebharath 3:2e32d7974962 168 {
andrebharath 3:2e32d7974962 169 putMessage(MSG_HASHCOUNT, hashcount);
andrebharath 3:2e32d7974962 170 hashcount = 0;
andrebharath 3:2e32d7974962 171 }
andrebharath 3:2e32d7974962 172
TrebleStick 0:88c3d6c8a4eb 173 //Set a given drive state
andrebharath 3:2e32d7974962 174 void motorOut(int8_t driveState)
andrebharath 3:2e32d7974962 175 {
TrebleStick 0:88c3d6c8a4eb 176
TrebleStick 0:88c3d6c8a4eb 177 //Lookup the output byte from the drive state.
TrebleStick 0:88c3d6c8a4eb 178 int8_t driveOut = driveTable[driveState & 0x07];
TrebleStick 0:88c3d6c8a4eb 179
TrebleStick 0:88c3d6c8a4eb 180 //Turn off first
TrebleStick 0:88c3d6c8a4eb 181 if (~driveOut & 0x01) L1L = 0;
TrebleStick 0:88c3d6c8a4eb 182 if (~driveOut & 0x02) L1H = 1;
TrebleStick 0:88c3d6c8a4eb 183 if (~driveOut & 0x04) L2L = 0;
TrebleStick 0:88c3d6c8a4eb 184 if (~driveOut & 0x08) L2H = 1;
TrebleStick 0:88c3d6c8a4eb 185 if (~driveOut & 0x10) L3L = 0;
TrebleStick 0:88c3d6c8a4eb 186 if (~driveOut & 0x20) L3H = 1;
TrebleStick 0:88c3d6c8a4eb 187
TrebleStick 0:88c3d6c8a4eb 188 //Then turn on
TrebleStick 0:88c3d6c8a4eb 189 if (driveOut & 0x01) L1L = 1;
TrebleStick 0:88c3d6c8a4eb 190 if (driveOut & 0x02) L1H = 0;
TrebleStick 0:88c3d6c8a4eb 191 if (driveOut & 0x04) L2L = 1;
TrebleStick 0:88c3d6c8a4eb 192 if (driveOut & 0x08) L2H = 0;
TrebleStick 0:88c3d6c8a4eb 193 if (driveOut & 0x10) L3L = 1;
TrebleStick 0:88c3d6c8a4eb 194 if (driveOut & 0x20) L3H = 0;
andrebharath 3:2e32d7974962 195 }
TrebleStick 0:88c3d6c8a4eb 196
andrebharath 3:2e32d7974962 197 //Convert photointerrupter inputs to a rotor state
andrebharath 3:2e32d7974962 198 inline int8_t readRotorState()
andrebharath 3:2e32d7974962 199 {
TrebleStick 0:88c3d6c8a4eb 200 return stateMap[I1 + 2*I2 + 4*I3];
andrebharath 3:2e32d7974962 201 }
andrebharath 3:2e32d7974962 202
TrebleStick 0:88c3d6c8a4eb 203 //Basic synchronisation routine
andrebharath 3:2e32d7974962 204 int8_t motorHome()
andrebharath 3:2e32d7974962 205 {
TrebleStick 0:88c3d6c8a4eb 206 //Put the motor in drive state 0 and wait for it to stabilise
TrebleStick 0:88c3d6c8a4eb 207 motorOut(0);
andrebharath 3:2e32d7974962 208 wait(2.0);
TrebleStick 0:88c3d6c8a4eb 209
TrebleStick 0:88c3d6c8a4eb 210 //Get the rotor state
TrebleStick 0:88c3d6c8a4eb 211 return readRotorState();
TrebleStick 0:88c3d6c8a4eb 212 }
andrebharath 3:2e32d7974962 213
andrebharath 3:2e32d7974962 214 void photointerrupter_isr()
andrebharath 3:2e32d7974962 215 {
andrebharath 3:2e32d7974962 216 int8_t orState = motorHome();
andrebharath 3:2e32d7974962 217 int8_t intState = readRotorState();
andrebharath 3:2e32d7974962 218 motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
andrebharath 3:2e32d7974962 219 }
TrebleStick 0:88c3d6c8a4eb 220
TrebleStick 0:88c3d6c8a4eb 221 //Main
andrebharath 3:2e32d7974962 222 int main()
andrebharath 3:2e32d7974962 223 {
andrebharath 3:2e32d7974962 224 I1.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 225 I2.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 226 I3.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 227
andrebharath 3:2e32d7974962 228 I1.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 229 I2.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 230 I3.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 231
andrebharath 3:2e32d7974962 232 Ticker hashcounter;
andrebharath 3:2e32d7974962 233 hashcounter.attach(&do_hashcount, 1.0);
TrebleStick 0:88c3d6c8a4eb 234
andrebharath 3:2e32d7974962 235 commOutT.start(&commOutFn);
TrebleStick 0:88c3d6c8a4eb 236
TrebleStick 5:fe9b21ba2e33 237 decodeT.start(&decodeFn);
TrebleStick 4:e1141c1d8b19 238
andrebharath 3:2e32d7974962 239 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
andrebharath 3:2e32d7974962 240 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
andrebharath 3:2e32d7974962 241 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
andrebharath 3:2e32d7974962 242 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
andrebharath 3:2e32d7974962 243 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
andrebharath 3:2e32d7974962 244 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
andrebharath 3:2e32d7974962 245 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
andrebharath 3:2e32d7974962 246 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
andrebharath 3:2e32d7974962 247 uint64_t* key = (uint64_t*)((int)sequence + 48);
andrebharath 3:2e32d7974962 248 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
andrebharath 3:2e32d7974962 249 uint8_t hash[32];
TrebleStick 0:88c3d6c8a4eb 250
TrebleStick 0:88c3d6c8a4eb 251 //Poll the rotor state and set the motor outputs accordingly to spin the motor
TrebleStick 0:88c3d6c8a4eb 252 while (1) {
andrebharath 3:2e32d7974962 253 SHA256::computeHash(hash, sequence, 64);
TrebleStick 0:88c3d6c8a4eb 254
andrebharath 3:2e32d7974962 255 if (hash[0] == 0 && hash[1] == 0) {
andrebharath 3:2e32d7974962 256 putMessage(MSG_NONCE_OK, *nonce);
andrebharath 3:2e32d7974962 257 }
andrebharath 3:2e32d7974962 258
andrebharath 3:2e32d7974962 259 (*nonce)++;
andrebharath 3:2e32d7974962 260 hashcount++;
TrebleStick 1:a530f6235850 261
TrebleStick 0:88c3d6c8a4eb 262 }
TrebleStick 0:88c3d6c8a4eb 263 }
andrebharath 3:2e32d7974962 264
andrebharath 3:2e32d7974962 265