View: 18509|Reply: 8

Access SPI bus with C/C++ on OrangePI pc

[Copy link]

4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
Published in 2017-6-5 15:15:39 | Show all floors |Read mode
Hi,
I have a need to use 4-5 UARTS on OrangePI. The only way for now is to use spi2UART board found on ebay. This board uses SPI bus so I'am looking for some example or library for C/C++ on how to use SPI bus on orangepi pc.
Thank you

regards
Peter

4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
 Author| Published in 2017-6-5 21:40:32 | Show all floors
Edited by peterv6i at 2017-6-5 21:46

Hi, can you explain me what you mean? What is ESP12/13?
If I google I can find some module (ESP-13 ESP8266 Serial Wi-Fi Wireless Transceive) + MQTT? (https://en.wikipedia.org/wiki/MQTT)..

Is there some example?

I have 5 sensors (for oceanography) with uart output to connect with orangepi and then parse received packages and send over ethernet to some other remote pc...

thank you for any info & idea!

regards
Peter

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-6-5 19:20:34 | Show all floors
Edited by nopnop2002 at 2017-6-5 19:23

There is a different idea.

This thread contains more resources

You need to Log in to download or view,No account?    Register

x

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-6-6 04:20:47 | Show all floors
peterv6i replied at 2017-6-5 21:40
Hi, can you explain me what you mean? What is ESP12/13?
If I google I can find some module (ESP-13  ...

ESP12/13 is a micro controller using ESP8266 chip.
There is many example in the INTERNET.

4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
 Author| Published in 2017-6-6 14:27:47 | Show all floors
Edited by peterv6i at 2017-6-6 14:30

The problem is that I have limited space in our project.. There are 5 sensors for (oxygen, temperature, salinity, chlorophile, pressure) and all of them have UART output. My job is to read all sensors connected over UART and parse datas and redirect the values in some other format over socket (ethernet) to the "boat" over ethernet (POE) cable...

The idea is to read all uarts over uart2spi board (4 uart input to spi).. the second idea is to use some arduino for this job..

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-6-6 17:18:38 | Show all floors
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/


4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
 Author| Published in 2017-6-7 14:30:17 | Show all floors
Jap.. I have buy this interface on ebay.. Will try with Arduino and then with Orangepi... hope will work ;)

4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
 Author| Published in 2017-7-12 16:13:01 | Show all floors
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

4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
 Author| Published in 2017-7-21 15:14:02 | Show all floors
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:


  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. }
Copy code


Here is the result ;)

You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list