cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Bus

sheva94140
Associate
Posted on April 10, 2012 at 15:31

I try to use an exemple by KEIL, but when i try to debug it doesn't work,

my main is : ''/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************

* File Name          : main.c

* Author             : MCD Application Team

* Version            : V2.0

* Date               : 12/07/2007

* Description        : Main program body

********************************************************************************

* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH

* CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS

* A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT

* OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT

* OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION

* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

*******************************************************************************/

/* Standard include ----------------------------------------------------------*/

#include ''91x_lib.h''

/* Include of other module interface headers ---------------------------------*/

/* Local includes ------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

canmsg RxCanMsg;

canmsg CanMsg;

GPIO_InitTypeDef    GPIO_InitStructure;

CAN_InitTypeDef     CAN_InitStructure;

  /* buffer for receive messages */

  canmsg RxCanMsg;

  /* used message object numbers */

  enum {

    CAN_TX_MSGOBJ = 0,

    CAN_RX_MSGOBJ = 1

  };

  /* array of pre-defined transmit messages */

  canmsg TxCanMsg[3] = {

    { CAN_STD_ID,      0x123, 4, { 0x01, 0x02, 0x04, 0x08 } },

    { CAN_STD_ID,      0x321, 4, { 0xAA, 0x55, 0xAA, 0x55 } },

    { CAN_EXT_ID, 0x12345678, 8, { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 } }

  };

/* Private functions ---------------------------------------------------------*/

void SCU_Configuration(void)

{

  SCU_MCLKSourceConfig(SCU_MCLK_OSC);

  SCU_PCLKDivisorConfig(SCU_PCLK_Div2);

  SCU_PLLFactorsConfig(128,25,4);

  SCU_PLLCmd(ENABLE);

  SCU_MCLKSourceConfig(SCU_MCLK_PLL);

  SCU_APBPeriphClockConfig(__CAN, ENABLE);

  SCU_APBPeriphClockConfig(__GPIO0, ENABLE);

  SCU_APBPeriphClockConfig(__GPIO1, ENABLE);

  SCU_APBPeriphClockConfig(__GPIO3, ENABLE);

  SCU_APBPeriphClockConfig(__GPIO5, ENABLE);

  SCU_APBPeriphClockConfig(__GPIO9, ENABLE);

  SCU_AHBPeriphClockConfig(__VIC, ENABLE);

  SCU_APBPeriphReset(__CAN, DISABLE);

  SCU_APBPeriphReset(__GPIO0, DISABLE);

  SCU_APBPeriphReset(__GPIO1, DISABLE);

  SCU_APBPeriphReset(__GPIO3, DISABLE);

  SCU_APBPeriphReset(__GPIO5, DISABLE);

  SCU_APBPeriphReset(__GPIO9, DISABLE);

  SCU_AHBPeriphReset(__VIC, DISABLE);

}

void IO_Init(void)

{

/* P5.0 alternate input 1, CAN_RX pin 12*/

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;

GPIO_InitStructure.GPIO_Direction=GPIO_PinInput;

GPIO_InitStructure.GPIO_IPInputConnected=GPIO_IPInputConnected_Enable;

GPIO_InitStructure.GPIO_Alternate=GPIO_InputAlt1;

GPIO_Init(GPIO5,&GPIO_InitStructure);

/* P5.0 alternate output 2, CAN_TX */

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;

GPIO_InitStructure.GPIO_Direction=GPIO_PinOutput;

GPIO_InitStructure.GPIO_Type=GPIO_Type_PushPull;

GPIO_InitStructure.GPIO_IPInputConnected=GPIO_IPInputConnected_Disable;

GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt2;

GPIO_Init(GPIO5,&GPIO_InitStructure);

}

void CAN_Com_LoopBack(void)

{

  /* initialize the CAN at a standard bitrate, interrupts disabled */

  CAN_InitStructure.CAN_ConfigParameters=0x0;

  CAN_InitStructure.CAN_Bitrate=CAN_BITRATE_1M;

  CAN_Init(&CAN_InitStructure);

  /* switch into Loopback+Silent mode (self-test) */

  CAN_EnterTestMode(CAN_TESTR_LBACK | CAN_TESTR_SILENT);

  /* configure the message objects */

  CAN_SetUnusedAllMsgObj();

  CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID, DISABLE);

  CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);

  /* Send the pre-defined answer */

  CAN_SendMessage(CAN_TX_MSGOBJ, &TxCanMsg[1]);

  /* wait until end of transmission */

  CAN_WaitEndOfTx();

  /* wait for reception of a data frame */

  while (!CAN_ReceiveMessage(CAN_RX_MSGOBJ, FALSE, &RxCanMsg))

  {

    /*Add Timer*/

  }

  /* Test Received Msg */

  if((RxCanMsg.IdType == CAN_STD_ID)&&(RxCanMsg.Id == 0x321)&&(RxCanMsg.Dlc == 4)

    &&(RxCanMsg.Data[0]==0xAA)&&(RxCanMsg.Data[1]==0x55)&&(RxCanMsg.Data[2]==0xAA)&&(RxCanMsg.Data[3]==0x55)){

    /*Received Msg OK*/

  } else {

    /*Received Msg KO*/

  }

  /* release the message objects */

  CAN_ReleaseTxMessage(CAN_TX_MSGOBJ);

  CAN_ReleaseRxMessage(CAN_RX_MSGOBJ);

  /* switch back into Normal mode */

  CAN_LeaveTestMode();

}

/*******************************************************************************

* Function Name  : main

* Description    : Main program

* Input          : None

* Output         : None

* Return         : None

*******************************************************************************/

int main(void)

{

 

  SCU_Configuration();/*PCLK=8MHz*/

  IO_Init();

  /* CAN tests */

  CAN_Com_LoopBack();/* send and receive some frame */

 

  while(1);

}

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

''

you can see my debug results in the attach file

Best regards

 
1 REPLY 1
nick239955_st
Associate
Posted on November 12, 2012 at 10:53

I spoted that in your code you use pin 0 twice. On the MCB-STR9 the output pin is pin 0.