Skip to content

Commit 142c3e6

Browse files
committed
cosmetic changes, new file 'abi.cpp'
1 parent 3e4941f commit 142c3e6

File tree

3 files changed

+123
-8
lines changed

3 files changed

+123
-8
lines changed

cores/xmega/abi.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright (c) 2014 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <stdlib.h>
20+
21+
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
22+
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
23+
24+
void __cxa_pure_virtual(void)
25+
{
26+
// We might want to write some diagnostics to uart in this case
27+
//std::terminate();
28+
abort();
29+
}
30+
31+
void __cxa_deleted_virtual(void)
32+
{
33+
// We might want to write some diagnostics to uart in this case
34+
//std::terminate();
35+
abort();
36+
}
37+
38+

cores/xmega/wiring.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,15 @@ register unsigned char c1;
435435
// enable DFLL auto-calibration of the 32Mhz internal oscillator
436436
// (it uses the reasonably precise 32.768KHz clock to do it)
437437

438-
OSC_DFLLCTRL = 0; // sect 6.10.7 - select 32.768KHz osc for everything, basically
439-
DFLLRC32M_CTRL = 1; // set the bit to enable DFLL calibration - section 6.11.1
438+
#ifdef OSC_RC32MCREF_gm /* if this is present, the enum for OSC_RC32MCREF_enum is also present */
439+
OSC_DFLLCTRL = OSC_RC32MCREF_RC32K_gc; // sect 6.10.7 - select 32.768KHz osc for everything, basically
440+
// use the enumeration/constant if it's present
441+
#else // OSC_RC32MCREF_gm not present
442+
OSC_DFLLCTRL = 0; // sect 6.10.7 - select 32.768KHz osc for everything, basically
443+
// for header files that do not have the enumeration/constant defined, this will have to do
444+
#endif // OSC_RC32MCREF_gm
445+
446+
DFLLRC32M_CTRL = DFLL_ENABLE_bm; // set the bit to enable DFLL calibration - section 6.11.1
440447
}
441448

442449
// I'll be using the 1.024khz clock (from the 32.768KHz clock) for the real-time counter

cores/xmega/wiring_pulse.c

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
3030
* to 3 minutes in length, but must be called at least a few dozen microseconds
3131
* before the start of the pulse. */
32+
33+
// NOTE: Arduino code now has a wiring_pulse.S that implements this function
34+
// as assembly code. For now, the C code will stay in the xmega version
35+
// but at some point the assembly version should be implemented.
36+
3237
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
3338
{
3439
// cache the port and bit of the pin in order to speed up the
@@ -45,19 +50,31 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
4550
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
4651

4752
// wait for any previous pulse to end
48-
while ((*portInputRegister(port) & bit) == stateMask)
49-
if (numloops++ == maxloops)
53+
while((*portInputRegister(port) & bit) == stateMask)
54+
{
55+
if(numloops++ == maxloops)
56+
{
5057
return 0;
58+
}
59+
}
5160

5261
// wait for the pulse to start
53-
while ((*portInputRegister(port) & bit) != stateMask)
54-
if (numloops++ == maxloops)
62+
while((*portInputRegister(port) & bit) != stateMask)
63+
{
64+
if(numloops++ == maxloops)
65+
{
5566
return 0;
67+
}
68+
}
5669

5770
// wait for the pulse to stop
58-
while ((*portInputRegister(port) & bit) == stateMask) {
59-
if (numloops++ == maxloops)
71+
while((*portInputRegister(port) & bit) == stateMask)
72+
{
73+
if(numloops++ == maxloops)
74+
{
6075
return 0;
76+
}
77+
6178
width++;
6279
}
6380

@@ -67,3 +84,56 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
6784
// the interrupt handlers.
6885
return clockCyclesToMicroseconds(width * 21 + 16);
6986
}
87+
88+
// Added 11/10/2018 to bring it up to latest Arduino code
89+
90+
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
91+
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
92+
* to 3 minutes in length, but must be called at least a few dozen microseconds
93+
* before the start of the pulse.
94+
*
95+
* ATTENTION:
96+
* this function relies on micros() so cannot be used in noInterrupt() context
97+
*/
98+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
99+
{
100+
// cache the port and bit of the pin in order to speed up the
101+
// pulse width measuring loop and achieve finer resolution. calling
102+
// digitalRead() instead yields much coarser resolution.
103+
uint8_t bit = digitalPinToBitMask(pin);
104+
uint8_t port = digitalPinToPort(pin);
105+
uint8_t stateMask = (state ? bit : 0);
106+
107+
unsigned long startMicros = micros();
108+
109+
// wait for any previous pulse to end
110+
while((*portInputRegister(port) & bit) == stateMask)
111+
{
112+
if(micros() - startMicros > timeout)
113+
{
114+
return 0;
115+
}
116+
}
117+
118+
// wait for the pulse to start
119+
while((*portInputRegister(port) & bit) != stateMask)
120+
{
121+
if(micros() - startMicros > timeout)
122+
{
123+
return 0;
124+
}
125+
}
126+
127+
unsigned long start = micros();
128+
// wait for the pulse to stop
129+
while((*portInputRegister(port) & bit) == stateMask)
130+
{
131+
if(micros() - startMicros > timeout)
132+
{
133+
return 0;
134+
}
135+
}
136+
137+
return micros() - start;
138+
}
139+

0 commit comments

Comments
 (0)