Skip to content

Commit 881d140

Browse files
committed
Got some routines for digital pins control from arduino source code.
Use arduino 1.0.2 source code.
1 parent dfccab8 commit 881d140

File tree

5 files changed

+549
-2
lines changed

5 files changed

+549
-2
lines changed

platform/arduino/Makefile.arduino

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
CONTIKI_TARGET_DIRS = . apps net loader
1+
CONTIKI_TARGET_DIRS = . apps net loader arduino arduino/variants/standard
22
CONTIKI_CORE = contiki-arduino-main
33
CONTIKI_TARGET_MAIN = ${CONTIKI_CORE}.o
44

55
CONTIKI_TARGET_SOURCEFILES += rs232.c cfs-eeprom.c eeprom.c random.c \
6-
mmem.c contiki-arduino-main.c slip.c
6+
mmem.c contiki-arduino-main.c slip.c \
7+
wiring_digital.c
78

89
CONTIKIAVR = $(CONTIKI)/cpu/avr
910
CONTIKIBOARD = .

platform/arduino/arduino/Arduino.h

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#ifndef Arduino_h
2+
#define Arduino_h
3+
4+
#include <avr/pgmspace.h>
5+
6+
#ifdef __cplusplus
7+
extern "C"{
8+
#endif
9+
10+
#define HIGH 0x1
11+
#define LOW 0x0
12+
13+
#define INPUT 0x0
14+
#define OUTPUT 0x1
15+
#define INPUT_PULLUP 0x2
16+
17+
18+
// Get the bit location within the hardware port of the given virtual pin.
19+
// This comes from the pins_*.c file for the active board configuration.
20+
21+
#define analogInPinToBit(P) (P)
22+
23+
// On the ATmega1280, the addresses of some of the port registers are
24+
// greater than 255, so we can't store them in uint8_t's.
25+
extern const uint16_t PROGMEM port_to_mode_PGM[];
26+
extern const uint16_t PROGMEM port_to_input_PGM[];
27+
extern const uint16_t PROGMEM port_to_output_PGM[];
28+
29+
extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
30+
// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
31+
extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
32+
extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
33+
34+
// Get the bit location within the hardware port of the given virtual pin.
35+
// This comes from the pins_*.c file for the active board configuration.
36+
//
37+
// These perform slightly better as macros compared to inline functions
38+
//
39+
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
40+
#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
41+
#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
42+
#define analogInPinToBit(P) (P)
43+
#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
44+
#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
45+
#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
46+
47+
#define NOT_A_PIN 0
48+
#define NOT_A_PORT 0
49+
50+
//#ifdef ARDUINO_MAIN
51+
#define PA 1
52+
#define PB 2
53+
#define PC 3
54+
#define PD 4
55+
#define PE 5
56+
#define PF 6
57+
#define PG 7
58+
#define PH 8
59+
#define PJ 10
60+
#define PK 11
61+
#define PL 12
62+
//#endif
63+
64+
#define NOT_ON_TIMER 0
65+
#define TIMER0A 1
66+
#define TIMER0B 2
67+
#define TIMER1A 3
68+
#define TIMER1B 4
69+
#define TIMER2 5
70+
#define TIMER2A 6
71+
#define TIMER2B 7
72+
73+
#define TIMER3A 8
74+
#define TIMER3B 9
75+
#define TIMER3C 10
76+
#define TIMER4A 11
77+
#define TIMER4B 12
78+
#define TIMER4C 13
79+
#define TIMER4D 14
80+
#define TIMER5A 15
81+
#define TIMER5B 16
82+
#define TIMER5C 17
83+
84+
85+
void pinMode(uint8_t pin, uint8_t mode);
86+
void digitalWrite(uint8_t pin, uint8_t val);
87+
int digitalRead(uint8_t pin);
88+
89+
#ifdef __cplusplus
90+
} // extern "C"
91+
#endif
92+
93+
94+
#include "pins_arduino.h"
95+
96+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
pins_arduino.h - Pin definition functions for Arduino
3+
Part of Arduino - http://www.arduino.cc/
4+
5+
Copyright (c) 2007 David A. Mellis
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
22+
$Id: wiring.h 249 2007-02-03 16:52:51Z mellis $
23+
*/
24+
25+
#ifndef Pins_Arduino_h
26+
#define Pins_Arduino_h
27+
28+
#include <avr/pgmspace.h>
29+
30+
#define NUM_DIGITAL_PINS 20
31+
#define NUM_ANALOG_INPUTS 6
32+
#define analogInputToDigitalPin(p) ((p < 6) ? (p) + 14 : -1)
33+
34+
#if defined(__AVR_ATmega8__)
35+
#define digitalPinHasPWM(p) ((p) == 9 || (p) == 10 || (p) == 11)
36+
#else
37+
#define digitalPinHasPWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11)
38+
#endif
39+
40+
static const uint8_t SS = 10;
41+
static const uint8_t MOSI = 11;
42+
static const uint8_t MISO = 12;
43+
static const uint8_t SCK = 13;
44+
45+
static const uint8_t SDA = 18;
46+
static const uint8_t SCL = 19;
47+
static const uint8_t LED_BUILTIN = 13;
48+
49+
static const uint8_t A0 = 14;
50+
static const uint8_t A1 = 15;
51+
static const uint8_t A2 = 16;
52+
static const uint8_t A3 = 17;
53+
static const uint8_t A4 = 18;
54+
static const uint8_t A5 = 19;
55+
static const uint8_t A6 = 20;
56+
static const uint8_t A7 = 21;
57+
58+
#define digitalPinToPCICR(p) (((p) >= 0 && (p) <= 21) ? (&PCICR) : ((uint8_t *)0))
59+
#define digitalPinToPCICRbit(p) (((p) <= 7) ? 2 : (((p) <= 13) ? 0 : 1))
60+
#define digitalPinToPCMSK(p) (((p) <= 7) ? (&PCMSK2) : (((p) <= 13) ? (&PCMSK0) : (((p) <= 21) ? (&PCMSK1) : ((uint8_t *)0))))
61+
#define digitalPinToPCMSKbit(p) (((p) <= 7) ? (p) : (((p) <= 13) ? ((p) - 8) : ((p) - 14)))
62+
63+
#ifdef ARDUINO_MAIN
64+
65+
// On the Arduino board, digital pins are also used
66+
// for the analog output (software PWM). Analog input
67+
// pins are a separate set.
68+
69+
// ATMEL ATMEGA8 & 168 / ARDUINO
70+
//
71+
// +-\/-+
72+
// PC6 1| |28 PC5 (AI 5)
73+
// (D 0) PD0 2| |27 PC4 (AI 4)
74+
// (D 1) PD1 3| |26 PC3 (AI 3)
75+
// (D 2) PD2 4| |25 PC2 (AI 2)
76+
// PWM+ (D 3) PD3 5| |24 PC1 (AI 1)
77+
// (D 4) PD4 6| |23 PC0 (AI 0)
78+
// VCC 7| |22 GND
79+
// GND 8| |21 AREF
80+
// PB6 9| |20 AVCC
81+
// PB7 10| |19 PB5 (D 13)
82+
// PWM+ (D 5) PD5 11| |18 PB4 (D 12)
83+
// PWM+ (D 6) PD6 12| |17 PB3 (D 11) PWM
84+
// (D 7) PD7 13| |16 PB2 (D 10) PWM
85+
// (D 8) PB0 14| |15 PB1 (D 9) PWM
86+
// +----+
87+
//
88+
// (PWM+ indicates the additional PWM pins on the ATmega168.)
89+
90+
// ATMEL ATMEGA1280 / ARDUINO
91+
//
92+
// 0-7 PE0-PE7 works
93+
// 8-13 PB0-PB5 works
94+
// 14-21 PA0-PA7 works
95+
// 22-29 PH0-PH7 works
96+
// 30-35 PG5-PG0 works
97+
// 36-43 PC7-PC0 works
98+
// 44-51 PJ7-PJ0 works
99+
// 52-59 PL7-PL0 works
100+
// 60-67 PD7-PD0 works
101+
// A0-A7 PF0-PF7
102+
// A8-A15 PK0-PK7
103+
104+
105+
// these arrays map port names (e.g. port B) to the
106+
// appropriate addresses for various functions (e.g. reading
107+
// and writing)
108+
const uint16_t PROGMEM port_to_mode_PGM[] = {
109+
NOT_A_PORT,
110+
NOT_A_PORT,
111+
(uint16_t) &DDRB,
112+
(uint16_t) &DDRC,
113+
(uint16_t) &DDRD,
114+
};
115+
116+
const uint16_t PROGMEM port_to_output_PGM[] = {
117+
NOT_A_PORT,
118+
NOT_A_PORT,
119+
(uint16_t) &PORTB,
120+
(uint16_t) &PORTC,
121+
(uint16_t) &PORTD,
122+
};
123+
124+
const uint16_t PROGMEM port_to_input_PGM[] = {
125+
NOT_A_PORT,
126+
NOT_A_PORT,
127+
(uint16_t) &PINB,
128+
(uint16_t) &PINC,
129+
(uint16_t) &PIND,
130+
};
131+
132+
const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
133+
PD, /* 0 */
134+
PD,
135+
PD,
136+
PD,
137+
PD,
138+
PD,
139+
PD,
140+
PD,
141+
PB, /* 8 */
142+
PB,
143+
PB,
144+
PB,
145+
PB,
146+
PB,
147+
PC, /* 14 */
148+
PC,
149+
PC,
150+
PC,
151+
PC,
152+
PC,
153+
};
154+
155+
const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
156+
_BV(0), /* 0, port D */
157+
_BV(1),
158+
_BV(2),
159+
_BV(3),
160+
_BV(4),
161+
_BV(5),
162+
_BV(6),
163+
_BV(7),
164+
_BV(0), /* 8, port B */
165+
_BV(1),
166+
_BV(2),
167+
_BV(3),
168+
_BV(4),
169+
_BV(5),
170+
_BV(0), /* 14, port C */
171+
_BV(1),
172+
_BV(2),
173+
_BV(3),
174+
_BV(4),
175+
_BV(5),
176+
};
177+
178+
const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
179+
NOT_ON_TIMER, /* 0 - port D */
180+
NOT_ON_TIMER,
181+
NOT_ON_TIMER,
182+
// on the ATmega168, digital pin 3 has hardware pwm
183+
#if defined(__AVR_ATmega8__)
184+
NOT_ON_TIMER,
185+
#else
186+
TIMER2B,
187+
#endif
188+
NOT_ON_TIMER,
189+
// on the ATmega168, digital pins 5 and 6 have hardware pwm
190+
#if defined(__AVR_ATmega8__)
191+
NOT_ON_TIMER,
192+
NOT_ON_TIMER,
193+
#else
194+
TIMER0B,
195+
TIMER0A,
196+
#endif
197+
NOT_ON_TIMER,
198+
NOT_ON_TIMER, /* 8 - port B */
199+
TIMER1A,
200+
TIMER1B,
201+
#if defined(__AVR_ATmega8__)
202+
TIMER2,
203+
#else
204+
TIMER2A,
205+
#endif
206+
NOT_ON_TIMER,
207+
NOT_ON_TIMER,
208+
NOT_ON_TIMER,
209+
NOT_ON_TIMER, /* 14 - port C */
210+
NOT_ON_TIMER,
211+
NOT_ON_TIMER,
212+
NOT_ON_TIMER,
213+
NOT_ON_TIMER,
214+
};
215+
216+
#endif
217+
218+
#endif

0 commit comments

Comments
 (0)