Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement Feature/hal_can Add/Del filters and return error message #63

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions firmware/Sources/HAL/HAL_CAN/hal_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ HAL_CAN_result_e HAL_CanInit (){
return res;
}

HAL_CAN_result_e HAL_CanFilters(const uint16_t id, const uint16_t mask){
HAL_CAN_result_e HAL_CanAddFilters(const uint16_t id, const uint16_t mask){
if (filterBank >13){
filterBank=0;
}
Expand All @@ -134,25 +134,69 @@ HAL_CAN_result_e HAL_CanFilters(const uint16_t id, const uint16_t mask){
return res;
}

HAL_CAN_result_e HAL_CanDelFilters(void){
HAL_CAN_result_e res = HAL_CAN_Stop(&hcan);
CAN_FilterTypeDef sFilterConfig;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT; // STID[10:3] STID[2:0] RTR IDE EXID[17:15]
sFilterConfig.FilterIdHigh = id << 5; // STDID + RTR=0 IDE=0 EXID = 00
/* In list mode the Mask also works as part of the list.
* In the 16bit scale you have 4 ids per filter bank
*/
sFilterConfig.FilterIdLow = 0x000;
sFilterConfig.FilterMaskIdHigh= 0xFFFF;
sFilterConfig.FilterMaskIdLow=0xFFFF;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
for (filterBank = 1; filterBank<14;filterBank++){
sFilterConfig.FilterBank = filterBank;
res = HAL_CAN_ConfigFilter(&hcan, &sFilterConfig);
}
filterBank = 0;
sFilterConfig.FilterBank = filterBank;
sFilterConfig.FilterMaskIdHigh=0x000;//filter mask number or identification number,according to the mode - MSBs for a 32-bit configuration
sFilterConfig.FilterMaskIdLow=0x000;//filter mask number or identification number,according to the mode - LSBs for a 32-bit configuration
res = HAL_CAN_ConfigFilter(&hcan, &sFilterConfig);
if (res == HAL_CAN_RESULT_SUCCESS){
res = HAL_CAN_Start(&hcan);
}
return res;

}

HAL_CAN_result_e HAL_CanTransmit (const uint32_t id, const uint8_t* data, const uint8_t size)
{
/* Start the Transmission process */
HAL_CAN_result_e res = HAL_CAN_RESULT_ERROR;
if (size <= 8){
uint32_t tsr = READ_REG(hcan.Instance->TSR);
/* Check that all the Tx mailboxes are not full */
if (((tsr & CAN_TSR_TME0) != 0U) ||
((tsr & CAN_TSR_TME1) != 0U) ||
((tsr & CAN_TSR_TME2) != 0U)){
if (size <= 8){
TxHeader.DLC =size;
TxHeader.StdId = id;
res = HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailbox);
}
}else{
res = HAL_CAN_RESULT_BUSY;
}
return res;
}


HAL_CAN_result_e HAL_CanReceive (uint32_t* const id, uint8_t* data, uint8_t* const size)
{
HAL_CAN_result_e res = HAL_CAN_RESULT_ERROR;
/* Get RX message */
HAL_CAN_result_e res = HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, data);
*id = RxHeader.StdId;
*size = RxHeader.DLC;
/* Check that the Rx FIFO 0 is not empty */
if ((hcan.Instance->RF0R & CAN_RF0R_FMP0) != 0U){
res = HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, data);
*id = RxHeader.StdId;
*size = RxHeader.DLC;
}else{
res = HAL_CAN_RESULT_NO_MESSAGE;
}
return res;
}
25 changes: 19 additions & 6 deletions firmware/Sources/HAL/HAL_CAN/hal_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
*/
typedef enum
{
HAL_CAN_RESULT_SUCCESS = 0x00U, /**< HAL_CAN success operation result **/
HAL_CAN_RESULT_ERROR = 0x01U, /**< HAL_CAN error operation result **/
HAL_CAN_RESULT_BUSY = 0x02U, /**< HAL_CAN busy result **/
HAL_CAN_RESULT_TIMEOUT = 0x03 /**< HAL_CAN timeout expired **/
HAL_CAN_RESULT_SUCCESS = 0x00U, /**< HAL_CAN success operation result **/
HAL_CAN_RESULT_ERROR = 0x01U, /**< HAL_CAN error operation result **/
HAL_CAN_RESULT_BUSY = 0x02U, /**< HAL_CAN busy result **/
HAL_CAN_RESULT_NO_MESSAGE = 0x03U,
HAL_CAN_RESULT_TIMEOUT = 0x04 /**< HAL_CAN timeout expired **/
}HAL_CAN_result_e;

/**********************************************************************************/
Expand Down Expand Up @@ -78,9 +79,10 @@ HAL_CAN_result_e HAL_CanInit();


/**
* @fn HAL_CAN_result_e HAL_CanFilters(const uint16_t id, const uint16_t mask)
* @fn HAL_CAN_result_e HAL_CanAddFilters(const uint16_t id, const uint16_t mask)
* @brief Configure the CAN filter with the options provided by the
* manufacturer provided functions. This will have a id and a mask aplied to the id.
* The receive message id must match the values where the mask is 0, with the id imposed.
* Every time the function is called will set a filter in a bank up to 14
*
* @param id ID reference that will have the messages in order to receive them
Expand All @@ -90,7 +92,18 @@ HAL_CAN_result_e HAL_CanInit();
* HAL_CAN_result_eIMEOUT if the CAN_TIMEOUT timeout has expired and
* HAL_CAN_RESULT_ERROR otherwise
*/
HAL_CAN_result_e HAL_CanFilters(const uint16_t id, const uint16_t mask);
HAL_CAN_result_e HAL_CanAddFilters(const uint16_t id, const uint16_t mask);

/**
* @fn HAL_CAN_result_e HAL_CanDelFilters(void)
* @brief Configure the CAN filter to receive all messages deleting all filters
*
* @return HAL_CAN_RESULT_SUCCESS if interface was initialized correctly,
* HAL_CAN_RESULT_BUSY if the peripheral is not ready,
* HAL_CAN_result_eIMEOUT if the CAN_TIMEOUT timeout has expired and
* HAL_CAN_RESULT_ERROR otherwise
*/
HAL_CAN_result_e HAL_CanDelFilters(void);

/**
* @fn HAL_CAN_result_e HAL_CanTransmit(const uint32_t id, const uint8_t* data, const uint8_t size)
Expand Down
2 changes: 1 addition & 1 deletion firmware/Sources/Test/HAL/hal_can_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ HAL_CAN_result_e HAL_CanTest(void)
uint8_t dataR[8]={0,0,0,0,0,0,0,0};
uint32_t id;
HAL_CAN_result_e res;
res = HAL_CanFilters(0x130, 0x7F0);
res = HAL_CanAddFilters(0x130, 0x7F0);
res = HAL_CanTransmit(0x102, &data, 1);
if (res == HAL_CAN_RESULT_SUCCESS){
while (i<5){
Expand Down