1 /*
2 * Copyright (c) 2006-2018, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2017-09-06 ���� first version
9 */
10
11 // һЩ���õġ����õĽӿ�
12
13 /*
14 * ��ָ���Ĵ�����ָ��λ��1
15 * @reg �Ĵ�����ַ
16 * @bit ��Ҫ��1����һbit
17 */
reg_set_one_bit(volatile unsigned int * reg,unsigned int bit)18 void reg_set_one_bit(volatile unsigned int *reg, unsigned int bit)
19 {
20 unsigned int temp, mask;
21
22 mask = 1 << bit;
23 temp = *reg;
24 temp |= mask;
25 *reg = temp;
26
27 return ;
28 }
29
30
31 /*
32 * ��ָ���Ĵ�����ָ��λ����
33 * @reg �Ĵ�����ַ
34 * @bit ��Ҫ�������һbit
35 */
reg_clr_one_bit(volatile unsigned int * reg,unsigned int bit)36 void reg_clr_one_bit(volatile unsigned int *reg, unsigned int bit)
37 {
38 unsigned int temp, mask;
39
40 mask = 1 << bit;
41 temp = *reg;
42 temp &= ~mask;
43 *reg = temp;
44
45 return ;
46 }
47
48
49
50 /*
51 * ��ȡָ���Ĵ�����ָ��λ��ֵ
52 * @reg �Ĵ�����ַ
53 * @bit ��Ҫ��ȡֵ����һbit
54 * @ret ָ��λ��ֵ
55 */
reg_get_bit(volatile unsigned int * reg,unsigned int bit)56 unsigned int reg_get_bit(volatile unsigned int *reg, unsigned int bit)
57 {
58 unsigned int temp;
59
60 temp = *reg;
61 temp = (temp >> bit) & 1;
62
63 return temp;
64 }
65
66
67 /*
68 * ��Ĵ�����д��8bit(һ���ֽ�)����
69 * @data ��������
70 * @addr �Ĵ�����ַ
71 */
reg_write_8(unsigned char data,volatile unsigned char * addr)72 void reg_write_8(unsigned char data, volatile unsigned char *addr)
73 {
74 *addr = data;
75 }
76
77
78 /*
79 * �ӼĴ�������8bit(һ���ֽ�)����
80 * @addr �Ĵ�����ַ
81 * @ret ����������
82 */
reg_read_8(volatile unsigned char * addr)83 unsigned char reg_read_8(volatile unsigned char *addr)
84 {
85 return (*addr);
86 }
87
88
89 /*
90 * ��Ĵ�����дһ��32bit������
91 * @data ��������
92 * @addr �Ĵ�����ַ
93 */
reg_write_32(unsigned int data,volatile unsigned int * addr)94 void reg_write_32(unsigned int data, volatile unsigned int *addr)
95 {
96 *addr = data;
97 }
98
99
100 /*
101 * �ӼĴ�������һ��32bit����
102 * @addr �Ĵ�����ַ
103 * @ret ����������
104 */
reg_read_32(volatile unsigned int * addr)105 unsigned int reg_read_32(volatile unsigned int *addr)
106 {
107 return (*addr);
108 }
109
110
111
112 /**
113 * ffs - find first bit set
114 * @x: the word to search
115 *
116 * This is defined the same way as
117 * the libc and compiler builtin ffs routines, therefore
118 * differs in spirit from the above ffz (man ffs).
119 */
ls1c_ffs(int x)120 int ls1c_ffs(int x)
121 {
122 int r = 1;
123
124 if (!x)
125 return 0;
126 if (!(x & 0xffff)) {
127 x >>= 16;
128 r += 16;
129 }
130 if (!(x & 0xff)) {
131 x >>= 8;
132 r += 8;
133 }
134 if (!(x & 0xf)) {
135 x >>= 4;
136 r += 4;
137 }
138 if (!(x & 3)) {
139 x >>= 2;
140 r += 2;
141 }
142 if (!(x & 1)) {
143 x >>= 1;
144 r += 1;
145 }
146 return r;
147 }
148
149
150 /*
151 * fls - find last (most-significant) bit set
152 * @x: the word to search
153 *
154 * This is defined the same way as ffs.
155 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
156 */
ls1c_fls(int x)157 int ls1c_fls(int x)
158 {
159 int r = 32;
160
161 if (!x)
162 return 0;
163 if (!(x & 0xffff0000u))
164 {
165 x <<= 16;
166 r -= 16;
167 }
168 if (!(x & 0xff000000u))
169 {
170 x <<= 8;
171 r -= 8;
172 }
173 if (!(x & 0xf0000000u))
174 {
175 x <<= 4;
176 r -= 4;
177 }
178 if (!(x & 0xc0000000u))
179 {
180 x <<= 2;
181 r -= 2;
182 }
183 if (!(x & 0x80000000u))
184 {
185 x <<= 1;
186 r -= 1;
187 }
188
189 return r;
190 }
191
192
193