1 /***************************************************************************//**
2  * Copyright 2019-2020 Microchip FPGA Embedded Systems Solutions.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * MPFS HAL Embedded Software
25  *
26  */
27 /***************************************************************************//**
28  *
29  * Hardware abstraction layer functions.
30  *
31  * Legacy register interrupt functions
32  * Pointers are now recommended for use in drivers
33  *
34  */
35 #ifndef HAL_H
36 #define HAL_H
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include "cpu_types.h"
43 #include "hw_reg_access.h"
44 #include "hal/hal_assert.h"
45 /***************************************************************************//**
46  * Enable all interrupts at the processor level.
47  */
48 void HAL_enable_interrupts( void );
49 
50 /***************************************************************************//**
51  * Disable all interrupts at the processor core level.
52  * Return the interrupts enable state before disabling occurred so that it can
53  * later be restored.
54  */
55 psr_t HAL_disable_interrupts( void );
56 
57 /***************************************************************************//**
58  * Restore the interrupts enable state at the processor core level.
59  * This function is normally passed the value returned from a previous call to
60  * HAL_disable_interrupts().
61  */
62 void HAL_restore_interrupts( psr_t saved_psr );
63 
64 /***************************************************************************//**
65  */
66 #define FIELD_OFFSET(FIELD_NAME)  (FIELD_NAME##_OFFSET)
67 #define FIELD_SHIFT(FIELD_NAME)   (FIELD_NAME##_SHIFT)
68 #define FIELD_MASK(FIELD_NAME)    (FIELD_NAME##_MASK)
69 
70 /***************************************************************************//**
71  * The macro HAL_set_32bit_reg() allows writing a 32 bits wide register.
72  *
73  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
74  *              peripheral containing the register.
75  * REG_NAME:    A string identifying the register to write. These strings are
76  *              specified in a header file associated with the peripheral.
77  * VALUE:       A variable of type uint32_t containing the value to write.
78  */
79 #define HAL_set_32bit_reg(BASE_ADDR, REG_NAME, VALUE) \
80           (HW_set_32bit_reg( ((BASE_ADDR) + (REG_NAME##_REG_OFFSET)), (VALUE) ))
81 
82 /***************************************************************************//**
83  * The macro HAL_get_32bit_reg() is used to read the value  of a 32 bits wide
84  * register.
85  *
86  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
87  *              peripheral containing the register.
88  * REG_NAME:    A string identifying the register to read. These strings are
89  *              specified in a header file associated with the peripheral.
90  * RETURN:      This function-like macro returns a uint32_t value.
91  */
92 #define HAL_get_32bit_reg(BASE_ADDR, REG_NAME) \
93           (HW_get_32bit_reg( ((BASE_ADDR) + (REG_NAME##_REG_OFFSET)) ))
94 
95 /***************************************************************************//**
96  * The macro HAL_set_32bit_reg_field() is used to write a field within a
97  * 32 bits wide register. The field written can be one or more bits.
98  *
99  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
100  *              peripheral containing the register.
101  * FIELD_NAME:  A string identifying the register field to write. These strings
102  *              are specified in a header file associated with the peripheral.
103  * VALUE:       A variable of type uint32_t containing the field value to write.
104  */
105 #define HAL_set_32bit_reg_field(BASE_ADDR, FIELD_NAME, VALUE) \
106             (HW_set_32bit_reg_field(\
107                 (BASE_ADDR) + FIELD_OFFSET(FIELD_NAME),\
108                 FIELD_SHIFT(FIELD_NAME),\
109                 FIELD_MASK(FIELD_NAME),\
110                 (VALUE)))
111 
112 /***************************************************************************//**
113  * The macro HAL_get_32bit_reg_field() is used to read a register field from
114  * within a 32 bit wide peripheral register. The field can be one or more bits.
115  *
116  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
117  *              peripheral containing the register.
118  * FIELD_NAME:  A string identifying the register field to write. These strings
119  *              are specified in a header file associated with the peripheral.
120  * RETURN:      This function-like macro returns a uint32_t value.
121  */
122 #define HAL_get_32bit_reg_field(BASE_ADDR, FIELD_NAME) \
123             (HW_get_32bit_reg_field(\
124                 (BASE_ADDR) + FIELD_OFFSET(FIELD_NAME),\
125                 FIELD_SHIFT(FIELD_NAME),\
126                 FIELD_MASK(FIELD_NAME)))
127 
128 /***************************************************************************//**
129  * The macro HAL_set_16bit_reg() allows writing a 16 bits wide register.
130  *
131  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
132  *              peripheral containing the register.
133  * REG_NAME:    A string identifying the register to write. These strings are
134  *              specified in a header file associated with the peripheral.
135  * VALUE:       A variable of type uint_fast16_t containing the value to write.
136  */
137 #define HAL_set_16bit_reg(BASE_ADDR, REG_NAME, VALUE) \
138             (HW_set_16bit_reg( ((BASE_ADDR) + (REG_NAME##_REG_OFFSET)), (VALUE) ))
139 
140 /***************************************************************************//**
141  * The macro HAL_get_16bit_reg() is used to read the value  of a 16 bits wide
142  * register.
143  *
144  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
145  *              peripheral containing the register.
146  * REG_NAME:    A string identifying the register to read. These strings are
147  *              specified in a header file associated with the peripheral.
148  * RETURN:      This function-like macro returns a uint16_t value.
149  */
150 #define HAL_get_16bit_reg(BASE_ADDR, REG_NAME) \
151             (HW_get_16bit_reg( (BASE_ADDR) + (REG_NAME##_REG_OFFSET) ))
152 
153 /***************************************************************************//**
154  * The macro HAL_set_16bit_reg_field() is used to write a field within a
155  * 16 bits wide register. The field written can be one or more bits.
156  *
157  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
158  *              peripheral containing the register.
159  * FIELD_NAME:  A string identifying the register field to write. These strings
160  *              are specified in a header file associated with the peripheral.
161  * VALUE:       A variable of type uint16_t containing the field value to write.
162  */
163 #define HAL_set_16bit_reg_field(BASE_ADDR, FIELD_NAME, VALUE) \
164             (HW_set_16bit_reg_field(\
165                 (BASE_ADDR) + FIELD_OFFSET(FIELD_NAME),\
166                 FIELD_SHIFT(FIELD_NAME),\
167                 FIELD_MASK(FIELD_NAME),\
168                 (VALUE)))
169 
170 /***************************************************************************//**
171  * The macro HAL_get_16bit_reg_field() is used to read a register field from
172  * within a 8 bit wide peripheral register. The field can be one or more bits.
173  *
174  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
175  *              peripheral containing the register.
176  * FIELD_NAME:  A string identifying the register field to write. These strings
177  *              are specified in a header file associated with the peripheral.
178  * RETURN:      This function-like macro returns a uint16_t value.
179  */
180 #define HAL_get_16bit_reg_field(BASE_ADDR, FIELD_NAME) \
181             (HW_get_16bit_reg_field(\
182                 (BASE_ADDR) + FIELD_OFFSET(FIELD_NAME),\
183                 FIELD_SHIFT(FIELD_NAME),\
184                 FIELD_MASK(FIELD_NAME)))
185 
186 /***************************************************************************//**
187  * The macro HAL_set_8bit_reg() allows writing a 8 bits wide register.
188  *
189  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
190  *              peripheral containing the register.
191  * REG_NAME:    A string identifying the register to write. These strings are
192  *              specified in a header file associated with the peripheral.
193  * VALUE:       A variable of type uint_fast8_t containing the value to write.
194  */
195 #define HAL_set_8bit_reg(BASE_ADDR, REG_NAME, VALUE) \
196           (HW_set_8bit_reg( ((BASE_ADDR) + (REG_NAME##_REG_OFFSET)), (VALUE) ))
197 
198 /***************************************************************************//**
199  * The macro HAL_get_8bit_reg() is used to read the value of a 8 bits wide
200  * register.
201  *
202  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
203  *              peripheral containing the register.
204  * REG_NAME:    A string identifying the register to read. These strings are
205  *              specified in a header file associated with the peripheral.
206  * RETURN:      This function-like macro returns a uint8_t value.
207  */
208 #define HAL_get_8bit_reg(BASE_ADDR, REG_NAME) \
209           (HW_get_8bit_reg( (BASE_ADDR) + (REG_NAME##_REG_OFFSET) ))
210 
211 /***************************************************************************//**
212  */
213 #define HAL_set_8bit_reg_field(BASE_ADDR, FIELD_NAME, VALUE) \
214             (HW_set_8bit_reg_field(\
215                 (BASE_ADDR) + FIELD_OFFSET(FIELD_NAME),\
216                 FIELD_SHIFT(FIELD_NAME),\
217                 FIELD_MASK(FIELD_NAME),\
218                 (VALUE)))
219 
220 /***************************************************************************//**
221  * The macro HAL_get_8bit_reg_field() is used to read a register field from
222  * within a 8 bit wide peripheral register. The field can be one or more bits.
223  *
224  * BASE_ADDR:   A variable of type addr_t specifying the base address of the
225  *              peripheral containing the register.
226  * FIELD_NAME:  A string identifying the register field to write. These strings
227  *              are specified in a header file associated with the peripheral.
228  * RETURN:      This function-like macro returns a uint8_t value.
229  */
230 #define HAL_get_8bit_reg_field(BASE_ADDR, FIELD_NAME) \
231             (HW_get_8bit_reg_field(\
232                 (BASE_ADDR) + FIELD_OFFSET(FIELD_NAME),\
233                 FIELD_SHIFT(FIELD_NAME),\
234                 FIELD_MASK(FIELD_NAME)))
235 
236 #ifdef __cplusplus
237 }
238 #endif
239 
240 #endif /*HAL_H*/
241 
242