Skip to content

[TARGET_STMF0] Fix #1419 baudrateissue #1432

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

Merged
merged 7 commits into from
Jan 4, 2016
Merged

[TARGET_STMF0] Fix #1419 baudrateissue #1432

merged 7 commits into from
Jan 4, 2016

Conversation

adustm
Copy link
Member

@adustm adustm commented Nov 17, 2015

Issue was: stdio printf is ok, serial.printf is failing in case it is initialized as a global variable. But it works on mbed version 107.
Fix: revert to version 107 for systemxx.c, mbed_overrides.c + cmsis_nvic.c
Modify cmsis_nvic.c to use direct addressing instead of a pointer (this pointer is not yet initialized when called from systemxx.c, thus can crash the platform).
Call HAL_Init in mbed_overrides.c for every platforms.

Issue was : stdio printf is ok, serial.printf is failing, in case it is
inialized as a global variable. It works on mbed version 107.
Fix: revert to version 107 for systemxxx.c, mbed_overrides.c +
cmsis_nvic.c.
Modify cmsis_nvic.c to use direct addressing instead of a pointer (this
pointer is not yet initialized when called in systemxx.c)
Call HAL_Init in mbed_overrides.c for every platforms.
@0xc0170
Copy link
Contributor

0xc0170 commented Nov 17, 2015

There's a failure, see https://travis-ci.org/mbedmicro/mbed/builds/91585087

Please separate pull request to a feature-complete set in future. For instance, this is about fixing a baudrate, but there is vtor change, and others. Edit: Looking at the code, and the commit message. I see that its a bugfix. It was not that much clear at first sight :)

@adustm
Copy link
Member Author

adustm commented Nov 17, 2015

Hi,
I will fix the compilation issue and replace 0x03 by its define.

Let me try to explain better.
From mbed rev 107 to mbed rev 108 I had modified SystemInit function because it used to crash.
The call to HAL_Init(), then NVIC_SetVector was using the -not yet initialized- pointer *vectors.
Depending on the value of vectors at this moment (random), it was leading to hard fault error.

I then moved the HAL_Init and SetSysClk functions in mbed_sdk_init() function, when global variables are already initialized.
The side effect of that is the use case :
serial pc(USBTX,UBSRX);
main(){
pc.printf("hello world");
}
pc is a global variable, initialised before the SetSysClk, with a wrong sysclock, then a wrong baud rate.

