1
0

ls1c_public.c 2.8 KB

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