From 295f799ec9b7167063e10ca78caf2ceca6227040 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sat, 3 May 2025 08:41:02 +0800 Subject: Update password. --- Makefile | 2 ++ README.txt | 5 +++++ fpm.c | 36 ++++++++++++++++++++++-------------- fpm.h | 4 +++- main.c | 11 ++++------- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 42cc0aa..41ebd02 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ CC = avr-gcc MCU = atmega328p PORT = /dev/cuaU0 TARGET = fend +FPM_NEWPWD = 0x00 SRC = main.c fpm.c OBJ = $(SRC:.c=.o) @@ -13,6 +14,7 @@ CFLAGS += -mmcu=$(MCU) CFLAGS += -DBAUD=57600 CFLAGS += -DF_CPU=16000000UL CFLAGS += -DDEBUG=0 +CFLAGS += -DFPM_PWD=$(FPM_PWD) CFLAGS += -ffunction-sections -fdata-sections LDFLAGS = -mmcu=$(MCU) diff --git a/README.txt b/README.txt index 2f50da9..b951c18 100644 --- a/README.txt +++ b/README.txt @@ -8,3 +8,8 @@ Since ATmega328P has only one hardware UART, I'm using lynxzp's software UART implementation [1] for debugging purposes. [1] https://github.com/lynxzp/Software_UART_for_AVR + +BUILD + +make -FPM_PWD=$(pasword) hex + diff --git a/fpm.c b/fpm.c index de48782..41a106c 100644 --- a/fpm.c +++ b/fpm.c @@ -7,10 +7,6 @@ #define MAXPDLEN 64 #define RST_DELAY_MS 500 -#define PWD 0x00 - -#define CMD 0x01 - #define OK 0x00 static uint8_t start_code[] = { 0xEF, 0x01 }; @@ -121,12 +117,12 @@ static inline uint8_t check_pwd(void) uint8_t buf[MAXPDLEN]; buf[0] = 0x13; - buf[1] = (uint8_t)((uint32_t)PWD >> 24); - buf[2] = (uint8_t)((uint32_t)PWD >> 16); - buf[3] = (uint8_t)((uint32_t)PWD >> 8); - buf[4] = (uint8_t)((uint32_t)PWD & 0xFF); + buf[1] = (uint8_t)((uint32_t)FPM_PWD >> 24); + buf[2] = (uint8_t)((uint32_t)FPM_PWD >> 16); + buf[3] = (uint8_t)((uint32_t)FPM_PWD >> 8); + buf[4] = (uint8_t)((uint32_t)FPM_PWD & 0xFF); - send(CMD, buf, 5); + send(0x01, buf, 5); n = 0; recv(buf, &n); @@ -146,19 +142,16 @@ uint8_t fpm_init(void) UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); _delay_ms(RST_DELAY_MS); - return check_pwd(); } -uint8_t fpm_get_config(struct fpm_config *cfg) +uint8_t fpm_getcfg(struct fpm_config *cfg) { uint16_t n; uint8_t buf[MAXPDLEN]; buf[0] = 0x0F; - send(CMD, buf, 1); - - n = 0; + send(0x01, buf, 1); recv(buf, &n); if (buf[0] == OK && n >= 17) { @@ -179,3 +172,18 @@ uint8_t fpm_get_config(struct fpm_config *cfg) return 0; } +uint8_t fpm_setpwd(uint32_t pwd) +{ + uint16_t n; + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x12; + buf[1] = (uint8_t)((uint32_t)pwd >> 24); + buf[2] = (uint8_t)((uint32_t)pwd >> 16); + buf[3] = (uint8_t)((uint32_t)pwd >> 8); + buf[4] = (uint8_t)((uint32_t)pwd & 0xFF); + + send(0x01, buf, 5); + recv(buf, &n); + return buf[0] == OK; +} diff --git a/fpm.h b/fpm.h index 0ea98df..fd3b05c 100644 --- a/fpm.h +++ b/fpm.h @@ -15,6 +15,8 @@ struct fpm_config { uint8_t fpm_init(void); -uint8_t fpm_get_config(struct fpm_config *cfg); +uint8_t fpm_getcfg(struct fpm_config *cfg); + +uint8_t fpm_setpwd(); #endif /* FPM_H */ diff --git a/main.c b/main.c index 6f351f4..37ef329 100644 --- a/main.c +++ b/main.c @@ -38,7 +38,7 @@ static inline void print_config(void) char s[SLEN]; struct fpm_config cfg; - if (fpm_get_config(&cfg)) { + if (fpm_getcfg(&cfg)) { uart_write("FPM config:"); snprintf(s, SLEN, "\tstatus: 0x%02X", cfg.status); uart_write(s); @@ -78,6 +78,8 @@ static inline void print_config(void) uart_write("\tbaud: 105600"); else if (cfg.baud == 12) uart_write("\tbaud: 115200"); + } else { + uart_write("Valid FPM config not found"); } } @@ -88,14 +90,9 @@ int main(void) bit_set(DDRB,5); sei(); - if (fpm_init()) { - uart_write("FPM detected"); + if (fpm_init()) print_config(); - } - else - uart_write("FPM not detected"); - while (1) { _delay_ms(1000); -- cgit v1.2.3