AnsweredAssumed Answered

STM32F107 CAN1 peripheral doesn't work as CAN2 peri (pin configuration mistake?)

Question asked by tonesk on Mar 25, 2016
Latest reply on Mar 30, 2016 by sonar
Hello to everybody.
I have a problem with my home made project. I'm using the STM32-H107 board of Olimex to create a can network. It mounts an STM32F107VCT6 chip of connectivity line with 2 can peripheral.
I'm using also HAL library STM32Cube_FW_F1_V1.3.0 version.
The problem is that: 
During the configuration of pin of CAN1 peripheral the CAN TX pin pull down the line.
Otherwise during the configuration of pin TX of CAN2 peripheral the pin doesn't pull down the line. 
I think that CAN2 config is correct because if I remove the connection on breadboard of CAN1 TX pin the CAN2 transmits the message but without ACK it continues without end. 
I'm using an oscilloscope to prove what I'm telling you.

I'm using this circuit before use the circuit with CAN transceiver.
Page 4 bottom left of this pdf: A Tiny Network without Transceiver ICs

At row 325, In HAL_CAN_MspInit function where CAN1 peri is initialized as Alternate function in  the HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); //CH1 CAN TX PB9 pull down the  line.
The counter part in CAN2 peri , at row 364 doesn't create the problem.

I have been crying since a lot of time and I have no idea of what is happening.

I have also handled the guilty PIN as normal output to check if maybe the internal transistor was broken. But it works.

Below I have inserted my code, I ask you help because I have reached the bottom.
Thanks guys in advance, I'm waiting your replies.

