Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzucchi committed Feb 17, 2024
1 parent 46ff6ed commit c6fdb8d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 44 deletions.
4 changes: 2 additions & 2 deletions can-manager/inc/can_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#ifndef CAN_MANAGER_H
#define CAN_MANAGER_H

#include "can_manager_config.h"
#include "can_manager_can_types.h"
#include "can_manager_config.h"

#ifndef CAN_MGR_STM32_APPLICATION
#include "can_manager_type_mocking.h"
Expand All @@ -24,7 +24,7 @@ extern int can_mgr_error_code;
extern HAL_StatusTypeDef can_mgr_hal_code;

int can_mgr_init(CAN_HandleTypeDef *hcan);
int can_mgr_config_filter(int can_id, CAN_FilterTypeDef *hfilter, uint32_t rx_fifo_assignment, can_mgr_msg_t *msg_states, size_t n_msg);
int can_mgr_config(int can_id, CAN_FilterTypeDef *hfilter, uint32_t rx_fifo_assignment, can_mgr_msg_t *message_states, size_t message_states_size);
int can_mgr_activate_its(int can_id, uint32_t its);
int can_mgr_start(int can_id);

Expand Down
94 changes: 55 additions & 39 deletions can-manager/src/can_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,35 @@

#include "can_manager.h"

#define CAN_MGR_NO_ERROR 0
#define CAN_MGR_TOO_MANY_PERIPHERALS_ERROR 1
#define CAN_MGR_HAL_ERROR 2
#define CAN_MGR_INVALID_MSG_INDEX 3
#define CAN_MGR_INDEX_OUT_OF_BOUND 4
enum can_mgr_errors {
can_mgr_no_error,
can_mgr_too_many_peripherals_error,
can_mgr_hal_error,
can_mgr_invalid_msg_index_error,
can_mgr_index_out_of_bound_error,
can_mgr_invalid_can_id_error,
can_mgr_n_errors
};

CAN_HandleTypeDef *_can_mgr_peripherals[CAN_MGR_N_CAN];
int _can_mgr_fifo_assignment[CAN_MGR_TOTAL_CAN_RX_FIFOS];
can_mgr_msg_t *can_mgr_msg_states[CAN_MGR_N_CAN];
int *_can_mgr_message_maps[CAN_MGR_N_CAN];
size_t _can_mgr_msg_states_size[CAN_MGR_N_CAN];
int _can_mgr_msg_states_sizes[CAN_MGR_N_CAN];

int _can_mgr_current_can_counter = 0;

int can_mgr_error_code = CAN_MGR_NO_ERROR;
int can_mgr_error_code = can_mgr_no_error;
HAL_StatusTypeDef can_mgr_hal_code;

/**
* TODO: every function check of can_id real
*/
#define CAN_MGR_ID_CHECK(id) \
if (id < 0 || id >= _can_mgr_current_can_counter) { \
can_mgr_error_code = can_mgr_invalid_can_id_error; \
return -1; \
}

