AnsweredAssumed Answered

CAN transmission problem - STM32f429IDiscovery

Question asked by oren.nadav on May 14, 2015
Latest reply on Feb 17, 2017 by kaltofen.jupp
Hi all,

I am trying to send and receive messages from and to the same board with CAN protocol and im having some difficulties..
I use the STM32f29IDiscovery kit with STM32F429I MCU and i'm using a code uploaded to the forum by Clive (the guru :-D) and implementing the bus on a breadboard by a schematic also uploaded by Clive, only difference is im using the TI64HVD251 transceivers.
In LoopBack mode everything is working fine, In normal mode i get this problem:
when im trying to transmit a message a mailbox is assigned and it seems as if the message is sent only the TSR flags don't change - in the "CAN_TransmitStatus" function the switch(state) enters the default case.
anyone can help? i'm working on this for quite a while and searched the forum thoroughly but didn't find an answer.
 
The code (uploaded by Clive with small changes):

#include "main.h"
#include "stm32f4xx_can.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // memset 


uint16_t CAN1_ID;
uint8_t CAN1_DATA0,CAN1_DATA1,CAN1_DATA2,CAN1_DATA3,CAN1_DATA4,CAN1_DATA5,CAN1_DATA6,CAN1_DATA7;
__IO uint8_t Can1Flag;
 
 //****************************************************************************
 
void RCC_Configuration(void)
{
  /* --------------------------- System Clocks Configuration -----------------*/
  /* USART6 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
 
  /* Enable CAN clocks */
  RCC_AHB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);
 
  /* GPIO[B,D] clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD , ENABLE);
}
 
/* CAN GPIOs configuration **************************************************/
void GPIO_Configuration(void){
  
  GPIO_InitTypeDef GPIO_InitStructure;
  
  /* Configure CAN GPIO */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  
  /*Configure Pin D as CAN RX and TX*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_Init(GPIOD, &GPIO_InitStructure);


  /* Connect CAN D pins */
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1);


  /*Configure Pin B as CAN RX and TX*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* Connect CAN B pins */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2);  
}


 
//****************************************************************************
 
void USART6_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
 
  // USARTx configuration ------------------------------------------------------
  // USARTx configured as follow:
  //      - BaudRate = 115200 baud
  //     - Word Length = 8 Bits
  //     - One Stop Bit
  //      - No parity
  //      - Hardware flow control disabled (RTS and CTS signals)
  //      - Receive and transmit enabled
  
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
  USART_Init(USART6, &USART_InitStructure);
 
  USART_Cmd(USART6, ENABLE);
}
 
//****************************************************************************
 
void CAN_Configuration(void)
{
  RCC_ClocksTypeDef     RCC_Clocks;
  CAN_InitTypeDef       CAN_InitStructure;
  CAN_FilterInitTypeDef CAN_FilterInitStructure;
 
  RCC_GetClocksFreq(&RCC_Clocks);
 
  /* CAN configuration ********************************************************/
 
  /* CAN register init */
  CAN_DeInit(CAN1);
  CAN_DeInit(CAN2);
  
  CAN_StructInit(&CAN_InitStructure);
 
  /* CAN cell init */
  CAN_InitStructure.CAN_TTCM = DISABLE;
  CAN_InitStructure.CAN_ABOM = DISABLE;
  CAN_InitStructure.CAN_AWUM = DISABLE;
  CAN_InitStructure.CAN_NART = DISABLE;
  CAN_InitStructure.CAN_RFLM = DISABLE;
  CAN_InitStructure.CAN_TXFP = DISABLE;
  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal ;//CAN_Mode_LoopBack;
 
  /* quanta 1+6+7 = 14, 14 * 24 = 336, 42000000 / 336 = 125000 */
  /* CAN Baudrate = 125Kbps (CAN clocked at 42 MHz) Prescale = 24 */
 
  /* Requires a clock with integer division into APB clock */
 
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // 1+6+7 = 14, 1+14+6 = 21, 1+15+5 = 21
  CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;
  CAN_InitStructure.CAN_Prescaler = RCC_Clocks.PCLK1_Frequency / (14 * 125000); // quanta by baudrate
 
  CAN_Init(CAN1, &CAN_InitStructure);
  CAN_Init(CAN2, &CAN_InitStructure);
 
  /* CAN filter init */
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // IdMask or IdList
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; // 16 or 32
 
  CAN_FilterInitStructure.CAN_FilterIdHigh      = 0x0000; // Everything, otherwise 11-bit in top bits
  CAN_FilterInitStructure.CAN_FilterIdLow       = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh  = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow   = 0x0000;
 
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // Rx
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
 
  CAN_FilterInitStructure.CAN_FilterNumber = 0; // CAN1 [ 0..13]
 
  CAN_FilterInit(&CAN_FilterInitStructure);
 
  CAN_FilterInitStructure.CAN_FilterNumber = 14; // CAN2 [14..27]
 
  CAN_FilterInit(&CAN_FilterInitStructure);
}
 
void CAN2RX(void)
{
  CanRxMsg RxMessage;
  if (CAN_MessagePending(CAN2, CAN_FIFO0))
  {
    memset(&RxMessage, 0, sizeof(RxMessage));
 
    /* receive */
    CAN_Receive(CAN2, CAN_FIFO0, &RxMessage);
 
    printf("RX %04X - %02X - %02X %02X %02X %02X %02X %02X %02X %02X\n",
      RxMessage.StdId, RxMessage.DLC,
      RxMessage.Data[0],RxMessage.Data[1],RxMessage.Data[2],RxMessage.Data[3],
      RxMessage.Data[4],RxMessage.Data[5],RxMessage.Data[6],RxMessage.Data[7]);
  }
}
 
//****************************************************************************
 
void CAN1TX(void)
{
  CanTxMsg TxMessage;
 
  // transmit */
  TxMessage.StdId = 0x123;
  TxMessage.ExtId = 0x00;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;
 
  TxMessage.Data[0] = 0x02;
  TxMessage.Data[1] = 0x11;
  TxMessage.Data[2] = 0x11;
  TxMessage.Data[3] = 0x11;
 
  while(1) // Do not want to exit
  {
    volatile uint32_t i;
    static int j = 0;
    uint8_t TransmitMailbox = 0;
 
    TxMessage.Data[4] = (j >>  0) & 0xFF; // Cycling
    TxMessage.Data[5] = (j >>  8) & 0xFF;
    TxMessage.Data[6] = (j >> 16) & 0xFF;
    TxMessage.Data[7] = (j >> 24) & 0xFF;
    j++; 
    TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
    i = 0;
    while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFFFFFF)) // Wait on Transmit
    {
      i++;
      CAN2RX(); // Pump RX
    }
 
    CAN2RX();
  }
}


//****************************************************************************
int main(void)
{
  RCC_Configuration();

  GPIO_Configuration();

  //USART6_Configuration();
 
  CAN_Configuration();
 
  puts("CAN Test");
 
  CAN1TX();
  
  while(1); // Don't want to exit
}  


Bus implementation schematic:

sn65vd230_can_pd01.png

Thanks for the help.

Outcomes