AnsweredAssumed Answered

CAN bus is not receiving any message

Question asked by ABCD on Dec 17, 2014
Latest reply on Apr 1, 2015 by luongo.mario
I am using STM32F429 Microcontroller and need to implement CAN Bus Communication between CAN2 and PCAN View.I am able to transmit the message from CAN2 but I am not able to receive any message.I am using TJA1041A CAN transreceiver in the microcontroller.The Problem is that during debugging my CAN bus are properly initalized but it doesn't go to the receive command although I have initalized FIFO0.Herewith I am attching the program for further reference.I have used STM32 HAL Cube for programming.
001./**
002.  ******************************************************************************
003.  * File Name          : main.c
004.  * Date               : 11/12/2014 15:46:18
005.  * Description        : Main program body
006.  ******************************************************************************
007.  *
008.  * COPYRIGHT(c) 2014 STMicroelectronics
009.  *
010.  * Redistribution and use in source and binary forms, with or without modification,
011.  * are permitted provided that the following conditions are met:
012.  *   1. Redistributions of source code must retain the above copyright notice,
013.  *      this list of conditions and the following disclaimer.
014.  *   2. Redistributions in binary form must reproduce the above copyright notice,
015.  *      this list of conditions and the following disclaimer in the documentation
016.  *      and/or other materials provided with the distribution.
017.  *   3. Neither the name of STMicroelectronics nor the names of its contributors
018.  *      may be used to endorse or promote products derived from this software
019.  *      without specific prior written permission.
020.  *
021.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
024.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
025.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
027.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
028.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
029.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
030.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031.  *
032.  ******************************************************************************
033.  */
034. 
035./* Includes ------------------------------------------------------------------*/
036.#include "stm32f4xx_hal.h"
037. 
038./* USER CODE BEGIN Includes */
039. 
040./* USER CODE END Includes */
041. 
042./* Private variables ---------------------------------------------------------*/
043.CAN_HandleTypeDef hcan1;
044.CAN_HandleTypeDef hcan2;
045. 
046./* USER CODE BEGIN PV */
047. 
048./* USER CODE END PV */
049. 
050./* Private function prototypes -----------------------------------------------*/
051.void SystemClock_Config(void);
052.static void MX_GPIO_Init(void);
053.static void MX_CAN1_Init(void);
054.static void MX_CAN2_Init(void);
055.static void CAN2_RX0_IRQHandler(void);
056. 
057./* USER CODE BEGIN PFP */
058. 
059./* USER CODE END PFP */
060. 
061./* USER CODE BEGIN 0 */
062. 
063./* USER CODE END 0 */
064. 
065.int main(void)
066.{
067. 
068.  /* USER CODE BEGIN 1 */
069. 
070.  /* USER CODE END 1 */
071. 
072.  /* MCU Configuration----------------------------------------------------------*/
073. 
074.  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
075.  HAL_Init();
076. 
077.  /* Configure the system clock */
078.  SystemClock_Config();
079. 
080.  /* Initialize all configured peripherals */
081.  MX_GPIO_Init();
082.   
083. MX_CAN1_Init();
084.  
085.    
086.  MX_CAN2_Init();
087.   
088. 
089. 
090.  /* USER CODE BEGIN 2 */
091. 
092.  GPIO_InitTypeDef  GPIO_Initpins;
093.  
094. GPIO_Initpins.Mode = GPIO_MODE_OUTPUT_PP ;
095. GPIO_Initpins.Pin = GPIO_PIN_5|GPIO_PIN_7;
096. GPIO_Initpins.Pull = GPIO_NOPULL  ;
097. GPIO_Initpins.Speed = GPIO_SPEED_LOW;
098. HAL_GPIO_Init(GPIOD, &GPIO_Initpins);
099.  
100. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5|GPIO_PIN_7, GPIO_PIN_SET);
101.  /* USER CODE END 2 */
102. 
103.  /* USER CODE BEGIN 3 */
104.  /* Infinite loop */
105. CanTxMsgTypeDef TxMess;
106.   
107. TxMess.StdId = 0x123;
108. //TxMess.ExtId = 0x13375000;
109. TxMess.DLC = 0x1;
110. TxMess.Data[0] = 0xAA;
111. TxMess.IDE = CAN_ID_STD ;
112. TxMess.RTR = CAN_RTR_DATA;
113. hcan2.pTxMsg = &TxMess;
114.  
115.//HAL_CAN_Transmit(&hcan2,50);
116. 
117.CanRxMsgTypeDef rmess;
118. 
119.rmess.FIFONumber = CAN_FIFO0;
120.rmess.FMI = 14;
121.rmess.IDE = CAN_ID_STD;
122.hcan2.pRxMsg = &rmess;
123. 
124. 
125.HAL_CAN_Receive_IT(&hcan2,CAN_FIFO0);
126. 
127. 
128. 
129.while(1)
130.{
131.  HAL_CAN_Receive(&hcan2,CAN_FIFO0,0);
132.  HAL_Delay(50);
133.}
134.  /* USER CODE END 3 */
135. 
136.}
137. 
138./** System Clock Configuration
139.*/
140.void SystemClock_Config(void)
141.{
142. 
143.  RCC_OscInitTypeDef RCC_OscInitStruct;
144.  RCC_ClkInitTypeDef RCC_ClkInitStruct;
145. 
146.  __PWR_CLK_ENABLE();
147. 
148.  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
149. 
150.  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
151.  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
152.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
153.  HAL_RCC_OscConfig(&RCC_OscInitStruct);
154. 
155.  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
156.  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
157.  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
158.  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
159.  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
160.  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
161. 
162.}
163. 
164./* CAN1 init function */
165.void MX_CAN1_Init(void)
166.{
167. 
168. hcan1.Instance = CAN1;
169. hcan1.Init.Prescaler = 2;
170. hcan1.Init.Mode = CAN_MODE_NORMAL;
171. hcan1.Init.SJW = CAN_SJW_1TQ;
172. hcan1.Init.BS1 = CAN_BS1_5TQ;
173.  hcan1.Init.BS2 = CAN_BS2_2TQ;
174. hcan1.Init.TTCM = DISABLE;
175. hcan1.Init.ABOM = DISABLE;
176.  hcan1.Init.AWUM = DISABLE;
177. hcan1.Init.NART = DISABLE;
178.  hcan1.Init.RFLM = DISABLE;
179.  hcan1.Init.TXFP = DISABLE;
180. HAL_CAN_Init(&hcan1);
181. 
182.  
183. 
184.}
185. 
186./* CAN2 init function */
187.void MX_CAN2_Init(void)
188.{
189. 
190.  hcan2.Instance = CAN2;
191.  hcan2.Init.Prescaler = 2;
192.  hcan2.Init.Mode = CAN_MODE_NORMAL;
193.  hcan2.Init.SJW = CAN_SJW_1TQ;
194.  hcan2.Init.BS1 = CAN_BS1_5TQ;
195.  hcan2.Init.BS2 = CAN_BS2_2TQ;
196.  hcan2.Init.TTCM = DISABLE;
197.  hcan2.Init.ABOM = DISABLE;
198.  hcan2.Init.AWUM = DISABLE;
199.  hcan2.Init.NART = DISABLE;
200.  hcan2.Init.RFLM = DISABLE;
201.  hcan2.Init.TXFP = DISABLE;
202.  HAL_CAN_Init(&hcan2);
203. 
204.   
205.CAN_FilterConfTypeDef sFilterConfig;
206.sFilterConfig.FilterActivation = ENABLE;
207.sFilterConfig.FilterNumber = 14;                                           
208.sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;                  
209.sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;                 
210.sFilterConfig.FilterIdHigh = 0x00;                                         
211.sFilterConfig.FilterIdLow = 0x00;                                          
212.sFilterConfig.FilterMaskIdHigh = 0x00;                                     
213.sFilterConfig.FilterMaskIdLow = 0x00;                                      
214.sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;                             
215.HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig);
216. 
217.}
218. 
219./** Configure pins as
220.        * Analog
221.        * Input
222.        * Output
223.        * EVENT_OUT
224.        * EXTI
225.*/
226.void MX_GPIO_Init(void)
227.{
228. 
229.  /* GPIO Ports Clock Enable */
230.  __GPIOH_CLK_ENABLE();
231.  __GPIOB_CLK_ENABLE();
232.  __GPIOA_CLK_ENABLE();
233. __GPIOD_CLK_ENABLE();
234.}
235. 
236./* USER CODE BEGIN 4 */
237. 
238./* USER CODE END 4 */
239. 
240.#ifdef USE_FULL_ASSERT
241. 
242./**
243.   * @brief Reports the name of the source file and the source line number
244.   * where the assert_param error has occurred.
245.   * @param file: pointer to the source file name
246.   * @param line: assert_param error line source number
247.   * @retval None
248.   */
249.void assert_failed(uint8_t* file, uint32_t line)
250.{
251.  /* USER CODE BEGIN 6 */
252.  /* User can add his own implementation to report the file name and line number,
253.    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
254.  /* USER CODE END 6 */
255. 
256.}
257. 
258.#endif
259. 
260./**
261.  * @}
262.  */
263. 
264./**
265.  * @}
266.*/
267. 
268./************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Outcomes