Skip to content

Commit

Permalink
net: can: Added a 'private' field to store a driver internal structure.
Browse files Browse the repository at this point in the history
- Also forced some constants type to be unsigned to fix compilation warnings.
- Removed the now useless protocol version field from a CAN controller
  configuration, as the version can now be retrieved from the baud rate
  configuration in a way that is more compliant with AUTOSAR.
- Used the 'priv' field in the CAN demo driver to store the CAN protocol
  version.
- Removed the now useless protocol version field of the CAN controllers
  configurations from the CAN demo.

Signed-off-by: Adrien Ricciardi <aricciardi@baylibre.com>
  • Loading branch information
RICCIARDI-Adrien committed Oct 6, 2023
1 parent 4161018 commit e0b0e31
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
2 changes: 0 additions & 2 deletions examples/posix/can_demo/can_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ int main(void)
// First controller will use CAN 2.0 (values are fake)
{
&can_demo_driver_controller_1,
CAN_PROTOCOL_VERSION_CLASSIC,
{
.CanControllerBaudRate = 250,
.CanControllerBaudRateConfigID = 0,
Expand All @@ -53,7 +52,6 @@ int main(void)
// Second controller will use CAN FD with bit rate switch (values are fake)
{
&can_demo_driver_controller_2,
CAN_PROTOCOL_VERSION_FD,
{
.CanControllerBaudRate = 1000,
.CanControllerBaudRateConfigID = 1,
Expand Down
27 changes: 22 additions & 5 deletions machines/posix/tpl_can_demo_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ static Std_ReturnType can_demo_driver_transmit(struct tpl_can_controller_t *ctrl
static Std_ReturnType can_demo_driver_receive(struct tpl_can_controller_t *ctrl, Can_PduType *pdu_info);
static int can_demo_driver_is_data_available(struct tpl_can_controller_t *ctrl);

struct can_demo_driver_priv
{
int is_can_fd_enabled;
};

static struct can_demo_driver_priv can_demo_driver_controller_priv[2];

tpl_can_controller_t can_demo_driver_controller_1 =
{
0x12341111,
can_demo_driver_init,
can_demo_driver_set_baudrate,
can_demo_driver_transmit,
can_demo_driver_receive,
can_demo_driver_is_data_available
can_demo_driver_is_data_available,
&can_demo_driver_controller_priv[0]
};

tpl_can_controller_t can_demo_driver_controller_2 =
Expand All @@ -52,19 +60,28 @@ tpl_can_controller_t can_demo_driver_controller_2 =
can_demo_driver_set_baudrate,
can_demo_driver_transmit,
can_demo_driver_receive,
can_demo_driver_is_data_available
can_demo_driver_is_data_available,
&can_demo_driver_controller_priv[1]
};

static int can_demo_driver_init(struct tpl_can_controller_config_t *config)
{
struct can_demo_driver_priv *priv = config->controller->priv;

// Determine the CAN protocol version
if (config->baud_rate_config.use_fd_configuration)
priv->is_can_fd_enabled = 1;
else
priv->is_can_fd_enabled = 0;

printf("[%s:%d] Initializing controller 0x%08X...\r\n",
__func__,
__LINE__,
config->controller->base_address);
printf("Protocol version : %s\r\nNominal baud rate : %u kbps\r\n",
config->protocol_version == CAN_PROTOCOL_VERSION_CLASSIC ? "CAN classic 2.0" : "CAN-FD",
priv->is_can_fd_enabled ? "CAN-FD" : "CAN classic 2.0",
config->baud_rate_config.CanControllerBaudRate);
if (config->protocol_version == CAN_PROTOCOL_VERSION_FD)
if (priv->is_can_fd_enabled)
printf("Data baud rate (only for CAN-FD) : %u kbps\r\n", config->baud_rate_config.can_fd_config.CanControllerFdBaudRate);
return 0;
}
Expand All @@ -78,7 +95,7 @@ static int can_demo_driver_set_baudrate(struct tpl_can_controller_t *ctrl, CanCo
baud_rate_config->use_fd_configuration ? "CAN-FD" : "CAN classic 2.0",
baud_rate_config->CanControllerBaudRate);
if (baud_rate_config->use_fd_configuration)
printf(", CAN-FD baud rate : %u kbps", baud_rate_config->can_fd_config.CanControllerFdBaudRate);
printf(", CAN-FD data baud rate : %u kbps", baud_rate_config->can_fd_config.CanControllerFdBaudRate);
printf(".\r\n");

return 0;
Expand Down
27 changes: 7 additions & 20 deletions net/can/Can_GeneralTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,17 @@
#include <ComStack_Types.h>
#include <tpl_os.h>

#define TPL_CAN_ID_TYPE_STANDARD (0x00 << 30)
#define TPL_CAN_ID_TYPE_FD_STANDARD (0x01 << 30)
#define TPL_CAN_ID_TYPE_EXTENDED (0x02 << 30)
#define TPL_CAN_ID_TYPE_FD_EXTENDED (0x03 << 30)
#define TPL_CAN_ID_TYPE_MASK (0x03 << 30)
#define TPL_CAN_ID_TYPE_STANDARD (0x00U << 30)
#define TPL_CAN_ID_TYPE_FD_STANDARD (0x01U << 30)
#define TPL_CAN_ID_TYPE_EXTENDED (0x02U << 30)
#define TPL_CAN_ID_TYPE_FD_EXTENDED (0x03U << 30)
#define TPL_CAN_ID_TYPE_MASK (0x03U << 30)
#define TPL_CAN_ID_TYPE_GET(id) ((id & TPL_CAN_ID_TYPE_MASK) >> 30)
#define TPL_CAN_ID_MASK (0x3FFFFFFF)
#define TPL_CAN_ID_MASK (0x3FFFFFFFU)

#define TPL_CAN_CLASSIC_FRAME_MAXIMUM_PAYLOAD_SIZE (8)
#define TPL_CAN_FD_FRAME_MAXIMUM_PAYLOAD_SIZE (64)

/**
* @typedef tpl_can_protocol_version_t
*
* Select the version of the CAN protocol to operate for a specific CAN
* controller.
*/
typedef enum
{
CAN_PROTOCOL_VERSION_CLASSIC,
CAN_PROTOCOL_VERSION_FD,
CAN_PROTOCOL_VERSION_XL
} tpl_can_protocol_version_t;

/**
* @typedef Can_IdType
*
Expand Down Expand Up @@ -137,6 +124,7 @@ struct tpl_can_controller_t
Std_ReturnType (*transmit)(struct tpl_can_controller_t *ctrl, const Can_PduType *pdu_info);
Std_ReturnType (*receive)(struct tpl_can_controller_t *ctrl, Can_PduType *pdu_info);
int (*is_data_available)(struct tpl_can_controller_t *ctrl);
void *priv; // Store a custom structure for the driver internal use
};
typedef struct tpl_can_controller_t tpl_can_controller_t;

Expand All @@ -148,7 +136,6 @@ typedef struct tpl_can_controller_t tpl_can_controller_t;
struct tpl_can_controller_config_t
{
tpl_can_controller_t *controller;
tpl_can_protocol_version_t protocol_version;
CanControllerBaudrateConfig baud_rate_config;
};
typedef struct tpl_can_controller_config_t tpl_can_controller_config_t;
Expand Down

0 comments on commit e0b0e31

Please sign in to comment.