Thread: How to "share" (extern) a structure in my code?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    How to "share" (extern) a structure in my code?

    I'm writing a driver for communicating with a DAC, this will run on an ARM processor. The SPI communication initializes a structure called hspi3 and it hold various settings for the SPI hardware. I don't care much about this structure, however the command I use for sending a byte over SPI needs to have access to this structure.

    Here is the SPI sending command:
    Code:
    HAL_SPI_Transmit(&hspi3,(uint8_t*)aTxBuffer,3,1000);
    The 1st argument is the structure.

    Now here is the problem. My driver library is located in a seperate C file with its own header. My code in that C file can not see the structure that is generated in init code placed in the main C file because it is defined as follows:
    Code:
    SPI_HandleTypeDef hspi3;
    How can I make this structure visible in my library?

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Deleted (I just realized my answer was irrelevant to the question)

    So, on a second attempt from my part, declaring: extern SPI_HandleTypeDef hspi3; in your library does not work?
    Last edited by migf1; 06-05-2015 at 03:39 AM.
    "Talk is cheap, show me the code" - Linus Torvalds

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Of course that should work, however the code including the deceleration statement is automatically generated by a software and in case I will change settings it will be re-written. Can I somehow make a pointer to that structure external and just use that?

  4. #4
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Unfortunately I'm not familiar with your codebase, nor the SPI. Isn't the type SPI_HandleTypeDef publicly exposed, via a header for example? If so, your library could use arguments of that type in the functions that need to manipulate objects of that type.
    "Talk is cheap, show me the code" - Linus Torvalds

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Actually it does not matter what SPI is... The structure type is SPI_HandleTypeDef and it is defined in a header which indeed has public privileges. The thing is that I need to use the specific instance that is created in main, which I showed before, declared as private. I somehow need to be able to point at it. I guess an option would be to create a copy of the structure but since all structure manipulation is taken care of by the SPI library I can't be sure that my copy is up to date and I don't want to modify the SPI library or write a function that would be called repeatedly to compare my copy structure with the original one.
    Last edited by mr_monster; 06-05-2015 at 05:08 AM.

  6. #6
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by mr_monster View Post
    Actually it does not matter what SPI is... The structure type is SPI_HandleTypeDef and it is defined in a header which indeed has public privileges. The thing is that I need to use the specific instance that is created in main, which I showed before, declared as private. I somehow need to be able to point at it. I guess an option would be to create a copy of the structure but since all structure manipulation is taken care of by the SPI library I can't be sure that my copy is up to date and I don't want to modify the SPI library or write a function that would be called repeatedly to compare my copy structure with the original one.
    I kinda lost you!

    Since SPI_HandleTypeDef is a public type, which you instantiate in your main.c file, what stops you from passing a pointer to that instant as an argument to a function of your own library? Your library function gets access directly to that particular instance of SPI_HandleTypeDef.

    Code:
    // main.c
    #include "spi.h"
    #include "mylib.h"
    ...
    int main( void )
    {
        SPI_HandleTypeDef hspi3;
        mylib_foo( &hspi3, ... );
        ...
    }
    Code:
    // mylib.h
    ...
    #include "spi.h"
    ...
    whatever mylib_foo( SPI_HandleTypeDef *, ... );
    ...
    Code:
    // mylib.c
    #include "spi.h"
    ...
    whatever mylib_foo( SPI_HandleTyleDef *handle, ... )
    {
        ...
        return whatever;
    }
    "Talk is cheap, show me the code" - Linus Torvalds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-15-2012, 09:04 AM
  2. Replies: 1
    Last Post: 12-08-2007, 10:49 PM
  3. Replies: 17
    Last Post: 12-15-2006, 11:02 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM