annotate Lib/delay.c @ 412:e7a3be42ae1e

Debug code for interconnect cable debugging
author Jacob Alexander <haata@kiibohd.com>
date Sat, 20 Feb 2016 13:27:49 -0800
parents 6ac92b8614c0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
192
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
1 /* Teensyduino Core Library
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
2 * http://www.pjrc.com/teensy/
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
3 * Copyright (c) 2013 PJRC.COM, LLC.
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
4 * Modifications by Jacob Alexander 2013-2014
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
5 *
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
6 * Permission is hereby granted, free of charge, to any person obtaining
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
7 * a copy of this software and associated documentation files (the
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
8 * "Software"), to deal in the Software without restriction, including
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
9 * without limitation the rights to use, copy, modify, merge, publish,
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
10 * distribute, sublicense, and/or sell copies of the Software, and to
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
11 * permit persons to whom the Software is furnished to do so, subject to
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
12 * the following conditions:
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
13 *
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
14 * 1. The above copyright notice and this permission notice shall be
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
15 * included in all copies or substantial portions of the Software.
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
16 *
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
17 * 2. If the Software is incorporated into a build system that allows
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
18 * selection among a list of target devices, then similar target
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
19 * devices manufactured by PJRC.COM must be included in the list of
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
20 * target devices and selectable in the same manner.
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
21 *
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
26 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
27 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
29 * SOFTWARE.
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
30 */
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
31
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
32 // ----- Local Includes -----
70
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
33
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
34 #include "delay.h"
118
b61ca96b7c24 File and macro modifications for supporting Teensy 3.1
Jacob Alexander <haata@kiibohd.com>
parents: 70
diff changeset
35 #include "mk20dx.h"
70
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
36
192
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
37
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
38
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
39 // ----- Variables -----
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
40
70
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
41 // the systick interrupt is supposed to increment this at 1 kHz rate
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
42 volatile uint32_t systick_millis_count = 0;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
43
192
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
44
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
45
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
46 // ----- Functions -----
6ac92b8614c0 Fixing RAM calculator and reduced actual SRAM usage
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
47
70
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
48 void yield(void) {};
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
49
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
50 uint32_t micros(void)
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
51 {
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
52 uint32_t count, current, istatus;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
53
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
54 __disable_irq();
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
55 current = SYST_CVR;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
56 count = systick_millis_count;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
57 istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
58 __enable_irq();
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
59 if ((istatus & SCB_ICSR_PENDSTSET) && current > ((F_CPU / 1000) - 50)) count++;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
60 current = ((F_CPU / 1000) - 1) - current;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
61 return count * 1000 + current / (F_CPU / 1000000);
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
62 }
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
63
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
64 void delay(uint32_t ms)
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
65 {
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
66 uint32_t start = micros();
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
67
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
68 while (1) {
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
69 if ((micros() - start) >= 1000) {
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
70 ms--;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
71 if (ms == 0) break;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
72 start += 1000;
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
73 }
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
74 yield();
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
75 }
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
76 }
23600aaa5e15 Adding initial Teensy 3 support, compiles, but not fully functional yet.
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
77