<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Transfer object through different .c files</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/73882/transfer-object-through-different-c-files</link><description>My project starts to grow and I would like to separate different parts of code into modules. Each module will present one .c file together with its .h file. In some .c files, I have to have the instance of the timer_id or struct which is defined in .h</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 13 Apr 2021 13:42:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/73882/transfer-object-through-different-c-files" /><item><title>RE: Transfer object through different .c files</title><link>https://devzone.nordicsemi.com/thread/304580?ContentTypeID=1</link><pubDate>Tue, 13 Apr 2021 13:42:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:71637d33-c569-493c-bef0-eb2427b75749</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;As you note the app timer is created using the APP_TIMER_DEF that does a few things, including making a&amp;nbsp;app_timer_id_t instance with the name you provide to&amp;nbsp;APP_TIMER_DEF This is the same regardless of which app timer implementation you use, but I show both here from SDK 17.0.2 for reference:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#ifdef APP_TIMER_V2
/**
 * @brief app_timer control block
 */
typedef struct
{
    nrf_sortlist_item_t         list_item;     /**&amp;lt; Token used by sortlist. */
    uint64_t                    end_val;       /**&amp;lt; RTC counter value when timer expires. */
    uint32_t                    repeat_period; /**&amp;lt; Repeat period (0 if single shot mode). */
    app_timer_timeout_handler_t handler;       /**&amp;lt; User handler. */
    void *                      p_context;     /**&amp;lt; User context. */
    NRF_LOG_INSTANCE_PTR_DECLARE(p_log)        /**&amp;lt; Pointer to instance of the logger object (Conditionally compiled). */
    volatile bool               active;        /**&amp;lt; Flag indicating that timer is active. */
} app_timer_t;

/**@brief Timer ID type.
 * Never declare a variable of this type, but use the macro @ref APP_TIMER_DEF instead.*/
typedef app_timer_t * app_timer_id_t;

#define _APP_TIMER_DEF(timer_id)                                                              \
    NRF_LOG_INSTANCE_REGISTER(APP_TIMER_LOG_NAME, timer_id,                                   \
                              APP_TIMER_CONFIG_INFO_COLOR,                                    \
                              APP_TIMER_CONFIG_DEBUG_COLOR,                                   \
                              APP_TIMER_CONFIG_INITIAL_LOG_LEVEL,                             \
                              APP_TIMER_CONFIG_LOG_ENABLED ?                                  \
                                         APP_TIMER_CONFIG_LOG_LEVEL : NRF_LOG_SEVERITY_NONE); \
    static app_timer_t CONCAT_2(timer_id,_data) = {                                           \
            .active = false,                                                                  \
            NRF_LOG_INSTANCE_PTR_INIT(p_log, APP_TIMER_LOG_NAME, timer_id)                    \
    };                                                                                        \
    static const app_timer_id_t timer_id = &amp;amp;CONCAT_2(timer_id,_data)

#else //APP_TIMER_V2
typedef struct app_timer_t { uint32_t data[CEIL_DIV(APP_TIMER_NODE_SIZE, sizeof(uint32_t))]; } app_timer_t;

/**@brief Timer ID type.
 * Never declare a variable of this type, but use the macro @ref APP_TIMER_DEF instead.*/
typedef app_timer_t * app_timer_id_t;

#define _APP_TIMER_DEF(timer_id)                                      \
    static app_timer_t CONCAT_2(timer_id,_data) = { {0} };           \
    static const app_timer_id_t timer_id = &amp;amp;CONCAT_2(timer_id,_data)

#endif&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So you could declare the app_timer_id_t instance as extern if it was not for the fact that it is static (meaning you have to modify the SDK code if you do it).&lt;/p&gt;
&lt;p&gt;While the above is possible it is perhaps not so clean as it interferes with the SDK code, so a better solution could perhaps be to use the app_timer only in one file, either in your main.c or another file. And if you need to access it, use access functions instead of the app_timer API, so that the actual app_timer API is only called from one file. This would be a mater of preference and how you like to organize your C code more than specific to the nRF5 SDK per se.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>