Problems with STM32-F103 CAN configuration and functioning

Started by minaise, October 01, 2015, 10:00:50 AM

Previous topic - Next topic

minaise

Hello

Once again I have stumbled with STM32-F103. I am using Olimexino STM32 board and using Eclipse ARM GCC OpenOCD toolchain. I have gone through the uploaded example which is for IAR Embedded Workbench and I have copied all the necessary code. The code is compiling but unfortunately I can't see any CAN messages from the STM32. In the code I am sending message while blinking PB0 output and I can see that while starting the code, it is blinking only four times. After that some buffer is filled? However even with those 4 cycles I can't see any messages.
I have CAN terminator resistors in both ends. CAN bitrate calculation values should be correct for 250kbit/s communication.
For CAN logging I am using IXXAT USB-to-CAN logger and program MiniMon, which could monitor CAN, has baud detection and detect any communication on it (I can also see other CAN devices communication nicely).
Am I missing something or could somebody please give a hint, what to do?


#include <stm32f10x.h>
#include <stdio.h>
#include <misc.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_adc.h>
#include <stm32f10x_i2c.h>
#include <stm32f10x_can.h>



CanTxMsg    TxMessage;
CanRxMsg    RxMessage;
uint8_t     CAN_TxRdy;
uint8_t     CAN_RxRdy;
uint8_t     val_Rx;
int returnedvalue = 0;
/*============================================================================*/
void    Init_CLK    (void)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE);


//internal clock 8mhz
RCC_HSICmd(DISABLE);
// wait until the HSI is ready
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
// 2. Enable ext. high frequency OSC
RCC_HSEConfig(RCC_HSE_ON);
// wait until the HSE is ready
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
// 3. Init PLL
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); // 72MHz
RCC_PLLCmd(DISABLE);
// wait until the PLL is ready
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
// 4. Set system clock dividers
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);

#ifdef EMB_FLASH
FLASH_SetLatency(FLASH_Latency_2);
// Half cycle access
FLASH_HalfCycleAccessCmd(FLASH_HalfCycleAccess_Disable);
// Prefetch buffer
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
#endif

// 5. Clock system from PLL
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

}
/*=============================================================================*/
void   Init_GPIO_CAN   (void)
{
GPIO_InitTypeDef   GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

//Tx
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

//Rx
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE);


}
/*=========================================================================*/
void    Init_CAN            (void)
{
    CAN_InitTypeDef        CAN_InitStructure;
    CAN_FilterInitTypeDef  CAN_FilterInitStructure;


    CAN_DeInit(CAN1);
    CAN_StructInit(&CAN_InitStructure);

    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_InitStructure.CAN_SJW = CAN_SJW_1tq;
    CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq;
    CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
    CAN_InitStructure.CAN_Prescaler = 2;

   
    if (CAN_Init(CAN1, &CAN_InitStructure) == 1){
    returnedvalue = 1;
    }

    CAN_FilterInitStructure.CAN_FilterNumber = 0;
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
    CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FilterFIFO0;
    CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
    CAN_FilterInit(&CAN_FilterInitStructure);


}

/*======================================================================*/
void NVIC_CAN_Configuration(void)
{



#ifndef  EMB_FLASH
// Set the Vector Table base location at 0x20000000
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  // VECT_TAB_FLASH
// Set the Vector Table base location at 0x08009000
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif

NVIC_InitTypeDef  NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel=USB_HP_CAN1_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);


NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStructure);
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);


NVIC_InitStructure.NVIC_IRQChannel=CAN1_SCE_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_Init(&NVIC_InitStructure);
CAN_ITConfig(CAN1, CAN_IT_EWG | CAN_IT_EPV | CAN_IT_LEC | CAN_IT_ERR, ENABLE);


}
/*=============================================================================*/
void USB_HP_CAN1_TX_IRQHandler (void)
{

if (CAN_GetITStatus(CAN1,CAN_IT_TME))
{                 
CAN_ClearITPendingBit(CAN1,CAN_IT_TME);
CAN_TxRdy++;
}
}
/*================================================================================*/
void USB_LP_CAN1_RX0_IRQHandler (void)
{

CanRxMsg RxMessage;

RxMessage.StdId=0x00;
RxMessage.ExtId=0x00;
RxMessage.IDE=0;
RxMessage.DLC=0;
RxMessage.FMI=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;

CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
    CAN_FIFORelease(CAN1, CAN_FIFO0);

}
/*==========================================================================*/
int main (void)
{
volatile uint32_t i=0;
    Init_CLK();
    NVIC_CAN_Configuration();
    Init_GPIO_CAN();
    Init_CAN();
    uint8_t mail;
    uint8_t staatuscan;

    while (1)
    {

        if(i==750000)
        {
// Set PB0
GPIOB->BSRR = 1 << 0;

        TxMessage.StdId = 0x702;
        TxMessage.ExtId = 0x702;
        TxMessage.RTR = CAN_RTR_DATA;
        TxMessage.IDE = CAN_ID_STD;
        TxMessage.DLC = 1;
        TxMessage.Data[0]=0x7F;
        do
        {
            mail = CAN_Transmit( CAN1, &TxMessage );
        }
        while( mail == CAN_NO_MB );

        if (mail == CAN_NO_MB) {
            staatuscan = 1;
        }

        }

        if(i==1500000)
        {
// Reset PB0
GPIOB->BRR  = 1 << 0;
i=0;
        }


i=i+1;
    }
}