Skip to content

Commit 1889082

Browse files
henrikbrixandersencarlescufi
authored andcommitted
drivers: can: split CAN classic and CAN-FD syscalls
Split CAN classic and CAN-FD syscalls into two: - can_set_timing() -> can_set_timing() + can_set_timing_data() - can_set_bitrate() -> can_set_bitrate() + can_set_bitrate_data() Fixes: #45303 Signed-off-by: Henrik Brix Andersen <[email protected]>
1 parent bad1fa4 commit 1889082

File tree

19 files changed

+187
-88
lines changed

19 files changed

+187
-88
lines changed

doc/hardware/peripherals/canbus/controller.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,7 @@ Setting the bitrate
284284
*******************
285285

286286
The bitrate and sampling point is initially set at runtime. To change it from
287-
the application, one can use the :c:func:`can_set_timing` API. This function
288-
takes three arguments. The first timing parameter sets the timing for classic
289-
CAN and arbitration phase for CAN-FD. The second parameter sets the timing of
290-
the data phase for CAN-FD. For classic CAN, you can use only the first
291-
parameter and put NULL to the second one. The :c:func:`can_calc_timing`
287+
the application, one can use the :c:func:`can_set_timing` API. The :c:func:`can_calc_timing`
292288
function can calculate timing from a bitrate and sampling point in permille.
293289
The following example sets the bitrate to 250k baud with the sampling point at
294290
87.5%.
@@ -309,11 +305,14 @@ The following example sets the bitrate to 250k baud with the sampling point at
309305
return;
310306
}
311307
312-
ret = can_set_timing(can_dev, &timing, NULL);
308+
ret = can_set_timing(can_dev, &timing);
313309
if (ret != 0) {
314310
LOG_ERR("Failed to set timing");
315311
}
316312
313+
A similar API exists for calculating and setting the timing for the data phase for CAN-FD capable
314+
controllers. See :c:func:`can_set_timing_data` and :c:func:`can_calc_timing_data`.
315+
317316
SocketCAN
318317
*********
319318

drivers/can/can_common.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,9 @@ uint16_t sample_point_for_bitrate(uint32_t bitrate)
207207
return sample_pnt;
208208
}
209209

