diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2025-04-29 17:21:31 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2025-04-29 17:21:31 +0800 |
| commit | 0398894a39406ead5921df64892e3a1c82cc25b7 (patch) | |
| tree | 58425972a8ed8c72b268c41ce75a23b4e8e1648f /sleep | |
| parent | a9ce750c420813b1e7dab5eb4e0340d107a34b20 (diff) | |
| download | smart-home-0398894a39406ead5921df64892e3a1c82cc25b7.tar.gz | |
Sleep test.
Diffstat (limited to 'sleep')
| -rw-r--r-- | sleep/Makefile | 44 | ||||
| -rw-r--r-- | sleep/main.c | 57 | ||||
| -rw-r--r-- | sleep/uart.c | 37 | ||||
| -rw-r--r-- | sleep/uart.h | 8 |
4 files changed, 146 insertions, 0 deletions
diff --git a/sleep/Makefile b/sleep/Makefile new file mode 100644 index 0000000..c818369 --- /dev/null +++ b/sleep/Makefile @@ -0,0 +1,44 @@ +CC = avr-gcc +MCU = atmega328p +PORT = /dev/cuaU0 +TARGET = sleep + +SRC = main.c uart.c +OBJ = $(SRC:.c=.o) + +CFLAGS = -std=gnu99 +CFLAGS += -Os +CFLAGS += -Wall +CFLAGS += -mmcu=$(MCU) +CFLAGS += -DBAUD=115200 +CFLAGS += -DF_CPU=16000000UL +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS = -mmcu=$(MCU) +LDFLAGS += -Wl,--gc-sections + +HEX_FLAGS = -O ihex +HEX_FLAGS += -j .text -j .data + +AVRDUDE_FLAGS = -p $(MCU) +AVRDUDE_FLAGS += -c arduino +AVRDUDE_FLAGS += -P $(PORT) +AVRDUDE_FLAGS += -D -U + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +elf: $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf + +hex: elf + avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex + +upload: hex + avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i + +.PHONY: clean + +clean: + rm *.o *.elf *.hex + diff --git a/sleep/main.c b/sleep/main.c new file mode 100644 index 0000000..007dc18 --- /dev/null +++ b/sleep/main.c @@ -0,0 +1,57 @@ +#include <avr/interrupt.h> +#include <avr/sleep.h> +#include <util/delay.h> + +#include "uart.h" + +#define LOCK_PIN PD2 +#define UNLOCK_PIN PD3 + +static inline void init_btns(void) +{ + DDRD &= ~((1 << LOCK_PIN) | (1 << UNLOCK_PIN)); + PORTD |= ((1 << LOCK_PIN) | (1 << UNLOCK_PIN)); + EICRA = 0b00000000; + EIMSK = (1 << INT0) | (1 << INT1); +} + +int main(void) +{ + init_btns(); + uart_init(); + + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sei(); + + for (;;) { + uart_write_line("Going to sleep"); + sleep_enable(); + sleep_bod_disable(); + sleep_cpu(); + sleep_disable(); + uart_write_line("It's alive!"); + _delay_ms(1500); + } + return 0; +} + +static inline int is_btn_pressed(uint8_t pin, uint8_t btn) +{ + if (!((pin >> btn) & 0x01)) { + _delay_ms(100); + return !((pin >> btn) & 0x01); + } + return 0; +} + +ISR(INT0_vect) +{ + if (is_btn_pressed(PIND, LOCK_PIN)) + uart_write_line("Locked"); +} + +ISR(INT1_vect) +{ + if (is_btn_pressed(PIND, UNLOCK_PIN)) + uart_write_line("Unlocked"); +} diff --git a/sleep/uart.c b/sleep/uart.c new file mode 100644 index 0000000..a6d6674 --- /dev/null +++ b/sleep/uart.c @@ -0,0 +1,37 @@ +#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); +} + +static inline void uart_write_char(char c) +{ + while (!(UCSR0A & (1 << UDRE0))) + ; + UDR0 = c; +} + +void uart_write(const char *s) +{ + for (; *s; s++) + uart_write_char(*s); +} + +void uart_write_line(const char *s) +{ + uart_write(s); + uart_write_char('\r'); + uart_write_char('\n'); +} diff --git a/sleep/uart.h b/sleep/uart.h new file mode 100644 index 0000000..a88a3c6 --- /dev/null +++ b/sleep/uart.h @@ -0,0 +1,8 @@ +#ifndef UART_H +#define UART_H + +void uart_init(void); +void uart_write(const char *s); +void uart_write_line(const char *s); + +#endif /* UART_H */ |
