Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segger RTT: support CONFIG_SEGGER_RTT_INIT_MODE and optimizations #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

giansta
Copy link

@giansta giansta commented Jan 6, 2023

  • Different initialization modes of Control Block
  • Safer INIT() like in SystemView V352a
  • Made _aTerminalId const like in SystemView V352a

Fixes #53544.

@giansta
Copy link
Author

giansta commented Jan 6, 2023

Expected to fix Issue #53544 with PR #53569 in Zephyr repo

@@ -167,6 +167,26 @@ Additional information:
#endif
#endif

#ifndef SEGGER_RTT_MEMCMP
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a new segger version or your modifications?
I'm afraid that those changes may get lost if someone updates with new version of segger_rtt.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my modifications. I was assuming that Segger people follow this repo and approve these changes so that they become they also take them.
If not, what do you suggest? Contact them through their official website?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I look in previous commits, they don't seem to be done by Segger, like my one. Are there some additional rules to coordinate with Segger?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to push it to segger.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @sigvartmh or @carlescufi can help?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Segger is not part of the zephyrproject-rtos Git-Repo.
However, we will review the changes and extend the official RTT sources if we
come to the conclusion that it is a good addition to the current RTT state.
I will keep you posted.

I got this reply after opening a ticket in Segger support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After opening a ticket in support.segger.com, they proposed the update V7.86h that gives equivalent changes and improved initialization. I tried to leave the SEGGER_RTT.c untouched and follow their suggestion to not call at all SEGGER_RTT_Init() in the chained application, but, even if working, I saw that the APIs using use INIT() aren't used in Zephyr log backend. So I added a change only in SEGGER_RTT_Init(), using the configuration I'm aware of that distinguish the cases when we want to to a forced init of CB or just check if it's already done.


#ifndef SEGGER_RTT_MEMSET
#ifdef MEMSET
#define SEGGER_RTT_MEMSET(pDest, FillChar, NumBytes) MEMCPY((pDest), (FillChar), (NumBytes))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy paste error. MEMCPY -> MEMSET

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this, fixed.

@@ -167,6 +167,26 @@ Additional information:
#endif
#endif

#ifndef SEGGER_RTT_MEMCMP
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to push it to segger.

@giansta giansta changed the title Zero-init RTT control block fields if not in BSS, don't init it twice Zero-init RTT control block if not in BSS, don't init it twice Apr 14, 2023
@MarcianoPreciado
Copy link

Can we get this reviewed? This is causing me issues also

@carlescufi
Copy link
Member

@sigvartmh please review this as part of this change
@giansta please rebase since we've updated to RTT 3.40

* Segger code using _DoInit() is used. For applications started by
* bootloader, Segger recommends not calling this function, as all the
* recommended APIs use INIT(). Unfortunately Zephyr log backend uses
* SEGGER_RTT_WriteSkipNoLock() that doesn't call INT(), so we need
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: INIT

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add a dedicated Kconfig option to let user decide if Zephyr application should actually force overwrite of the RTT structures during initialization. The current approach cannot support e.g. other bootloaders.
That would also allow to preserve RTT content during e.g. software reboot that does not clear RAM content.

Copy link
Contributor

@MarekPieta MarekPieta May 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wonder if it wouldn't be reasonable to check whole acID right before initialization (instead of checking just the first byte) to ensure no false positives in case RAM content is not zero-initialized (with current implementation chance of false positive when checking if structure is initialized is quite high - ~1/255).
Actually after we improve the check, we may event consider to always call INIT here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to rely on bootloader to initialize the RTT structures, we need to ensure backwards compatibility of the initialization operation (many embedded applications rely on immutable bootloader that cannot be updated once flashed).

Copy link
Author

@giansta giansta Jun 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestions. Unfortunately I was too busy and I'll be out now for a couple of weeks, but afterwards I'll try to follow them.
The Kconfig option is a good idea, I was not very satisfied with my #ifdefs that are less general. I just have to figure out where such a Kconfig can be placed.
I also agree on the whole acID check, the INIT() macro can be used like it is only for other functions than SEGGER_RTT_Init(). I tried not to touch the SEGGER module, then trying with minimal change, but this is indeed reasonable.
I'll even rebase to RTT 3.40.

Copy link
Author

@giansta giansta Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With latest release I tried to take account of all above suggestions:

  • Rebased to SystemView V340
  • Managed dedicated CONFIG_SEGGER_RTT_INIT_MODE to have different init behaviours

The second change gives more flexibility: application level configs can force initialization of control block without condition or have a complete or partial check on acID to have a contitional init. Yet this requires some more changes in Segger code.

The SystemView V340 code misses some minor changes (safer INIT(), const _aTerminalId) tht are provided by latest SystemView V352a: I've added them, as in previous version.

The CONFIG_SEGGER_RTT_INIT_MODE is defined in PR #53569 in Zephyr repo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your changes looks good :) I've done similar modifications but haven't pushed changes to Zephyr due to having it interop with other parts of downstream Nordic Fork, which I couldn't figure out how to handle in a nice way. However having this upstream would definitely be a good addition.

@giansta giansta changed the title Zero-init RTT control block if not in BSS, don't init it twice Segger RTT: support CONFIG_SEGGER_RTT_INIT_MODE and optimizations Aug 29, 2023
Copy link
Member

@henrikbrixandersen henrikbrixandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally think this looks good, but it could benefit from either a more elaborate Git commit message describing the changes in detail, or more code comments - or both.

@giansta
Copy link
Author

giansta commented Apr 26, 2024

I generally think this looks good, but it could benefit from either a more elaborate Git commit message describing the changes in detail, or more code comments - or both.

I tried to have the commit messages clearer.
With regard to comments, I believe that most code changes are taken from Segger code and they should be quite clear.
The key point is in SEGGER_RTT_Init(), where I wrote a comment explaining the reason of changes.
If you feel that something else could be enhanced, please let me know.

@@ -98,7 +98,7 @@ Revision: $Rev: 24316 $
#define SEGGER_RTT_SECTION ".dtcm_data"
#elif defined(CONFIG_SEGGER_RTT_SECTION_CCM)
#define SEGGER_RTT_SECTION ".ccm_data"
#elif defined(CONFIG_SEGGER_RTT_SECTION_CUSTOM)
#elif defined(CONFIG_SEGGER_RTT_SECTION_CUSTOM) || defined(CONFIG_SEGGER_RTT_SECTION_CUSTOM_DTS_REGION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is CONFIG_SEGGER_RTT_SECTION_CUSTOM_DTS_REGION declared?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this PR depends on PR #53569: there the choice SEGGER_RTT_SECTION in modules/segger/Kconfig has been extended, SEGGER_RTT_SECTION_CUSTOM_NAME and choice SEGGER_RTT_INIT_MODE have been added.

@@ -1892,6 +1893,28 @@ int SEGGER_RTT_SetFlagsDownBuffer(unsigned BufferIndex, unsigned Flags) {
return r;
}

#if defined(CONFIG_SEGGER_RTT_INIT_MODE_STRONG_CHECK)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is CONFIG_SEGGER_RTT_INIT_MODE_STRONG_CHECK declared?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

*/
#if defined(CONFIG_SEGGER_RTT_INIT_MODE_STRONG_CHECK)
SEGGER_RTT_StrongCheckInit();
#elif defined(CONFIG_SEGGER_RTT_INIT_MODE_WEAK_CHECK)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is CONFIG_SEGGER_RTT_INIT_MODE_WEAK_CHECK declared?

Copy link
Contributor

@sigvartmh sigvartmh Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected them to come from here https://github.com/zephyrproject-rtos/zephyr/pull/53569/files

Do we need to add another layer. I think that's only reasonable if this change is expected to be merged into Seggers actual source code.

Please correct me if I'm wrong.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

volatile SEGGER_RTT_CB* p;
p = (volatile SEGGER_RTT_CB*)((char*)&_SEGGER_RTT + SEGGER_RTT_UNCACHED_OFF);
unsigned i;
bool DoInit = false;
Copy link
Member

@cfriedt cfriedt Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to include <stdbool.h> at the top of this file to avoid this error when building against v3.6-branch

SEGGER/SEGGER_RTT.c:1906:3: error: unknown type name 'bool'
 1906 |   bool DoInit = false;

Copy link
Author

@giansta giansta Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cfriedt, for signalling this issue. It probably depends on the toolchain, as I didn't have it.
Yet, looking at Seger code, they seem not to use boolean, so I changed it to int without using false/true values.

RTT__DMB(); // Force order of memory accesses for cores that may perform out-of-order memory accesses
for (i = 0; i < sizeof(_aInitStr) - 1; ++i) {
if (p->acID[i] != _aInitStr[sizeof(_aInitStr) - 2 - i]) { // Skip terminating \0 at the end of the array
DoInit = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar error / need for <stdbool.h>

SEGGER/SEGGER_RTT.c:1913:16: error: 'true' undeclared (first use in this function)
 1913 |       DoInit = true;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

Support Zephyr "RTT Initialization mode" config choice, allowing
Control Block initialization to be retained from another program
(e.g. bootloader).
It includes conditional CB init with full check on Control Block ID or
partial one that checks only the first byte, yet improving its logic.
Minor optimizations with static const added.

Signed-off-by: Giancarlo Stasi <[email protected]>
Allow to define the name of custom RTT section both when it's configured
to be placed at RAM start and when its address is defined by a memory
region in DTS.

Signed-off-by: Giancarlo Stasi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants