First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Committer:
TrebleStick
Date:
Mon Mar 12 18:57:42 2018 +0000
Revision:
8:e7818c369bd3
Parent:
7:0d6632fba8d4
Child:
10:cedc98128562
Problems with the outputs and with the buffer length, ask me

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