Usage of an external library (bcm2835) with halcompile

More
7 years 3 months ago #140401 by wicki
I try to use the rapsberry-bcm2835-library inside of my
ethraw.comp.

Even if compiled with oder without

option extra_link_args "-lbcm2835";

I get a "ethraw.so" without any errors or warnings - but every time
I try to load the component, I get this error:

ethraw: dlopen: /home/pi/linuxcnc/rtlib/ethraw.so: undefined symbol: bcm2835_gpio_write


How is it possible to use an external library inside such a component?

Please Log in to join the conversation.

More
7 years 3 months ago #140549 by tommylight
I can not help, but replied so it moves to the top of "recent posts", makes it easier for others to see it.

Please Log in to join the conversation.

More
7 years 3 months ago #140551 by wicki
My plan was to set a GPIO-pin before sending the eth-packet.

I have bypassed the problem by direct IO (see below - shamelessly stolen from here: www.pieter-jan.com/node/15 ).

Now I can measure the time difference between send and reaction to the reception.
The last image here is the result.

erste.de/timing-tests.PI4.html

license "GPL";
;;


#define BCM2708_PERI_BASE 0xfe000000 /* 0x20000000 */
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define PIN 22 // GPIO3 // 27 GPIO2 17 // GPIO0


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)

int mem_fd;
void *gpio_map;

// I/O access
volatile unsigned *gpio;


// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))

#define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0

void flash() {
int f1=0;
GPIO_CLR = (1 << PIN);
GPIO_SET = (1 << PIN);
usleep(1);
GPIO_CLR = (1 << PIN);
return;
}


//
// Set up a memory regions to access GPIO
//
void setup_io()
{
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit(-1);
}

/* mmap GPIO */
gpio_map = mmap(
NULL, //Any adddress in our space will do
BLOCK_SIZE, //Map length
PROT_READ|PROT_WRITE, // Enable reading & writting to mapped memory
MAP_SHARED, //Shared with other processes
mem_fd, //File to map
GPIO_BASE //Offset to GPIO peripheral
);

close(mem_fd); //No need to keep mem_fd open after mmap

if (gpio_map == MAP_FAILED) {
printf("mmap error %d\n", (int)gpio_map); //errno also set!
exit(-1);
}

// Always use volatile pointer!
gpio = (volatile unsigned *)gpio_map;


int g,rep;
// Set up gpi pointer for direct register access
INP_GPIO(PIN); // must use INP_GPIO before we can use OUT_GPIO
OUT_GPIO(PIN);
return;
} // setup_io

Please Log in to join the conversation.

More
7 years 3 months ago #140569 by Grotius
Hi,

undefined symbol: bcm2835_gpio_write

You are almost where you want to be.

Where is this symbol declared in your component file? And is it defined as int, bool, double etc?
Or is it a stream?

Please Log in to join the conversation.

More
7 years 3 months ago #140607 by wicki
> Where is this symbol declared in your component file

bcm2835_gpio_write is part of the library I want to use.
www.airspayce.com/mikem/bcm2835/

bcm2835_gpio_write()
void bcm2835_gpio_write ( uint8_t pin,
uint8_t on
)

Please Log in to join the conversation.

More
7 years 3 months ago #140610 by rodw
I might be way off but
Provided the object file is being linked or loaded dynamically, I would think you need to add a prototype as an extern. eg
Code:
extern void bcm2835_gpio_write ( uint8_t pin, uint8_t on);

I've never tried this from halcompile

Please Log in to join the conversation.

More
7 years 3 months ago #140617 by wicki
> I would think you need to add a prototype as an extern. eg

nice idea...
but it does not solve this problem:
option extra_link_args "-lbcm2835";
option extra_compiler_args "-lbcm2835";

;;
#include <bcm2835.h>
extern void bcm2835_gpio_write( uint8_t pin, uint8_t on);
extern int bcm2835_init(void);

compils without error - but if I call

bcm2835_init()

I get this:

ethraw: dlopen: /home/pi/linuxcnc/rtlib/ethraw.so: undefined symbol: bcm2835_init


btw: its a .a-library

Please Log in to join the conversation.

More
7 years 3 months ago #140620 by rodw
I've never done any linux c coding so can't really help. But it looks like its looking in the right spot and coming up empty handed. I wonder if its written as a class?

Please Log in to join the conversation.

More
7 years 3 months ago #140697 by Grotius
The preprocess will help him to dig more into the comp code...

Preprocess .comp files into .c files (the --preprocess flag)

how to :
linuxcnc.org/docs/2.5/html/man/man1/comp.1.html

The preprocess option is very nice. It translate's the comp code into a fully c code.

What is your include name for the library where bcm2835_gpio_write lives?

To files attached. The abs.comp is preprocessed into abc.c
Very nice !!

Attachment not found


Warning: Spoiler!

Attachment not found


Warning: Spoiler!


You see, much more code to dig in. When you are done, halcompile the c back into comp.

Please Log in to join the conversation.

Time to create page: 0.116 seconds
Powered by Kunena Forum