我正在为我的stm32f429板编写嵌入式操作系统的代码。我正在测试此代码的使用时间:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdint.h>
#define DEFAULT_DELAY 1
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 STOPWATCH_START { m_nStart = *(*(volatile unsigned int*)0xE0001004);}//DWT_CYCCNT;}
#define STOPWATCH_STOP { m_nStop = *(*(volatile unsigned int *)0xE0001004);}
static inline void stopwatch_reset(void)
{
/* Enable DWT */
DEMCR |= DEMCR_TRCENA;
*DWT_CYCCNT = 0;
/* Enable CPU cycle counter */
DWT_CTRL |= CYCCNTENA;
}
static inline uint32_t stopwatch_getticks()
{
return CPU_CYCLES;
}
static inline void stopwatch_delay(uint32_t ticks)
{
stopwatch_reset();
while(1)
{
if (stopwatch_getticks() >= ticks)
break;
}
}
uint32_t CalcNanosecondsFromStopwatch(uint32_t nStart, uint32_t nStop)
{
uint32_t nTemp;
uint32_t n;
uint32_t SystemCoreClock = 180000000;
nTemp = nStop - nStart;
nTemp *= 1000; // Scale cycles by 1000.
n = SystemCoreClock / 1000000; // Convert Hz to MHz
nTemp = nTemp / n; // nanosec = (Cycles * 1000) / (Cycles/microsec)
return nTemp;
}
int main( int argc, char **argv )
{
int delay = DEFAULT_DELAY; // Initial value for the delay
int timeDiff = 0;
STOPWATCH_START;
printf("Try\n\n");
STOPWATCH_STOP;
timeDiff = CalcNanosecondsFromStopwatch(m_nStart, m_nStop);
printf("My function took %d nanoseconds\n", timeDiff);
return( 0 );
}
它编译没有错误,但是当我在我的stm32f429上运行这个程序时,我得到了一个SEGV错误,可能在#define STOPWATCH_START中。也许我在寄存器上有问题(?)。
代码为http://pastebin.com/qr6sF9eU(它删除了我使用的系统调用调用)
make的输出是:http://pastebin.com/Q14xTaXH
我在stm32f429板上运行测试时的输出是:http://pastebin.com/sGmjZjxj
你能帮助我吗?
答案 0 :(得分:0)
您无法在SEGV
中获得#define
,因为#define
是预处理程序指令,并且不构成输出的一部分。它可能位于#define生成的代码中。您是否故意从0xE0001004
读取地址,将该地址的内容读作另一个地址,然后阅读其内容?这听起来不太可能是操作时钟的方式。
答案 1 :(得分:0)
宏STOPWATCH_START;
和STOPWATCH_STOP;
导致&#34;非法间接&#34;我的编译器的错误。双指针引用使得读取的计数器值用作指针。
#define STOPWATCH_START { m_nStart = *(*(volatile unsigned int*)0xE0001004);}
#define STOPWATCH_STOP { m_nStop = *(*(volatile unsigned int *)0xE0001004);}
根据您的其他#define
语句及其中的地址,应该是
#define STOPWATCH_START { m_nStart = CPU_CYCLES;}
#define STOPWATCH_STOP { m_nStop = CPU_CYCLES;}
正如您在代码中的其他地方使用过的那样?
答案 2 :(得分:0)
NVIC寄存器很可能受MPU保护,用户代码无法访问。使用操作系统时,不能搞砸所有内容。