Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp32-core: Fix BOARD_CLOCK_FREQUENCY #413

Merged
merged 1 commit into from
Mar 6, 2020
Merged

Conversation

yamt
Copy link
Contributor

@yamt yamt commented Mar 2, 2020

Tested on qemu.

I left CONFIG_ESP32CORE_RUN_IRAM case and the "Don't ask me" comment
as it is because I don't understand it or can test it.

Tested on qemu.

I left CONFIG_ESP32CORE_RUN_IRAM case and the "Don't ask me" comment
as it is because I don't understand it or can test it.
Copy link
Contributor

@jerpelea jerpelea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not hardcode the frequency because tehre are 2 available crystal frequencyes
https://bitbucket.org/alinjerpelea/nuttx/src/ce7a0b390c7c983fbf3811734c8d0a4274b9c3bf/boards/xtensa/esp32/esp32-core/Kconfig#lines-9

@yamt
Copy link
Contributor Author

yamt commented Mar 2, 2020

@jerpelea
it's about BOARD_XTAL_FREQUENCY, isn't it?
this 80MHz is hardcoded in the bootloader. see the code referenced by the code comment.

@jerpelea
Copy link
Contributor

jerpelea commented Mar 2, 2020

@yamt
Copy link
Contributor Author

yamt commented Mar 2, 2020

@jerpelea
CONFIG_ESP32CORE_XTAL_40MZ is about BOARD_XTAL_FREQUENCY.
this change is about BOARD_CLOCK_FREQUENCY.
the reason to hardcode is explained in the comment in this patch.

are you going to investigate CONFIG_ESP32CORE_RUN_IRAM?
i don't have OpenOCD environment.

@jerpelea
Copy link
Contributor

jerpelea commented Mar 2, 2020

-# define BOARD_CLOCK_FREQUENCY BOARD_XTAL_FREQUENCY
+# define BOARD_CLOCK_FREQUENCY 80000000

this is your change

@jerpelea
Copy link
Contributor

jerpelea commented Mar 2, 2020

I do not have OpenOCD

@yamt
Copy link
Contributor Author

yamt commented Mar 2, 2020

@jerpelea
sorry, i don't understand your comments.
do you mean that, for some reasons, BOARD_CLOCK_FREQUENCY should be same as BOARD_XTAL_FREQUENCY? why?

@jerpelea
Copy link
Contributor

jerpelea commented Mar 2, 2020

In your change you replace this line with a hardcoded value
I am trying to understand your thinking since nither you or me have the actual HW

@yamt
Copy link
Contributor Author

yamt commented Mar 2, 2020

my thinking is that

  • the current code is broken at least if you run nuttx with esp-idf bootloader on qemu
  • the bootloader configures it 80MHz (hardcoded) and nuttx relies on the configuration
  • the "Don't ask me for an explanation" comment made me think the current code didn't have a good rationale anyway 89c33e9
  • i left the CONFIG_ESP32CORE_RUN_IRAM=y case unchanged because i can't test or understand

@patacongo
Copy link
Contributor

The frequency is only hardcoded if CONFIG_SUPPRESS_CLOCK_CONFIG is selected.

That is because the actual clock configuration logic is (or at least was) proprietary and Expressif never released this in source form, only as a binary library. If that library is downloaded and included in the build, then you can select any of the supported frequencies.

I don't follow ESP32 closely, but this change does not look correct and I do not think that it should be incorporated.

You should also look at TizenRT. Expressif took the NuttX ESP32 port and extended it in Tizen RT. You can bring those improvements in as you see fit, provided that we make sure that the Apache licensing is correct. See https://github.com/Samsung/TizenRT/tree/master/os/arch/xtensa.

Since that code is supported by Expressif, that should be considered an authoritative source.

@yamt
Copy link
Contributor Author

yamt commented Mar 3, 2020

do you mean non-CONFIG_SUPPRESS_CLOCK_CONFIG code can work? i don't think it even compile.
i agree it's better to fix it though.

i don't know how it was. but at this point the source code of the bootloader and hal are available. and they seem to hardcode 80MHz.

TizenRT just uses "#define BOARD_CLOCK_FREQUENCY (2 * BOARD_XTAL_FREQUENCY)" unconditionally.
which is 80MHz in case of CONFIG_ESP32CORE_XTAL_40MZ.

@jerpelea
Copy link
Contributor

jerpelea commented Mar 3, 2020

in my opion this is the proper fix instead of hardcoding it
#define BOARD_CLOCK_FREQUENCY (2 * BOARD_XTAL_FREQUENCY)

@yamt
Copy link
Contributor Author

