First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Committer:
TrebleStick
Date:
Tue Mar 20 13:53:28 2018 +0000
Revision:
14:66746291017c
Parent:
13:ecccfc611025
Child:
15:bd303ab8a21f
Parser semi implemented;

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