Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.

Commit f3a9bed

Browse files
committed
improved startup code
1 parent 8c2e34b commit f3a9bed

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

mm32/cores/arduino/system_MM32SPIN2xx_p.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,24 @@ void SystemInit (void)
183183
and at this point in the boot process we still do not have a AHB clock.
184184
*/
185185

186-
u32 u32_divide(u32 n, u32 d)
186+
u32 u32_divide (u32 num, uint32_t den)
187187
{
188-
u32 q = 0;
189-
while (n >= d) {
190-
q++;
191-
n -= d;
192-
}
193-
return q;
188+
u32 bit = 1;
189+
u32 res = 0;
190+
191+
while (den < num && bit && !(den & (1L << 31))) {
192+
den <<= 1;
193+
bit <<= 1;
194+
}
195+
while (bit) {
196+
if (num >= den) {
197+
num -= den;
198+
res |= bit;
199+
}
200+
bit >>= 1;
201+
den >>= 1;
202+
}
203+
return res;
194204
}
195205

196206
/**
@@ -212,7 +222,7 @@ u32 AutoCalPllFactor(u32 pllclkSourceFrq, u32 pllclkFrq, u8 *plln, u8* pllm)
212222
for(n = 0; n < 64 ; n++)
213223
{
214224
// tempFrq = pllclkSourceFrq * (n + 1) / (m + 1);
215-
tempFrq = pllclkSourceFrq * u32_divide(n + 1, m + 1);
225+
tempFrq = u32_divide(pllclkSourceFrq * (n + 1), m + 1);
216226
tempFrq = (tempFrq > pllclkFrq) ? (tempFrq - pllclkFrq) : (pllclkFrq - tempFrq) ;
217227

218228
if(minDiff > tempFrq)

0 commit comments

Comments
 (0)