Browse Source

Added spaces and cleaning up the structure.

master
Englebert 4 years ago
parent
commit
79549a7eb9
  1. 414
      programmer/programmer.ino

414
programmer/programmer.ino

@ -13,17 +13,17 @@
// Baud rate: 115200
// Digital pin D10 on arduino nano
#define C2D_PORT PORTB // D10
#define C2D_PIN 2 // D10
#define C2D_PORT PORTB // D10
#define C2D_PIN 2 // D10
// Digital pin 3 on Mega
#define C2CK_PORT PORTB // D11
#define C2CK_PIN 3 // D11
// Digital pin D11 on arduino nano
#define C2CK_PORT PORTB // D11
#define C2CK_PIN 3 // D11
#define LED LED_BUILTIN
#define LED LED_BUILTIN
#define INBUSY 0x02
#define OUTREADY 0x01
#define INBUSY 0x02
#define OUTREADY 0x01
static void c2_send_bits (unsigned char data, unsigned char len);
static unsigned char c2_read_bits (unsigned char len);
@ -43,234 +43,232 @@ unsigned char c2_erase_device (void);
void c2_rst() {
C2CK_PORT &= ~(1<<C2CK_PIN);
delayMicroseconds(50);
C2CK_PORT |= (1<<C2CK_PIN);
delayMicroseconds(50);
C2CK_PORT &= ~(1<<C2CK_PIN);
delayMicroseconds(50);
C2CK_PORT |= (1<<C2CK_PIN);
delayMicroseconds(50);
}
#define c2_pulse_clk()\
C2CK_PORT &= ~(1<<C2CK_PIN); \
C2CK_PORT |= (1<<C2CK_PIN);
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++) {
c2_pulse_clk();
data = data >> 1;
if (PINB & (1<<C2D_PIN)) {
data = data | mask;
C2CK_PORT &= ~(1<<C2CK_PIN); \
C2CK_PORT |= (1<<C2CK_PIN);
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++) {
c2_pulse_clk();
data = data >> 1;
if(PINB & (1<<C2D_PIN)) {
data = data | mask;
}
}
}
DDRB |= (1<<C2D_PIN);
//pinMode(C2D, OUTPUT);
DDRB |= (1<<C2D_PIN);
//pinMode(C2D, OUTPUT);
return data;
return data;
}
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) {
C2D_PORT |= (1<<C2D_PIN);
} else {
C2D_PORT &= ~(1<<C2D_PIN);
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) {
C2D_PORT |= (1<<C2D_PIN);
} else {
C2D_PORT &= ~(1<<C2D_PIN);
}
c2_pulse_clk();
data = data >> 1;
}
c2_pulse_clk();
data = data >> 1;
}
}
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) {
retval = c2_read_bits(1);
}
c2_send_bits(0x0, 1);
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) {
retval = c2_read_bits(1);
}
c2_send_bits(0x0, 1);
}
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();
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();
}
static unsigned char c2_poll_bit_low (unsigned char mask) {
unsigned char retval;
retval = c2_read_addr();
while (retval&mask) retval = c2_read_addr();
static unsigned char c2_poll_bit_low(unsigned char mask) {
unsigned char retval;
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 retval,i;
c2_write_addr(0xB4);
c2_write_data(0x06);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(addr>>8);
c2_poll_bit_low(INBUSY);
c2_write_data(addr&0xFF);
c2_poll_bit_low(INBUSY);
c2_write_data(len);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
for (i=0;i<len;i++) {
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);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
data[i] = retval;
}
return i;
c2_write_data(addr>>8);
c2_poll_bit_low(INBUSY);
c2_write_data(addr&0xFF);
c2_poll_bit_low(INBUSY);
c2_write_data(len);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
for(i=0; i<len; i++){
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
data[i] = retval;
}
return i;
}
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);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(addr>>8);
c2_poll_bit_low(INBUSY);
c2_write_data(addr&0xFF);
c2_poll_bit_low(INBUSY);
c2_write_data(len);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
for (i=0;i<len;i++) {
c2_write_data(data[i] );
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);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(addr>>8);
c2_poll_bit_low(INBUSY);
c2_write_data(addr&0xFF);
c2_poll_bit_low(INBUSY);
}
c2_poll_bit_high(OUTREADY);
c2_write_data(len);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
for(i=0; i<len; i++) {
c2_write_data(data[i] );
c2_poll_bit_low(INBUSY);
}
c2_poll_bit_high(OUTREADY);
}
unsigned char c2_erase_device (void) {
unsigned char retval;
c2_write_addr(0xB4);
c2_write_data(0x03);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(0xDE);
c2_poll_bit_low(INBUSY);
c2_write_data(0xAD);
c2_poll_bit_low(INBUSY);
c2_write_data(0xA5);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
unsigned char retval;
c2_write_addr(0xB4);
c2_write_data(0x03);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(0xDE);
c2_poll_bit_low(INBUSY);
c2_write_data(0xAD);
c2_poll_bit_low(INBUSY);
c2_write_data(0xA5);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
}
unsigned char c2_write_sfr (unsigned char addr, unsigned char val) {
unsigned char retval;
c2_write_addr(0xB4);
c2_write_data(0x0A);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(addr);
c2_poll_bit_low(INBUSY);
c2_write_data(0x1);
c2_poll_bit_low(INBUSY);
c2_write_data(val);
c2_poll_bit_low(INBUSY);
unsigned char retval;
c2_write_addr(0xB4);
c2_write_data(0x0A);
c2_poll_bit_low(INBUSY);
c2_poll_bit_high(OUTREADY);
retval = c2_read_data();
c2_write_data(addr);
c2_poll_bit_low(INBUSY);
c2_write_data(0x1);
c2_poll_bit_low(INBUSY);
c2_write_data(val);
c2_poll_bit_low(INBUSY);
}
unsigned char c2_init_PI (void) {
c2_rst();
c2_write_addr(0x02);
c2_write_data(0x02);
c2_write_data(0x04);
c2_write_data(0x01);
return 0;
c2_rst();
c2_write_addr(0x02);
c2_write_data(0x02);
c2_write_data(0x04);
c2_write_data(0x01);
return 0;
}
unsigned char c2_init_PI_sfr (void) {
c2_rst();
c2_write_addr(0x02);
c2_write_data(0x02);
c2_write_data(0x04);
c2_write_data(0x01);
// set up SFRs
delay(25);
c2_write_sfr(0xff, 0x80);
delay(1);
c2_write_sfr(0xef, 0x02);
delay(1);
c2_write_sfr(0xA9, 0x00);
return 0;
c2_rst();
c2_write_addr(0x02);
c2_write_data(0x02);
c2_write_data(0x04);
c2_write_data(0x01);
// set up SFRs
delay(25);
c2_write_sfr(0xff, 0x80);
delay(1);
c2_write_sfr(0xef, 0x02);
delay(1);
c2_write_sfr(0xA9, 0x00);
return 0;
}
static unsigned char c2_read_data() {
unsigned char retval;
c2_send_bits(0x0, 1);
c2_send_bits(0x0, 2);
c2_send_bits(0x0, 2);
retval = 0;
while (retval == 0) {
retval = c2_read_bits(1);
}
retval = c2_read_bits(8);
c2_send_bits(0x0, 1);
return retval;
unsigned char retval;
c2_send_bits(0x0, 1);
c2_send_bits(0x0, 2);
c2_send_bits(0x0, 2);
retval = 0;
while (retval == 0) {
retval = c2_read_bits(1);
}
retval = c2_read_bits(8);
c2_send_bits(0x0, 1);
return retval;
}
static unsigned char c2_read_addr() {
unsigned char retval;
c2_send_bits(0x0, 1);
c2_send_bits(0x2, 2);
retval = c2_read_bits(8);
c2_send_bits(0x0, 1);
return retval;
unsigned char retval;
c2_send_bits(0x0, 1);
c2_send_bits(0x2, 2);
retval = c2_read_bits(8);
c2_send_bits(0x0, 1);
return retval;
}
void c2_write_addr(unsigned char addr) {
c2_send_bits(0x0,1);
c2_send_bits(0x3,2);
c2_send_bits(addr,8);
c2_send_bits(0x0,1);
c2_send_bits(0x0,1);
c2_send_bits(0x3,2);
c2_send_bits(addr,8);
c2_send_bits(0x0,1);
}
void setup() {
// Serial.begin(1000000);
Serial.begin(115200);
DDRB |= (1<<C2D_PIN);
DDRB |= (1<<C2CK_PIN);
C2CK_PORT |= (1<<C2CK_PIN);
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);
// Serial.begin(1000000);
Serial.begin(115200);
DDRB |= (1<<C2D_PIN);
DDRB |= (1<<C2CK_PIN);
C2CK_PORT |= (1<<C2CK_PIN);
delay(50);
}
*/
// Startup indicator
for(int i = 0; i < 10; i++) {
digitalWrite(LED, HIGH);
delay(50);
digitalWrite(LED, LOW);
delay(50);
}
}
unsigned int i;
@ -281,23 +279,25 @@ unsigned char flash_buffer[300];
unsigned long addr;
unsigned char rx_state_machine (unsigned char state, unsigned char rx_char) {
if (state==0) {
rx_message_ptr = 0;
rx_message[rx_message_ptr++] = rx_char;
return 1;
}
if (state==1) {
bytes_to_receive = rx_char;
rx_message[rx_message_ptr++] = rx_char;
if (bytes_to_receive==0) return 3;
return 2;
}
if (state==2) {
rx_message[rx_message_ptr++] = rx_char;
bytes_to_receive--;
if (bytes_to_receive==0) return 3;
}
return state;
if(state==0) {
rx_message_ptr = 0;
rx_message[rx_message_ptr++] = rx_char;
return 1;
}
if(state==1) {
bytes_to_receive = rx_char;
rx_message[rx_message_ptr++] = rx_char;
if(bytes_to_receive==0)
return 3;
return 2;
}
if(state==2) {
rx_message[rx_message_ptr++] = rx_char;
bytes_to_receive--;
if(bytes_to_receive==0)
return 3;
}
return state;
}
#define swap(x) ((((x)>>8) & 0xff) | (((x)<<8) & 0xff00))

Loading…
Cancel
Save