Skip to content

Commit

Permalink
drivers: can: take minimum supported bitrate into consideration
Browse files Browse the repository at this point in the history
Take the minimum supported bitrate into consideration when validating the
bitrate.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
  • Loading branch information
henrikbrixandersen committed Mar 4, 2024
1 parent 0604d5e commit 1d8e15a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions drivers/can/can_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,13 @@ int z_impl_can_set_timing(const struct device *dev,
int z_impl_can_set_bitrate(const struct device *dev, uint32_t bitrate)
{
struct can_timing timing = { 0 };
uint32_t min_bitrate;
uint32_t max_bitrate;
uint16_t sample_pnt;
int ret;

(void)can_get_min_bitrate(dev, &min_bitrate);

ret = can_get_max_bitrate(dev, &max_bitrate);
if (ret == -ENOSYS) {
/* Maximum bitrate unknown */
Expand All @@ -350,7 +353,7 @@ int z_impl_can_set_bitrate(const struct device *dev, uint32_t bitrate)
return ret;
}

if ((max_bitrate > 0) && (bitrate > max_bitrate)) {
if ((bitrate < min_bitrate) || (((max_bitrate > 0) && (bitrate > max_bitrate)))) {
return -ENOTSUP;
}

Expand Down Expand Up @@ -391,10 +394,13 @@ int z_impl_can_set_timing_data(const struct device *dev,
int z_impl_can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data)
{
struct can_timing timing_data = { 0 };
uint32_t min_bitrate;
uint32_t max_bitrate;
uint16_t sample_pnt;
int ret;

(void)can_get_min_bitrate(dev, &min_bitrate);

ret = can_get_max_bitrate(dev, &max_bitrate);
if (ret == -ENOSYS) {
/* Maximum bitrate unknown */
Expand All @@ -403,7 +409,7 @@ int z_impl_can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data)
return ret;
}

if ((max_bitrate > 0) && (bitrate_data > max_bitrate)) {
if ((bitrate_data < min_bitrate) || ((max_bitrate > 0) && (bitrate_data > max_bitrate))) {
return -ENOTSUP;
}

Expand Down

0 comments on commit 1d8e15a

Please sign in to comment.