![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
An embedded device
Dependencies: Crypto
main.cpp@16:10d53b056b17, 2019-03-12 (annotated)
- Committer:
- tanyuzhuo
- Date:
- Tue Mar 12 16:27:30 2019 +0000
- Revision:
- 16:10d53b056b17
- Parent:
- 15:f43d1d4e4260
- Child:
- 17:ff5300ba5442
Bitcoin mining moved to a separate thread; Priority setting (might be working lol)
Who changed what in which revision?
User | Revision | Line number | New 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); |
estott | 0:de4320f74764 | 64 | |
peterith | 13:c51d828531d5 | 65 | int8_t orState = 0; |
tanyuzhuo | 12:899cd6bf9844 | 66 | int8_t intState = 0; |
tanyuzhuo | 12:899cd6bf9844 | 67 | int8_t intStateOld = 0; |
tanyuzhuo | 12:899cd6bf9844 | 68 | |
peterith | 14:0481b606d10e | 69 | typedef struct { |
peterith | 14:0481b606d10e | 70 | uint64_t nonce; |
peterith | 14:0481b606d10e | 71 | } mail_t; |
peterith | 14:0481b606d10e | 72 | |
peterith | 14:0481b606d10e | 73 | Mail<mail_t, 16> mail_box; |
peterith | 14:0481b606d10e | 74 | Thread commandProcessorthread; |
tanyuzhuo | 16:10d53b056b17 | 75 | Thread bitcointhread; |
peterith | 14:0481b606d10e | 76 | RawSerial pc(SERIAL_TX, SERIAL_RX); |
peterith | 14:0481b606d10e | 77 | Queue<void, 8> inCharQ; |
peterith | 14:0481b606d10e | 78 | Mutex newKey_mutex; |
peterith | 14:0481b606d10e | 79 | uint64_t newKey = 0; |
peterith | 14:0481b606d10e | 80 | |
peterith | 14:0481b606d10e | 81 | void putMessage(uint64_t *nonce){ |
peterith | 14:0481b606d10e | 82 | mail_t *mail = mail_box.alloc(); |
peterith | 14:0481b606d10e | 83 | mail->nonce = *nonce; |
peterith | 14:0481b606d10e | 84 | mail_box.put(mail); |
peterith | 14:0481b606d10e | 85 | } |
peterith | 14:0481b606d10e | 86 | |
peterith | 14:0481b606d10e | 87 | void serialISR() { |
peterith | 14:0481b606d10e | 88 | uint8_t newChar = pc.getc(); |
peterith | 14:0481b606d10e | 89 | inCharQ.put((void*) newChar); |
peterith | 14:0481b606d10e | 90 | } |
peterith | 14:0481b606d10e | 91 | |
peterith | 14:0481b606d10e | 92 | void commandProcessor() { |
peterith | 14:0481b606d10e | 93 | pc.attach(&serialISR); |
peterith | 14:0481b606d10e | 94 | char command[18]; |
peterith | 14:0481b606d10e | 95 | //char k; |
peterith | 14:0481b606d10e | 96 | uint64_t receivedKey; |
peterith | 14:0481b606d10e | 97 | uint8_t index = 0; |
peterith | 14:0481b606d10e | 98 | while(1) { |
peterith | 14:0481b606d10e | 99 | osEvent newEvent = inCharQ.get(); |
peterith | 14:0481b606d10e | 100 | uint8_t newChar = (uint8_t) newEvent.value.p; |
peterith | 14:0481b606d10e | 101 | command[index] = newChar; |
peterith | 14:0481b606d10e | 102 | index++; |
peterith | 14:0481b606d10e | 103 | if (newChar == '\r') { |
peterith | 14:0481b606d10e | 104 | command[17] = '\0'; |
peterith | 14:0481b606d10e | 105 | receivedKey = strtoull(command, NULL, 16); |
peterith | 14:0481b606d10e | 106 | //receivedKey = 2147483648; |
peterith | 14:0481b606d10e | 107 | //sscanf(command, "%d", &receivedKey); |
tanyuzhuo | 15:f43d1d4e4260 | 108 | pc.printf("Received key: %016llx\n\r", receivedKey); |
peterith | 14:0481b606d10e | 109 | memset(command, 0, sizeof(command)); |
peterith | 14:0481b606d10e | 110 | newKey_mutex.lock(); |
peterith | 14:0481b606d10e | 111 | newKey = receivedKey; |
peterith | 14:0481b606d10e | 112 | newKey_mutex.unlock(); |
peterith | 14:0481b606d10e | 113 | index = 0; |
peterith | 14:0481b606d10e | 114 | } else { |
peterith | 14:0481b606d10e | 115 | pc.printf("Current command: %s\n\r", command); |
peterith | 14:0481b606d10e | 116 | } |
peterith | 14:0481b606d10e | 117 | } |
peterith | 14:0481b606d10e | 118 | } |
tanyuzhuo | 16:10d53b056b17 | 119 | void bitcoin(){ |
tanyuzhuo | 16:10d53b056b17 | 120 | while(1) { |
tanyuzhuo | 16:10d53b056b17 | 121 | SHA256 sha; |
tanyuzhuo | 16:10d53b056b17 | 122 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
tanyuzhuo | 16:10d53b056b17 | 123 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
tanyuzhuo | 16:10d53b056b17 | 124 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
tanyuzhuo | 16:10d53b056b17 | 125 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
tanyuzhuo | 16:10d53b056b17 | 126 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
tanyuzhuo | 16:10d53b056b17 | 127 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
tanyuzhuo | 16:10d53b056b17 | 128 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
tanyuzhuo | 16:10d53b056b17 | 129 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
tanyuzhuo | 16:10d53b056b17 | 130 | uint64_t* key = (uint64_t*) ((int) sequence + 48); |
tanyuzhuo | 16:10d53b056b17 | 131 | uint64_t* nonce = (uint64_t*) ((int) sequence + 56); |
tanyuzhuo | 16:10d53b056b17 | 132 | uint8_t hash[32]; |
tanyuzhuo | 16:10d53b056b17 | 133 | |
tanyuzhuo | 16:10d53b056b17 | 134 | Timer t; |
tanyuzhuo | 16:10d53b056b17 | 135 | t.start(); |
tanyuzhuo | 16:10d53b056b17 | 136 | unsigned currentTime = 0; |
tanyuzhuo | 16:10d53b056b17 | 137 | unsigned currentCount = 0; |
tanyuzhuo | 16:10d53b056b17 | 138 | |
tanyuzhuo | 16:10d53b056b17 | 139 | for (unsigned i = 0; i <= UINT_MAX; i++) { |
tanyuzhuo | 16:10d53b056b17 | 140 | (*nonce)++; |
tanyuzhuo | 16:10d53b056b17 | 141 | newKey_mutex.lock(); |
tanyuzhuo | 16:10d53b056b17 | 142 | *key = newKey; |
tanyuzhuo | 16:10d53b056b17 | 143 | newKey_mutex.unlock(); |
tanyuzhuo | 16:10d53b056b17 | 144 | sha.computeHash(hash, sequence, 64); |
tanyuzhuo | 16:10d53b056b17 | 145 | if (hash[0] == 0 && hash[1] == 0) { |
tanyuzhuo | 16:10d53b056b17 | 146 | //putMessage(nonce); |
tanyuzhuo | 16:10d53b056b17 | 147 | pc.printf("Successful nonce: %016x\n\r", *nonce); |
tanyuzhuo | 16:10d53b056b17 | 148 | } |
tanyuzhuo | 16:10d53b056b17 | 149 | if ((unsigned) t.read() == currentTime) { |
tanyuzhuo | 16:10d53b056b17 | 150 | //pc.printf("Hash rate: %d\n\r", i - currentCount); |
tanyuzhuo | 16:10d53b056b17 | 151 | pc.printf("Current key: %016llx\n\r", *key); |
tanyuzhuo | 16:10d53b056b17 | 152 | currentTime++; |
tanyuzhuo | 16:10d53b056b17 | 153 | currentCount = i; |
tanyuzhuo | 16:10d53b056b17 | 154 | } |
tanyuzhuo | 16:10d53b056b17 | 155 | } |
tanyuzhuo | 16:10d53b056b17 | 156 | t.stop(); |
tanyuzhuo | 16:10d53b056b17 | 157 | } |
tanyuzhuo | 16:10d53b056b17 | 158 | } |
estott | 0:de4320f74764 | 159 | //Set a given drive state |
estott | 0:de4320f74764 | 160 | void motorOut(int8_t driveState){ |
estott | 0:de4320f74764 | 161 | |
estott | 2:4e88faab6988 | 162 | //Lookup the output byte from the drive state. |
estott | 2:4e88faab6988 | 163 | int8_t driveOut = driveTable[driveState & 0x07]; |
estott | 2:4e88faab6988 | 164 | |
estott | 2:4e88faab6988 | 165 | //Turn off first |
estott | 2:4e88faab6988 | 166 | if (~driveOut & 0x01) L1L = 0; |
estott | 2:4e88faab6988 | 167 | if (~driveOut & 0x02) L1H = 1; |
estott | 2:4e88faab6988 | 168 | if (~driveOut & 0x04) L2L = 0; |
estott | 2:4e88faab6988 | 169 | if (~driveOut & 0x08) L2H = 1; |
estott | 2:4e88faab6988 | 170 | if (~driveOut & 0x10) L3L = 0; |
estott | 2:4e88faab6988 | 171 | if (~driveOut & 0x20) L3H = 1; |
estott | 2:4e88faab6988 | 172 | |
estott | 2:4e88faab6988 | 173 | //Then turn on |
estott | 2:4e88faab6988 | 174 | if (driveOut & 0x01) L1L = 1; |
estott | 2:4e88faab6988 | 175 | if (driveOut & 0x02) L1H = 0; |
estott | 2:4e88faab6988 | 176 | if (driveOut & 0x04) L2L = 1; |
estott | 2:4e88faab6988 | 177 | if (driveOut & 0x08) L2H = 0; |
estott | 2:4e88faab6988 | 178 | if (driveOut & 0x10) L3L = 1; |
estott | 2:4e88faab6988 | 179 | if (driveOut & 0x20) L3H = 0; |
peterith | 13:c51d828531d5 | 180 | } |
estott | 0:de4320f74764 | 181 | |
peterith | 13:c51d828531d5 | 182 | //Convert photointerrupter inputs to a rotor state |
estott | 0:de4320f74764 | 183 | inline int8_t readRotorState(){ |
estott | 2:4e88faab6988 | 184 | return stateMap[I1 + 2*I2 + 4*I3]; |
peterith | 13:c51d828531d5 | 185 | } |
estott | 0:de4320f74764 | 186 | |
estott | 2:4e88faab6988 | 187 | int8_t motorHome() { |
estott | 0:de4320f74764 | 188 | motorOut(0); |
estott | 3:569b35e2a602 | 189 | wait(2.0); |
estott | 2:4e88faab6988 | 190 | return readRotorState(); |
estott | 0:de4320f74764 | 191 | } |
peterith | 13:c51d828531d5 | 192 | |
peterith | 13:c51d828531d5 | 193 | void push() { |
peterith | 13:c51d828531d5 | 194 | intState = readRotorState(); |
peterith | 13:c51d828531d5 | 195 | if (intState != intStateOld) { |
peterith | 13:c51d828531d5 | 196 | intStateOld = intState; |
peterith | 13:c51d828531d5 | 197 | motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive |
peterith | 13:c51d828531d5 | 198 | } |
peterith | 13:c51d828531d5 | 199 | } |
peterith | 13:c51d828531d5 | 200 | |
peterith | 13:c51d828531d5 | 201 | int main() { |
peterith | 14:0481b606d10e | 202 | //Serial pc(SERIAL_TX, SERIAL_RX); |
peterith | 14:0481b606d10e | 203 | |
tanyuzhuo | 16:10d53b056b17 | 204 | bitcointhread.set_priority(osPriorityNormal); |
tanyuzhuo | 16:10d53b056b17 | 205 | commandProcessorthread.set_priority(osPriorityHigh); |
tanyuzhuo | 16:10d53b056b17 | 206 | |
peterith | 14:0481b606d10e | 207 | commandProcessorthread.start(commandProcessor); |
tanyuzhuo | 16:10d53b056b17 | 208 | bitcointhread.start(bitcoin); |
peterith | 14:0481b606d10e | 209 | |
peterith | 13:c51d828531d5 | 210 | pc.printf("Hello Pete\n\r"); |
peterith | 14:0481b606d10e | 211 | |
estott | 2:4e88faab6988 | 212 | orState = motorHome(); |
peterith | 13:c51d828531d5 | 213 | pc.printf("Rotor origin: %x\n\r", orState); |
estott | 0:de4320f74764 | 214 | |
tanyuzhuo | 12:899cd6bf9844 | 215 | I1.rise(&push); |
tanyuzhuo | 12:899cd6bf9844 | 216 | I2.rise(&push); |
tanyuzhuo | 12:899cd6bf9844 | 217 | I3.rise(&push); |
peterith | 13:c51d828531d5 | 218 | |
peterith | 13:c51d828531d5 | 219 | I1.fall(&push); |
peterith | 13:c51d828531d5 | 220 | I2.fall(&push); |
peterith | 13:c51d828531d5 | 221 | I3.fall(&push); |
peterith | 13:c51d828531d5 | 222 | |
tanyuzhuo | 16:10d53b056b17 | 223 | |
estott | 0:de4320f74764 | 224 | } |