Float Assignment Causing HardFault
- November 9, 2017
- 1 reply
- 2272 views
Hello. I am trying to control a step motor with a driver like m542 stepper driver. I can control the rotation speed and direction of the motor. I'm working with STM32F4-Discovery to get prototype done. Everything was good when I tried to add some distance control functions. So, the step motor is 200 pulse/rev and I need to control some small size distances and the timers have capabilities about that. I created a struct and it just has five elements one of them is float.
I want to calculate the pulse ratio between motor and driver and send pulses due to the range. But I cannot assign any value to that float ratio element of struct. The core goes hard fault handler when I want to simple assignment to that variable like as; `STRUCT.fVAR = (float)40.0;`. Here is my struct and code which is causing the fault;
typedef enum
{
Z = 0,
Y1 = 1,
Y2 = 2
}MSMD_AxesEnum_t;
typedef struct
{
__IO MSMD_DirectionEnum_t dir;
__IO int speed;
__IO float sensitivity;
__IO uint32_t driverPPR;
__IO uint32_t motorPPR;
}MSMD_TypeDef_t;
__IO MSMD_TypeDef_t MSMD[3]; // Using 3 different motor
static __inline void MSMD_CalculateRatio(MSMD_AxesEnum_t axis)
{
if ((MSMD[axis].motorPPR != 0) && (MSMD[axis].driverPPR != 0))
{
/*
HardFault occurred first here but I didn't understand why then I added a simple assignment line before calling this function in init.
*/
MSMD[axis].sensitivity = ((float)MSMD[axis].motorPPR / (float)MSMD[axis].driverPPR);
}
}
int MSMD_Init(MSMD_AxesEnum_t axis)
{
/**
** Timer and GPIO initializations. They are working well I added the problematic part after check all the functionalities
**/
MSMD[axis].driverPPR = 40000;
MSMD[axis].motorPPR = 200;
MSMD[axis].sensitivity = (float)40.0; // Then HardFault occurred in here again.
MSMD_CalculateRatio(axis);
}
I'm using Keil MDK 5.24 Plus edition and STM32F4-Discovery_FW_V1.1.0. And I checked the Fault Reports then saw the hard fault was forced. I understand that it is forced by another disabled fault handler and the NOCP bit is set in the UsageFault section. I'm adding the Fault Report screen image.
I didn't see any unusual thing on the SFRs and/or SP, PC.
EDIT: When I comment just these lines everything works well.
Thanks.
#stm32f4 #float #hardfault