Skip to content
Snippets Groups Projects
  • da1l6's avatar
    2e7c200c
    CardReader: Improve Error condition user feedback. · 2e7c200c
    da1l6 authored
    Display control error messages on Cardreader display
    Reset Cardreader display after 4 seconds of idle to make sure the display is clean
    when a new user inserts a card.
    Detect a dead portal control and display an error message.
    2e7c200c
    History
    CardReader: Improve Error condition user feedback.
    da1l6 authored
    Display control error messages on Cardreader display
    Reset Cardreader display after 4 seconds of idle to make sure the display is clean
    when a new user inserts a card.
    Detect a dead portal control and display an error message.
random.c 1.27 KiB
#include "common.h"
#include "random.h"
#include <util/delay.h>

#define RNG_POWER_PIN (1 << 4)
#define RNG_RANDOM_PIN (1 << 3)
#define RNG_DDR       DDRA
#define RNG_DORT      PORTA


#define ADC_RNG_CHANNEL 3
uint16_t seed;

uint8_t rnd_adc_read(void);

void rnd_init(void){
	RNG_DDR |= RNG_POWER_PIN;
	RNG_DDR &= ~RNG_RANDOM_PIN;
	
	RNG_DORT |= RNG_POWER_PIN;
	
	seed = 0xFFFF;
	seed = _crc16_update(seed, rnd_adc_read());
	seed = _crc16_update(seed, rnd_adc_read());
}

uint8_t rnd_adc_read(void){
	uint8_t result;
	
	ADMUX = ADC_RNG_CHANNEL | (1 << REFS0) | (1 << REFS1); // Select channel, 2.56V internal Reference voltage.
	
	ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler 
	                                                           // setzen auf 8 (1) und ADC aktivieren (1)
	
	ADCSRA |= (1<<ADSC);
	while ( ADCSRA & (1<<ADSC) ) {;}
	
	result = ADCL; //use lower byte
	ADCH;          //discard high byte
	
	return result;
}

uint8_t rnd_get_byte(void){
	seed = _crc16_update(seed, rnd_adc_read());
	return ((seed & 0xFF) ^ (seed >> 8));
}

uint8_t rnd_get_bytes(uint8_t* buffer, uint8_t length){
	RNG_DORT |= RNG_POWER_PIN;
	_delay_us(1);
	
	for (uint8_t i = 0; i < length; i++){
		buffer[i] = rnd_get_byte();
	}
	
	RNG_DORT &= ~RNG_POWER_PIN;
	return 1;
}