diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | README.txt | 5 | ||||
| -rw-r--r-- | fpm.c | 36 | ||||
| -rw-r--r-- | fpm.h | 4 | ||||
| -rw-r--r-- | main.c | 11 |
5 files changed, 36 insertions, 22 deletions
@@ -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) @@ -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 + @@ -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; +} @@ -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 */ @@ -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); |
