trying to read my first CAN message

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
User avatar
russian
Posts: 368
Joined: Mon Oct 29, 2012 3:17 am
Has thanked: 16 times
Been thanked: 14 times

trying to read my first CAN message

Post 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!
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: trying to read my first CAN message

Post 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
User avatar
russian
Posts: 368
Joined: Mon Oct 29, 2012 3:17 am
Has thanked: 16 times
Been thanked: 14 times

Re: trying to read my first CAN message

Post 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 :)
User avatar
russian
Posts: 368
Joined: Mon Oct 29, 2012 3:17 am
Has thanked: 16 times
Been thanked: 14 times

Re: trying to read my first CAN message

Post 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.
User avatar
russian
Posts: 368
Joined: Mon Oct 29, 2012 3:17 am
Has thanked: 16 times
Been thanked: 14 times

Re: trying to read my first CAN message

Post by russian »

Bingo!
[youtube]_p4o54ZFcKQ[/youtube]
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: trying to read my first CAN message

Post by Giovanni »

Good job :)
Post Reply