int can_mgr_init(CAN_HandleTypeDef *hcan) {
if (_can_mgr_current_can_counter == CAN_MGR_N_CAN) {
can_mgr_error_code = CAN_MGR_TOO_MANY_PERIPHERALS_ERROR;
can_mgr_error_code = can_mgr_too_many_peripherals_error;
return -1;
}
_can_mgr_peripherals[_can_mgr_current_can_counter] = hcan;
Expand All @@ -38,42 +45,49 @@ int can_mgr_init(CAN_HandleTypeDef *hcan) {
return assigned_id;
}

int can_mgr_config_filter(int can_id, CAN_FilterTypeDef *hfilter,
uint32_t rx_fifo_assignment,
can_mgr_msg_t *msg_states,
size_t n_msg) {
can_mgr_msg_states[can_id] = msg_states;
_can_mgr_msg_states_size[can_id] = n_msg;
/**
* Se message_states == NULL ignorare tutti i messaggi ricevuti (consigliato e'
* farlo anche a livello hardware con i filtri)
* Con NULL ci si aspetta state_size == 0
*/
int can_mgr_config(int can_id, CAN_FilterTypeDef *hfilter, uint32_t rx_fifo_assignment, can_mgr_msg_t *message_states, size_t message_states_size) {
CAN_MGR_ID_CHECK(can_id);
_can_mgr_fifo_assignment[rx_fifo_assignment] = can_id;

can_mgr_msg_states[can_id] = message_states;
_can_mgr_msg_states_sizes[can_id] = message_states_size;
#ifdef CAN_MGR_STM32_APPLICATION
can_mgr_hal_code =
HAL_CAN_ConfigFilter(_can_mgr_peripherals[can_id], hfilter);
if (can_mgr_hal_code != HAL_OK) {
can_mgr_error_code = CAN_MGR_HAL_ERROR;
return -1;
if (hfilter != NULL) {
can_mgr_hal_code =
HAL_CAN_ConfigFilter(_can_mgr_peripherals[can_id], hfilter);
if (can_mgr_hal_code != HAL_OK) {
can_mgr_error_code = can_mgr_hal_error;
return -1;
}
}

#endif
return 0;
}

int can_mgr_activate_its(int can_id, uint32_t its) {
CAN_MGR_ID_CHECK(can_id);
#ifdef CAN_MGR_STM32_APPLICATION
can_mgr_error_code =
HAL_CAN_ActivateNotification(_can_mgr_peripherals[can_id], its);
if (can_mgr_hal_code != HAL_OK) {
can_mgr_error_code = CAN_MGR_HAL_ERROR;
can_mgr_error_code = can_mgr_hal_error;
return -1;
}
#endif
return 0;
}

int can_mgr_start(int can_id) {
CAN_MGR_ID_CHECK(can_id);
#ifdef CAN_MGR_STM32_APPLICATION
can_mgr_error_code = HAL_CAN_Start(_can_mgr_peripherals[can_id]);
if (can_mgr_hal_code != HAL_OK) {
can_mgr_error_code = CAN_MGR_HAL_ERROR;
can_mgr_error_code = can_mgr_hal_error;
return -1;
}
#endif
Expand All @@ -90,8 +104,9 @@ void _can_mgr_wait(CAN_HandleTypeDef *hcan) {
}

int can_mgr_send(int can_id, can_mgr_msg_t *msg) {
CAN_MGR_ID_CHECK(can_id);
// TODO: make this less hardware dependent
#ifdef CAN_MGR_STM32_APPLICATION
// CAN_MGR_ID_CHECK(can_id);
CAN_HandleTypeDef *hcan = _can_mgr_peripherals[can_id];
CAN_TxHeaderTypeDef header = {.StdId = msg->id,
.IDE = CAN_ID_STD,
Expand All @@ -113,47 +128,48 @@ int can_mgr_send(int can_id, can_mgr_msg_t *msg) {
// User-defined function
int can_mgr_from_id_to_index(int can_id, int msg_id);

void _can_mgr_it_callback(CAN_HandleTypeDef *hcan,
uint32_t rx_fifo_assignment, can_mgr_msg_t* mock_msg) {
int msg_id;
int msg_dlc;
void _can_mgr_it_callback(CAN_HandleTypeDef *hcan, uint32_t rx_fifo_assignment, can_mgr_msg_t *mock_msg) {
int can_id = _can_mgr_fifo_assignment[rx_fifo_assignment];
if (can_mgr_msg_states[can_id] == NULL) {
return;
}
int msg_id, msg_dlc;
uint8_t msg_data[8] = {0};
#ifdef CAN_MGR_STM32_APPLICATION
CAN_RxHeaderTypeDef header;
HAL_CAN_GetRxMessage(hcan, rx_fifo_assignment, &header, msg_data);
msg_id = header.StdId;
msg_dlc = header.DLC;
#else
#else
msg_id = mock_msg->id;
msg_dlc = mock_msg->size;
memcpy(msg_data, mock_msg->data, msg_dlc);
#endif
int can_id = _can_mgr_fifo_assignment[rx_fifo_assignment];
int index = can_mgr_from_id_to_index(can_id, msg_id);
if (index < 0) {
can_mgr_error_code = CAN_MGR_INVALID_MSG_INDEX;
} else if (index >= _can_mgr_msg_states_size[can_id]) {
can_mgr_error_code = CAN_MGR_INDEX_OUT_OF_BOUND;
can_mgr_error_code = can_mgr_invalid_msg_index_error;
} else if (index >= _can_mgr_msg_states_sizes[can_id]) {
can_mgr_error_code = can_mgr_index_out_of_bound_error;
} else {
can_mgr_msg_states[index]->id = msg_id;
can_mgr_msg_states[index]->size = msg_dlc;
memcpy(can_mgr_msg_states[index]->data, msg_data, msg_dlc);
}
}

#if TOTAL_CAN_RX_FIFOS > 0
#if CAN_MGR_TOTAL_CAN_RX_FIFOS > 0
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
_can_mgr_it_callback(hcan, CAN_RX_FIFO0, NULL);
}
#endif

#if TOTAL_CAN_RX_FIFOS > 1
#if CAN_MGR_TOTAL_CAN_RX_FIFOS > 1
void HAL_CAN_RxFifo10MsgPendingCallback(CAN_HandleTypeDef *hcan) {
_can_mgr_it_callback(hcan, CAN_RX_FIFO1, NULL);
}
#endif

#if TOTAL_CAN_RX_FIFOS > 2
#if CAN_MGR_TOTAL_CAN_RX_FIFOS > 2
void HAL_CAN_RxFifo2MsgPendingCallback(CAN_HandleTypeDef *hcan) {
_can_mgr_it_callback(hcan, CAN_RX_FIFO2, NULL);
}
Expand Down
4 changes: 1 addition & 3 deletions can-manager/test/test-can-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
#include "can_manager.h"
#include "unity.h"

int can_manager_from_id_to_index(int can_id, int msg_id) {
return 1;
}
int can_manager_from_id_to_index(int can_id, int msg_id) { return 1; }

void setUp(void) {}

Expand Down

0 comments on commit c6fdb8d

Please sign in to comment.