From 52080fed19d6cbc2461e3c74d63f4408ea70b590 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Tue, 6 May 2025 07:14:22 +0800 Subject: Remove soft uart from code and reorganize files. --- fpm.c | 269 --------------------------------------------------------------- fpm.h | 30 ------- fpm10a.c | 269 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fpm10a.h | 30 +++++++ main.c | 7 +- r503.c | 194 +-------------------------------------------- r503.h | 8 ++ 7 files changed, 310 insertions(+), 497 deletions(-) delete mode 100644 fpm.c delete mode 100644 fpm.h create mode 100644 fpm10a.c create mode 100644 fpm10a.h create mode 100644 r503.h diff --git a/fpm.c b/fpm.c deleted file mode 100644 index 44f8b4c..0000000 --- a/fpm.c +++ /dev/null @@ -1,269 +0,0 @@ -#include -#include -#include - -#include "fpm.h" - -#define MAXPDLEN 64 -#define RST_DELAY_MS 500 - -#define OK 0x00 - -static uint8_t start_code[] = { 0xEF, 0x01 }; -static uint8_t addr[] = { 0xFF, 0xFF, 0xFF, 0xFF }; - -static inline uint8_t read(void) -{ - while (!(UCSR0A & (1 << RXC0))) - ; - return UDR0; -} - -static inline void write(uint8_t c) -{ - while (!(UCSR0A & (1 << UDRE0))) - ; - UDR0 = c; -} - -static inline void write_bulk(uint8_t *data, uint16_t n) -{ - int i; - - for (i = 0; i < n; i++) - write(data[i]); -} - -static inline void send(uint8_t pktid, uint8_t *data, uint8_t n) -{ - int i; - uint16_t pktlen, sum; - - write_bulk(start_code, 2); - write_bulk(addr, 4); - write(pktid); - - pktlen = n + 3; - write((uint8_t)(pktlen >> 8)); - write((uint8_t)pktlen); - - sum = (pktlen >> 8) + (pktlen & 0xFF) + pktid; - for (i = 0; i < n; i++) { - write(data[i]); - sum += data[i]; - } - - write((uint8_t)(sum >> 8)); - write((uint8_t)sum); -} - -static inline void recv(uint8_t buf[MAXPDLEN], uint16_t *n) -{ - int i; - uint16_t len; - uint8_t byte; - - i = 0, len = 0; - - for (;;) { - byte = read(); - switch (i) { - case 0: - if (byte != start_code[0]) - continue; - break; - case 1: - if (byte != start_code[1]) - goto bad_pkt; - break; - case 2: - case 3: - case 4: - case 5: - // toss the address - break; - case 6: - // toss the packet id - break; - case 7: - len = (uint16_t)byte << 8; - break; - case 8: - len |= byte; - break; - default: - if ((i - 9) < MAXPDLEN) { - buf[i - 9] = byte; - if ((i - 8) == len) { - *n = len; - return; - } - } else { - goto bad_pkt; - } - break; - } - i++; - } - -bad_pkt: - *n = 0; - return; -} - -static inline uint8_t check_pwd(void) -{ - unsigned int n; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x13; - 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(0x01, buf, 5); - recv(buf, &n); - return buf[0] == OK; -} - -static inline void uint8_t aura_on(void) -{ - unsigned int n; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x35; - buf[1] = 0x01; - buf[2] = 0x00; - buf[3] = 0x01; - buf[4] = 0x00; - - send(0x01, buf, 5); - recv(buf, &n); - return buf[0] == OK; -} - -uint8_t fpm_init(void) -{ - UBRR0H = UBRRH_VALUE; - UBRR0L = UBRRL_VALUE; -#if USE_2X - UCSR0A |= (1 << U2X0); -#else - UCSR0A &= ~(1 << U2X0); -#endif - UCSR0B = (1 << TXEN0) | (1 << RXEN0); - UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); - - _delay_ms(RST_DELAY_MS); - aura_on(); - return check_pwd(); -} - -uint8_t fpm_getcfg(struct fpm_cfg *cfg) -{ - uint16_t n; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x0F; - send(0x01, buf, 1); - recv(buf, &n); - - if (buf[0] == OK && n >= 17) { - cfg->status = ((uint16_t)buf[1] << 8) | buf[2]; - cfg->sysid = ((uint16_t)buf[3] << 8) | buf[4]; - cfg->cap = ((uint16_t)buf[5] << 8) | buf[6]; - cfg->sec_level = ((uint16_t)buf[7] << 8) | buf[8]; - cfg->addr[0] = buf[9]; - cfg->addr[1] = buf[10]; - cfg->addr[2] = buf[11]; - cfg->addr[3] = buf[12]; - cfg->pkt_size = ((uint16_t)buf[13] << 8) | buf[14]; - cfg->pkt_size = 1 << (cfg->pkt_size + 5); - cfg->baud = (((uint16_t)buf[15] << 8) | buf[16]); - - return 1; - } - return 0; -} - -uint8_t fpm_setpwd(uint32_t pwd) -{ - uint16_t n; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x12; - buf[1] = (uint8_t)(pwd >> 24); - buf[2] = (uint8_t)(pwd >> 16); - buf[3] = (uint8_t)(pwd >> 8); - buf[4] = (uint8_t)(pwd & 0xFF); - - send(0x01, buf, 5); - recv(buf, &n); - return buf[0] == OK; -} - -uint16_t fpm_getcount(void) -{ - uint16_t n, count; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x1D; - send(0x01, buf, 1); - recv(buf, &n); - - count = 0; - if (buf[0] == OK && n >= 2) { - count = buf[1]; - count <<= 8; - count |= buf[2]; - } - return count; -} - -uint8_t fpm_enroll(void) -{ - uint16_t n, retries; - uint8_t buf[MAXPDLEN]; - - retries = 0; - - do { - _delay_ms(100); - buf[0] = 0x10; - send(0x01, buf, 1); - recv(buf, &n); - retries++; - } while (buf[0] != OK && retries < 50); - - return buf[0] == OK; -} - -uint8_t fpm_match(void) -{ - uint16_t n, retries; - uint8_t buf[MAXPDLEN]; - - retries = 0; - - do { - _delay_ms(100); - buf[0] = 0x11; - send(0x01, buf, 1); - recv(buf, &n); - retries++; - } while (buf[0] != OK && retries < 10); - - return buf[0] == OK; -} - -uint8_t fpm_delete_all(void) -{ - uint16_t n; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x0D; - send(0x01, buf, 1); - recv(buf, &n); - return buf[0] == OK; -} diff --git a/fpm.h b/fpm.h deleted file mode 100644 index f165958..0000000 --- a/fpm.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef FPM_H -#define FPM_H - -#include - -struct fpm_cfg { - uint16_t status; - uint16_t sysid; - uint16_t cap; - uint16_t sec_level; - uint8_t addr[4]; - uint16_t pkt_size; - uint16_t baud; -}; - -uint8_t fpm_init(void); - -uint8_t fpm_getcfg(struct fpm_cfg *cfg); - -uint8_t fpm_setpwd(uint32_t pwd); - -uint16_t fpm_getcount(void); - -uint8_t fpm_enroll(void); - -uint8_t fpm_match(void); - -uint8_t fpm_delete_all(void); - -#endif /* FPM_H */ diff --git a/fpm10a.c b/fpm10a.c new file mode 100644 index 0000000..44f8b4c --- /dev/null +++ b/fpm10a.c @@ -0,0 +1,269 @@ +#include +#include +#include + +#include "fpm.h" + +#define MAXPDLEN 64 +#define RST_DELAY_MS 500 + +#define OK 0x00 + +static uint8_t start_code[] = { 0xEF, 0x01 }; +static uint8_t addr[] = { 0xFF, 0xFF, 0xFF, 0xFF }; + +static inline uint8_t read(void) +{ + while (!(UCSR0A & (1 << RXC0))) + ; + return UDR0; +} + +static inline void write(uint8_t c) +{ + while (!(UCSR0A & (1 << UDRE0))) + ; + UDR0 = c; +} + +static inline void write_bulk(uint8_t *data, uint16_t n) +{ + int i; + + for (i = 0; i < n; i++) + write(data[i]); +} + +static inline void send(uint8_t pktid, uint8_t *data, uint8_t n) +{ + int i; + uint16_t pktlen, sum; + + write_bulk(start_code, 2); + write_bulk(addr, 4); + write(pktid); + + pktlen = n + 3; + write((uint8_t)(pktlen >> 8)); + write((uint8_t)pktlen); + + sum = (pktlen >> 8) + (pktlen & 0xFF) + pktid; + for (i = 0; i < n; i++) { + write(data[i]); + sum += data[i]; + } + + write((uint8_t)(sum >> 8)); + write((uint8_t)sum); +} + +static inline void recv(uint8_t buf[MAXPDLEN], uint16_t *n) +{ + int i; + uint16_t len; + uint8_t byte; + + i = 0, len = 0; + + for (;;) { + byte = read(); + switch (i) { + case 0: + if (byte != start_code[0]) + continue; + break; + case 1: + if (byte != start_code[1]) + goto bad_pkt; + break; + case 2: + case 3: + case 4: + case 5: + // toss the address + break; + case 6: + // toss the packet id + break; + case 7: + len = (uint16_t)byte << 8; + break; + case 8: + len |= byte; + break; + default: + if ((i - 9) < MAXPDLEN) { + buf[i - 9] = byte; + if ((i - 8) == len) { + *n = len; + return; + } + } else { + goto bad_pkt; + } + break; + } + i++; + } + +bad_pkt: + *n = 0; + return; +} + +static inline uint8_t check_pwd(void) +{ + unsigned int n; + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x13; + 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(0x01, buf, 5); + recv(buf, &n); + return buf[0] == OK; +} + +static inline void uint8_t aura_on(void) +{ + unsigned int n; + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x35; + buf[1] = 0x01; + buf[2] = 0x00; + buf[3] = 0x01; + buf[4] = 0x00; + + send(0x01, buf, 5); + recv(buf, &n); + return buf[0] == OK; +} + +uint8_t fpm_init(void) +{ + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; +#if USE_2X + UCSR0A |= (1 << U2X0); +#else + UCSR0A &= ~(1 << U2X0); +#endif + UCSR0B = (1 << TXEN0) | (1 << RXEN0); + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); + + _delay_ms(RST_DELAY_MS); + aura_on(); + return check_pwd(); +} + +uint8_t fpm_getcfg(struct fpm_cfg *cfg) +{ + uint16_t n; + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x0F; + send(0x01, buf, 1); + recv(buf, &n); + + if (buf[0] == OK && n >= 17) { + cfg->status = ((uint16_t)buf[1] << 8) | buf[2]; + cfg->sysid = ((uint16_t)buf[3] << 8) | buf[4]; + cfg->cap = ((uint16_t)buf[5] << 8) | buf[6]; + cfg->sec_level = ((uint16_t)buf[7] << 8) | buf[8]; + cfg->addr[0] = buf[9]; + cfg->addr[1] = buf[10]; + cfg->addr[2] = buf[11]; + cfg->addr[3] = buf[12]; + cfg->pkt_size = ((uint16_t)buf[13] << 8) | buf[14]; + cfg->pkt_size = 1 << (cfg->pkt_size + 5); + cfg->baud = (((uint16_t)buf[15] << 8) | buf[16]); + + return 1; + } + return 0; +} + +uint8_t fpm_setpwd(uint32_t pwd) +{ + uint16_t n; + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x12; + buf[1] = (uint8_t)(pwd >> 24); + buf[2] = (uint8_t)(pwd >> 16); + buf[3] = (uint8_t)(pwd >> 8); + buf[4] = (uint8_t)(pwd & 0xFF); + + send(0x01, buf, 5); + recv(buf, &n); + return buf[0] == OK; +} + +uint16_t fpm_getcount(void) +{ + uint16_t n, count; + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x1D; + send(0x01, buf, 1); + recv(buf, &n); + + count = 0; + if (buf[0] == OK && n >= 2) { + count = buf[1]; + count <<= 8; + count |= buf[2]; + } + return count; +} + +uint8_t fpm_enroll(void) +{ + uint16_t n, retries; + uint8_t buf[MAXPDLEN]; + + retries = 0; + + do { + _delay_ms(100); + buf[0] = 0x10; + send(0x01, buf, 1); + recv(buf, &n); + retries++; + } while (buf[0] != OK && retries < 50); + + return buf[0] == OK; +} + +uint8_t fpm_match(void) +{ + uint16_t n, retries; + uint8_t buf[MAXPDLEN]; + + retries = 0; + + do { + _delay_ms(100); + buf[0] = 0x11; + send(0x01, buf, 1); + recv(buf, &n); + retries++; + } while (buf[0] != OK && retries < 10); + + return buf[0] == OK; +} + +uint8_t fpm_delete_all(void) +{ + uint16_t n; + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x0D; + send(0x01, buf, 1); + recv(buf, &n); + return buf[0] == OK; +} diff --git a/fpm10a.h b/fpm10a.h new file mode 100644 index 0000000..f165958 --- /dev/null +++ b/fpm10a.h @@ -0,0 +1,30 @@ +#ifndef FPM_H +#define FPM_H + +#include + +struct fpm_cfg { + uint16_t status; + uint16_t sysid; + uint16_t cap; + uint16_t sec_level; + uint8_t addr[4]; + uint16_t pkt_size; + uint16_t baud; +}; + +uint8_t fpm_init(void); + +uint8_t fpm_getcfg(struct fpm_cfg *cfg); + +uint8_t fpm_setpwd(uint32_t pwd); + +uint16_t fpm_getcount(void); + +uint8_t fpm_enroll(void); + +uint8_t fpm_match(void); + +uint8_t fpm_delete_all(void); + +#endif /* FPM_H */ diff --git a/main.c b/main.c index 4f23d57..a7d9df2 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,4 @@ -/* - * example_usage.c - * - */ - -#include "fpm.h" +#include "r503.h" int main(void) { diff --git a/r503.c b/r503.c index 0d34812..3b3ac7c 100644 --- a/r503.c +++ b/r503.c @@ -1,26 +1,8 @@ - -#define bit_clr(a,b) ((a) &=~(1<<(b))) -#define bit_set(a,b) ((a) |= (1<<(b))) -#define bit_tst(a,b) ((a) & (1<<(b))) -#define bit_change(a,b) ((a) ^= (1<<(b))) - -#include - -#include -#include -#include - -#define Soft_UART_TX_PORT PORTD -#define Soft_UART_TX_DDR DDRD -#define Soft_UART_TX_PIN 3 -#define Soft_UART_Baud 9600 -#include "Soft_UART_Timer1.h" - #include #include #include -#include "fpm.h" +#include "r503.h" #define MAXPDLEN 64 #define RST_DELAY_MS 500 @@ -31,16 +13,6 @@ #define OK 0x00 -static inline void uart_write(const char *s) -{ - for (; *s; s++) { - Soft_UART_send_byte(*s); - } - Soft_UART_send_byte('\r'); - Soft_UART_send_byte('\n'); -} - - static inline uint8_t read(void) { while (!(UCSR0A & (1 << RXC0))) @@ -162,7 +134,7 @@ static inline uint8_t aura_on(void) buf[0] = 0x35; buf[1] = 0x01; buf[2] = 0x20; - buf[3] = 0x01; + buf[3] = 0x03; buf[4] = 0x00; send(0x01, buf, 5); @@ -172,11 +144,6 @@ static inline uint8_t aura_on(void) uint8_t fpm_init(void) { - cli(); - Soft_UART_init(); - bit_set(DDRB,5); - sei(); - UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; #if USE_2X @@ -192,160 +159,3 @@ uint8_t fpm_init(void) return check_pwd(); } -uint8_t fpm_getcfg(struct fpm_cfg *cfg) -{ - uint16_t n; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x0F; - send(0x01, buf, 1); - recv(buf, &n); - - if (buf[0] == OK && n >= 17) { - cfg->status = ((uint16_t)buf[1] << 8) | buf[2]; - cfg->sysid = ((uint16_t)buf[3] << 8) | buf[4]; - cfg->cap = ((uint16_t)buf[5] << 8) | buf[6]; - cfg->sec_level = ((uint16_t)buf[7] << 8) | buf[8]; - cfg->addr[0] = buf[9]; - cfg->addr[1] = buf[10]; - cfg->addr[2] = buf[11]; - cfg->addr[3] = buf[12]; - cfg->pkt_size = ((uint16_t)buf[13] << 8) | buf[14]; - cfg->pkt_size = 1 << (cfg->pkt_size + 5); - cfg->baud = (((uint16_t)buf[15] << 8) | buf[16]); - - return 1; - } - return 0; -} - -uint8_t fpm_setpwd(uint32_t pwd) -{ - uint16_t n; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x12; - buf[1] = (uint8_t)(pwd >> 24); - buf[2] = (uint8_t)(pwd >> 16); - buf[3] = (uint8_t)(pwd >> 8); - buf[4] = (uint8_t)(pwd & 0xFF); - - send(0x01, buf, 5); - recv(buf, &n); - return buf[0] == OK; -} - -uint16_t fpm_getcount(void) -{ - uint16_t n, count; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x1D; - send(0x01, buf, 1); - recv(buf, &n); - - count = 0; - if (buf[0] == OK && n >= 2) { - count = buf[1]; - count <<= 8; - count |= buf[2]; - } - return count; -} - -uint8_t fpm_enroll(void) -{ - uint16_t n, retries; - uint8_t buf[MAXPDLEN]; - - retries = 0; - - do { - _delay_ms(100); - buf[0] = 0x10; - send(0x01, buf, 1); - recv(buf, &n); - retries++; - } while (buf[0] != OK && retries < 50); - - return buf[0] == OK; -} - -uint8_t fpm_match(void) -{ - uint16_t n, retries; - uint8_t buf[MAXPDLEN]; - - retries = 0; - - do { - _delay_ms(100); - buf[0] = 0x11; - send(0x01, buf, 1); - recv(buf, &n); - retries++; - } while (buf[0] != OK && retries < 10); - - return buf[0] == OK; -} - -uint8_t fpm_delete_all(void) -{ - uint16_t n; - uint8_t buf[MAXPDLEN]; - - buf[0] = 0x0D; - send(0x01, buf, 1); - recv(buf, &n); - return buf[0] == OK; -} - -static inline void print_config(void) -{ - const int SLEN = 25; - - char s[SLEN]; - struct fpm_cfg cfg; - - if (fpm_getcfg(&cfg)) { - uart_write("FPM config:"); - snprintf(s, SLEN, "\tstatus: 0x%02X", cfg.status); - uart_write(s); - snprintf(s, SLEN, "\tsysid: 0x%02X", cfg.sysid); - uart_write(s); - snprintf(s, SLEN, "\tcap: %d", cfg.cap); - uart_write(s); - snprintf(s, SLEN, "\tsec: %d", cfg.sec_level); - uart_write(s); - snprintf(s, SLEN, "\taddr: 0x%02X%02X%02X%02X", cfg.addr[0], - cfg.addr[1], cfg.addr[2], cfg.addr[3]); - uart_write(s); - snprintf(s, SLEN, "\tpkt size: %d", cfg.pkt_size); - uart_write(s); - - if (cfg.baud == 1) - uart_write("\tbaud: 9600"); - else if (cfg.baud == 2) - uart_write("\tbaud: 19200"); - else if (cfg.baud == 3) - uart_write("\tbaud: 28800"); - else if (cfg.baud == 4) - uart_write("\tbaud: 38400"); - else if (cfg.baud == 5) - uart_write("\tbaud: 48000"); - else if (cfg.baud == 6) - uart_write("\tbaud: 57600"); - else if (cfg.baud == 7) - uart_write("\tbaud: 67200"); - else if (cfg.baud == 8) - uart_write("\tbaud: 76800"); - else if (cfg.baud == 9) - uart_write("\tbaud: 86400"); - else if (cfg.baud == 10) - uart_write("\tbaud: 96000"); - else if (cfg.baud == 11) - uart_write("\tbaud: 105600"); - else if (cfg.baud == 12) - uart_write("\tbaud: 115200"); - } -} diff --git a/r503.h b/r503.h new file mode 100644 index 0000000..980fb9c --- /dev/null +++ b/r503.h @@ -0,0 +1,8 @@ +#ifndef FPM_R503_H +#define FPM_R503_H + +#include + +uint8_t fpm_init(void); + +#endif /* FPM_R50_H */ -- cgit v1.2.3