![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
First Commit
Dependencies: mbed Crypto_light mbed-rtos
Spin it 2 win it
Diff: main.cpp
- Revision:
- 3:2e32d7974962
- Parent:
- 2:862ee3609eee
- Child:
- 4:e1141c1d8b19
- Child:
- 9:ecef1e8cbe3d
--- a/main.cpp Sun Mar 11 17:32:59 2018 +0000 +++ b/main.cpp Mon Mar 12 11:58:08 2018 +0000 @@ -1,14 +1,16 @@ #include "mbed.h" +#include "Crypto_light/hash/SHA256.h" +#include "mbed-rtos/rtos/rtos.h" //Photointerrupter input pins #define I1pin D2 #define I2pin D11 #define I3pin D12 - + //Incremental encoder input pins #define CHA D7 #define CHB D8 - + //Motor Drive output pins //Mask in output byte #define L1Lpin D4 //0x01 #define L1Hpin D5 //0x02 @@ -17,6 +19,10 @@ #define L3Lpin D9 //0x10 #define L3Hpin D10 //0x20 +//Enum for putMessage message types +#define MSG_HASHCOUNT 0 +#define MSG_NONCE_OK 1 + //Mapping from sequential drive states to motor phase outputs /* State L1 L2 L3 @@ -31,22 +37,55 @@ */ //Drive state to output table const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; - + //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed - + //Phase lead to make motor spin const int8_t lead = 2; //2 for forwards, -2 for backwards + +//Instantiate the serial port +Serial pc(SERIAL_TX, SERIAL_RX); + +typedef struct { + uint8_t code; + uint32_t data; +} message_t ; + +Mail<message_t,16> outMessages; + +void putMessage(uint8_t code, uint32_t data) +{ + message_t *pMessage = outMessages.alloc(); + pMessage->code = code; + pMessage->data = data; + outMessages.put(pMessage); +} + +Thread commOutT; + +void commOutFn() +{ + while(1) { + osEvent newEvent = outMessages.get(); + message_t *pMessage = (message_t*)newEvent.value.p; + pc.printf("Message %d with data 0x%016x\r\n", + pMessage->code,pMessage->data); + outMessages.free(pMessage); + } +} + + //Status LED DigitalOut led1(LED1); - + //Photointerrupter inputs -DigitalIn I1(I1pin); -DigitalIn I2(I2pin); -DigitalIn I3(I3pin); - +InterruptIn I1(I1pin); +InterruptIn I2(I2pin); +InterruptIn I3(I3pin); + //Motor Drive outputs DigitalOut L1L(L1Lpin); DigitalOut L1H(L1Hpin); @@ -54,9 +93,18 @@ DigitalOut L2H(L2Hpin); DigitalOut L3L(L3Lpin); DigitalOut L3H(L3Hpin); + +volatile uint16_t hashcount = 0; +void do_hashcount() +{ + putMessage(MSG_HASHCOUNT, hashcount); + hashcount = 0; +} + //Set a given drive state -void motorOut(int8_t driveState){ +void motorOut(int8_t driveState) +{ //Lookup the output byte from the drive state. int8_t driveOut = driveTable[driveState & 0x07]; @@ -76,50 +124,72 @@ if (driveOut & 0x08) L2H = 0; if (driveOut & 0x10) L3L = 1; if (driveOut & 0x20) L3H = 0; - } +} - //Convert photointerrupter inputs to a rotor state -inline int8_t readRotorState(){ +//Convert photointerrupter inputs to a rotor state +inline int8_t readRotorState() +{ return stateMap[I1 + 2*I2 + 4*I3]; - } - +} + //Basic synchronisation routine -int8_t motorHome() { +int8_t motorHome() +{ //Put the motor in drive state 0 and wait for it to stabilise motorOut(0); - wait(1.0); + wait(2.0); //Get the rotor state return readRotorState(); } + +void photointerrupter_isr() +{ + int8_t orState = motorHome(); + int8_t intState = readRotorState(); + motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive +} //Main -int main() { - int8_t orState = 0; //Rotot offset at motor state 0 - int8_t intState = 0; - float delay = 0.05; - //Initialise the serial port - Serial pc(SERIAL_TX, SERIAL_RX); - pc.printf("Hello\n\r"); +int main() +{ + I1.rise(&photointerrupter_isr); + I2.rise(&photointerrupter_isr); + I3.rise(&photointerrupter_isr); + + I1.fall(&photointerrupter_isr); + I2.fall(&photointerrupter_isr); + I3.fall(&photointerrupter_isr); + + Ticker hashcounter; + hashcounter.attach(&do_hashcount, 1.0); - //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 + commOutT.start(&commOutFn); + 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]; //Poll the rotor state and set the motor outputs accordingly to spin the motor while (1) { - - intState = (intState-orState+lead+6)%6; - motorOut(intState); //+6 to make sure the remainder is positive + SHA256::computeHash(hash, sequence, 64); - wait(delay); + if (hash[0] == 0 && hash[1] == 0) { + putMessage(MSG_NONCE_OK, *nonce); + } + + (*nonce)++; + hashcount++; - - pc.printf("State: %x intState: %x Speed : %f \n\r", readRotorState(), intState, 1/(delay*6)); - //orState is subtracted from future rotor state inputs to align rotor and motor states } } - + + \ No newline at end of file