|
| 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 |
| 6 | + * the network topology allowing messages to be routed to nodes. |
| 7 | + * |
| 8 | + * Created by Henrik Ekblad <[email protected]> |
| 9 | + * Copyright (C) 2013-2019 Sensnology AB |
| 10 | + * Full contributor list: |
| 11 | + * https://github.com/mysensors/MySensors/graphs/contributors |
| 12 | + * |
| 13 | + * Documentation: http://www.mysensors.org |
| 14 | + * Support Forum: http://forum.mysensors.org |
| 15 | + * |
| 16 | + * This program is free software; you can redistribute it and/or |
| 17 | + * modify it under the terms of the GNU General Public License |
| 18 | + * version 2 as published by the Free Software Foundation. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "MyHwSTM32.h" |
| 22 | +#include "drivers/hal_conf_custom.h" |
| 23 | +#include "drivers/stm32_ADC_internal_channels.h" |
| 24 | + |
| 25 | +/* |
| 26 | + * Pinout STM32F103C8 dev board: |
| 27 | + * http://wiki.stm32duino.com/images/a/ae/Bluepillpinout.gif |
| 28 | + * |
| 29 | + * Wiring RFM69 radio / SPI1 |
| 30 | + * -------------------------------------------------- |
| 31 | + * CLK PA5 |
| 32 | + * MISO PA6 |
| 33 | + * MOSI PA7 |
| 34 | + * CSN PA4 |
| 35 | + * CE NA |
| 36 | + * IRQ PA3 (default) |
| 37 | + * |
| 38 | + * Wiring RF24 radio / SPI1 |
| 39 | + * -------------------------------------------------- |
| 40 | + * CLK PA5 |
| 41 | + * MISO PA6 |
| 42 | + * MOSI PA7 |
| 43 | + * CSN PA4 |
| 44 | + * CE PB0 (default) |
| 45 | + * IRQ NA |
| 46 | + * |
| 47 | + */ |
| 48 | +bool hwInit(void) { |
| 49 | +#if !defined(MY_DISABLED_SERIAL) |
| 50 | + MY_SERIALDEVICE.begin(MY_BAUD_RATE); |
| 51 | +#if defined(MY_GATEWAY_SERIAL) |
| 52 | + while (!MY_SERIALDEVICE) { |
| 53 | + } |
| 54 | +#endif |
| 55 | +#endif |
| 56 | + return true; |
| 57 | + // return EEPROM.begin(); |
| 58 | + |
| 59 | + // if (EEPROM.init() == EEPROM_OK) { |
| 60 | + // uint16 cnt; |
| 61 | + // EEPROM.count(&cnt); |
| 62 | + // if(cnt>=EEPROM.maxcount()) { |
| 63 | + // // tmp, WIP: format eeprom if full |
| 64 | + // EEPROM.format(); |
| 65 | + // } |
| 66 | + // return true; |
| 67 | + // } |
| 68 | + // return false; |
| 69 | +} |
| 70 | + |
| 71 | +void hwReadConfigBlock(void *buf, void *addr, size_t length) { |
| 72 | + uint8_t *dst = static_cast<uint8_t *>(buf); |
| 73 | + int pos = reinterpret_cast<int>(addr); |
| 74 | + eeprom_buffer_fill(); |
| 75 | + // return eeprom_buffered_read_byte(pos); |
| 76 | + |
| 77 | + // while (length-- > 0) { |
| 78 | + // *dst++ = EEPROM.read(pos++); |
| 79 | + // } |
| 80 | + while (length-- > 0) { |
| 81 | + *dst++ = eeprom_buffered_read_byte(pos++); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void hwWriteConfigBlock(void *buf, void *addr, size_t length) { |
| 86 | + uint8_t *src = static_cast<uint8_t *>(buf); |
| 87 | + int pos = reinterpret_cast<int>(addr); |
| 88 | + // while (length-- > 0) { |
| 89 | + // EEPROM.write(pos++, *src++); |
| 90 | + // } |
| 91 | + while (length-- > 0) { |
| 92 | + // EEPROM.write(pos++, *src++); |
| 93 | + eeprom_buffered_write_byte(pos++, *src++); |
| 94 | + } |
| 95 | + eeprom_buffer_flush(); |
| 96 | +} |
| 97 | + |
| 98 | +// void hwReadConfigBlock(void *buf, void *addr, size_t length) { |
| 99 | +// uint8_t *dst = static_cast<uint8_t *>(buf); |
| 100 | +// int pos = reinterpret_cast<int>(addr); |
| 101 | +// while (length-- > 0) { |
| 102 | +// *dst++ = EEPROM.read(pos++); |
| 103 | +// } |
| 104 | +// } |
| 105 | + |
| 106 | +// void hwWriteConfigBlock(void *buf, void *addr, size_t length) { |
| 107 | +// uint8_t *src = static_cast<uint8_t *>(buf); |
| 108 | +// int pos = reinterpret_cast<int>(addr); |
| 109 | +// while (length-- > 0) { |
| 110 | +// EEPROM.write(pos++, *src++); |
| 111 | +// } |
| 112 | +// } |
| 113 | + |
| 114 | +// uint8_t hwReadConfig(const int addr) { |
| 115 | +// uint8_t value; |
| 116 | +// hwReadConfigBlock(&value, reinterpret_cast<void *>(addr), 1); |
| 117 | +// return value; |
| 118 | +// } |
| 119 | + |
| 120 | +// void hwWriteConfig(const int addr, uint8_t value) { |
| 121 | +// hwWriteConfigBlock(&value, reinterpret_cast<void *>(addr), 1); |
| 122 | +// } |
| 123 | + |
| 124 | +int8_t hwSleep(uint32_t ms) { |
| 125 | + // TODO: Not supported! |
| 126 | + (void)ms; |
| 127 | + return MY_SLEEP_NOT_POSSIBLE; |
| 128 | +} |
| 129 | + |
| 130 | +int8_t hwSleep(const uint8_t interrupt, const uint8_t mode, uint32_t ms) { |
| 131 | + // TODO: Not supported! |
| 132 | + (void)interrupt; |
| 133 | + (void)mode; |
| 134 | + (void)ms; |
| 135 | + return MY_SLEEP_NOT_POSSIBLE; |
| 136 | +} |
| 137 | + |
| 138 | +int8_t hwSleep(const uint8_t interrupt1, const uint8_t mode1, |
| 139 | + const uint8_t interrupt2, const uint8_t mode2, uint32_t ms) { |
| 140 | + // TODO: Not supported! |
| 141 | + (void)interrupt1; |
| 142 | + (void)mode1; |
| 143 | + (void)interrupt2; |
| 144 | + (void)mode2; |
| 145 | + (void)ms; |
| 146 | + return MY_SLEEP_NOT_POSSIBLE; |
| 147 | +} |
| 148 | + |
| 149 | +void hwRandomNumberInit(void) { |
| 150 | + // use internal temperature sensor as noise source |
| 151 | + uint32_t seed = 0; |
| 152 | + uint16_t currentValue = 0; |
| 153 | + uint16_t newValue = 0; |
| 154 | + |
| 155 | + for (uint8_t i = 0; i < 32; i++) { |
| 156 | + const uint32_t timeout = hwMillis() + 20; |
| 157 | + while (timeout >= hwMillis()) { |
| 158 | + newValue = analogRead(ATEMP); |
| 159 | + if (newValue != currentValue) { |
| 160 | + currentValue = newValue; |
| 161 | + break; |
| 162 | + } |
| 163 | + } |
| 164 | + seed ^= ((newValue + hwMillis()) & 7) << i; |
| 165 | + } |
| 166 | + randomSeed(seed); |
| 167 | +} |
| 168 | + |
| 169 | +bool hwUniqueID(unique_id_t *uniqueID) { |
| 170 | + (void)memcpy((uint8_t *)uniqueID, (uint32_t *)0x1FFFF7E0, |
| 171 | + 16); // FlashID + ChipID |
| 172 | + return true; |
| 173 | +} |
| 174 | + |
| 175 | +uint16_t hwCPUVoltage(void) { return (uint16_t)readVref(); } |
| 176 | + |
| 177 | +uint16_t hwCPUFrequency(void) { return F_CPU / 100000UL; } |
| 178 | + |
| 179 | +int8_t hwCPUTemperature(void) { return (int8_t)readTempSensor(readVref()); } |
| 180 | + |
| 181 | +uint16_t hwFreeMem(void) { |
| 182 | + // Not yet implemented |
| 183 | + return FUNCTION_NOT_SUPPORTED; |
| 184 | +} |
0 commit comments