AVR: Harmonize code (#1059)
This commit is contained in:
@@ -38,7 +38,7 @@ volatile uint8_t _wakeUp1Interrupt =
|
|||||||
volatile uint8_t _wakeUp2Interrupt =
|
volatile uint8_t _wakeUp2Interrupt =
|
||||||
INVALID_INTERRUPT_NUM; // Interrupt number for wakeUp2-callback.
|
INVALID_INTERRUPT_NUM; // Interrupt number for wakeUp2-callback.
|
||||||
|
|
||||||
void wakeUp1()
|
void wakeUp1(void)
|
||||||
{
|
{
|
||||||
// Disable sleep. When an interrupt occurs after attachInterrupt,
|
// Disable sleep. When an interrupt occurs after attachInterrupt,
|
||||||
// but before sleeping the CPU would not wake up.
|
// but before sleeping the CPU would not wake up.
|
||||||
@@ -50,7 +50,7 @@ void wakeUp1()
|
|||||||
_wokeUpByInterrupt = _wakeUp1Interrupt;
|
_wokeUpByInterrupt = _wakeUp1Interrupt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void wakeUp2()
|
void wakeUp2(void)
|
||||||
{
|
{
|
||||||
sleep_disable();
|
sleep_disable();
|
||||||
detachInterrupt(_wakeUp2Interrupt);
|
detachInterrupt(_wakeUp2Interrupt);
|
||||||
@@ -60,7 +60,7 @@ void wakeUp2()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool interruptWakeUp()
|
inline bool interruptWakeUp(void)
|
||||||
{
|
{
|
||||||
return _wokeUpByInterrupt != INVALID_INTERRUPT_NUM;
|
return _wokeUpByInterrupt != INVALID_INTERRUPT_NUM;
|
||||||
}
|
}
|
||||||
@@ -86,7 +86,7 @@ void hwPowerDown(const uint8_t wdto)
|
|||||||
// disable ADC for power saving
|
// disable ADC for power saving
|
||||||
ADCSRA &= ~(1 << ADEN);
|
ADCSRA &= ~(1 << ADEN);
|
||||||
// save WDT settings
|
// save WDT settings
|
||||||
uint8_t WDTsave = WDTCSR;
|
const uint8_t WDTsave = WDTCSR;
|
||||||
if (wdto != WDTO_SLEEP_FOREVER) {
|
if (wdto != WDTO_SLEEP_FOREVER) {
|
||||||
wdt_enable(wdto);
|
wdt_enable(wdto);
|
||||||
// enable WDT interrupt before system reset
|
// enable WDT interrupt before system reset
|
||||||
@@ -98,7 +98,7 @@ void hwPowerDown(const uint8_t wdto)
|
|||||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||||
cli();
|
cli();
|
||||||
sleep_enable();
|
sleep_enable();
|
||||||
#if defined __AVR_ATmega328P__
|
#if defined(__AVR_ATmega328P__)
|
||||||
sleep_bod_disable();
|
sleep_bod_disable();
|
||||||
#endif
|
#endif
|
||||||
// Enable interrupts & sleep until WDT or ext. interrupt
|
// Enable interrupts & sleep until WDT or ext. interrupt
|
||||||
@@ -144,12 +144,13 @@ int8_t hwSleep(uint32_t ms)
|
|||||||
return MY_WAKE_UP_BY_TIMER;
|
return MY_WAKE_UP_BY_TIMER;
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t hwSleep(uint8_t interrupt, uint8_t mode, uint32_t ms)
|
int8_t hwSleep(const uint8_t interrupt, const uint8_t mode, uint32_t ms)
|
||||||
{
|
{
|
||||||
return hwSleep(interrupt,mode,INVALID_INTERRUPT_NUM,0u,ms);
|
return hwSleep(interrupt, mode, INVALID_INTERRUPT_NUM, 0u, ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2,
|
int8_t hwSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2,
|
||||||
|
const uint8_t mode2,
|
||||||
uint32_t ms)
|
uint32_t ms)
|
||||||
{
|
{
|
||||||
// ATMega328P supports following modes to wake from sleep: LOW, CHANGE, RISING, FALLING
|
// ATMega328P supports following modes to wake from sleep: LOW, CHANGE, RISING, FALLING
|
||||||
@@ -203,14 +204,13 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void hwRandomNumberInit()
|
inline void hwRandomNumberInit(void)
|
||||||
{
|
{
|
||||||
// This function initializes the random number generator with a seed
|
// This function initializes the random number generator with a seed
|
||||||
// of 32 bits. This method is good enough to earn FIPS 140-2 conform
|
// of 32 bits. This method is good enough to earn FIPS 140-2 conform
|
||||||
// random data. This should reach to generate 32 Bit for randomSeed().
|
// random data. This should reach to generate 32 Bit for randomSeed().
|
||||||
uint32_t seed = 0;
|
uint32_t seed = 0;
|
||||||
uint32_t start = millis();
|
uint32_t timeout = millis() + 20;
|
||||||
uint32_t timeout = start + 20;
|
|
||||||
|
|
||||||
// Trigger floating effect of an unconnected pin
|
// Trigger floating effect of an unconnected pin
|
||||||
pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP);
|
pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP);
|
||||||
@@ -219,7 +219,7 @@ inline void hwRandomNumberInit()
|
|||||||
|
|
||||||
// Generate 32 bits of datas
|
// Generate 32 bits of datas
|
||||||
for (uint8_t i=0; i<32; i++) {
|
for (uint8_t i=0; i<32; i++) {
|
||||||
int pinValue = analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN);
|
const int pinValue = analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN);
|
||||||
// Wait until the analog value has changed
|
// Wait until the analog value has changed
|
||||||
while ((pinValue == analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)) && (timeout>=millis())) {
|
while ((pinValue == analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)) && (timeout>=millis())) {
|
||||||
seed ^= (millis() << i);
|
seed ^= (millis() << i);
|
||||||
@@ -230,11 +230,10 @@ inline void hwRandomNumberInit()
|
|||||||
pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT);
|
pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT);
|
||||||
// Pause a short while
|
// Pause a short while
|
||||||
delay(seed % 10);
|
delay(seed % 10);
|
||||||
timeout = millis()+20;
|
timeout = millis() + 20;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
randomSeed(seed);
|
randomSeed(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,14 +246,18 @@ bool hwUniqueID(unique_id_t* uniqueID)
|
|||||||
*((uint8_t*)uniqueID + 1) = boot_signature_byte_get(0x02);
|
*((uint8_t*)uniqueID + 1) = boot_signature_byte_get(0x02);
|
||||||
*((uint8_t*)uniqueID + 2) = boot_signature_byte_get(0x04);
|
*((uint8_t*)uniqueID + 2) = boot_signature_byte_get(0x04);
|
||||||
*((uint8_t*)uniqueID + 3) = boot_signature_byte_get(0x01); //OSCCAL
|
*((uint8_t*)uniqueID + 3) = boot_signature_byte_get(0x01); //OSCCAL
|
||||||
|
#if defined(__AVR_ATmega328PB__)
|
||||||
// ATMEGA328PB specifics, has unique ID
|
// ATMEGA328PB specifics, has unique ID
|
||||||
//for(uint8_t idx = 0; idx < 10; idx++) {
|
for(uint8_t idx = 0; idx < 10; idx++) {
|
||||||
// *((uint8_t*)uniqueID + 4 + idx) = boot_signature_byte_get(0xE + idx);
|
*((uint8_t*)uniqueID + 4 + idx) = boot_signature_byte_get(0xE + idx);
|
||||||
//}
|
}
|
||||||
return false; // false, since no unique ID returned
|
return true; // unique ID returned
|
||||||
|
#else
|
||||||
|
return false; // no unique ID returned
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t hwCPUVoltage()
|
uint16_t hwCPUVoltage(void)
|
||||||
{
|
{
|
||||||
// Measure Vcc against 1.1V Vref
|
// Measure Vcc against 1.1V Vref
|
||||||
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||||
@@ -275,16 +278,19 @@ uint16_t hwCPUVoltage()
|
|||||||
return (1125300UL) / ADC;
|
return (1125300UL) / ADC;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t hwCPUFrequency()
|
uint16_t hwCPUFrequency(void)
|
||||||
{
|
{
|
||||||
cli();
|
cli();
|
||||||
|
// save WDT & timer settings
|
||||||
|
const uint8_t WDTsave = WDTCSR;
|
||||||
|
const uint8_t TCCR1Asave = TCCR1A;
|
||||||
|
const uint8_t TCCR1Csave = TCCR1C;
|
||||||
// setup timer1
|
// setup timer1
|
||||||
TIFR1 = 0xFF;
|
TIFR1 = 0xFF;
|
||||||
TCNT1 = 0;
|
TCNT1 = 0;
|
||||||
TCCR1A = 0;
|
TCCR1A = 0;
|
||||||
TCCR1C = 0;
|
TCCR1C = 0;
|
||||||
// save WDT settings
|
// set wdt
|
||||||
uint8_t WDTsave = WDTCSR;
|
|
||||||
wdt_enable(WDTO_500MS);
|
wdt_enable(WDTO_500MS);
|
||||||
// enable WDT interrupt mode => first timeout WDIF, 2nd timeout reset
|
// enable WDT interrupt mode => first timeout WDIF, 2nd timeout reset
|
||||||
WDTCSR |= (1 << WDIE);
|
WDTCSR |= (1 << WDIE);
|
||||||
@@ -300,11 +306,14 @@ uint16_t hwCPUFrequency()
|
|||||||
WDTCSR |= (1 << WDCE) | (1 << WDE);
|
WDTCSR |= (1 << WDCE) | (1 << WDE);
|
||||||
WDTCSR = WDTsave;
|
WDTCSR = WDTsave;
|
||||||
sei();
|
sei();
|
||||||
|
// restore timer settings
|
||||||
|
TCCR1A = TCCR1Asave;
|
||||||
|
TCCR1C = TCCR1Csave;
|
||||||
// return frequency in 1/10MHz (accuracy +- 10%)
|
// return frequency in 1/10MHz (accuracy +- 10%)
|
||||||
return TCNT1 * 2048UL / 100000UL;
|
return TCNT1 * 2048UL / 100000UL;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t hwFreeMem()
|
uint16_t hwFreeMem(void)
|
||||||
{
|
{
|
||||||
extern int __heap_start, *__brkval;
|
extern int __heap_start, *__brkval;
|
||||||
int v;
|
int v;
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ bool hwInit(void);
|
|||||||
#define hwReadConfigBlock(__buf, __pos, __length) eeprom_read_block((void*)(__buf), (void*)(__pos), (__length))
|
#define hwReadConfigBlock(__buf, __pos, __length) eeprom_read_block((void*)(__buf), (void*)(__pos), (__length))
|
||||||
#define hwWriteConfigBlock(__buf, __pos, __length) eeprom_update_block((void*)(__buf), (void*)(__pos), (__length))
|
#define hwWriteConfigBlock(__buf, __pos, __length) eeprom_update_block((void*)(__buf), (void*)(__pos), (__length))
|
||||||
|
|
||||||
inline void hwRandomNumberInit();
|
inline void hwRandomNumberInit(void);
|
||||||
void hwInternalSleep(unsigned long ms);
|
void hwInternalSleep(uint32_t ms);
|
||||||
|
|
||||||
#ifndef DOXYGEN
|
#ifndef DOXYGEN
|
||||||
#define MY_CRITICAL_SECTION ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
|
#define MY_CRITICAL_SECTION ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
|
||||||
|
|||||||
Reference in New Issue
Block a user