/**
  * @brief  Rx Transfer completed callback
  * @param  None
  * @retval None
  */
void can2RxCpltCallback()
{
//	if ((CAN_Handle.pRxMsg->StdId == 0x321) && (CAN_Handle.pRxMsg->IDE == CAN_ID_STD) && (CAN_Handle.pRxMsg->DLC == 2))
//	{
//		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_3);
//		volatile uint8_t test1 = CAN_Handle.pRxMsg->Data[0];
//		volatile uint8_t test2 = CAN_Handle.pRxMsg->Data[1];
//	}

	if (prvRxBuffer1State != CANBufferState_Reading && prvRxBuffer1Count < RX_BUFFER_SIZE)
	{
		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_3);
		prvRxBuffer1State = CANBufferState_Writing;
		/* Save the message */
		prvRxBuffer1[prvRxBuffer1CurrentIndex].id = CAN_Handle.pRxMsg->StdId;
		prvRxBuffer1[prvRxBuffer1CurrentIndex].dlc = CAN_Handle.pRxMsg->DLC;
		for (uint32_t i = 0; i < CAN_Handle.pRxMsg->DLC; i++)
			prvRxBuffer1[prvRxBuffer1CurrentIndex].data[i] = CAN_Handle.pRxMsg->Data[i];

		/* Increment the counters */
		prvRxBuffer1CurrentIndex++;
		prvRxBuffer1Count++;

		/* Start the timer which will clear the buffer if it's not already started */
		if (xTimerIsTimerActive(prvBuffer1ClearTimer) == pdFALSE)
			xTimerStartFromISR(prvBuffer1ClearTimer, NULL);
	}
	else if (prvRxBuffer2State != CANBufferState_Reading && prvRxBuffer2Count < RX_BUFFER_SIZE)
	{
		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_3);
		prvRxBuffer2State = CANBufferState_Writing;
		/* Save the message */
		prvRxBuffer2[prvRxBuffer1CurrentIndex].id = CAN_Handle.pRxMsg->StdId;
		prvRxBuffer2[prvRxBuffer1CurrentIndex].dlc = CAN_Handle.pRxMsg->DLC;
		for (uint32_t i = 0; i < CAN_Handle.pRxMsg->DLC; i++)
			prvRxBuffer2[prvRxBuffer1CurrentIndex].data[i] = CAN_Handle.pRxMsg->Data[i];

		/* Increment the counters */
		prvRxBuffer2CurrentIndex++;
		prvRxBuffer2Count++;

		/* Start the timer which will clear the buffer if it's not already started */
		if (xTimerIsTimerActive(prvBuffer2ClearTimer) == pdFALSE)
			xTimerStartFromISR(prvBuffer2ClearTimer, NULL);
	}
	else
	{
		/* No buffer available, something has gone wrong */
		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_3);
	}


	/* Receive */
	if (HAL_CAN_Receive_IT(&CAN_Handle, CAN_FIFO1) != HAL_OK)
	{
		/* Reception Error */
//		Error_Handler();
	}
}
/**
 * @brief	Enables the CAN2 interface with the current settings
 * @param	None
 * @retval	None
 */
static ErrorStatus prvEnableCan2Interface()
{
	/*##-1- Enable peripheral Clocks ###########################################*/
	/* CAN2 Peripheral clock enable */
	__CAN1_CLK_ENABLE();		/* IMPORTANT!!! As CAN2 is a slave device we need to enable the clock for CAN1 as well */
	__CAN2_CLK_ENABLE();

	/*##-2- Configure the NVIC #################################################*/
	/* NVIC configuration for CAN2 Reception complete interrupt */
	HAL_NVIC_SetPriority(CAN2_RX0_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0);
	HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);

	HAL_NVIC_SetPriority(CAN2_TX_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0);
	HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);

	/*##-3- Configure the CAN peripheral #######################################*/
	if (HAL_CAN_Init(&CAN_Handle) != HAL_OK)
	{
		/* Initialization Error */
		goto error;
	}

	/*##-4- Configure the CAN Filter ###########################################*/
	if (HAL_CAN_ConfigFilter(&CAN_Handle, &CAN_Filter) != HAL_OK)
	{
		/* Filter configuration Error */
		goto error;
	}

	/*##-5- Configure Transmission process #####################################*/
	CAN_Handle.pTxMsg->StdId = 0x321;
	CAN_Handle.pTxMsg->ExtId = 0x01;
	CAN_Handle.pTxMsg->RTR = CAN_RTR_DATA;
	CAN_Handle.pTxMsg->IDE = CAN_ID_STD;
	CAN_Handle.pTxMsg->DLC = 2;

	/*##-6- Start the Reception process and enable reception interrupt #########*/
	if (HAL_CAN_Receive_IT(&CAN_Handle, CAN_FIFO1) != HAL_OK)
	{
		/* Reception Error */
		goto error;
	}

	return SUCCESS;

