First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Committer:
TrebleStick
Date:
Tue Mar 20 17:20:13 2018 +0000
Revision:
21:0d1025dc6433
Parent:
20:a435105305fe
Child:
22:7c94575d7792
Wrong way buddy

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 9:ecef1e8cbe3d 9
TrebleStick 0:88c3d6c8a4eb 10 //Incremental encoder input pins
TrebleStick 0:88c3d6c8a4eb 11 #define CHA D7
TrebleStick 12:1b2e2540e4e1 12 #define CHB D8
andrebharath 9:ecef1e8cbe3d 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 9:ecef1e8cbe3d 22 #define CHAR_ARR_SIZE 18 //Max length of input codes
andrebharath 17:80159ace5ddf 23 #define MAX_PWM_PERIOD 2000
andrebharath 18:05e5d280a082 24 #define MAX_TORQUE 1000
andrebharath 3:2e32d7974962 25
TrebleStick 0:88c3d6c8a4eb 26 //Mapping from sequential drive states to motor phase outputs
TrebleStick 0:88c3d6c8a4eb 27 /*
TrebleStick 0:88c3d6c8a4eb 28 State L1 L2 L3
TrebleStick 0:88c3d6c8a4eb 29 0 H - L
TrebleStick 0:88c3d6c8a4eb 30 1 - H L
TrebleStick 0:88c3d6c8a4eb 31 2 L H -
TrebleStick 0:88c3d6c8a4eb 32 3 L - H
TrebleStick 0:88c3d6c8a4eb 33 4 - L H
TrebleStick 0:88c3d6c8a4eb 34 5 H L -
TrebleStick 0:88c3d6c8a4eb 35 6 - - -
TrebleStick 0:88c3d6c8a4eb 36 7 - - -
TrebleStick 0:88c3d6c8a4eb 37 */
TrebleStick 0:88c3d6c8a4eb 38 //Drive state to output table
TrebleStick 0:88c3d6c8a4eb 39 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
andrebharath 9:ecef1e8cbe3d 40
TrebleStick 0:88c3d6c8a4eb 41 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
TrebleStick 12:1b2e2540e4e1 42 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
TrebleStick 0:88c3d6c8a4eb 43 //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
andrebharath 9:ecef1e8cbe3d 44
TrebleStick 0:88c3d6c8a4eb 45 //Phase lead to make motor spin
TrebleStick 0:88c3d6c8a4eb 46 const int8_t lead = 2; //2 for forwards, -2 for backwards
andrebharath 9:ecef1e8cbe3d 47
andrebharath 9:ecef1e8cbe3d 48 //Rotor offset at motor state 0
andrebharath 18:05e5d280a082 49 volatile int8_t orState = 0;
andrebharath 9:ecef1e8cbe3d 50
andrebharath 17:80159ace5ddf 51 //Set initial torque of 1000
andrebharath 18:05e5d280a082 52 volatile uint32_t torque = 1000;
andrebharath 17:80159ace5ddf 53
andrebharath 9:ecef1e8cbe3d 54
TrebleStick 12:1b2e2540e4e1 55 enum MSG {MSG_RESET, MSG_HASHCOUNT, MSG_NONCE_OK,
TrebleStick 20:a435105305fe 56 MSG_OVERFLOW, MSG_ROT_PEN, MSG_MAX_SPD, MSG_NEW_KEY, MSG_INP_ERR, MSG_TORQUE,
TrebleStick 20:a435105305fe 57 MSG_TEST, MSG_CUR_SPD};
andrebharath 9:ecef1e8cbe3d 58
andrebharath 3:2e32d7974962 59 //Instantiate the serial port
andrebharath 9:ecef1e8cbe3d 60 RawSerial pc(SERIAL_TX, SERIAL_RX);
andrebharath 9:ecef1e8cbe3d 61
andrebharath 9:ecef1e8cbe3d 62 //Status LED
andrebharath 9:ecef1e8cbe3d 63 DigitalOut led1(LED1);
andrebharath 9:ecef1e8cbe3d 64
andrebharath 9:ecef1e8cbe3d 65 //Photointerrupter inputs
andrebharath 9:ecef1e8cbe3d 66 InterruptIn I1(I1pin);
andrebharath 9:ecef1e8cbe3d 67 InterruptIn I2(I2pin);
andrebharath 9:ecef1e8cbe3d 68 InterruptIn I3(I3pin);
andrebharath 9:ecef1e8cbe3d 69
andrebharath 9:ecef1e8cbe3d 70 //Motor Drive outputs
andrebharath 17:80159ace5ddf 71 PwmOut L1L(L1Lpin);
andrebharath 9:ecef1e8cbe3d 72 DigitalOut L1H(L1Hpin);
andrebharath 17:80159ace5ddf 73 PwmOut L2L(L2Lpin);
andrebharath 9:ecef1e8cbe3d 74 DigitalOut L2H(L2Hpin);
andrebharath 17:80159ace5ddf 75 PwmOut L3L(L3Lpin);
andrebharath 9:ecef1e8cbe3d 76 DigitalOut L3H(L3Hpin);
andrebharath 9:ecef1e8cbe3d 77
TrebleStick 0:88c3d6c8a4eb 78
andrebharath 3:2e32d7974962 79
TrebleStick 20:a435105305fe 80
andrebharath 3:2e32d7974962 81 typedef struct {
TrebleStick 20:a435105305fe 82 uint8_t code;
TrebleStick 20:a435105305fe 83 int32_t data;
andrebharath 3:2e32d7974962 84 } message_t ;
andrebharath 3:2e32d7974962 85
andrebharath 3:2e32d7974962 86 Mail<message_t,16> outMessages;
andrebharath 3:2e32d7974962 87
TrebleStick 12:1b2e2540e4e1 88
TrebleStick 15:bd303ab8a21f 89 void putMessage(uint8_t code, int32_t data) {//uint64_t
TrebleStick 12:1b2e2540e4e1 90 message_t *pMessage = outMessages.alloc();
TrebleStick 12:1b2e2540e4e1 91 pMessage->code = code;
TrebleStick 12:1b2e2540e4e1 92 pMessage->data = data;
TrebleStick 12:1b2e2540e4e1 93 outMessages.put(pMessage);
TrebleStick 12:1b2e2540e4e1 94 }
TrebleStick 12:1b2e2540e4e1 95
TrebleStick 21:0d1025dc6433 96 Thread commOutT (osPriorityNormal, 1024);
andrebharath 3:2e32d7974962 97
TrebleStick 12:1b2e2540e4e1 98 void commOutFn() {
andrebharath 3:2e32d7974962 99 while(1) {
andrebharath 3:2e32d7974962 100 osEvent newEvent = outMessages.get();
andrebharath 3:2e32d7974962 101 message_t *pMessage = (message_t*)newEvent.value.p;
TrebleStick 15:bd303ab8a21f 102 pc.printf("Message: [%d], data: 0x%08x\r\n",
andrebharath 3:2e32d7974962 103 pMessage->code,pMessage->data);
andrebharath 3:2e32d7974962 104 outMessages.free(pMessage);
andrebharath 3:2e32d7974962 105 }
andrebharath 3:2e32d7974962 106 }
andrebharath 3:2e32d7974962 107
andrebharath 9:ecef1e8cbe3d 108
andrebharath 9:ecef1e8cbe3d 109
TrebleStick 14:66746291017c 110 //Global varible for the Bitcoin Key,maxspeed and rotations_pending
TrebleStick 12:1b2e2540e4e1 111 volatile uint64_t newKey = 0; //check initialise value? ****
TrebleStick 14:66746291017c 112 volatile float maxspeed = 0, rotations_pending = 0;
TrebleStick 14:66746291017c 113 //mutex variables
TrebleStick 14:66746291017c 114 Mutex newKey_mutex;
TrebleStick 14:66746291017c 115 Mutex maxspeed_mutex;
TrebleStick 14:66746291017c 116 Mutex rotations_pending_mutex;
andrebharath 9:ecef1e8cbe3d 117
andrebharath 9:ecef1e8cbe3d 118 //Instantiate a Queue to buffer incoming characters
andrebharath 9:ecef1e8cbe3d 119 Queue<void, 8> inCharQ;
andrebharath 9:ecef1e8cbe3d 120 //serial port ISR to receive each incoming byte and place into queue
andrebharath 9:ecef1e8cbe3d 121 void serialISR() {
andrebharath 9:ecef1e8cbe3d 122 uint8_t newChar = pc.getc();
andrebharath 9:ecef1e8cbe3d 123 inCharQ.put((void*)newChar);
andrebharath 9:ecef1e8cbe3d 124 }
TrebleStick 20:a435105305fe 125 Thread motorCtrlT (osPriorityNormal, 2048);
TrebleStick 20:a435105305fe 126
TrebleStick 20:a435105305fe 127 int8_t readRotorState();
TrebleStick 20:a435105305fe 128 void motorOut(int8_t,uint32_t);
TrebleStick 20:a435105305fe 129 volatile int32_t motorPosition;
TrebleStick 20:a435105305fe 130
TrebleStick 20:a435105305fe 131 void photointerrupter_isr() {
TrebleStick 20:a435105305fe 132 static int8_t oldRotorState = 0;
TrebleStick 20:a435105305fe 133 int8_t rotorState = readRotorState();
TrebleStick 20:a435105305fe 134 motorOut((rotorState-orState+lead+6)%6, torque); //+6 to make sure the remainder is positive
TrebleStick 20:a435105305fe 135 if (rotorState - oldRotorState == 5) motorPosition--;
TrebleStick 20:a435105305fe 136 else if (rotorState - oldRotorState == -5) motorPosition++;
TrebleStick 20:a435105305fe 137 else motorPosition += (rotorState - oldRotorState);
TrebleStick 20:a435105305fe 138 oldRotorState = rotorState;
TrebleStick 20:a435105305fe 139 }
TrebleStick 20:a435105305fe 140
TrebleStick 20:a435105305fe 141 //TODO Implement timer for greater accuracy
TrebleStick 20:a435105305fe 142 void motorCtrlTick(){
TrebleStick 20:a435105305fe 143 motorCtrlT.signal_set(0x1);
TrebleStick 20:a435105305fe 144 }
TrebleStick 20:a435105305fe 145
TrebleStick 21:0d1025dc6433 146 volatile float currentSpeed = 0;
andrebharath 9:ecef1e8cbe3d 147
andrebharath 9:ecef1e8cbe3d 148
TrebleStick 20:a435105305fe 149 void motorCtrlFn(){
TrebleStick 21:0d1025dc6433 150 int32_t posStart = 0, posEnd = 0;
TrebleStick 20:a435105305fe 151 Ticker motorCtrlTicker;
TrebleStick 20:a435105305fe 152 motorCtrlTicker.attach_us(&motorCtrlTick,100000);
TrebleStick 21:0d1025dc6433 153 putMessage(MSG_TEST, 0x505);
TrebleStick 20:a435105305fe 154 uint8_t cnt = 0;
TrebleStick 20:a435105305fe 155
TrebleStick 20:a435105305fe 156 while(1){
TrebleStick 20:a435105305fe 157 posStart = motorPosition;
TrebleStick 20:a435105305fe 158 motorCtrlT.signal_wait(0x1);
TrebleStick 20:a435105305fe 159 posEnd = motorPosition;
TrebleStick 21:0d1025dc6433 160 currentSpeed = (posEnd - posStart)*10;
TrebleStick 20:a435105305fe 161
TrebleStick 20:a435105305fe 162 if(cnt >= 9) {
TrebleStick 21:0d1025dc6433 163 putMessage(MSG_CUR_SPD, currentSpeed);
TrebleStick 21:0d1025dc6433 164 // putMessage(MSG_TEST, posEnd);
TrebleStick 20:a435105305fe 165 cnt = 0;
TrebleStick 20:a435105305fe 166 }
TrebleStick 20:a435105305fe 167 else{ cnt++; }
TrebleStick 20:a435105305fe 168 }
TrebleStick 19:526fd700e1b3 169 }
andrebharath 9:ecef1e8cbe3d 170
TrebleStick 20:a435105305fe 171
andrebharath 9:ecef1e8cbe3d 172
TrebleStick 14:66746291017c 173 void setNewCmd(char s[CHAR_ARR_SIZE])
TrebleStick 14:66746291017c 174 {
TrebleStick 14:66746291017c 175 uint64_t newKey_;
andrebharath 17:80159ace5ddf 176 uint32_t torque_;
TrebleStick 14:66746291017c 177 float maxspeed_, rotations_pending_;
TrebleStick 14:66746291017c 178 //R
TrebleStick 14:66746291017c 179 if (sscanf(s, "R%f", &rotations_pending_)) {
andrebharath 17:80159ace5ddf 180 rotations_pending_mutex.lock();
andrebharath 17:80159ace5ddf 181 rotations_pending = rotations_pending_;
andrebharath 17:80159ace5ddf 182 rotations_pending_mutex.unlock();
andrebharath 17:80159ace5ddf 183 putMessage(MSG_ROT_PEN, rotations_pending);
TrebleStick 14:66746291017c 184 //V
TrebleStick 20:a435105305fe 185 }
TrebleStick 20:a435105305fe 186 else if (sscanf(s, "V%f", &maxspeed_)) {
andrebharath 17:80159ace5ddf 187 maxspeed_mutex.lock();
andrebharath 17:80159ace5ddf 188 maxspeed = maxspeed_;
andrebharath 17:80159ace5ddf 189 maxspeed_mutex.unlock();
andrebharath 17:80159ace5ddf 190 putMessage(MSG_MAX_SPD, maxspeed);
TrebleStick 14:66746291017c 191 //K
TrebleStick 20:a435105305fe 192 }
TrebleStick 20:a435105305fe 193 else if (sscanf(s, "K%llx", &newKey_)) {
andrebharath 17:80159ace5ddf 194 newKey_mutex.lock();
andrebharath 17:80159ace5ddf 195 newKey = newKey_;
andrebharath 17:80159ace5ddf 196 newKey_mutex.unlock();
andrebharath 17:80159ace5ddf 197 putMessage(MSG_NEW_KEY, newKey);
TrebleStick 20:a435105305fe 198 }
TrebleStick 20:a435105305fe 199 else if (sscanf(s, "T%u", &torque_)) {
andrebharath 17:80159ace5ddf 200 torque = torque_;
TrebleStick 19:526fd700e1b3 201 photointerrupter_isr(); //Give it a kick
andrebharath 17:80159ace5ddf 202 putMessage(MSG_TORQUE, torque);
TrebleStick 14:66746291017c 203 //ERROR
TrebleStick 20:a435105305fe 204 }
TrebleStick 20:a435105305fe 205 else{
TrebleStick 20:a435105305fe 206 putMessage(MSG_INP_ERR, 0x404);
TrebleStick 20:a435105305fe 207 }
TrebleStick 14:66746291017c 208 }
TrebleStick 12:1b2e2540e4e1 209
TrebleStick 20:a435105305fe 210 Thread decodeT (osPriorityNormal, 512);
andrebharath 9:ecef1e8cbe3d 211
andrebharath 9:ecef1e8cbe3d 212 void decodeFn() {
andrebharath 9:ecef1e8cbe3d 213 pc.attach(&serialISR);
andrebharath 9:ecef1e8cbe3d 214 char charSeq[CHAR_ARR_SIZE] = "";
andrebharath 9:ecef1e8cbe3d 215 uint8_t bufPos = 0;
andrebharath 9:ecef1e8cbe3d 216 while(1) {
TrebleStick 12:1b2e2540e4e1 217
andrebharath 9:ecef1e8cbe3d 218 if(bufPos >= CHAR_ARR_SIZE) {
andrebharath 9:ecef1e8cbe3d 219 putMessage(MSG_OVERFLOW, bufPos);
TrebleStick 12:1b2e2540e4e1 220 bufPos = 0;
andrebharath 9:ecef1e8cbe3d 221 }
TrebleStick 13:ecccfc611025 222 else{
TrebleStick 12:1b2e2540e4e1 223
TrebleStick 13:ecccfc611025 224 osEvent newEvent = inCharQ.get();
TrebleStick 13:ecccfc611025 225 uint8_t newChar = (uint8_t)newEvent.value.p;
TrebleStick 12:1b2e2540e4e1 226
TrebleStick 13:ecccfc611025 227 if(newChar == '\r' || newChar == '\n') {
TrebleStick 13:ecccfc611025 228 charSeq[bufPos] = '\0';
TrebleStick 13:ecccfc611025 229 bufPos = 0;
TrebleStick 13:ecccfc611025 230 setNewCmd(charSeq);
TrebleStick 13:ecccfc611025 231 }
TrebleStick 13:ecccfc611025 232 else {
TrebleStick 13:ecccfc611025 233 charSeq[bufPos] = newChar;
TrebleStick 13:ecccfc611025 234 bufPos++;
TrebleStick 13:ecccfc611025 235 }
TrebleStick 12:1b2e2540e4e1 236 }
andrebharath 9:ecef1e8cbe3d 237 }
andrebharath 9:ecef1e8cbe3d 238 }
andrebharath 9:ecef1e8cbe3d 239
andrebharath 3:2e32d7974962 240 volatile uint16_t hashcount = 0;
TrebleStick 0:88c3d6c8a4eb 241
andrebharath 9:ecef1e8cbe3d 242 void do_hashcount() {
TrebleStick 15:bd303ab8a21f 243 //putMessage(MSG_HASHCOUNT, hashcount);
andrebharath 3:2e32d7974962 244 hashcount = 0;
andrebharath 3:2e32d7974962 245 }
andrebharath 9:ecef1e8cbe3d 246
andrebharath 9:ecef1e8cbe3d 247
TrebleStick 0:88c3d6c8a4eb 248 //Set a given drive state
andrebharath 17:80159ace5ddf 249 void motorOut(int8_t driveState, uint32_t t){
TrebleStick 12:1b2e2540e4e1 250
TrebleStick 0:88c3d6c8a4eb 251 //Lookup the output byte from the drive state.
TrebleStick 0:88c3d6c8a4eb 252 int8_t driveOut = driveTable[driveState & 0x07];
TrebleStick 12:1b2e2540e4e1 253
TrebleStick 0:88c3d6c8a4eb 254 //Turn off first
andrebharath 17:80159ace5ddf 255 if (~driveOut & 0x01) L1L.pulsewidth_us(0);
TrebleStick 0:88c3d6c8a4eb 256 if (~driveOut & 0x02) L1H = 1;
andrebharath 17:80159ace5ddf 257 if (~driveOut & 0x04) L2L.pulsewidth_us(0);
TrebleStick 0:88c3d6c8a4eb 258 if (~driveOut & 0x08) L2H = 1;
andrebharath 17:80159ace5ddf 259 if (~driveOut & 0x10) L3L.pulsewidth_us(0);
TrebleStick 0:88c3d6c8a4eb 260 if (~driveOut & 0x20) L3H = 1;
TrebleStick 12:1b2e2540e4e1 261
TrebleStick 0:88c3d6c8a4eb 262 //Then turn on
andrebharath 17:80159ace5ddf 263 if (driveOut & 0x01) L1L.pulsewidth_us(t);
TrebleStick 0:88c3d6c8a4eb 264 if (driveOut & 0x02) L1H = 0;
andrebharath 17:80159ace5ddf 265 if (driveOut & 0x04) L2L.pulsewidth_us(t);
TrebleStick 0:88c3d6c8a4eb 266 if (driveOut & 0x08) L2H = 0;
andrebharath 17:80159ace5ddf 267 if (driveOut & 0x10) L3L.pulsewidth_us(t);
TrebleStick 0:88c3d6c8a4eb 268 if (driveOut & 0x20) L3H = 0;
andrebharath 17:80159ace5ddf 269 }
TrebleStick 12:1b2e2540e4e1 270
andrebharath 9:ecef1e8cbe3d 271 //Convert photointerrupter inputs to a rotor state
andrebharath 9:ecef1e8cbe3d 272 inline int8_t readRotorState(){
TrebleStick 0:88c3d6c8a4eb 273 return stateMap[I1 + 2*I2 + 4*I3];
andrebharath 17:80159ace5ddf 274 }
andrebharath 9:ecef1e8cbe3d 275
TrebleStick 12:1b2e2540e4e1 276 //Basic synchronisation routine
andrebharath 9:ecef1e8cbe3d 277 int8_t motorHome() {
TrebleStick 0:88c3d6c8a4eb 278 //Put the motor in drive state 0 and wait for it to stabilise
andrebharath 18:05e5d280a082 279 motorOut(0, MAX_TORQUE);
andrebharath 3:2e32d7974962 280 wait(2.0);
TrebleStick 12:1b2e2540e4e1 281
TrebleStick 0:88c3d6c8a4eb 282 //Get the rotor state
TrebleStick 0:88c3d6c8a4eb 283 return readRotorState();
TrebleStick 0:88c3d6c8a4eb 284 }
andrebharath 3:2e32d7974962 285
andrebharath 9:ecef1e8cbe3d 286
TrebleStick 12:1b2e2540e4e1 287
TrebleStick 0:88c3d6c8a4eb 288 //Main
andrebharath 9:ecef1e8cbe3d 289 int main() {
TrebleStick 12:1b2e2540e4e1 290
andrebharath 9:ecef1e8cbe3d 291 putMessage(MSG_RESET, 0);
andrebharath 9:ecef1e8cbe3d 292
TrebleStick 20:a435105305fe 293 commOutT.start(&commOutFn);
TrebleStick 12:1b2e2540e4e1 294
TrebleStick 20:a435105305fe 295 motorCtrlT.start(&motorCtrlFn);
TrebleStick 12:1b2e2540e4e1 296
TrebleStick 12:1b2e2540e4e1 297 decodeT.start(&decodeFn);
TrebleStick 12:1b2e2540e4e1 298
andrebharath 9:ecef1e8cbe3d 299 //Run the motor synchronisation
andrebharath 9:ecef1e8cbe3d 300 orState = motorHome();
TrebleStick 20:a435105305fe 301
andrebharath 9:ecef1e8cbe3d 302 //orState is subtracted from future rotor state inputs to align rotor and motor states
TrebleStick 12:1b2e2540e4e1 303
andrebharath 3:2e32d7974962 304 I1.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 305 I2.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 306 I3.rise(&photointerrupter_isr);
TrebleStick 12:1b2e2540e4e1 307
andrebharath 3:2e32d7974962 308 I1.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 309 I2.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 310 I3.fall(&photointerrupter_isr);
TrebleStick 12:1b2e2540e4e1 311
andrebharath 18:05e5d280a082 312
andrebharath 18:05e5d280a082 313 L1L.period_us(MAX_PWM_PERIOD);
andrebharath 18:05e5d280a082 314 L2L.period_us(MAX_PWM_PERIOD);
andrebharath 18:05e5d280a082 315 L3L.period_us(MAX_PWM_PERIOD);
andrebharath 18:05e5d280a082 316
andrebharath 9:ecef1e8cbe3d 317 //Calling the ISR once starts the motor movement
andrebharath 9:ecef1e8cbe3d 318 photointerrupter_isr();
TrebleStick 19:526fd700e1b3 319
TrebleStick 19:526fd700e1b3 320
TrebleStick 12:1b2e2540e4e1 321
andrebharath 9:ecef1e8cbe3d 322 SHA256 sha256;
TrebleStick 12:1b2e2540e4e1 323
andrebharath 3:2e32d7974962 324 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
andrebharath 3:2e32d7974962 325 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
andrebharath 3:2e32d7974962 326 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
andrebharath 3:2e32d7974962 327 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
andrebharath 3:2e32d7974962 328 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
andrebharath 3:2e32d7974962 329 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
andrebharath 3:2e32d7974962 330 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
andrebharath 3:2e32d7974962 331 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
andrebharath 3:2e32d7974962 332 uint64_t* key = (uint64_t*)((int)sequence + 48);
andrebharath 3:2e32d7974962 333 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
andrebharath 3:2e32d7974962 334 uint8_t hash[32];
TrebleStick 12:1b2e2540e4e1 335
andrebharath 9:ecef1e8cbe3d 336 Ticker hashcounter;
andrebharath 9:ecef1e8cbe3d 337 hashcounter.attach(&do_hashcount, 1.0);
TrebleStick 12:1b2e2540e4e1 338
TrebleStick 0:88c3d6c8a4eb 339 //Poll the rotor state and set the motor outputs accordingly to spin the motor
TrebleStick 0:88c3d6c8a4eb 340 while (1) {
TrebleStick 12:1b2e2540e4e1 341
andrebharath 9:ecef1e8cbe3d 342 *key = newKey;
TrebleStick 12:1b2e2540e4e1 343
andrebharath 9:ecef1e8cbe3d 344 sha256.computeHash(hash, sequence, 64);
TrebleStick 12:1b2e2540e4e1 345
andrebharath 3:2e32d7974962 346 if (hash[0] == 0 && hash[1] == 0) {
TrebleStick 15:bd303ab8a21f 347 //putMessage(MSG_NONCE_OK, *nonce);
andrebharath 3:2e32d7974962 348 }
andrebharath 3:2e32d7974962 349
andrebharath 3:2e32d7974962 350 (*nonce)++;
andrebharath 3:2e32d7974962 351 hashcount++;
TrebleStick 0:88c3d6c8a4eb 352 }
TrebleStick 0:88c3d6c8a4eb 353 }