Path: blob/master/Unofficial miners/STM32/src/main.cpp
922 views
// Duino-coin miner for STM32 and AVR chip family1// https://duinocoin.com/23// by: pankleks 20224// v: 1.25// https://github.com/pankleks6// MIT License78#include <Arduino.h>9#include <sha1.h>10#include <ArduinoUniqueID.h>1112// features13#define RESET_NO_JOBS // comment to not reset board if no jobs1415#ifdef ARDUINO_ARCH_STM3216#define LOG // comment to disable debug logs to Serial117#define LED_BUILTIN PC1318#endif1920#define NO_JOBS_RESET_S 60 // reset board if no jobs for longer than 60s2122String clientId;2324void log(String msg)25{26#ifdef LOG27Serial1.println(msg);28#endif29}3031String getClientId()32{33String temp = "DUCOID";3435for (int i = 0; i < UniqueIDsize; i++)36temp += String(UniqueID[i], HEX);3738return temp;39}4041void ledOn()42{43digitalWrite(LED_BUILTIN, false);44}4546void ledOff()47{48digitalWrite(LED_BUILTIN, true);49}5051void setup()52{53clientId = getClientId();54pinMode(LED_BUILTIN, OUTPUT);55ledOn();5657#ifdef LOG58Serial1.begin(115200);59#endif60log(clientId);6162Serial.begin(115200);63Serial.setTimeout(3000);6465// wait for connection66while (!Serial)67;6869Serial.flush();7071log(F("setup done"));72ledOff();73}7475void clearSerial()76{77while (Serial.available())78Serial.read();79}8081int ducos1a(String lastHash, String expHash, int difficulty)82{83const char *c = expHash.c_str();84uint8_t length = expHash.length() / 2;85uint8_t job[104];8687for (size_t i = 0, j = 0; j < length; i += 2, j++)88job[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25;8990SHA1_CTX baseCtx;91SHA1Init(&baseCtx);92SHA1Update(&baseCtx, (uint8_t *)lastHash.c_str(), lastHash.length());9394uint8_t buf[20];9596for (int result = 0; result < difficulty * 100 + 1; result++)97{98SHA1_CTX ctx = SHA1Copy(baseCtx);99itoa(result, (char *)buf, 10);100101SHA1Update(&ctx, buf, strlen((char *)buf));102SHA1Final(buf, &ctx);103104if (memcmp(buf, job, 20) == 0) // if found hash = expected hash -> result105return result;106}107108return -1; // not found109}110111#ifdef ARDUINO_ARCH_AVR112void (*resetFn)(void) = 0;113#endif114115void reset()116{117#ifdef ARDUINO_ARCH_AVR118resetFn();119#endif120#ifdef ARDUINO_ARCH_STM32121NVIC_SystemReset();122#endif123}124125String lastHash;126String expHash;127uint32_t difficulty;128long lastOkJobT = millis();129long t = 0;130131void loop()132{133if (Serial.available() > 0)134{135log(F("got data"));136137lastHash = Serial.readStringUntil(',');138log("LH: " + lastHash);139140expHash = Serial.readStringUntil(',');141log("EH: " + expHash);142143difficulty = Serial.readStringUntil(',').toInt() * 100;144log("DF: " + String(difficulty));145146clearSerial();147ledOn();148149long startT = micros();150151expHash.toUpperCase();152int result = ducos1a(lastHash, expHash, difficulty);153154long elapsedT = micros() - startT;155156ledOff();157clearSerial();158159Serial.print(String(result) + "," + String(elapsedT) + "," + clientId + "\n");160161log("R: " + String(result) + ", " + String((float)elapsedT / 1000000) + " s");162163lastOkJobT = millis();164}165166if (millis() - t > 1000)167{168t = millis();169170long n = millis() - lastOkJobT;171log("last ok job " + String(n) + " ms ago");172173#ifdef RESET_NO_JOBS174if (n > NO_JOBS_RESET_S * 1000L)175{176log(F("no jobs, reset"));177}178#endif179}180}181182183