![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
An embedded device
Dependencies: Crypto
main.cpp@13:c51d828531d5, 2019-03-06 (annotated)
- Committer:
- peterith
- Date:
- Wed Mar 06 17:34:06 2019 +0000
- Revision:
- 13:c51d828531d5
- Parent:
- 12:899cd6bf9844
- Child:
- 14:0481b606d10e
complete lab 1
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 | |
estott | 0:de4320f74764 | 69 | //Set a given drive state |
estott | 0:de4320f74764 | 70 | void motorOut(int8_t driveState){ |
estott | 0:de4320f74764 | 71 | |
estott | 2:4e88faab6988 | 72 | //Lookup the output byte from the drive state. |
estott | 2:4e88faab6988 | 73 | int8_t driveOut = driveTable[driveState & 0x07]; |
estott | 2:4e88faab6988 | 74 | |
estott | 2:4e88faab6988 | 75 | //Turn off first |
estott | 2:4e88faab6988 | 76 | if (~driveOut & 0x01) L1L = 0; |
estott | 2:4e88faab6988 | 77 | if (~driveOut & 0x02) L1H = 1; |
estott | 2:4e88faab6988 | 78 | if (~driveOut & 0x04) L2L = 0; |
estott | 2:4e88faab6988 | 79 | if (~driveOut & 0x08) L2H = 1; |
estott | 2:4e88faab6988 | 80 | if (~driveOut & 0x10) L3L = 0; |
estott | 2:4e88faab6988 | 81 | if (~driveOut & 0x20) L3H = 1; |
estott | 2:4e88faab6988 | 82 | |
estott | 2:4e88faab6988 | 83 | //Then turn on |
estott | 2:4e88faab6988 | 84 | if (driveOut & 0x01) L1L = 1; |
estott | 2:4e88faab6988 | 85 | if (driveOut & 0x02) L1H = 0; |
estott | 2:4e88faab6988 | 86 | if (driveOut & 0x04) L2L = 1; |
estott | 2:4e88faab6988 | 87 | if (driveOut & 0x08) L2H = 0; |
estott | 2:4e88faab6988 | 88 | if (driveOut & 0x10) L3L = 1; |
estott | 2:4e88faab6988 | 89 | if (driveOut & 0x20) L3H = 0; |
peterith | 13:c51d828531d5 | 90 | } |
estott | 0:de4320f74764 | 91 | |
peterith | 13:c51d828531d5 | 92 | //Convert photointerrupter inputs to a rotor state |
estott | 0:de4320f74764 | 93 | inline int8_t readRotorState(){ |
estott | 2:4e88faab6988 | 94 | return stateMap[I1 + 2*I2 + 4*I3]; |
peterith | 13:c51d828531d5 | 95 | } |
estott | 0:de4320f74764 | 96 | |
estott | 2:4e88faab6988 | 97 | int8_t motorHome() { |
estott | 0:de4320f74764 | 98 | motorOut(0); |
estott | 3:569b35e2a602 | 99 | wait(2.0); |
estott | 0:de4320f74764 | 100 | |
estott | 2:4e88faab6988 | 101 | return readRotorState(); |
estott | 0:de4320f74764 | 102 | } |
peterith | 13:c51d828531d5 | 103 | |
peterith | 13:c51d828531d5 | 104 | void push() { |
peterith | 13:c51d828531d5 | 105 | intState = readRotorState(); |
peterith | 13:c51d828531d5 | 106 | if (intState != intStateOld) { |
peterith | 13:c51d828531d5 | 107 | intStateOld = intState; |
peterith | 13:c51d828531d5 | 108 | motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive |
peterith | 13:c51d828531d5 | 109 | } |
peterith | 13:c51d828531d5 | 110 | } |
peterith | 13:c51d828531d5 | 111 | |
peterith | 13:c51d828531d5 | 112 | int main() { |
estott | 0:de4320f74764 | 113 | Serial pc(SERIAL_TX, SERIAL_RX); |
peterith | 13:c51d828531d5 | 114 | pc.printf("Hello Pete\n\r"); |
estott | 0:de4320f74764 | 115 | |
estott | 2:4e88faab6988 | 116 | orState = motorHome(); |
peterith | 13:c51d828531d5 | 117 | pc.printf("Rotor origin: %x\n\r", orState); |
estott | 0:de4320f74764 | 118 | |
tanyuzhuo | 12:899cd6bf9844 | 119 | I1.rise(&push); |
tanyuzhuo | 12:899cd6bf9844 | 120 | I2.rise(&push); |
tanyuzhuo | 12:899cd6bf9844 | 121 | I3.rise(&push); |
peterith | 13:c51d828531d5 | 122 | |
peterith | 13:c51d828531d5 | 123 | I1.fall(&push); |
peterith | 13:c51d828531d5 | 124 | I2.fall(&push); |
peterith | 13:c51d828531d5 | 125 | I3.fall(&push); |
peterith | 13:c51d828531d5 | 126 | |
peterith | 13:c51d828531d5 | 127 | while(1) { |
peterith | 13:c51d828531d5 | 128 | SHA256 sha; |
peterith | 13:c51d828531d5 | 129 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
peterith | 13:c51d828531d5 | 130 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
peterith | 13:c51d828531d5 | 131 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
peterith | 13:c51d828531d5 | 132 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
peterith | 13:c51d828531d5 | 133 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
peterith | 13:c51d828531d5 | 134 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
peterith | 13:c51d828531d5 | 135 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
peterith | 13:c51d828531d5 | 136 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
peterith | 13:c51d828531d5 | 137 | uint64_t* key = (uint64_t*) ((int) sequence + 48); |
peterith | 13:c51d828531d5 | 138 | uint64_t* nonce = (uint64_t*) ((int) sequence + 56); |
peterith | 13:c51d828531d5 | 139 | uint8_t hash[32]; |
peterith | 13:c51d828531d5 | 140 | |
peterith | 13:c51d828531d5 | 141 | Timer t; |
peterith | 13:c51d828531d5 | 142 | |
peterith | 13:c51d828531d5 | 143 | t.start(); |
peterith | 13:c51d828531d5 | 144 | unsigned curr = 1; |
peterith | 13:c51d828531d5 | 145 | unsigned hashRate = 0; |
peterith | 13:c51d828531d5 | 146 | for (uint64_t i = 0; i <= 0b1111111111111111111111111111111111111111111111111111111111111111; i++) { |
peterith | 13:c51d828531d5 | 147 | hashRate++; |
peterith | 13:c51d828531d5 | 148 | (*nonce)++; |
peterith | 13:c51d828531d5 | 149 | sha.computeHash(&hash[0], &sequence[0], 64); |
peterith | 13:c51d828531d5 | 150 | if (hash[0] == 0 && hash[1] == 0) { |
peterith | 13:c51d828531d5 | 151 | pc.printf("Successful nonce: %016x\n\r", *nonce); |
peterith | 13:c51d828531d5 | 152 | } |
peterith | 13:c51d828531d5 | 153 | if ((unsigned) t.read() == curr) { |
peterith | 13:c51d828531d5 | 154 | curr++; |
peterith | 13:c51d828531d5 | 155 | pc.printf("Hash rate: %d\n\r", hashRate); |
peterith | 13:c51d828531d5 | 156 | hashRate = 0; |
peterith | 13:c51d828531d5 | 157 | } |
peterith | 13:c51d828531d5 | 158 | } |
peterith | 13:c51d828531d5 | 159 | } |
estott | 0:de4320f74764 | 160 | } |