1 /*
2  * Copyright (C) 2017-2020 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file       drv/io.h
7  * @brief      Header File for register bits operation
8  * @version    V1.0
9  * @date       9. Oct 2020
10  * @model      io
11  ******************************************************************************/
12 
13 #ifndef _DRV_IO_H_
14 #define _DRV_IO_H_
15 
16 #include <stdint.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /* Bit field operate*/
23 #define REG64(addr)        (*(volatile uint64_t *)(addr))
24 #define REG32(addr)        (*(volatile uint32_t *)(addr))
25 #define REG16(addr)        (*(volatile uint16_t *)(addr))
26 #define REG8(addr)         (*(volatile uint8_t *)(addr))
27 
28 /* Insert value to some field in reg, other field is set to 0(the field make macro) */
29 #define HAL_FMK(PER_REG_FIELD, val)                                         \
30     (((val) << PER_REG_FIELD##_SHIFT) & PER_REG_FIELD##_MASK)
31 
32 /* Get value of some field in reg(the field extract macro) */
33 #define HAL_FEXT(reg, PER_REG_FIELD)                                        \
34     (((reg) & PER_REG_FIELD##_MASK) >> PER_REG_FIELD##_SHIFT)
35 
36 /* Insert value to some field in reg, other field don't change(the field insert macro) */
37 #define HAL_FINS(reg, PER_REG_FIELD, val)                                   \
38     ((reg) = ((reg) & ~PER_REG_FIELD##_MASK)                                \
39              | HAL_FMK(PER_REG_FIELD, val))
40 
41 
42 /* Bit operate */
43 /* Set one value to 1, other bit don't change*/
44 #define HAL_BIT_SET(reg, bit) ((reg) = ((reg) | (1U << (bit))))
45 
46 /* Set one value to 0, other bit don't change*/
47 #define HAL_BIT_CLR(reg, bit) ((reg) = ((reg) & (~(1U << (bit)))))
48 
49 /* Get value of one bit(0/1) */
50 #define HAL_GET_BIT_VAL(reg, bit) (((reg)>> (bit)) & 1U)
51 
52 /* Judge one bit is 1 or not */
53 #define HAL_IS_BIT_SET(reg, pos) (((reg) & (1U << (pos))) != 0x0U)
54 
55 /* Judge one bit is 0 or not */
56 #define HAL_IS_BIT_CLR(reg, pos) (((reg) & (1U << (pos))) == 0x0U)
57 
58 /* Set one value to bit, other bit don't change*/
59 #define HAL_BIT_INSR(reg, bit, val)                                       \
60     ((reg) = (((reg) & (~(1U << (bit)))) | (((val) & 1U) << (bit))))
61 
62 
getreg8(volatile void * addr)63 static inline uint8_t getreg8(volatile void *addr)
64 {
65     return *(volatile uint8_t *)addr;
66 }
67 
putreg8(uint8_t val,volatile void * addr)68 static inline void putreg8(uint8_t val, volatile void *addr)
69 {
70     *(volatile uint8_t *)addr = val;
71 }
72 
getreg16(volatile void * addr)73 static inline uint16_t getreg16(volatile void *addr)
74 {
75     return *(volatile uint16_t *)addr;
76 }
77 
putreg16(uint16_t val,volatile void * addr)78 static inline void putreg16(uint16_t val, volatile void *addr)
79 {
80     *(volatile uint16_t *)addr = val;
81 }
82 
getreg32(volatile void * addr)83 static inline uint32_t getreg32(volatile void *addr)
84 {
85     return *(volatile uint32_t *)addr;
86 }
87 
putreg32(uint32_t val,volatile void * addr)88 static inline void putreg32(uint32_t val, volatile void *addr)
89 {
90     *(volatile uint32_t *)addr = val;
91 }
92 
getreg64(volatile void * addr)93 static inline uint64_t getreg64(volatile void *addr)
94 {
95     return *(volatile uint64_t *)addr;
96 }
97 
putreg64(uint32_t val,volatile void * addr)98 static inline void putreg64(uint32_t val, volatile void *addr)
99 {
100     *(volatile uint64_t *)addr = val;
101 }
102 
inl(void * addr)103 static inline uint32_t inl(void *addr)
104 {
105     return *(volatile uint32_t *)addr;
106 }
107 
outl(uint32_t val,void * addr)108 static inline void outl(uint32_t val, void *addr)
109 {
110     *(volatile uint32_t *)addr = val;
111 }
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif /* _DRV_IO_H_ */
118