Page 1 of 1

trying to read my first CAN message

Posted: Fri Dec 13, 2013 2:19 am
by russian
I am trying to read my first CAN message and obviously I barely know what I am doing.

I have a SN65HVD230 CAN Board, I have a device sending out CAN messages, I see some activity on logic analyzer

Here is my code

Code: Select all

#define EFI_CAN_DEVICE CAND1
#define EFI_CAN_RX_PORT GPIOB
#define EFI_CAN_RX_PIN 8
#define EFI_CAN_RX_AF 9
#define EFI_CAN_TX_PORT GPIOB
#define EFI_CAN_TX_PIN 9
#define EFI_CAN_TX_AF 9

static void canRead(void) {
   scheduleSimpleMsg(&logger, "waiting for CAN ", 0);
   canReceive(&EFI_CAN_DEVICE, CAN_ANY_MAILBOX, &rxFrame, TIME_INFINITE);
   scheduleSimpleMsg(&logger, "GOT CAN ", rxFrame.FMI);
   scheduleSimpleMsg(&logger, "GOT CAN ", rxFrame.TIME);
   scheduleSimpleMsg(&logger, "GOT CAN ", rxFrame.DLC);
   scheduleSimpleMsg(&logger, "GOT CAN ", rxFrame.SID);
}

void initCan(void) {
   int CAN_Prescaler = 4;// (STM32_PCLK1 / 15) / 500000;        // baudrate is set to 500k bit/s

   int CAN_Mode = CAN_Mode_Normal;
   int CAN_SJW = CAN_SJW_4tq;
   int CAN_BS1 = CAN_BS1_10tq;
   int CAN_BS2 = CAN_BS2_5tq;

   canConfig.btr = (uint32_t)((uint32_t)CAN_Mode << 30) | \
                ((uint32_t)CAN_SJW << 24) | \
                ((uint32_t)CAN_BS1 << 16) | \
                ((uint32_t)CAN_BS2 << 20) | \
               ((uint32_t)CAN_Prescaler - 1);

   canConfig.mcr = (CAN_MCR_NART | CAN_MCR_INRQ);       // init mode, disable auto. retransmission

   canObjectInit(&EFI_CAN_DEVICE);
   canStart(&EFI_CAN_DEVICE, &canConfig);

   mySetPadMode("CAN TX", EFI_CAN_TX_PORT, EFI_CAN_TX_PIN, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
   mySetPadMode("CAN RX", EFI_CAN_RX_PORT, EFI_CAN_RX_PIN, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));

   addConsoleAction("canr", canRead);
}


I am not sure about the prescaler, my STM32_PCLK1=42000000. Ohh, I use stm32f4discovery. I initialize everything with all debug checks and execution goes to canReceive and never returns.

Any hints? Any working CAN example? I would take anything!

Re: trying to read my first CAN message

Posted: Fri Dec 13, 2013 10:51 am
by Giovanni
Hi,

I recommend to start from the CAN demo in ChibiOS, there are several problems there, for example calObjectInit is not required. You just need to remove the loopback mode from the demo and remove the TX thread,

Giovanni

Re: trying to read my first CAN message

Posted: Fri Dec 13, 2013 1:15 pm
by russian
Giovanni wrote:I recommend to start from the CAN demo in ChibiOS

I am hopeless, I cannot find it :( I have searched the 'demos' folder in the 2.6.1 tag for the word 'canStart' and there are no matches.

On the other hand I've just found a relevant folder in testhal, let me try that :)

Re: trying to read my first CAN message

Posted: Fri Dec 13, 2013 1:55 pm
by russian
It was about the config, with the config from the testhal (not demos - I was looking in the wrong folder) without the CAN_BTR_LBKM everything has worked!

Code: Select all

static const CANConfig canConfig = {
  CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
  CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
  CAN_BTR_TS1(8) | CAN_BTR_BRP(6)
};


This gave me
line:105:msg,waiting for CAN 0,msg,GOT CAN 0,msg,GOT CAN 0,msg,GOT CAN 8,msg,GOT CAN 1555,msg,confirmation_canr:4,

Which is ARBID 0x613 I was waiting for!

Once again I have to thank Giovanni for the great support of the great product. I cannot imagine how you find time for both the development and the support.

Re: trying to read my first CAN message

Posted: Sat Dec 14, 2013 5:00 am
by russian
Bingo!
[youtube]_p4o54ZFcKQ[/youtube]

Re: trying to read my first CAN message

Posted: Sat Dec 14, 2013 8:40 am
by Giovanni
Good job :)