math.h on 43xx

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

math.h on 43xx

691 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ArriaLive on Sat Nov 08 17:05:17 MST 2014
I'm trying to use some fairly basic math.h functions (including sinf() and cosf()) in a project that is running on an LPC4330.  I am using the FPv4-SP (Soft ABI) floating point library as recommended by these forums.  I can work with float numbers--add, subtract, multiply, divide, etc., but when I add the math.h functions (even in single-precision floating-point mode), I get a hardfault with a NoCP (no coprocessor) fault.

Here's my test code:

/* This one works */
float testfunction(void) {
  float a,b;
  a = 2.5F;
  b = 2.0F*a;
  return b;
}

/* This one gets a hard fault (NoCP) */
float testfunction(void) {
  float a,b;
  a = 2.5F;
  b =sinf(a);
  return b;
}

Any ideas what I'm doing wrong?  Am I missing a parameter somewhere?

Thanks,
Labels (1)
0 Kudos
5 Replies

531 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ArriaLive on Mon Nov 10 08:48:22 MST 2014
Thank you for your replies!


Quote: MikeSimmonds

I presume that you have read http://www.lpcware.com/content/faq/lpcxpresso/cm4-floating-point



Mike, yes, I had read that and had found it quite informative, but it sure felt incomplete--there was obviously something still missing.


Quote: capiman
From DUI0553A_cortex_m4_dgug.pdf:
(see here http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/DUI0553A_cortex_m4_dgug.pdf )

Chapter 3.1.1:
"These instructions are only available if the FPU is included, [color=#f00]and enabled[/color], in the system. See
Enabling the FPU on page 4-52 for information about enabling the floating-point unit."

/<snip>

Something like this before your floating point calculations:

SCB->CPACR = (0x3 << (10*2)) | (0x3 << (11*2));



capiman, this is exactly what I needed.  It sure felt like something needed to be enabled, but the LPC documentation was mum.  It never occurred to me to check for additional information specifically on the ARM processor.  I read the recommended sections, added something like your suggested code, and now I'm running with the FPU enabled, and without hard faults.  Thank you for that pointer!

Thanks again!
0 Kudos

531 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Mon Nov 10 01:16:26 MST 2014

Quote: ArriaLive
I'm trying to use some fairly basic math.h functions (including sinf() and cosf()) in a project that is running on an LPC4330.  I am using the FPv4-SP (Soft ABI) floating point library as recommended by these forums.  I can work with float numbers--add, subtract, multiply, divide, etc., but when I add the math.h functions (even in single-precision floating-point mode), I get a hardfault with a NoCP (no coprocessor) fault.


If you are getting a hard fault, then you are almost certainly not enabling the floating point unit. This is normally done in the LPCOpen initialisation code, or with LPCXpresso, if you are not using LPCOpen, then in the startup file. Thus if you are not using LPCOpen, or you have incorrect compiler/linker options set, then you can run into problems.

If you need further assistance beyond that provided by other forum contributors, it would be helpful to know what toolchain you are actually using, and also for you to post a simple example showing your problem.

Regards,
LPCXpresso Support
0 Kudos

531 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by capiman on Sun Nov 09 04:57:40 MST 2014
From DUI0553A_cortex_m4_dgug.pdf:
(see here http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/DUI0553A_cortex_m4_dgug.pdf )

Chapter 3.1.1:
"These instructions are only available if the FPU is included, [color=#f00]and enabled[/color], in the system. See
Enabling the FPU on page 4-52 for information about enabling the floating-point unit."

Chapter 4.6.6:
"The FPU [color=#f00]is disabled from reset[/color]. You must enable it before you can use any floating-point instructions."

Perhaps have a look at register CPACR:

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/BABDBFBJ.html

0b00 = Access denied. Any attempted access generates a [color=#f00]NOCP[/color] UsageFault.

0b01 = Privileged access only. An unprivileged access generates a [color=#f00]NOCP[/color] fault.

0b10 = Reserved. The result of any access is Unpredictable.

0b11 = Full access.

Something like this before your floating point calculations:

SCB->CPACR = (0x3 << (10*2)) | (0x3 << (11*2));

0 Kudos

531 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MikeSimmonds on Sun Nov 09 01:27:55 MST 2014
Are you building the library from source, or using a pre-compiled library.
If the latter, it may have been compiled for, say, ARM and not for M4.
If the former, check the compiler switches and look for any configuration  #defines.

I don't use FP so just a guess.

I presume that you have read http://www.lpcware.com/content/faq/lpcxpresso/cm4-floating-point

Mike
0 Kudos

531 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ArriaLive on Sat Nov 08 17:20:42 MST 2014
Update:

The first version of my test function above was apparently being optimized out since it always resolved the the same value.  Changed the code to this:

float testfunction(float x) {
float a,b;
a = 1.0F + x;
b = 2.0F*a;
return b;
}

This version fails, with or without the math.h functions, with the same NoCP hard fault.  Does that suggests that I genuinely don't have a math processor in the 4330--contrary to the documentation?  Does that mean that I shouldn't be using the SoftABI floating point library at all--contrary to the documentation?  What am I missing?

Thanks,
0 Kudos