Skip to content

Commit 1875ec5

Browse files
author
Robert Muchsel
authored
MAX78002: Run CNN at 50 MHz in non-pipeline mode (#245)
* MAX78002: Run CNN at 50 MHz in non-pipeline mode * Improve printout to better match display
1 parent 7cea14a commit 1875ec5

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

izer/backend/max7800x.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ def create_net(self) -> str: # pylint: disable=too-many-locals,too-many-branche
227227
state.pipeline = tc.dev.SUPPORT_PIPELINE
228228
pipeline = state.pipeline # Cache
229229

230-
if state.pll is None:
231-
state.pll = pipeline
230+
if state.pll is None and tc.dev.SUPPORT_PLL:
231+
state.pll = True
232232

233233
if not state.balance_power and not state.pll:
234234
eprint('`--max-speed` requires `--pll` or `--pipeline`.')

izer/toplevel.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ def main(
288288
debugwait = state.debug_wait
289289
measure_energy = state.measure_energy
290290
pll = state.pll
291-
clock_switch = pll and state.balance_power
291+
pipeline = state.pipeline
292+
clock_switch = pll and pipeline and state.balance_power
292293
clock_speed = f'PLL ({tc.dev.PLL_SPEED} MHz)' if pll else f'APB ({tc.dev.APB_SPEED} MHz)'
293294
sleep = state.sleep
294295
softmax = state.softmax
@@ -527,7 +528,7 @@ def main(
527528
memfile.write(' cnn_disable(); // Disable clock and power to CNN\n'
528529
' // Enable primary clock\n'
529530
' MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IPO);\n\n'
530-
' printf("Measuring system base power...\\n");\n'
531+
' printf("Measuring system base (idle) power...\\n");\n'
531532
' SYS_START;\n')
532533
if not riscv:
533534
memfile.write(' MXC_Delay(SEC(1));\n')
@@ -536,7 +537,7 @@ def main(
536537
memfile.write(' SYS_COMPLETE;\n\n')
537538

538539
if embedded_code and apifile is not None:
539-
cdiv = '4' if clock_switch else '1'
540+
cdiv = '4' if pll and state.balance_power else '1'
540541
memfile.write(' // Enable peripheral, enable CNN interrupt, turn on CNN clock\n'
541542
f' // CNN clock: {clock_speed} div {cdiv}\n'
542543
' cnn_enable(MXC_S_GCR_PCLKDIV_CNNCLKSEL_'
@@ -694,7 +695,7 @@ def main(
694695
memfile.write(' load_input(); // Load data input\n')
695696
if clock_switch:
696697
select_clock(memfile, 'IPLL', 'DIV1', f'CNN clock: {clock_speed} div 1',
697-
pll_wait=False)
698+
pll_wait=False, prefix=' ')
698699
memfile.write(' cnn_start(); // Run inference\n')
699700
if fifo:
700701
memfile.write(' load_input(); // Load data input via FIFO\n')
@@ -710,7 +711,7 @@ def main(
710711
memfile.write(' while (cnn_time == 0); // Spin wait\n')
711712
if clock_switch:
712713
select_clock(memfile, 'IPLL', 'DIV4', f'CNN clock: {clock_speed} div 4',
713-
pll_wait=False)
714+
pll_wait=False, prefix=' ')
714715
memfile.write(' }\n'
715716
' CNN_COMPLETE;\n\n')
716717

@@ -733,9 +734,9 @@ def main(
733734
memfile.write(' }\n\n')
734735

735736
if clock_switch:
736-
select_clock(memfile, 'PCLK', 'DIV1',
737-
f'Switch CNN clock to APB ({tc.dev.APB_SPEED} MHz) and disable PLL')
738-
memfile.write(' MXC_GCR->ipll_ctrl &= ~MXC_F_GCR_IPLL_CTRL_EN;\n\n')
737+
select_clock(memfile, 'IPLL', 'DIV4',
738+
f'Switch CNN clock to {clock_speed} div 4\n',
739+
pll_wait=False)
739740

740741
if embedded_code and apifile is not None:
741742
function_header(apifile, function='boost_disable',
@@ -792,6 +793,9 @@ def main(
792793

793794
if embedded_code and apifile is not None:
794795
function_footer(apifile) # disable()
796+
if pll:
797+
memfile.write(' MXC_GCR->ipll_ctrl &= ~MXC_F_GCR_IPLL_CTRL_EN; '
798+
'// Disable IPLL\n\n')
795799

796800
if not forever:
797801
if softmax:
@@ -902,16 +906,17 @@ def select_clock(
902906
divider: int,
903907
comment: str = '',
904908
pll_wait: bool = True,
909+
prefix: str = '',
905910
) -> None:
906911
"""
907912
Switch clock source and divider.
908913
"""
909914
if comment != '':
910-
memfile.write(f' // {comment}\n')
915+
memfile.write(f'{prefix} // {comment}\n')
911916
if source == 'IPLL' and pll_wait:
912-
memfile.write(' while ((MXC_GCR->ipll_ctrl & MXC_F_GCR_IPLL_CTRL_RDY) != '
917+
memfile.write(f'{prefix} while ((MXC_GCR->ipll_ctrl & MXC_F_GCR_IPLL_CTRL_RDY) != '
913918
'MXC_F_GCR_IPLL_CTRL_RDY) ;\n')
914-
memfile.write(' MXC_GCR->pclkdiv = (MXC_GCR->pclkdiv & '
919+
memfile.write(f'{prefix} MXC_GCR->pclkdiv = (MXC_GCR->pclkdiv & '
915920
'~(MXC_F_GCR_PCLKDIV_CNNCLKDIV | MXC_F_GCR_PCLKDIV_CNNCLKSEL))\n'
916-
f' | MXC_S_GCR_PCLKDIV_CNNCLKDIV_{divider} | '
921+
f'{prefix} | MXC_S_GCR_PCLKDIV_CNNCLKDIV_{divider} | '
917922
f'MXC_S_GCR_PCLKDIV_CNNCLKSEL_{source};\n')

0 commit comments

Comments
 (0)