Callum and Adel's changes on 12/02/19

Dependencies:   Crypto

Committer:
adehadd
Date:
Wed Mar 06 10:43:22 2019 +0000
Revision:
16:db7ef0a4aa23
Parent:
15:2f95f2fb68e3
Child:
17:5a443275680a
Child:
18:7ee632098fd4
Fixed global issue for callback

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CallumAlder 14:4e312fb83330 1 #include "SHA256.h"
estott 0:de4320f74764 2 #include "mbed.h"
CallumAlder 14:4e312fb83330 3 //#include <iostream>
CallumAlder 14:4e312fb83330 4
estott 0:de4320f74764 5
estott 0:de4320f74764 6 //Photointerrupter input pins
estott 10:a4b5723b6c9d 7 #define I1pin D3
estott 10:a4b5723b6c9d 8 #define I2pin D6
estott 10:a4b5723b6c9d 9 #define I3pin D5
estott 2:4e88faab6988 10
estott 2:4e88faab6988 11 //Incremental encoder input pins
estott 10:a4b5723b6c9d 12 #define CHApin D12
estott 10:a4b5723b6c9d 13 #define CHBpin D11
estott 0:de4320f74764 14
estott 0:de4320f74764 15 //Motor Drive output pins //Mask in output byte
estott 10:a4b5723b6c9d 16 #define L1Lpin D1 //0x01
estott 10:a4b5723b6c9d 17 #define L1Hpin A3 //0x02
estott 10:a4b5723b6c9d 18 #define L2Lpin D0 //0x04
estott 10:a4b5723b6c9d 19 #define L2Hpin A6 //0x08
estott 10:a4b5723b6c9d 20 #define L3Lpin D10 //0x10
estott 10:a4b5723b6c9d 21 #define L3Hpin D2 //0x20
estott 10:a4b5723b6c9d 22
estott 10:a4b5723b6c9d 23 #define PWMpin D9
estott 5:08f338b5e4d9 24
estott 5:08f338b5e4d9 25 //Motor current sense
estott 5:08f338b5e4d9 26 #define MCSPpin A1
estott 5:08f338b5e4d9 27 #define MCSNpin A0
estott 0:de4320f74764 28
estott 0:de4320f74764 29 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 30 /*
estott 0:de4320f74764 31 State L1 L2 L3
estott 0:de4320f74764 32 0 H - L
estott 0:de4320f74764 33 1 - H L
estott 0:de4320f74764 34 2 L H -
estott 0:de4320f74764 35 3 L - H
estott 0:de4320f74764 36 4 - L H
estott 0:de4320f74764 37 5 H L -
estott 0:de4320f74764 38 6 - - -
estott 0:de4320f74764 39 7 - - -
estott 0:de4320f74764 40 */
estott 0:de4320f74764 41 //Drive state to output table
estott 0:de4320f74764 42 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 43
estott 0:de4320f74764 44 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
estott 2:4e88faab6988 45 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 46 //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
estott 2:4e88faab6988 47
estott 2:4e88faab6988 48 //Phase lead to make motor spin
estott 3:569b35e2a602 49 const int8_t lead = 2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 50
estott 0:de4320f74764 51 //Status LED
estott 0:de4320f74764 52 DigitalOut led1(LED1);
estott 0:de4320f74764 53
estott 0:de4320f74764 54 //Photointerrupter inputs
iachinweze1 12:41b3112021a3 55 InterruptIn I1(I1pin);
iachinweze1 12:41b3112021a3 56 InterruptIn I2(I2pin);
iachinweze1 12:41b3112021a3 57 InterruptIn I3(I3pin);
estott 0:de4320f74764 58
estott 0:de4320f74764 59 //Motor Drive outputs
estott 0:de4320f74764 60 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 61 DigitalOut L1H(L1Hpin);
estott 0:de4320f74764 62 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 63 DigitalOut L2H(L2Hpin);
estott 0:de4320f74764 64 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 65 DigitalOut L3H(L3Hpin);
estott 0:de4320f74764 66
estott 0:de4320f74764 67 //Set a given drive state
estott 0:de4320f74764 68 void motorOut(int8_t driveState){
estott 0:de4320f74764 69
estott 2:4e88faab6988 70 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 71 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 72
estott 2:4e88faab6988 73 //Turn off first
estott 2:4e88faab6988 74 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 75 if (~driveOut & 0x02) L1H = 1;
estott 2:4e88faab6988 76 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 77 if (~driveOut & 0x08) L2H = 1;
estott 2:4e88faab6988 78 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 79 if (~driveOut & 0x20) L3H = 1;
estott 2:4e88faab6988 80
estott 2:4e88faab6988 81 //Then turn on
estott 2:4e88faab6988 82 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 83 if (driveOut & 0x02) L1H = 0;
estott 2:4e88faab6988 84 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 85 if (driveOut & 0x08) L2H = 0;
estott 2:4e88faab6988 86 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 87 if (driveOut & 0x20) L3H = 0;
estott 0:de4320f74764 88 }
estott 0:de4320f74764 89
estott 2:4e88faab6988 90 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 91 inline int8_t readRotorState(){
estott 2:4e88faab6988 92 return stateMap[I1 + 2*I2 + 4*I3];
estott 0:de4320f74764 93 }
estott 0:de4320f74764 94
estott 0:de4320f74764 95 //Basic synchronisation routine
estott 2:4e88faab6988 96 int8_t motorHome() {
estott 0:de4320f74764 97 //Put the motor in drive state 0 and wait for it to stabilise
estott 0:de4320f74764 98 motorOut(0);
estott 3:569b35e2a602 99 wait(2.0);
estott 0:de4320f74764 100
estott 0:de4320f74764 101 //Get the rotor state
estott 2:4e88faab6988 102 return readRotorState();
estott 0:de4320f74764 103 }
iachinweze1 12:41b3112021a3 104
adehadd 16:db7ef0a4aa23 105
adehadd 16:db7ef0a4aa23 106 void stateUpdate(int8_t *params[]) { // () { // **params
adehadd 16:db7ef0a4aa23 107 int8_t currentState = *params[0];
adehadd 16:db7ef0a4aa23 108 int8_t offset = *params[1];
iachinweze1 12:41b3112021a3 109 currentState = readRotorState();
adehadd 16:db7ef0a4aa23 110 motorOut((currentState - offset + lead + 6) % 6);
iachinweze1 12:41b3112021a3 111 }
iachinweze1 12:41b3112021a3 112
estott 0:de4320f74764 113 //Main
estott 0:de4320f74764 114 int main() {
estott 0:de4320f74764 115 //Initialise the serial port
CallumAlder 14:4e312fb83330 116 Serial pc(SERIAL_TX, SERIAL_RX);
CallumAlder 14:4e312fb83330 117 pc.printf("Hello\n\r");
CallumAlder 14:4e312fb83330 118
CallumAlder 14:4e312fb83330 119 // std::ios::sync_with_stdio(false);
CallumAlder 14:4e312fb83330 120 SHA256::SHA256 Miner;
CallumAlder 14:4e312fb83330 121
CallumAlder 14:4e312fb83330 122 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
CallumAlder 14:4e312fb83330 123 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
CallumAlder 14:4e312fb83330 124 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
CallumAlder 14:4e312fb83330 125 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
CallumAlder 14:4e312fb83330 126 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
CallumAlder 14:4e312fb83330 127 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
CallumAlder 14:4e312fb83330 128 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
CallumAlder 14:4e312fb83330 129 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
CallumAlder 14:4e312fb83330 130 uint64_t* key = (uint64_t*)((int)sequence + 48);
CallumAlder 14:4e312fb83330 131 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
CallumAlder 14:4e312fb83330 132 uint8_t hash[32];
CallumAlder 14:4e312fb83330 133 uint32_t length64 = 64;
CallumAlder 15:2f95f2fb68e3 134 uint32_t hashCounter = 0;
CallumAlder 15:2f95f2fb68e3 135 Timer timer;
estott 0:de4320f74764 136
adehadd 16:db7ef0a4aa23 137 // Motor States
adehadd 16:db7ef0a4aa23 138 int8_t orState = 0; //Rotot offset at motor state 0
adehadd 16:db7ef0a4aa23 139 int8_t currentState = 0; //Rotot offset at motor state 0
estott 0:de4320f74764 140 //Run the motor synchronisation
estott 2:4e88faab6988 141 orState = motorHome();
iachinweze1 12:41b3112021a3 142
CallumAlder 15:2f95f2fb68e3 143 // Add callbacks
adehadd 16:db7ef0a4aa23 144 // I1.fall(&stateUpdate);
adehadd 16:db7ef0a4aa23 145 // I2.fall(&stateUpdate);
adehadd 16:db7ef0a4aa23 146 // I3.fall(&stateUpdate);
adehadd 16:db7ef0a4aa23 147 int8_t* params[2];
adehadd 16:db7ef0a4aa23 148 params[0] = &currentState;
adehadd 16:db7ef0a4aa23 149 params[1] = &orState;
adehadd 16:db7ef0a4aa23 150
adehadd 16:db7ef0a4aa23 151 I1.fall(callback(&stateUpdate,params));
adehadd 16:db7ef0a4aa23 152 I2.fall(callback(&stateUpdate,params));
adehadd 16:db7ef0a4aa23 153 I3.fall(callback(&stateUpdate,params));
iachinweze1 12:41b3112021a3 154
CallumAlder 15:2f95f2fb68e3 155 // Push motor to move
iachinweze1 12:41b3112021a3 156 currentState = readRotorState();
iachinweze1 12:41b3112021a3 157 motorOut((currentState-orState+lead+6)%6); // We push it digitally
estott 0:de4320f74764 158
iachinweze1 12:41b3112021a3 159 // pc.printf("Rotor origin: %x\n\r",orState);
iachinweze1 12:41b3112021a3 160 // orState is subtracted from future rotor state inputs to align rotor and motor states
iachinweze1 12:41b3112021a3 161 // intState = readRotorState();
iachinweze1 12:41b3112021a3 162 //if (intState != intStateOld) {
iachinweze1 12:41b3112021a3 163 // pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6);
iachinweze1 12:41b3112021a3 164 // intStateOld = intState;
iachinweze1 12:41b3112021a3 165 // motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
iachinweze1 12:41b3112021a3 166 // }
iachinweze1 12:41b3112021a3 167
CallumAlder 15:2f95f2fb68e3 168
iachinweze1 12:41b3112021a3 169 // Keep the program running indefinitely
CallumAlder 15:2f95f2fb68e3 170 timer.start(); // start timer
CallumAlder 15:2f95f2fb68e3 171 while (1) {
CallumAlder 15:2f95f2fb68e3 172 // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6);
CallumAlder 15:2f95f2fb68e3 173 Miner.computeHash(hash, sequence, length64);
CallumAlder 15:2f95f2fb68e3 174 hashCounter++;
CallumAlder 15:2f95f2fb68e3 175 if ((hash[0]==0) && (hash[1]==0)){
CallumAlder 15:2f95f2fb68e3 176 pc.printf("hash: ");
CallumAlder 15:2f95f2fb68e3 177 for(int i = 0; i < 32; ++i)
CallumAlder 15:2f95f2fb68e3 178 pc.printf("%02x", hash[i]);
CallumAlder 15:2f95f2fb68e3 179 pc.printf("\n\r");
CallumAlder 15:2f95f2fb68e3 180 }
CallumAlder 15:2f95f2fb68e3 181
adehadd 16:db7ef0a4aa23 182 // Try a new nonce
CallumAlder 15:2f95f2fb68e3 183 (*nonce)++;
CallumAlder 15:2f95f2fb68e3 184
CallumAlder 15:2f95f2fb68e3 185 // Per Second i.e. when greater or equal to 1
CallumAlder 15:2f95f2fb68e3 186 if (timer.read() >= 1){
CallumAlder 15:2f95f2fb68e3 187 pc.printf("HashRate = %02u \n\r",hashCounter);
CallumAlder 15:2f95f2fb68e3 188 hashCounter=0;
CallumAlder 15:2f95f2fb68e3 189 timer.reset();
CallumAlder 15:2f95f2fb68e3 190 }
CallumAlder 15:2f95f2fb68e3 191 }
CallumAlder 15:2f95f2fb68e3 192
estott 0:de4320f74764 193 }
estott 0:de4320f74764 194
CallumAlder 15:2f95f2fb68e3 195
CallumAlder 15:2f95f2fb68e3 196
CallumAlder 15:2f95f2fb68e3 197