AnsweredAssumed Answered

CAN on stm32f303xC processor

Question asked by Vycas.Arturas.001 on Nov 21, 2014
Latest reply on Dec 1, 2014 by peacock.jack.003
Hello everyone. Im having some problems with CAN configuration, but i don't understand what and i hope someone could help me. While i have never tried to implement CAN before this, it didn't look very hard. But i want to ask few questions.
1) Is it possible to see with oscilloscope how CAN works without having a tranceiver chip? I mean should i see something on processor CAN Tx pin by connecting oscilloscope to it?
2) I know that my code doesn't work and it always stucks while trying to leave CAN initalization (see file attached). I am testing now CAN in silent_loopback mode and it still fails ant the same spot. So anyone could help me to understand this and say what i am doing wrong? Maybe i am doing something wrong with time calculations, if that could cause the problem? I am adding a full code. Will appreciate any help.

#include "stm32f30x.h"


RCC_ClocksTypeDef RCC_ClockFreq;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_Filter_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
CanTxMsg TxMessage;
CanRxMsg RxMessage;


void CAN_configuration(void);
void GPIO_configuration(void);
void RCCClocks_configuration(void);
void CAN1_Tx(void);


uint32_t clock;
uint8_t TransmitMailbox;


int main(void)
{
  RCCClocks_configuration();
  GPIO_configuration();
  CAN_configuration();
  CAN1_Tx();
 
 /* Get clock source (used to observe system) */
 clock = RCC_GetSYSCLKSource();
 /* possible clock variable values */
   /* 
     0x00: HSI used as system clock
     0x04: HSE used as system clock  
     0x08: PLL used as system clock
   */
  RCC_GetClocksFreq(&RCC_ClockFreq); // Get clock frequency (used to observe system)


  
  while(1)
  {
  }
}


void CAN_configuration(void)

  /* Reset CAN peripheral registers to default values */
  CAN_DeInit(CAN1);
  CAN_StructInit(&CAN_InitStructure);
  
  /* CAN_BaudRate = CAN_Clock / (CAN_Prescaler * (CAN_SJW + CAN_BS1 + CAN_BS2)) */
  /* Time quantum = CAN_SJW + CAN_BS1 + CAN_BS2 */
  /* Time quantum = 1+10+7 = 18 */
  /* CAN_BaudRate = 36000000 / (16*18) */
  /* CAN_BaudRate = 125kbps */
  
  /* Configure CAN structure */
  /* CAN_Clock = PCLK1_Frequency = 36MHz */
  /* CAN_Prescaler = 36000000/ ( 18*125000 )  = 16 */
  CAN_InitStructure.CAN_Prescaler = RCC_ClockFreq.PCLK1_Frequency / ( 18*125000); // CAN_Prescaler = 16
  CAN_InitStructure.CAN_Mode = CAN_Mode_Silent_LoopBack;
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;  // 1 time quantum
  CAN_InitStructure.CAN_BS1 = CAN_BS1_10tq; // 6 time quantum
  CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;  // 7 quantum time
  CAN_InitStructure.CAN_TTCM = DISABLE;     // Disable TTCM ( Time-triggered communication mode)
  CAN_InitStructure.CAN_ABOM = DISABLE;     // Disable ABOM ( Automatic buss-off managament )
  CAN_InitStructure.CAN_AWUM = DISABLE;     // Disable AWUM ( Automatic wake-up mode )
  CAN_InitStructure.CAN_NART = DISABLE;     // Disable NART ( Non-automatic retransmission mode )
  CAN_InitStructure.CAN_RFLM = DISABLE;     // Disable RFLM ( Receive FIFO locked mode )
  CAN_InitStructure.CAN_TXFP = DISABLE;     // Disable TXFP ( Transmit FIFO priority )
  /* Write values to CAN registers */
  CAN_Init(CAN1, &CAN_InitStructure);
 
  /* CAN filter structure */
  /*  Examine these commands. This configuration sees all data flow. */
  //===================================================================================================
  CAN_Filter_InitStructure.CAN_FilterIdHigh = 0x0000;
  CAN_Filter_InitStructure.CAN_FilterIdLow = 0x0000;
  CAN_Filter_InitStructure.CAN_FilterMaskIdHigh = 0x0000; 
  CAN_Filter_InitStructure.CAN_FilterMaskIdLow = 0x0000;
  CAN_Filter_InitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
  CAN_Filter_InitStructure.CAN_FilterNumber = 0;
  CAN_Filter_InitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; 
  CAN_Filter_InitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
  CAN_Filter_InitStructure.CAN_FilterActivation = ENABLE; 
  
  CAN_FilterInit(&CAN_Filter_InitStructure); // Write values to the registers
  //====================================================================================================
}


void CAN1_Tx(void)
{
  
  TxMessage.StdId = 0x124; // Standard identifier of the message. Value has to be between [0 - 0x7FF]. 11-bits long.
  TxMessage.ExtId = 0x000; // Extended identifier of the message. Value has to be between [0 - 1FFFFFFF]. 29-bits long.
  TxMessage.RTR = CAN_RTR_Data; // RTR - Remote transmission request. CAN_RTR_Data - carries regular data. 
  /* CAN_RTR_Remote - used to request the transmission of a DATA FRAME with the same ID */
  TxMessage.IDE = CAN_Id_Standard; // IDE - identifier. Use standart identifier of the message ( 11-bits ).
  TxMessage.DLC = 8; // Data lenght frame, which will be transmitted. 8 = eight bytes will be transmitted.
  
   volatile unsigned int i;
  
  /* Data to be send on CAN bus. 8 bytes. It ranges from 0 to 0xFF */                     
  TxMessage.Data[0] = 0xAA;
  TxMessage.Data[1] = 0x22;
  TxMessage.Data[2] = 0x33;
  TxMessage.Data[3] = 0x33;
  TxMessage.Data[4] = 0xDD;
  TxMessage.Data[5] = 0xCC;
  TxMessage.Data[6] = 0xBB;
  TxMessage.Data[7] = 0xAA;
  
   TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);


      i = 0;
      while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFFFFFF)) // Wait on Transmit
      {
        i++;
      }
}


void GPIO_configuration(void)
{
  /* Configure CAN Rx and Tx */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;    
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;              // Pin is Input Floating mode
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;            // Output type - Push-pull
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;          // No pull-up resistors
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         // Maximum GPIO toggling speed = 50MHz
  GPIO_Init(GPIOA, &GPIO_InitStructure);                    // Write values to the registers


  /* CAN Tx */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;    
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;              // Pin is in alternate-function mode.
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;            // Output type - Push-pull
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;          // No pull-up resistors
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         // Maximum GPIO toggling speed = 50MHz
  GPIO_Init(GPIOA, &GPIO_InitStructure);      
  
  /* Connect CAN Tx to GPIO Pin */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_9); // CAN1_Tx = PA12
}


void RCCClocks_configuration(void)
{
  /* Enable clocks for PORT A */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 
  /* Enable clocks for CAN1 */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); 
}

Attachments

Outcomes