CAN Transmit failure on the chibios system

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

Moderator: RoccoMarco

Post Reply
dotaviper
Posts: 10
Joined: Mon Sep 28, 2015 4:44 am
Has thanked: 3 times

CAN Transmit failure on the chibios system

Post by dotaviper »

Hi everybody:
There is no questions of my hardware,i have tested it with standard CAN transmit program. But on the chibios system,i cant detect any signal on the TX pin of the hardware.I think the CAN configurations of my program are correct, so, what is the reason caused the CAN transmit failure? Can you give me some advice or give me the correct CAN transmmit program of the chibios system?
Here is the code i am now running, showing the CAN config:

Code: Select all

/* PA11 - Normal input (CAN RX).
 * PA12 - Normal input (CAN TX).
 */
#define VAL_GPIOACRL            0xA8882AAA      /*  PA7...PA0 */
#define VAL_GPIOACRH            0x888B8AAA      /* PA15...PA8 */
#define VAL_GPIOAODR            0xFFFFFFFF
static const CANConfig cancfg = {
      CAN_MCR_TXFP,
      CAN_BTR_SJW(0)|CAN_BTR_TS2(4)|
      CAN_BTR_TS1(5)|CAN_BTR_BRP(5)
};
/**
 * CAN thread.
 */
static WORKING_AREA(waCANThread,4096);
static msg_t CANThread(void *arg)
{
   (void)arg;

//   EventListener el;
   CANRxFrame rxmsg;
   BaseChannel *seri=(BaseChannel *)&SD1;
   static uint8_t smsg[20]={1},*rmsg;
   unsigned char txr[8]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
while(1)
      {
//         chMtxLock(&can_mtx);
         msg_t result = canReceive(&CAND1, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE);
//         chMtxUnlock();

         if(result==RDY_OK)
         {


            if(rxmsg.EID==0x1fffffff)
            {
               palTogglePad(GPIOB,9);
            }

         }

         palTogglePad(GPIOB, GPIOB_LED_A);
         CANTransmit(0x1fffffff,txr,8);

           chSysLock();
           /* The following function must be called from within a system lock zone. */
           size_t bytesAvailable = chnBytesAvailable(seri);
           chSysUnlock();

         if(bytesAvailable)
         {
            chnRead(seri, rmsg, bytesAvailable);
            palTogglePad(GPIOB,9);

         }

         smsg[0]=0x11;
         chnWrite(seri, smsg, 1);


         chThdSleepMilliseconds(1000);
      }
   return 0;
}



void CANInit(void)
{
   palSetPad(GPIOB,9);
   canObjectInit(&CAND1);//can be delete,it is implicitly invoked by halInit(),
//    chMtxLock(&can_mtx); //
    canStart(&CAND1,&cancfg);  //
    chThdSleepMilliseconds(1000); /* delay: time ms */
//    chMtxUnlock();  //
    sdStart(&SD1, NULL);

    chThdCreateStatic(waCANThread,sizeof(waCANThread),HIGHPRIO,
          CANThread,NULL);  //
}

void CANTransmit(uint32_t id,uint8_t *data,uint8_t len)
{
//   chMtxLock(&can_mtx);

   static CANTxFrame txmsg[10];
   static int txmsg_ind=0;
   txmsg[txmsg_ind].IDE=CAN_IDE_EXT; 
   txmsg[txmsg_ind].EID=id;
   txmsg[txmsg_ind].RTR=CAN_RTR_DATA;
   txmsg[txmsg_ind].DLC=len;       
   memcpy(txmsg[txmsg_ind].data8,data,len);

    canTransmit(&CAND1,1,&txmsg[txmsg_ind],MS2ST(50));
   txmsg_ind++;
   if(txmsg_ind<=10){
      txmsg_ind=0;
   }
}
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: CAN Transmit failure on the chibios system

Post by Giovanni »

Hi,

It is probably an issue with GPIO alternate setup, make sure that CAN functions are only programmed on a single pin. There are also CAN examples under /testhal, try running one of those first.

Giovanni
dotaviper
Posts: 10
Joined: Mon Sep 28, 2015 4:44 am
Has thanked: 3 times

Re: CAN Transmit failure on the chibios system

Post by dotaviper »

thank you for your advice,i will try it and ask you later if have other problems
dotaviper
Posts: 10
Joined: Mon Sep 28, 2015 4:44 am
Has thanked: 3 times

