View: 4361|Reply: 4

Using externalhardware over SPI from C/C++

[Copy link]

4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
Published in 2017-7-14 19:24:45 | Show all floors |Read mode
Edited by peterv6i at 2017-7-14 19:36

Hi, need help on how to use  /dev/spi0.0 in C/C++

I need to use external hardware (uart2spi) with orangepipc. For now I have connected SPI pins and power to external board.




This external "board" works well with Arduino over a library provided by author of the board.
The source of library is here: https://github.com/RowlandTechno ... aster/MULTIUART.cpp

I need a help on how to "convert" this method to C function using /dev/spi0.0 device (how to write and receive byte/bytes) from spi.
The digitalWrite(_ss_pin, HIGH); is used to set CS signal high or low.. (I have connected CS pin from my board to CS pin on GPIO).

the methods below are for Arduino (need help how to rewrite to C using /dev/spi0.0 device)
  1. char CheckRx(char UART)
  2. {
  3.         char RETVAL = 0;
  4. if (UART < 4)
  5.         {
  6.                 digitalWrite(_ss_pin, LOW);
  7. SPI.transfer(0x10 | UART);
  8. delayMicroseconds(250);
  9. RETVAL = SPI.transfer(0xFF);
  10. digitalWrite(_ss_pin, HIGH);
  11. delayMicroseconds(50);
  12.         }
  13. return (RETVAL);
  14. }
Copy code
or this
  1. void ReceiveString(char *RETVAL, char UART, char NUMBYTES)
  2. {
  3.         //Local variable definitions
  4.         char IDX = (0x0);

  5.         if (UART < 4)
  6.         {
  7.                 digitalWrite(_ss_pin, LOW);
  8. SPI.transfer(0x20 | UART);
  9. delayMicroseconds(50);
  10. SPI.transfer(NUMBYTES);
  11. delayMicroseconds(50);
  12. while ((IDX < NUMBYTES))
  13.                 {
  14.                         RETVAL[IDX] = SPI.transfer(0xFF);
  15. delayMicroseconds(50);
  16. IDX = IDX + 1;
  17.                 }

  18.                 digitalWrite(_ss_pin, HIGH);
  19. delayMicroseconds(50);
  20. RETVAL[IDX] = 0;
  21.         }
  22. }
Copy code


some instructions are also here:

"This board provides up to four buffered UART channels from a single SPI connection. If more than four UARTs are required then a second board can be added to provide eight channels and so on.

The command code and UART channel are packed together into a single byte to increase efficiency. The command code resides in the top 4 bits and the channel resides in the bottom 2 bits.

Here are the bits in the byte and their representation.
cccc00uu
where cccc is the command code and uu is the UART channel (0 - 3).


Command CodeParametersReturnsDescription
0x10N/ANumBytes (0-255)Reads the number of bytes in the channel receive buffer.
0x30N/ANumBytes (0-255)Reads the number of bytes in the channel transmit buffer.
0x20NumBytes (0-255)DataByte (0-255) [0-x]Reads data byte(s) from the channel receive buffer.
0x40NumBytes (0-255), DataByte (0-255) [0-x]N/APuts data byte(s) into the channel transmit buffer.
0x80Baud (0-7)N/ASets the channel baud rate

Here are the options for the baud rate parameter.
0=1200, 1=2400, 2=4800, 3=9600, 4=19200, 5=38400, 6=57600, 7=115200

So to read the number of bytes in UART channel 2 receive buffer we send the following command code and then perform a read.
CS Low
SPISend ( 0x12 )
NumBytes = SPIReceive ( 0xFF )
CS High

To read 5 bytes from UART channel 2 receive buffer we send the following command code, the number of bytes and then perform enough reads to pull out all the data we want.
CS Low
SPISend ( 0x22 )
SPISend ( 0x05 )
Byte[0] = SPIReceive ( 0xFF )
Byte[1] = SPIReceive ( 0xFF )
Byte[2] = SPIReceive ( 0xFF )
Byte[3] = SPIReceive ( 0xFF )
Byte[4] = SPIReceive ( 0xFF )
CS High

To write 5 bytes to UART channel 2 transmit buffer we send the following command code, the number of bytes and then perform enough writes to send out all the data we want.
CS Low
SPISend ( 0x42 )
SPISend ( 0x05 )
SPISend ( Byte[0] )
SPISend ( Byte[1] )
SPISend ( Byte[2] )
SPISend ( Byte[3] )
SPISend ( Byte[4] )
CS High

Each UART peripheral has an 1000 byte buffer dedicated for receiving and another 1000 byte buffer dedicated to transmitting allowing a large amount of data to be sent and received simultaneously which should mean that you never loose a byte.
"

For now I have setup Netbeans with remote acess to orangepipc..this simple code work well ;)

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


  14. using namespace std;

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

  16. static const char *device = "/dev/spidev0.0";
  17. static uint8_t mode = 3;
  18. static uint8_t bits = 8;
  19. static uint32_t speed = 1000000;
  20. static uint16_t delay;

  21. int main(int argc, char** argv) {
  22.     int ret = 0;
  23.     int fd;

  24.     fd = open(device, O_RDWR);
  25.     if (fd < 0) {
  26.         printf("can't open device");
  27.         return -1;
  28.     }
  29.    
  30.    
  31.    
  32.     close(fd);
  33.     return ret;




  34.     /*
  35.     int init = sunxi_gpio_init();
  36.     cout << "sunxi_gpio_init(): " << init << ".\n";
  37.     sunxi_gpio_set_cfgpin(SUNXI_GPA(11), SUNXI_GPIO_OUTPUT);
  38.     sunxi_gpio_set_cfgpin(SUNXI_GPA(0), SUNXI_GPIO_OUTPUT);

  39.     sunxi_gpio_output(SUNXI_GPA(11), 1);
  40.     sleep(1);
  41.     sunxi_gpio_output(SUNXI_GPA(11), 0);
  42.     sunxi_gpio_output(SUNXI_GPA(0), 0);
  43.     sleep(1);
  44.     cout << ".";
  45.      */


  46.     return 0;


  47. }
Copy code



thank you for any help
Peter


3

threads

159

posts

110K

credits

Forum patriarch

Rank: 8Rank: 8

credits
11816
Published in 2017-7-15 19:13:07 | Show all floors
Hi,
Just to make sure:
Why can't you use the UART pins, next to the power socket?
Don't get me wrong, I never used the GPIO for UART communication, have you considered:
  • If you haven't had a look at the GPIO pinout, you can find it here
  • as far a I can see you connected 6 wires to the TTL controler -> what pins at the GPIO of the OPI did you use and what corrosponding pins at your board.
  • As far as I understand the UART1_TX is pin38 and UART1_RX pin40 in MUX2 (?/I have not much knowledge about them but others might). UART2 and 3 see link above
  • Wouldn't you need to remap these?

I am just trying to help a bit and maybe others will have a solution.
Please click on reply when answering direct to me. That way I get a notification ;)

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety."
                                  Benjamin Franklin 1755

4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
 Author| Published in 2017-7-16 17:03:02 | Show all floors
Hi, this external interface board uses

3.3V pin for power supply,
GND pin for ground

and

MISO
MOSI
CLK
CS

pin from GPIO for SPI communication..

I use this board because I have no additional space in the housing where 5 or more sensors are installed. Sensors are for:
salinity, pressure, temperature, oxygen, clorophile.. I know that OrangePi PC has some UARTs on board but I need minimum 5 uarts port. I like to avoid external usb2uart interfaces.

Initially I have used this interface with Arduino board over SPI channel but I like to use OrangePi because it's a computer (not a microcontroller) and on orangpipc I can install mysql and other software to store measured datas. I'm doing some research If I can use orangpi as autonomous measurement system.

2

threads

52

posts

565

credits

Senior member

Rank: 4

credits
565
Published in 2017-7-16 22:15:37 | Show all floors
@peterv6i there are $2 ways to remote your arduino 150feet away from recording station, check your PM
These aren't the Droids you're looking for. . .You look for H3Droid !

4

threads

11

posts

73

credits

Registered member

Rank: 2

credits
73
 Author| Published in 2017-7-18 13:26:42 | Show all floors
I just need to use SPI bus with C/C++ and I'am looking for help thank you..
You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list