This is probably never gonna get done

Dependencies:   Crypto

main.cpp

Committer:
tanyuzhuo
Date:
2019-03-12
Revision:
16:10d53b056b17
Parent:
15:f43d1d4e4260
Child:
17:ff5300ba5442

File content as of revision 16:10d53b056b17:

#include "mbed.h"
#include "Crypto.h"

//Photointerrupter input pins
#define I1pin D3
#define I2pin D6
#define I3pin D5

//Incremental encoder input pins
#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 PWMpin D9

//Motor current sense
#define MCSPpin A1
#define MCSNpin A0

//Mapping from sequential drive states to motor phase outputs
/*
State   L1  L2  L3
0       H   -   L
1       -   H   L
2       L   H   -
3       L   -   H
4       -   L   H
5       H   L   -
6       -   -   -
7       -   -   -
*/
//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

//Status LED
DigitalOut led1(LED1);

//Photointerrupter inputs
InterruptIn I1(I1pin);
InterruptIn I2(I2pin);
InterruptIn I3(I3pin);

//Motor Drive outputs
DigitalOut L1L(L1Lpin);
DigitalOut L1H(L1Hpin);
DigitalOut L2L(L2Lpin);
DigitalOut L2H(L2Hpin);
DigitalOut L3L(L3Lpin);
DigitalOut L3H(L3Hpin);

int8_t orState = 0;
int8_t intState = 0;
int8_t intStateOld = 0;

typedef struct {
  uint64_t nonce;
} mail_t;
 
Mail<mail_t, 16> mail_box;
Thread commandProcessorthread;
Thread bitcointhread;
RawSerial pc(SERIAL_TX, SERIAL_RX);
Queue<void, 8> inCharQ;
Mutex newKey_mutex;
uint64_t newKey = 0;

void putMessage(uint64_t *nonce){
    mail_t *mail = mail_box.alloc();
    mail->nonce = *nonce;
    mail_box.put(mail);
}

void serialISR() {
    uint8_t newChar = pc.getc();
    inCharQ.put((void*) newChar);
}

void commandProcessor() {
    pc.attach(&serialISR);
    char command[18];
    //char k;
    uint64_t receivedKey;
    uint8_t index = 0;
    while(1) {
        osEvent newEvent = inCharQ.get();
        uint8_t newChar = (uint8_t) newEvent.value.p;
        command[index] = newChar;
        index++;
        if (newChar == '\r') {
            command[17] = '\0';
            receivedKey = strtoull(command, NULL, 16);
            //receivedKey = 2147483648;
            //sscanf(command, "%d", &receivedKey);
            pc.printf("Received key: %016llx\n\r", receivedKey);
            memset(command, 0, sizeof(command));
            newKey_mutex.lock();
            newKey = receivedKey;
            newKey_mutex.unlock();
            index = 0;
        } else {
            pc.printf("Current command: %s\n\r", command);
        }
    }
}
void bitcoin(){
     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 currentTime = 0;
        unsigned currentCount = 0;
        
        for (unsigned i = 0; i <= UINT_MAX;  i++) {
            (*nonce)++;
            newKey_mutex.lock();
            *key = newKey;
            newKey_mutex.unlock();
            sha.computeHash(hash, sequence, 64);
            if (hash[0] == 0 && hash[1] == 0) {
                //putMessage(nonce);
                pc.printf("Successful nonce: %016x\n\r", *nonce);
            }
            if ((unsigned) t.read() == currentTime) {
                 //pc.printf("Hash rate: %d\n\r", i - currentCount);
                 pc.printf("Current key: %016llx\n\r", *key);
                 currentTime++;
                 currentCount = i;
            }
        }
        t.stop();
    }
    }
//Set a given drive state
void motorOut(int8_t driveState){
    
    //Lookup the output byte from the drive state.
    int8_t driveOut = driveTable[driveState & 0x07];
      
    //Turn off first
    if (~driveOut & 0x01) L1L = 0;
    if (~driveOut & 0x02) L1H = 1;
    if (~driveOut & 0x04) L2L = 0;
    if (~driveOut & 0x08) L2H = 1;
    if (~driveOut & 0x10) L3L = 0;
    if (~driveOut & 0x20) L3H = 1;
    
    //Then turn on
    if (driveOut & 0x01) L1L = 1;
    if (driveOut & 0x02) L1H = 0;
    if (driveOut & 0x04) L2L = 1;
    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(){
    return stateMap[I1 + 2*I2 + 4*I3];
}

int8_t motorHome() {
    motorOut(0);
    wait(2.0);
    return readRotorState();
}

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);
    
    bitcointhread.set_priority(osPriorityNormal);
    commandProcessorthread.set_priority(osPriorityHigh);
    
    commandProcessorthread.start(commandProcessor);
    bitcointhread.start(bitcoin);

    pc.printf("Hello Pete\n\r");
        
    orState = motorHome();
    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);
    
   
}