Re: CAN Transmit failure on the chibios system

Post by dotaviper »

Giovanni wrote:Hi,

It is probably an issue with GPIO alternate setup, make sure that CAN functions are only programmed on a single pin. There are also CAN examples under /testhal, try running one of those first.

Giovanni

I have tested using CAN examples of you adviced, It can receiving data in silent mode and working properly in the loopback mode, but it cant detect the signal in the TX pin, can you tell me the probably reasons? As described earlier, the hardware is to be able to work properly. Thank you for your reply.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: CAN Transmit failure on the chibios system

Post by Giovanni »

If the loopback demo works then there are several possible causes:

1) The loopback has not been removed from configuration (not your case).
2) The GPIO has not been correctly programmed (alternate not set or set more than one time on different pins).
3) The CAN pins are used for other things at board level that drives them to 0 or 1.
4) Electrical problems (pullups, drivers).
5) A measurement problem.

Giovanni
avrhack
Posts: 140
Joined: Tue Dec 24, 2013 1:42 pm
Has thanked: 17 times
Been thanked: 3 times

Re: CAN Transmit failure on the chibios system

Post by avrhack »

dotaviper wrote:Hi everybody:
There is no questions of my hardware,i have tested it with standard CAN transmit program. But on the chibios system,i cant detect any signal on the TX pin of the hardware.I think the CAN configurations of my program are correct, so, what is the reason caused the CAN transmit failure? Can you give me some advice or give me the correct CAN transmmit program of the chibios system?
Here is the code i am now running, showing the CAN config:


Can you tell us which device you're using? On an STM32F303 my PAD setup code is as follows:

Code: Select all

   palSetPadMode(config->port, config->rxPad, PAL_MODE_ALTERNATE(config->alternate) | PAL_STM32_PUDR_PULLUP);
       
        palSetPad(config->port, config->txPad);
   palSetPadMode(config->port, config->txPad, PAL_MODE_ALTERNATE(config->alternate) | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_PUSHPULL);


In this case for an F303 config->port is GPIOB, config->rxPad is 8, config->txPad is 9 and config->alternate is 9.

My guess for you is that you've either got the wrong PAL_MODE_ALTERNATE or you haven't set the TX pin to output. If you tell us which device you're using I will cross-check the data sheet as the code above works perfectly on the F303.
dotaviper
Posts: 10
Joined: Mon Sep 28, 2015 4:44 am
Has thanked: 3 times

Re: CAN Transmit failure on the chibios system

Post by dotaviper »

thank you for your reply, I have find the problem is the GPIO has not been correctly programmed

Giovanni wrote:If the loopback demo works then there are several possible causes:

1) The loopback has not been removed from configuration (not your case).
2) The GPIO has not been correctly programmed (alternate not set or set more than one time on different pins).
3) The CAN pins are used for other things at board level that drives them to 0 or 1.
4) Electrical problems (pullups, drivers).
5) A measurement problem.

Giovanni
dotaviper
Posts: 10
Joined: Mon Sep 28, 2015 4:44 am
Has thanked: 3 times

Re: CAN Transmit failure on the chibios system

Post by dotaviper »

my device is stm32f103, and i have find the problem is the GPIO has not been correctly programmed, thank you for your reply,have a nice day
avrhack wrote:
dotaviper wrote:Hi everybody:
There is no questions of my hardware,i have tested it with standard CAN transmit program. But on the chibios system,i cant detect any signal on the TX pin of the hardware.I think the CAN configurations of my program are correct, so, what is the reason caused the CAN transmit failure? Can you give me some advice or give me the correct CAN transmmit program of the chibios system?
Here is the code i am now running, showing the CAN config:


Can you tell us which device you're using? On an STM32F303 my PAD setup code is as follows:

Code: Select all

   palSetPadMode(config->port, config->rxPad, PAL_MODE_ALTERNATE(config->alternate) | PAL_STM32_PUDR_PULLUP);
       
        palSetPad(config->port, config->txPad);
   palSetPadMode(config->port, config->txPad, PAL_MODE_ALTERNATE(config->alternate) | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_PUSHPULL);


In this case for an F303 config->port is GPIOB, config->rxPad is 8, config->txPad is 9 and config->alternate is 9.

My guess for you is that you've either got the wrong PAL_MODE_ALTERNATE or you haven't set the TX pin to output. If you tell us which device you're using I will cross-check the data sheet as the code above works perfectly on the F303.
Post Reply