1 /**
2  * @file
3  * @brief      Registers, Bit Masks and Bit Positions for the 1-Wire Master
4  *             peripheral module.
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_MAX32650_OWM_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_OWM_H_
29 
30 /* **** Includes **** */
31 #include "mxc_sys.h"
32 #include "owm_regs.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @ingroup periphlibs
40  * @defgroup owm 1-Wire Master (OWM)
41  * @{
42  */
43 
44 /* **** Definitions **** */
45 
46 /**
47  * Enumeration type for specifying options for 1-Wire external pullup mode.
48  */
49 typedef enum {
50     MXC_OWM_EXT_PU_ACT_HIGH = 0, /**< Pullup pin is active high when enabled.        */
51     MXC_OWM_EXT_PU_ACT_LOW = 1, /**< Pullup pin is active low when enabled.         */
52     MXC_OWM_EXT_PU_UNUSED = 2, /**< Pullup pin is not used for an external pullup. */
53 } mxc_owm_ext_pu_t;
54 
55 /**
56  * Structure type for 1-Wire Master configuration.
57  */
58 typedef struct {
59     uint8_t int_pu_en; /**< 1 = internal pullup on.   */
60     mxc_owm_ext_pu_t ext_pu_mode; /**< See #owm_ext_pu_t.   */
61     uint8_t long_line_mode; /**< 1 = long line mode enable.    */
62     // owm_overdrive_t overdrive_spec; /**< 0 = timeslot is 12us, 1 = timeslot is 10us.   */
63 } mxc_owm_cfg_t;
64 
65 #define READ_ROM_COMMAND 0x33 /**< Read ROM Command */
66 #define MATCH_ROM_COMMAND 0x55 /**< Match ROM Command */
67 #define SEARCH_ROM_COMMAND 0xF0 /**< Search ROM Command */
68 #define SKIP_ROM_COMMAND 0xCC /**< Skip ROM Command */
69 #define OD_SKIP_ROM_COMMAND 0x3C /**< Overdrive Skip ROM Command */
70 #define OD_MATCH_ROM_COMMAND 0x69 /**< Overdrive Match ROM Command */
71 #define RESUME_COMMAND 0xA5 /**< Resume Command */
72 
73 /* **** Globals **** */
74 
75 /* **** Function Prototypes **** */
76 
77 /**
78  * @brief   Initialize and enable OWM module.
79  * @param   cfg         Pointer to OWM configuration.
80  *
81  * @return  #E_NO_ERROR if everything is successful
82  * @return  #E_NULL_PTR if parameter is a null pointer
83  * @return  #E_BUSY if IOMAN was not configured correctly
84  * @return  #E_UNINITIALIZED if OWM CLK disabled
85  * @return  #E_NOT_SUPPORTED if 1MHz CLK cannot be created with given system and owm CLK
86  * @return  #E_BAD_PARAM if bad cfg parameter passed in
87  */
88 int MXC_OWM_Init(const mxc_owm_cfg_t *cfg);
89 
90 /**
91  * @brief   Shutdown OWM module.
92  */
93 void MXC_OWM_Shutdown(void);
94 
95 /**
96  * @brief   Send 1-Wire reset pulse. Will block until transaction is complete.
97  *
98  * @return  0 if no 1-wire devices reponded during the presence pulse, 1 otherwise
99  */
100 int MXC_OWM_Reset(void);
101 
102 /**
103  * @brief   Get the presence pulse detect status.
104  * @return  0 if no 1-wire devices reponded during the presence pulse, 1 otherwise
105  */
106 int MXC_OWM_GetPresenceDetect(void);
107 
108 /**
109  * @brief   Send and receive one byte of data. Will block until transaction is complete.
110  * @param   data        data to send
111  * @return  data read (1 byte)
112  */
113 int MXC_OWM_TouchByte(uint8_t data);
114 
115 /**
116  * @brief   Write one byte of data. Will block until transaction is complete.
117  * @param   data        data to send
118  * @return  #E_NO_ERROR if everything is successful
119  * @return  #E_COMM_ERR if data written != data parameter
120  */
121 int MXC_OWM_WriteByte(uint8_t data);
122 
123 /**
124  * @brief   Read one byte of data. Will block until transaction is complete.
125  * @return  data read (1 byte)
126  */
127 int MXC_OWM_ReadByte(void);
128 
129 /**
130  * @brief   Send and receive one bit of data. Will block until transaction is complete.
131  * @param   bit         bit to send
132  * @return  bit read
133  */
134 int MXC_OWM_TouchBit(uint8_t bit);
135 
136 /**
137  * @brief   Write one bit of data. Will block until transaction is complete.
138  * @param   bit         bit to send
139  * @return  #E_NO_ERROR if everything is successful
140  * @return  #E_COMM_ERR if bit written != bit parameter
141  */
142 int MXC_OWM_WriteBit(uint8_t bit);
143 
144 /**
145  * @brief   Read one bit of data. Will block until transaction is complete.
146  * @return  bit read
147  */
148 int MXC_OWM_ReadBit(void);
149 
150 /**
151  * @brief   Write multiple bytes of data. Will block until transaction is complete.
152  * @param   data    Pointer to buffer for write data.
153  * @param   len     Number of bytes to write.
154  *
155  * @return  Number of bytes written if successful
156  * @return  #E_COMM_ERR if line short detected before transaction
157  */
158 int MXC_OWM_Write(uint8_t *data, int len);
159 
160 /**
161  * @brief   Read multiple bytes of data. Will block until transaction is complete.
162  * @param   data    Pointer to buffer for read data.
163  * @param   len     Number of bytes to read.
164  *
165  * @return Number of bytes read if successful
166  * @return #E_COMM_ERR if line short detected before transaction
167  */
168 int MXC_OWM_Read(uint8_t *data, int len);
169 
170 /**
171  * @brief   Starts 1-Wire communication with Read ROM command
172  * @note    Only use the Read ROM command with one slave on the bus
173  * @param   ROMCode     Pointer to buffer for ROM code read
174  * @return  #E_NO_ERROR if everything is successful
175  * @return  #E_COMM_ERR if reset, read or write fails
176  */
177 int MXC_OWM_ReadROM(uint8_t *ROMCode);
178 
179 /**
180  * @brief   Starts 1-Wire communication with Match ROM command
181  * @param   ROMCode     Pointer to buffer with ROM code to match
182  * @return  #E_NO_ERROR if everything is successful
183  * @return  #E_COMM_ERR if reset or write fails
184  */
185 int MXC_OWM_MatchROM(uint8_t *ROMCode);
186 
187 /**
188  * @brief   Starts 1-Wire communication with Overdrive Match ROM command
189  * @note    After Overdrive Match ROM command is sent, the OWM is set to
190  *          overdrive speed. To set back to standard speed use OWM_SetOverdrive.
191  * @param   ROMCode     Pointer to buffer with ROM code to match
192  * @return  #E_NO_ERROR if everything is successful
193  * @return  #E_COMM_ERR if reset or write fails
194  */
195 int MXC_OWM_ODMatchROM(uint8_t *ROMCode);
196 
197 /**
198  * @brief   Starts 1-Wire communication with Skip ROM command
199  * @return  #E_NO_ERROR if everything is successful
200  * @return  #E_COMM_ERR if reset or write fails
201  */
202 int MXC_OWM_SkipROM(void);
203 
204 /**
205  * @brief   Starts 1-Wire communication with Overdrive Skip ROM command
206  * @note    After Overdrive Skip ROM command is sent, the OWM is set to
207  *          overdrive speed. To set back to standard speed use OWM_SetOverdrive
208  * @return  #E_NO_ERROR if everything is successful
209  * @return  #E_COMM_ERR if reset or write fails
210  */
211 int MXC_OWM_ODSkipROM(void);
212 
213 /**
214  * @brief   Starts 1-Wire communication with Resume command
215  * @return  #E_NO_ERROR if everything is successful
216  * @return  #E_COMM_ERR if reset or write fails
217  */
218 int MXC_OWM_Resume(void);
219 
220 /**
221  * @brief   Starts 1-Wire communication with Search ROM command
222  * @param   newSearch   (1) = start new search, (0) = continue search for next ROM
223  * @param   ROMCode     Pointer to buffer with ROM code found
224  * @return  (1) = ROM found, (0) = no new ROM found, end of search
225  */
226 int MXC_OWM_SearchROM(int newSearch, uint8_t *ROMCode);
227 
228 /**
229  * @brief   Clear interrupt flags.
230  * @param   mask        Mask of interrupts to clear.
231  */
232 void MXC_OWM_ClearFlags(uint32_t mask);
233 
234 /**
235  * @brief   Get interrupt flags.
236  * @return  Mask of active flags.
237  */
238 unsigned MXC_OWM_GetFlags(void);
239 
240 /**
241  * @brief   Enables/Disables the External pullup
242  * @param   enable      (1) = enable, (0) = disable
243  */
244 void MXC_OWM_SetExtPullup(int enable);
245 
246 /**
247  * @brief   Enables/Disables Overdrive speed
248  * @param   enable      (1) = overdrive, (0) = standard
249  */
250 void MXC_OWM_SetOverdrive(int enable);
251 
252 /**
253  * @brief   Enables interrupts
254  * @param   flags      which owm interrupts to enable
255  */
256 void MXC_OWM_EnableInt(int flags);
257 
258 /**
259  * @brief   Disables interrupts
260  * @param   flags      which owm interrupts to disable
261  */
262 void MXC_OWM_DisableInt(int flags);
263 
264 /**
265  * @brief   Enables/Disables driving of OWM_IO low during presence detection
266  * @param   enable      (1) = enable, (0) = disable
267  * @return  See \ref MXC_Error_Codes for a list of return codes.
268  */
269 int MXC_OWM_SetForcePresenceDetect(int enable);
270 
271 /**
272  * @brief   Enables/Disables the Internal pullup
273  * @param   enable      (1) = enable, (0) = disable
274  * @return  See \ref MXC_Error_Codes for a list of return codes.
275  */
276 int MXC_OWM_SetInternalPullup(int enable);
277 
278 /**
279  * @brief   Enables/Disables the External pullup
280  * @param   ext_pu_mode  See mxc_owm_ext_pu_t for values
281  * @return  See \ref MXC_Error_Codes for a list of return codes.
282  */
283 int MXC_OWM_SetExternalPullup(mxc_owm_ext_pu_t ext_pu_mode);
284 
285 /**
286  * @brief   Call to correct divider if system clock has changed
287  * @return  See \ref MXC_Error_Codes for a list of return codes.
288  */
289 int MXC_OWM_SystemClockUpdated(void);
290 
291 /**
292  * @brief   Enable/Disable Search ROM Accelerator mode
293  * @param   enable      (1) = enable, (0) = disable
294  * @return  See \ref MXC_Error_Codes for a list of return codes.
295  */
296 int MXC_OWM_SetSearchROMAccelerator(int enable);
297 
298 /**
299  * @brief   Prepare OWM for bit bang mode
300  * @param   initialState  Starting value of owm
301  * @return  See \ref MXC_Error_Codes for a list of return codes.
302  */
303 int MXC_OWM_BitBang_Init(int initialState);
304 
305 /**
306  * @brief   Read current value of wire
307  * @return  Value of wire
308  */
309 int MXC_OWM_BitBang_Read(void);
310 
311 /**
312  * @brief   Set value of wire
313  * @param   state       Value to drive wire to
314  * @return  See \ref MXC_Error_Codes for a list of return codes.
315  */
316 int MXC_OWM_BitBang_Write(int state);
317 
318 /**
319  * @brief   Disable Bit Bang mode
320  * @return  See \ref MXC_Error_Codes for a list of return codes.
321  */
322 int MXC_OWM_BitBang_Disable(void);
323 
324 /**@} end of group owm */
325 #ifdef __cplusplus
326 }
327 #endif
328 
329 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_OWM_H_
330