yamt commented Mar 3, 2020

@jerpelea why do you think it should be 2 * BOARD_XTAL_FREQUENCY?
my understanding is that it should match the bootloader, which seems to hardcode 80MHz.

@jerpelea
Copy link
Contributor

jerpelea commented Mar 3, 2020

The frequency must match a clock source and if on the board is a 26MHZ quarts it it impossible to get 80MHZ. There may be 2 or more variants of the bootloader with different hardc oded frequencyes.
As a general rule you should avoid hardcoding clock s(I learned it the hard way)

@yamt
Copy link
Contributor Author

yamt commented Mar 3, 2020

@jerpelea
there seems to be multiple clock sources available for CPU clock.
see esp32_technical_reference_manual_en.pdf 3.2.3 "CPU Clock"
for 80MHz, "PLL" clock, which is 320MHz, is used.
https://github.com/espressif/esp-idf/blob/master/components/soc/src/esp32/rtc_clk.c#L581-L585

@patacongo
Copy link
Contributor

@jerpelea I am expected that you will merge the change (or close it) after your discussion with @yamt concludes. Since you raise the issues, only you will know how to dispose of this PR.

@jerpelea
Copy link
Contributor

jerpelea commented Mar 3, 2020

@yamt please add the following change (same as TizenRT)so that I can merge the PR
#define BOARD_CLOCK_FREQUENCY (2 * BOARD_XTAL_FREQUENCY)

@jerpelea
Copy link
Contributor

jerpelea commented Mar 3, 2020

@patacongo yes I will

@yamt
Copy link
Contributor Author

yamt commented Mar 4, 2020

@jerpelea i won't until i understand why it should be (2 * BOARD_XTAL_FREQUENCY). maybe i will try to ask espressif.

@masayuki2009 masayuki2009 mentioned this pull request Mar 5, 2020
@MasayukiIshikawa
Copy link
Contributor

Hi, @yamt

Tested on qemu.

Could you tell us how to run nuttx for esp32 with qemu?
I'm happy if you update README.txt under boards/xtensa/esp32/esp32-core.

@igrr
Copy link

igrr commented Mar 5, 2020

The frequency must match a clock source and if on the board is a 26MHZ quarts it it impossible to get 80MHZ.

On the ESP32, APB/CPU frequencies higher than the XTAL frequency are obtained using one of the two PLLs. So either of 26 or 40 MHz XTAL can be used as clock source for the PLL. PLL clock (320 or 480 MHz) is then internally divided to produce the CPU clock (80/160/240MHz) and the APB clock (80MHz).

@igrr
Copy link

igrr commented Mar 5, 2020

By the way, this line:

#ifdef CONFIG_ESP32CORE_RUN_IRAM
#  define BOARD_CLOCK_FREQUENCY (2 * BOARD_XTAL_FREQUENCY)
#else

looks incorrect to me. As I understand, CONFIG_ESP32CORE_RUN_IRAM option means that the program is loaded directly into IRAM over JTAG, and the 2nd stage bootloader is not involved. In this case, CPU and APB frequencies will be the same as after reset, and will exactly match the XTAL frequency, so 2 * should be removed.

@jerpelea
Copy link
Contributor

jerpelea commented Mar 5, 2020

This is why I usggested that the change should be
define BOARD_CLOCK_FREQUENCY (2 * BOARD_XTAL_FREQUENCY)
instead of a fixed frequency

@maht
Copy link
Contributor

maht commented Mar 5, 2020

@MasayukiIshikawa

Hi, @yamt

Tested on qemu.

Could you tell us how to run nuttx for esp32 with qemu?
I'm happy if you update README.txt under boards/xtensa/esp32/esp32-core.

By request, I created this pull request after polishing some patches I did some days ago to ease running the ESP32 QEMU: #437

Hope it would be useful. Feedback and/or improvements are welcome.

@igrr
Copy link

igrr commented Mar 5, 2020

@jerpelea, sorry, i still don't understand why the BOARD_CLOCK_FREQUENCY should be exactly twice BOARD_XTAL_FREQUENCY. In the ESP32, the CPU frequency can be either of the following:

  • XTAL frequency, divided by integer N
  • 80MHz
  • 160MHz
  • 240MHz

Note that 80/160/240 values are independent of the XTAL frequency, which can be 40 or 26 MHz. So in the specific case when XTAL frequency is 40 MHz, indeed the CPU frequency can be twice the XTAL frequency. But in theory you may have a 26MHz XTAL along with 80MHz CPU frequency.

@jerpelea jerpelea merged commit c0762fe into apache:master Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants