Having major problems with my own handlers for my libraries

McDottie
Posts: 7
Joined: Fri Nov 05, 2021 11:18 am

Having major problems with my own handlers for my libraries

Postby McDottie » Mon May 15, 2023 9:55 am

-----------------EDIT-----------------
SOLVED! The names of the functions was the problem. the compiler thought they were read and write from unistd :)

Code: Select all

_READ_WRITE_RETURN_TYPE read (int __fd, void *__buf, size_t __nbyte);
_READ_WRITE_RETURN_TYPE write (int __fd, const void *__buf, size_t __nbyte);
-----------------EDIT-----------------

Hi, i'm sure i'm missing something here but i'm having major trouble with my code. (I can't post my code here but I will do my best to give an example in order to receive help).

So I have a structure like this:

Code: Select all

typedef struct {
    uint8_t i2c_address;
    int i2c_frequency;
    gpio_num_t sda;
    gpio_num_t scl;
    sensor_err_t (*i2c_init)(int sda, int scl, int i2c_frequency);
    sensor_err_t (*i2c_write)(uint8_t address, uint8_t registerAddress, uint8_t count, uint8_t * data);
    sensor_err_t (*i2c_read)(uint8_t address, uint8_t registerAddress, uint8_t count, uint8_t * dest);
} sensor_i2c_t ;

typedef struct{
    sensor_i2c_t * i2c;
    int x;
    int y;
} sensor_t;

#define SENSOR_DEFAULT_STATIC_SENSOR_STRUCT(sensor,sda,scl,init,write,read)  		\
		sensor_i2c_t sensor_internal_i2c = SENSOR_DEFAULT_SENSOR_I2C_STRUCT(sda,scl,init,write,read);	\
        	sensor->i2c = &sensor_internal_i2c;     													\
		sensor->buffer_elems_size = 0;                         								\
		sensor->buffer_max_size = fifo_buffer_size;            								\
		sensor->buffer_processed = 0;                          								\
		sensor->fifo_buffer = malloc(fifo_buffer_size);										\
		
#define SENSOR_DEFAULT_SENSOR_I2C_STRUCT(sdaP,sclP,init,write,read)  	\
            {                                   \
                .i2c_address = SENSOR_I2C_ADDR,    \
                .i2c_frequency = SENSOR_I2C_RATE,  \
                .sda = sdaP,                    \
                .scl = sclP,                    \
				.i2c_init = init,				\
				.i2c_write = write,				\
				.i2c_read = read,				\
            }
The problem here is, I create the structures like below:

Code: Select all

sensor_err_t init(int sda, int scl, int i2c_frequency){ .....}
sensor_err_t read(uint8_t address, uint8_t registerAddress, uint8_t count, uint8_t * dest){ .....}
sensor_err_t write(uint8_t address, uint8_t registerAddress, uint8_t * data, uint8_t count) { .....}

void app_main(void)
{
	sensor160d_t * sensor = malloc(sizeof(sensor_t));
	SENSOR_DEFAULT_STATIC_SENSOR_STRUCT(sensor, 32,19,init,write,read)
	.....
And every time I do a call to the library that requires accessing init write or read the function is not called and returns the value -1 for some reason, or even worse the program crashes with a fetch error. Indicating that the function is not being called, which does not make any sense because it can access all the other fields in snesor_i2c_t struct.

Any tip, help or something that I'm missing?
Much appreciated.
Last edited by McDottie on Mon May 15, 2023 3:29 pm, edited 1 time in total.

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: Having major problems with my own handlers for my libraries

Postby boarchuz » Mon May 15, 2023 12:05 pm

Possibly due to sensor_internal_i2c being a stack variable. Do you delete app_main's task, or does sensor_internal_i2c go out of scope?

McDottie
Posts: 7
Joined: Fri Nov 05, 2021 11:18 am

Re: Having major problems with my own handlers for my libraries

Postby McDottie » Mon May 15, 2023 12:56 pm

boarchuz wrote:
Mon May 15, 2023 12:05 pm
Possibly due to sensor_internal_i2c being a stack variable. Do you delete app_main's task, or does sensor_internal_i2c go out of scope?
Hi, thanks for the response. I tried allocating memory fo the snesor_internal_i2c and the same problem arises. I do belive it means I'm out of scope but can't really understand why. The call to "init" works but write and read does not.
I have come to the conclusion that this only appends on the write and read functions for some reason
PS: I do not delete app_main task.

MicroController
Posts: 1732
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Having major problems with my own handlers for my libraries

Postby MicroController » Mon May 15, 2023 5:13 pm

First, I suggest you switch to C++. It makes life much easier, especially since you are manually re-implementing classes, constructors and virtual methods/member functions at the moment.

Then, sensor_i2c_t sensor_internal_i2c is a local variable in app_main to which you store a pointer. That pointer becomes invalid when app_main returns.

Of course, there may also be a bug in the library code or in the read/write/init functions.

Who is online

Users browsing this forum: MicroController and 124 guests