Skip to content

Commit

Permalink
Merge pull request #63 from WattRex/feature/HAL_CAN
Browse files Browse the repository at this point in the history
Enhancement Feature/hal_can  Add/Del filters and return error message
  • Loading branch information
pastorpflores authored Jun 20, 2023
2 parents 6d5da96 + 0f5347d commit 26dc10f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
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

0 comments on commit 26dc10f

Please sign in to comment.