|
Solved..
this is how I have connected my interface with orangepi
Then I have ported two methods from arduino library to C.
In my pc i have plugged a USB/UART TTL cable and connected RX,TX ang GND pin to interface which is then connected over SPI to OrangePI..
Here is source code:
- #include <iostream>
- #include <stdint.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <getopt.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <sys/times.h>
- #include <linux/types.h>
- #include <linux/spi/spidev.h>
- #include <spi_lib.h>
- using namespace std;
- #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
- static const char *device = "/dev/spidev0.0";
- static uint8_t mode = 0;
- static uint8_t bits = 8;
- static uint32_t speed = 1500000;
- static uint16_t delays;
- static void pabort(const char *s) {
- perror(s);
- abort();
- }
- void setBaudRate(int fd, char uart) {
- if (uart < 4) {
- int ret = -1;
- uint8_t tx_buffer_baud = (uint8_t) (0x80 | uart); //9600 baud rate UART 1
- ret = spi_write(fd, &tx_buffer_baud, 1);
- printf("ret1: %d\n", ret);
- tx_buffer_baud = (uint8_t) 0x3;
- ret = spi_write(fd, &tx_buffer_baud, 1);
- printf("ret1: %d\n", ret);
- }
- }
- void TransmitString(int fd, char uart, char *DATA, char NUMBYTES) {
- char IDX = (0x0);
- int ret = -1;
- if (uart < 4) {
- uint8_t tx_buffer_baud = (uint8_t) (0x40 | uart); //9600 baud rate UART 1
- ret = spi_write(fd, &tx_buffer_baud, 1);
- tx_buffer_baud = (uint8_t) (NUMBYTES);
- ret = spi_write(fd, &tx_buffer_baud, 1);
- usleep(150);
- while (IDX < NUMBYTES) {
- tx_buffer_baud = (uint8_t) DATA[IDX];
- ret = spi_write(fd, &tx_buffer_baud, 1);
- IDX = IDX + 1;
- }
-
- }
- }
- int main(int argc, char** argv) {
- int ret = 0;
- int fd;
- fd = open(device, O_RDWR); //read write
- if (fd < 0) {
- printf("can't open device");
- return -1;
- }
-
- /*
- * 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)
- printf("can't set bits per word");
- ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
- if (ret == -1)
- printf("can't get bits per word");
- /*
- * max speed hz
- */
- ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
- if (ret == -1)
- printf("can't set max speed hz");
- ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
- if (ret == -1)
- printf("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);
- //set baud rate
- // Initialise the UART baud rates
- // 0=1200, 1=2400, 2=4800, 3=9600, 4=19200, 5=38400, 6=57600, 7=115200
- setBaudRate(fd, 0); //UART 1
- setBaudRate(fd, 1); //UART 1
- setBaudRate(fd, 2); //UART 1
- setBaudRate(fd, 3); //UART 1
-
-
- TransmitString(fd,0,"PETER TEST",10);
-
- close(fd);
- return ret;
- }
Copy code
Here is the result ;)
|
|