我正在爲我的stm32f429開發板編寫嵌入式操作系統代碼。我測試此代碼經過時間:在ARM Cortex M4處理器中經歷時間測試C代碼時出錯SEGV
#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(它刪除調用系統調用,我使用)
化妝的輸出是:http://pastebin.com/Q14xTaXH
輸出,當我在stm32f429主板上運行我的測試是:http://pastebin.com/sGmjZjxj
你能幫我嗎?
你沒有得到一個告訴「賦值指針從整數沒有轉換」警告?你已經有了一個非常好的定義。爲什麼不使用'#define STOPWATCH_START {m_nStart = CPU_CYCLES; }而不是重複代碼並在過程中引入虛假的雙引號? – Notlikethat 2014-11-23 18:22:18
你好,不好意思。 我再次獲得SEGV,同樣使用'#define STOPWATCH_START {m_nStart = CPU_CYCLES;} #define STOPWATCH_STOP {m_nStop = CPU_CYCLES;}' – Anth 2014-11-23 18:34:56
如何在沒有操作系統且沒有MMU的微控制器「給出分段故障」你能更具體一些,因爲你描述的東西在這個微控制器上是不可能的... – 2014-11-23 19:30:53