component APIs with pointer/reference access approach?

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

cross mob
RaAl_264636
Level 6
Level 6
50 sign-ins 25 sign-ins 10 solutions authored

Hi,

   

 

   

I wonder why the component APIs aren't written for accessing the components "by reference". Instead, for each component the API is 'cloned'. Having the API functions only once would save code space.

   

Is there any special reason for that (e.g. hard to implement or not fail-save, etc.)? In fact, the ARM Cortex architecture is well suited for such an approach since registers must accessed by pointers anyway.

   

 

   

Regards,

   

   

 

   

Ralf

0 Likes
1 Solution
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

No, this would not always save space. Right now the configuration of the components is taken into account when generating code (e.g. when you use a UART but don't need the address matching feature no code is generated). When the code yould get a reference to a data object defining which hardware to use, and how it is configured, all the code needs to be there all the time. Also less optimizations could be done (speed-wise) since all cases must be evaluated all the time.

View solution in original post

0 Likes
4 Replies
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

No, this would not always save space. Right now the configuration of the components is taken into account when generating code (e.g. when you use a UART but don't need the address matching feature no code is generated). When the code yould get a reference to a data object defining which hardware to use, and how it is configured, all the code needs to be there all the time. Also less optimizations could be done (speed-wise) since all cases must be evaluated all the time.

0 Likes
RaAl_264636
Level 6
Level 6
50 sign-ins 25 sign-ins 10 solutions authored

Hi hli,

   

 

   

hmmm... no one keeps Cypress from ORing the component configurations to decide if a given function has to be included or not. And I think in this case space can be saved - for example, the 4100/4200 SCBs in unconfigured state eat >10kB of code space. Even if they'd be configured differently, the space for the common functions could be saved.

   

Of course, it'd be error prone if the developer must specify the pointer to the desired instance, but this could be solved like it's already done by the actual implementation ($INSTANCE_NAME prefix).

   
    

Also less optimizations could be done (speed-wise) since all cases must be evaluated all the time.

   
   

Not sure if I understand what you mean.

   

 

   

Regards,

   

 

   

Ralf

0 Likes
HuEl_264296
Level 5
Level 5
First like given 25 sign-ins First solution authored

This is also something I would like. For example, if I want to control 4 motors, then I need 4 quadrature decoders and 4 PWMs. Now I have to have 4 copies of my code, each identical, except for the names of the components it's talking to. It would be much cleaner if I could have just 1 function which took a pointer to the component.

0 Likes
RaAl_264636
Level 6
Level 6
50 sign-ins 25 sign-ins 10 solutions authored

I agree, even the initialization functions can be created only once. Only the variables marking that the initialization has already be done must be handled per instance.

   

 

   

Regards,

   

 

   

Ralf

0 Likes