Having major problems with my own handlers for my libraries
Posted: 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
-----------------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:
The problem here is, I create the structures like below:
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.
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);
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, \
}
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)
.....
Any tip, help or something that I'm missing?
Much appreciated.