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

Reduce ADC noise #3652

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Reduce ADC noise #3652

wants to merge 2 commits into from

Conversation

zyren
Copy link
Contributor

@zyren zyren commented Jun 3, 2023

This is an experimental pull request. Another engineer told me: After he consulted a lot of relevant information, including ST official information, he obtained a description of FLASH_PrefetchBufferCmd. Try turning it off with FLASH_PrefetchBufferCmd(DISABLE), and by observation, it does reduce the magnitude of the joystick noise.
The indirect influence factors of FLASH_PrefetchBufferCmd on ADC noise are as follows:

When FLASH_PrefetchBufferCmd is enabled, the noise of the ADC increases, which may suggest some indirect effects.

Here are some possibilities:

  1. System Noise: Enabling the prefetch instruction buffer may increase the system's power consumption, introduce more power supply noise, or affect the CPU's handling time for ADC interrupts. This could indirectly affect the noise level of the ADC, leading to an increase in the LSB value..

  2. Interrupt Delay: The prefetch instruction buffer could potentially cause a delay in the CPU when handling ADC interrupts, which may affect the timing of ADC reads and possibly lead to increased noise.

  3. Impact of System Clock: The prefetch instruction buffer could potentially have an effect on the system clock. If the ADC sampling time is correlated with these clock signals, it could possibly lead to increased noise.

@ggoraa
Copy link

ggoraa commented Jun 25, 2023

Why did you disable FLASH_PrefetchBufferCmd only for TX16S and Taranis? Why not enable it for Horus also? TX16S and Horus radios actually share the same MCU, as all color radios do

@zyren
Copy link
Contributor Author

zyren commented Jun 26, 2023

Why did you disable FLASH_PrefetchBufferCmd only for TX16S and Taranis? Why not enable it for Horus also? TX16S and Horus radios actually share the same MCU, as all color radios do

Hi, sorry I have little development experience, I am not a professional developer, I just try to submit this PR. because this is experimental, there is a little effect in the actual test. But I haven't tested on other radios, so I don't know if this PR can be merged. I hope more developers will see more tests of different hardware. I don't want to make any changes to other Radio hardware type that I haven't tested. I'm afraid my changes will mislead others. In addition, I'm not sure if the major developers have specifically turned on this feature for essential functionality.

@ggoraa
Copy link

ggoraa commented Jun 26, 2023

Other radios are very similar in terms of hardware, X12S and TX16S for example have the same MCU and same board layout, I think there won't be any problems if FLASH_PrefetchBufferCmd will be disabled for all color radios
@gagarinlg any thoughts?

@gagarinlg
Copy link
Member

I think it should be disabled for all radios that use the internal ADC of the MCU, if the performance input is not too bad

@raphaelcoeffic raphaelcoeffic marked this pull request as draft August 12, 2023 05:57
@pfeerick
Copy link
Member

pfeerick commented Jul 7, 2024

Just asked ChatGPT about this... seems to be able to regurtigate the same sort of information from somewhere Google won't readily tell me about 🤷 ...

edit: welp... no longer a valid command command 😢 Should we be turning the PREFETCH_ENABLE off in stm32f(2/4)xx_hal_conf.h instead? As I don't think __HAL_FLASH_PREFETCH_BUFFER_DISABLE is right... i.e. configure it right the first time?

The FLASH_PrefetchBufferCmd function is used to enable or disable the Flash prefetch buffer in STM32 microcontrollers. This prefetch buffer can improve the performance of code execution from Flash memory by preloading instructions.

However, enabling the prefetch buffer can have implications for ADC (Analog-to-Digital Converter) noise. Here are the key points to consider:

