New version with PRNG numbers for challenge response

master
Wilhelm Westermark 8 years ago
parent 5afa7988fc
commit 46cddf2792

@ -1,48 +1,59 @@
/* I2C Echo Example */ /* I2C Echo Example */
#include "../I2CSlave/I2CSlave.h" #include "../avr-i2c-slave/I2CSlave.h"
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define I2C_ADDR 0x20 #define I2C_ADDR 0x20
volatile uint8_t data; volatile uint8_t data;
volatile uint8_t final; volatile uint8_t send=0;
void I2C_received(uint8_t received_data) void I2C_received(uint8_t received_data)
{ {
data = received_data; data = received_data;
} }
void I2C_requested() void I2C_requested() {
{ I2C_transmitByte(send);
I2C_transmitByte(data);
} }
void I2C_challenge() { void I2C_challenge() {
I2C_transmitByte(data*3); I2C_transmitByte(send);
} }
void setup() void setup() {
{
// set received/requested callbacks // set received/requested callbacks
I2C_setCallbacks(I2C_received, I2C_requested); I2C_setCallbacks(I2C_received, I2C_requested);
// init I2C // init I2C
I2C_init(I2C_ADDR); I2C_init(I2C_ADDR);
// init output gpio // init output gpio
DDRB = 0b00000001; DDRC = 0b00000100; // PC2
} TCNT1 = 0;
TCCR1B |= (1 << CS10) | (1 << CS11); // No prescaling
int main() }
{ int main() {
setup(); setup();
int challenge = 0;
unsigned int i = 0;
// Main program loop // Main program loop
while(1) { while(1) {
if(data != 4) { if(data == 1) {
i = TCNT1;
srand(i);
send = rand();
while(data != send+3) {
I2C_challenge(); I2C_challenge();
} }
else { send = 17;
//PORTB |= 1<<1; I2C_challenge();
data = 2; PORTC |= 2<<1;
I2C_requested(); _delay_ms(5000);
PORTC = 0;
data = 0;
} }/* else {
send = 0;
I2C_challenge();
}*/
} }
} }

@ -9,7 +9,7 @@ MCU = atmega328p
FORMAT = ihex FORMAT = ihex
TARGET = main TARGET = main
SRC = $(TARGET).c \ SRC = $(TARGET).c \
I2CSlave.c ../avr-i2c-slave/I2CSlave.c
ASRC = ASRC =
OPT = s OPT = s

Loading…
Cancel
Save