1 /**
2  * @file    owm.h
3  * @brief   Registers, Bit Masks and Bit Positions for the 1-Wire Master
4  *          peripheral module.
5  */
6 
7 /******************************************************************************
8  *
9  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
10  * Analog Devices, Inc.),
11  * Copyright (C) 2023-2024 Analog Devices, Inc.
12  *
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  *     http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *
25  ******************************************************************************/
26 
27 /* Define to prevent redundant inclusion */
28 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32665_OWM_H_
29 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32665_OWM_H_
30 
31 /* **** Includes **** */
32 #include "mxc_device.h"
33 #include "mxc_sys.h"
34 #include "owm_regs.h"
35 #include "mxc_pins.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @ingroup periphlibs
43  * @defgroup owm 1-Wire Master (OWM)
44  * @{
45  */
46 
47 /* **** Definitions **** */
48 
49 /**
50  * @brief   Enumeration type for specifying options for 1-Wire external pullup mode.
51  */
52 typedef enum {
53     MXC_OWM_EXT_PU_ACT_HIGH = 0, /**< Pullup pin is active high when enabled.        */
54     MXC_OWM_EXT_PU_ACT_LOW = 1, /**< Pullup pin is active low when enabled.         */
55     MXC_OWM_EXT_PU_UNUSED = 2, /**< Pullup pin is not used for an external pullup. */
56 } mxc_owm_ext_pu_t;
57 
58 /**
59  * @brief   Structure type for 1-Wire Master configuration.
60  */
61 typedef struct {
62     uint8_t int_pu_en; /**< 1 = internal pullup on.   */
63     mxc_owm_ext_pu_t ext_pu_mode; /**< See #mxc_owm_ext_pu_t.   */
64     uint8_t long_line_mode; /**< 1 = long line mode enable.    */
65     // mxc_owm_overdrive_t overdrive_spec; /**< 0 = timeslot is 12us, 1 = timeslot is 10us.   */
66 } mxc_owm_cfg_t;
67 
68 #define READ_ROM_COMMAND 0x33 /**< Read ROM Command */
69 #define MATCH_ROM_COMMAND 0x55 /**< Match ROM Command */
70 #define SEARCH_ROM_COMMAND 0xF0 /**< Search ROM Command */
71 #define SKIP_ROM_COMMAND 0xCC /**< Skip ROM Command */
72 #define OD_SKIP_ROM_COMMAND 0x3C /**< Overdrive Skip ROM Command */
73 #define OD_MATCH_ROM_COMMAND 0x69 /**< Overdrive Match ROM Command */
74 #define RESUME_COMMAND 0xA5 /**< Resume Command */
75 
76 /* **** Globals **** */
77 
78 /* **** Function Prototypes **** */
79 
80 /**
81  * @brief   Initialize and enable OWM module.
82  * @param   cfg         Pointer to OWM configuration.
83  * @param   map         MAP_A, MAP_B or MAP_C onewire pins select. Has no effect
84  *                      incase of MSDK_NO_GPIO_CLK_INIT has been defined.
85  *
86  * @return  #E_NO_ERROR if everything is successful
87  * @return  #E_NULL_PTR if parameter is a null pointer
88  * @return  #E_BUSY if IOMAN was not configured correctly
89  * @return  #E_UNINITIALIZED if OWM CLK disabled
90  * @return  #E_NOT_SUPPORTED if 1MHz CLK cannot be created with given system and owm CLK
91  * @return  #E_BAD_PARAM if bad cfg parameter passed in
92  */
93 int MXC_OWM_Init(const mxc_owm_cfg_t *cfg, sys_map_t map);
94 
95 /**
96  * @brief   Shutdown OWM module.
97  *
98  */
99 void MXC_OWM_Shutdown(void);
100 
101 /**
102  * @brief   Send 1-Wire reset pulse. Will block until transaction is complete.
103  *
104  * @return  0 if no 1-wire devices reponded during the presence pulse, 1 otherwise
105  */
106 int MXC_OWM_Reset(void);
107 
108 /**
109  * @brief   Get the presence pulse detect status.
110  *
111  * @return  0 if no 1-wire devices reponded during the presence pulse, 1 otherwise
112  */
113 int MXC_OWM_GetPresenceDetect(void);
114 
115 /**
116  * @brief   Send and receive one byte of data. Will block until transaction is complete.
117  *
118  * @param   data        data to send
119  *
120  * @return  data read (1 byte)
121  */
122 int MXC_OWM_TouchByte(uint8_t data);
123 
124 /**
125  * @brief   Write one byte of data. Will block until transaction is complete.
126  *
127  * @param   data        data to send
128  *
129  * @return  #E_NO_ERROR if everything is successful
130  * @return  #E_COMM_ERR if data written != data parameter
131  */
132 int MXC_OWM_WriteByte(uint8_t data);
133 
134 /**
135  * @brief   Read one byte of data. Will block until transaction is complete.
136  *
137  * @return  data read (1 byte)
138  */
139 int MXC_OWM_ReadByte(void);
140 
141 /**
142  * @brief   Send and receive one bit of data. Will block until transaction is complete.
143  *
144  * @param   bit         bit to send
145  *
146  * @return  bit read
147  */
148 int MXC_OWM_TouchBit(uint8_t bit);
149 
150 /**
151  * @brief   Write one bit of data. Will block until transaction is complete.
152  *
153  * @param   bit         bit to send
154  *
155  * @return  #E_NO_ERROR if everything is successful
156  * @return  #E_COMM_ERR if bit written != bit parameter
157  */
158 int MXC_OWM_WriteBit(uint8_t bit);
159 
160 /**
161  * @brief   Read one bit of data. Will block until transaction is complete.
162  *
163  * @return  bit read
164  */
165 int MXC_OWM_ReadBit(void);
166 
167 /**
168  * @brief   Write multiple bytes of data. Will block until transaction is complete.
169  *
170  * @param   data    Pointer to buffer for write data.
171  * @param   len     Number of bytes to write.
172  *
173  * @return  Number of bytes written if successful
174  * @return  #E_COMM_ERR if line short detected before transaction
175  */
176 int MXC_OWM_Write(uint8_t *data, int len);
177 
178 /**
179  * @brief   Read multiple bytes of data. Will block until transaction is complete.
180  *
181  * @param   data    Pointer to buffer for read data.
182  * @param   len     Number of bytes to read.
183  *
184  * @return Number of bytes read if successful
185  * @return #E_COMM_ERR if line short detected before transaction
186  */
187 int MXC_OWM_Read(uint8_t *data, int len);
188 
189 /**
190  * @brief   Starts 1-Wire communication with Read ROM command
191  * @note    Only use the Read ROM command with one slave on the bus
192  *
193  * @param   ROMCode     Pointer to buffer for ROM code read
194  *
195  * @return  #E_NO_ERROR if everything is successful
196  * @return  #E_COMM_ERR if reset, read or write fails
197  */
198 int MXC_OWM_ReadROM(uint8_t *ROMCode);
199 
200 /**
201  * @brief   Starts 1-Wire communication with Match ROM command
202  *
203  * @param   ROMCode     Pointer to buffer with ROM code to match
204  *
205  * @return  #E_NO_ERROR if everything is successful
206  * @return  #E_COMM_ERR if reset or write fails
207  */
208 int MXC_OWM_MatchROM(uint8_t *ROMCode);
209 
210 /**
211  * @brief   Starts 1-Wire communication with Overdrive Match ROM command
212  * @note    After Overdrive Match ROM command is sent, the OWM is set to
213  *          overdrive speed. To set back to standard speed use MXC_OWM_SetOverdrive.
214  *
215  * @param   ROMCode     Pointer to buffer with ROM code to match
216  *
217  * @return  #E_NO_ERROR if everything is successful
218  * @return  #E_COMM_ERR if reset or write fails
219  */
220 int MXC_OWM_ODMatchROM(uint8_t *ROMCode);
221 
222 /**
223  * @brief   Starts 1-Wire communication with Skip ROM command
224  *
225  * @return  #E_NO_ERROR if everything is successful
226  * @return  #E_COMM_ERR if reset or write fails
227  */
228 int MXC_OWM_SkipROM(void);
229 
230 /**
231  * @brief   Starts 1-Wire communication with Overdrive Skip ROM command
232  * @note    After Overdrive Skip ROM command is sent, the OWM is set to
233  *          overdrive speed. To set back to standard speed use MXC_OWM_SetOverdrive
234  *
235  * @return  #E_NO_ERROR if everything is successful
236  * @return  #E_COMM_ERR if reset or write fails
237  */
238 int MXC_OWM_ODSkipROM(void);
239 
240 /**
241  * @brief   Starts 1-Wire communication with Resume command
242  *
243  * @return  #E_NO_ERROR if everything is successful
244  * @return  #E_COMM_ERR if reset or write fails
245  */
246 int MXC_OWM_Resume(void);
247 
248 /**
249  * @brief   Starts 1-Wire communication with Search ROM command
250  *
251  * @param   newSearch   (1) = start new search, (0) = continue search for next ROM
252  * @param   ROMCode     Pointer to buffer with ROM code found
253  *
254  * @return  (1) = ROM found, (0) = no new ROM found, end of search
255  */
256 int MXC_OWM_SearchROM(int newSearch, uint8_t *ROMCode);
257 
258 /**
259  * @brief   Clear interrupt flags.
260  *
261  * @param   mask        Mask of interrupts to clear.
262  */
263 void MXC_OWM_ClearFlags(uint32_t mask);
264 
265 /**
266  * @brief   Get interrupt flags.
267  *
268  * @return  Mask of active flags.
269  */
270 unsigned MXC_OWM_GetFlags(void);
271 
272 /**
273  * @brief   Enables/Disables the External pullup
274  *
275  * @param   enable      (1) = enable, (0) = disable
276  */
277 void MXC_OWM_SetExtPullup(int enable);
278 
279 /**
280  * @brief   Enables/Disables Overdrive speed
281  *
282  * @param   enable      (1) = overdrive, (0) = standard
283  */
284 void MXC_OWM_SetOverdrive(int enable);
285 
286 /**
287  * @brief   Enables interrupts
288  *
289  * @param   flags      which owm interrupts to enable
290  */
291 void MXC_OWM_EnableInt(int flags);
292 
293 /**
294  * @brief   Disables interrupts
295  *
296  * @param   flags      which owm interrupts to disable
297  */
298 void MXC_OWM_DisableInt(int flags);
299 
300 /**
301  * @brief   Enables/Disables driving of OWM_IO low during presence detection
302  *
303  * @param   enable      (1) = enable, (0) = disable
304  *
305  * @return  See \ref MXC_Error_Codes for a list of return codes.
306  */
307 int MXC_OWM_SetForcePresenceDetect(int enable);
308 
309 /**
310  * @brief   Enables/Disables the Internal pullup
311  *
312  * @param   enable      (1) = enable, (0) = disable
313  *
314  * @return  See \ref MXC_Error_Codes for a list of return codes.
315  */
316 int MXC_OWM_SetInternalPullup(int enable);
317 
318 /**
319  * @brief   Enables/Disables the External pullup
320  *
321  * @param   ext_pu_mode  See mxc_owm_ext_pu_t for values
322  *
323  * @return  See \ref MXC_Error_Codes for a list of return codes.
324  */
325 int MXC_OWM_SetExternalPullup(mxc_owm_ext_pu_t ext_pu_mode);
326 
327 /**
328  * @brief   Call to correct divider if system clock has changed
329  *
330  * @return  See \ref MXC_Error_Codes for a list of return codes.
331  */
332 int MXC_OWM_SystemClockUpdated(void);
333 
334 /**
335  * @brief   Enable/Disable Search ROM Accelerator mode
336  *
337  * @param   enable      (1) = enable, (0) = disable
338  *
339  * @return  See \ref MXC_Error_Codes for a list of return codes.
340  */
341 int MXC_OWM_SetSearchROMAccelerator(int enable);
342 
343 /**
344  * @brief   Prepare OWM for bit bang mode
345  *
346  * @param   initialState  Starting value of owm
347  *
348  * @return  See \ref MXC_Error_Codes for a list of return codes.
349  */
350 int MXC_OWM_BitBang_Init(int initialState);
351 
352 /**
353  * @brief   Read current value of wire
354  *
355  * @return  Value of wire
356  */
357 int MXC_OWM_BitBang_Read(void);
358 
359 /**
360  * @brief   Set value of wire
361  *
362  * @param   state       Value to drive wire to
363  *
364  * @return  See \ref MXC_Error_Codes for a list of return codes.
365  */
366 int MXC_OWM_BitBang_Write(int state);
367 
368 /**
369  * @brief   Disable Bit Bang mode
370  *
371  * @return  See \ref MXC_Error_Codes for a list of return codes.
372  */
373 int MXC_OWM_BitBang_Disable(void);
374 
375 /**@} end of group owm */
376 #ifdef __cplusplus
377 }
378 #endif
379 
380 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32665_OWM_H_
381