1 /**
2  * @file    afe.h
3  * @brief   Analog Front End (AFE) communications r
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 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32675_HART_UART_H_
27 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32675_HART_UART_H_
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /***** Includes *******/
34 #include "stdint.h"
35 #include "afe.h"
36 #include "afe_adc_zero_regs.h"
37 #include "afe_adc_one_regs.h"
38 #include "afe_dac_regs.h"
39 #include "afe_hart_regs.h"
40 #include "mxc_sys.h"
41 #include "mxc_assert.h"
42 
43 /***** Definitions *****/
44 #define NORMAL_HART_TRANSCEIVE_MODE 0
45 #define HART_TEST_MODE_TX_1200 1
46 #define HART_TEST_MODE_TX_2200 2
47 #define HART_TEST_MODE_EXTERNAL 3
48 
49 /** TPDLL Communications Errors, based on Command Summary Specification
50     HCF-Spec099 Section 7.3.1 Table 10 Communication Status. */
51 #define TPDLL_COMM_ERROR_INDICATOR 0x80
52 #define TPDLL_VERTICAL_PARITY_ERROR 0x40
53 #define TPDLL_OVERRUN_ERROR 0x20
54 #define TPDLL_FRAMING_ERROR 0x10
55 #define TPDLL_BUFFER_OVERFLOW_ERROR 0x02
56 
57 /** SAP definitions */
58 #define HART_STATE_IDLE 0
59 #define HART_STATE_TRANSMIT 1
60 #define HART_STATE_RECEIVE_ACTIVE 2
61 
62 // Note: no flag in UARTn_INT_FL register map for buffer overflow since
63 //      since its a SW error
64 #define UART_FLAG_BUFFER_OVERFLOW_ERROR 0x80
65 
66 /** HART UART SAP Callback for RESET.confirm() */
67 typedef void (*reset_confirm_callback_t)(void);
68 
69 /** HART UART SAP callback for ENABLE.confirm(state) */
70 typedef void (*enable_confirm_callback_t)(uint32_t);
71 
72 /** HART UART SAP callback for ENABLE.Indicate(state) */
73 typedef void (*enable_indicate_callback_t)(uint32_t);
74 
75 /** HART UART SAP Callback for DATA.confirm(data) */
76 typedef void (*data_confirm_callback_t)(uint8_t);
77 
78 /** HART UART SAP Callback for DATA.Indicate(data) */
79 typedef void (*data_indicate_callback_t)(uint8_t);
80 
81 /** HART UART SAP Callback for Error.Indicate(status, data) */
82 typedef void (*error_indicate_callback_t)(uint8_t, uint8_t);
83 
84 /** HART UART Callbacks */
85 typedef struct {
86     reset_confirm_callback_t reset_confirm_cb; /**< Callback function for RESET.confirm() */
87     enable_confirm_callback_t enable_confirm_cb; /**< Callback function for ENABLE.confirm() */
88     enable_indicate_callback_t enable_indicate_cb; /**< Callback function for ENABLE.Indicate() */
89     data_confirm_callback_t data_confirm_cb; /**< Callback function for DATA.confirm() */
90     data_indicate_callback_t data_indicate_cb; /**< Callback function for DATA.Indicate(data) */
91     error_indicate_callback_t
92         error_indicate_cb; /**< Callback function for ERROR.Indicate(status, data) */
93 } hart_uart_callbacks_t;
94 
95 /***** Function Prototypes *****/
96 /**
97  * @brief   Setup UART connection to HART.
98  * @note    Test mode is required for physical layer test to output constant bit
99  *          frequencies.
100  *
101  * @param   test_mode   Select the test mode to run the HART. (Check Definitions)
102  *
103  * @return  See \ref MXC_Error_Codes for a list of return codes.
104  */
105 int hart_uart_setup(uint32_t test_mode);
106 
107 /**
108  * @brief   Set callbacks for HART Physical Layer SAPs.
109  *
110  * @param   callbacks   \ref hart_uart_callbacks_t Struct of callbacks for HART Physical Layer SAPs.
111  */
112 void hart_uart_setup_saps(hart_uart_callbacks_t callbacks);
113 
114 /**
115  * @brief   Enable HART.
116  *
117  * @return  See \ref MXC_Error_Codes for a list of return codes.
118  */
119 int hart_uart_enable(void);
120 
121 /**
122  * @brief   Disable HART.
123  *
124  * @return  See \ref MXC_Error_Codes for a list of return codes.
125  */
126 int hart_uart_disable(void);
127 
128 /**
129  * @brief   Select HART transmission test at 1200 Hz.
130  */
131 void hart_uart_test_transmit_1200(void);
132 
133 /**
134  * @brief   Select HART transmission test at 2200 Hz.
135  */
136 void hart_uart_test_transmit_2200(void);
137 
138 /**
139  * @brief   Send data to HART, copies data into internal buffer and uses
140  *          interrupts to handle transmission. Toggles RTS before and after.
141  *
142  * @param   data        The buffer of data to write
143  * @param   length      The number of bytes to write
144  *
145  * @return  See \ref MXC_Error_Codes for a list of return codes.
146  */
147 int hart_uart_send(uint8_t *data, uint32_t length);
148 
149 /**
150  * @brief   Check if current transmit packet is complete. This state is reset
151  *          to 0 (false) on every call to \ref hart_uart_send, or \ref hart_uart_send_nonblocking
152  *
153  * @return  0 - transmission still active
154  *          1 - transmission completed
155  */
156 int hart_uart_check_transmit_complete(void);
157 
158 /**
159  * @brief   Unload the data received from the HART.
160  *
161  * @param   buffer          The buffer to store data in
162  * @param   *packet_length  The number of bytes received
163  * @param   *comm_errors    Any communications error during reception
164  *
165  * @return  See \ref MXC_Error_Codes for a list of return codes.
166  */
167 int hart_uart_get_received_packet(uint8_t *buffer, uint32_t *packet_length, uint32_t *comm_errors);
168 
169 /**
170  * @brief   The processing function for HART UART interrupts.
171  *
172  * @note    When using the HART UART, the application must call this
173  *          function periodically. This can be done from within the UART interrupt
174  *          handler or periodically by the application if UART interrupts are disabled.
175  */
176 void hart_uart_handler(void);
177 
178 /**
179  * @brief   Set HART RTS pin to receive mode, usually for testing purposes.
180  *
181  */
182 void hart_rts_receive_mode(void);
183 
184 /**
185  * @brief   Set HART RTS pin to transmit mode, usually for testing purposes.
186  *
187  */
188 void hart_rts_transmit_mode(void);
189 
190 /**
191  * @brief   Enables the 4Mhz clock that drives HART state machine
192  *
193  */
194 int hart_clock_enable(void);
195 
196 /**
197  * @brief   Disables the 4Mhz clock that drives HART state machine
198  *
199  */
200 void hart_clock_disable(void);
201 
202 /**
203  * @brief   Disables then enables HART UART Physical Layer interface
204  *
205  */
206 void hart_sap_reset_request(void);
207 
208 /**
209  * @brief  Adds data to transmit FIFO
210  * @param  data   Data to transmit
211  *
212  * @return  See \ref MXC_Error_Codes for a list of return codes.
213  */
214 int hart_sap_data_request(uint8_t data);
215 
216 /**
217  * @brief Sets RTS state
218  * @param state State for RTS pin /ref HART_STATE_TRANSMIT /ref HART_STATE_RECEIVE
219  *
220  */
221 void hart_sap_enable_request(uint32_t state);
222 
223 /**@} end of group hart_uart */
224 
225 #ifdef __cplusplus
226 }
227 #endif
228 
229 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32675_HART_UART_H_
230