This is the reason why I came back to the previous implementation of Systemxx.c + mbed_override.c. The modification of NVIC_SetVector is a new bug fix for the not-yet-itilialized *vector pointer.
I am not sure it is a clearer explanation, at least I tried to be exhaustive :)

}
SYSCFG->CFGR1 |= 0x03; // Embedded SRAM mapped at 0x00000000
vtor_remap = 1; // The vectors remap is done
if (NVIC_vtor_remap == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of this global variable, why not to use CFGR1 check if it's MEM_MODE? which is set right below?

@dinau
Copy link
Contributor

dinau commented Nov 30, 2015

I'm looking over these PRs.
I'll add some commets here later.

@0xc0170
Copy link
Contributor

0xc0170 commented Dec 7, 2015

Bump, any progress for this PR?

@dinau
Copy link
Contributor

dinau commented Dec 7, 2015

Hi,
There are some problems in this case.

  1. @adustm has probably misunderstood as to the characteristics of local static variables.
    'static volatile uint32_t vtor_remap = 0' in NVIC_SetVector() is definitely set vtor_remap to zero at first call, just one time.
  2. The real problem is in _start function(in .S) that is translated to _mainCRTStartup function by the linker.
    We can look at _mainCRTStartup function in the list file(
    .lst) generated by 'make lst' command.
  3. The _mainCRTStartup function clears AGAIN '.bss region' in its first half codes.(see below list)
    This is the reason why the vtor_remap value is weirdly cleared after the end of some initializations.

I think we should not use _start call any more because _mainCRTStartup funciton,
its inside is invisible when we are writig around the initialization codes.
Otherwise we would occasionally (or frequently) encounter the same problem in the future.

It is better that we explicitly write the initialization codes being equivalent to _mainCRTStartup except for the codes cleaning .bss region.

I'm more investigating, coding and checking now,
I'll make an another PRs about this problem.

This is _mainCRTStartup function generated after linking by the linker.

08000120 <_mainCRTStartup>:
 8000120:   4b16        ldr r3, [pc, #88]   ; (800017c <_mainCRTStartup+0x5c>)
 8000122:   2b00        cmp r3, #0
 8000124:   d100        bne.n   8000128 <_mainCRTStartup+0x8>
 8000126:   4b14        ldr r3, [pc, #80]   ; (8000178 <_mainCRTStartup+0x58>)
 8000128:   469d        mov sp, r3
 800012a:   2240        movs    r2, #64 ; 0x40
 800012c:   0292        lsls    r2, r2, #10
 800012e:   1a9a        subs    r2, r3, r2
 8000130:   4692        mov sl, r2
 8000132:   2100        movs    r1, #0
 8000134:   468b        mov fp, r1
 8000136:   460f        mov r7, r1
 8000138:   4813        ldr r0, [pc, #76]   ; (8000188 <_mainCRTStartup+0x68>)
 800013a:   4a14        ldr r2, [pc, #80]   ; (800018c <_mainCRTStartup+0x6c>)
 800013c:   1a12        subs    r2, r2, r0
 800013e:   f001 fd8e   bl  8001c5e <memset>
 8000142:   4b0f        ldr r3, [pc, #60]   ; (8000180 <_mainCRTStartup+0x60>)
 8000144:   2b00        cmp r3, #0
 8000146:   d000        beq.n   800014a <_mainCRTStartup+0x2a>
 8000148:   4798        blx r3
 800014a:   4b0e        ldr r3, [pc, #56]   ; (8000184 <_mainCRTStartup+0x64>)
 800014c:   2b00        cmp r3, #0
 800014e:   d000        beq.n   8000152 <_mainCRTStartup+0x32>
 8000150:   4798        blx r3
 8000152:   2000        movs    r0, #0
 8000154:   2100        movs    r1, #0
 8000156:   0004        movs    r4, r0
 8000158:   000d        movs    r5, r1
 800015a:   480d        ldr r0, [pc, #52]   ; (8000190 <_mainCRTStartup+0x70>)
 800015c:   2800        cmp r0, #0
 800015e:   d002        beq.n   8000166 <_mainCRTStartup+0x46>
 8000160:   480c        ldr r0, [pc, #48]   ; (8000194 <_mainCRTStartup+0x74>)
 8000162:   e000        b.n 8000166 <_mainCRTStartup+0x46>
 8000164:   bf00        nop
 8000166:   f001 fd4b   bl  8001c00 <__libc_init_array>
 800016a:   0020        movs    r0, r4
 800016c:   0029        movs    r1, r5
 800016e:   f001 fbe8   bl  8001942 <__wrap_main>
 8000172:   f001 fd2f   bl  8001bd4 <exit>
 8000176:   46c0        nop         ; (mov r8, r8)
 8000178:   00080000    .word   0x00080000
 800017c:   20002000    .word   0x20002000
 8000180:   080011d9    .word   0x080011d9
 8000184:   080011c9    .word   0x080011c9
 8000188:   20000134    .word   0x20000134
 800018c:   200002a8    .word   0x200002a8
    ...

This is _mainCRTStartup before linking by the linker. (equal to crt0.o)

SYMBOL TABLE:
00000000 l    d  .text  00000000 .text
00000000 l    d  .data  00000000 .data
00000000 l    d  .bss   00000000 .bss
00000000 l    d  .ARM.extab 00000000 .ARM.extab
00000000 l    d  .ARM.exidx 00000000 .ARM.exidx
00000000 l    d  .ARM.attributes    00000000 .ARM.attributes
00000000 g     F .text  00000000 _mainCRTStartup
00000000 g     F .text  00000000 _start
00000000         *UND*  00000000 memset
00000000  w      *UND*  00000000 atexit
00000000         *UND*  00000000 __libc_init_array
00000000         *UND*  00000000 main
00000000         *UND*  00000000 exit
00000000  w      *UND*  00000000 __stack
00000000  w      *UND*  00000000 hardware_init_hook
00000000  w      *UND*  00000000 software_init_hook
00000000         *UND*  00000000 __bss_start__
00000000         *UND*  00000000 __bss_end__
00000000  w      *UND*  00000000 __libc_fini_array

00000000 <_mainCRTStartup>:
   0:   4b16        ldr r3, [pc, #88]   ; (5c <_mainCRTStartup+0x5c>)
   2:   2b00        cmp r3, #0
   4:   d100        bne.n   8 <_mainCRTStartup+0x8>
   6:   4b14        ldr r3, [pc, #80]   ; (58 <_mainCRTStartup+0x58>)
   8:   469d        mov sp, r3
   a:   2240        movs    r2, #64 ; 0x40
   c:   0292        lsls    r2, r2, #10
   e:   1a9a        subs    r2, r3, r2
  10:   4692        mov sl, r2
  12:   2100        movs    r1, #0
  14:   468b        mov fp, r1
  16:   460f        mov r7, r1
  18:   4813        ldr r0, [pc, #76]   ; (68 <_mainCRTStartup+0x68>)
  1a:   4a14        ldr r2, [pc, #80]   ; (6c <_mainCRTStartup+0x6c>)
  1c:   1a12        subs    r2, r2, r0
  1e:   f7ff fffe   bl  0 <memset>
            1e: R_ARM_THM_CALL  memset
  22:   4b0f        ldr r3, [pc, #60]   ; (60 <_mainCRTStartup+0x60>)
  24:   2b00        cmp r3, #0
  26:   d000        beq.n   2a <_mainCRTStartup+0x2a>
  28:   4798        blx r3
  2a:   4b0e        ldr r3, [pc, #56]   ; (64 <_mainCRTStartup+0x64>)
  2c:   2b00        cmp r3, #0
  2e:   d000        beq.n   32 <_mainCRTStartup+0x32>
  30:   4798        blx r3
  32:   2000        movs    r0, #0
  34:   2100        movs    r1, #0
  36:   0004        movs    r4, r0
  38:   000d        movs    r5, r1
  3a:   480d        ldr r0, [pc, #52]   ; (70 <_mainCRTStartup+0x70>)
  3c:   2800        cmp r0, #0
  3e:   d002        beq.n   46 <_mainCRTStartup+0x46>
  40:   480c        ldr r0, [pc, #48]   ; (74 <_mainCRTStartup+0x74>)
  42:   f7ff fffe   bl  0 <atexit>
            42: R_ARM_THM_CALL  atexit
  46:   f7ff fffe   bl  0 <__libc_init_array>
            46: R_ARM_THM_CALL  __libc_init_array
  4a:   0020        movs    r0, r4
  4c:   0029        movs    r1, r5
  4e:   f7ff fffe   bl  0 <main>
            4e: R_ARM_THM_CALL  main
  52:   f7ff fffe   bl  0 <exit>
            52: R_ARM_THM_CALL  exit
  56:   46c0        nop         ; (mov r8, r8)
  58:   00080000    .word   0x00080000
    ...
            5c: R_ARM_ABS32 __stack
            60: R_ARM_ABS32 hardware_init_hook
            64: R_ARM_ABS32 software_init_hook
            68: R_ARM_ABS32 __bss_start__
            6c: R_ARM_ABS32 __bss_end__
            70: R_ARM_ABS32 atexit
            74: R_ARM_ABS32 __libc_fini_array

dinau

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 4, 2016

What's the status with this PR?

@adustm
Copy link
Member Author

adustm commented Jan 4, 2016

Hello @0xc0170, happy new year :)
Is the question for me ? If so, the PR is waiting for the merge.
Cheers

0xc0170 added a commit that referenced this pull request Jan 4, 2016
@0xc0170 0xc0170 merged commit a63257b into ARMmbed:master Jan 4, 2016
@adustm adustm deleted the DEV_FIX_1419_baudrateissue branch January 4, 2016 16:41
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.

3 participants