Lines Matching +full:transmit +full:- +full:retries
4 * SPDX-License-Identifier: Apache-2.0
28 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_cmd_soft_reset()
39 return spi_write_dt(&dev_cfg->bus, &tx); in mcp2515_cmd_soft_reset()
46 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_cmd_bit_modify()
57 return spi_write_dt(&dev_cfg->bus, &tx); in mcp2515_cmd_bit_modify()
63 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_cmd_write_reg()
75 return spi_write_dt(&dev_cfg->bus, &tx); in mcp2515_cmd_write_reg()
81 * When loading a transmit buffer, reduces the overhead of a normal WRITE
95 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_cmd_load_tx_buffer()
109 return spi_write_dt(&dev_cfg->bus, &tx); in mcp2515_cmd_load_tx_buffer()
113 * Request-to-Send Instruction
121 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_cmd_rts()
134 return spi_write_dt(&dev_cfg->bus, &tx); in mcp2515_cmd_rts()
140 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_cmd_read_reg()
159 return spi_transceive_dt(&dev_cfg->bus, &tx, &rx); in mcp2515_cmd_read_reg()
176 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_cmd_read_rx_buffer()
197 return spi_transceive_dt(&dev_cfg->bus, &tx, &rx); in mcp2515_cmd_read_rx_buffer()
207 if ((source->flags & CAN_FRAME_IDE) != 0) { in mcp2515_convert_canframe_to_mcp2515frame()
208 target[MCP2515_FRAME_OFFSET_SIDH] = source->id >> 21; in mcp2515_convert_canframe_to_mcp2515frame()
210 (((source->id >> 18) & 0x07) << 5) | (BIT(3)) | in mcp2515_convert_canframe_to_mcp2515frame()
211 ((source->id >> 16) & 0x03); in mcp2515_convert_canframe_to_mcp2515frame()
212 target[MCP2515_FRAME_OFFSET_EID8] = source->id >> 8; in mcp2515_convert_canframe_to_mcp2515frame()
213 target[MCP2515_FRAME_OFFSET_EID0] = source->id; in mcp2515_convert_canframe_to_mcp2515frame()
215 target[MCP2515_FRAME_OFFSET_SIDH] = source->id >> 3; in mcp2515_convert_canframe_to_mcp2515frame()
217 (source->id & 0x07) << 5; in mcp2515_convert_canframe_to_mcp2515frame()
220 rtr = (source->flags & CAN_FRAME_RTR) != 0 ? BIT(6) : 0; in mcp2515_convert_canframe_to_mcp2515frame()
221 dlc = (source->dlc) & 0x0F; in mcp2515_convert_canframe_to_mcp2515frame()
228 source->data[data_idx]; in mcp2515_convert_canframe_to_mcp2515frame()
241 target->flags |= CAN_FRAME_IDE; in mcp2515_convert_mcp2515frame_to_canframe()
242 target->id = in mcp2515_convert_mcp2515frame_to_canframe()
249 target->id = (source[MCP2515_FRAME_OFFSET_SIDH] << 3) | in mcp2515_convert_mcp2515frame_to_canframe()
253 target->dlc = source[MCP2515_FRAME_OFFSET_DLC] & 0x0F; in mcp2515_convert_mcp2515frame_to_canframe()
256 target->flags |= CAN_FRAME_RTR; in mcp2515_convert_mcp2515frame_to_canframe()
258 for (data_idx = 0U; data_idx < target->dlc; data_idx++) { in mcp2515_convert_mcp2515frame_to_canframe()
259 target->data[data_idx] = source[MCP2515_FRAME_OFFSET_D0 + in mcp2515_convert_mcp2515frame_to_canframe()
267 int retries = MCP2515_MODE_CHANGE_RETRIES; in mcp2515_set_mode_int() local
277 if (--retries < 0) { in mcp2515_set_mode_int()
279 return -EIO; in mcp2515_set_mode_int()
291 struct mcp2515_data *dev_data = dev->data; in mcp2515_tx_done()
292 can_tx_callback_t callback = dev_data->tx_cb[tx_idx].cb; in mcp2515_tx_done()
295 callback(dev, status, dev_data->tx_cb[tx_idx].cb_arg); in mcp2515_tx_done()
296 dev_data->tx_cb[tx_idx].cb = NULL; in mcp2515_tx_done()
298 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp2515_tx_done()
299 dev_data->tx_busy_map &= ~BIT(tx_idx); in mcp2515_tx_done()
300 k_mutex_unlock(&dev_data->mutex); in mcp2515_tx_done()
301 k_sem_give(&dev_data->tx_sem); in mcp2515_tx_done()
307 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_get_core_clock()
309 *rate = dev_cfg->osc_freq / 2; in mcp2515_get_core_clock()
323 struct mcp2515_data *dev_data = dev->data; in mcp2515_set_timing()
327 return -EINVAL; in mcp2515_set_timing()
330 if (dev_data->common.started) { in mcp2515_set_timing()
331 return -EBUSY; in mcp2515_set_timing()
338 __ASSERT(timing->prescaler > 0, "Prescaler should be bigger than zero"); in mcp2515_set_timing()
339 uint8_t brp = timing->prescaler - 1; in mcp2515_set_timing()
340 uint8_t sjw = (timing->sjw - 1) << 6; in mcp2515_set_timing()
346 const uint8_t phseg1 = (timing->phase_seg1 - 1) << 3; in mcp2515_set_timing()
347 const uint8_t prseg = (timing->prop_seg - 1); in mcp2515_set_timing()
355 const uint8_t phseg2 = (timing->phase_seg2 - 1); in mcp2515_set_timing()
373 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp2515_set_timing()
397 k_mutex_unlock(&dev_data->mutex); in mcp2515_set_timing()
412 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_start()
413 struct mcp2515_data *dev_data = dev->data; in mcp2515_start()
416 if (dev_data->common.started) { in mcp2515_start()
417 return -EALREADY; in mcp2515_start()
420 if (dev_cfg->common.phy != NULL) { in mcp2515_start()
421 ret = can_transceiver_enable(dev_cfg->common.phy, dev_data->common.mode); in mcp2515_start()
430 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp2515_start()
432 ret = mcp2515_set_mode_int(dev, dev_data->mcp2515_mode); in mcp2515_start()
436 if (dev_cfg->common.phy != NULL) { in mcp2515_start()
438 (void)can_transceiver_disable(dev_cfg->common.phy); in mcp2515_start()
441 dev_data->common.started = true; in mcp2515_start()
444 k_mutex_unlock(&dev_data->mutex); in mcp2515_start()
451 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_stop()
452 struct mcp2515_data *dev_data = dev->data; in mcp2515_stop()
456 if (!dev_data->common.started) { in mcp2515_stop()
457 return -EALREADY; in mcp2515_stop()
460 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp2515_stop()
477 k_mutex_unlock(&dev_data->mutex); in mcp2515_stop()
481 dev_data->common.started = false; in mcp2515_stop()
483 k_mutex_unlock(&dev_data->mutex); in mcp2515_stop()
486 mcp2515_tx_done(dev, i, -ENETDOWN); in mcp2515_stop()
489 if (dev_cfg->common.phy != NULL) { in mcp2515_stop()
490 ret = can_transceiver_disable(dev_cfg->common.phy); in mcp2515_stop()
502 struct mcp2515_data *dev_data = dev->data; in mcp2515_set_mode()
504 if (dev_data->common.started) { in mcp2515_set_mode()
505 return -EBUSY; in mcp2515_set_mode()
510 dev_data->mcp2515_mode = MCP2515_MODE_NORMAL; in mcp2515_set_mode()
513 dev_data->mcp2515_mode = MCP2515_MODE_SILENT; in mcp2515_set_mode()
516 dev_data->mcp2515_mode = MCP2515_MODE_LOOPBACK; in mcp2515_set_mode()
520 return -ENOTSUP; in mcp2515_set_mode()
523 dev_data->common.mode = mode; in mcp2515_set_mode()
533 struct mcp2515_data *dev_data = dev->data; in mcp2515_send()
540 if (frame->dlc > CAN_MAX_DLC) { in mcp2515_send()
542 frame->dlc, CAN_MAX_DLC); in mcp2515_send()
543 return -EINVAL; in mcp2515_send()
546 if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0) { in mcp2515_send()
547 LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags); in mcp2515_send()
548 return -ENOTSUP; in mcp2515_send()
551 if (!dev_data->common.started) { in mcp2515_send()
552 return -ENETDOWN; in mcp2515_send()
555 if (k_sem_take(&dev_data->tx_sem, timeout) != 0) { in mcp2515_send()
556 return -EAGAIN; in mcp2515_send()
559 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp2515_send()
563 if ((BIT(tx_idx) & dev_data->tx_busy_map) == 0) { in mcp2515_send()
564 dev_data->tx_busy_map |= BIT(tx_idx); in mcp2515_send()
569 k_mutex_unlock(&dev_data->mutex); in mcp2515_send()
573 return -EIO; in mcp2515_send()
576 dev_data->tx_cb[tx_idx].cb = callback; in mcp2515_send()
577 dev_data->tx_cb[tx_idx].cb_arg = user_data; in mcp2515_send()
585 len = sizeof(tx_frame) - CAN_MAX_DLC + frame->dlc; in mcp2515_send()
601 struct mcp2515_data *dev_data = dev->data; in mcp2515_add_rx_filter()
606 if ((filter->flags & ~(CAN_FILTER_IDE)) != 0) { in mcp2515_add_rx_filter()
607 LOG_ERR("unsupported CAN filter flags 0x%02x", filter->flags); in mcp2515_add_rx_filter()
608 return -ENOTSUP; in mcp2515_add_rx_filter()
611 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp2515_add_rx_filter()
614 while ((BIT(filter_id) & dev_data->filter_usage) in mcp2515_add_rx_filter()
621 dev_data->filter_usage |= BIT(filter_id); in mcp2515_add_rx_filter()
623 dev_data->filter[filter_id] = *filter; in mcp2515_add_rx_filter()
624 dev_data->rx_cb[filter_id] = rx_cb; in mcp2515_add_rx_filter()
625 dev_data->cb_arg[filter_id] = cb_arg; in mcp2515_add_rx_filter()
628 filter_id = -ENOSPC; in mcp2515_add_rx_filter()
631 k_mutex_unlock(&dev_data->mutex); in mcp2515_add_rx_filter()
638 struct mcp2515_data *dev_data = dev->data; in mcp2515_remove_rx_filter()
645 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp2515_remove_rx_filter()
646 dev_data->filter_usage &= ~BIT(filter_id); in mcp2515_remove_rx_filter()
647 k_mutex_unlock(&dev_data->mutex); in mcp2515_remove_rx_filter()
654 struct mcp2515_data *dev_data = dev->data; in mcp2515_set_state_change_callback()
656 dev_data->common.state_change_cb = cb; in mcp2515_set_state_change_callback()
657 dev_data->common.state_change_cb_user_data = user_data; in mcp2515_set_state_change_callback()
663 struct mcp2515_data *dev_data = dev->data; in mcp2515_rx_filter()
669 if ((frame->flags & CAN_FRAME_RTR) != 0U) { in mcp2515_rx_filter()
674 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp2515_rx_filter()
677 if (!(BIT(filter_id) & dev_data->filter_usage)) { in mcp2515_rx_filter()
681 if (!can_frame_matches_filter(frame, &dev_data->filter[filter_id])) { in mcp2515_rx_filter()
685 callback = dev_data->rx_cb[filter_id]; in mcp2515_rx_filter()
689 callback(dev, &tmp_frame, dev_data->cb_arg[filter_id]); in mcp2515_rx_filter()
692 k_mutex_unlock(&dev_data->mutex); in mcp2515_rx_filter()
715 struct mcp2515_data *dev_data = dev->data; in mcp2515_get_state()
723 return -EIO; in mcp2515_get_state()
727 if (!dev_data->common.started) { in mcp2515_get_state()
745 return -EIO; in mcp2515_get_state()
748 err_cnt->tx_err_cnt = err_cnt_buf[0]; in mcp2515_get_state()
749 err_cnt->rx_err_cnt = err_cnt_buf[1]; in mcp2515_get_state()
761 return -EIO; in mcp2515_get_state()
771 struct mcp2515_data *dev_data = dev->data; in mcp2515_handle_errors()
772 can_state_change_callback_t state_change_cb = dev_data->common.state_change_cb; in mcp2515_handle_errors()
773 void *state_change_cb_data = dev_data->common.state_change_cb_user_data; in mcp2515_handle_errors()
784 if (state_change_cb && dev_data->old_state != state) { in mcp2515_handle_errors()
785 dev_data->old_state = state; in mcp2515_handle_errors()
792 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_handle_interrupts()
847 ret = gpio_pin_get_dt(&dev_cfg->int_gpio); in mcp2515_handle_interrupts()
863 struct mcp2515_data *dev_data = dev->data; in mcp2515_int_thread()
866 k_sem_take(&dev_data->int_sem, K_FOREVER); in mcp2515_int_thread()
877 k_sem_give(&dev_data->int_sem); in mcp2515_int_gpio_callback()
912 const struct mcp2515_config *dev_cfg = dev->config; in mcp2515_init()
913 struct mcp2515_data *dev_data = dev->data; in mcp2515_init()
918 k_sem_init(&dev_data->int_sem, 0, 1); in mcp2515_init()
919 k_mutex_init(&dev_data->mutex); in mcp2515_init()
920 k_sem_init(&dev_data->tx_sem, MCP2515_TX_CNT, MCP2515_TX_CNT); in mcp2515_init()
922 if (dev_cfg->common.phy != NULL) { in mcp2515_init()
923 if (!device_is_ready(dev_cfg->common.phy)) { in mcp2515_init()
925 return -ENODEV; in mcp2515_init()
929 if (!spi_is_ready_dt(&dev_cfg->bus)) { in mcp2515_init()
930 LOG_ERR("SPI bus %s not ready", dev_cfg->bus.bus->name); in mcp2515_init()
931 return -ENODEV; in mcp2515_init()
936 LOG_ERR("Soft-reset failed"); in mcp2515_init()
937 return -EIO; in mcp2515_init()
941 if (!gpio_is_ready_dt(&dev_cfg->int_gpio)) { in mcp2515_init()
943 return -ENODEV; in mcp2515_init()
946 if (gpio_pin_configure_dt(&dev_cfg->int_gpio, GPIO_INPUT)) { in mcp2515_init()
948 return -EINVAL; in mcp2515_init()
951 gpio_init_callback(&(dev_data->int_gpio_cb), mcp2515_int_gpio_callback, in mcp2515_init()
952 BIT(dev_cfg->int_gpio.pin)); in mcp2515_init()
954 if (gpio_add_callback(dev_cfg->int_gpio.port, in mcp2515_init()
955 &(dev_data->int_gpio_cb))) { in mcp2515_init()
956 return -EINVAL; in mcp2515_init()
959 if (gpio_pin_interrupt_configure_dt(&dev_cfg->int_gpio, in mcp2515_init()
961 return -EINVAL; in mcp2515_init()
964 tid = k_thread_create(&dev_data->int_thread, dev_data->int_thread_stack, in mcp2515_init()
965 dev_cfg->int_thread_stack_size, in mcp2515_init()
967 NULL, NULL, K_PRIO_COOP(dev_cfg->int_thread_priority), in mcp2515_init()
971 (void)memset(dev_data->rx_cb, 0, sizeof(dev_data->rx_cb)); in mcp2515_init()
972 (void)memset(dev_data->filter, 0, sizeof(dev_data->filter)); in mcp2515_init()
973 dev_data->old_state = CAN_STATE_ERROR_ACTIVE; in mcp2515_init()
975 ret = can_calc_timing(dev, &timing, dev_cfg->common.bitrate, in mcp2515_init()
976 dev_cfg->common.sample_point); in mcp2515_init()
977 if (ret == -EINVAL) { in mcp2515_init()
979 return -EIO; in mcp2515_init()
984 LOG_DBG("Sample-point err : %d", ret); in mcp2515_init()