sprintf error

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

sprintf error

2,586 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by szempy on Fri Sep 18 01:05:18 MST 2015
Hello
I am using LPC1756 Newlib(nohost) in bootloader mode, that means my program begins at address 0x10000 and when i use the sprintf function my program hangs with hardfault and it is giving this error in vectpc :
Faults     = PreciseErr
Fault PC   = 0x0001C27C
           = strlen + 12 in section .text
           = No line number information available for address 0x1c27c <strlen+12>

Is there maybe someone who came across with this error ?
When i don't run my program in bootloader mode it runs smoothly.
Regard.
Labels (1)
0 Kudos
Reply
8 Replies

2,378 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by szempy on Thu Nov 05 00:21:13 MST 2015
void GetDateFromTimestamp(unsigned long time_stamp, RTC_TIME_T * FullTime) {
struct tm * time;
time = gmtime((const time_t *) &time_stamp);

I haven't set the Cortex-M3 UNALIGN_TRP
0 Kudos
Reply

2,378 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Wed Nov 04 11:38:35 MST 2015
What's the alignment of the parameter you are passing to gmtime?

And have you turned the Cortex-M3 UNALIGN_TRP on in your system?

Regards,
LPCXpresso Support
0 Kudos
Reply

2,378 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by szempy on Wed Nov 04 04:52:34 MST 2015
I got the problem again now from another part when i called the gmtime function

Before the hardfault exception: http://prntscr.com/8ywa8z
r00x00000027
r10x0000007B
r20x0000165E
r30x00000003
r40x00001661
r50x00000028
r60x0000000D
r70x00000258
r80xB6DB7DB3
r90xFFFFF668
r100xF7368B72
r110x1FCF61B4
r120x00000000
sp0x10003F6C
lr0x00000CC9
pc0x000188A8
psr0x21000000
flagsnzCvq
epsrnone
ipsr0  (Base)
psp0x27ECE1DC
control0x00000000
cycle0x00000000
cycledelta0x00000000
vectpcn/a
faultsn/a
basepri0x00000000
primask0x00000000
faultmask0x00000000

After the hardfault: http://prntscr.com/8ywah9
r00x00000027
r10x0000007B
r20x0000165E
r30x00000003
r40x00001661
r50x00000028
r60x0000000D
r70x00000258
r80xB6DB7DB3
r90xFFFFF668
r100xF7368B72
r110x1FCF61B4
r120x00000000
sp0x10003F48
lr0xFFFFFFF9
pc0x000100F6
psr0x21000003
flagsnzCvq
epsrnone
ipsr3  (HardFault)
psp0x27ECE1DC
control0x00000000
cycle0x00000000
cycledelta0x00000000
vectpc0x000188A8
faultsUnaligned
basepri0x00000000
primask0x00000000
faultmask0x00000000

Faults     = Unaligned
Fault PC   = 0x000188A8
           = gmtime_r + 112 in section .text
           = No line number information available for address 0x188a8 <gmtime_r+112>

Fault status registers:
IPSR       = 0x21000003: 3  (HardFault)
CFSR       = 01000000 (Configurable Fault Status Register)
HFSR       = 40000000 (Hard Fault Status Register)
DFSR       = 00000000 (Debug Fault Status Register)
MMAR       = e000edf8 [INVALID] (MemManage Fault Address Register)
BFAR       = e000edf8 [INVALID] (Bus Fault Address Register)
AFSR       = 00000000 (Auxiliary Fault Status Register)

Stacked registers:
R0         = 00000027
R1         = 0000007b
R2         = 0000165e
R3         = 00000003
R12        = 00000000
LR         = 00000cc9
PC         = 000188a8
PSR        = 21000200
SP         = 10003f48

The dissasembly:
00018874:   ldr r5, [pc, #276]      ; (0x1898c <gmtime_r+340>)
00018876:   ldr r3, [pc, #280]      ; (0x18990 <gmtime_r+344>)
00018878:   smull r4, r5, r5, r0
0001887c:   adds r4, r2, #3
0001887e:   smull r8, r9, r4, r3
00018882:   add r5, r0
00018884:   asrs r3, r0, #31
00018886:   rsb r5, r3, r5, asr #5
0001888a:   add.w lr, r4, r9
0001888e:   asrs r3, r4, #31
00018890:   rsb r3, r3, lr, asr #2
00018894:   rsb r3, r3, r3, lsl #3
00018898:   subs r3, r4, r3
0001889a:   rsb r7, r5, r5, lsl #4
0001889e:   it mi
000188a0:   addmi r3, #7
000188a2:   sub.w r0, r0, r7, lsl #2
000188a6:   cmp r2, #0
000188a8:   stmia.w r1, {r0, r5, r6}
0 Kudos
Reply

2,378 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Tue Sep 22 00:05:03 MST 2015
And where is the stack pointer pointing when you hit the breakpoint at sprintf() ?

To be honest, you may well need to switch down to instruction level stepping, and identify what the exact instruction triggering the fault actually is.

https://www.lpcware.com/content/faq/lpcxpresso/disassembly-view

Regards,
LPCXpresso Support
0 Kudos
Reply

2,378 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by szempy on Mon Sep 21 02:15:25 MST 2015
I used the bootloader from Code Red Technologies - Minor modifications to original NXP AN10866 example code for use in RDB1768 secondary USB bootloader based on LPCUSB USB stack. and it seams that they didn't change the stack pointer.
this was the code that they used :
void execute_user_code(void) {
void (*user_code_entry)(void);

unsigned *p;// used for loading address of reset handler from user flash

/* Change the Vector Table to the USER_FLASH_START 
 in case the user application uses interrupts */

SCB ->VTOR = (USER_FLASH_START& 0x1FFFFF80);

// Load contents of second word of user flash - the reset handler address
// in the applications vector table
p = (unsigned *)(USER_FLASH_START+4);

// Set user_code_entry to be the address contained in that second word
// of user flash
user_code_entry = (void *)*p;

if((unsigned int)user_code_entry>USER_FLASH_START){
// Jump to user application
user_code_entry();
} else {
// The FW is not good.
}

}

I have changed it to this :
void execute_user_code(void) {
void (*user_code_entry)(void);

unsigned *p;// used for loading address of reset handler from user flash

/* Change the Vector Table to the USER_FLASH_START 
 in case the user application uses interrupts */

SCB ->VTOR = (USER_FLASH_START& 0x1FFFFF80);

// Load contents of second word of user flash - the reset handler address
// in the applications vector table
p = (unsigned *)(USER_FLASH_START);

// Set stack pointer to given address
__set_MSP(*p);

p++;

// Set user_code_entry to be the address contained in that second word
// of user flash
user_code_entry = (void *)*p;

if((unsigned int)user_code_entry>USER_FLASH_START){
// Jump to user application
user_code_entry();
} else {
// The FW is not good.
}

}

But the sprintf doesn't work..
The r0 - r3 registers have the correct values.
0 Kudos
Reply

2,378 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Sat Sep 19 10:27:26 MST 2015
I can't replicate any problem here. Thus I assume your problem is something to do with the way your boot loader sets things up before vectoring onto your application.

I suggest setting a breakpoint on the call to sprint / snprintf, and look at the values in the registers (r0-r3 being the parameters being passed into the function, and in particular check the value in the sp - stack pointer).

It might be worth showing us the code in your boot loader that branches off to your application code. There have been lots of previous threads talking about what such code should do, which you might want to look up. Try using the "Search the Forums" box on the right hand side of the window to find them.

Regards
LPCXpresso Support
0 Kudos
Reply

2,378 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by szempy on Fri Sep 18 03:06:59 MST 2015
Thank you for your response.

Quote: lpcxpresso-support
I would imagine the most likely cause of your problem is running out of heap / stack space.



How could i check this ?


Quote: lpcxpresso-support

Also make sure that the buffer that you are passing to sprintf is valid and large enough to take all of the characters you are trying to place in it. You might want to try using snprintf instead to prevent overflow.
LPCXpresso Support



I tried with snprintf too and it gave the same error.
here is a sample from my code:
char TBuf[128];
char * Bufp = &TBuf[0];

and in a function :

snprintf(Bufp, 100, "%d,4", BT_Paired_selected);

0 Kudos
Reply

2,378 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Fri Sep 18 02:26:37 MST 2015
I would imagine the most likely cause of your problem is running out of heap / stack space.

Also make sure that the buffer that you are passing to sprintf is valid and large enough to take all of the characters you are trying to place in it. You might want to try using snprintf instead to prevent overflow.

Regards,
LPCXpresso Support
0 Kudos
Reply