-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassert.c
More file actions
executable file
·148 lines (123 loc) · 3.32 KB
/
assert.c
File metadata and controls
executable file
·148 lines (123 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "kinetis.h"
#include "core_pins.h"
#include "assert.h"
#define LED (1<<5)
#define CS (1<<1)
#define CLK (1<<0)
#define MOSI (1<<0)
#define SET_LED(x) do{ ((x) ? (GPIOC_PSOR = LED) : (GPIOC_PCOR = LED )); spinDelayUs(1); } while(0)
#define SET_CS(x) do{ ((x) ? (GPIOC_PSOR = CS) : (GPIOC_PCOR = CS )); spinDelayUs(1); } while(0)
#define SET_CLK(x) do{ ((x) ? (GPIOC_PSOR = CLK) : (GPIOC_PCOR = CLK )); spinDelayUs(1); } while(0)
#define SET_MOSI(x) do{ ((x) ? (GPIOC_PSOR = MOSI): (GPIOC_PCOR = MOSI)); spinDelayUs(1); } while(0)
int isIntContext(void)
{
int res = 0;
__asm ("mrs r0, iapsr\n\t"
"mov %[result], r0"
: [result]"=r" (res) /* 'result' is output */
: /* No input. */
: "r0" /* r0 was clobbered */
);
return (res & 0x000001ff);
}
void
spinDelayUs(uint32_t us)
{
while(us--)
{
__asm(" nop\n"
" nop\n"
" nop\n"
" nop\n"
" nop\n"
" nop\n"
" nop\n"
" nop\n"
" nop\n"
" nop\n"
" nop\n"
" nop\n");
}
}
void
spinDelayMs(uint32_t ms)
{
while(ms--)
{
spinDelayUs(1000);
}
}
#define THROTTLE spinDelayUs(10)
uint8_t transfer(uint8_t out)
{
uint8_t count, in = 0;
SET_CLK(0);
THROTTLE;
for (count = 0; count < 8; count++)
{
in <<= 1;
SET_MOSI(out & 0x80); // set Output bit
THROTTLE;
SET_CLK(1); // Clock Rising Edge
THROTTLE;
in += 0; // normally read MISO here.
SET_CLK(0); // Clock Rising Edge
THROTTLE;
out <<= 1; // shift read bit
}
SET_MOSI(0);
THROTTLE;
return (in);
}
void
_assert_failed (const char *assertion, const char *file, unsigned int line)
{
// Not alot that we can do at the current time so simply blink the
// LED rapidly
//
// Normally an IO would display:
// Assertion failed: expression, file filename, line line number
if ( ! isIntContext() )
{
// disable interrupts and task switching
}
// Denergize any outputs
//
// Enable the GPIO port that is used for the on-board LED.
//
//
// Do a dummy read to insert a few cycles after enabling the peripheral.
//
//
// Enable the GPIO pin for the LED (PF3). Set the direction as output, and
// enable the GPIO pin for digital function.
//
// turn off all the LED's
//
PORTC_PCR5 = (1<<6) | (1<<8);
PORTD_PCR1 = (1<<6) | (1<<8);
PORTC_PCR0 = (1<<6) | (1<<8);
PORTB_PCR0 = (1<<6) | (1<<8);
GPIOB_PDDR = MOSI;
GPIOB_PDIR = 0;
GPIOD_PDDR = CS;
GPIOD_PDIR = 0;
GPIOC_PDDR = LED | CLK;
GPIOC_PDIR = 0;
SET_CLK(0);
SET_CS(1);
//
// Loop forever.
//
while (1)
{
SET_LED(1);
spinDelayMs(50);
SET_LED(0);
spinDelayMs(50);
SET_CS(0);
transfer(0xbc);
SET_CS(1);
spinDelayMs(50);
}
}