1
0

ls1b_public.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * 2021-02-02 michael5hzg@gmail.com adapt to ls1b
  10. */
  11. // 一些常用的、共用的接口
  12. /*
  13. * 将指定寄存器的指定位置1
  14. * @reg 寄存器地址
  15. * @bit 需要置1的那一bit
  16. */
  17. void reg_set_one_bit(volatile unsigned int *reg, unsigned int bit)
  18. {
  19. unsigned int temp, mask;
  20. mask = 1 << bit;
  21. temp = *reg;
  22. temp |= mask;
  23. *reg = temp;
  24. return ;
  25. }
  26. /*
  27. * 将指定寄存器的指定位清零
  28. * @reg 寄存器地址
  29. * @bit 需要清零的那一bit
  30. */
  31. void reg_clr_one_bit(volatile unsigned int *reg, unsigned int bit)
  32. {
  33. unsigned int temp, mask;
  34. mask = 1 << bit;
  35. temp = *reg;
  36. temp &= ~mask;
  37. *reg = temp;
  38. return ;
  39. }
  40. /*
  41. * 获取指定寄存器的指定位的值
  42. * @reg 寄存器地址
  43. * @bit 需要读取值的那一bit
  44. * @ret 指定位的值
  45. */
  46. unsigned int reg_get_bit(volatile unsigned int *reg, unsigned int bit)
  47. {
  48. unsigned int temp;
  49. temp = *reg;
  50. temp = (temp >> bit) & 1;
  51. return temp;
  52. }
  53. /*
  54. * 向寄存器中写入8bit(一个字节)数据
  55. * @data 待写入的数据
  56. * @addr 寄存器地址
  57. */
  58. void reg_write_8(unsigned char data, volatile unsigned char *addr)
  59. {
  60. *addr = data;
  61. }
  62. /*
  63. * 从寄存器读出8bit(一个字节)数据
  64. * @addr 寄存器地址
  65. * @ret 读出的数据
  66. */
  67. unsigned char reg_read_8(volatile unsigned char *addr)
  68. {
  69. return (*addr);
  70. }
  71. /*
  72. * 向寄存器中写一个32bit的数据
  73. * @data 待写入的数据
  74. * @addr 寄存器地址
  75. */
  76. void reg_write_32(unsigned int data, volatile unsigned int *addr)
  77. {
  78. *addr = data;
  79. }
  80. /*
  81. * 从寄存器读出一个32bit数据
  82. * @addr 寄存器地址
  83. * @ret 读出的数据
  84. */
  85. unsigned int reg_read_32(volatile unsigned int *addr)
  86. {
  87. return (*addr);
  88. }
  89. /**
  90. * ffs - find first bit set
  91. * @x: the word to search
  92. *
  93. * This is defined the same way as
  94. * the libc and compiler builtin ffs routines, therefore
  95. * differs in spirit from the above ffz (man ffs).
  96. */
  97. int ls1b_ffs(int x)
  98. {
  99. int r = 1;
  100. if (!x)
  101. return 0;
  102. if (!(x & 0xffff)) {
  103. x >>= 16;
  104. r += 16;
  105. }
  106. if (!(x & 0xff)) {
  107. x >>= 8;
  108. r += 8;
  109. }
  110. if (!(x & 0xf)) {
  111. x >>= 4;
  112. r += 4;
  113. }
  114. if (!(x & 3)) {
  115. x >>= 2;
  116. r += 2;
  117. }
  118. if (!(x & 1)) {
  119. x >>= 1;
  120. r += 1;
  121. }
  122. return r;
  123. }
  124. /*
  125. * fls - find last (most-significant) bit set
  126. * @x: the word to search
  127. *
  128. * This is defined the same way as ffs.
  129. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  130. */
  131. int ls1b_fls(int x)
  132. {
  133. int r = 32;
  134. if (!x)
  135. return 0;
  136. if (!(x & 0xffff0000u))
  137. {
  138. x <<= 16;
  139. r -= 16;
  140. }
  141. if (!(x & 0xff000000u))
  142. {
  143. x <<= 8;
  144. r -= 8;
  145. }
  146. if (!(x & 0xf0000000u))
  147. {
  148. x <<= 4;
  149. r -= 4;
  150. }
  151. if (!(x & 0xc0000000u))
  152. {
  153. x <<= 2;
  154. r -= 2;
  155. }
  156. if (!(x & 0x80000000u))
  157. {
  158. x <<= 1;
  159. r -= 1;
  160. }
  161. return r;
  162. }