From 6babf84a1b0acac2fdc314ed669231bebe8e91b3 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sat, 19 Jan 2019 03:53:43 +0200 Subject: [PATCH 1/3] Update cpufreq module. * Support H7. * Disable this module for M4. * Pass frequencies in MHz instead of constants. * Add get_supported_frequencies(). --- src/omv/py/py_cpufreq.c | 157 ++++++++++++++++++++++++++++++--------- src/omv/py/qstrdefsomv.h | 8 +- 2 files changed, 122 insertions(+), 43 deletions(-) diff --git a/src/omv/py/py_cpufreq.c b/src/omv/py/py_cpufreq.c index bb431f348..69e39feb2 100644 --- a/src/omv/py/py_cpufreq.c +++ b/src/omv/py/py_cpufreq.c @@ -3,7 +3,7 @@ * Copyright (c) 2013/2014 Ibrahim Abdelkader * This work is licensed under the MIT license, see the file LICENSE for details. * - * CPU frequency module. + * CPU frequency scaling module. * */ @@ -14,57 +14,136 @@ #include STM32_HAL_H #include "py_cpufreq.h" -enum cpufreq_freqs { - CPUFREQ_120MHZ=0, - CPUFREQ_144MHZ=1, - CPUFREQ_168MHZ=2, - CPUFREQ_192MHZ=3, - CPUFREQ_216MHZ=4, - CPUFREQ_MAX -}; +#if defined(STM32F7) || defined(STM32H7) +#define ARRAY_LENGTH(x) (sizeof(x)/sizeof(x[0])) + +#if defined(STM32H7) +static const uint32_t cpufreq_freqs[] = {100, 200, 400}; +#elif defined(STM32F7) static const uint32_t cpufreq_pllq[] = {5, 6, 7, 8, 9}; -static const uint32_t cpufreq_freq[] = {120, 144, 168, 192, 216}; +static const uint32_t cpufreq_freqs[] = {120, 144, 168, 192, 216}; static const uint32_t cpufreq_latency[] = { // Flash latency (see table 11) FLASH_LATENCY_3, FLASH_LATENCY_4, FLASH_LATENCY_5, FLASH_LATENCY_7, FLASH_LATENCY_7 }; +#endif -void py_cpufreq_init0() +uint32_t cpufreq_get_cpuclk() { + uint32_t cpuclk = HAL_RCC_GetSysClockFreq(); + #if defined(STM32H7) + uint32_t flatency; + RCC_ClkInitTypeDef RCC_ClkInitStruct; + + HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &flatency); + switch (RCC_ClkInitStruct.SYSCLKDivider) { + case RCC_SYSCLK_DIV1: + break; + case RCC_SYSCLK_DIV2: + cpuclk /= 2; + break; + case RCC_SYSCLK_DIV4: + cpuclk /= 4; + break; + default: + break; + } + #endif + return cpuclk; } -mp_obj_t py_cpufreq_get_frequency() +mp_obj_t py_cpufreq_get_current_frequencies() { mp_obj_t tuple[4] = { - mp_obj_new_int(HAL_RCC_GetSysClockFreq()), - mp_obj_new_int(HAL_RCC_GetHCLKFreq()), - mp_obj_new_int(HAL_RCC_GetPCLK1Freq()), - mp_obj_new_int(HAL_RCC_GetPCLK2Freq()), + mp_obj_new_int(cpufreq_get_cpuclk() / (1000000)), + mp_obj_new_int(HAL_RCC_GetHCLKFreq() / (1000000)), + mp_obj_new_int(HAL_RCC_GetPCLK1Freq() / (1000000)), + mp_obj_new_int(HAL_RCC_GetPCLK2Freq() / (1000000)), }; return mp_obj_new_tuple(4, tuple); } -mp_obj_t py_cpufreq_set_frequency(mp_obj_t cpufreq_idx_obj) +mp_obj_t py_cpufreq_get_supported_frequencies() +{ + mp_obj_t freq_list = mp_obj_new_list(0, NULL); + for (int i=0; i= CPUFREQ_MAX) { + // Check if frequency is supported + int cpufreq_idx = -1; + uint32_t cpufreq = mp_obj_get_int(cpufreq_obj); + for (int i=0; i Date: Sat, 19 Jan 2019 03:56:35 +0200 Subject: [PATCH 2/3] Update CPU frequency scaling example. --- .../02-Board-Control/cpufreq_scaling.py | 24 +++++++++++++++++++ .../examples/02-Board-Control/overclocking.py | 18 -------------- 2 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 scripts/examples/02-Board-Control/cpufreq_scaling.py delete mode 100644 scripts/examples/02-Board-Control/overclocking.py diff --git a/scripts/examples/02-Board-Control/cpufreq_scaling.py b/scripts/examples/02-Board-Control/cpufreq_scaling.py new file mode 100644 index 000000000..95b994cab --- /dev/null +++ b/scripts/examples/02-Board-Control/cpufreq_scaling.py @@ -0,0 +1,24 @@ +# CPU frequency scaling example. +# +# This example shows how to use the cpufreq module to change the CPU frequency on the fly. +import sensor, image, time, cpufreq + +sensor.reset() # Reset and initialize the sensor. +sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE) +sensor.set_framesize(sensor.QQVGA) # Set frame size to QVGA (320x240) +sensor.skip_frames(time = 2000) # Wait for settings take effect. +clock = time.clock() # Create a clock object to track the FPS. + +def test_image_processing(): + for i in range(0, 50): + clock.tick() # Update the FPS clock. + img = sensor.snapshot() # Take a picture and return the image. + img.find_edges(image.EDGE_CANNY, threshold=(50, 80)) + +print("\nFrequency Scaling Test...") +for f in cpufreq.get_supported_frequencies(): + cpufreq.set_frequency(f) + clock.reset() + test_image_processing() + freqs = cpufreq.get_current_frequencies() + print("CPU Freq:%dMHz HCLK:%dMhz PCLK1:%dMhz PCLK2:%dMhz FPS:%.2f" %(freqs[0], freqs[1], freqs[2], freqs[3], clock.fps())) diff --git a/scripts/examples/02-Board-Control/overclocking.py b/scripts/examples/02-Board-Control/overclocking.py deleted file mode 100644 index ba0ddc0ca..000000000 --- a/scripts/examples/02-Board-Control/overclocking.py +++ /dev/null @@ -1,18 +0,0 @@ -# Overclocking Example -# -# This example shows how to overclock your OMV2 cam to 216MHz. The camera will -# stay overclocked until the next hard reset, if you need to keep this frequency -# call the set_frequency function from your main script. -# -# WARNING: Overclocking to 216MHz should be safe, however Use at your own risk! - -import cpufreq - -# Print current CPU frequency -print(cpufreq.get_frequency()) - -# Set frequency valid values are (120, 144, 168, 192, 216) -cpufreq.set_frequency(cpufreq.CPUFREQ_216MHZ) - -# Print current CPU frequency -print(cpufreq.get_frequency()) From 69f1f3c1f057e0f8c244f38bc7872be3e73a66c4 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sat, 19 Jan 2019 04:16:22 +0200 Subject: [PATCH 3/3] Fix OMV2 build. --- src/omv/py/py_cpufreq.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/omv/py/py_cpufreq.c b/src/omv/py/py_cpufreq.c index 69e39feb2..0fe6a61b4 100644 --- a/src/omv/py/py_cpufreq.c +++ b/src/omv/py/py_cpufreq.c @@ -13,6 +13,7 @@ #include #include STM32_HAL_H #include "py_cpufreq.h" +#include "py_helper.h" #if defined(STM32F7) || defined(STM32H7)