210-
int z_impl_can_set_bitrate(const struct device *dev, uint32_t bitrate, uint32_t bitrate_data)
210+
int z_impl_can_set_bitrate(const struct device *dev, uint32_t bitrate)
211211
{
212212
struct can_timing timing;
213-
#ifdef CONFIG_CAN_FD_MODE
214-
struct can_timing timing_data;
215-
#endif /* CONFIG_CAN_FD_MODE */
216213
uint32_t max_bitrate;
217214
uint16_t sample_pnt;
218215
int ret;
@@ -241,7 +238,25 @@ int z_impl_can_set_bitrate(const struct device *dev, uint32_t bitrate, uint32_t
241238

242239
timing.sjw = CAN_SJW_NO_CHANGE;
243240

241+
return can_set_timing(dev, &timing);
242+
}
243+
244244
#ifdef CONFIG_CAN_FD_MODE
245+
int z_impl_can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data)
246+
{
247+
struct can_timing timing_data;
248+
uint32_t max_bitrate;
249+
uint16_t sample_pnt;
250+
int ret;
251+
252+
ret = can_get_max_bitrate(dev, &max_bitrate);
253+
if (ret == -ENOSYS) {
254+
/* Maximum bitrate unknown */
255+
max_bitrate = 0;
256+
} else if (ret < 0) {
257+
return ret;
258+
}
259+
245260
if ((max_bitrate > 0) && (bitrate_data > max_bitrate)) {
246261
return -ENOTSUP;
247262
}
@@ -258,8 +273,6 @@ int z_impl_can_set_bitrate(const struct device *dev, uint32_t bitrate, uint32_t
258273

259274
timing_data.sjw = CAN_SJW_NO_CHANGE;
260275

261-
return can_set_timing(dev, &timing, &timing_data);
262-
#else /* CONFIG_CAN_FD_MODE */
263-
return can_set_timing(dev, &timing, NULL);
264-
#endif /* !CONFIG_CAN_FD_MODE */
276+
return can_set_timing_data(dev, &timing_data);
265277
}
278+
#endif /* CONFIG_CAN_FD_MODE */

drivers/can/can_handlers.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,14 @@ static int z_vrfy_can_calc_timing(const struct device *dev, struct can_timing *r
2424
#include <syscalls/can_calc_timing_mrsh.c>
2525

2626
static inline int z_vrfy_can_set_timing(const struct device *dev,
27-
const struct can_timing *timing,
28-
const struct can_timing *timing_data)
27+
const struct can_timing *timing)
2928
{
3029
struct can_timing timing_copy;
31-
struct can_timing timing_data_copy;
3230

3331
Z_OOPS(Z_SYSCALL_DRIVER_CAN(dev, set_timing));
3432
Z_OOPS(z_user_from_copy(&timing_copy, timing, sizeof(timing_copy)));
3533

36-
if (timing_data != NULL) {
37-
Z_OOPS(z_user_from_copy(&timing_data_copy, timing_data, sizeof(timing_data_copy)));
38-
return z_impl_can_set_timing(dev, &timing_copy, &timing_data_copy);
39-
}
40-
41-
return z_impl_can_set_timing(dev, &timing_copy, NULL);
34+
return z_impl_can_set_timing(dev, &timing_copy);
4235
}
4336
#include <syscalls/can_set_timing_mrsh.c>
4437

@@ -113,6 +106,27 @@ static inline const struct can_timing *z_vrfy_can_get_timing_max_data(const stru
113106
}
114107
#include <syscalls/can_get_timing_max_data_mrsh.c>
115108

109+
static inline int z_vrfy_can_set_timing_data(const struct device *dev,
110+
const struct can_timing *timing_data)
111+
{
112+
struct can_timing timing_data_copy;
113+
114+
Z_OOPS(Z_SYSCALL_DRIVER_CAN(dev, set_timing_data));
115+
Z_OOPS(z_user_from_copy(&timing_data_copy, timing_data, sizeof(timing_data_copy)));
116+
117+
return z_impl_can_set_timing_data(dev, &timing_data_copy);
118+
}
119+
#include <syscalls/can_set_timing_data_mrsh.c>
120+
121+
static inline int z_vrfy_can_set_bitrate_data(const struct device *dev,
122+
uint32_t bitrate_data)
123+
{
124+
Z_OOPS(Z_SYSCALL_DRIVER_CAN(dev, set_timing_data));
125+
126+
return z_impl_can_set_bitrate_data(dev, bitrate_data);
127+
}
128+
#include <syscalls/can_set_bitrate_data_mrsh.c>
129+
116130
#endif /* CONFIG_CAN_FD_MODE */
117131

118132
static inline int z_vrfy_can_get_max_filters(const struct device *dev, enum can_ide id_type)
@@ -132,12 +146,11 @@ static inline int z_vrfy_can_set_mode(const struct device *dev, enum can_mode mo
132146
}
133147
#include <syscalls/can_set_mode_mrsh.c>
134148

135-
static inline int z_vrfy_can_set_bitrate(const struct device *dev, uint32_t bitrate,
136-
uint32_t bitrate_data)
149+
static inline int z_vrfy_can_set_bitrate(const struct device *dev, uint32_t bitrate)
137150
{
138151
Z_OOPS(Z_SYSCALL_DRIVER_CAN(dev, set_timing));
139152

140-
return z_impl_can_set_bitrate(dev, bitrate, bitrate_data);
153+
return z_impl_can_set_bitrate(dev, bitrate);
141154
}
142155
#include <syscalls/can_set_bitrate_mrsh.c>
143156

drivers/can/can_loopback.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,11 @@ static int can_loopback_set_mode(const struct device *dev, enum can_mode mode)
206206
}
207207

208208
static int can_loopback_set_timing(const struct device *dev,
209-
const struct can_timing *timing,
210-
const struct can_timing *timing_data)
209+
const struct can_timing *timing)
211210
{
212211
ARG_UNUSED(dev);
213212
ARG_UNUSED(timing);
214-
ARG_UNUSED(timing_data);
213+
215214
return 0;
216215
}
217216

drivers/can/can_mcan.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,34 @@ void can_mcan_configure_timing(struct can_mcan_reg *can,
185185
}
186186

