Skip to main content
muhammet
Associate II
May 12, 2014
Question

Can-Bus Error

  • May 12, 2014
  • 26 replies
  • 7130 views
Posted on May 12, 2014 at 19:56

The original post was too long to process during our migration. Please click on the attachment to read the original post.
    This topic has been closed for replies.

    26 replies

    muhammet
    muhammetAuthor
    Associate II
    May 14, 2014
    Posted on May 15, 2014 at 00:33

    https://drive.google.com/file/d/0B7rfM2PgJKjqSkVDRVQyWkxGVmc/edit?usp=sharing

    https://drive.google.com/file/d/0B7rfM2PgJKjqSkVDRVQyWkxGVmc/edit?usp=sharing

    You can download my source code to the link. I make same schematic with you. Maybe source code have some problem. I use below link tranciever.

    http://www.wvshare.com/product/SN65HVD230-CAN-Board.htm

    Tesla DeLorean
    Guru
    May 16, 2014
    Posted on May 16, 2014 at 21:34

    Ok, this works for me on the

    http://www.ebay.com/itm/RedDragon407-STM32F4-Cortex-M4-Ethernet-wireless-2-4G-SDIO-Serial-camera-/121087293812

    http://propix.com.pl/pl/p/file/c607647297f9dda1e83738404a3c69b2/schemat_f4pdf

    although I found that the CAN2 RX/TX were labelled/connected backward

    // RedDragon 407 - CAN 125 Kbps RX and TX - sourcer32@gmail.com
    // CAN1 PD0 (RX), PD1 (TX) J2, J3 ON
    // CAN2 PB12 (RX), PB13 (TX) board miswired RX/TX switched
    // J9-1 to J10-2, J9-2 to J10-1 fixes routing to SN65VD230
    // CAN1_L to CAN2_L, CAN1_H to CAN2_H, GND already Common
    // USART6 PG9 (RX), PG14 (TX) J13/J14 at 2-3
    #include ''stm32f4xx.h''
    #include <
    stdio.h
    >
    #include <
    stdlib.h
    >
    #include <
    string.h
    > // memset
    //****************************************************************************
    void RCC_Configuration(void)
    {
    /* --------------------------- System Clocks Configuration -----------------*/
    /* USART6 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
    /* Enable CAN clocks */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);
    /* GPIO[B,D,G] clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOG, ENABLE);
    }
    //****************************************************************************
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /*-------------------------- GPIO Configuration ----------------------------*/
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    /* Configure USART RX and TX pins */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_14;
    GPIO_Init(GPIOG, &GPIO_InitStructure);
    /* Connect USART pins to AF */
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource9, GPIO_AF_USART6); // USART6_RX
    GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_USART6); // USART6_TX
    /* Configure CAN RX and TX pins */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    /* Connect CAN pins to AF */
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1); // CAN1_RX
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1); // CAN1_TX
    /* Configure CAN RX and TX pins */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    /* Connect CAN pins to AF */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2); // CAN2_RX
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2); // CAN2_TX
    }
    //****************************************************************************
    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 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;
    /* 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 [.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 %02
    X'',
    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
    }
    //****************************************************************************
    #include <
    rt_misc.h
    >
    #pragma import(__use_no_semihosting_swi)
    struct __FILE { int handle; /* Add whatever you need here */ };
    FILE __stdout;
    FILE __stdin;
    int fputc(int ch, FILE *f)
    {
    while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET);
    USART_SendData(USART6, ch);
    return(ch);
    }
    int fgetc(FILE *f)
    {
    char ch;
    while(USART_GetFlagStatus(USART6, USART_FLAG_RXNE) == RESET);
    ch = USART_ReceiveData(USART6);
    return((int)ch);
    }
    int ferror(FILE *f)
    {
    /* Your implementation of ferror */
    return EOF;
    }
    void _ttywrch(int ch)
    {
    while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET);
    USART_SendData(USART6, ch);
    }
    void _sys_exit(int return_code)
    {
    label: goto label; /* endless loop */
    }
    //****************************************************************************
    #ifdef USE_FULL_ASSERT
    /**
    * @brief Reports the name of the source file and the source line number
    * where the assert_param error has occurred.
    * @param file: pointer to the source file name
    * @param line: assert_param error line source number
    * @retval None
    */
    void assert_failed(uint8_t* file, uint32_t line)
    {
    /* User can add his own implementation to report the file name and line number,
    ex: printf(''Wrong parameters value: file %s on line %d
    
    '', file, line) */
    /* Infinite loop */
    while (1)
    {
    }
    }
    #endif
    //****************************************************************************

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    muhammet
    muhammetAuthor
    Associate II
    May 19, 2014
    Posted on May 19, 2014 at 13:11

    Thank you very very much my code is working now :)))

    johnbroadbent.ky9
    Visitor II
    August 7, 2015
    Posted on August 07, 2015 at 02:59

    This is my first post.

    I want to thank clive1 (the keymaker) for all his help. He is very helpful.

    I am using the stm32f407 discovery board. The code below works, but the bus does not operate at the correct speeds. 

    But if the 

    CAN_Prescaler is set to 3 the messages work at the correct speed.

    CAN_InitStructure.CAN_Prescaler = 3

    Also (as stated else where) it is critical that a can transceiver to connected to can_Rx, and can_Tx pins, for the pins to send messages. The MCP2551 does work as transceiver, but the (AMIS-30663, and the sn65hvd230 are better choices imho).

    Thank you all for your help, and nice support forum 

    John Broadbent

     http://jbr.io/

    Tesla DeLorean
    Guru
    August 7, 2015
    Posted on August 07, 2015 at 04:20

    If timings don't decompose correctly from the internal clock numbers I'd probably look at the HSE_VALUE, the DISCO uses an 8 MHz crystal/source, vs the EVAL boards that use 25 MHz. And double check the PLL settings in the system_stm32f4xx.c

    This often reflects in serial baud rates.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    aubin
    Associate II
    August 7, 2015
    Posted on August 07, 2015 at 11:00

    Hi clive1,

    thanks you for your multiples helps, i'm a new user of STM32F1.  This is my first post related to SPI1 remapped.   I'm using STM32F107VCT6 in my project. I need to remap the SPI1 signals to pins

    PB5 = MOSI

    PA4 = CS

    PA5 = SCK

    PA6 =MISO

    when i send dato, using a scope i see the MOSI waves, the CS but no clk.

    i read STM32F10xxB Errata sheet

    2.8.7 I2C1 with SPI1 remapped and used in master mode

    Conditions

    • I2C1 and SPI1 are clocked.

    • SPI1 is remapped.

    • I/O port pin PB5 is configured as an alternate function output.

    Description

    Conflict between the SPI1 MOSI signal and the I2C1 SMBA signal (even if SMBA is not used).

    Workaround

    Do not use SPI1 remapped in master mode and I2C1 together.

    When using SPI1 remapped, the I2C1 clock must be disabled.

    but when i disable the clock I2C nothing change

    please do yo have any ideas about this ?

    Thanks very much in advance

    Zoubi