MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / ls1c_spi_get_div

Function ls1c_spi_get_div

bsp/loongson/ls1cdev/libraries/ls1c_spi.c:73–129  ·  view source on GitHub ↗

* 根据SPI时钟频率计算分频系数 * @max_speed_hz SPI最大通信速度 * @ret 分频系数 */

Source from the content-addressed store, hash-verified

71 * @ret 分频系数
72 */
73unsigned int ls1c_spi_get_div(unsigned int max_speed_hz)
74{
75 unsigned long clk = 0;
76 unsigned int div = 0;
77 unsigned int div_tmp = 0;
78 unsigned int bit = 0;
79
80 clk = clk_get_apb_rate();
81 div = DIV_ROUND_UP(clk, max_speed_hz);
82
83 if (div < 2)
84 div = 2;
85
86 if (div > 4096)
87 div = 4096;
88
89 bit = ls1c_fls(div) - 1;
90 switch (1 << bit)
91 {
92 case 16:
93 div_tmp = 2;
94 if (div > (1 << bit))
95 {
96 div_tmp++;
97 }
98 break;
99
100 case 32:
101 div_tmp = 3;
102 if (div > (1 << bit))
103 {
104 div_tmp += 2;
105 }
106 break;
107
108 case 8:
109 div_tmp = 4;
110 if (div > (1 << bit))
111 {
112 div_tmp -= 2;
113 }
114 break;
115
116 default:
117 div_tmp = bit - 1;
118 if (div > (1 << bit))
119 {
120 div_tmp++;
121 }
122 break;
123 }
124/*
125 printf("[%s] clk=%ld, max_speed_hz=%d, div_tmp=%d, bit=%d\r\n",
126 __FUNCTION__, clk, max_speed_hz, div_tmp, bit);
127*/
128 return div_tmp;
129}
130

Callers 1

ls1c_spi_set_clockFunction · 0.85

Calls 2

ls1c_flsFunction · 0.85
clk_get_apb_rateFunction · 0.70

Tested by

no test coverage detected