Skip to content

Commit

Permalink
Added CAN transmit and receive
Browse files Browse the repository at this point in the history
  • Loading branch information
APBashara committed Sep 3, 2024
1 parent e7cc63e commit 71058f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @brief Prototype Functions for CAN Driver
***********************************************/

#include "stm32f407xx.h"
#include "stm32f415xx.h"

typedef enum {
CAN_RTR_Data,
Expand Down
27 changes: 23 additions & 4 deletions Core/Src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <stddef.h>

#include "stm32f407xx.h"
#include "stm32f415xx.h"
#include "can.h"

/**
Expand Down Expand Up @@ -108,7 +108,9 @@ CAN_Status CAN_Stop() {
* @return [CAN_Status] Status of Transmission
*/
CAN_Status CAN_Transmit(CAN_TypeDef* CAN, CAN_Frame* frame) {
if (CAN == NULL || frame == NULL) {
if (CAN == NULL || frame == NULL
|| CAN1_State != CAN_State_Listening
|| CAN1_State != CAN_State_Ready) {
return CAN_Error;
}

Expand Down Expand Up @@ -144,11 +146,13 @@ CAN_Status CAN_Transmit(CAN_TypeDef* CAN, CAN_Frame* frame) {
* @return [CAN_Status] Status of Reception
*/
CAN_Status CAN_Receive(CAN_TypeDef* CAN, CAN_Frame* frame) {
if (CAN == NULL || frame == NULL) {
if (CAN == NULL || frame == NULL
|| CAN1_State != CAN_State_Listening
|| CAN1_State != CAN_State_Ready) {
return CAN_Error;
}

if ((CAN->RF0R & CAN_RF0R_FMP0) == 0x1) {
if ((CAN->RF0R & CAN_RF0R_FMP0)) {
frame->id = (CAN->sFIFOMailBox[0].RIR & CAN_RI0R_STID_Msk) >> CAN_RI0R_STID_Pos;

for (int i = 0; i < 4; i++) {
Expand All @@ -160,5 +164,20 @@ CAN_Status CAN_Receive(CAN_TypeDef* CAN, CAN_Frame* frame) {
CAN->RF0R |= CAN_RF0R_RFOM0; // Release FIFO 0
return CAN_OK;
}
else if ((CAN->RF1R & CAN_RF1R_FMP1)) {
frame->id = (CAN->sFIFOMailBox[1].RIR & CAN_RI1R_STID_Msk) >> CAN_RI1R_STID_Pos;

for (int i = 0; i < 4; i++) {
frame->data[i] = (CAN->sFIFOMailBox[1].RDLR >> (i * 8)) & 0xFF;
}
for (int i = 0; i < 4; i++) {
frame->data[i + 4] = (CAN->sFIFOMailBox[1].RDHR >> (i * 8)) & 0xFF;
}
CAN->RF1R |= CAN_RF1R_RFOM1; // Release FIFO 1
return CAN_OK;
}
else {
return CAN_Fifo_Error;
}
}

1 change: 1 addition & 0 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdint.h>

#include "gpio.h"
#include "can.h"

void Status_LED(void *argument);

Expand Down

0 comments on commit 71058f5

Please sign in to comment.