fork of what I have been writing
ES_CW2_Starter_STARFISH/main.cpp@2:60ec222456b6, 2020-02-28 (annotated)
- Committer:
- kubitz
- Date:
- Fri Feb 28 18:00:01 2020 +0000
- Revision:
- 2:60ec222456b6
- Parent:
- 1:e9be8b2c8c69
- Child:
- 3:8443825642d1
added rtos and email
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kubitz | 0:19fd8c1944fb | 1 | #include "mbed.h" |
kubitz | 0:19fd8c1944fb | 2 | #include "SHA256.h" |
kubitz | 2:60ec222456b6 | 3 | #include "rtos.h" |
kubitz | 0:19fd8c1944fb | 4 | |
kubitz | 0:19fd8c1944fb | 5 | Timer timer_nonce; |
kubitz | 0:19fd8c1944fb | 6 | |
kubitz | 1:e9be8b2c8c69 | 7 | typedef struct { |
kubitz | 2:60ec222456b6 | 8 | uint8_t hash[32]; /* hash of successful nonce */ |
kubitz | 1:e9be8b2c8c69 | 9 | } mail_t; |
kubitz | 1:e9be8b2c8c69 | 10 | |
kubitz | 1:e9be8b2c8c69 | 11 | Mail<mail_t, 16> mail_box; |
kubitz | 0:19fd8c1944fb | 12 | |
kubitz | 0:19fd8c1944fb | 13 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
kubitz | 0:19fd8c1944fb | 14 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
kubitz | 0:19fd8c1944fb | 15 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
kubitz | 0:19fd8c1944fb | 16 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
kubitz | 0:19fd8c1944fb | 17 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
kubitz | 0:19fd8c1944fb | 18 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
kubitz | 0:19fd8c1944fb | 19 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
kubitz | 0:19fd8c1944fb | 20 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
kubitz | 1:e9be8b2c8c69 | 21 | |
kubitz | 0:19fd8c1944fb | 22 | uint64_t* key = (uint64_t*)&sequence[48]; |
kubitz | 0:19fd8c1944fb | 23 | uint64_t* nonce = (uint64_t*)&sequence[56]; |
kubitz | 0:19fd8c1944fb | 24 | uint32_t successful_nonce = 0; |
kubitz | 0:19fd8c1944fb | 25 | uint32_t last_nonce_number = 0; |
kubitz | 0:19fd8c1944fb | 26 | uint8_t hash[32]; |
kubitz | 0:19fd8c1944fb | 27 | uint32_t previous_time; |
kubitz | 1:e9be8b2c8c69 | 28 | |
kubitz | 1:e9be8b2c8c69 | 29 | |
kubitz | 0:19fd8c1944fb | 30 | //Photointerrupter input pins |
kubitz | 0:19fd8c1944fb | 31 | #define I1pin D3 |
kubitz | 0:19fd8c1944fb | 32 | #define I2pin D6 |
kubitz | 0:19fd8c1944fb | 33 | #define I3pin D5 |
kubitz | 0:19fd8c1944fb | 34 | |
kubitz | 0:19fd8c1944fb | 35 | //Incremental encoder input pins |
kubitz | 0:19fd8c1944fb | 36 | #define CHApin D12 |
kubitz | 0:19fd8c1944fb | 37 | #define CHBpin D11 |
kubitz | 0:19fd8c1944fb | 38 | |
kubitz | 0:19fd8c1944fb | 39 | //Motor Drive output pins //Mask in output byte |
kubitz | 0:19fd8c1944fb | 40 | #define L1Lpin D1 //0x01 |
kubitz | 0:19fd8c1944fb | 41 | #define L1Hpin A3 //0x02 |
kubitz | 0:19fd8c1944fb | 42 | #define L2Lpin D0 //0x04 |
kubitz | 0:19fd8c1944fb | 43 | #define L2Hpin A6 //0x08 |
kubitz | 0:19fd8c1944fb | 44 | #define L3Lpin D10 //0x10 |
kubitz | 0:19fd8c1944fb | 45 | #define L3Hpin D2 //0x20 |
kubitz | 0:19fd8c1944fb | 46 | |
kubitz | 0:19fd8c1944fb | 47 | #define PWMpin D9 |
kubitz | 0:19fd8c1944fb | 48 | |
kubitz | 0:19fd8c1944fb | 49 | //Motor current sense |
kubitz | 0:19fd8c1944fb | 50 | #define MCSPpin A1 |
kubitz | 0:19fd8c1944fb | 51 | #define MCSNpin A0 |
kubitz | 0:19fd8c1944fb | 52 | |
kubitz | 0:19fd8c1944fb | 53 | //Test outputs |
kubitz | 0:19fd8c1944fb | 54 | #define TP0pin D4 |
kubitz | 0:19fd8c1944fb | 55 | #define TP1pin D13 |
kubitz | 0:19fd8c1944fb | 56 | #define TP2pin A2 |
kubitz | 0:19fd8c1944fb | 57 | |
kubitz | 0:19fd8c1944fb | 58 | //Mapping from sequential drive states to motor phase outputs |
kubitz | 0:19fd8c1944fb | 59 | /* |
kubitz | 0:19fd8c1944fb | 60 | State L1 L2 L3 |
kubitz | 0:19fd8c1944fb | 61 | 0 H - L |
kubitz | 0:19fd8c1944fb | 62 | 1 - H L |
kubitz | 0:19fd8c1944fb | 63 | 2 L H - |
kubitz | 0:19fd8c1944fb | 64 | 3 L - H |
kubitz | 0:19fd8c1944fb | 65 | 4 - L H |
kubitz | 0:19fd8c1944fb | 66 | 5 H L - |
kubitz | 0:19fd8c1944fb | 67 | 6 - - - |
kubitz | 0:19fd8c1944fb | 68 | 7 - - - |
kubitz | 0:19fd8c1944fb | 69 | */ |
kubitz | 0:19fd8c1944fb | 70 | //Drive state to output table |
kubitz | 0:19fd8c1944fb | 71 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
kubitz | 0:19fd8c1944fb | 72 | |
kubitz | 0:19fd8c1944fb | 73 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
kubitz | 0:19fd8c1944fb | 74 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
kubitz | 0:19fd8c1944fb | 75 | //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed |
kubitz | 0:19fd8c1944fb | 76 | |
kubitz | 0:19fd8c1944fb | 77 | //Phase lead to make motor spin |
kubitz | 0:19fd8c1944fb | 78 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
kubitz | 0:19fd8c1944fb | 79 | |
kubitz | 0:19fd8c1944fb | 80 | //Status LED |
kubitz | 0:19fd8c1944fb | 81 | DigitalOut led1(LED1); |
kubitz | 0:19fd8c1944fb | 82 | |
kubitz | 0:19fd8c1944fb | 83 | //Photointerrupter inputs |
kubitz | 0:19fd8c1944fb | 84 | InterruptIn I1(I1pin); |
kubitz | 0:19fd8c1944fb | 85 | InterruptIn I2(I2pin); |
kubitz | 0:19fd8c1944fb | 86 | InterruptIn I3(I3pin); |
kubitz | 0:19fd8c1944fb | 87 | |
kubitz | 0:19fd8c1944fb | 88 | //Motor Drive outputs |
kubitz | 0:19fd8c1944fb | 89 | DigitalOut L1L(L1Lpin); |
kubitz | 0:19fd8c1944fb | 90 | DigitalOut L1H(L1Hpin); |
kubitz | 0:19fd8c1944fb | 91 | DigitalOut L2L(L2Lpin); |
kubitz | 0:19fd8c1944fb | 92 | DigitalOut L2H(L2Hpin); |
kubitz | 0:19fd8c1944fb | 93 | DigitalOut L3L(L3Lpin); |
kubitz | 0:19fd8c1944fb | 94 | DigitalOut L3H(L3Hpin); |
kubitz | 0:19fd8c1944fb | 95 | |
kubitz | 0:19fd8c1944fb | 96 | DigitalOut TP1(TP1pin); |
kubitz | 0:19fd8c1944fb | 97 | PwmOut MotorPWM(PWMpin); |
kubitz | 0:19fd8c1944fb | 98 | |
kubitz | 0:19fd8c1944fb | 99 | int8_t orState = 0; //Rotot offset at motor state 0 |
kubitz | 0:19fd8c1944fb | 100 | int8_t intState = 0; |
kubitz | 0:19fd8c1944fb | 101 | int8_t intStateOld = 0; |
kubitz | 0:19fd8c1944fb | 102 | |
kubitz | 0:19fd8c1944fb | 103 | |
kubitz | 0:19fd8c1944fb | 104 | //Set a given drive state |
kubitz | 0:19fd8c1944fb | 105 | void motorOut(int8_t driveState){ |
kubitz | 0:19fd8c1944fb | 106 | |
kubitz | 0:19fd8c1944fb | 107 | //Lookup the output byte from the drive state. |
kubitz | 0:19fd8c1944fb | 108 | int8_t driveOut = driveTable[driveState & 0x07]; |
kubitz | 0:19fd8c1944fb | 109 | |
kubitz | 0:19fd8c1944fb | 110 | //Turn off first |
kubitz | 0:19fd8c1944fb | 111 | if (~driveOut & 0x01) L1L = 0; |
kubitz | 0:19fd8c1944fb | 112 | if (~driveOut & 0x02) L1H = 1; |
kubitz | 0:19fd8c1944fb | 113 | if (~driveOut & 0x04) L2L = 0; |
kubitz | 0:19fd8c1944fb | 114 | if (~driveOut & 0x08) L2H = 1; |
kubitz | 0:19fd8c1944fb | 115 | if (~driveOut & 0x10) L3L = 0; |
kubitz | 0:19fd8c1944fb | 116 | if (~driveOut & 0x20) L3H = 1; |
kubitz | 0:19fd8c1944fb | 117 | |
kubitz | 0:19fd8c1944fb | 118 | //Then turn on |
kubitz | 0:19fd8c1944fb | 119 | if (driveOut & 0x01) L1L = 1; |
kubitz | 0:19fd8c1944fb | 120 | if (driveOut & 0x02) L1H = 0; |
kubitz | 0:19fd8c1944fb | 121 | if (driveOut & 0x04) L2L = 1; |
kubitz | 0:19fd8c1944fb | 122 | if (driveOut & 0x08) L2H = 0; |
kubitz | 0:19fd8c1944fb | 123 | if (driveOut & 0x10) L3L = 1; |
kubitz | 0:19fd8c1944fb | 124 | if (driveOut & 0x20) L3H = 0; |
kubitz | 0:19fd8c1944fb | 125 | } |
kubitz | 0:19fd8c1944fb | 126 | |
kubitz | 0:19fd8c1944fb | 127 | //Convert photointerrupter inputs to a rotor state |
kubitz | 0:19fd8c1944fb | 128 | inline int8_t readRotorState(){ |
kubitz | 0:19fd8c1944fb | 129 | return stateMap[I1 + 2*I2 + 4*I3]; |
kubitz | 0:19fd8c1944fb | 130 | } |
kubitz | 0:19fd8c1944fb | 131 | |
kubitz | 0:19fd8c1944fb | 132 | //Basic synchronisation routine |
kubitz | 0:19fd8c1944fb | 133 | int8_t motorHome() { |
kubitz | 0:19fd8c1944fb | 134 | //Put the motor in drive state 0 and wait for it to stabilise |
kubitz | 0:19fd8c1944fb | 135 | motorOut(0); |
kubitz | 0:19fd8c1944fb | 136 | wait(2.0); |
kubitz | 0:19fd8c1944fb | 137 | |
kubitz | 0:19fd8c1944fb | 138 | //Get the rotor state |
kubitz | 0:19fd8c1944fb | 139 | return readRotorState(); |
kubitz | 0:19fd8c1944fb | 140 | } |
kubitz | 0:19fd8c1944fb | 141 | |
kubitz | 0:19fd8c1944fb | 142 | void move() { |
kubitz | 0:19fd8c1944fb | 143 | intState = readRotorState(); |
kubitz | 0:19fd8c1944fb | 144 | motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive |
kubitz | 0:19fd8c1944fb | 145 | intStateOld = intState; |
kubitz | 0:19fd8c1944fb | 146 | } |
kubitz | 0:19fd8c1944fb | 147 | |
kubitz | 1:e9be8b2c8c69 | 148 | void print_crypto_thread() { |
kubitz | 1:e9be8b2c8c69 | 149 | while (true) { |
kubitz | 1:e9be8b2c8c69 | 150 | osEvent evt = mail_box.get(); |
kubitz | 1:e9be8b2c8c69 | 151 | if (evt.status == osEventMail) { |
kubitz | 1:e9be8b2c8c69 | 152 | mail_t *mail = (mail_t*)evt.value.p; |
kubitz | 2:60ec222456b6 | 153 | |
kubitz | 2:60ec222456b6 | 154 | printf("Number of cycles: %u\n\r", mail->hash); |
kubitz | 1:e9be8b2c8c69 | 155 | mail_box.free(mail); |
kubitz | 1:e9be8b2c8c69 | 156 | } |
kubitz | 1:e9be8b2c8c69 | 157 | } |
kubitz | 2:60ec222456b6 | 158 | |
kubitz | 2:60ec222456b6 | 159 | void putMessage(uint8_t hash[32]){ |
kubitz | 2:60ec222456b6 | 160 | mail_t *mail = mail_box.alloc(); |
kubitz | 2:60ec222456b6 | 161 | mail->hash = hash; |
kubitz | 2:60ec222456b6 | 162 | mail_box.put(mail); |
kubitz | 2:60ec222456b6 | 163 | } |
kubitz | 0:19fd8c1944fb | 164 | |
kubitz | 0:19fd8c1944fb | 165 | //Main |
kubitz | 0:19fd8c1944fb | 166 | int main() { |
kubitz | 0:19fd8c1944fb | 167 | |
kubitz | 0:19fd8c1944fb | 168 | const int32_t PWM_PRD = 2500; |
kubitz | 0:19fd8c1944fb | 169 | MotorPWM.period_us(PWM_PRD); |
kubitz | 0:19fd8c1944fb | 170 | MotorPWM.pulsewidth_us(PWM_PRD); |
kubitz | 0:19fd8c1944fb | 171 | |
kubitz | 0:19fd8c1944fb | 172 | //Initialise the serial port |
kubitz | 0:19fd8c1944fb | 173 | Serial pc(SERIAL_TX, SERIAL_RX); |
kubitz | 0:19fd8c1944fb | 174 | pc.printf("Hello\n\r"); |
kubitz | 0:19fd8c1944fb | 175 | |
kubitz | 0:19fd8c1944fb | 176 | //Run the motor synchronisation |
kubitz | 0:19fd8c1944fb | 177 | orState = motorHome(); |
kubitz | 0:19fd8c1944fb | 178 | pc.printf("Rotor origin: %x\n\r",orState); |
kubitz | 0:19fd8c1944fb | 179 | |
kubitz | 0:19fd8c1944fb | 180 | I1.rise(&move); |
kubitz | 0:19fd8c1944fb | 181 | I1.fall(&move); |
kubitz | 0:19fd8c1944fb | 182 | I2.rise(&move); |
kubitz | 0:19fd8c1944fb | 183 | I2.fall(&move); |
kubitz | 0:19fd8c1944fb | 184 | I3.rise(&move); |
kubitz | 0:19fd8c1944fb | 185 | I3.fall(&move); |
kubitz | 0:19fd8c1944fb | 186 | timer_nonce.start(); |
kubitz | 0:19fd8c1944fb | 187 | previous_time = timer_nonce.read(); |
kubitz | 0:19fd8c1944fb | 188 | |
kubitz | 0:19fd8c1944fb | 189 | while(1==1){ |
kubitz | 0:19fd8c1944fb | 190 | uint8_t hash[32]; |
kubitz | 0:19fd8c1944fb | 191 | |
kubitz | 0:19fd8c1944fb | 192 | *nonce ++; |
kubitz | 0:19fd8c1944fb | 193 | SHA256::computeHash(hash, (uint8_t*)sequence, 64); |
kubitz | 2:60ec222456b6 | 194 | mail_box.put(mail); |
kubitz | 0:19fd8c1944fb | 195 | |
kubitz | 0:19fd8c1944fb | 196 | if ((hash[0]==0)&&(hash[1]==0)){ |
kubitz | 0:19fd8c1944fb | 197 | last_nonce_number = successful_nonce; |
kubitz | 0:19fd8c1944fb | 198 | successful_nonce++; |
kubitz | 2:60ec222456b6 | 199 | putMessage(hash); |
kubitz | 0:19fd8c1944fb | 200 | } |
kubitz | 0:19fd8c1944fb | 201 | |
kubitz | 0:19fd8c1944fb | 202 | if ((timer_nonce.read()-previous_time) > 1000){ |
kubitz | 0:19fd8c1944fb | 203 | printf("Computation Rate: %d computation /sec", (*nonce-last_nonce_number)); |
kubitz | 0:19fd8c1944fb | 204 | last_nonce_number = *nonce; |
kubitz | 0:19fd8c1944fb | 205 | } |
kubitz | 0:19fd8c1944fb | 206 | |
kubitz | 0:19fd8c1944fb | 207 | } |
kubitz | 0:19fd8c1944fb | 208 | |
kubitz | 0:19fd8c1944fb | 209 | return 0; |
kubitz | 0:19fd8c1944fb | 210 | |
kubitz | 0:19fd8c1944fb | 211 | } |
kubitz | 0:19fd8c1944fb | 212 |