Lines Matching +full:reg +full:- +full:cmr

4  * SPDX-License-Identifier: Apache-2.0
22 static inline void can_sja1000_write_reg(const struct device *dev, uint8_t reg, uint8_t val) in can_sja1000_write_reg() argument
24 const struct can_sja1000_config *config = dev->config; in can_sja1000_write_reg()
26 config->write_reg(dev, reg, val); in can_sja1000_write_reg()
29 static inline uint8_t can_sja1000_read_reg(const struct device *dev, uint8_t reg) in can_sja1000_read_reg() argument
31 const struct can_sja1000_config *config = dev->config; in can_sja1000_read_reg()
33 return config->read_reg(dev, reg); in can_sja1000_read_reg()
44 if (--retries < 0) { in can_sja1000_enter_reset_mode()
45 return -EIO; in can_sja1000_enter_reset_mode()
72 if (--retries < 0) { in can_sja1000_leave_reset_mode()
73 return -EIO; in can_sja1000_leave_reset_mode()
96 struct can_sja1000_data *data = dev->data; in can_sja1000_tx_done()
97 can_tx_callback_t callback = data->tx_callback; in can_sja1000_tx_done()
98 void *user_data = data->tx_user_data; in can_sja1000_tx_done()
101 data->tx_callback = NULL; in can_sja1000_tx_done()
105 k_sem_give(&data->tx_idle); in can_sja1000_tx_done()
110 struct can_sja1000_data *data = dev->data; in can_sja1000_set_timing()
114 if (data->common.started) { in can_sja1000_set_timing()
115 return -EBUSY; in can_sja1000_set_timing()
118 k_mutex_lock(&data->mod_lock, K_FOREVER); in can_sja1000_set_timing()
120 btr0 = CAN_SJA1000_BTR0_BRP_PREP(timing->prescaler - 1) | in can_sja1000_set_timing()
121 CAN_SJA1000_BTR0_SJW_PREP(timing->sjw - 1); in can_sja1000_set_timing()
122 btr1 = CAN_SJA1000_BTR1_TSEG1_PREP(timing->phase_seg1 - 1) | in can_sja1000_set_timing()
123 CAN_SJA1000_BTR1_TSEG2_PREP(timing->phase_seg2 - 1); in can_sja1000_set_timing()
125 if ((data->common.mode & CAN_MODE_3_SAMPLES) != 0) { in can_sja1000_set_timing()
132 k_mutex_unlock(&data->mod_lock); in can_sja1000_set_timing()
153 const struct can_sja1000_config *config = dev->config; in can_sja1000_start()
154 struct can_sja1000_data *data = dev->data; in can_sja1000_start()
157 if (data->common.started) { in can_sja1000_start()
158 return -EALREADY; in can_sja1000_start()
161 if (config->common.phy != NULL) { in can_sja1000_start()
162 err = can_transceiver_enable(config->common.phy, data->common.mode); in can_sja1000_start()
174 if (config->common.phy != NULL) { in can_sja1000_start()
176 (void)can_transceiver_disable(config->common.phy); in can_sja1000_start()
182 data->common.started = true; in can_sja1000_start()
189 const struct can_sja1000_config *config = dev->config; in can_sja1000_stop()
190 struct can_sja1000_data *data = dev->data; in can_sja1000_stop()
193 if (!data->common.started) { in can_sja1000_stop()
194 return -EALREADY; in can_sja1000_stop()
203 if (config->common.phy != NULL) { in can_sja1000_stop()
204 err = can_transceiver_disable(config->common.phy); in can_sja1000_stop()
211 data->common.started = false; in can_sja1000_stop()
213 can_sja1000_tx_done(dev, -ENETDOWN); in can_sja1000_stop()
222 struct can_sja1000_data *data = dev->data; in can_sja1000_set_mode()
232 return -ENOTSUP; in can_sja1000_set_mode()
235 if (data->common.started) { in can_sja1000_set_mode()
236 return -EBUSY; in can_sja1000_set_mode()
239 k_mutex_lock(&data->mod_lock, K_FOREVER); in can_sja1000_set_mode()
267 data->common.mode = mode; in can_sja1000_set_mode()
269 k_mutex_unlock(&data->mod_lock); in can_sja1000_set_mode()
285 frame->flags |= CAN_FRAME_RTR; in can_sja1000_read_frame()
288 frame->dlc = CAN_SJA1000_FRAME_INFO_DLC_GET(info); in can_sja1000_read_frame()
289 if (frame->dlc > CAN_MAX_DLC) { in can_sja1000_read_frame()
290 LOG_ERR("RX frame DLC %u exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC); in can_sja1000_read_frame()
295 frame->flags |= CAN_FRAME_IDE; in can_sja1000_read_frame()
305 frame->id = id; in can_sja1000_read_frame()
307 if ((frame->flags & CAN_FRAME_RTR) == 0U) { in can_sja1000_read_frame()
308 for (i = 0; i < frame->dlc; i++) { in can_sja1000_read_frame()
309 frame->data[i] = can_sja1000_read_reg(dev, CAN_SJA1000_EFF_DATA + in can_sja1000_read_frame()
318 frame->id = id; in can_sja1000_read_frame()
320 if ((frame->flags & CAN_FRAME_RTR) == 0U) { in can_sja1000_read_frame()
321 for (i = 0; i < frame->dlc; i++) { in can_sja1000_read_frame()
322 frame->data[i] = can_sja1000_read_reg(dev, CAN_SJA1000_SFF_DATA + in can_sja1000_read_frame()
335 info = CAN_SJA1000_FRAME_INFO_DLC_PREP(frame->dlc); in can_sja1000_write_frame()
337 if ((frame->flags & CAN_FRAME_RTR) != 0) { in can_sja1000_write_frame()
341 if ((frame->flags & CAN_FRAME_IDE) != 0) { in can_sja1000_write_frame()
347 if ((frame->flags & CAN_FRAME_IDE) != 0) { in can_sja1000_write_frame()
348 id = frame->id; in can_sja1000_write_frame()
358 if ((frame->flags & CAN_FRAME_RTR) == 0U) { in can_sja1000_write_frame()
359 for (i = 0; i < frame->dlc; i++) { in can_sja1000_write_frame()
361 frame->data[i]); in can_sja1000_write_frame()
365 id = frame->id; in can_sja1000_write_frame()
371 if ((frame->flags & CAN_FRAME_RTR) == 0U) { in can_sja1000_write_frame()
372 for (i = 0; i < frame->dlc; i++) { in can_sja1000_write_frame()
374 frame->data[i]); in can_sja1000_write_frame()
383 struct can_sja1000_data *data = dev->data; in can_sja1000_send()
384 uint8_t cmr; in can_sja1000_send() local
387 if (frame->dlc > CAN_MAX_DLC) { in can_sja1000_send()
388 LOG_ERR("TX frame DLC %u exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC); in can_sja1000_send()
389 return -EINVAL; in can_sja1000_send()
392 if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0) { in can_sja1000_send()
393 LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags); in can_sja1000_send()
394 return -ENOTSUP; in can_sja1000_send()
397 if (!data->common.started) { in can_sja1000_send()
398 return -ENETDOWN; in can_sja1000_send()
401 if (data->state == CAN_STATE_BUS_OFF) { in can_sja1000_send()
402 LOG_DBG("transmit failed, bus-off"); in can_sja1000_send()
403 return -ENETUNREACH; in can_sja1000_send()
406 if (k_sem_take(&data->tx_idle, timeout) != 0) { in can_sja1000_send()
407 return -EAGAIN; in can_sja1000_send()
413 return -EIO; in can_sja1000_send()
416 data->tx_callback = callback; in can_sja1000_send()
417 data->tx_user_data = user_data; in can_sja1000_send()
421 if ((data->common.mode & CAN_MODE_LOOPBACK) != 0) { in can_sja1000_send()
422 cmr = CAN_SJA1000_CMR_SRR; in can_sja1000_send()
424 cmr = CAN_SJA1000_CMR_TR; in can_sja1000_send()
427 if ((data->common.mode & CAN_MODE_ONE_SHOT) != 0) { in can_sja1000_send()
428 cmr |= CAN_SJA1000_CMR_AT; in can_sja1000_send()
431 can_sja1000_write_reg(dev, CAN_SJA1000_CMR, cmr); in can_sja1000_send()
439 struct can_sja1000_data *data = dev->data; in can_sja1000_add_rx_filter()
440 int filter_id = -ENOSPC; in can_sja1000_add_rx_filter()
443 if ((filter->flags & ~(CAN_FILTER_IDE)) != 0) { in can_sja1000_add_rx_filter()
444 LOG_ERR("unsupported CAN filter flags 0x%02x", filter->flags); in can_sja1000_add_rx_filter()
445 return -ENOTSUP; in can_sja1000_add_rx_filter()
448 for (i = 0; i < ARRAY_SIZE(data->filters); i++) { in can_sja1000_add_rx_filter()
449 if (!atomic_test_and_set_bit(data->rx_allocs, i)) { in can_sja1000_add_rx_filter()
456 data->filters[filter_id].filter = *filter; in can_sja1000_add_rx_filter()
457 data->filters[filter_id].user_data = user_data; in can_sja1000_add_rx_filter()
458 data->filters[filter_id].callback = callback; in can_sja1000_add_rx_filter()
466 struct can_sja1000_data *data = dev->data; in can_sja1000_remove_rx_filter()
468 if (filter_id < 0 || filter_id >= ARRAY_SIZE(data->filters)) { in can_sja1000_remove_rx_filter()
473 if (atomic_test_and_clear_bit(data->rx_allocs, filter_id)) { in can_sja1000_remove_rx_filter()
474 data->filters[filter_id].callback = NULL; in can_sja1000_remove_rx_filter()
475 data->filters[filter_id].user_data = NULL; in can_sja1000_remove_rx_filter()
476 data->filters[filter_id].filter = (struct can_filter){0}; in can_sja1000_remove_rx_filter()
483 struct can_sja1000_data *data = dev->data; in can_sja1000_recover()
488 if (!data->common.started) { in can_sja1000_recover()
489 return -ENETDOWN; in can_sja1000_recover()
492 if ((data->common.mode & CAN_MODE_MANUAL_RECOVERY) == 0U) { in can_sja1000_recover()
493 return -ENOTSUP; in can_sja1000_recover()
503 err = k_mutex_lock(&data->mod_lock, timeout); in can_sja1000_recover()
512 k_mutex_unlock(&data->mod_lock); in can_sja1000_recover()
516 k_mutex_unlock(&data->mod_lock); in can_sja1000_recover()
519 if (k_uptime_ticks() - start_ticks > timeout.ticks) { in can_sja1000_recover()
521 return -EAGAIN; in can_sja1000_recover()
534 struct can_sja1000_data *data = dev->data; in can_sja1000_get_state()
537 if (!data->common.started) { in can_sja1000_get_state()
540 *state = data->state; in can_sja1000_get_state()
545 err_cnt->rx_err_cnt = can_sja1000_read_reg(dev, CAN_SJA1000_RXERR); in can_sja1000_get_state()
546 err_cnt->tx_err_cnt = can_sja1000_read_reg(dev, CAN_SJA1000_TXERR); in can_sja1000_get_state()
555 struct can_sja1000_data *data = dev->data; in can_sja1000_set_state_change_callback()
557 data->common.state_change_cb = callback; in can_sja1000_set_state_change_callback()
558 data->common.state_change_cb_user_data = user_data; in can_sja1000_set_state_change_callback()
571 struct can_sja1000_data *data = dev->data; in can_sja1000_handle_receive_irq()
583 for (i = 0; i < ARRAY_SIZE(data->filters); i++) { in can_sja1000_handle_receive_irq()
584 if (!atomic_test_bit(data->rx_allocs, i)) { in can_sja1000_handle_receive_irq()
588 if (can_frame_matches_filter(&frame, &data->filters[i].filter)) { in can_sja1000_handle_receive_irq()
589 callback = data->filters[i].callback; in can_sja1000_handle_receive_irq()
591 callback(dev, &frame, data->filters[i].user_data); in can_sja1000_handle_receive_irq()
611 status = -EIO; in can_sja1000_handle_transmit_irq()
632 /* Read the Error Code Capture register to re-activate it */ in can_sja1000_handle_bus_error_irq()
672 struct can_sja1000_data *data = dev->data; in can_sja1000_handle_error_warning_irq()
677 data->state = CAN_STATE_BUS_OFF; in can_sja1000_handle_error_warning_irq()
678 can_sja1000_tx_done(dev, -ENETUNREACH); in can_sja1000_handle_error_warning_irq()
680 if (data->common.started && in can_sja1000_handle_error_warning_irq()
681 (data->common.mode & CAN_MODE_MANUAL_RECOVERY) == 0U) { in can_sja1000_handle_error_warning_irq()
685 data->state = CAN_STATE_ERROR_WARNING; in can_sja1000_handle_error_warning_irq()
687 data->state = CAN_STATE_ERROR_ACTIVE; in can_sja1000_handle_error_warning_irq()
693 struct can_sja1000_data *data = dev->data; in can_sja1000_handle_error_passive_irq()
695 if (data->state == CAN_STATE_ERROR_PASSIVE) { in can_sja1000_handle_error_passive_irq()
696 data->state = CAN_STATE_ERROR_WARNING; in can_sja1000_handle_error_passive_irq()
698 data->state = CAN_STATE_ERROR_PASSIVE; in can_sja1000_handle_error_passive_irq()
704 struct can_sja1000_data *data = dev->data; in can_sja1000_isr()
705 const can_state_change_callback_t cb = data->common.state_change_cb; in can_sja1000_isr()
706 void *cb_data = data->common.state_change_cb_user_data; in can_sja1000_isr()
707 enum can_state prev_state = data->state; in can_sja1000_isr()
739 if (prev_state != data->state && cb != NULL) { in can_sja1000_isr()
742 cb(dev, data->state, err_cnt, cb_data); in can_sja1000_isr()
748 const struct can_sja1000_config *config = dev->config; in can_sja1000_init()
749 struct can_sja1000_data *data = dev->data; in can_sja1000_init()
753 __ASSERT_NO_MSG(config->read_reg != NULL); in can_sja1000_init()
754 __ASSERT_NO_MSG(config->write_reg != NULL); in can_sja1000_init()
756 if (config->common.phy != NULL) { in can_sja1000_init()
757 if (!device_is_ready(config->common.phy)) { in can_sja1000_init()
759 return -ENODEV; in can_sja1000_init()
763 k_mutex_init(&data->mod_lock); in can_sja1000_init()
764 k_sem_init(&data->tx_idle, 1, 1); in can_sja1000_init()
766 data->state = CAN_STATE_ERROR_ACTIVE; in can_sja1000_init()
777 can_sja1000_write_reg(dev, CAN_SJA1000_CDR, config->cdr | CAN_SJA1000_CDR_CAN_MODE); in can_sja1000_init()
790 err = can_calc_timing(dev, &timing, config->common.bitrate, in can_sja1000_init()
791 config->common.sample_point); in can_sja1000_init()
792 if (err == -EINVAL) { in can_sja1000_init()
807 can_sja1000_write_reg(dev, CAN_SJA1000_OCR, config->ocr); in can_sja1000_init()
816 data->common.mode = CAN_MODE_NORMAL; in can_sja1000_init()