![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
First Commit
Dependencies: mbed Crypto_light mbed-rtos
Spin it 2 win it
main.cpp@3:2e32d7974962, 2018-03-12 (annotated)
- Committer:
- andrebharath
- Date:
- Mon Mar 12 11:58:08 2018 +0000
- Revision:
- 3:2e32d7974962
- Parent:
- 2:862ee3609eee
- Child:
- 4:e1141c1d8b19
- Child:
- 9:ecef1e8cbe3d
Implemented interrupts to initiate the motor sequencing task, bitcoin mining in the main loop, outgoing communication with the host.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TrebleStick | 0:88c3d6c8a4eb | 1 | #include "mbed.h" |
andrebharath | 3:2e32d7974962 | 2 | #include "Crypto_light/hash/SHA256.h" |
andrebharath | 3:2e32d7974962 | 3 | #include "mbed-rtos/rtos/rtos.h" |
TrebleStick | 0:88c3d6c8a4eb | 4 | |
TrebleStick | 0:88c3d6c8a4eb | 5 | //Photointerrupter input pins |
TrebleStick | 0:88c3d6c8a4eb | 6 | #define I1pin D2 |
TrebleStick | 0:88c3d6c8a4eb | 7 | #define I2pin D11 |
TrebleStick | 0:88c3d6c8a4eb | 8 | #define I3pin D12 |
andrebharath | 3:2e32d7974962 | 9 | |
TrebleStick | 0:88c3d6c8a4eb | 10 | //Incremental encoder input pins |
TrebleStick | 0:88c3d6c8a4eb | 11 | #define CHA D7 |
TrebleStick | 0:88c3d6c8a4eb | 12 | #define CHB D8 |
andrebharath | 3:2e32d7974962 | 13 | |
TrebleStick | 0:88c3d6c8a4eb | 14 | //Motor Drive output pins //Mask in output byte |
TrebleStick | 0:88c3d6c8a4eb | 15 | #define L1Lpin D4 //0x01 |
TrebleStick | 0:88c3d6c8a4eb | 16 | #define L1Hpin D5 //0x02 |
TrebleStick | 0:88c3d6c8a4eb | 17 | #define L2Lpin D3 //0x04 |
TrebleStick | 0:88c3d6c8a4eb | 18 | #define L2Hpin D6 //0x08 |
TrebleStick | 0:88c3d6c8a4eb | 19 | #define L3Lpin D9 //0x10 |
TrebleStick | 0:88c3d6c8a4eb | 20 | #define L3Hpin D10 //0x20 |
TrebleStick | 0:88c3d6c8a4eb | 21 | |
andrebharath | 3:2e32d7974962 | 22 | //Enum for putMessage message types |
andrebharath | 3:2e32d7974962 | 23 | #define MSG_HASHCOUNT 0 |
andrebharath | 3:2e32d7974962 | 24 | #define MSG_NONCE_OK 1 |
andrebharath | 3:2e32d7974962 | 25 | |
TrebleStick | 0:88c3d6c8a4eb | 26 | //Mapping from sequential drive states to motor phase outputs |
TrebleStick | 0:88c3d6c8a4eb | 27 | /* |
TrebleStick | 0:88c3d6c8a4eb | 28 | State L1 L2 L3 |
TrebleStick | 0:88c3d6c8a4eb | 29 | 0 H - L |
TrebleStick | 0:88c3d6c8a4eb | 30 | 1 - H L |
TrebleStick | 0:88c3d6c8a4eb | 31 | 2 L H - |
TrebleStick | 0:88c3d6c8a4eb | 32 | 3 L - H |
TrebleStick | 0:88c3d6c8a4eb | 33 | 4 - L H |
TrebleStick | 0:88c3d6c8a4eb | 34 | 5 H L - |
TrebleStick | 0:88c3d6c8a4eb | 35 | 6 - - - |
TrebleStick | 0:88c3d6c8a4eb | 36 | 7 - - - |
TrebleStick | 0:88c3d6c8a4eb | 37 | */ |
TrebleStick | 0:88c3d6c8a4eb | 38 | //Drive state to output table |
TrebleStick | 0:88c3d6c8a4eb | 39 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
andrebharath | 3:2e32d7974962 | 40 | |
TrebleStick | 0:88c3d6c8a4eb | 41 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
TrebleStick | 0:88c3d6c8a4eb | 42 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
TrebleStick | 0:88c3d6c8a4eb | 43 | //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed |
andrebharath | 3:2e32d7974962 | 44 | |
TrebleStick | 0:88c3d6c8a4eb | 45 | //Phase lead to make motor spin |
TrebleStick | 0:88c3d6c8a4eb | 46 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
andrebharath | 3:2e32d7974962 | 47 | |
andrebharath | 3:2e32d7974962 | 48 | //Instantiate the serial port |
andrebharath | 3:2e32d7974962 | 49 | Serial pc(SERIAL_TX, SERIAL_RX); |
TrebleStick | 0:88c3d6c8a4eb | 50 | |
andrebharath | 3:2e32d7974962 | 51 | |
andrebharath | 3:2e32d7974962 | 52 | typedef struct { |
andrebharath | 3:2e32d7974962 | 53 | uint8_t code; |
andrebharath | 3:2e32d7974962 | 54 | uint32_t data; |
andrebharath | 3:2e32d7974962 | 55 | } message_t ; |
andrebharath | 3:2e32d7974962 | 56 | |
andrebharath | 3:2e32d7974962 | 57 | Mail<message_t,16> outMessages; |
andrebharath | 3:2e32d7974962 | 58 | |
andrebharath | 3:2e32d7974962 | 59 | void putMessage(uint8_t code, uint32_t data) |
andrebharath | 3:2e32d7974962 | 60 | { |
andrebharath | 3:2e32d7974962 | 61 | message_t *pMessage = outMessages.alloc(); |
andrebharath | 3:2e32d7974962 | 62 | pMessage->code = code; |
andrebharath | 3:2e32d7974962 | 63 | pMessage->data = data; |
andrebharath | 3:2e32d7974962 | 64 | outMessages.put(pMessage); |
andrebharath | 3:2e32d7974962 | 65 | } |
andrebharath | 3:2e32d7974962 | 66 | |
andrebharath | 3:2e32d7974962 | 67 | Thread commOutT; |
andrebharath | 3:2e32d7974962 | 68 | |
andrebharath | 3:2e32d7974962 | 69 | void commOutFn() |
andrebharath | 3:2e32d7974962 | 70 | { |
andrebharath | 3:2e32d7974962 | 71 | while(1) { |
andrebharath | 3:2e32d7974962 | 72 | osEvent newEvent = outMessages.get(); |
andrebharath | 3:2e32d7974962 | 73 | message_t *pMessage = (message_t*)newEvent.value.p; |
andrebharath | 3:2e32d7974962 | 74 | pc.printf("Message %d with data 0x%016x\r\n", |
andrebharath | 3:2e32d7974962 | 75 | pMessage->code,pMessage->data); |
andrebharath | 3:2e32d7974962 | 76 | outMessages.free(pMessage); |
andrebharath | 3:2e32d7974962 | 77 | } |
andrebharath | 3:2e32d7974962 | 78 | } |
andrebharath | 3:2e32d7974962 | 79 | |
andrebharath | 3:2e32d7974962 | 80 | |
TrebleStick | 0:88c3d6c8a4eb | 81 | //Status LED |
TrebleStick | 0:88c3d6c8a4eb | 82 | DigitalOut led1(LED1); |
andrebharath | 3:2e32d7974962 | 83 | |
TrebleStick | 0:88c3d6c8a4eb | 84 | //Photointerrupter inputs |
andrebharath | 3:2e32d7974962 | 85 | InterruptIn I1(I1pin); |
andrebharath | 3:2e32d7974962 | 86 | InterruptIn I2(I2pin); |
andrebharath | 3:2e32d7974962 | 87 | InterruptIn I3(I3pin); |
andrebharath | 3:2e32d7974962 | 88 | |
TrebleStick | 0:88c3d6c8a4eb | 89 | //Motor Drive outputs |
TrebleStick | 0:88c3d6c8a4eb | 90 | DigitalOut L1L(L1Lpin); |
TrebleStick | 0:88c3d6c8a4eb | 91 | DigitalOut L1H(L1Hpin); |
TrebleStick | 0:88c3d6c8a4eb | 92 | DigitalOut L2L(L2Lpin); |
TrebleStick | 0:88c3d6c8a4eb | 93 | DigitalOut L2H(L2Hpin); |
TrebleStick | 0:88c3d6c8a4eb | 94 | DigitalOut L3L(L3Lpin); |
TrebleStick | 0:88c3d6c8a4eb | 95 | DigitalOut L3H(L3Hpin); |
andrebharath | 3:2e32d7974962 | 96 | |
andrebharath | 3:2e32d7974962 | 97 | volatile uint16_t hashcount = 0; |
TrebleStick | 0:88c3d6c8a4eb | 98 | |
andrebharath | 3:2e32d7974962 | 99 | void do_hashcount() |
andrebharath | 3:2e32d7974962 | 100 | { |
andrebharath | 3:2e32d7974962 | 101 | putMessage(MSG_HASHCOUNT, hashcount); |
andrebharath | 3:2e32d7974962 | 102 | hashcount = 0; |
andrebharath | 3:2e32d7974962 | 103 | } |
andrebharath | 3:2e32d7974962 | 104 | |
TrebleStick | 0:88c3d6c8a4eb | 105 | //Set a given drive state |
andrebharath | 3:2e32d7974962 | 106 | void motorOut(int8_t driveState) |
andrebharath | 3:2e32d7974962 | 107 | { |
TrebleStick | 0:88c3d6c8a4eb | 108 | |
TrebleStick | 0:88c3d6c8a4eb | 109 | //Lookup the output byte from the drive state. |
TrebleStick | 0:88c3d6c8a4eb | 110 | int8_t driveOut = driveTable[driveState & 0x07]; |
TrebleStick | 0:88c3d6c8a4eb | 111 | |
TrebleStick | 0:88c3d6c8a4eb | 112 | //Turn off first |
TrebleStick | 0:88c3d6c8a4eb | 113 | if (~driveOut & 0x01) L1L = 0; |
TrebleStick | 0:88c3d6c8a4eb | 114 | if (~driveOut & 0x02) L1H = 1; |
TrebleStick | 0:88c3d6c8a4eb | 115 | if (~driveOut & 0x04) L2L = 0; |
TrebleStick | 0:88c3d6c8a4eb | 116 | if (~driveOut & 0x08) L2H = 1; |
TrebleStick | 0:88c3d6c8a4eb | 117 | if (~driveOut & 0x10) L3L = 0; |
TrebleStick | 0:88c3d6c8a4eb | 118 | if (~driveOut & 0x20) L3H = 1; |
TrebleStick | 0:88c3d6c8a4eb | 119 | |
TrebleStick | 0:88c3d6c8a4eb | 120 | //Then turn on |
TrebleStick | 0:88c3d6c8a4eb | 121 | if (driveOut & 0x01) L1L = 1; |
TrebleStick | 0:88c3d6c8a4eb | 122 | if (driveOut & 0x02) L1H = 0; |
TrebleStick | 0:88c3d6c8a4eb | 123 | if (driveOut & 0x04) L2L = 1; |
TrebleStick | 0:88c3d6c8a4eb | 124 | if (driveOut & 0x08) L2H = 0; |
TrebleStick | 0:88c3d6c8a4eb | 125 | if (driveOut & 0x10) L3L = 1; |
TrebleStick | 0:88c3d6c8a4eb | 126 | if (driveOut & 0x20) L3H = 0; |
andrebharath | 3:2e32d7974962 | 127 | } |
TrebleStick | 0:88c3d6c8a4eb | 128 | |
andrebharath | 3:2e32d7974962 | 129 | //Convert photointerrupter inputs to a rotor state |
andrebharath | 3:2e32d7974962 | 130 | inline int8_t readRotorState() |
andrebharath | 3:2e32d7974962 | 131 | { |
TrebleStick | 0:88c3d6c8a4eb | 132 | return stateMap[I1 + 2*I2 + 4*I3]; |
andrebharath | 3:2e32d7974962 | 133 | } |
andrebharath | 3:2e32d7974962 | 134 | |
TrebleStick | 0:88c3d6c8a4eb | 135 | //Basic synchronisation routine |
andrebharath | 3:2e32d7974962 | 136 | int8_t motorHome() |
andrebharath | 3:2e32d7974962 | 137 | { |
TrebleStick | 0:88c3d6c8a4eb | 138 | //Put the motor in drive state 0 and wait for it to stabilise |
TrebleStick | 0:88c3d6c8a4eb | 139 | motorOut(0); |
andrebharath | 3:2e32d7974962 | 140 | wait(2.0); |
TrebleStick | 0:88c3d6c8a4eb | 141 | |
TrebleStick | 0:88c3d6c8a4eb | 142 | //Get the rotor state |
TrebleStick | 0:88c3d6c8a4eb | 143 | return readRotorState(); |
TrebleStick | 0:88c3d6c8a4eb | 144 | } |
andrebharath | 3:2e32d7974962 | 145 | |
andrebharath | 3:2e32d7974962 | 146 | void photointerrupter_isr() |
andrebharath | 3:2e32d7974962 | 147 | { |
andrebharath | 3:2e32d7974962 | 148 | int8_t orState = motorHome(); |
andrebharath | 3:2e32d7974962 | 149 | int8_t intState = readRotorState(); |
andrebharath | 3:2e32d7974962 | 150 | motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive |
andrebharath | 3:2e32d7974962 | 151 | } |
TrebleStick | 0:88c3d6c8a4eb | 152 | |
TrebleStick | 0:88c3d6c8a4eb | 153 | //Main |
andrebharath | 3:2e32d7974962 | 154 | int main() |
andrebharath | 3:2e32d7974962 | 155 | { |
andrebharath | 3:2e32d7974962 | 156 | I1.rise(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 157 | I2.rise(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 158 | I3.rise(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 159 | |
andrebharath | 3:2e32d7974962 | 160 | I1.fall(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 161 | I2.fall(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 162 | I3.fall(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 163 | |
andrebharath | 3:2e32d7974962 | 164 | Ticker hashcounter; |
andrebharath | 3:2e32d7974962 | 165 | hashcounter.attach(&do_hashcount, 1.0); |
TrebleStick | 0:88c3d6c8a4eb | 166 | |
andrebharath | 3:2e32d7974962 | 167 | commOutT.start(&commOutFn); |
TrebleStick | 0:88c3d6c8a4eb | 168 | |
andrebharath | 3:2e32d7974962 | 169 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
andrebharath | 3:2e32d7974962 | 170 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
andrebharath | 3:2e32d7974962 | 171 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
andrebharath | 3:2e32d7974962 | 172 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
andrebharath | 3:2e32d7974962 | 173 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
andrebharath | 3:2e32d7974962 | 174 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
andrebharath | 3:2e32d7974962 | 175 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
andrebharath | 3:2e32d7974962 | 176 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
andrebharath | 3:2e32d7974962 | 177 | uint64_t* key = (uint64_t*)((int)sequence + 48); |
andrebharath | 3:2e32d7974962 | 178 | uint64_t* nonce = (uint64_t*)((int)sequence + 56); |
andrebharath | 3:2e32d7974962 | 179 | uint8_t hash[32]; |
TrebleStick | 0:88c3d6c8a4eb | 180 | |
TrebleStick | 0:88c3d6c8a4eb | 181 | //Poll the rotor state and set the motor outputs accordingly to spin the motor |
TrebleStick | 0:88c3d6c8a4eb | 182 | while (1) { |
andrebharath | 3:2e32d7974962 | 183 | SHA256::computeHash(hash, sequence, 64); |
TrebleStick | 0:88c3d6c8a4eb | 184 | |
andrebharath | 3:2e32d7974962 | 185 | if (hash[0] == 0 && hash[1] == 0) { |
andrebharath | 3:2e32d7974962 | 186 | putMessage(MSG_NONCE_OK, *nonce); |
andrebharath | 3:2e32d7974962 | 187 | } |
andrebharath | 3:2e32d7974962 | 188 | |
andrebharath | 3:2e32d7974962 | 189 | (*nonce)++; |
andrebharath | 3:2e32d7974962 | 190 | hashcount++; |
TrebleStick | 1:a530f6235850 | 191 | |
TrebleStick | 0:88c3d6c8a4eb | 192 | } |
TrebleStick | 0:88c3d6c8a4eb | 193 | } |
andrebharath | 3:2e32d7974962 | 194 | |
andrebharath | 3:2e32d7974962 | 195 |