Write code independent of component name

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DavideM
Level 3
Level 3
10 replies posted 5 replies posted 5 questions asked

Hi all,

I'm still new to PSoC (and PSoC Creator) world: I would like to port some libraries that I wrote for STM32 MCU to PSoC, but I have some doubts about it.

The libraries that I wrote are intended to be used with MEMS sensors (temperature, pressure, and humidity) which communicate with the MCU over I2C. When I wrote them for STM32 MCU, I wanted them to be independent of the I2C interface chosen (I2C1, I2C2, I2C3,..), so that the programmer could choose which I2C to be used. Therefore, my code looked like this:

// Function prototype - I2C_TypeDef pointer

void readTemperature(I2C_TypeDef *I2C, SensorStruct sensor);

int main(void)

{

     SensorStruct mySensor;

     I2C_Init(I2C1);

    

     while(1)

     {

          read_temperature(I2C1,&mySensor);

          // Do some stuff

     }

}

In PSoC, can I do something similar? My problem right now is that, if I write a library, I'll need to use some I2C methods, but these methods have to be called with the name of the component. So if I change the name of the component, I need to change it everywhere in my code, as follows:

// Programmer used I2C component naming it I2C

I2C_MasterSendStart(SLAVE_ADDRESS, READ);

// Programmer used I2C component naming it I2C_1

I2C_1_MasterSendStart(SLAVE_ADDRESS, READ);

What kind of approach do you suggest me?

Thank you in advance,

Davide

0 Likes
1 Solution
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Davide,

One way to do it with PSoC is to encapsulate your library into a custom component. Inside the component you can have a schematic with stock I2C, named e.g. I2C_1 and API (library code).  Then inside component API code you can refer to this I2C unique name like this: `$INSTANCE_NAME`_I2C_1. Whenever the component name is changed, the I2C name will update automatically.

For component creation look into

http://www.cypress.com/documentation/component-datasheets/psoc-creator-component-author-guide

/odissey1

View solution in original post

0 Likes
3 Replies
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Davide,

One way to do it with PSoC is to encapsulate your library into a custom component. Inside the component you can have a schematic with stock I2C, named e.g. I2C_1 and API (library code).  Then inside component API code you can refer to this I2C unique name like this: `$INSTANCE_NAME`_I2C_1. Whenever the component name is changed, the I2C name will update automatically.

For component creation look into

http://www.cypress.com/documentation/component-datasheets/psoc-creator-component-author-guide

/odissey1

0 Likes

Hi odissey,

thank you for your reply. I'm trying to build a custom component integrating a stock I2C in the component schematic, but my doubt is that, doing this, the stock I2C of the custom component cannot be used for other purposes: is it right?

Thanks,

Davide

0 Likes

Davide,

Yes this is the case. Once a component is Incorporated it is no longer useful for other tasks (AFAIK).

/odissey1

0 Likes