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)
- char CheckRx(char UART)
- {
- char RETVAL = 0;
- if (UART < 4)
- {
- digitalWrite(_ss_pin, LOW);
- SPI.transfer(0x10 | UART);
- delayMicroseconds(250);
- RETVAL = SPI.transfer(0xFF);
- digitalWrite(_ss_pin, HIGH);
- delayMicroseconds(50);
- }
- return (RETVAL);
- }
Copy code or this
- void ReceiveString(char *RETVAL, char UART, char NUMBYTES)
- {
- //Local variable definitions
- char IDX = (0x0);
- if (UART < 4)
- {
- digitalWrite(_ss_pin, LOW);
- SPI.transfer(0x20 | UART);
- delayMicroseconds(50);
- SPI.transfer(NUMBYTES);
- delayMicroseconds(50);
- while ((IDX < NUMBYTES))
- {
- RETVAL[IDX] = SPI.transfer(0xFF);
- delayMicroseconds(50);
- IDX = IDX + 1;
- }
- digitalWrite(_ss_pin, HIGH);
- delayMicroseconds(50);
- RETVAL[IDX] = 0;
- }
- }
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 Code | Parameters | Returns | Description | 0x10 | N/A | NumBytes (0-255) | Reads the number of bytes in the channel receive buffer. | 0x30 | N/A | NumBytes (0-255) | Reads the number of bytes in the channel transmit buffer. | 0x20 | NumBytes (0-255) | DataByte (0-255) [0-x] | Reads data byte(s) from the channel receive buffer. | 0x40 | NumBytes (0-255), DataByte (0-255) [0-x] | N/A | Puts data byte(s) into the channel transmit buffer. | 0x80 | Baud (0-7) | N/A | Sets 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 ;)
- #include <iostream>
- #include <stdio.h>
- #include <stdint.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #include <string.h>
- #include <gpio_lib.h>
- #include <linux/spi/spidev.h>
- #include <spi_lib.h>
- #include <linux/types.h>
- using namespace std;
- #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
- static const char *device = "/dev/spidev0.0";
- static uint8_t mode = 3;
- static uint8_t bits = 8;
- static uint32_t speed = 1000000;
- static uint16_t delay;
- int main(int argc, char** argv) {
- int ret = 0;
- int fd;
- fd = open(device, O_RDWR);
- if (fd < 0) {
- printf("can't open device");
- return -1;
- }
-
-
-
- close(fd);
- return ret;
- /*
- int init = sunxi_gpio_init();
- cout << "sunxi_gpio_init(): " << init << ".\n";
- sunxi_gpio_set_cfgpin(SUNXI_GPA(11), SUNXI_GPIO_OUTPUT);
- sunxi_gpio_set_cfgpin(SUNXI_GPA(0), SUNXI_GPIO_OUTPUT);
- sunxi_gpio_output(SUNXI_GPA(11), 1);
- sleep(1);
- sunxi_gpio_output(SUNXI_GPA(11), 0);
- sunxi_gpio_output(SUNXI_GPA(0), 0);
- sleep(1);
- cout << ".";
- */
- return 0;
- }
Copy code
thank you for any help
Peter
|