1 /** 2 * @file qdec.h 3 * @brief Quadrature Encoder function prototypes and data types. 4 */ 5 6 /****************************************************************************** 7 * 8 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by 9 * Analog Devices, Inc.), 10 * Copyright (C) 2023-2024 Analog Devices, Inc. 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 * 24 ******************************************************************************/ 25 26 /* Define to prevent redundant inclusion */ 27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_QDEC_H_ 28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_QDEC_H_ 29 30 /* **** Includes **** */ 31 #include <stdint.h> 32 #include "qdec_regs.h" 33 #include "mcr_regs.h" 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /** 40 * @defgroup qdec QDEC 41 * @ingroup periphlibs 42 * @{ 43 */ 44 45 /*************************************************************************************************************** 46 DATA STRUCTURES FOR QDEC INITIALIZATION 47 ***************************************************************************************************************/ 48 49 /** 50 * @brief Enumeration type for the QDEC Counter Modes 51 */ 52 typedef enum { 53 MXC_QDEC_X1_MODE = 0, ///< Select Channel 0 54 MXC_QDEC_X2_MODE = 1, ///< Select Channel 1 55 MXC_QDEC_x4_MODE, ///< Select Channel 2 56 } mxc_qdec_counter_mode_t; 57 58 /** 59 * @brief Enumeration type for QDEC Phase Swap 60 */ 61 typedef enum { 62 MXC_QDEC_SWAP_CW_A_LEADS_B, ///< QDEC Clockwise QEA leads QEB 63 MXC_QDEC_SWAP_CW_B_LEADS_A, ///< QDEC Clockwise QEB leads QEA 64 } mxc_qdec_swap_t; 65 66 /** 67 * @brief Enumeration type for QDEC Filtering 68 */ 69 typedef enum { 70 MXC_QDEC_FILTER_1_SAMPLE = 71 MXC_S_QDEC_CTRL_FILTER_1_SAMPLE, ///< QDEC Pulse active for one clock cycle 72 MXC_QDEC_FILTER_2_SAMPLES = 73 MXC_S_QDEC_CTRL_FILTER_2_SAMPLES, ///< QDEC Pulse active for one clock cycle 74 MXC_QDEC_FILTER_3_SAMPLES = 75 MXC_S_QDEC_CTRL_FILTER_3_SAMPLES, ///< QDEC Pulse active for one clock cycle 76 MXC_QDEC_FILTER_4_SAMPLES = 77 MXC_S_QDEC_CTRL_FILTER_4_SAMPLES, ///< QDEC Pulse active for one clock cycle 78 } mxc_qdec_filter_t; 79 80 /** 81 * @brief Enumeration type for QDEC Sticky Condition 82 */ 83 typedef enum { 84 MXC_QDEC_STICKY_PULSE = 0, ///< QDEC Pulse active for one clock cycle 85 MXC_QDEC_STICKY_MIRROR, ///< QDEC Mirror state 86 } mxc_qdec_sticky_t; 87 88 /** 89 * @brief Enumeration type for QDEC clock divider 90 */ 91 typedef enum { 92 MXC_QDEC_CLKDIV_1 = 0, ///< QDEC Scale by 1 93 MXC_QDEC_CLKDIV_2, ///< QDEC Scale by 1/2 94 MXC_QDEC_CLKDIV_4, ///< QDEC Scale by 1/4 95 MXC_QDEC_CLKDIV_8, ///< QDEC Scale by 1/8 96 MXC_QDEC_CLKDIV_16, ///< QDEC Scale by 1/16 97 MXC_QDEC_CLKDIV_32, ///< QDEC Scale by 1/32 98 MXC_QDEC_CLKDIV_64, ///< QDEC Scale by 1/64 99 MXC_QDEC_CLKDIV_128, ///< QDEC Scale by 1/128 100 } mxc_qdec_clkdiv_t; 101 102 /** 103 * @brief Enumeration type for QDEC Reset on select 104 */ 105 typedef enum { 106 MXC_QDEC_RST_ON_MAXCNT = 0, 107 MXC_QDEC_RST_ON_INDEX, 108 } mxc_qdec_rst_on_t; 109 110 /** 111 * @brief Enumeration type for QDEC function states 112 */ 113 typedef enum { 114 MXC_QDEC_NONE = 0, 115 MXC_QDEC_CAPTURE, 116 MXC_QDEC_COMPARE, 117 } mxc_qdec_function_t; 118 119 ///< Callback used when interrupt occurs 120 typedef void (*mxc_qdec_cb_t)(void *req, int error); 121 122 typedef struct { 123 mxc_qdec_counter_mode_t mode; ///< counter mode 124 mxc_qdec_swap_t swap; ///< phase swap 125 mxc_qdec_filter_t sample; ///< filter 126 mxc_qdec_clkdiv_t clkdiv; ///< clock divider 127 mxc_qdec_sticky_t sticky; ///< sticky condition 128 mxc_qdec_rst_on_t rst; ///< reset on maxcnt or index 129 mxc_qdec_function_t func; ///< compare, capture, none 130 uint32_t maxcnt; ///< Maximum count value 131 uint32_t initial; ///< Initial count value 132 uint32_t compare; ///< Compare value 133 mxc_qdec_cb_t callback; 134 } mxc_qdec_req_t; 135 136 /** 137 * @brief Performs the QDEC startup procedure 138 * 139 * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. 140 */ 141 int MXC_QDEC_Init(mxc_qdec_req_t *req); 142 143 /** 144 * @brief Shuts down the QDEC 145 * 146 * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. 147 */ 148 int MXC_QDEC_Shutdown(void); 149 150 /** 151 * @brief Enable specific QDEC interrupts 152 * 153 * @param flags mask of interrupt flags to enables 154 */ 155 void MXC_QDEC_EnableInt(uint32_t flags); 156 157 /** 158 * @brief Disable specific QDEC interrupts 159 * 160 * @param flags mask of interrupt flags to enables 161 */ 162 void MXC_QDEC_DisableInt(uint32_t flags); 163 164 /** 165 * @brief Retrieves current interrupt flag status 166 * 167 * @return active flags 168 */ 169 int MXC_QDEC_GetFlags(void); 170 171 /** 172 * @brief Clear interrupt flags 173 * 174 * @param flags mask of flags to clear 175 */ 176 void MXC_QDEC_ClearFlags(uint32_t flags); 177 178 /** 179 * @brief Sets the Maximum Poisiton Count for the QDEC 180 * 181 * @param maximum position value 182 */ 183 void MXC_QDEC_SetMaxCount(uint32_t maxCount); 184 185 /** 186 * @brief Retrieves Maximum position value 187 * 188 * @return maximum position value 189 */ 190 int MXC_QDEC_GetMaxCount(void); 191 192 /** 193 * @brief Sets the Initial (Minimum) Poisiton Count for the QDEC 194 * 195 * @param minimum position value 196 */ 197 void MXC_QDEC_SetInitial(uint32_t initial); 198 199 /** 200 * @brief Retrieves Initial position value 201 * 202 * @return minimum position value 203 */ 204 int MXC_QDEC_GetInitial(void); 205 206 /** 207 * @brief Sets the Compare value for the QDEC 208 * 209 * @param compare register value 210 */ 211 void MXC_QDEC_SetCompare(uint32_t compare); 212 213 /** 214 * @brief Retrieves Compare value 215 * 216 * @return value in compare register 217 */ 218 int MXC_QDEC_GetCompare(void); 219 220 /** 221 * @brief Retrieves Index value 222 * 223 * @return value in index register 224 */ 225 int MXC_QDEC_GetIndex(void); 226 227 /** 228 * @brief Retrieves capture value 229 * 230 * @return capture value latched from position register 231 */ 232 int MXC_QDEC_GetCapture(void); 233 234 /** 235 * @brief Call this function from the QDEC ISR when using Async API 236 * functions 237 * 238 * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. 239 */ 240 int MXC_QDEC_Handler(void); 241 242 /** 243 * @brief Gets the current position of the QDEC 244 * 245 * @return see \ref MXC_Error_Codes for a list of return codes. 246 */ 247 int MXC_QDEC_GetPosition(void); 248 249 /** 250 * @brief Gets the direction (Clockwise/Counter-Clockwise) of the QDEC 251 * 252 * @return see \ref MXC_Error_Codes for a list of return codes. 253 */ 254 int MXC_QDEC_GetDirection(void); 255 256 /**@} end of group qdec */ 257 258 #ifdef __cplusplus 259 } 260 #endif 261 262 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_QDEC_H_ 263