1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 #include <stdint.h> 22 #include <stdbool.h> 23 #include "sdkconfig.h" 24 #include "soc/soc_caps.h" 25 26 /** 27 * @brief TWAI Constants 28 */ 29 #define TWAI_EXTD_ID_MASK 0x1FFFFFFF /**< Bit mask for 29 bit Extended Frame Format ID */ 30 #define TWAI_STD_ID_MASK 0x7FF /**< Bit mask for 11 bit Standard Frame Format ID */ 31 #define TWAI_FRAME_MAX_DLC 8 /**< Max data bytes allowed in TWAI */ 32 #define TWAI_FRAME_EXTD_ID_LEN_BYTES 4 /**< EFF ID requires 4 bytes (29bit) */ 33 #define TWAI_FRAME_STD_ID_LEN_BYTES 2 /**< SFF ID requires 2 bytes (11bit) */ 34 #define TWAI_ERR_PASS_THRESH 128 /**< Error counter threshold for error passive */ 35 36 /** @cond */ //Doxy command to hide preprocessor definitions from docs 37 /** 38 * @brief TWAI Message flags 39 * 40 * The message flags are used to indicate the type of message transmitted/received. 41 * Some flags also specify the type of transmission. 42 */ 43 #define TWAI_MSG_FLAG_NONE 0x00 /**< No message flags (Standard Frame Format) */ 44 #define TWAI_MSG_FLAG_EXTD 0x01 /**< Extended Frame Format (29bit ID) */ 45 #define TWAI_MSG_FLAG_RTR 0x02 /**< Message is a Remote Frame */ 46 #define TWAI_MSG_FLAG_SS 0x04 /**< Transmit as a Single Shot Transmission. Unused for received. */ 47 #define TWAI_MSG_FLAG_SELF 0x08 /**< Transmit as a Self Reception Request. Unused for received. */ 48 #define TWAI_MSG_FLAG_DLC_NON_COMP 0x10 /**< Message's Data length code is larger than 8. This will break compliance with TWAI */ 49 50 #define TWAI_BRP_MAX SOC_TWAI_BRP_MAX /**< Maximum configurable BRP value */ 51 #define TWAI_BRP_MIN SOC_TWAI_BRP_MIN /**< Minimum configurable BRP value */ 52 53 54 /** 55 * @brief Initializer macros for timing configuration structure 56 * 57 * The following initializer macros offer commonly found bit rates. These macros 58 * place the sample point at 80% or 67% of a bit time. 59 * 60 * @note These timing values are based on the assumption APB clock is at 80MHz 61 * @note The available bit rates are dependent on the chip target and revision. 62 */ 63 #if (SOC_TWAI_BRP_MAX > 256) 64 #define TWAI_TIMING_CONFIG_1KBITS() {.brp = 4000, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 65 #define TWAI_TIMING_CONFIG_5KBITS() {.brp = 800, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 66 #define TWAI_TIMING_CONFIG_10KBITS() {.brp = 400, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 67 #endif 68 #if (SOC_TWAI_BRP_MAX > 128) || (CONFIG_ESP32_REV_MIN >= 2) 69 #define TWAI_TIMING_CONFIG_12_5KBITS() {.brp = 256, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} 70 #define TWAI_TIMING_CONFIG_16KBITS() {.brp = 200, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} 71 #define TWAI_TIMING_CONFIG_20KBITS() {.brp = 200, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 72 #endif 73 #define TWAI_TIMING_CONFIG_25KBITS() {.brp = 128, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} 74 #define TWAI_TIMING_CONFIG_50KBITS() {.brp = 80, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 75 #define TWAI_TIMING_CONFIG_100KBITS() {.brp = 40, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 76 #define TWAI_TIMING_CONFIG_125KBITS() {.brp = 32, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 77 #define TWAI_TIMING_CONFIG_250KBITS() {.brp = 16, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 78 #define TWAI_TIMING_CONFIG_500KBITS() {.brp = 8, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 79 #define TWAI_TIMING_CONFIG_800KBITS() {.brp = 4, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} 80 #define TWAI_TIMING_CONFIG_1MBITS() {.brp = 4, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} 81 82 /** 83 * @brief Initializer macro for filter configuration to accept all IDs 84 */ 85 #define TWAI_FILTER_CONFIG_ACCEPT_ALL() {.acceptance_code = 0, .acceptance_mask = 0xFFFFFFFF, .single_filter = true} 86 /** @endcond */ 87 88 /** 89 * @brief TWAI Controller operating modes 90 */ 91 typedef enum { 92 TWAI_MODE_NORMAL, /**< Normal operating mode where TWAI controller can send/receive/acknowledge messages */ 93 TWAI_MODE_NO_ACK, /**< Transmission does not require acknowledgment. Use this mode for self testing */ 94 TWAI_MODE_LISTEN_ONLY, /**< The TWAI controller will not influence the bus (No transmissions or acknowledgments) but can receive messages */ 95 } twai_mode_t; 96 97 /** 98 * @brief Structure to store a TWAI message 99 * 100 * @note The flags member is deprecated 101 */ 102 typedef struct { 103 union { 104 struct { 105 //The order of these bits must match deprecated message flags for compatibility reasons 106 uint32_t extd: 1; /**< Extended Frame Format (29bit ID) */ 107 uint32_t rtr: 1; /**< Message is a Remote Frame */ 108 uint32_t ss: 1; /**< Transmit as a Single Shot Transmission. Unused for received. */ 109 uint32_t self: 1; /**< Transmit as a Self Reception Request. Unused for received. */ 110 uint32_t dlc_non_comp: 1; /**< Message's Data length code is larger than 8. This will break compliance with ISO 11898-1 */ 111 uint32_t reserved: 27; /**< Reserved bits */ 112 }; 113 //Todo: Deprecate flags 114 uint32_t flags; /**< Deprecated: Alternate way to set bits using message flags */ 115 }; 116 uint32_t identifier; /**< 11 or 29 bit identifier */ 117 uint8_t data_length_code; /**< Data length code */ 118 uint8_t data[TWAI_FRAME_MAX_DLC]; /**< Data bytes (not relevant in RTR frame) */ 119 } twai_message_t; 120 121 /** 122 * @brief Structure for bit timing configuration of the TWAI driver 123 * 124 * @note Macro initializers are available for this structure 125 */ 126 typedef struct { 127 uint32_t brp; /**< Baudrate prescaler (i.e., APB clock divider). Any even number from 2 to 128 for ESP32, 2 to 32768 for ESP32S2. 128 For ESP32 Rev 2 or later, multiples of 4 from 132 to 256 are also supported */ 129 uint8_t tseg_1; /**< Timing segment 1 (Number of time quanta, between 1 to 16) */ 130 uint8_t tseg_2; /**< Timing segment 2 (Number of time quanta, 1 to 8) */ 131 uint8_t sjw; /**< Synchronization Jump Width (Max time quanta jump for synchronize from 1 to 4) */ 132 bool triple_sampling; /**< Enables triple sampling when the TWAI controller samples a bit */ 133 } twai_timing_config_t; 134 135 /** 136 * @brief Structure for acceptance filter configuration of the TWAI driver (see documentation) 137 * 138 * @note Macro initializers are available for this structure 139 */ 140 typedef struct { 141 uint32_t acceptance_code; /**< 32-bit acceptance code */ 142 uint32_t acceptance_mask; /**< 32-bit acceptance mask */ 143 bool single_filter; /**< Use Single Filter Mode (see documentation) */ 144 } twai_filter_config_t; 145 146 #ifdef __cplusplus 147 } 148 #endif 149