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