ls1c_public.c 2.6 KB

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