|
|
@ -16,7 +16,7 @@ |
|
|
|
#define C2D_PORT PORTB // D10
|
|
|
|
#define C2D_PIN 2 // D10
|
|
|
|
|
|
|
|
// Digital pin 3 on Mega
|
|
|
|
// Digital pin D11 on arduino nano
|
|
|
|
#define C2CK_PORT PORTB // D11
|
|
|
|
#define C2CK_PIN 3 // D11
|
|
|
|
|
|
|
@ -54,17 +54,18 @@ void c2_rst() { |
|
|
|
C2CK_PORT |= (1<<C2CK_PIN); |
|
|
|
|
|
|
|
|
|
|
|
static unsigned char c2_read_bits (unsigned char len) { |
|
|
|
static unsigned char c2_read_bits(unsigned char len) { |
|
|
|
unsigned char i, data, mask; |
|
|
|
mask = 0x01 << (len-1); |
|
|
|
data = 0; |
|
|
|
|
|
|
|
//pinMode(C2D, INPUT);
|
|
|
|
DDRB &= ~(1<<C2D_PIN); |
|
|
|
PINB &= (1<<C2D_PIN); |
|
|
|
for (i=0;i<len;i++) { |
|
|
|
for(i=0; i<len; i++) { |
|
|
|
c2_pulse_clk(); |
|
|
|
data = data >> 1; |
|
|
|
if (PINB & (1<<C2D_PIN)) { |
|
|
|
if(PINB & (1<<C2D_PIN)) { |
|
|
|
data = data | mask; |
|
|
|
} |
|
|
|
} |
|
|
@ -74,12 +75,14 @@ static unsigned char c2_read_bits (unsigned char len) { |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
static void c2_send_bits (unsigned char data, unsigned char len) { |
|
|
|
|
|
|
|
static void c2_send_bits(unsigned char data, unsigned char len) { |
|
|
|
unsigned char i; |
|
|
|
|
|
|
|
//pinMode(C2D, OUTPUT);
|
|
|
|
DDRB |= (1<<C2D_PIN); |
|
|
|
for (i=0;i<len;i++) { |
|
|
|
if (data&0x01) { |
|
|
|
for(i=0; i<len; i++) { |
|
|
|
if(data & 0x01) { |
|
|
|
C2D_PORT |= (1<<C2D_PIN); |
|
|
|
} else { |
|
|
|
C2D_PORT &= ~(1<<C2D_PIN); |
|
|
@ -89,32 +92,36 @@ static void c2_send_bits (unsigned char data, unsigned char len) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static void c2_write_data (unsigned char data) { |
|
|
|
|
|
|
|
static void c2_write_data(unsigned char data) { |
|
|
|
unsigned char retval; |
|
|
|
c2_send_bits(0x0, 1); |
|
|
|
c2_send_bits(0x1, 2); |
|
|
|
c2_send_bits(0x0, 2); |
|
|
|
c2_send_bits(data, 8); |
|
|
|
retval = 0; |
|
|
|
while (retval == 0) { |
|
|
|
while(retval == 0) { |
|
|
|
retval = c2_read_bits(1); |
|
|
|
} |
|
|
|
c2_send_bits(0x0, 1); |
|
|
|
} |
|
|
|
|
|
|
|
static unsigned char c2_poll_bit_high (unsigned char mask) { |
|
|
|
|
|
|
|
static unsigned char c2_poll_bit_high(unsigned char mask) { |
|
|
|
unsigned char retval; |
|
|
|
retval = c2_read_addr(); |
|
|
|
while ((retval&mask)==0) retval = c2_read_addr(); |
|
|
|
while((retval&mask)==0) retval = c2_read_addr(); |
|
|
|
} |
|
|
|
|
|
|
|
static unsigned char c2_poll_bit_low (unsigned char mask) { |
|
|
|
|
|
|
|
static unsigned char c2_poll_bit_low(unsigned char mask) { |
|
|
|
unsigned char retval; |
|
|
|
retval = c2_read_addr(); |
|
|
|
while (retval&mask) retval = c2_read_addr(); |
|
|
|
while(retval&mask) retval = c2_read_addr(); |
|
|
|
} |
|
|
|
|
|
|
|
unsigned char c2_read_flash_block (unsigned int addr, unsigned char * data, unsigned char len) { |
|
|
|
|
|
|
|
unsigned char c2_read_flash_block(unsigned int addr, unsigned char * data, unsigned char len) { |
|
|
|
unsigned char retval,i; |
|
|
|
c2_write_addr(0xB4); |
|
|
|
c2_write_data(0x06); |
|
|
@ -129,7 +136,7 @@ unsigned char c2_read_flash_block (unsigned int addr, unsigned char * data, unsi |
|
|
|
c2_poll_bit_low(INBUSY); |
|
|
|
c2_poll_bit_high(OUTREADY); |
|
|
|
retval = c2_read_data(); |
|
|
|
for (i=0;i<len;i++) { |
|
|
|
for(i=0; i<len; i++){ |
|
|
|
c2_poll_bit_high(OUTREADY); |
|
|
|
retval = c2_read_data(); |
|
|
|
data[i] = retval; |
|
|
@ -137,7 +144,7 @@ unsigned char c2_read_flash_block (unsigned int addr, unsigned char * data, unsi |
|
|
|
return i; |
|
|
|
} |
|
|
|
|
|
|
|
unsigned char c2_write_flash_block (unsigned int addr, unsigned char * data, unsigned char len) { |
|
|
|
unsigned char c2_write_flash_block(unsigned int addr, unsigned char * data, unsigned char len) { |
|
|
|
unsigned char retval,i; |
|
|
|
c2_write_addr(0xB4); |
|
|
|
c2_write_data(0x07); |
|
|
@ -152,7 +159,7 @@ unsigned char c2_write_flash_block (unsigned int addr, unsigned char * data, uns |
|
|
|
c2_poll_bit_low(INBUSY); |
|
|
|
c2_poll_bit_high(OUTREADY); |
|
|
|
retval = c2_read_data(); |
|
|
|
for (i=0;i<len;i++) { |
|
|
|
for(i=0; i<len; i++) { |
|
|
|
c2_write_data(data[i] ); |
|
|
|
c2_poll_bit_low(INBUSY); |
|
|
|
} |
|
|
@ -255,22 +262,13 @@ void setup() { |
|
|
|
DDRB |= (1<<C2CK_PIN); |
|
|
|
C2CK_PORT |= (1<<C2CK_PIN); |
|
|
|
|
|
|
|
// Startup indicator
|
|
|
|
for(int i = 0; i < 10; i++) { |
|
|
|
digitalWrite(LED, HIGH); |
|
|
|
delay(500); |
|
|
|
digitalWrite(LED, LOW); |
|
|
|
|
|
|
|
// pinMode(11, OUTPUT);
|
|
|
|
// pinMode(12, OUTPUT);
|
|
|
|
|
|
|
|
// testing
|
|
|
|
/*
|
|
|
|
for(int i=0; i < 100; i++) { |
|
|
|
C2CK_PORT &= ~(1<<C2CK_PIN); |
|
|
|
delay(50); |
|
|
|
C2CK_PORT |= (1<<C2CK_PIN); |
|
|
|
digitalWrite(LED, LOW); |
|
|
|
delay(50); |
|
|
|
} |
|
|
|
*/ |
|
|
|
} |
|
|
|
|
|
|
|
unsigned int i; |
|
|
@ -281,21 +279,23 @@ unsigned char flash_buffer[300]; |
|
|
|
unsigned long addr; |
|
|
|
|
|
|
|
unsigned char rx_state_machine (unsigned char state, unsigned char rx_char) { |
|
|
|
if (state==0) { |
|
|
|
if(state==0) { |
|
|
|
rx_message_ptr = 0; |
|
|
|
rx_message[rx_message_ptr++] = rx_char; |
|
|
|
return 1; |
|
|
|
} |
|
|
|
if (state==1) { |
|
|
|
if(state==1) { |
|
|
|
bytes_to_receive = rx_char; |
|
|
|
rx_message[rx_message_ptr++] = rx_char; |
|
|
|
if (bytes_to_receive==0) return 3; |
|
|
|
if(bytes_to_receive==0) |
|
|
|
return 3; |
|
|
|
return 2; |
|
|
|
} |
|
|
|
if (state==2) { |
|
|
|
if(state==2) { |
|
|
|
rx_message[rx_message_ptr++] = rx_char; |
|
|
|
bytes_to_receive--; |
|
|
|
if (bytes_to_receive==0) return 3; |
|
|
|
if(bytes_to_receive==0) |
|
|
|
return 3; |
|
|
|
} |
|
|
|
return state; |
|
|
|
} |
|
|
|