|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <sys/param.h> |
| 8 | +#include "esp_check.h" |
| 9 | +#include "esp_twai.h" |
| 10 | +#include "esp_private/twai_interface.h" |
| 11 | +#include "esp_private/twai_priv.h" |
| 12 | + |
| 13 | +static const char *TAG = "esp_twai"; |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief Calculate twai timing param by giving bitrate and hardware limit. |
| 17 | + * |
| 18 | + * +---------------------------------------------------+ |
| 19 | + * | bit time in time quanta (total_tq) | |
| 20 | + * +--------------+----------+------------+------------+ |
| 21 | + * | sync_seg | prop_seg | phase_seg1 | phase_seg2 | |
| 22 | + * +--------------+----------+------------+------------+ |
| 23 | + * | 1 | tseg1 | tseg2 | |
| 24 | + * +--------------+----------+------------+------------+ |
| 25 | + * | tseg2/2 ^ ^ |
| 26 | + * sjw sample_point |
| 27 | + */ |
| 28 | +uint32_t twai_node_timing_calc_param(const uint32_t source_freq, const twai_timing_basic_config_t *in_param, const twai_timing_constraint_t *hw_limit, twai_timing_advanced_config_t *out_param) |
| 29 | +{ |
| 30 | + uint32_t total_div = (source_freq + in_param->bitrate / 2) / in_param->bitrate; |
| 31 | + uint32_t pre_div = hw_limit->brp_min; |
| 32 | + uint16_t tseg = 0; |
| 33 | + for (; pre_div <= hw_limit->brp_max; pre_div ++) { |
| 34 | + tseg = total_div / pre_div; |
| 35 | + if (total_div != tseg * pre_div) { |
| 36 | + continue; // no integer tseg |
| 37 | + } |
| 38 | + if ((tseg < (hw_limit->tseg1_max + hw_limit->tseg2_max)) && (tseg >= (hw_limit->tseg1_min + hw_limit->tseg2_min))) { |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + if (pre_div > hw_limit->brp_max) { // no valid pre_div |
| 43 | + return 0; |
| 44 | + } |
| 45 | + |
| 46 | + uint16_t default_point = (in_param->bitrate >= 800000) ? 750 : ((in_param->bitrate >= 500000) ? 800 : 875); |
| 47 | + uint16_t sample_point = in_param->sample_point ? in_param->sample_point : default_point; // default sample point based on bitrate if not configured |
| 48 | + uint16_t tseg_1 = (tseg * sample_point) / 1000; |
| 49 | + tseg_1 = MAX(hw_limit->tseg1_min, MIN(tseg_1, hw_limit->tseg1_max)); |
| 50 | + uint16_t tseg_2 = tseg - tseg_1 - 1; |
| 51 | + tseg_2 = MAX(hw_limit->tseg2_min, MIN(tseg_2, hw_limit->tseg2_max)); |
| 52 | + tseg_1 = tseg - tseg_2 - 1; |
| 53 | + uint16_t prop = tseg_1 / 2; // distribute tseg1 evenly between prop_seg and tseg_1 |
| 54 | + tseg_1 -= prop; |
| 55 | + |
| 56 | + out_param->quanta_resolution_hz = 0; // going to deprecated IDF-12725 |
| 57 | + out_param->brp = pre_div; |
| 58 | + out_param->prop_seg = prop; |
| 59 | + out_param->tseg_1 = tseg_1; |
| 60 | + out_param->tseg_2 = tseg_2; |
| 61 | + out_param->sjw = MAX(1, MIN(tseg_2 >> 1, hw_limit->sjw_max)); |
| 62 | + out_param->ssp_offset = (tseg * in_param->ssp_permill) / 1000; // ssp is optional, default 0 if not configured |
| 63 | + |
| 64 | + return source_freq / (pre_div * (prop + tseg_1 + tseg_2 + 1)); |
| 65 | +} |
| 66 | + |
| 67 | +esp_err_t twai_node_enable(twai_node_handle_t node) |
| 68 | +{ |
| 69 | + ESP_RETURN_ON_FALSE(node, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null handle"); |
| 70 | + ESP_RETURN_ON_FALSE(node->enable, ESP_ERR_NOT_SUPPORTED, TAG, "enable func null"); |
| 71 | + |
| 72 | + return node->enable(node); |
| 73 | +} |
| 74 | + |
| 75 | +esp_err_t twai_node_disable(twai_node_handle_t node) |
| 76 | +{ |
| 77 | + ESP_RETURN_ON_FALSE(node, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null handle"); |
| 78 | + ESP_RETURN_ON_FALSE(node->disable, ESP_ERR_NOT_SUPPORTED, TAG, "disable func null"); |
| 79 | + |
| 80 | + return node->disable(node); |
| 81 | +} |
| 82 | + |
| 83 | +esp_err_t twai_node_delete(twai_node_handle_t node) |
| 84 | +{ |
| 85 | + ESP_RETURN_ON_FALSE(node, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null handle"); |
| 86 | + ESP_RETURN_ON_FALSE(node->del, ESP_ERR_NOT_SUPPORTED, TAG, "delete func null"); |
| 87 | + |
| 88 | + return node->del(node); |
| 89 | +} |
| 90 | + |
| 91 | +esp_err_t twai_node_config_mask_filter(twai_node_handle_t node, uint8_t filter_id, const twai_mask_filter_config_t *mask_cfg) |
| 92 | +{ |
| 93 | + ESP_RETURN_ON_FALSE(node && mask_cfg, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null"); |
| 94 | + ESP_RETURN_ON_FALSE(node->config_mask_filter, ESP_ERR_NOT_SUPPORTED, TAG, "config_mask_filter func null"); |
| 95 | + |
| 96 | + return node->config_mask_filter(node, filter_id, mask_cfg); |
| 97 | +} |
| 98 | + |
| 99 | +esp_err_t twai_node_config_range_filter(twai_node_handle_t node, uint8_t filter_id, const twai_range_filter_config_t *range_cfg) |
| 100 | +{ |
| 101 | + ESP_RETURN_ON_FALSE(node && range_cfg, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null"); |
| 102 | + ESP_RETURN_ON_FALSE(node->config_range_filter, ESP_ERR_NOT_SUPPORTED, TAG, "config_range_filter func null"); |
| 103 | + |
| 104 | + return node->config_range_filter(node, filter_id, range_cfg); |
| 105 | +} |
| 106 | + |
| 107 | +esp_err_t twai_node_reconfig_timing(twai_node_handle_t node, const twai_timing_advanced_config_t *bit_timing, const twai_timing_advanced_config_t *data_timing) |
| 108 | +{ |
| 109 | + ESP_RETURN_ON_FALSE(node && (bit_timing || data_timing), ESP_ERR_INVALID_ARG, TAG, "invalid argument: null"); |
| 110 | + ESP_RETURN_ON_FALSE(node->timing_reconfig, ESP_ERR_NOT_SUPPORTED, TAG, "timing_reconfig func null"); |
| 111 | + |
| 112 | + return node->timing_reconfig(node, bit_timing, data_timing); |
| 113 | +} |
| 114 | + |
| 115 | +esp_err_t twai_node_register_event_callbacks(twai_node_handle_t node, const twai_event_callbacks_t *cbs, void *user_data) |
| 116 | +{ |
| 117 | + ESP_RETURN_ON_FALSE(node && cbs, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null"); |
| 118 | + ESP_RETURN_ON_FALSE(node->register_cbs, ESP_ERR_NOT_SUPPORTED, TAG, "register_cbs func null"); |
| 119 | + |
| 120 | + return node->register_cbs(node, cbs, user_data); |
| 121 | +} |
| 122 | + |
| 123 | +esp_err_t twai_node_recover(twai_node_handle_t node) |
| 124 | +{ |
| 125 | + ESP_RETURN_ON_FALSE(node, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null handle"); |
| 126 | + ESP_RETURN_ON_FALSE(node->recover, ESP_ERR_NOT_SUPPORTED, TAG, "recover func null"); |
| 127 | + |
| 128 | + return node->recover(node); |
| 129 | +} |
| 130 | + |
| 131 | +esp_err_t twai_node_get_info(twai_node_handle_t node, twai_node_status_t *status_ret, twai_node_record_t *statistics_ret) |
| 132 | +{ |
| 133 | + ESP_RETURN_ON_FALSE(node, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null handle"); |
| 134 | + ESP_RETURN_ON_FALSE(node->get_info, ESP_ERR_NOT_SUPPORTED, TAG, "get_info func null"); |
| 135 | + |
| 136 | + return node->get_info(node, status_ret, statistics_ret); |
| 137 | +} |
| 138 | + |
| 139 | +esp_err_t twai_node_transmit(twai_node_handle_t node, const twai_frame_t *frame, int timeout_ms) |
| 140 | +{ |
| 141 | + ESP_RETURN_ON_FALSE(node && frame, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null"); |
| 142 | + ESP_RETURN_ON_FALSE(node->transmit, ESP_ERR_NOT_SUPPORTED, TAG, "transmit func null"); |
| 143 | + |
| 144 | + return node->transmit(node, frame, timeout_ms); |
| 145 | +} |
| 146 | + |
| 147 | +esp_err_t twai_node_receive_from_isr(twai_node_handle_t node, twai_frame_header_t *header, uint8_t *rx_buffer, size_t buf_sz, size_t *received_len) |
| 148 | +{ |
| 149 | + ESP_RETURN_ON_FALSE_ISR(node && header, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null"); |
| 150 | + ESP_RETURN_ON_FALSE_ISR(node->receive_isr, ESP_ERR_NOT_SUPPORTED, TAG, "receive func null"); |
| 151 | + |
| 152 | + return node->receive_isr(node, header, rx_buffer, buf_sz, received_len); |
| 153 | +} |
0 commit comments