summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-08-04 21:17:51 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-08-04 21:17:51 +0800
commit26d04ee4de6c0306120c5ee1616c3e51f51328f6 (patch)
tree8694ea73cce6a4e0ccbf26d7bdecf5f61de1d482
parente7f1a2fb64a3d965eb482a21a585fe6f43378d55 (diff)
downloadfpm-door-lock-26d04ee4de6c0306120c5ee1616c3e51f51328f6.tar.gz
Refactor UART code to separate file.
-rw-r--r--Makefile2
-rw-r--r--fpm.c17
-rw-r--r--main.c16
-rw-r--r--uart.c31
-rw-r--r--uart.h10
5 files changed, 52 insertions, 24 deletions
diff --git a/Makefile b/Makefile
index 8f22e14..ee25780 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ MCU = atmega328p
PORT = /dev/cuaU0
TARGET = app
-SRC = main.c fpm.c
+SRC = main.c uart.c fpm.c
OBJ = $(SRC:.c=.o)
CFLAGS = -std=gnu99
diff --git a/fpm.c b/fpm.c
index 6599996..e85ee94 100644
--- a/fpm.c
+++ b/fpm.c
@@ -1,8 +1,7 @@
-#include <avr/io.h>
#include <util/delay.h>
-#include <util/setbaud.h>
#include "fpm.h"
+#include "uart.h"
#define MAXPDLEN 64
@@ -15,16 +14,12 @@
static inline uint8_t read(void)
{
- while (!(UCSR0A & (1 << RXC0)))
- ;
- return UDR0;
+ return uart_recv();
}
static inline void write(uint8_t c)
{
- while (!(UCSR0A & (1 << UDRE0)))
- ;
- UDR0 = c;
+ uart_send(c);
}
static inline void send(uint8_t *data, uint8_t n)
@@ -167,13 +162,13 @@ static inline uint8_t img2tz(uint8_t bufid)
uint8_t fpm_init(void)
{
- uint8_t rc;
+ uint8_t rc, wt = 100;
uint16_t dt = 0;
do {
rc = check_pwd();
- _delay_ms(100);
- dt += 100;
+ dt += wt;
+ _delay_ms(wt);
} while (rc == 0 && dt < 500);
return rc;
diff --git a/main.c b/main.c
index 3af81c5..9c43e20 100644
--- a/main.c
+++ b/main.c
@@ -5,6 +5,7 @@
#include <util/delay.h>
#include "fpm.h"
+#include "uart.h"
#define SERVO_PIN PB1
#define SERVO_DDR DDRB
@@ -95,21 +96,12 @@ int main(void)
WDTCSR |= (1 << WDCE) | (1 << WDE);
WDTCSR = 0x00;
- /* power on peripherals */
+ uart_init();
+
+ /* power on the FPM and the servo */
PWR_DDR |= (1 << PWR_FPM) | (1 << PWR_SERVO);
pwr_fpm_servo_on();
- /* UART for FPM */
- 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);
-
/* servo */
TCCR1A |= (1 << WGM11);
TCCR1B |= (1 << WGM12) | (1 << WGM13);
diff --git a/uart.c b/uart.c
new file mode 100644
index 0000000..1a9956d
--- /dev/null
+++ b/uart.c
@@ -0,0 +1,31 @@
+#include <avr/io.h>
+#include <util/setbaud.h>
+
+#include "uart.h"
+
+void uart_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);
+}
+
+uint8_t uart_recv(void)
+{
+ while (!(UCSR0A & (1 << RXC0)))
+ ;
+ return UDR0;
+}
+
+void uart_send(uint8_t c)
+{
+ while (!(UCSR0A & (1 << UDRE0)))
+ ;
+ UDR0 = c;
+}
diff --git a/uart.h b/uart.h
new file mode 100644
index 0000000..c5fc87e
--- /dev/null
+++ b/uart.h
@@ -0,0 +1,10 @@
+#ifndef UART_H
+#define UART_H
+
+void uart_init(void);
+
+uint8_t uart_recv(void);
+
+void uart_send(uint8_t c);
+
+#endif /* UART_H */