From bab75a6068ab0a64fe22395ad11efafccbf0d842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sun, 11 Oct 2015 13:06:28 +0200 Subject: Implement SPI and remove files --- examples/spiblink/README.md | 18 ++++++++++++++++++ examples/spiblink/makefile | 30 ++++++++++++++++++++++++++++++ examples/spiblink/master.c | 20 ++++++++++++++++++++ examples/spiblink/slave.c | 21 +++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 examples/spiblink/README.md create mode 100644 examples/spiblink/makefile create mode 100644 examples/spiblink/master.c create mode 100644 examples/spiblink/slave.c (limited to 'examples/spiblink') diff --git a/examples/spiblink/README.md b/examples/spiblink/README.md new file mode 100644 index 0000000..3b18a8f --- /dev/null +++ b/examples/spiblink/README.md @@ -0,0 +1,18 @@ +This example shows simple SPI communication. It uses two MCUs. One is configured +as master. Other is configured as slave. Master has button connected to its input +and slave has output led. Button status is mirrored by slave led. + +Connection +---------- +Both MCUs needs basic power connections and crystal. +Except of that, they need following other connections. + +| Mater pin | Slave pin | Other connections | Description | +|-----------|-----------|-------------------|----------------------| +| PB1 | | Led | Indication of button | +| | PB1 | Led | Receive led | +| PB2 | PB2 | | SS | +| PB3 | PB3 | | MOSI | +| PB4 | PB4 | | MISO | +| PB5 | PB5 | | SCL | +| PC1 | | Pull down button | Input button | diff --git a/examples/spiblink/makefile b/examples/spiblink/makefile new file mode 100644 index 0000000..a831f86 --- /dev/null +++ b/examples/spiblink/makefile @@ -0,0 +1,30 @@ +MAKEFLAGS += --no-builtin-rules + +MMCU = atmega328p +F_CPU = 16000000L +IOE_PREFIX = ../.. +IOE_CFLAGS ?= -Os -ffunction-sections -fdata-sections -fshort-enums -g -Wall + +.PHONY: all +all: master.hex slave.hex + @echo + @echo Flash master.hex and slave.hex to two chips and... TODO + +.PHONY: all +clean: ioeclean + $(RM) master.o slave.o + $(RM) master.elf master.hex + $(RM) slave.elf slave.hex + +include ../../avr-ioe.mk + +master.elf: master.o +slave.elf: slave.o +master.elf slave.elf: %.elf: $(IOE_OBJ_SPI) + avr-gcc -Os -mmcu=$(MMCU) $^ -o $@ + +master.hex slave.hex: %.hex: %.elf + avr-objcopy -O ihex -R .eeprom $< $@ + +master.o slave.o: %.o: %.c + avr-gcc $(IOE_CFLAGS) -c -o $@ $< diff --git a/examples/spiblink/master.c b/examples/spiblink/master.c new file mode 100644 index 0000000..267d7b2 --- /dev/null +++ b/examples/spiblink/master.c @@ -0,0 +1,20 @@ +#include +#include +#include "../../spi.h" + +int main() { + DDRB |= _BV(DDB1) | _BV(DDB2); + PORTC |= _BV(PORTC1); + spi_init(SPI_MODE_MASTER); + SREG |= _BV(7); + while (1) { + if (PINC & _BV(PINC1)) { + PORTB &= ~_BV(PORTB1); + } else { + PORTB |= _BV(PORTB1); + } + PORTB &= ~_BV(PORTB2); + spi_send(!(PINC & _BV(PINC1))); + PORTB |= _BV(PORTB2); + } +} diff --git a/examples/spiblink/slave.c b/examples/spiblink/slave.c new file mode 100644 index 0000000..cf14408 --- /dev/null +++ b/examples/spiblink/slave.c @@ -0,0 +1,21 @@ +#include +#include +#include "../../spi.h" + +void receive(uint8_t data); + +int main() { + DDRB |= _BV(DDB1); + spi_receive = receive; + spi_init(SPI_MODE_SLAVE); + SREG |= _BV(7); + while (1) { + } +} + +void receive(uint8_t data) { + if (data) + PORTB |= _BV(PORTB1); + else + PORTB &= ~_BV(PORTB1); +} -- cgit v1.2.3