diff options
author | Sergey Nazaryev <sergey@nazaryev.ru> | 2017-02-26 18:27:07 +0000 |
---|---|---|
committer | Sergey Nazaryev <sergey@nazaryev.ru> | 2017-02-26 18:29:11 +0000 |
commit | b9fa2340cf1f34e151c4757a09b7f45c959beef1 (patch) | |
tree | f3f0bcafc8248f15121ad71fa31914c48981d4e4 | |
parent | 2f4cd69e4d68a043f61ba0dc93f9412b7bdc58ca (diff) | |
download | vkr-mesh-rpi-edition.zip vkr-mesh-rpi-edition.tar.gz vkr-mesh-rpi-edition.tar.bz2 |
RPi supportrpi-edition
-rw-r--r-- | client/Makefile | 4 | ||||
-rw-r--r-- | client/client.c | 4 | ||||
-rw-r--r-- | nrf24l01.h | 32 | ||||
-rw-r--r-- | server/Makefile | 4 | ||||
-rw-r--r-- | server/server.c | 4 | ||||
-rw-r--r-- | spi.c | 111 | ||||
-rw-r--r-- | spi.h | 18 | ||||
-rw-r--r-- | util.h | 5 |
8 files changed, 119 insertions, 63 deletions
diff --git a/client/Makefile b/client/Makefile index 4b6afb2..a7d00cf 100644 --- a/client/Makefile +++ b/client/Makefile @@ -3,8 +3,8 @@ PROGRAM = nrf24l01-client SRC = client.c ../spi.c OBJ = $(SRC:.c=.o) -CFLAGS ?= -Wall -Wpedantic -Werror -I../ -mmcu=atmega328p -LDFLAGS ?= -mmcu=atmega328p +CFLAGS ?= -Wall -Wpedantic -Werror -I../ +LDFLAGS ?= CROSS_COMPILE ?= avr- CC = $(CROSS_COMPILE)gcc diff --git a/client/client.c b/client/client.c index a6d51bd..d73c315 100644 --- a/client/client.c +++ b/client/client.c @@ -1,7 +1,5 @@ -#define F_CPU 1000000UL - -#include <util/delay.h> #include <nrf24l01.h> +#include "util.h" int main(void) { @@ -2,14 +2,9 @@ #define __NRF24L01__H #include <unistd.h> -#include <avr/io.h> -#include <util/delay.h> #include "spi.h" - -#define PORT_NRF24L01 PORTB -#define DDR_NRF24L01 DDRB -#define DD_CE DDB1 -#define DD_IRQ DDB0 +#include "util.h" +#include <stdio.h> #define NRF24L01_CMD_R_REGISTER 0x00 #define NRF24L01_CMD_W_REGISTER 0x20 @@ -32,14 +27,33 @@ #define MAX_PAYLOAD_IN_BYTES 1 +static inline void gpio_set(uint8_t port, uint8_t value) +{ + FILE *f; + f = fopen("/sys/class/gpio/export", "w"); + fprintf(f, "%d\n", port); + fclose(f); + char file[128]; + sprintf(file, "/sys/class/gpio/gpio%d/direction", port); + f = fopen(file, "w"); + fprintf(f, "out\n"); + fclose(f); + sprintf(file, "/sys/class/gpio/gpio%d/value", port); + f = fopen(file, "w"); + if (value == 0) fprintf(f, "0\n"); + else fprintf(f, "1\n"); + + fclose(f); +} + static inline void nrf24l01_ce_high() { - PORT_NRF24L01 |= _BV(DD_CE); + gpio_set(27, 1); } static inline void nrf24l01_ce_low() { - PORT_NRF24L01 &= ~_BV(DD_CE); + gpio_set(27, 0); } static inline uint8_t nrf24l01_read_cmd(uint8_t cmd) diff --git a/server/Makefile b/server/Makefile index 340f0be..3555311 100644 --- a/server/Makefile +++ b/server/Makefile @@ -3,8 +3,8 @@ PROGRAM = nrf24l01-server SRC = server.c ../spi.c OBJ = $(SRC:.c=.o) -CFLAGS ?= -Wall -Wpedantic -Werror -I../ -mmcu=atmega328p -LDFLAGS ?= -mmcu=atmega328p +CFLAGS ?= -Wall -Wpedantic -Werror -I../ +LDFLAGS ?= CROSS_COMPILE ?= avr- CC = $(CROSS_COMPILE)gcc diff --git a/server/server.c b/server/server.c index 6b85493..f6bea8d 100644 --- a/server/server.c +++ b/server/server.c @@ -1,7 +1,3 @@ -#define F_CPU 1000000UL - -#include <avr/io.h> -#include <util/delay.h> #include <nrf24l01.h> int main(void) @@ -1,39 +1,100 @@ #include <stdint.h> -#include <avr/io.h> -#include <avr/interrupt.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <getopt.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include <linux/types.h> +#include <linux/spi/spidev.h> + #include "spi.h" -void spi_init() +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + +static void pabort(const char *s) { - DDR_SPI &= ~((1<<DD_MOSI)|(1<<DD_MISO)|(1<<DD_SS)|(1<<DD_SCK)); - DDR_SPI |= ((1<<DD_MOSI)|(1<<DD_SS)|(1<<DD_SCK)); - - SPCR = ((1<<SPE)| // SPI Enable - (0<<SPIE)| // SPI Interupt Enable - (0<<DORD)| // Data Order (0:MSB first / 1:LSB first) - (1<<MSTR)| // Master/Slave select - (1<<SPR1)|(1<<SPR0)| // SPI Clock Rate - (0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle) - (0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling) - - SPSR = (0<<SPIF)|(0<<WCOL)|(0<<SPI2X); // Double Clock Rate + perror(s); + abort(); } -uint8_t spi_fast_shift(uint8_t data) +static const char *device = "/dev/spidev0.0"; +static uint8_t mode; +static uint8_t bits = 8; +static uint32_t speed = 500000; +static uint16_t delay; + +int fd; + +void spi_init() +// Initialize pins for spi communication { - SPDR = data; - while((SPSR & (1<<SPIF))==0); - return SPDR; + int ret = 0; + + fd = open(device, O_RDWR); + if (fd < 0) + pabort("can't open device"); + + /* + * spi mode + */ + ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); + if (ret == -1) + pabort("can't set spi mode"); + + ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); + if (ret == -1) + pabort("can't get spi mode"); + + /* + * bits per word + */ + ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); + if (ret == -1) + pabort("can't set bits per word"); + + ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); + if (ret == -1) + pabort("can't get bits per word"); + + /* + * max speed hz + */ + ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); + if (ret == -1) + pabort("can't set max speed hz"); + + ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); + if (ret == -1) + pabort("can't get max speed hz"); + + printf("spi mode: %d\n", mode); + printf("bits per word: %d\n", bits); + printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); } uint8_t spi_two_byte(uint8_t cmd, uint8_t arg) { - uint8_t data_buf; + int ret; + + uint8_t tx[2] = { 0, }; + uint8_t rx[2] = { 0, }; + + tx[0] = cmd; + tx[1] = arg; + + struct spi_ioc_transfer tr = { + .tx_buf = (unsigned long)tx, + .rx_buf = (unsigned long)rx, + .len = ARRAY_SIZE(tx), + .delay_usecs = delay, + .speed_hz = speed, + .bits_per_word = bits, + }; - spi_cs_low(); - spi_fast_shift(cmd); - data_buf = spi_fast_shift(arg); - spi_cs_high(); + ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); + if (ret < 1) + pabort("can't send spi message"); - return data_buf; + return rx[1]; } @@ -2,24 +2,6 @@ #define _SPI_H_ #include <stdint.h> -#include <avr/io.h> - -#define PORT_SPI PORTB -#define DDR_SPI DDRB -#define DD_MISO DDB4 -#define DD_MOSI DDB3 -#define DD_SS DDB2 -#define DD_SCK DDB5 - -inline void spi_cs_low() -{ - PORT_SPI &= ~(_BV(DD_SS)); -} - -inline void spi_cs_high() -{ - PORT_SPI |= _BV(DD_SS); -} extern void spi_init(); extern uint8_t spi_two_byte(uint8_t cmd, uint8_t data); @@ -0,0 +1,5 @@ +#ifndef __ZARUTIL__H +#define __ZARUTIL__H +#define _BV(bit) (1 << (bit)) +#define _delay_us usleep +#endif |