001./*
002. * canm.c
003. *
004. *  Created on: 08/gen/2014
005. *      Author: Andrea Tonello
006. */
007. 
008./* Includes ------------------------------------------------------------------*/
009.#include "canm.h"
010.#include "stm32f1xx_hal.h"
011./* Private typedef -----------------------------------------------------------*/
012. 
013./* Private define ------------------------------------------------------------*/
014. 
015./* Private macro -------------------------------------------------------------*/
016. 
017./* Private variables ---------------------------------------------------------*/
018.//static ulong Canm_xLastExecutionTime;
019. 
020.typedef enum CANM_State_e
021.{
022.  CANM_STATE_INIT = 0,
023.  CANM_STATE_RUNNING,
024.  CANM_STATE_ERROR,
025.  CANM_STATE_MAX_NUMBER,
026.}CANM_State_t;
027. 
028./* Public variables ----------------------------------------------------------*/
029.CANM_State_t CANM_State;
030.CAN_HandleTypeDef hcan1;
031.CAN_HandleTypeDef hcan2;
032.   
033./* Private function prototypes -----------------------------------------------*/
034. 
035./* Private functions ---------------------------------------------------------*/
036.static void vCanmTask( void *pvParameters );
037.static void MX_CAN1_Init(void);
038.static void MX_CAN2_Init(void);
039. 
040./* Public functions ----------------------------------------------------------*/
041.void CANM_Init(void)
042.{
043.  #ifdef FREERTOS
044.  if(SYST_taskEnable[SYST_HANDLE_CANM] != FALSE)
045.  {
046.    if(xTaskCreate( vCanmTask, "CANM", SYST_taskStackSize[SYST_HANDLE_CANM], NULL, SYST_tpriority[SYST_HANDLE_CANM], &SYST_tids[SYST_HANDLE_CANM] ) == pdPASS)
047.    {
048.      configASSERT( SYST_tids[SYST_HANDLE_CANM] );
049.    }
050.    
051.    CANM_State = CANM_STATE_INIT;
052.  }
053.  #else
054.    CANM_State = CANM_STATE_INIT;
055.  #endif
056.}
057. 
058.static CanTxMsgTypeDef Tx1Message;
059.static CanRxMsgTypeDef Rx1Message;
060.static CanTxMsgTypeDef Tx2Message;
061.static CanRxMsgTypeDef Rx2Message;
062. 
063.#define CANM_PERIOD_TIME_MS (1) /* ms */
064. 
065.static void vCanmTask( void *pvParameters )
066.{
067.  //Canm_xLastExecutionTime = xTaskGetTickCount();
068.  while(TRUE)
069.  {
070.    //vTaskDelayUntil( &Canm_xLastExecutionTime, pdMS_TO_TICKS(CANM_PERIOD_TIME_MS));
071.    HAL_Delay(10);
072.    switch(CANM_State)
073.    {
074.      case CANM_STATE_INIT:
075.        MX_CAN1_Init();
076.        MX_CAN2_Init();
077.       
078.        hcan1.pTxMsg = &Tx1Message;
079.        hcan1.pRxMsg = &Rx1Message;
080.        hcan2.pTxMsg = &Tx2Message;
081.        hcan2.pRxMsg = &Rx2Message;
082.       
083.        if (HAL_CAN_Receive_IT(&hcan1, CAN_FIFO0) != HAL_OK)
084.        {
085.          /* Reception Error */
086.          Error_Handler();
087.        }
088.       
089.        CANM_State = CANM_STATE_RUNNING;
090.        break;
091.      case CANM_STATE_RUNNING:
092.        Tx2Message.StdId = 0x00F;
093.        Tx2Message.IDE = CAN_ID_STD;
094.        Tx2Message.RTR = CAN_RTR_DATA;
095.        Tx2Message.DLC = 7;
096.        Tx2Message.Data[0] = ((Canm_xLastExecutionTime & 0xFF000000) >> 24);
097.        Tx2Message.Data[1] = ((Canm_xLastExecutionTime & 0x00FF0000) >> 16);
098.        Tx2Message.Data[2] = ((Canm_xLastExecutionTime & 0x0000FF00) >> 8);
099.        Tx2Message.Data[3] = ((Canm_xLastExecutionTime & 0x000000FF) >> 0);
100.        Tx2Message.Data[4] = 0xFF;
101.      Tx2Message.Data[5] = 0x00;
102.      Tx2Message.Data[6] = 0xFF;
103.       
104.        /*##-2- Start the Reception process and enable reception interrupt #########*/
105.        HAL_CAN_Transmit_IT(&hcan2);
106.        break;
107.      case CANM_STATE_ERROR:
108.         
109.        break;
110.      default:
111.        break;
112.    }
113.  }
114.}
115. 
116./* CAN1 init function */
117.void MX_CAN1_Init(void)
118.{
119.  //CAN_FilterConfTypeDef  sFilterConfig;
120.   
121.  hcan1.Instance = CAN1;
122.  hcan1.Init.Prescaler = 16;
123.  hcan1.Init.Mode = CAN_MODE_NORMAL;
124.  hcan1.Init.SJW = CAN_SJW_1TQ;
125.  hcan1.Init.BS1 = CAN_BS1_6TQ;
126.  hcan1.Init.BS2 = CAN_BS2_8TQ;
127.  hcan1.Init.TTCM = DISABLE;
128.  hcan1.Init.ABOM = DISABLE;
129.  hcan1.Init.AWUM = DISABLE;
130.  hcan1.Init.NART = DISABLE;
131.  hcan1.Init.RFLM = DISABLE;
132.  hcan1.Init.TXFP = DISABLE;
133.  if(HAL_CAN_Init(&hcan1) != HAL_OK)
134.  {
135.    Error_Handler();
136.  }
137.   
138.  //sFilterConfig.FilterNumber = 0;
139.  //sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
140.  //sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
141.  //sFilterConfig.FilterIdHigh = 0x0000;
142.  //sFilterConfig.FilterIdLow = 0x0000;
143.  //sFilterConfig.FilterMaskIdHigh = 0x0000;
144.  //sFilterConfig.FilterMaskIdLow = 0x0000;
145.  //sFilterConfig.FilterFIFOAssignment = 0;
146.  //sFilterConfig.FilterActivation = ENABLE;
147.  //sFilterConfig.BankNumber = 14;
148.  //
149.  //if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
150.  //{
151.  //  /* Filter configuration Error */
152.  //  Error_Handler();
153.  //}
154.}
155. 
156./* CAN2 init function */
157.void MX_CAN2_Init(void)
158.{
159.  //CAN_FilterConfTypeDef  sFilterConfig;
160.   
161.  hcan2.Instance = CAN2;
162.  hcan2.Init.Prescaler = 16;
163.  hcan2.Init.Mode = CAN_MODE_NORMAL;
164.  hcan2.Init.SJW = CAN_SJW_1TQ;
165.  hcan2.Init.BS1 = CAN_BS1_6TQ;
166.  hcan2.Init.BS2 = CAN_BS2_8TQ;
167.  hcan2.Init.TTCM = DISABLE;
168.  hcan2.Init.ABOM = DISABLE;
169.  hcan2.Init.AWUM = DISABLE;
170.  hcan2.Init.NART = DISABLE;
171.  hcan2.Init.RFLM = DISABLE;
172.  hcan2.Init.TXFP = DISABLE;
173.  if(HAL_CAN_Init(&hcan2) != HAL_OK)
174.  {
175.    Error_Handler();
176.  }
177.   
178.  //sFilterConfig.FilterNumber = 1;
179.  //sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
180.  //sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
181.  //sFilterConfig.FilterIdHigh = 0x0000;
182.  //sFilterConfig.FilterIdLow = 0x0000;
183.  //sFilterConfig.FilterMaskIdHigh = 0x0000;
184.  //sFilterConfig.FilterMaskIdLow = 0x0000;
185.  //sFilterConfig.FilterFIFOAssignment = 0;
186.  //sFilterConfig.FilterActivation = ENABLE;
187.  //sFilterConfig.BankNumber = 15;
188.  //
189.  //if (HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)
190.  //{
191.  //  /* Filter configuration Error */
192.  //  Error_Handler();
193.  //}
194. 
195.}
196. 
197.void CAN1_TX_IRQHandler(void)
198.{
199.  HAL_CAN_IRQHandler(&hcan1);
200.}
201. 
202.void CAN1_RX0_IRQHandler(void)
203.{
204.  HAL_CAN_IRQHandler(&hcan1);
205.}
206. 
207.void CAN1_RX1_IRQHandler(void)
208.{
209.  HAL_CAN_IRQHandler(&hcan1);
210.}
211. 
212.void CAN1_SCE_IRQHandler(void)
213.{
214.  HAL_CAN_IRQHandler(&hcan1);
215.}
216. 
217.void CAN2_TX_IRQHandler(void)
218.{
219.  HAL_CAN_IRQHandler(&hcan2);
220.}
221. 
222.void CAN2_RX0_IRQHandler(void)
223.{
224.  HAL_CAN_IRQHandler(&hcan2);
225.}
226. 
227.void CAN2_RX1_IRQHandler(void)
228.{
229.  HAL_CAN_IRQHandler(&hcan2);
230.}
231. 
232.void CAN2_SCE_IRQHandler(void)
233.{
234.  HAL_CAN_IRQHandler(&hcan2);
235.}
236. 
237./**
238.  * @brief  Transmission  complete callback in non blocking mode
239.  * @param  hcan: pointer to a CAN_HandleTypeDef structure that contains
240.  *         the configuration information for the specified CAN.
241.  * @retval None
242.  */
243.void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan)
244.{
245.  if(hcan == &hcan1)
246.  {
247.     
248.  }
249.  else if(hcan == &hcan2)
250.  {
251.     
252.  }
253.}
254. 
255./**
256.  * @brief  Transmission  complete callback in non blocking mode
257.  * @param  hcan: pointer to a CAN_HandleTypeDef structure that contains
258.  *         the configuration information for the specified CAN.
259.  * @retval None
260.  */
261.void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
262.{
263.  if(hcan == &hcan1)
264.  {
265.    /* Receive */
266.    if (HAL_CAN_Receive_IT(hcan, CAN_FIFO0) != HAL_OK)
267.    {
268.      /* Reception Error */
269.      Error_Handler();
270.    }
271.  }
272.  else if(hcan == &hcan2)
273.  {
274.    /* Receive */
275.    if (HAL_CAN_Receive_IT(hcan, CAN_FIFO0) != HAL_OK)
276.    {
277.      /* Reception Error */
278.      Error_Handler();
279.    }
280.  }
281.}
282. 
283./**
284.  * @brief  Error CAN callback.
285.  * @param  hcan: pointer to a CAN_HandleTypeDef structure that contains
286.  *         the configuration information for the specified CAN.
287.  * @retval None
288.  */
289.void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
290.{
291.  if(hcan == &hcan1)
292.  {
293.     
294.  }
295.  else if(hcan == &hcan2)
296.  {
297.     
298.  }
299.}
300. 
301.static ubyte CAN1_CLK_ENABLED=0;
302. 
303.void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
304.{
305.  GPIO_InitTypeDef GPIO_InitStruct;
306.  if(hcan->Instance==CAN1)
307.  {
308.    __HAL_RCC_GPIOB_CLK_ENABLE();
309.     
310.    CAN1_CLK_ENABLED++;
311.    if(CAN1_CLK_ENABLED==1)
312.    {
313.      __HAL_RCC_CAN1_CLK_ENABLE();
314.    }
315. 
316.    //CH1 CAN TX EXT1-21 PB9, CH1 CAN RX EXT1-20 PB8  
317.    GPIO_InitStruct.Pin = GPIO_PIN_8;
318.    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
319.    GPIO_InitStruct.Pull = GPIO_NOPULL;
320.    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
321.     
322.    GPIO_InitStruct.Pin = GPIO_PIN_9;
323.    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
324.    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
325.    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
326.     
327.    __HAL_AFIO_REMAP_CAN1_2();
328.     
329.      /* Peripheral interrupt init*/
330.    HAL_NVIC_SetPriority(CAN1_TX_IRQn, 0, 0);
331.    HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
332.    HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0);
333.    HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
334.    HAL_NVIC_SetPriority(CAN1_RX1_IRQn, 0, 0);
335.    HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
336.    HAL_NVIC_SetPriority(CAN1_SCE_IRQn, 0, 0);
337.    HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);
338.  }
339.  else if(hcan->Instance==CAN2)
340.  {
341.    __HAL_RCC_GPIOB_CLK_ENABLE();
342. 
343.    /* Peripheral clock enable */
344.    __HAL_RCC_CAN2_CLK_ENABLE();
345.    CAN1_CLK_ENABLED++;
346.    if(CAN1_CLK_ENABLED==1)
347.    {
348.      __HAL_RCC_CAN1_CLK_ENABLE();
349.    }
350.   
351.    /**CAN2 GPIO Configuration   
352.    PB12     ------> CAN2_RX
353.    PB13     ------> CAN2_TX
354.    */
355.    //CH2 CAN TX EXT1-25 PB13, CH2 CAN RX EXT1-24 PB12
356.    GPIO_InitStruct.Pin = GPIO_PIN_12;
357.    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
358.    GPIO_InitStruct.Pull = GPIO_NOPULL;
359.    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
360. 
361.    GPIO_InitStruct.Pin = GPIO_PIN_13;
362.    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
363.    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
364.    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
365.     
366.  /* Peripheral interrupt init*/
367.    HAL_NVIC_SetPriority(CAN2_TX_IRQn, 0, 0);
368.    HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);
369.    HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 0, 0);
370.    HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
371.    HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 0, 0);
372.    HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
373.    HAL_NVIC_SetPriority(CAN2_SCE_IRQn, 0, 0);
374.    HAL_NVIC_EnableIRQ(CAN2_SCE_IRQn);
375.  }
376. 
377.}
378. 
379.void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
380.{
381.  if(hcan->Instance==CAN1)
382.  {
383.    CAN1_CLK_ENABLED--;
384.    if(CAN1_CLK_ENABLED==0)
385.    {
386.      __HAL_RCC_CAN1_CLK_DISABLE();
387.    }
388.   
389.    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9);
390. 
391.    /* Peripheral interrupt DeInit*/
392.    HAL_NVIC_DisableIRQ(CAN1_TX_IRQn);
393. 
394.    HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn);
395. 
396.    HAL_NVIC_DisableIRQ(CAN1_RX1_IRQn);
397. 
398.    HAL_NVIC_DisableIRQ(CAN1_SCE_IRQn);
399.  }
400.  else if(hcan->Instance==CAN2)
401.  {
402.    __HAL_RCC_CAN2_CLK_DISABLE();
403.    CAN1_CLK_ENABLED--;
404.    if(CAN1_CLK_ENABLED==0)
405.    {
406.      __HAL_RCC_CAN1_CLK_DISABLE();
407.    }
408.   
409.    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13);
410. 
411.    /* Peripheral interrupt DeInit*/
412.    HAL_NVIC_DisableIRQ(CAN2_TX_IRQn);
413. 
414.    HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn);
415. 
416.    HAL_NVIC_DisableIRQ(CAN2_RX1_IRQn);
417. 
418.    HAL_NVIC_DisableIRQ(CAN2_SCE_IRQn);
419.  }
420. 
421.}

Outcomes