Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
revoxhere
GitHub Repository: revoxhere/duino-coin
Path: blob/master/Unofficial miners/STM32/src/main.cpp
922 views
1
// Duino-coin miner for STM32 and AVR chip family
2
// https://duinocoin.com/
3
4
// by: pankleks 2022
5
// v: 1.2
6
// https://github.com/pankleks
7
// MIT License
8
9
#include <Arduino.h>
10
#include <sha1.h>
11
#include <ArduinoUniqueID.h>
12
13
// features
14
#define RESET_NO_JOBS // comment to not reset board if no jobs
15
16
#ifdef ARDUINO_ARCH_STM32
17
#define LOG // comment to disable debug logs to Serial1
18
#define LED_BUILTIN PC13
19
#endif
20
21
#define NO_JOBS_RESET_S 60 // reset board if no jobs for longer than 60s
22
23
String clientId;
24
25
void log(String msg)
26
{
27
#ifdef LOG
28
Serial1.println(msg);
29
#endif
30
}
31
32
String getClientId()
33
{
34
String temp = "DUCOID";
35
36
for (int i = 0; i < UniqueIDsize; i++)
37
temp += String(UniqueID[i], HEX);
38
39
return temp;
40
}
41
42
void ledOn()
43
{
44
digitalWrite(LED_BUILTIN, false);
45
}
46
47
void ledOff()
48
{
49
digitalWrite(LED_BUILTIN, true);
50
}
51
52
void setup()
53
{
54
clientId = getClientId();
55
pinMode(LED_BUILTIN, OUTPUT);
56
ledOn();
57
58
#ifdef LOG
59
Serial1.begin(115200);
60
#endif
61
log(clientId);
62
63
Serial.begin(115200);
64
Serial.setTimeout(3000);
65
66
// wait for connection
67
while (!Serial)
68
;
69
70
Serial.flush();
71
72
log(F("setup done"));
73
ledOff();
74
}
75
76
void clearSerial()
77
{
78
while (Serial.available())
79
Serial.read();
80
}
81
82
int ducos1a(String lastHash, String expHash, int difficulty)
83
{
84
const char *c = expHash.c_str();
85
uint8_t length = expHash.length() / 2;
86
uint8_t job[104];
87
88
for (size_t i = 0, j = 0; j < length; i += 2, j++)
89
job[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25;
90
91
SHA1_CTX baseCtx;
92
SHA1Init(&baseCtx);
93
SHA1Update(&baseCtx, (uint8_t *)lastHash.c_str(), lastHash.length());
94
95
uint8_t buf[20];
96
97
for (int result = 0; result < difficulty * 100 + 1; result++)
98
{
99
SHA1_CTX ctx = SHA1Copy(baseCtx);
100
itoa(result, (char *)buf, 10);
101
102
SHA1Update(&ctx, buf, strlen((char *)buf));
103
SHA1Final(buf, &ctx);
104
105
if (memcmp(buf, job, 20) == 0) // if found hash = expected hash -> result
106
return result;
107
}
108
109
return -1; // not found
110
}
111
112
#ifdef ARDUINO_ARCH_AVR
113
void (*resetFn)(void) = 0;
114
#endif
115
116
void reset()
117
{
118
#ifdef ARDUINO_ARCH_AVR
119
resetFn();
120
#endif
121
#ifdef ARDUINO_ARCH_STM32
122
NVIC_SystemReset();
123
#endif
124
}
125
126
String lastHash;
127
String expHash;
128
uint32_t difficulty;
129
long lastOkJobT = millis();
130
long t = 0;
131
132
void loop()
133
{
134
if (Serial.available() > 0)
135
{
136
log(F("got data"));
137
138
lastHash = Serial.readStringUntil(',');
139
log("LH: " + lastHash);
140
141
expHash = Serial.readStringUntil(',');
142
log("EH: " + expHash);
143
144
difficulty = Serial.readStringUntil(',').toInt() * 100;
145
log("DF: " + String(difficulty));
146
147
clearSerial();
148
ledOn();
149
150
long startT = micros();
151
152
expHash.toUpperCase();
153
int result = ducos1a(lastHash, expHash, difficulty);
154
155
long elapsedT = micros() - startT;
156
157
ledOff();
158
clearSerial();
159
160
Serial.print(String(result) + "," + String(elapsedT) + "," + clientId + "\n");
161
162
log("R: " + String(result) + ", " + String((float)elapsedT / 1000000) + " s");
163
164
lastOkJobT = millis();
165
}
166
167
if (millis() - t > 1000)
168
{
169
t = millis();
170
171
long n = millis() - lastOkJobT;
172
log("last ok job " + String(n) + " ms ago");
173
174
#ifdef RESET_NO_JOBS
175
if (n > NO_JOBS_RESET_S * 1000L)
176
{
177
log(F("no jobs, reset"));
178
}
179
#endif
180
}
181
}
182
183