示例#1
0
// Check if a CAN message has been received and is waiting in the FIFO
uint8_t is_can_msg_pending(uint8_t fifo)
{
    if (bus_state == OFF_BUS)
    {
        return 0;
    }
    return (__HAL_CAN_MSG_PENDING(&can_handle, fifo) > 0);
}
示例#2
0
/**
  * @brief  Receives a correct CAN frame.
  * @param  hcan: pointer to a CAN_HandleTypeDef structure that contains
  *         the configuration information for the specified CAN.
  * @param  FIFONumber: FIFO Number value
  * @param  Timeout: Specify Timeout value
  * @retval HAL status
  * @retval None
  */
HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef* hcan, uint8_t FIFONumber, uint32_t Timeout)
{
  uint32_t tickstart = 0;

  /* Check the parameters */
  assert_param(IS_CAN_FIFO(FIFONumber));

  /* Process locked */
  __HAL_LOCK(hcan);

  if(hcan->State == HAL_CAN_STATE_BUSY_TX)
  {
    /* Change CAN state */
    hcan->State = HAL_CAN_STATE_BUSY_TX_RX;
  }
  else
  {
    /* Change CAN state */
    hcan->State = HAL_CAN_STATE_BUSY_RX;
  }

  /* Get tick */
  tickstart = HAL_GetTick();

  /* Check pending message */
  while(__HAL_CAN_MSG_PENDING(hcan, FIFONumber) == 0)
  {
    /* Check for the Timeout */
    if(Timeout != HAL_MAX_DELAY)
    {
      if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
      {
        hcan->State = HAL_CAN_STATE_TIMEOUT;

        /* Process unlocked */
        __HAL_UNLOCK(hcan);

        return HAL_TIMEOUT;
      }
    }
  }

  /* Get the Id */
  hcan->pRxMsg->IDE = (uint8_t)CAN_ID_EXT & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
  if (hcan->pRxMsg->IDE == CAN_ID_STD)
  {
    hcan->pRxMsg->StdId = (uint32_t)0x000007FF & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 21);
  }
示例#3
0
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CAN_Init();
  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */
  setup_can_filter();

  for (int i = 0; i < 10; ++i)
  {
	  led_blink();
	  HAL_Delay(100);
  }

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  CanRxMsgTypeDef rx_msg;
  uint8_t prebuf[12];
  uint8_t uart_msg[18];

  while (1)
  {
	  //

	  while (__HAL_CAN_MSG_PENDING(&hcan, CAN_FIFO0) <= 0) ;

	  hcan.pRxMsg = &rx_msg;
	  if (HAL_OK != HAL_CAN_Receive(&hcan, CAN_FIFO0, 3))
		  continue;

	  //

	  uint8_t flags = rx_msg.DLC & 0xf;

	  if (rx_msg.RTR == CAN_RTR_REMOTE)
		  flags |= 0x10;

	  if (rx_msg.IDE == CAN_ID_EXT)
		  flags |= 0x20;

	  uint32_t id = (rx_msg.IDE == CAN_ID_EXT ? rx_msg.ExtId : rx_msg.StdId);

	  for (int i = 3; i >= 0; --i)
	  {
		  prebuf[i] = (id & 0xff);
		  id >>= 8;
	  }

	  for (int i = 4; i < 12; ++i)
		  prebuf[i] = 0;

	  for (int i = 0; i < 8 && i < rx_msg.DLC; ++i)
		  prebuf[4+i] = rx_msg.Data[i];

	  //

	  int j = 0;
	  for (int i = 0; i < 12; i += 3, j += 4)
	  {
		  uint32_t triple = 0;
		  for (int k = 0; k < 3; ++k)
		  {
			  triple <<= 8;
			  triple += prebuf[i+k];
		  }

		  for (int k = 0; k < 4; ++k)
		  {
			  uart_msg[j+3-k] = basis_64[triple & 63];
			  triple >>= 6;
		  }

	  }

	  uart_msg[16] = basis_64[flags];
	  uart_msg[17] = '\n';

	  //

	  HAL_UART_Transmit(&huart1, uart_msg, 18, 1000);

	  //

	  led_blink();

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}