|
| 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