复制代码- #include <stdint.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 "tx01_30_simply.h"
- #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
- 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;
- void init_NRF24L01(int fdo, int fd) //initialize the NRF24L01
- {
- int t;
- uint8_t tx1[] = {
- 0x30, 0xE7, 0xD3, 0xF0, 0x35, 0x77,
- };
- uint8_t rx1[ARRAY_SIZE(tx1)] = {0, };
- struct spi_ioc_transfer tr1 = {
- .tx_buf = (unsigned long)tx1,
- .rx_buf = (unsigned long)rx1,
- .len = ARRAY_SIZE(tx1),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx2[] = {
- 0x2A, 0xE7, 0xD3, 0xF0, 0x35, 0x77,
- };
- uint8_t rx2[ARRAY_SIZE(tx2)] = {0, };
- struct spi_ioc_transfer tr2 = {
- .tx_buf = (unsigned long)tx2,
- .rx_buf = (unsigned long)rx2,
- .len = ARRAY_SIZE(tx2),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx3[] = {
- 0x21, 0x01,
- };
- uint8_t rx3[ARRAY_SIZE(tx3)] = {0, };
- struct spi_ioc_transfer tr3 = {
- .tx_buf = (unsigned long)tx3,
- .rx_buf = (unsigned long)rx3,
- .len = ARRAY_SIZE(tx3),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx4[] = {
- 0x22, 0x01,
- };
- uint8_t rx4[ARRAY_SIZE(tx4)] = {0, };
- struct spi_ioc_transfer tr4 = {
- .tx_buf = (unsigned long)tx4,
- .rx_buf = (unsigned long)rx4,
- .len = ARRAY_SIZE(tx4),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx5[] = {
- 0x25, 0x00,
- };
- uint8_t rx5[ARRAY_SIZE(tx5)] = {0, };
- struct spi_ioc_transfer tr5 = {
- .tx_buf = (unsigned long)tx5,
- .rx_buf = (unsigned long)rx5,
- .len = ARRAY_SIZE(tx5),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx6[] = {
- 0x31, 30,
- };
- uint8_t rx6[ARRAY_SIZE(tx6)] = {0, };
- struct spi_ioc_transfer tr6 = {
- .tx_buf = (unsigned long)tx6,
- .rx_buf = (unsigned long)rx6,
- .len = ARRAY_SIZE(tx6),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx7[] = {
- 0x26, 0x07,
- };
- uint8_t rx7[ARRAY_SIZE(tx7)] = {0, };
- struct spi_ioc_transfer tr7 = {
- .tx_buf = (unsigned long)tx7,
- .rx_buf = (unsigned long)rx7,
- .len = ARRAY_SIZE(tx7),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx8[] = {
- 0x20, 0x0E,
- };
- uint8_t rx8[ARRAY_SIZE(tx8)] = {0, };
- struct spi_ioc_transfer tr8 = {
- .tx_buf = (unsigned long)tx8,
- .rx_buf = (unsigned long)rx8,
- .len = ARRAY_SIZE(tx8),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- ioctl(fdo,1,0); //CE=0, chip enable
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr1);
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr2);
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr3);
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr4);
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr5);
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr6);
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr7);
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr8);
-
- }
- void NRF24L01_TX (int fdo, int fd) //send message funcion
- {
- int num;
- int t;
- uint8_t tx1[] = {
- 0x30, 0xE7, 0xD3, 0xF0, 0x35, 0x77,
- };
- uint8_t rx1[ARRAY_SIZE(tx1)] = {0, };
- struct spi_ioc_transfer tr1 = {
- .tx_buf = (unsigned long)tx1,
- .rx_buf = (unsigned long)rx1,
- .len = ARRAY_SIZE(tx1),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx2[] = {
- 0x2A, 0xE7, 0xD3, 0xF0, 0x35, 0x77,
- };
- uint8_t rx2[ARRAY_SIZE(tx2)] = {0, };
- struct spi_ioc_transfer tr2 = {
- .tx_buf = (unsigned long)tx2,
- .rx_buf = (unsigned long)rx2,
- .len = ARRAY_SIZE(tx2),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx3[] = {
- 0x21, 0x01,
- };
- uint8_t rx3[ARRAY_SIZE(tx3)] = {0, };
- struct spi_ioc_transfer tr3 = {
- .tx_buf = (unsigned long)tx3,
- .rx_buf = (unsigned long)rx3,
- .len = ARRAY_SIZE(tx3),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx4[] = {
- 0x22, 0x01,
- };
- uint8_t rx4[ARRAY_SIZE(tx4)] = {0, };
- struct spi_ioc_transfer tr4 = {
- .tx_buf = (unsigned long)tx4,
- .rx_buf = (unsigned long)rx4,
- .len = ARRAY_SIZE(tx4),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx5[] = {
- 0x25, 0x00,
- };
- uint8_t rx5[ARRAY_SIZE(tx5)] = {0, };
- struct spi_ioc_transfer tr5 = {
- .tx_buf = (unsigned long)tx5,
- .rx_buf = (unsigned long)rx5,
- .len = ARRAY_SIZE(tx5),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx6[] = {
- 0x31, 30,
- };
- uint8_t rx6[ARRAY_SIZE(tx6)] = {0, };
- struct spi_ioc_transfer tr6 = {
- .tx_buf = (unsigned long)tx6,
- .rx_buf = (unsigned long)rx6,
- .len = ARRAY_SIZE(tx6),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx7[] = {
- 0x26, 0x07,
- };
- uint8_t rx7[ARRAY_SIZE(tx7)] = {0, };
- struct spi_ioc_transfer tr7 = {
- .tx_buf = (unsigned long)tx7,
- .rx_buf = (unsigned long)rx7,
- .len = ARRAY_SIZE(tx7),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t tx8[] = {
- 0x20, 0x0E,
- };
- uint8_t rx8[ARRAY_SIZE(tx8)] = {0, };
- struct spi_ioc_transfer tr8 = {
- .tx_buf = (unsigned long)tx8,
- .rx_buf = (unsigned long)rx8,
- .len = ARRAY_SIZE(tx8),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t txa[] = {
- 0xA0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
- 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,0x28, 0x29, 0x30,
- };
- uint8_t rxa[ARRAY_SIZE(txa)] = {0, };
- struct spi_ioc_transfer tra = {
- .tx_buf = (unsigned long)txa,
- .rx_buf = (unsigned long)rxa,
- .len = ARRAY_SIZE(txa),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- ioctl(fdo, 1, 0); //CE=0, StandBy Mode
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr2); //load the address of the receiver
- ioctl(fd, SPI_IOC_MESSAGE(1), &tra); //load the data
- ioctl(fd, SPI_IOC_MESSAGE(1), &tr8); //IRQ works, and 16CRC, and TX mode
- ioctl(fdo, 0, 0); //CE=1, to send the message
- //****To display the sending message
- for (num = 1; num < 31; num++) {
- printf("%.2X ", txa[num]); //txa[] is right, not rxa[]
- }
- puts("");
- }
-
- int tx01()
- {
- int success = 0;
- int fd;
- int fdo;
- fd = open(device, O_RDWR);
- fdo = open("/dev/CE", O_RDWR);
- /*
- * spi mode
- */
- ioctl(fd, SPI_IOC_WR_MODE, &mode);
- ioctl(fd, SPI_IOC_RD_MODE, &mode);
-
- /*
- * bits per word
- */
- ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
-
- ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
-
- /*
- * max speed hz
- */
- ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
- ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
- 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 txi[] = {
- 0x27, 0xFF,
- };
- uint8_t rxi[ARRAY_SIZE(txi)] = {0, };
- struct spi_ioc_transfer tri = {
- .tx_buf = (unsigned long)txi,
- .rx_buf = (unsigned long)rxi,
- .len = ARRAY_SIZE(txi),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
- uint8_t txj[] = {
- 0x07,
- };
- uint8_t rxj[ARRAY_SIZE(txj)] = {0, };
- struct spi_ioc_transfer trj = {
- .tx_buf = (unsigned long)txj,
- .rx_buf = (unsigned long)rxj,
- .len = ARRAY_SIZE(txj),
- .delay_usecs = delay,
- .speed_hz = speed,
- .bits_per_word = bits,
- };
-
- init_NRF24L01(fdo,fd);
- while(1)
- {
- NRF24L01_TX (fdo,fd);
-
- ioctl(fdo, 1, 0); //CE=0
- ioctl(fd, SPI_IOC_MESSAGE(1), &trj); //read the STATUS
- if((rxj[0]&0x20)==0x20) //send messages successfully
- success = 1;
- ioctl(fd, SPI_IOC_MESSAGE(1), &tri); // write the STATUS,to clear the IRQ
- if(success==1)
- break;
- }
- close(fd);
- close(fdo);
- return 1;
- }
|