Skip to content

Commit 9b416c7

Browse files
KooLruvirtual-maker
authored andcommitted
STM32 Cores start implementation
1 parent 093afa0 commit 9b416c7

File tree

5 files changed

+461
-1
lines changed

5 files changed

+461
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ doxygen.log
1313
TAGS
1414
tags
1515
.DS_Store
16+
.vs/*

MySensors.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
#include "drivers/extEEPROM/extEEPROM.cpp"
6868
#include "hal/architecture/SAMD/MyHwSAMD.cpp"
6969
#include "hal/crypto/generic/MyCryptoGeneric.cpp"
70+
#elif defined(ARDUINO_ARCH_STM32)
71+
#include "hal/architecture/STM32/MyHwSTM32.cpp"
72+
#include "hal/crypto/generic/MyCryptoGeneric.cpp"
7073
#elif defined(ARDUINO_ARCH_STM32F1)
7174
#include "hal/architecture/STM32F1/MyHwSTM32F1.cpp"
7275
#include "hal/crypto/generic/MyCryptoGeneric.cpp"
@@ -326,7 +329,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
326329
#define MY_RAM_ROUTING_TABLE_ENABLED
327330
#elif defined(MY_RAM_ROUTING_TABLE_FEATURE) && defined(MY_REPEATER_FEATURE)
328331
// activate feature based on architecture
329-
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_ARCH_STM32F1) || defined(TEENSYDUINO) || defined(__linux__)
332+
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32) || defined(TEENSYDUINO) || defined(__linux__)
330333
#define MY_RAM_ROUTING_TABLE_ENABLED
331334
#elif defined(ARDUINO_ARCH_AVR)
332335
#if defined(__avr_atmega1280__) || defined(__avr_atmega1284__) || defined(__avr_atmega2560__)
@@ -459,6 +462,8 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
459462
#include "hal/architecture/Linux/MyMainLinuxGeneric.cpp"
460463
#elif defined(ARDUINO_ARCH_STM32F1)
461464
#include "hal/architecture/STM32F1/MyMainSTM32F1.cpp"
465+
#elif defined(ARDUINO_ARCH_STM32)
466+
#include "hal/architecture/STM32/MyMainSTM32.cpp"
462467
#elif defined(__arm__) && defined(TEENSYDUINO)
463468
#include "hal/architecture/Teensy3/MyMainTeensy3.cpp"
464469
#endif

hal/architecture/STM32/MyHwSTM32.cpp

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2020 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*/
19+
20+
#include "MyHwSTM32.h"
21+
22+
/*
23+
* Pinout STM32F103C8 dev board:
24+
* http://wiki.stm32duino.com/images/a/ae/Bluepillpinout.gif
25+
*
26+
* Wiring
27+
* --------------------------------------------------
28+
RFM69 CLK MISO MOSI CSN CE IRQ
29+
SPI1 PA5 PA6 PA7 PA4 NA PA3 (default)
30+
31+
RF24 CLK MISO MOSI CSN CE IRQ
32+
SPI1 PA5 PA6 PA7 PA4 PB0 NA
33+
34+
*/
35+
36+
bool hwInit(void)
37+
{
38+
#if !defined(MY_DISABLED_SERIAL)
39+
MY_SERIALDEVICE.begin(MY_BAUD_RATE);
40+
#if defined(MY_GATEWAY_SERIAL)
41+
while (!MY_SERIALDEVICE) {}
42+
#endif
43+
#endif
44+
//todo EEPROM initialisation????
45+
Serial1.print("EEPROM.length ");
46+
Serial1.println(EEPROM.length());
47+
48+
//if (EEPROM.init() == EEPROM_OK) {
49+
// uint16 cnt;
50+
// EEPROM.count(&cnt);
51+
// if(cnt>=EEPROM.maxcount()) {
52+
// // tmp, WIP: format eeprom if full
53+
// EEPROM.format();
54+
// }
55+
return true;
56+
//}
57+
//return false;
58+
}
59+
60+
void hwReadConfigBlock(void *buf, void *addr, size_t length)
61+
{
62+
uint8_t *dst = static_cast<uint8_t *>(buf);
63+
int offs = reinterpret_cast<int>(addr);
64+
while (length-- > 0) {
65+
*dst++ = EEPROM.read(offs++);
66+
}
67+
}
68+
69+
void hwWriteConfigBlock(void *buf, void *addr, size_t length)
70+
{
71+
uint8_t *src = static_cast<uint8_t *>(buf);
72+
int offs = reinterpret_cast<int>(addr);
73+
while (length-- > 0) {
74+
EEPROM.write(offs++, *src++);
75+
}
76+
// EEPROM.commit();
77+
}
78+
79+
uint8_t hwReadConfig(const int addr)
80+
{
81+
uint8_t value;
82+
hwReadConfigBlock(&value, reinterpret_cast<void *>(addr), 1);
83+
return value;
84+
}
85+
86+
void hwWriteConfig(const int addr, uint8_t value)
87+
{
88+
if (hwReadConfig(addr) != value) {
89+
hwWriteConfigBlock(&value, reinterpret_cast<void *>(addr), 1);
90+
}
91+
}
92+
93+
int8_t hwSleep(uint32_t ms)
94+
{
95+
// TODO: Not supported!
96+
(void)ms;
97+
return MY_SLEEP_NOT_POSSIBLE;
98+
}
99+
100+
int8_t hwSleep(const uint8_t interrupt, const uint8_t mode, uint32_t ms)
101+
{
102+
// TODO: Not supported!
103+
(void)interrupt;
104+
(void)mode;
105+
(void)ms;
106+
return MY_SLEEP_NOT_POSSIBLE;
107+
}
108+
109+
int8_t hwSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2,
110+
const uint8_t mode2,
111+
uint32_t ms)
112+
{
113+
// TODO: Not supported!
114+
(void)interrupt1;
115+
(void)mode1;
116+
(void)interrupt2;
117+
(void)mode2;
118+
(void)ms;
119+
return MY_SLEEP_NOT_POSSIBLE;
120+
}
121+
122+
123+
//void hwRandomNumberInit(void)
124+
//{
125+
// // This function initializes the random number generator with a seed
126+
// // of 32 bits. This method is good enough to earn FIPS 140-2 conform
127+
// // random data. This should reach to generate 32 Bit for randomSeed().
128+
// uint32_t seed = 0;
129+
// uint32_t timeout = millis() + 20;
130+
//
131+
// // Trigger floating effect of an unconnected pin
132+
// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP);
133+
// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT);
134+
// delay(10);
135+
//
136+
// // Generate 32 bits of datas
137+
// for (uint8_t i=0; i<32; i++) {
138+
// const int pinValue = analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN);
139+
// // Wait until the analog value has changed
140+
// while ((pinValue == analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)) && (timeout>=millis())) {
141+
// seed ^= (millis() << i);
142+
// // Check of data generation is slow
143+
// if (timeout<=millis()) {
144+
// // Trigger pin again
145+
// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP);
146+
// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT);
147+
// // Pause a short while
148+
// delay(seed % 10);
149+
// timeout = millis() + 20;
150+
// }
151+
// }
152+
// }
153+
// randomSeed(seed);
154+
//
155+
//// // use internal temperature sensor as noise source
156+
//// adc_reg_map *regs = ADC1->regs;
157+
//// regs->CR2 |= ADC_CR2_TSVREFE;
158+
//// regs->SMPR1 |= ADC_SMPR1_SMP16;
159+
////
160+
//// uint32_t seed = 0;
161+
//// uint16_t currentValue = 0;
162+
//// uint16_t newValue = 0;
163+
////
164+
//// for (uint8_t i = 0; i < 32; i++) {
165+
//// const uint32_t timeout = hwMillis() + 20;
166+
//// while (timeout >= hwMillis()) {
167+
//// newValue = adc_read(ADC1, 16);
168+
//// if (newValue != currentValue) {
169+
//// currentValue = newValue;
170+
//// break;
171+
//// }
172+
//// }
173+
//// seed ^= ( (newValue + hwMillis()) & 7) << i;
174+
//// }
175+
//// randomSeed(seed);
176+
//// regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor
177+
//}
178+
179+
bool hwUniqueID(unique_id_t *uniqueID)
180+
{
181+
(void)memcpy((uint8_t *)uniqueID, (uint32_t *)0x1FFFF7E0, 16); // FlashID + ChipID
182+
return true;
183+
}
184+
185+
uint16_t hwCPUVoltage(void)
186+
{
187+
//Not yet implemented
188+
return FUNCTION_NOT_SUPPORTED;
189+
190+
//adc_reg_map *regs = ADC1->regs;
191+
//regs->CR2 |= ADC_CR2_TSVREFE; // enable VREFINT and temp sensor
192+
//regs->SMPR1 = ADC_SMPR1_SMP17; // sample rate for VREFINT ADC channel
193+
//adc_calibrate(ADC1);
194+
//
195+
//const uint16_t vdd = adc_read(ADC1, 17);
196+
//regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor
197+
//return (uint16_t)(1200u * 4096u / vdd);
198+
}
199+
200+
uint16_t hwCPUFrequency(void)
201+
{
202+
return F_CPU/100000UL;
203+
}
204+
//https://github.com/stm32duino/Arduino_Core_STM32/issues/5#issuecomment-411868042
205+
int8_t hwCPUTemperature(void)
206+
{
207+
//STM32ADC adc(ADC1);
208+
209+
//adc.begin(PB1);
210+
211+
// adc.startConversion();
212+
213+
//uint32_t v = adc.read();
214+
//uint32_t vcc = adc.measureVcc();
215+
//int16_t temp = adc.measureTemp();
216+
//printf("ADC: %lu, vcc: %lumV temp: %dC\n", v, vcc, temp);
217+
//delay(1000);
218+
219+
// return adc.measureTemp();
220+
221+
//adc_reg_map *regs = ADC1->regs;
222+
//regs->CR2 |= ADC_CR2_TSVREFE; // enable VREFINT and Temperature sensor
223+
//regs->SMPR1 |= ADC_SMPR1_SMP16 | ADC_SMPR1_SMP17;
224+
//adc_calibrate(ADC1);
225+
//
226+
////const uint16_t adc_temp = adc_read(ADC1, 16);
227+
////const uint16_t vref = 1200 * 4096 / adc_read(ADC1, 17);
228+
//// calibrated at 25°C, ADC output = 1430mV, avg slope = 4.3mV / °C, increasing temp ~ lower voltage
229+
//const int8_t temp = static_cast<int8_t>((1430.0 - (adc_read(ADC1, 16) * 1200 / adc_read(ADC1,
230+
// 17))) / 4.3 + 25.0);
231+
//regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor
232+
//return (temp - MY_STM32F1_TEMPERATURE_OFFSET) / MY_STM32F1_TEMPERATURE_GAIN;
233+
return FUNCTION_NOT_SUPPORTED;
234+
235+
}
236+
237+
uint16_t hwFreeMem(void)
238+
{
239+
//Not yet implemented
240+
return FUNCTION_NOT_SUPPORTED;
241+
}
242+
243+
244+
void hwWatchdogReset(void)
245+
{
246+
IWatchdog.reload();
247+
//NRF_WDT->RR[0] = WDT_RR_RR_Reload;
248+
}
249+
250+
void hwReboot(void)
251+
{
252+
NVIC_SystemReset();
253+
while (true)
254+
;
255+
}

0 commit comments

Comments
 (0)