error:
	/* Something went wrong so disable */
	prvDisableCan2Interface();
	return ERROR;
}
Exemple #3
0
/**
  * @brief  Transmission  complete callback in non blocking mode
  * @param  CanHandle: pointer to a CAN_HandleTypeDef structure that contains
  *         the configuration information for the specified CAN.
  * @retval None
  */
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
{
  if ((CanHandle->pRxMsg->StdId == 0x321) && (CanHandle->pRxMsg->IDE == CAN_ID_STD) && (CanHandle->pRxMsg->DLC == 2))
  {
    LED_Display(CanHandle->pRxMsg->Data[0]);
    ubKeyNumber = CanHandle->pRxMsg->Data[0];
  }

  /* Receive */
  if (HAL_CAN_Receive_IT(CanHandle, CAN_FIFO0) != HAL_OK)
  {
    /* Reception Error */
    Error_Handler();
  }
}
Exemple #4
0
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan) {
	if (hcan && hcan->pRxMsg) {

		CanMsg_t mgs = {
				hcan->pRxMsg->IDE == CAN_ID_STD ? hcan->pTxMsg->StdId : hcan->pTxMsg->ExtId,
				hcan->pRxMsg->IDE == CAN_ID_EXT,
				hcan->pTxMsg->RTR == CAN_RTR_REMOTE,
				hcan->pTxMsg->RTR == CAN_RTR_DATA ? hcan->pTxMsg->DLC : 0,
		};
		memcpy(mgs.buff, hcan->pTxMsg->Data, mgs.size);
		EventQueue_Push(
				EVENT_CAN,
				newEvent(CAN_EVENT_RX, &mgs, HAL_CAN_GetError(hcan)),
				onEventDispose);
		HAL_CAN_Receive_IT(hcan, CAN_FIFO0);
	}
}
Exemple #5
0
/**
 * @brief	Enables the CAN1 interface with the current settings
 * @param	None
 * @retval	None
 */
static ErrorStatus prvEnableCan1Interface()
{
	/*##-1- Enable peripheral Clocks ###########################################*/
	/* CAN1 Peripheral clock enable */
	__CAN1_CLK_ENABLE();

	/*##-2- Configure the NVIC #################################################*/
	HAL_NVIC_SetPriority(CAN1_RX0_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0);
	HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);

	HAL_NVIC_SetPriority(CAN1_TX_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0);
	HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);

	/*##-3- Configure the CAN peripheral #######################################*/
	if (HAL_CAN_Init(&CAN_Handle) != HAL_OK)
	{
		/* Initialization Error */
		goto error;
	}

	/*##-4- Configure the CAN Filter ###########################################*/
	if (HAL_CAN_ConfigFilter(&CAN_Handle, &CAN_Filter) != HAL_OK)
	{
		/* Filter configuration Error */
		goto error;
	}

	/*##-6- Start the Reception process and enable reception interrupt #########*/
	if (HAL_CAN_Receive_IT(&CAN_Handle, CAN_FIFO0) != HAL_OK)
	{
		/* Reception Error */
		goto error;
	}

	return SUCCESS;