187187
int can_mcan_set_timing(const struct device *dev,
188-
const struct can_timing *timing,
188+
const struct can_timing *timing)
189+
{
190+
const struct can_mcan_config *cfg = dev->config;
191+
struct can_mcan_reg *can = cfg->can;
192+
int ret;
193+
194+
ret = can_enter_init_mode(can, K_MSEC(CAN_INIT_TIMEOUT));
195+
if (ret) {
196+
LOG_ERR("Failed to enter init mode");
197+
return -EIO;
198+
}
199+
200+
/* Configuration Change Enable */
201+
can->cccr |= CAN_MCAN_CCCR_CCE;
202+
203+
can_mcan_configure_timing(can, timing, NULL);
204+
205+
ret = can_leave_init_mode(can, K_MSEC(CAN_INIT_TIMEOUT));
206+
if (ret) {
207+
LOG_ERR("Failed to leave init mode");
208+
return -EIO;
209+
}
210+
211+
return 0;
212+
}
213+
214+
#ifdef CONFIG_CAN_FD_MODE
215+
int can_mcan_set_timing_data(const struct device *dev,
189216
const struct can_timing *timing_data)
190217
{
191218
const struct can_mcan_config *cfg = dev->config;
@@ -201,7 +228,7 @@ int can_mcan_set_timing(const struct device *dev,
201228
/* Configuration Change Enable */
202229
can->cccr |= CAN_MCAN_CCCR_CCE;
203230

204-
can_mcan_configure_timing(can, timing, timing_data);
231+
can_mcan_configure_timing(can, NULL, timing_data);
205232

206233
ret = can_leave_init_mode(can, K_MSEC(CAN_INIT_TIMEOUT));
207234
if (ret) {
@@ -211,6 +238,7 @@ int can_mcan_set_timing(const struct device *dev,
211238

212239
return 0;
213240
}
241+
#endif /* CONFIG_CAN_FD_MODE */
214242

215243
int can_mcan_set_mode(const struct device *dev, enum can_mode mode)
216244
{

drivers/can/can_mcan.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,10 @@ struct can_mcan_reg;
260260
int can_mcan_set_mode(const struct device *dev, enum can_mode mode);
261261

262262
int can_mcan_set_timing(const struct device *dev,
263-
const struct can_timing *timing,
264-
const struct can_timing *timing_data);
263+
const struct can_timing *timing);
264+
265+
int can_mcan_set_timing_data(const struct device *dev,
266+
const struct can_timing *timing_data);
265267

266268
int can_mcan_init(const struct device *dev);
267269

drivers/can/can_mcp2515.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,8 @@ static int mcp2515_get_max_bitrate(const struct device *dev, uint32_t *max_bitra
330330
}
331331

332332
static int mcp2515_set_timing(const struct device *dev,
333-
const struct can_timing *timing,
334-
const struct can_timing *timing_data)
333+
const struct can_timing *timing)
335334
{
336-
ARG_UNUSED(timing_data);
337335
struct mcp2515_data *dev_data = dev->data;
338336
int ret;
339337

@@ -949,7 +947,7 @@ static int mcp2515_init(const struct device *dev)
949947
}
950948
}
951949

952-
ret = can_set_timing(dev, &timing, NULL);
950+
ret = can_set_timing(dev, &timing);
953951
if (ret) {
954952
return ret;
955953
}

drivers/can/can_mcux_flexcan.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,8 @@ static int mcux_flexcan_get_max_bitrate(const struct device *dev, uint32_t *max_
152152
}
153153

154154
static int mcux_flexcan_set_timing(const struct device *dev,
155-
const struct can_timing *timing,
156-
const struct can_timing *timing_data)
155+
const struct can_timing *timing)
157156
{
158-
ARG_UNUSED(timing_data);
159157
struct mcux_flexcan_data *data = dev->data;
160158
const struct mcux_flexcan_config *config = dev->config;
161159
uint8_t sjw_backup = data->timing.sjw;

drivers/can/can_mcux_mcan.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static const struct can_driver_api mcux_mcan_driver_api = {
9494
.prescaler = 512,
9595
},
9696
#ifdef CONFIG_CAN_FD_MODE
97+
.set_timing_data = can_mcan_set_timing_data,
9798
/*
9899
* MCUX MCAN data timing limits are specified in the "Data bit timing
99100
* and prescaler register (DBTP)" table in the SoC reference manual.

drivers/can/can_rcar.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,15 +648,12 @@ static void can_rcar_set_bittiming(const struct can_rcar_cfg *config,
648648
}
649649

650650
static int can_rcar_set_timing(const struct device *dev,
651-
const struct can_timing *timing,
652-
const struct can_timing *timing_data)
651+
const struct can_timing *timing)
653652
{
654653
const struct can_rcar_cfg *config = dev->config;
655654
struct can_rcar_data *data = dev->data;
656655
int ret = 0;
657656

658-
ARG_UNUSED(timing_data);
659-
660657
k_mutex_lock(&data->inst_mutex, K_FOREVER);
661658

662659
/* Changing bittiming should be done in reset mode */
@@ -955,7 +952,7 @@ static int can_rcar_init(const struct device *dev)
955952
}
956953
}
957954

958-
ret = can_rcar_set_timing(dev, &timing, NULL);
955+
ret = can_rcar_set_timing(dev, &timing);
959956
if (ret) {
960957
return ret;
961958
}

0 commit comments

Comments
 (0)