Mercurial > ~darius > hgwebdir.cgi > stm32temp
annotate delay.c @ 89:fc21fb5b171b default tip
Make error message more useful
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Fri, 13 Mar 2015 11:39:59 +1030 |
parents | cecb0506f4b8 |
children |
rev | line source |
---|---|
40
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
1 #include <assert.h> |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
2 #include <stdint.h> |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
3 #include "stm32f10x.h" |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
4 #include "delay.h" |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
5 |
14
891841f5f785
Fix delay() to be accurate as measured on the cro.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
6 /* Sleep for nCount usec |
891841f5f785
Fix delay() to be accurate as measured on the cro.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
7 */ |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 void |
14
891841f5f785
Fix delay() to be accurate as measured on the cro.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
9 delay(uint32_t nCount) { |
40
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
10 uint32_t dly, cnt, clk_per_usec, max_dly; |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
11 volatile uint32_t *DWT_CYCCNT = (uint32_t *)0xe0001004; |
14
891841f5f785
Fix delay() to be accurate as measured on the cro.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
12 |
40
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
13 #ifdef SYSCLK_FREQ_72MHz |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
14 clk_per_usec = 72; |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
15 max_dly = (1<<31) / clk_per_usec; /* Really half the maximum (still ~30 seconds at 72MHz) */ |
14
891841f5f785
Fix delay() to be accurate as measured on the cro.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
16 #else |
40
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
17 #error "Unknown clock frequency" |
14
891841f5f785
Fix delay() to be accurate as measured on the cro.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
18 #endif |
40
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
19 assert(nCount < max_dly); |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
20 |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
21 cnt = *DWT_CYCCNT; /* Get current cycle count */ |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
22 dly = nCount * clk_per_usec; |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
23 dly += cnt; /* Compute cycle count to stop at */ |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
24 if (dly < cnt) |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
25 /* Stop count wrapped, wait until the counter wraps around */ |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
26 while (*DWT_CYCCNT > cnt) |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
27 ; |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
28 /* Wait until we get to the stop count */ |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
29 while (*DWT_CYCCNT < dly) |
a38003b97de6
Use debug cycle counter to handle delays.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
30 ; |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
31 } |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
32 |