error:
	/* Something went wrong so disable */
	prvDisableCan1Interface();
	return ERROR;
}
CAN_ERROR CAN_hw_start (U32 ctrl)  
{
	CAN_HandleTypeDef *hCAN;
	switch (ctrl)
	{
#if RTE_CAN1 == 1
		case __CTRL1:
			hCAN = &hCAN1;
			break;
#endif
#if RTE_CAN2 == 1
		case __CTRL2:
			hCAN= &hCan2;
			break;
#endif
		default:
			return CAN_UNEXIST_CTRL_ERROR;
	}
	if (hCAN->State==HAL_CAN_STATE_ERROR | hCAN->State == HAL_CAN_STATE_RESET)
		return CAN_INIT_ERROR;
	
	HAL_CAN_Receive_IT(hCAN, CAN_FIFO0);
  return CAN_OK;
}
Exemple #7
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* Enable the CPU Cache */
  CPU_CACHE_Enable();

  /* STM32F7xx HAL library initialization:
       - Configure the Flash ART accelerator on ITCM interface
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();


  /* Configure the system clock to 216 MHz */
  SystemClock_Config();

  /* Since MFX is used, LED init is done after clock config */
  /* Configure LED1, LED2, LED3 and LED4 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  
  /* Configure Tamper push-button */
  BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_GPIO);

  /*##-1- Configure the CAN peripheral #######################################*/
  CAN_Config();

  /*##-2- Start the Reception process and enable reception interrupt #########*/
  if (HAL_CAN_Receive_IT(&CanHandle, CAN_FIFO0) != HAL_OK)
  {
    /* Reception Error */
    Error_Handler();
  }

  /* Infinite loop */
  while (1)
  {
    while (BSP_PB_GetState(BUTTON_TAMPER) == KEY_PRESSED)
    {
      if (ubKeyNumber == 0x4)
      {
        ubKeyNumber = 0x00;
      }
      else
      {
        LED_Display(++ubKeyNumber);
        
        /* Set the data to be transmitted */
        CanHandle.pTxMsg->Data[0] = ubKeyNumber;
        CanHandle.pTxMsg->Data[1] = 0xAD;
        
        /*##-3- Start the Transmission process ###############################*/
        if (HAL_CAN_Transmit(&CanHandle, 10) != HAL_OK)
        {
          /* Transmission Error */
          Error_Handler();
        }
        HAL_Delay(10);
        
        while (BSP_PB_GetState(BUTTON_TAMPER) != KEY_NOT_PRESSED)
        {
        }
      }
    }
  }
}
Exemple #8
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /* Configure LED1, LED2, LED3 and LED4 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  
  /* Configure Key Button */ 
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
  
  /*##-1- Configure the CAN peripheral #######################################*/
  CAN_Config();
  
  /*##-2- Start the Reception process and enable reception interrupt #########*/
  if(HAL_CAN_Receive_IT(&CanHandle, CAN_FIFO0) != HAL_OK)
  {
    /* Reception Error */
    Error_Handler();
  }
  
  /* Infinite loop */
  while(1)
  {
    while(BSP_PB_GetState(BUTTON_KEY) == KEY_PRESSED)
    {
      if(ubKeyNumber == 0x4) 
      {
        ubKeyNumber = 0x00;
      }
      else
      {
        LED_Display(++ubKeyNumber);
        
        /* Set the data to be transmitted */
        CanHandle.pTxMsg->Data[0] = ubKeyNumber;
        CanHandle.pTxMsg->Data[1] = 0xAD;
        
        /*##-3- Start the Transmission process ###############################*/
        if(HAL_CAN_Transmit(&CanHandle, 10) != HAL_OK)
        {
          /* Transmition Error */
          Error_Handler();
        }
        HAL_Delay(10);
        
        while(BSP_PB_GetState(BUTTON_KEY) != KEY_NOT_PRESSED)
        {
        }
      }
    }
  } 
}
Exemple #9
0
_Bool CAN_init(void *data) {
	CAN_HandleTypeDef *handle = data;

	HAL_StatusTypeDef result = HAL_ERROR;
	const uint32_t baudRate = 125000;

	CAN_InitTypeDef ifaceParams = {
			HAL_RCC_GetPCLK1Freq()/(baudRate*(1+4+3)), //(CAN_SJW + CAN_BS1 + CAN_BS2)
			CAN_MODE_NORMAL,
			CAN_SJW_1TQ,
			CAN_BS1_4TQ,
			CAN_BS2_3TQ,
			.TTCM = DISABLE,
			.ABOM = DISABLE,
			.AWUM = DISABLE,
			.NART = DISABLE,
			.RFLM = DISABLE,
			.TXFP = DISABLE,
	};
	CAN_FilterConfTypeDef filter = {
			0, 0, 0, 0,
			CAN_FIFO0,
			0,
			CAN_FILTERMODE_IDMASK,
			CAN_FILTERSCALE_32BIT,
			ENABLE,
			0
	};

	if (handle) {
		free(handle->pRxMsg);
		free(handle->pTxMsg);
		handle->pRxMsg = NULL;
		handle->pTxMsg = NULL;
		memset(handle, 0, sizeof(*handle));
		handle->Instance = CAN1;
		handle->Init = ifaceParams;

		result = HAL_CAN_Init(handle);
		if (result == HAL_OK) {
			HAL_CAN_ConfigFilter(handle, &filter);
			HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
			HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
			HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
//			HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);
			handle->pRxMsg = malloc(sizeof(*handle->pRxMsg));
			result &= HAL_CAN_Receive_IT(handle, CAN_FIFO0);
		}
		s_can1Handle = handle;
	}
	return result == HAL_OK;
}

_Bool CAN_write(const CanMsg_t *data) {
	HAL_StatusTypeDef result = HAL_ERROR;
	static const size_t msgSize = sizeof(CanTxMsgTypeDef);
	if (!data)
		return false;
	CanTxMsgTypeDef txMsg = {
		data->id,
		data->id,
		data->isExtended ? CAN_ID_EXT : CAN_ID_STD,
		data->isRemoteFrame ? CAN_RTR_REMOTE : CAN_RTR_DATA,
		data->isRemoteFrame ? 0 :
				data->size > 8 ? 8 : data->size,
	};

	do {
		if (!s_can1Handle)
			break;
		memcpy(txMsg.Data, data->buff, txMsg.DLC);
		free(s_can1Handle->pTxMsg);
		s_can1Handle->pTxMsg = malloc(msgSize);
		if (!s_can1Handle->pTxMsg)
			break;
		memcpy(s_can1Handle->pTxMsg, &txMsg, msgSize);
		result = HAL_CAN_Transmit_IT(s_can1Handle);
	} while (0);
	return result == HAL_OK;
}
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef * hcan)
{
	osSignalSet(worker1Handle, 0x0001);
	HAL_CAN_Receive_IT(&hcan1, CAN_FIFO0);
}