RT-AICHIP-sample
SystemTickTimer.c
[詳解]
1 /* ------------------------------------------------------------ *
2 File SystemTickTimer.c
3 
4 SystemTickTimerを用いた関数群
5 1msec,1usec刻みのタイマー
6 1usecの時間測定用関数
7 * ------------------------------------------------------------ */
8 #include "type.h"
9 #include "LPC13xx.h" // LPC13xx Peripheral Registers
10 #include "core_cm3.h"
11 #include "SystemTickTimer.h"
12 
13 volatile static uint32_t systick_wait_count;
14 volatile static uint32_t systick_time_count;
15 volatile static uint8_t systick_handler_state;
16 
17 
18 /*----------------------------------------------------------------------------
19  Input void
20  Output void
21 
22  SysTickTimerの初期化
23  *---------------------------------------------------------------------------*/
24 static void initSystemTickTimer()
25 {
26  SysTick->CTRL = 0;
27  SysTick->LOAD = 0;
28  SysTick->VAL = 0;
29  SysTick->CTRL = 0x7;
30  SysTick->LOAD = 72-1; //720000-1で10msec, 72-1で1usec
31  systick_handler_state = 1;
32 };
33 /*----------------------------------------------------------------------------
34  Input wait_count
35  Output void
36 
37  wait_count * 1usec待つ
38  割り込み関数内とmainで同時に呼ばないこと
39  *---------------------------------------------------------------------------*/
40 void wait1usec(uint32_t wait_count)
41 {
42  initSystemTickTimer();
43  systick_wait_count = 0;
44  while(systick_wait_count < wait_count);
45  SysTick->CTRL = 0x0;
46  systick_handler_state = 0;
47 
48  return;
49 };
50 /*----------------------------------------------------------------------------
51  Input wait_count
52  Output void
53 
54  wait_count * 1msec待つ
55  割り込み関数内とmainで同時に呼ばないこと
56  *---------------------------------------------------------------------------*/
57 void wait1msec(uint32_t wait_count)
58 {
59  if(systick_handler_state == 0)initSystemTickTimer();
60  systick_wait_count = 0;
61  while(systick_wait_count < wait_count*1000);
62  SysTick->CTRL = 0x0;
63  systick_handler_state = 0;
64 
65  return;
66 };
67 /*----------------------------------------------------------------------------
68  Input void
69  Output void
70 
71  time_count_end(void)とセットで使用.
72  time_count_start()を呼んでから, time_count_end();
73  を呼ぶまでsystick_wait_countをインクリメントする.
74  時間の測定に使用
75  *---------------------------------------------------------------------------*/
76 void time_count_start(void)
77 {
78  if(systick_handler_state == 0)initSystemTickTimer();
79  systick_time_count = 0;
80 
81 }
82 /*----------------------------------------------------------------------------
83  Input void
84  Output systick_wait_count タイマのカウント値
85 
86  time_count_start(void)とセットで使用.
87  *---------------------------------------------------------------------------*/
89 {
90  SysTick->CTRL = 0x0;
91  systick_handler_state = 0;
92  return systick_time_count;
93 }
94 /*----------------------------------------------------------------------------
95  Input void
96  Output void
97  SysTickTimerの割り込み関数.
98  *---------------------------------------------------------------------------*/
99 void SysTick_Handler (void)
100 {
101  systick_wait_count ++;
102  systick_time_count ++;
103 }
104 
105 
106 /******************************************************************************
107 ** End Of File
108 ******************************************************************************/
void wait1usec(uint32_t wait_count)
CMSIS Cortex-M3 Core Peripheral Access Layer Header File.
void SysTick_Handler(void)
unsigned char uint8_t
Definition: type.h:27
#define SysTick
Definition: core_cm3.h:724
void wait1msec(uint32_t wait_count)
void time_count_start(void)
uint32_t time_count_end(void)
unsigned int uint32_t
Definition: type.h:29