summaryrefslogtreecommitdiffstats
path: root/rf_test
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2024-11-23 15:14:01 +0800
committerSadeep Madurange <sadeep@asciimx.com>2024-11-23 15:14:01 +0800
commit96631bbf2575ad3fa28d3ecb7fcd970accd07615 (patch)
tree8e3cb7e1221fbc0b43238764f420f90799ce4a17 /rf_test
parent6b31b3f77b05286ca56590dde14717541f4cb5ba (diff)
downloadsmart-home-96631bbf2575ad3fa28d3ecb7fcd970accd07615.tar.gz
Basic SPI functions for atmega328p.
Diffstat (limited to 'rf_test')
-rw-r--r--rf_test/Recv.Makefile1
-rw-r--r--rf_test/Send.Makefile1
-rw-r--r--rf_test/send.c45
3 files changed, 17 insertions, 30 deletions
diff --git a/rf_test/Recv.Makefile b/rf_test/Recv.Makefile
index cd5b4ff..535b2c0 100644
--- a/rf_test/Recv.Makefile
+++ b/rf_test/Recv.Makefile
@@ -9,7 +9,6 @@ CFLAGS = -std=gnu99
CFLAGS += -Os
CFLAGS += -Wall
CFLAGS += -mmcu=$(MCU)
-CFLAGS += -DBAUD=2400
CFLAGS += -DF_CPU=16000000UL
CFLAGS += -ffunction-sections -fdata-sections
diff --git a/rf_test/Send.Makefile b/rf_test/Send.Makefile
index 397a9db..a9aaeb0 100644
--- a/rf_test/Send.Makefile
+++ b/rf_test/Send.Makefile
@@ -9,7 +9,6 @@ CFLAGS = -std=gnu99
CFLAGS += -Os
CFLAGS += -Wall
CFLAGS += -mmcu=$(MCU)
-CFLAGS += -DBAUD=2400
CFLAGS += -DF_CPU=16000000UL
CFLAGS += -ffunction-sections -fdata-sections
diff --git a/rf_test/send.c b/rf_test/send.c
index 3ea0719..370a6f4 100644
--- a/rf_test/send.c
+++ b/rf_test/send.c
@@ -2,7 +2,6 @@
#include <avr/interrupt.h>
#include <util/delay.h>
-#define LOCK_LED PB5
#define LOCK_BTN PD6
#define UNLOCK_BTN PD7
@@ -10,36 +9,29 @@
#define LOCK 0xB5
#define UNLOCK 0xAE
-#define SIGPIN PD2
-#define SIGLEN 500
-
-static inline void send(unsigned char c)
+static inline void spi_init(void)
{
- int n;
-
- for (n = 7; n >= 0; n--) {
- PORTD = ((c >> n) & 1) == 1
- ? PORTD | (1 << SIGPIN)
- : PORTD & ~(1 << SIGPIN);
-
- _delay_us(SIGLEN);
- }
+ DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK);
+ SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
+}
- PORTD &= ~(1 << SIGPIN);
+static inline void spi_send(unsigned char data)
+{
+ SPRD = data;
+ while (!(SPSR & (1 << SPIF)))
+ ;
}
static inline void lock(void)
{
- send(SYN);
- send(LOCK);
- PORTB |= (1 << LOCK_LED);
+ spi_send(SYN);
+ spi_send(LOCK);
}
static inline void unlock(void)
{
- send(SYN);
- send(UNLOCK);
- PORTB &= ~(1 << LOCK_LED);
+ spi_send(SYN);
+ spi_send(UNLOCK);
}
static inline int is_btn_pressed(unsigned char btn)
@@ -60,19 +52,16 @@ static inline void pcint2_init(void)
int main(void)
{
- DDRB |= (1 << LOCK_LED);
-
DDRD &= ~((1 << LOCK_BTN) | (1 << UNLOCK_BTN));
PORTD |= (1 << LOCK_BTN) | (1 << UNLOCK_BTN);
- DDRD |= (1 << SIGPIN);
- PORTD &= ~(1 << SIGPIN);
-
+ spi_init();
pcint2_init();
+
sei();
- for (;;) {
- }
+ for (;;)
+ ;
return 0;
}