Can't call struct on another file or call the function in main code

Hi,

I am planning to create an ultrasonic library (based on ch201) because I can't find any library for it. So I am porting code from ch201 development kit to nrf52 custom board.

So in my main code, I am defining a method for creating initialization just like the development kit does. Below is the header of my main code, as you can see there is 

#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "ch201.h"
#include "ch201_gprmt.h"
#include "EventRecorder.h"

#define LED_PIN 7
#define CH201_SENSOR_FW_INIT_FUNC ch201_gprmt_init

///////////// methods & variables declaration ////////////////////////
uint8_t chirp_error = 0;
uint8_t init_sensor(ch201_device *dev_ptr, ch_fw_init_func_t fw_init_func);
uint8_t start_sensor(ch201_device *dev_ptr);

In my main loop, you can check at the part of sensor initialization, there is a method called ch201_init and has 2 arguments.

int main(void)
{
	EventRecorderInitialize(EventRecordAll, 1);
	printf("init fw\n");
	nrf_gpio_cfg_output(LED_PIN);
	nrf_gpio_pin_clear(LED_PIN);
	nrf_delay_ms(500);
  	
		// I2C initiate
	printf("\nInit i2c");
	twi_init();
	nrf_delay_ms(500);
	
	printf("\nWrite i2c");
	// find sensors
	find_sensors();
	
	// initialize sensors
	/*
	init sensor firmware
	*/
	chirp_error |= ch201_init(&sensor, CH201_SENSOR_FW_INIT_FUNC);
	if (chirp_error != 0) {
		printf("Fail initialize sensor.\n");
		printf("Error: %d\n", chirp_error);
	}

First argument for selecting sensor struct, and second argument is firmware method for initialization process.

Here is the source code for that method.

uint8_t ch201_gprmt_init(ch201_device *dev_ptr, uint8_t i2c_addr, uint8_t io_index, uint8_t i2c_bus_index) {
	
	dev_ptr->part_number = CH201_PART_NUMBER;
	dev_ptr->app_i2c_address = i2c_addr;
	dev_ptr->io_index = io_index;
	dev_ptr->i2c_bus_index = i2c_bus_index;

	/* Init firmware-specific function pointers */
	dev_ptr->firmware 								= ch201_gprmt_fw;
	dev_ptr->fw_version_string				= ch201_gprmt_version;
	dev_ptr->ram_init 								= (uint8_t *) get_ram_ch201_gprmt_init_ptr();
	dev_ptr->get_fw_ram_init_size 		= get_ch201_gprmt_fw_ram_init_size;
	dev_ptr->get_fw_ram_init_addr 		= get_ch201_gprmt_fw_ram_init_addr;

	dev_ptr->prepare_pulse_timer 		= ch_common_prepare_pulse_timer;
	dev_ptr->store_pt_result 			= ch_common_store_pt_result;
	dev_ptr->store_op_freq 				= ch_common_store_op_freq;
	dev_ptr->store_scalefactor 			= ch_common_store_scale_factor;
	dev_ptr->get_locked_state 			= ch_common_get_locked_state;
	
	dev_ptr->api_funcs.fw_load          = ch_common_fw_load;
	dev_ptr->api_funcs.set_mode         = ch_common_set_mode;
	dev_ptr->api_funcs.set_sample_interval  = ch_common_set_sample_interval;
	dev_ptr->api_funcs.set_num_samples  = ch_common_set_num_samples;
	dev_ptr->api_funcs.set_max_range    = ch_common_set_max_range;
	dev_ptr->api_funcs.set_static_range = NULL;								// not supported
	dev_ptr->api_funcs.get_range        = ch_common_get_range;
	dev_ptr->api_funcs.get_amplitude    = ch_common_get_amplitude;
	dev_ptr->api_funcs.get_iq_data      = ch_common_get_iq_data;
	dev_ptr->api_funcs.samples_to_mm    = ch_common_samples_to_mm;
	dev_ptr->api_funcs.set_thresholds   = ch_common_set_thresholds;
	dev_ptr->api_funcs.get_thresholds   = ch_common_get_thresholds;

	/* Init max sample count */
	dev_ptr->max_samples = CH201_GPRMT_MAX_SAMPLES;

	/* This firmware does not use oversampling */
	dev_ptr->oversample = 0;

	/* Init device and group descriptor linkage */
	// dev_ptr->group						= grp_ptr;			// set parent group pointer
	// grp_ptr->device[io_index] 	   		= dev_ptr;			// add to parent group

	return 0;
}

When I compile the code, it gives me error like that method is not defined yet before. But you can check at my header, I call that method from it's file (but the code is not defined at header, but I just make the code works on it is source file).

Below is the code when I compile it without defining it on header file.

..\..\..\main.c(95): error:  #20: identifier "ch201_gprmt_init" is undefined
  	chirp_error |= ch201_init(&sensor, &CH201_SENSOR_FW_INIT_FUNC);

********

So I was planning to define it on header library. But, when I define it on header library, it gives me more errors message than before.

So as you can check in the picture below, now I define it and looks like no errors on it.

But, when I compile, it gives me more errors (4 errors). It is said the ch201_device is not defined, but I define it at my main library code. Below is the error result.

..\..\..\..\..\..\components\libraries\ch201\ch201_gprmt.h(38): error:  #20: identifier "ch201_device" is undefined
  uint8_t ch201_gprmt_init(ch201_device *dev_ptr, uint8_t i2c_addr, uint8_t dev_num, uint8_t i2c_bus_index);

So Is there any solution for this? Thank you for your appreciation.

Parents
  • Hi Imam

    Is ch201_device defined in a different header file than ch201_gprmt.h? 

    If so you need to include that header file at the top of ch201_gprmt.h, so that the ch201_device struct is defined at the point where you declare the ch201_gprmt_init function. 

    For all of this to work make sure you are using include guards, to ensure that you don't define things multiple times (this would cause even more errors). 

    Best regards
    Torbjørn

Reply
  • Hi Imam

    Is ch201_device defined in a different header file than ch201_gprmt.h? 

    If so you need to include that header file at the top of ch201_gprmt.h, so that the ch201_device struct is defined at the point where you declare the ch201_gprmt_init function. 

    For all of this to work make sure you are using include guards, to ensure that you don't define things multiple times (this would cause even more errors). 

    Best regards
    Torbjørn

Children
Related