
An embedded device
Dependencies: Crypto
Revision 13:c51d828531d5, committed 2019-03-06
- Comitter:
- peterith
- Date:
- Wed Mar 06 17:34:06 2019 +0000
- Parent:
- 12:899cd6bf9844
- Child:
- 14:0481b606d10e
- Commit message:
- complete lab 1
Changed in this revision
Crypto.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Crypto.lib Wed Mar 06 17:34:06 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/feb11/code/Crypto/#f04410cef037
--- a/main.cpp Thu Feb 28 11:52:05 2019 +0000 +++ b/main.cpp Wed Mar 06 17:34:06 2019 +0000 @@ -1,4 +1,5 @@ #include "mbed.h" +#include "Crypto.h" //Photointerrupter input pins #define I1pin D3 @@ -6,26 +7,23 @@ #define I3pin D5 //Incremental encoder input pins -#define CHApin D12 -#define CHBpin D11 +#define CHApin D12 +#define CHBpin D11 //Motor Drive output pins //Mask in output byte #define L1Lpin D1 //0x01 #define L1Hpin A3 //0x02 #define L2Lpin D0 //0x04 -#define L2Hpin A6 //0x08 -#define L3Lpin D10 //0x10 -#define L3Hpin D2 //0x20 +#define L2Hpin A6 //0x08 +#define L3Lpin D10 //0x10 +#define L3Hpin D2 //0x20 #define PWMpin D9 //Motor current sense -#define MCSPpin A1 -#define MCSNpin A0 +#define MCSPpin A1 +#define MCSNpin A0 -int8_t motorHome(); -inline int8_t readRotorState(); -void motorOut(int8_t driveState); //Mapping from sequential drive states to motor phase outputs /* State L1 L2 L3 @@ -56,8 +54,6 @@ InterruptIn I2(I2pin); InterruptIn I3(I3pin); - - //Motor Drive outputs DigitalOut L1L(L1Lpin); DigitalOut L1H(L1Hpin); @@ -66,20 +62,9 @@ DigitalOut L3L(L3Lpin); DigitalOut L3H(L3Hpin); -int8_t orState = 0; //Rotot offset at motor state 0 +int8_t orState = 0; int8_t intState = 0; int8_t intStateOld = 0; - -//interrupt function -void push(){ - //Poll the rotor state and set the motor outputs accordingly to spin the motor - intState = readRotorState(); - if (intState != intStateOld) { - intStateOld = intState; - motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive - //pc.printf("%d\n\r",intState); - } -} //Set a given drive state void motorOut(int8_t driveState){ @@ -102,36 +87,74 @@ if (driveOut & 0x08) L2H = 0; if (driveOut & 0x10) L3L = 1; if (driveOut & 0x20) L3H = 0; - } +} - //Convert photointerrupter inputs to a rotor state +//Convert photointerrupter inputs to a rotor state inline int8_t readRotorState(){ return stateMap[I1 + 2*I2 + 4*I3]; - } +} -//Basic synchronisation routine int8_t motorHome() { - //Put the motor in drive state 0 and wait for it to stabilise motorOut(0); wait(2.0); - //Get the rotor state return readRotorState(); } - -//Main -int main() { - //Initialise the serial port + +void push() { + intState = readRotorState(); + if (intState != intStateOld) { + intStateOld = intState; + motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive + } +} + +int main() { Serial pc(SERIAL_TX, SERIAL_RX); - pc.printf("Hello\n\r"); + pc.printf("Hello Pete\n\r"); - //Run the motor synchronisation orState = motorHome(); - pc.printf("Rotor origin: %x\n\r",orState); - //orState is subtracted from future rotor state inputs to align rotor and motor states + pc.printf("Rotor origin: %x\n\r", orState); I1.rise(&push); I2.rise(&push); I3.rise(&push); + + I1.fall(&push); + I2.fall(&push); + I3.fall(&push); + + while(1) { + SHA256 sha; + uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, + 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, + 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, + 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, + 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, + 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + uint64_t* key = (uint64_t*) ((int) sequence + 48); + uint64_t* nonce = (uint64_t*) ((int) sequence + 56); + uint8_t hash[32]; + + Timer t; + + t.start(); + unsigned curr = 1; + unsigned hashRate = 0; + for (uint64_t i = 0; i <= 0b1111111111111111111111111111111111111111111111111111111111111111; i++) { + hashRate++; + (*nonce)++; + sha.computeHash(&hash[0], &sequence[0], 64); + if (hash[0] == 0 && hash[1] == 0) { + pc.printf("Successful nonce: %016x\n\r", *nonce); + } + if ((unsigned) t.read() == curr) { + curr++; + pc.printf("Hash rate: %d\n\r", hashRate); + hashRate = 0; + } + } + } } -