cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to interface to CAN code

ebommer
Associate II
Posted on September 08, 2014 at 18:51

I am trying to assign a value to the IDE bit.  But when I try to write this I am getting a �HardFault_Handler�.  I am sure it has to do with my syntax, but I have been writing requirements for too many years now and not enough code, so any help is appreciated.

My current attempt is:

hcan.pTxMsg->IDE = 0x04;

Cube generated the following structures:

CAN_HandleTypeDef       hcan

typedef struct{

      uint32_t    StdId

      uint32_t    ExtId

      uint32_t    IDE

      uint32_t    RTR

      uint32_t    DLC

uint32_t    Data[8

}CanTxMsgTypeDef;

typedef struct{

      CAN_TypeDef                *Instance

      CAN_InitTypeDef            Init

      CanTxMsgTypeDef            *pTxMsg

      CanRxMsgTypeDef            *pRxMsg

      HAL_LockTypeDef            Lock

      __IO HAL_CAN_StateTypeDef  State

      __IO HAL_CAN_ErrorTypeDef  ErrorCode

}CAN_HandleTypeDef;

#stm32-cube
6 REPLIES 6
Posted on September 08, 2014 at 19:05

And have you set the buffer pointer to actually point at some memory structure or is it NULL?

What is the address of the memory causing the Hard Fault?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ebommer
Associate II
Posted on September 08, 2014 at 19:19

Sorry I am not sure how to set the pointer buffer.  So I am sure that my issue.  Anything you could do to direct me I appreciate.

HardFault = 0x080001A6 

Posted on September 08, 2014 at 19:28

Sorry I am not sure how to set the pointer buffer.

 

CanTxMsgTypeDef Tx[1];

hcan.pTxMsg = &Tx[0];

No, I think that's the address of the instruction, probably after the faulting code, it's not the address of the memory access (the thing it's trying to access). You'd want to look at the assembler code, and the registers in the processor, when it faults.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 08, 2014 at 19:32

printf(''%p\n'', hcan.pTxMsg);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ebommer
Associate II
Posted on September 08, 2014 at 20:08

Thank you very much.  Its going much better.

Just to make sure I understand it.

You create TX as a type CanTxMsgTypeDef, which allocates the memory location.

You then point  hcan.pTxMsg to that memory location so I have a location to write the value for IDE.

I guess old dogs can learn a new trick.

I think the address location you are looking for was:  0x080030B0, at least that's what being loaded from the register into the PC right after I wrote  to the IDE flag and the exception is called.

Posted on September 09, 2014 at 19:36

Hiebommer,

After investigating your code, it turned out that the Hard Fault occurs due the noninitializationof the pTxMsg pointer. You've just to inilialize thepTxMsgpointer, by declaring a global variable or a local static one.

/**
* @brief Configures the CAN.
* @param None
* @retval None
*/
static void CAN_Config(void)
{
CAN_FilterConfTypeDef sFilterConfig;
static CanTxMsgTypeDef TxMessage; // Static variable declaration
static CanRxMsgTypeDef RxMessage;
/*##-1- Configure the CAN peripheral #######################################*/
CanHandle.Instance = CAN1;
CanHandle.pTxMsg = &TxMessage; // Pointer initialization
CanHandle.pRxMsg = &RxMessage;
CanHandle.Init.TTCM = DISABLE;
CanHandle.Init.ABOM = DISABLE;
CanHandle.Init.AWUM = DISABLE;
CanHandle.Init.NART = DISABLE;
CanHandle.Init.RFLM = DISABLE;
CanHandle.Init.TXFP = DISABLE;
CanHandle.Init.Mode = CAN_MODE_NORMAL;
CanHandle.Init.SJW = CAN_SJW_1TQ;
CanHandle.Init.BS1 = CAN_BS1_6TQ;
CanHandle.Init.BS2 = CAN_BS2_8TQ;
CanHandle.Init.Prescaler = 2;
if(HAL_CAN_Init(&CanHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}

To have enough insight into, you can refer toFirmware\Projects\STM324xG_EVAL\Examples\CAN\ CAN_Networkingas a reference example, available

http://www.st.com/web/en/catalog/tools/PF259243#

under the STM32CubeF4 firmware package. Regards.