Loading...
 

SW4STM32 and SW4Linux fully supports the STM32MP1 asymmetric multicore Cortex/A7+M4 MPUs

   With System Workbench for Linux, Embedded Linux on the STM32MP1 family of MPUs from ST was never as simple to build and maintain, even for newcomers in the Linux world. And, if you install System Workbench for Linux in System Workbench for STM32 you can seamlessly develop and debug asymmetric applications running partly on Linux, partly on the Cortex-M4.
You can get more information from the ac6-tools website and download (registration required) various documents highlighting:

System Workbench for STM32


Defining a Macro to Return the Number of Elements in an Array

As a convenience I attempted to define this macro for providing the number of elements in a static array of an arbitrary type:

#define ARSIZE(AR) (sizeof(AR)/sizeof(*AR))

This did not work because the size of operator cannot be directly used in a macro. I have seen here:

http://cs-fundamentals.com/tech-interview/c/implement-sizeof-operator-in-c.phpQuestion

how to create a macro that would work for the numerator of the above #define:

#define NUMSIZE(AR) (char *)(&AR+1) - (char *)(&AR)

But what about the denominator which needs to be the size of the type? Would this work?

#define DENOMSIZE(AR) (char *)(&AR[1]) - (char *)(&AR[0])

If it did work would it still work for a single element array?

Would the above method be more expensive in processing time than giving up on the use of a macro to do this and expanding to the equivalent of (sizeof(AR)/sizeof(*AR) manually?

I use this in OpenStm32 and with other toolsets:

#define COUNT_OF(x) ((sizeof(x)/sizeof(0x)) / ((size_t)(!(sizeof(x) % sizeof(0x)))))


See thisQuestion for more info.


How are you able to get the compiler to accept the sizeof operator in your macro? When I do it I get these errors:

invalid application of ‘sizeof’ to incomplete type ‘const float[]’
invalid application of ‘sizeof’ to incomplete type ‘const uint16_t[]’
invalid application of ‘sizeof’ to incomplete type ‘const uint32_t[]’


The storage size of a float[] would be unknown to the compiler.

I wonder if you’re passing a statically allocated array into a function with a ‘const float[]’ arguement. If so, then, you’re right - that macro would not work in the context of that function. The function could be getting an array of any size and the compiler wouldn’t be able to resolve the size of the array in the context of the function.

Some options:

  • Have that function reference the statically allocated array directly
  • Add an array size parameter to the function arguement list
  • Create (or re-use) a class that knows the size
  • Create a template with a size

I have figured out why the compiler does not accept this. The array is global. Its size and initialization is done in a .c file other than the one this error appears in. It is not sufficient that a header be used to externally declare it. The size and initialization would have to be done in the header, but doing that has caused other problems. I am giving up on doing it this way. I am going to have to define a size variable in the .c file in the next line after the array is defined. For example:

int arr[] = { 1, 2, 3, 4, 5 };
size_t arr_N = sizeof(arr)/sizeof(*arr);

This is the only way to be sure the compiler knows the size of the array in advance of using the sizeof operator.

There is no advantage to using the above NUMSIZE macro over the sizeof operator. That NUMSIZE macro will work only where the sizeof operator works.

In my application I am not going to need the overload checking your COUNT_OF(x) macro provides because I am working only in C, not C++. I can see how it can be useful in C++ though.