login|Register
Forum > C/C++
Post|
看17146|回9|Favorite
nopnop2002 看全部
2017-6-6 17:18:38

quote:

peterv6i replied at 2017-6-6 14:27
The problem is that I have limited space in our project.. There are 5 sensors for (oxygen, temperatu ...

There is some information about uart2spi board.

http://www.instructables.com/id/SPI-to-4-x-UART-Bridge-MULTIUART/


peterv6i 看全部
2017-6-7 14:30:17
Jap.. I have buy this interface on ebay.. Will try with Arduino and then with Orangepi... hope will work ;)
peterv6i 看全部
2017-7-12 16:13:01
Is there some library for orangepi PC to acess SPI from C/C++ like Arduino SPI.h?

Have to port this code (https://github.com/RowlandTechno ... aster/MULTIUART.cpp) to OrangepiPC

thank you

peterv6i 看全部
2017-7-21 15:14:02
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:

code:


  1. #include <iostream>
  2. #include <stdint.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <getopt.h>
  7. #include <fcntl.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/times.h>
  10. #include <linux/types.h>
  11. #include <linux/spi/spidev.h>
  12. #include <spi_lib.h>

  13. using namespace std;

  14. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

  15. static const char *device = "/dev/spidev0.0";
  16. static uint8_t mode = 0;
  17. static uint8_t bits = 8;
  18. static uint32_t speed = 1500000;
  19. static uint16_t delays;

  20. static void pabort(const char *s) {
  21.     perror(s);
  22.     abort();
  23. }

  24. void setBaudRate(int fd, char uart) {
  25.     if (uart < 4) {
  26.         int ret = -1;
  27.         uint8_t tx_buffer_baud = (uint8_t) (0x80 | uart); //9600 baud rate UART 1
  28.         ret = spi_write(fd, &tx_buffer_baud, 1);
  29.         printf("ret1: %d\n", ret);
  30.         tx_buffer_baud = (uint8_t) 0x3;
  31.         ret = spi_write(fd, &tx_buffer_baud, 1);
  32.         printf("ret1: %d\n", ret);
  33.     }
  34. }

  35. void TransmitString(int fd, char uart, char *DATA, char NUMBYTES) {
  36.     char IDX = (0x0);
  37.     int ret = -1;
  38.     if (uart < 4) {
  39.         uint8_t tx_buffer_baud = (uint8_t) (0x40 | uart); //9600 baud rate UART 1
  40.         ret = spi_write(fd, &tx_buffer_baud, 1);
  41.         tx_buffer_baud = (uint8_t) (NUMBYTES);
  42.         ret = spi_write(fd, &tx_buffer_baud, 1);
  43.         usleep(150);
  44.         while (IDX < NUMBYTES) {
  45.             tx_buffer_baud = (uint8_t) DATA[IDX];
  46.             ret = spi_write(fd, &tx_buffer_baud, 1);
  47.             IDX = IDX + 1;
  48.         }
  49.         
  50.     }
  51. }

  52. int main(int argc, char** argv) {
  53.     int ret = 0;
  54.     int fd;

  55.     fd = open(device, O_RDWR); //read write
  56.     if (fd < 0) {
  57.         printf("can't open device");
  58.         return -1;
  59.     }
  60.    
  61.          /*
  62.          * spi mode
  63.          */
  64.         ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  65.         if (ret == -1)
  66.                 pabort("can't set spi mode");

  67.         ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  68.         if (ret == -1)
  69.                 pabort("can't get spi mode");

  70.         /*
  71.          * bits per word
  72.          */
  73.         ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  74.         if (ret == -1)
  75.                 printf("can't set bits per word");

  76.         ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  77.         if (ret == -1)
  78.                 printf("can't get bits per word");

  79.         /*
  80.          * max speed hz
  81.          */
  82.         ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  83.         if (ret == -1)
  84.                 printf("can't set max speed hz");

  85.         ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  86.         if (ret == -1)
  87.                 printf("can't get max speed hz");


  88.    
  89.     printf("spi mode: %d\n", mode);
  90.     printf("bits per word: %d\n", bits);
  91.     printf("max speed: %d Hz (%d KHz)\n", speed, speed / 1000);


  92.     //set baud rate
  93.     // Initialise the UART baud rates
  94.     // 0=1200, 1=2400, 2=4800, 3=9600, 4=19200, 5=38400, 6=57600, 7=115200
  95.     setBaudRate(fd, 0); //UART 1
  96.     setBaudRate(fd, 1); //UART 1
  97.     setBaudRate(fd, 2); //UART 1
  98.     setBaudRate(fd, 3); //UART 1
  99.    
  100.    

  101.     TransmitString(fd,0,"PETER TEST",10);
  102.    
  103.     close(fd);
  104.     return ret;

  105. }


Here is the result ;)

12

OrangePi En

Powered by Discuz! X3.4

homepage|Simple edition|Touch edition|PC