Commit 05b36608 authored by Mahmoud Rushdi's avatar Mahmoud Rushdi Committed by Oliver Horst
Browse files

[fix] Move the init of Tx freeRTOS task and queues to hal_can_open

+ fix some bugs.
parent 5289ea6e
......@@ -137,14 +137,20 @@ static void prvSendCanMsg( void* );
/******************************************************************************/
/*** Implementation ***/
/******************************************************************************/
/**
* \brief Initialize the CAN controller.
*
* It configures the CAN controller hardware:
* - Requested baud rate
* - Correct CAN Bit timing
* And Connect to the Interrupt controller
*/
BaseType_t hal_can_init(hal_io_handle_t DeviceId, uint32_t bitrate)
{
XCanPs_Config *CfgPtr;
CanInstance_t *CanInstPtr;
XCanPs *xilCanPtr;
int Status, i;
BaseType_t xReturned;
XScuGic *IntcInstPtr = & xInterruptController;
if (DeviceId < MAX_NUM_CAN_CONTROLLERS )
......@@ -220,37 +226,6 @@ BaseType_t hal_can_init(hal_io_handle_t DeviceId, uint32_t bitrate)
canBitTimingtable[i].can_BTR_FIRST_TIMESEGMENT
);
/* Create a receiving queue. */
CanInstPtr->xQueueRx = xQueueCreate( configHAL_CAN_MAX_RX_QUEUE_SIZE , sizeof(hal_can_message_t));
if( NULL == CanInstPtr->xQueueRx )
{
/* Queue was not created and must not be used. */
return pdFAIL;
}
/* Create a transmission queue. */
CanInstPtr->xQueueTx = xQueueCreate( configHAL_CAN_MAX_TX_QUEUE_SIZE , sizeof(hal_can_message_t));
if( NULL == CanInstPtr->xQueueTx )
{
/* Queue was not created and must not be used. */
return pdFAIL;
}
/* Create a transmission Task */
xReturned = xTaskCreate(
prvSendCanMsg, /* Function that implements the task. */
"Send CAN Frame Task", /* Text name for the task. */
configMINIMAL_STACK_SIZE, /* Stack size in words, not bytes. */
(void *) CanInstPtr, /* Parameter passed into the task. */
tskIDLE_PRIORITY, /* Priority at which the task is created. */
&CanInstPtr->canTxHandle ); /* Used to pass out the created task's handle. */
if( pdPASS != xReturned )
{
/* The task was not created. */
return pdFAIL;
}
/*
* Set interrupt handlers.
*/
......@@ -279,7 +254,7 @@ BaseType_t hal_can_init(hal_io_handle_t DeviceId, uint32_t bitrate)
*/
Status = XScuGic_Connect(IntcInstPtr, CanInterruptID[DeviceId],
(Xil_InterruptHandler)XCanPs_IntrHandler,
(void *)CanInstPtr);
(void *)xilCanPtr);
if ( XST_SUCCESS != Status) {
return pdFAIL;
}
......@@ -311,6 +286,12 @@ BaseType_t hal_can_init(hal_io_handle_t DeviceId, uint32_t bitrate)
}
}
/**
* \brief Deinitialize the CAN controller.
*
* - Resets the CAN controller hardware.
* - Disconnect from the Interrupt controller
*/
BaseType_t hal_can_release(hal_io_handle_t DeviceId)
{
if (DeviceId < MAX_NUM_CAN_CONTROLLERS )
......@@ -331,17 +312,53 @@ BaseType_t hal_can_release(hal_io_handle_t DeviceId)
/**
* \brief Open the CAN controller.
*
* It lookup for the CAN instance configuration.
* CAN controller can be accessed by only one freeRTOS task, a CAN
* controller can not be opened twice.
* It lookup for the CAN instance configuration. Creates the recourses
* needed by CAN and managed by FreeRTOS (queues, tasks)
* CAN controller can be accessed by only one freeRTOS task.
* A CAN controller can not be opened twice.
*/
BaseType_t hal_can_open(hal_io_handle_t DeviceId, hal_can_handle_t * canHandle )
{
CanInstance_t *CanInstPtr;
BaseType_t xReturned;
if (DeviceId < MAX_NUM_CAN_CONTROLLERS )
{
if (pdFALSE == CanInstance[DeviceId].isCtrlOpen)
{
*canHandle = &CanInstance[DeviceId];
CanInstPtr = & CanInstance[DeviceId];
*canHandle = CanInstPtr;
/* Create a receiving queue. */
CanInstPtr->xQueueRx = xQueueCreate( configHAL_CAN_MAX_RX_QUEUE_SIZE , sizeof(hal_can_message_t));
if( NULL == CanInstPtr->xQueueRx )
{
/* Queue was not created and must not be used. */
return pdFAIL;
}
/* Create a transmission queue. */
CanInstPtr->xQueueTx = xQueueCreate( configHAL_CAN_MAX_TX_QUEUE_SIZE , sizeof(hal_can_message_t));
if( NULL == CanInstPtr->xQueueTx )
{
/* Queue was not created and must not be used. */
return pdFAIL;
}
/* Create a transmission Task */
xReturned = xTaskCreate(
prvSendCanMsg, /* Function that implements the task. */
"Send CAN Frame Task", /* Text name for the task. */
configMINIMAL_STACK_SIZE, /* Stack size in words, not bytes. */
(void *) CanInstPtr, /* Parameter passed into the task. */
tskIDLE_PRIORITY, /* Priority at which the task is created. */
&CanInstPtr->canTxHandle ); /* Used to pass out the created task's handle. */
if( pdPASS != xReturned )
{
/* The task was not created. */
return pdFAIL;
}
CanInstance[DeviceId].isCtrlOpen = pdTRUE;
return pdPASS;
}
......@@ -362,13 +379,19 @@ BaseType_t hal_can_open(hal_io_handle_t DeviceId, hal_can_handle_t * canHandle )
/**
* \brief Close the CAN controller.
*
* So that
* It destroys the recourses managed by FreeRTOS (queues, tasks)
* and set the CAN port as closed.
*/
BaseType_t hal_can_close( hal_can_handle_t * canHandle )
{
CanInstance_t *CanInstPtr = *canHandle;
if (pdTRUE == CanInstPtr->isCtrlOpen)
{
/* Delete the receiving task */
vTaskDelete( CanInstPtr->canTxHandle );
/* Delete Tx and Rx queues */
vQueueDelete( CanInstPtr->xQueueRx );
vQueueDelete( CanInstPtr->xQueueTx );
CanInstPtr->isCtrlOpen = pdFALSE;
*canHandle = NULL;
}
......@@ -385,8 +408,12 @@ BaseType_t hal_can_close( hal_can_handle_t * canHandle )
BaseType_t hal_can_write(hal_can_handle_t canHandle, hal_can_message_t message)
{
CanInstance_t *CanInstPtr = canHandle;
/* Post the message to the transmitting queue. */
return xQueueSend(CanInstPtr->xQueueTx, &message, 0);
if (CanInstPtr->isCtrlOpen)
{
/* Post the message to the transmitting queue. */
return xQueueSend(CanInstPtr->xQueueTx, &message, 0);
}
return pdFAIL;
}
/**
......@@ -395,8 +422,12 @@ BaseType_t hal_can_write(hal_can_handle_t canHandle, hal_can_message_t message)
BaseType_t hal_can_read(hal_can_handle_t canHandle, hal_can_message_t *message)
{
CanInstance_t *CanInstPtr = canHandle;
/* Read a message from the receiving queue. */
return xQueueReceive(CanInstPtr->xQueueRx, message, 0);
if (CanInstPtr->isCtrlOpen)
{
/* Read a message from the receiving queue. */
return xQueueReceive(CanInstPtr->xQueueRx, message, portMAX_DELAY);
}
return pdFAIL;
}
/*****************************************************************************/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment