This is probably never gonna get done

Dependencies:   Crypto

Committer:
tanyuzhuo
Date:
Sat Mar 16 15:53:32 2019 +0000
Revision:
17:ff5300ba5442
Parent:
16:10d53b056b17
Child:
18:e48c0910c71e
ppm implemented, expanded comms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
estott 0:de4320f74764 1 #include "mbed.h"
peterith 13:c51d828531d5 2 #include "Crypto.h"
estott 0:de4320f74764 3
estott 0:de4320f74764 4 //Photointerrupter input pins
estott 10:a4b5723b6c9d 5 #define I1pin D3
estott 10:a4b5723b6c9d 6 #define I2pin D6
estott 10:a4b5723b6c9d 7 #define I3pin D5
estott 2:4e88faab6988 8
estott 2:4e88faab6988 9 //Incremental encoder input pins
peterith 13:c51d828531d5 10 #define CHApin D12
peterith 13:c51d828531d5 11 #define CHBpin D11
estott 0:de4320f74764 12
estott 0:de4320f74764 13 //Motor Drive output pins //Mask in output byte
estott 10:a4b5723b6c9d 14 #define L1Lpin D1 //0x01
estott 10:a4b5723b6c9d 15 #define L1Hpin A3 //0x02
estott 10:a4b5723b6c9d 16 #define L2Lpin D0 //0x04
peterith 13:c51d828531d5 17 #define L2Hpin A6 //0x08
peterith 13:c51d828531d5 18 #define L3Lpin D10 //0x10
peterith 13:c51d828531d5 19 #define L3Hpin D2 //0x20
estott 10:a4b5723b6c9d 20
estott 10:a4b5723b6c9d 21 #define PWMpin D9
estott 5:08f338b5e4d9 22
estott 5:08f338b5e4d9 23 //Motor current sense
peterith 13:c51d828531d5 24 #define MCSPpin A1
peterith 13:c51d828531d5 25 #define MCSNpin A0
estott 0:de4320f74764 26
estott 0:de4320f74764 27 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 28 /*
estott 0:de4320f74764 29 State L1 L2 L3
estott 0:de4320f74764 30 0 H - L
estott 0:de4320f74764 31 1 - H L
estott 0:de4320f74764 32 2 L H -
estott 0:de4320f74764 33 3 L - H
estott 0:de4320f74764 34 4 - L H
estott 0:de4320f74764 35 5 H L -
estott 0:de4320f74764 36 6 - - -
estott 0:de4320f74764 37 7 - - -
estott 0:de4320f74764 38 */
estott 0:de4320f74764 39 //Drive state to output table
estott 0:de4320f74764 40 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 41
estott 0:de4320f74764 42 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
estott 2:4e88faab6988 43 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 44 //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 45
estott 2:4e88faab6988 46 //Phase lead to make motor spin
estott 3:569b35e2a602 47 const int8_t lead = 2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 48
estott 0:de4320f74764 49 //Status LED
estott 0:de4320f74764 50 DigitalOut led1(LED1);
estott 0:de4320f74764 51
estott 0:de4320f74764 52 //Photointerrupter inputs
tanyuzhuo 12:899cd6bf9844 53 InterruptIn I1(I1pin);
tanyuzhuo 12:899cd6bf9844 54 InterruptIn I2(I2pin);
tanyuzhuo 12:899cd6bf9844 55 InterruptIn I3(I3pin);
tanyuzhuo 12:899cd6bf9844 56
estott 0:de4320f74764 57 //Motor Drive outputs
estott 0:de4320f74764 58 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 59 DigitalOut L1H(L1Hpin);
estott 0:de4320f74764 60 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 61 DigitalOut L2H(L2Hpin);
estott 0:de4320f74764 62 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 63 DigitalOut L3H(L3Hpin);
tanyuzhuo 17:ff5300ba5442 64 PwmOut PWM(PWMpin);
estott 0:de4320f74764 65
peterith 13:c51d828531d5 66 int8_t orState = 0;
tanyuzhuo 12:899cd6bf9844 67 int8_t intState = 0;
tanyuzhuo 12:899cd6bf9844 68 int8_t intStateOld = 0;
tanyuzhuo 17:ff5300ba5442 69 int32_t revoCounter = 0; //Counts the number of revolutions
tanyuzhuo 12:899cd6bf9844 70
peterith 14:0481b606d10e 71 typedef struct {
peterith 14:0481b606d10e 72 uint64_t nonce;
peterith 14:0481b606d10e 73 } mail_t;
peterith 14:0481b606d10e 74
peterith 14:0481b606d10e 75 Mail<mail_t, 16> mail_box;
peterith 14:0481b606d10e 76 Thread commandProcessorthread;
tanyuzhuo 16:10d53b056b17 77 Thread bitcointhread;
peterith 14:0481b606d10e 78 RawSerial pc(SERIAL_TX, SERIAL_RX);
peterith 14:0481b606d10e 79 Queue<void, 8> inCharQ;
peterith 14:0481b606d10e 80 Mutex newKey_mutex;
peterith 14:0481b606d10e 81 uint64_t newKey = 0;
peterith 14:0481b606d10e 82
peterith 14:0481b606d10e 83 void putMessage(uint64_t *nonce){
peterith 14:0481b606d10e 84 mail_t *mail = mail_box.alloc();
peterith 14:0481b606d10e 85 mail->nonce = *nonce;
peterith 14:0481b606d10e 86 mail_box.put(mail);
peterith 14:0481b606d10e 87 }
peterith 14:0481b606d10e 88
peterith 14:0481b606d10e 89 void serialISR() {
peterith 14:0481b606d10e 90 uint8_t newChar = pc.getc();
peterith 14:0481b606d10e 91 inCharQ.put((void*) newChar);
peterith 14:0481b606d10e 92 }
peterith 14:0481b606d10e 93
peterith 14:0481b606d10e 94 void commandProcessor() {
peterith 14:0481b606d10e 95 pc.attach(&serialISR);
tanyuzhuo 17:ff5300ba5442 96 char command[19];
tanyuzhuo 17:ff5300ba5442 97 char* number;
peterith 14:0481b606d10e 98 //char k;
peterith 14:0481b606d10e 99 uint64_t receivedKey;
peterith 14:0481b606d10e 100 uint8_t index = 0;
peterith 14:0481b606d10e 101 while(1) {
peterith 14:0481b606d10e 102 osEvent newEvent = inCharQ.get();
peterith 14:0481b606d10e 103 uint8_t newChar = (uint8_t) newEvent.value.p;
peterith 14:0481b606d10e 104 command[index] = newChar;
peterith 14:0481b606d10e 105 index++;
tanyuzhuo 17:ff5300ba5442 106 if (newChar == '\r') {
peterith 14:0481b606d10e 107 command[17] = '\0';
tanyuzhuo 17:ff5300ba5442 108
tanyuzhuo 17:ff5300ba5442 109 if (command[0] == 'R') {
tanyuzhuo 17:ff5300ba5442 110 pc.printf("Rotation command\n");
tanyuzhuo 17:ff5300ba5442 111 pc.printf("%s", command);
tanyuzhuo 17:ff5300ba5442 112 }
tanyuzhuo 17:ff5300ba5442 113 else if (command[0] == 'V') {
tanyuzhuo 17:ff5300ba5442 114 pc.printf("Max speed command\n");
tanyuzhuo 17:ff5300ba5442 115 pc.printf("%s", command);
tanyuzhuo 17:ff5300ba5442 116 }
tanyuzhuo 17:ff5300ba5442 117 else if (command[0] == 'K') {
tanyuzhuo 17:ff5300ba5442 118 if (index == 18){ // when index is 18 means you entered K and 16 digits
tanyuzhuo 17:ff5300ba5442 119 number = command +1; //super bad solution, but I don't know how to work with strings in C
tanyuzhuo 17:ff5300ba5442 120 receivedKey = strtoull(number, NULL, 16);
tanyuzhuo 17:ff5300ba5442 121 //receivedKey = 2147483648;
tanyuzhuo 17:ff5300ba5442 122 //sscanf(command, "%d", &receivedKey);
tanyuzhuo 17:ff5300ba5442 123 pc.printf("Received key: %016llx\n\r", receivedKey);
tanyuzhuo 17:ff5300ba5442 124 newKey_mutex.lock();
tanyuzhuo 17:ff5300ba5442 125 newKey = receivedKey;
tanyuzhuo 17:ff5300ba5442 126 newKey_mutex.unlock();
tanyuzhuo 17:ff5300ba5442 127 } else {
tanyuzhuo 17:ff5300ba5442 128 pc.printf("Not a valid key!");
tanyuzhuo 17:ff5300ba5442 129 };
tanyuzhuo 17:ff5300ba5442 130 }
tanyuzhuo 17:ff5300ba5442 131 else if (command[0] == 'T') {
tanyuzhuo 17:ff5300ba5442 132 pc.printf("Melody command\n");
tanyuzhuo 17:ff5300ba5442 133 pc.printf("%s", command);
tanyuzhuo 17:ff5300ba5442 134 }
peterith 14:0481b606d10e 135 memset(command, 0, sizeof(command));
peterith 14:0481b606d10e 136 index = 0;
peterith 14:0481b606d10e 137 } else {
peterith 14:0481b606d10e 138 pc.printf("Current command: %s\n\r", command);
peterith 14:0481b606d10e 139 }
peterith 14:0481b606d10e 140 }
peterith 14:0481b606d10e 141 }
tanyuzhuo 17:ff5300ba5442 142
tanyuzhuo 16:10d53b056b17 143 void bitcoin(){
tanyuzhuo 16:10d53b056b17 144 while(1) {
tanyuzhuo 16:10d53b056b17 145 SHA256 sha;
tanyuzhuo 16:10d53b056b17 146 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
tanyuzhuo 16:10d53b056b17 147 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
tanyuzhuo 16:10d53b056b17 148 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
tanyuzhuo 16:10d53b056b17 149 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
tanyuzhuo 16:10d53b056b17 150 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
tanyuzhuo 16:10d53b056b17 151 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
tanyuzhuo 16:10d53b056b17 152 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
tanyuzhuo 16:10d53b056b17 153 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
tanyuzhuo 16:10d53b056b17 154 uint64_t* key = (uint64_t*) ((int) sequence + 48);
tanyuzhuo 16:10d53b056b17 155 uint64_t* nonce = (uint64_t*) ((int) sequence + 56);
tanyuzhuo 16:10d53b056b17 156 uint8_t hash[32];
tanyuzhuo 16:10d53b056b17 157
tanyuzhuo 16:10d53b056b17 158 Timer t;
tanyuzhuo 16:10d53b056b17 159 t.start();
tanyuzhuo 16:10d53b056b17 160 unsigned currentTime = 0;
tanyuzhuo 16:10d53b056b17 161 unsigned currentCount = 0;
tanyuzhuo 16:10d53b056b17 162
tanyuzhuo 16:10d53b056b17 163 for (unsigned i = 0; i <= UINT_MAX; i++) {
tanyuzhuo 16:10d53b056b17 164 (*nonce)++;
tanyuzhuo 16:10d53b056b17 165 newKey_mutex.lock();
tanyuzhuo 16:10d53b056b17 166 *key = newKey;
tanyuzhuo 16:10d53b056b17 167 newKey_mutex.unlock();
tanyuzhuo 16:10d53b056b17 168 sha.computeHash(hash, sequence, 64);
tanyuzhuo 16:10d53b056b17 169 if (hash[0] == 0 && hash[1] == 0) {
tanyuzhuo 16:10d53b056b17 170 //putMessage(nonce);
tanyuzhuo 16:10d53b056b17 171 pc.printf("Successful nonce: %016x\n\r", *nonce);
tanyuzhuo 16:10d53b056b17 172 }
tanyuzhuo 16:10d53b056b17 173 if ((unsigned) t.read() == currentTime) {
tanyuzhuo 16:10d53b056b17 174 //pc.printf("Hash rate: %d\n\r", i - currentCount);
tanyuzhuo 16:10d53b056b17 175 pc.printf("Current key: %016llx\n\r", *key);
tanyuzhuo 16:10d53b056b17 176 currentTime++;
tanyuzhuo 16:10d53b056b17 177 currentCount = i;
tanyuzhuo 16:10d53b056b17 178 }
tanyuzhuo 16:10d53b056b17 179 }
tanyuzhuo 16:10d53b056b17 180 t.stop();
tanyuzhuo 16:10d53b056b17 181 }
tanyuzhuo 16:10d53b056b17 182 }
estott 0:de4320f74764 183 //Set a given drive state
estott 0:de4320f74764 184 void motorOut(int8_t driveState){
estott 0:de4320f74764 185
estott 2:4e88faab6988 186 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 187 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 188
estott 2:4e88faab6988 189 //Turn off first
estott 2:4e88faab6988 190 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 191 if (~driveOut & 0x02) L1H = 1;
estott 2:4e88faab6988 192 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 193 if (~driveOut & 0x08) L2H = 1;
estott 2:4e88faab6988 194 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 195 if (~driveOut & 0x20) L3H = 1;
estott 2:4e88faab6988 196
estott 2:4e88faab6988 197 //Then turn on
estott 2:4e88faab6988 198 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 199 if (driveOut & 0x02) L1H = 0;
estott 2:4e88faab6988 200 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 201 if (driveOut & 0x08) L2H = 0;
estott 2:4e88faab6988 202 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 203 if (driveOut & 0x20) L3H = 0;
peterith 13:c51d828531d5 204 }
estott 0:de4320f74764 205
peterith 13:c51d828531d5 206 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 207 inline int8_t readRotorState(){
estott 2:4e88faab6988 208 return stateMap[I1 + 2*I2 + 4*I3];
peterith 13:c51d828531d5 209 }
estott 0:de4320f74764 210
estott 2:4e88faab6988 211 int8_t motorHome() {
estott 0:de4320f74764 212 motorOut(0);
estott 3:569b35e2a602 213 wait(2.0);
estott 2:4e88faab6988 214 return readRotorState();
estott 0:de4320f74764 215 }
peterith 13:c51d828531d5 216
peterith 13:c51d828531d5 217 void push() {
peterith 13:c51d828531d5 218 intState = readRotorState();
peterith 13:c51d828531d5 219 if (intState != intStateOld) {
peterith 13:c51d828531d5 220 intStateOld = intState;
peterith 13:c51d828531d5 221 motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive
peterith 13:c51d828531d5 222 }
peterith 13:c51d828531d5 223 }
peterith 13:c51d828531d5 224
peterith 13:c51d828531d5 225 int main() {
peterith 14:0481b606d10e 226 //Serial pc(SERIAL_TX, SERIAL_RX);
peterith 14:0481b606d10e 227
tanyuzhuo 17:ff5300ba5442 228 //Initialise bincoin mining and communication
tanyuzhuo 16:10d53b056b17 229 bitcointhread.set_priority(osPriorityNormal);
tanyuzhuo 16:10d53b056b17 230 commandProcessorthread.set_priority(osPriorityHigh);
peterith 14:0481b606d10e 231 commandProcessorthread.start(commandProcessor);
tanyuzhuo 16:10d53b056b17 232 bitcointhread.start(bitcoin);
tanyuzhuo 17:ff5300ba5442 233
tanyuzhuo 17:ff5300ba5442 234 PWM.period(0.002f); //Set PWM period in seconds
tanyuzhuo 17:ff5300ba5442 235 PWM.write(0.5); //Set PWM duty in %
peterith 14:0481b606d10e 236
peterith 13:c51d828531d5 237 pc.printf("Hello Pete\n\r");
peterith 14:0481b606d10e 238
estott 2:4e88faab6988 239 orState = motorHome();
peterith 13:c51d828531d5 240 pc.printf("Rotor origin: %x\n\r", orState);
estott 0:de4320f74764 241
tanyuzhuo 12:899cd6bf9844 242 I1.rise(&push);
tanyuzhuo 12:899cd6bf9844 243 I2.rise(&push);
tanyuzhuo 12:899cd6bf9844 244 I3.rise(&push);
peterith 13:c51d828531d5 245
peterith 13:c51d828531d5 246 I1.fall(&push);
peterith 13:c51d828531d5 247 I2.fall(&push);
peterith 13:c51d828531d5 248 I3.fall(&push);
peterith 13:c51d828531d5 249
tanyuzhuo 16:10d53b056b17 250
estott 0:de4320f74764 251 }