/** * Based on https://gist.github.com/racerxdl/c9a592808acdd9cd178e6e97c83f8baf * which was based on: https://github.com/jaromir-sukuba/efm8prog/ * Use his SW to program EFM8 using arduino.a * Also modified from: https://github.com/conorpp/efm8-arduino-programmer.git * * Edited by: Englebert * Date: Sat 12 Jun 22:50:10 +08 2021 **/ // GPIO is manipulated through PORT mappings for better speed. // Flashes EFM8 at about 10kB/s // Baud rate: 115200 // Digital pin D10 on arduino nano #define C2D_PORT PORTB // D10 #define C2D_PIN 2 // D10 // Digital pin D11 on arduino nano #define C2CK_PORT PORTB // D11 #define C2CK_PIN 3 // D11 #define LED LED_BUILTIN #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); void c2_rst (void); void c2_write_addr (unsigned char addr); static unsigned char c2_read_addr (void); static unsigned char c2_read_data (void); static void c2_write_data (unsigned char addr); unsigned char c2_init_PI (void); unsigned char c2_read_flash_block (unsigned int addr, unsigned char * data, unsigned char len); static unsigned char c2_poll_bit_low (unsigned char mask); static unsigned char c2_poll_bit_high (unsigned char mask); unsigned char c2_write_flash_block (unsigned int addr, unsigned char * data, unsigned char len); unsigned char c2_erase_device (void); void c2_rst() { C2CK_PORT &= ~(1<> 1; if(PINB & (1<> 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_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>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>8) & 0xff) | (((x)<<8) & 0xff00)) void loop() { unsigned char crc; unsigned char newcrc; unsigned char c; unsigned long coff; if (Serial.available()) { rx = Serial.read(); rx_state = rx_state_machine(rx_state, rx); if (rx_state == 3) { switch (rx_message[0]) { case 0x0: Serial.write(0x80); break; case 0x01: c2_init_PI(); Serial.write(0x81); digitalWrite(LED, HIGH); rx_state = 0; break; case 0x02: c2_rst(); Serial.write(0x82); digitalWrite(LED, LOW); rx_state = 0; break; case 0x03: addr = (((unsigned long)(rx_message[4]))<<8) + (((unsigned long)(rx_message[5]))<<0); crc = rx_message[6]; newcrc = rx_message[5] + rx_message[4]; for (i=0;i