
An embedded device
Dependencies: Crypto
Revision 14:0481b606d10e, committed 2019-03-07
- Comitter:
- peterith
- Date:
- Thu Mar 07 00:57:37 2019 +0000
- Parent:
- 13:c51d828531d5
- Child:
- 15:f43d1d4e4260
- Commit message:
- Most of lab 2 done, but please fix truncation issue, type in more than 8 hex digits and you will see
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Mar 06 17:34:06 2019 +0000 +++ b/main.cpp Thu Mar 07 00:57:37 2019 +0000 @@ -66,6 +66,56 @@ int8_t intState = 0; int8_t intStateOld = 0; +typedef struct { + uint64_t nonce; +} mail_t; + +Mail<mail_t, 16> mail_box; +Thread commandProcessorthread; +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: %d\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); + } + } +} + //Set a given drive state void motorOut(int8_t driveState){ @@ -97,7 +147,6 @@ int8_t motorHome() { motorOut(0); wait(2.0); - return readRotorState(); } @@ -110,9 +159,12 @@ } int main() { - Serial pc(SERIAL_TX, SERIAL_RX); + //Serial pc(SERIAL_TX, SERIAL_RX); + + commandProcessorthread.start(commandProcessor); + pc.printf("Hello Pete\n\r"); - + orState = motorHome(); pc.printf("Rotor origin: %x\n\r", orState); @@ -139,22 +191,27 @@ uint8_t hash[32]; Timer t; - t.start(); - unsigned curr = 1; - unsigned hashRate = 0; - for (uint64_t i = 0; i <= 0b1111111111111111111111111111111111111111111111111111111111111111; i++) { - hashRate++; + unsigned currentTime = 0; + unsigned currentCount = 0; + + for (unsigned i = 0; i <= UINT_MAX; i++) { (*nonce)++; - sha.computeHash(&hash[0], &sequence[0], 64); + 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() == curr) { - curr++; - pc.printf("Hash rate: %d\n\r", hashRate); - hashRate = 0; + if ((unsigned) t.read() == currentTime) { + //pc.printf("Hash rate: %d\n\r", i - currentCount); + pc.printf("Current key: %016x\n\r", *key); + currentTime++; + currentCount = i; } } + t.stop(); } }