Implications of FLASH_PrefetchBufferCmd on ADC Noise

  1. Increased System Noise:

    • Enabling the prefetch buffer can lead to increased noise in the system. This noise can couple into the ADC analog circuitry and affect the ADC's conversion accuracy. This is because prefetch operations can create additional current spikes on the power supply lines, which can introduce noise into the analog power supply (VDDA) or the reference voltage (VREF).
  2. Power Supply Noise:

    • The power supply for the ADC (VDDA) and the microcontroller core (VDD) is often shared or at least closely related. Activity in the digital part of the microcontroller, such as prefetching instructions, can create transients on the power supply lines, leading to fluctuations that can affect the ADC readings.
  3. PCB Layout and Decoupling:

    • Proper PCB layout and decoupling are crucial to minimize the impact of digital noise on ADC performance. Adding sufficient decoupling capacitors close to the microcontroller’s power pins can help filter out high-frequency noise introduced by the prefetch buffer.
  4. Software Considerations:

    • If the prefetch buffer must be enabled for performance reasons, software strategies can be employed to minimize its impact on ADC readings. For example, ADC conversions can be triggered during periods of low digital activity or in low-power modes where the prefetch buffer might be less active.
  5. ADC Calibration:

    • Regularly calibrating the ADC can help compensate for some of the noise introduced by enabling the prefetch buffer. Calibration adjusts the ADC’s internal parameters to account for any offsets or gain errors that might be influenced by system noise.

Recommendations

  • Evaluate Performance Needs:

    • Determine if the performance benefit of enabling the prefetch buffer outweighs the potential impact on ADC accuracy. For many applications, the slight performance gain may not justify the increased noise.
  • Noise Mitigation:

    • Use good PCB design practices, such as separating analog and digital ground planes, using proper decoupling capacitors, and maintaining a clean power supply for the ADC.
  • Conditional Enabling:

    • Consider conditionally enabling the prefetch buffer only during specific operations where high performance is critical and disabling it during ADC conversions if noise is a concern.
  • Experimentation:

    • Perform empirical testing by enabling and disabling the prefetch buffer while measuring the ADC noise to quantify its impact. This can help make an informed decision based on actual data.

In summary, while FLASH_PrefetchBufferCmd can enhance code execution speed, it may introduce additional noise that affects ADC performance. Careful consideration, testing, and implementation of noise mitigation techniques are essential to balance the performance and accuracy requirements of your application.

@raphaelcoeffic
Copy link
Member

Maybe something to consider: we do use oversampling & decimation, which relies on noise to enhance the precision. Before disabling flash prefetch, we should have extensive tests showing the tradeoffs with tangible numbers.

@pfeerick
Copy link
Member

pfeerick commented Jul 8, 2024 via email

@3djc
Copy link
Collaborator

3djc commented Jul 8, 2024

And of course things might be very different for new H7/H5 based radio

@raphaelcoeffic
Copy link
Member

raphaelcoeffic commented Jul 8, 2024

is via the stm...hal.h file the right way to do this

PREFETCH_ENABLE is used only in HAL_Init(), which we don't use at all.

For reference:

HAL_StatusTypeDef HAL_Init(void)
{
  /* Configure Flash prefetch, Instruction cache, Data cache */ 
#if (INSTRUCTION_CACHE_ENABLE != 0U)
  __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
#endif /* INSTRUCTION_CACHE_ENABLE */

#if (DATA_CACHE_ENABLE != 0U)
  __HAL_FLASH_DATA_CACHE_ENABLE();
#endif /* DATA_CACHE_ENABLE */

#if (PREFETCH_ENABLE != 0U)
  __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
#endif /* PREFETCH_ENABLE */

  /* Set Interrupt Group Priority */
  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

  /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  HAL_InitTick(TICK_INT_PRIORITY);

  /* Init the low level hardware */
  HAL_MspInit();

  /* Return function status */
  return HAL_OK;
}

As far as I can see (did not check in debugger), none of the flash instruction cache, data cache or prefetch buffers are enabled.

Caches and prefetch buffer are enabled directly in SetSysClock on F4 and F2 targets:

  • system_stm32f4xx.c:
    /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
    FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS;
  • system_stm32f2xx.c:
    /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
    FLASH->ACR = FLASH_ACR_PRFTEN |FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_3WS;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants