[chbot] C question: variables in GCC

Charles Manning cdhmanning at gmail.com
Thu Jun 1 21:23:13 BST 2023


Hi Mark

The C start up code works as you say. There are two main sections normally:
.data* variables initialised to non-zero
.bss*   variables initialised to zero

.data* are initialised by copying initialisation values from flash to RAM
at start.
.bss* are normally just initialised to zero by a loop.

If you want untouched memory then you need some area outside of these and
there are at least two ways to do so.

1) Create a new section in the ld script called uninitilised or something.
Create a variable in C and use #pragmas or _attributes() to place the
variable in the uninitialised section.
2) Create a new section in the ld script at a specific location at a fixed
address (eg top of memory). Access it from C by creating a pointer to the
address.
int *magic_place = ((int *)0x1234);
3) Scratch around your memory map and find an untouched address, then
create a pointer to it. ie. a less controlled version of 2.

I would not recommend (3) because if your code changes and the memory map
changes then bad things could happen.



On Fri, Jun 2, 2023 at 6:48 AM Mark Atherton <markaren1 at xtra.co.nz> wrote:

> Hi All,
>
> I am working on a low-power application for an ATmega328P where the
> watchdog is used to repeatedly restart the machine for one particular
> state.
>
> One of the mechanisms (part of a bootloader) requires that the number of
> WD restarts are counted by a U16.
>
> I am pretty sure that C99 normally only supports initialized variables
> (with associated value) and uninitialized variables (which are set to 0).
>
> Is it possible to declare storage in C that isn’t touched by the
> associated startup code ?
>
> Options seem to be to resort to assembler (and possibly have to do
> battle with the linker script), or to find a couple of unused Read/Write
> peripheral registers within the 328 and use them.
>
> Thoughts please.
>
> Thanks, Mark
>
>
> _______________________________________________
> Chchrobotics mailing list Chchrobotics at lists.ourshack.com
> https://lists.ourshack.com/mailman/listinfo/chchrobotics
> Mail Archives: http://lists.ourshack.com/pipermail/chchrobotics/
> Meetings usually 3rd Monday each month. See http://kiwibots.org for
> venue, directions and dates.
> When replying, please edit your Subject line to reflect new subjects.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ourshack.com/pipermail/chchrobotics/attachments/20230602/ca35eb79/attachment-0001.html>


More information about the Chchrobotics mailing list