Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
orange pi / arm board and linking libraries for SPI and other devices
#1
Greetings, 

I hate asking, but I burned up a wild amount of time already so hoping for some direction. 
I have QB64PE running on orange pi zero 3 with ubuntu desktop orange pi release. 

I'm trying to communicate over SPI using qb64 (and other connections eventually) 
I spent a ton of time looking online but theres always a ddead end it seems. 
A friend suggested AI, so here's an SPI program AI designed :

Code: (Select All)
DECLARE DYNAMIC LIBRARY "C"
    FUNCTION open (path AS STRING, flags AS LONG) AS LONG
    FUNCTION ioctl (fd AS LONG, request AS LONG, argp AS _OFFSET) AS LONG
    FUNCTION write (fd AS LONG, buffer AS _OFFSET, count AS LONG) AS LONG
    FUNCTION read (fd AS LONG, buffer AS _OFFSET, count AS LONG) AS LONG
    FUNCTION close (fd AS LONG) AS LONG
END DECLARE

CONST O_RDWR = &H2
CONST SPI_IOC_MAGIC = &H6B
CONST SPI_IOC_RD_MODE = (SPI_IOC_MAGIC << 8) + 1
CONST SPI_IOC_WR_MODE = (SPI_IOC_MAGIC << 8) + 1

DIM fd AS LONG
DIM spi_mode AS _BYTE
DIM tx_buffer(0 TO 2) AS _BYTE
DIM rx_buffer(0 TO 2) AS _BYTE

' Open the SPI device
fd = open("/dev/spidev1.0", O_RDWR)
IF fd < 0 THEN
    PRINT "Failed to open SPI device."
    END
ELSE
    PRINT "SPI device opened successfully."
END IF

' Set SPI mode (0, 1, 2, or 3)
spi_mode = 0
IF ioctl(fd, SPI_IOC_WR_MODE, _OFFSET(spi_mode)) < 0 THEN
    PRINT "Failed to set SPI mode."
    CALL close(fd)
    END
ELSE
    PRINT "SPI mode set successfully."
END IF

' Prepare data to send
tx_buffer(0) = &H9F  ' Example command

' Send and receive data
IF write(fd, _OFFSET(tx_buffer(0)), 1) <> 1 THEN
    PRINT "Failed to write to SPI device."
    CALL close(fd)
    END
ELSE
    PRINT "Data written to SPI device."
END IF

IF read(fd, _OFFSET(rx_buffer(0)), 3) <> 3 THEN
    PRINT "Failed to read from SPI device."
    CALL close(fd)
    END
ELSE
    PRINT "Data read from SPI device: ";
    FOR i = 0 TO 2
        PRINT HEX$(rx_buffer(i)); " ";
    NEXT
    PRINT
END IF

' Close the SPI device
CALL close(fd)
PRINT "SPI device closed."

END
So far there are a few errors, including DYNAMIC LIBRARY NOT FOUND. 

I've honestly never linked a file or anything else to any QB before. 

Does the program look plausible though? 

I really need to make this work somehow, I love QB64 and too old to learn anything else lol. 
And I really need to make this project work...
Reply
#2
The dynamic library "C" cannot be found because it is not there, as banal as that sounds. The library was probably written in C, and the specified functions are located there. Was it not there? How was the AI program created?

"ioctl" is a system call under X systems (Unix, Linux). Most people here use Windows. Try it in a Linux or Ubuntu forum. I only know of one in German:
Ubuntu-Forum

For example, a program written in C is integrated in QB64 like this: C-Program in QB64
Reply
#3
For the APIs you're calling you can just use `Declare Library` with no library listed. Those APIs are part of the C standard library that QB64 already includes so you don't need to specify the library they come from.

As for the code, I don't know much about the `spidev` device but the code looks passable, the functions all look like they're called correctly.
Reply
#4
Thanks guys still pounding away at this
Reply




Users browsing this thread: 2 Guest(s)