在STM32上用C语言生成纳秒延迟

问题描述 投票:0回答:3

我正在使用 STM32F2 控制器,并通过 8 位并行接口与 ST7036 LCD 显示屏连接。

数据表称地址保持和设置时间之间应该有 20 纳秒的延迟。

如何在 C 中生成 20 纳秒延迟?

c stm32 lcd
3个回答
15
投票

使用下面的

stopwatch_delay(4
) 来实现大约 24ns 的延迟。它使用 STM32 的 DWT_CYCCNT 寄存器,该寄存器专门用于计算实际时钟周期,位于地址 0xE0001004。

要验证延迟准确性(请参阅

main
),您可以调用
STOPWATCH_START
,运行
stopwatch_delay(ticks)
,然后调用
STOPWATCH_STOP
并使用
CalcNanosecondsFromStopwatch(m_nStart, m_nStop)
进行验证。根据需要调整
ticks

uint32_t m_nStart;               //DEBUG Stopwatch start cycle counter value
uint32_t m_nStop;                //DEBUG Stopwatch stop cycle counter value

#define DEMCR_TRCENA    0x01000000

/* Core Debug registers */
#define DEMCR           (*((volatile uint32_t *)0xE000EDFC))
#define DWT_CTRL        (*(volatile uint32_t *)0xe0001000)
#define CYCCNTENA       (1<<0)
#define DWT_CYCCNT      ((volatile uint32_t *)0xE0001004)
#define CPU_CYCLES      *DWT_CYCCNT
#define CLK_SPEED         168000000 // EXAMPLE for CortexM4, EDIT as needed

#define STOPWATCH_START { m_nStart = CPU_CYCLES;}
#define STOPWATCH_STOP  { m_nStop = CPU_CYCLES;}


static inline void stopwatch_reset(void)
{
    /* Enable DWT */
    DEMCR |= DEMCR_TRCENA; 
    *DWT_CYCCNT = 0;             
    /* Enable CPU cycle counter */
    DWT_CTRL |= CYCCNTENA;
}

static inline void stopwatch_delay(uint32_t ticks)
{
    uint32_t end_ticks = ticks + CPU_CYCLES;
    while(1)
    {
            if (CPU_CYCLES >= end_ticks)
                    break;
    }
}

// WARNING: ONLY VALID FOR <25ms measurements due to scaling by 1000!
uint32_t CalcNanosecondsFromStopwatch(uint32_t nStart, uint32_t nStop)
{
    uint32_t nDiffTicks;
    uint32_t nSystemCoreTicksPerMicrosec;
    
    // Convert (clk speed per sec) to (clk speed per microsec)
    nSystemCoreTicksPerMicrosec = CLK_SPEED / 1000000;
    
    // Elapsed ticks
    nDiffTicks = nStop - nStart;
    
    // Elapsed nanosec = 1000 * (ticks-elapsed / clock-ticks in a microsec)
    return 1000 * nDiffTicks / nSystemCoreTicksPerMicrosec;
} 

void main(void)
{
    int timeDiff = 0;
    stopwatch_reset();
    
    // =============================================
    // Example: use a delay, and measure how long it took
    STOPWATCH_START;
    stopwatch_delay(168000); // 168k ticks is 1ms for 168MHz core
    STOPWATCH_STOP;
    
    timeDiff = CalcNanosecondsFromStopwatch(m_nStart, m_nStop);
    printf("My delay measured to be %d nanoseconds\n", timeDiff);
    
    // =============================================
    // Example: measure function duration in nanosec
    STOPWATCH_START;
    // run_my_function() => do something here
    STOPWATCH_STOP;
    
    timeDiff = CalcNanosecondsFromStopwatch(m_nStart, m_nStop);
    printf("My function took %d nanoseconds\n", timeDiff);
}

9
投票

我发现的 Stm32f2 的第一个规范假设时钟频率为 120 MHz。每个时钟周期大约需要 8ns。在连续的写入或读/写操作之间需要大约三个单周期指令。在 C 中,

a++;
可能会做(如果 a 位于堆栈中)。


1
投票

您应该查看芯片中可用的 FSMC 外设。虽然配置可能很复杂,特别是如果您没有放入其设计的内存部分,您可能会发现您的并行接口设备很好地映射到其中一种内存接口模式。

这些类型的外部内存控制器必须有一堆可配置的时序选项来支持各种不同的内存芯片,这样您就能够保证数据表所需的时序。

能够做到这一点的好处是,您的 LCD 将看起来像任何旧的内存映射外围设备,抽象出较低级别的接口细节。

© www.soinside.com 2019 - 2024. All rights reserved.