Author Topic: Qustion for C experts - array of IO registers?  (Read 2820 times)

0 Members and 1 Guest are viewing this topic.

Online NorthGuy

  • Super Contributor
  • ***
  • Posts: 3287
  • Country: ca
Re: Qustion for C experts - array of IO registers?
« Reply #25 on: November 30, 2024, 09:17:26 pm »
If you can populate the array at initialization time, making such pointers volatile type *const name and initializing them at variable/object declaration time, in the unit where the functions manipulating the structure are, you can similarly avoid the indirect load and have the functions generate machine code accessing the pointed-to structure directly (using its symbol, not through this structure).

Usually, the reason you use pointers is because you want the same code to work with different entities, different UART modules in this case. In such case the pointer cannot be optimized out.

I did think of an example where the array approach might be very useful: when creating an UART interfacing USB stick, with one serial endpoint being a control endpoint where you can use text commands –– terminal-like –– to configure the UARTs and other endpoints.

Very good example. For each converter, you would have a struct which contains pointers to endpoints, a pointer to UART, all the necessary settings, buffers, callback functions  etc. The C functions would typically take that struct as a parameter. This way, the same code would work for all converters.

In OOP this would be called class instance.
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 4004
  • Country: nl
Re: Question for C experts - array of IO registers?
« Reply #26 on: December 02, 2024, 04:23:40 pm »
You could put the UART registers in an array of structures. When you use C++ you have more options for "syntax sugar". Using C++ instead of C does not have a significant impact on code size as long as you keep to the right "sub set" of C++ (No exceptions, RTTI, be very careful (or avoid) templates). Several of my programs even became smaller when I switched to C++. The biggest disadvantage of C++ is that it's a (much) more complicated language. But I like the use of classes, which guides you into writing more readable and easier to maintain code. But these days I also do not do much programming anymore due to concentration problems.

With C++ you can write a class for a single uart, and then put the implementation specific initialization register names in the constructor. This is easy as long as all UART's have the same layout. If specific bits get swapped or moved to other registers for some of the UART's it becomes more difficult.

Also: There is a typo "Qustion for c experts" in the title of your topic.
« Last Edit: December 02, 2024, 04:34:17 pm by Doctorandus_P »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf