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

Serial broken with Nucleo F303K8 on GCC-ARM #1631

Closed
ducky64 opened this issue Mar 25, 2016 · 7 comments
Closed

Serial broken with Nucleo F303K8 on GCC-ARM #1631

ducky64 opened this issue Mar 25, 2016 · 7 comments

Comments

@ducky64
Copy link
Contributor

ducky64 commented Mar 25, 2016

Deploying code exported to GCC-ARM from the mbed online compiler using Serial causes the microcontroller to freeze and end up in WWDG_IRQHandler().

If compiled against the mbed library (at tag mbed_lib_rev116) from scratch (as opposed to the pre-compiled .a supplied from the online compiler export), stepping through has it failing in
_file = std::fopen(buf, "w+"); (mbed/common/Stream.cpp:24)

The instruction call stack looks like (inside the std::fopen call):
Serial uart(SERIAL_TX, SERIAL_RX);
mbed::Stream::Stream()
_fopen_r()
__sfp()
__sinit()
std.isra.0
where the last instruction executed is:
08005376: str r3, [r0, #0]
(where register r0 = 0, presumably what causes the hardfault)

Note that manually invoking the serial API functions seems to work:

serial_t obj;
serial_init(&obj, SERIAL_TX, SERIAL_RX);
serial_putc(&obj, 'x');

so the root cause of the problem doesn't appear to be the serial HAL, but rather the interaction of the system libraries with ... something ...

I'm using gcc version 5.2.1 20151202 (release) [ARM/embedded-5-branch revision 231848] (GNU Tools for ARM Embedded Processors).

Possibly related to this issue:
https://developer.mbed.org/forum/bugs-suggestions/topic/25983
which is 24 days old without a resolution.

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 26, 2016

@ducky64 I recommend using v4.x for now, however as noted in the link referenced it still valid for gcc v4.x.

cc @bcostm @adustm

@ducky64
Copy link
Contributor Author

ducky64 commented Mar 26, 2016

I attempted to do some debugging (with GCC 5.2, but hopefully results are still applicable), and found:

  • std::fopen fails because malloc fails
  • malloc fails because the static heap pointer (in mbed/common/retarget.cpp: _sbrk) is past the stack (well, actually past the range of valid SRAM addresses)
  • This is also the case with the initial invocation of _sbrk, but presumably whatever called it had more graceful error handling.
  • The static heap pointer in _sbrk is getting clobbered in SystemInit. In particular, NVIC_SetVector in TARGET_NUCLEO_F303K8 thinks there are 101 interrupt vectors (from cmsis_nvic.h), but the linker script only allocated space for 98. This causes the first few bytes of the .data segment to be overwritten.
  • Changing the NVIC number of vectors to 98 solves the problem.

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 26, 2016

Thanks for the write-up ! We will have a look at the fix

@adustm
Copy link
Member

adustm commented Mar 29, 2016

Hello, @ducky64 is right. The define in cmsis_nvic.h is not correct
#define NVIC_NUM_VECTORS 101
should be 98 (checked in datasheet and reference manual for the STM32F303K8)
As it is not aligned with the size defined in linker scripts, it leads to memory overwrite.
Thanks for the investigation. I'll fix this.

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 29, 2016

@adustm look at the referenced pull request (2 comments above) with the fix

@adustm
Copy link
Member

adustm commented Apr 12, 2016

Hello,
This issue has been fixed in PR #1633. Could you close it, please ?
Cheers
Adu

@bcostm
Copy link
Contributor

bcostm commented May 10, 2016

To be closed

@0xc0170 0xc0170 closed this as completed May 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants