diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2025-08-04 21:17:51 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2025-08-04 21:17:51 +0800 |
| commit | 26d04ee4de6c0306120c5ee1616c3e51f51328f6 (patch) | |
| tree | 8694ea73cce6a4e0ccbf26d7bdecf5f61de1d482 | |
| parent | e7f1a2fb64a3d965eb482a21a585fe6f43378d55 (diff) | |
| download | fpm-door-lock-26d04ee4de6c0306120c5ee1616c3e51f51328f6.tar.gz | |
Refactor UART code to separate file.
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | fpm.c | 17 | ||||
| -rw-r--r-- | main.c | 16 | ||||
| -rw-r--r-- | uart.c | 31 | ||||
| -rw-r--r-- | uart.h | 10 |
5 files changed, 52 insertions, 24 deletions
@@ -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 @@ -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; @@ -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); @@ -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; +} @@ -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 */ |
