RTduino Source Code Reference Manual
Arduino.h
Go to the documentation of this file.
1 
31 /*
32  Arduino.h - Main include file for the Arduino SDK
33  Copyright (c) 2005-2013 Arduino Team. All right reserved.
34 
35  This library is free software; you can redistribute it and/or
36  modify it under the terms of the GNU Lesser General Public
37  License as published by the Free Software Foundation; either
38  version 2.1 of the License, or (at your option) any later version.
39 
40  This library is distributed in the hope that it will be useful,
41  but WITHOUT ANY WARRANTY; without even the implied warranty of
42  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43  Lesser General Public License for more details.
44 
45  You should have received a copy of the GNU Lesser General Public
46  License along with this library; if not, write to the Free Software
47  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
48 */
49 
50 #ifndef __ARDUINO_CORE_H__
51 #define __ARDUINO_CORE_H__
52 
53 #include <rtdevice.h>
54 #include <rthw.h>
55 #include <stdlib.h>
56 #include <stdint.h>
57 #include <stdbool.h>
58 #include <math.h>
59 #include "binary.h"
60 #include "avr/io.h"
61 #include "avr/stdlib.h"
62 #include "avr/pgmspace.h"
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 #if RT_VER_NUM < 0x50000
69 #define rt_align(x) ALIGN(x)
70 #define rt_weak RT_WEAK
71 #endif /* RT_VER_NUM < 0x50000 */
72 
73 #ifndef RTDUINO_TINY_MODE
74 #include "pins_arduino.h"
75 #endif /* RTDUINO_TINY_MODE */
76 
77 #if RT_VER_NUM < 0x40101
78 #error "The minimum version requirement of RT-Thread is 4.1.1"
79 #endif /* RT_VER_NUM < 0x40101 */
80 
137 typedef struct
138 {
139  uint8_t arduino_pin;
140  rt_base_t rt_pin;
141  const char* device_name;
142  rt_int8_t channel;
143 } pin_map_t;
144 
150 typedef unsigned int word;
151 
157 typedef bool boolean;
158 
164 typedef uint8_t byte;
165 
170 #define LOW 0x0
171 
176 #define HIGH 0x1
177 
182 #define CHANGE 0x2
183 
188 #define FALLING 0x3
189 
194 #define RISING 0x4
195 
200 #define INPUT 0x0
201 
206 #define OUTPUT 0x1
207 
212 #define INPUT_PULLUP 0x2
213 
219 #define INPUT_FLOATING INPUT
220 
226 #define INPUT_PULLDOWN 0x3
227 
233 #define OUTPUT_OPEN_DRAIN 0x4
234 
239 #define PI 3.1415926535897932384626433832795
240 
245 #define HALF_PI 1.5707963267948966192313216916398
246 
251 #define TWO_PI 6.283185307179586476925286766559
252 
257 #define DEG_TO_RAD 0.017453292519943295769236907684886
258 
263 #define RAD_TO_DEG 57.295779513082320876798154814105
264 
269 #define EULER 2.718281828459045235360287471352
270 
271 #define SERIAL 0x0
272 #define DISPLAY 0x1
273 
278 #define LSBFIRST 0
279 
284 #define MSBFIRST 1
285 
286 /* math operations */
287 /* adb() in stdlib.h and round() in math.h */
288 /* min/max potential macro substitution problem */
289 #undef min
290 #undef max
298 #define min(a, b) ((a)<(b)?(a):(b))
299 
307 #define max(a, b) ((a)>(b)?(a):(b))
308 
317 #define constrain(amt, low, high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
318 
325 #define radians(deg) ((deg)*DEG_TO_RAD)
326 
333 #define degrees(rad) ((rad)*RAD_TO_DEG)
334 
341 #define sq(x) ((x)*(x)) /* x^2 */
342 
347 #define _NOP() do { __asm__ volatile ("nop"); } while (0)
348 
349 #ifdef F_CPU
354 #define clockCyclesPerMicrosecond() (F_CPU / 1000000L)
355 
361 #define clockCyclesToMicroseconds(a) ((a) / clockCyclesPerMicrosecond())
362 
368 #define microsecondsToClockCycles(a) ((a) * clockCyclesPerMicrosecond())
369 #elif !defined(RTDUINO_TINY_MODE)
373 #warning "Please define F_CPU in pins_arduino.h"
374 #endif /* F_CPU */
375 
383 #define bit(b) (1UL << (b))
384 
395 #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
396 
407 #define bitSet(value, bit) ((value) |= (1UL << (bit)))
416 #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
425 #define bitToggle(value, bit) ((value) ^= (1UL << (bit)))
435 #define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
436 
444 #define lowByte(w) ((uint8_t) ((w) & 0xff))
445 
453 #define highByte(w) ((uint8_t) ((w) >> 8))
454 
465 void pinMode(uint8_t pin, uint8_t mode);
466 
474 void digitalWrite(uint8_t pin, uint8_t val);
475 
483 int digitalRead(uint8_t pin);
484 
492 int analogRead(uint8_t pin);
493 
501 void analogWrite(uint8_t pin, int val);
502 
509 void analogReference(uint8_t mode);
510 
517 void analogReadResolution(uint8_t bits);
518 
525 void analogWriteResolution(uint8_t bits);
526 
533 void analogWriteFrequency(uint32_t frequency);
534 
558 void yield(void);
559 
566 void delay(unsigned long ms);
567 
574 void delayMicroseconds(unsigned int us);
575 
582 unsigned long millis(void);
583 
590 unsigned long micros(void);
591 
606 unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
607 
622 unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
623 
633 void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
634 
644 uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
645 
651 void interrupts(void);
652 
658 void noInterrupts(void);
659 
667 uint8_t digitalPinToInterrupt(uint8_t pin);
668 
677 void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode);
678 
685 void detachInterrupt(uint8_t interruptNum);
686 
693 void randomSeed(unsigned long seed);
694 
706 long map(long x, long in_min, long in_max, long out_min, long out_max);
707 
714 void setup(void);
715 
722 void loop(void);
723 
730 void initVariant(void);
731 
732 #ifdef RTDUINO_USING_SPI
739 void switchToSPI(const char *bus_name);
740 #endif /* RTDUINO_USING_SPI */
741 
742 #ifdef __cplusplus
743 } /* extern "C" { */
744 #endif
745 
746 #ifdef __cplusplus
747 
755 long random(long max);
756 
765 long random(long min, long max);
766 
790 template <typename T>
791 T map(T x, T in_min, T in_max, T out_min, T out_max)
792 {
793  return (T)(((double)(x - in_min) * (double)(out_max - out_min)) / (double)((in_max - in_min) + out_min));
794 }
795 
803 uint16_t makeWord(uint16_t w);
804 
813 uint16_t makeWord(byte h, byte l);
814 
820 #define word(...) makeWord(__VA_ARGS__)
821 
831 unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
832 
842 unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
843 
852 void tone(uint8_t pin, unsigned int frequency, unsigned long duration = 0);
853 
860 void noTone(uint8_t pin);
861 
862 #include "WCharacter.h"
863 #include "WString.h"
864 #include "HardwareSerial.h"
865 #ifdef RTDUINO_USING_USBSERIAL
866 #include "USBSerial.h"
867 #endif /* RTDUINO_USING_USBSERIAL */
868 
869 #endif /* __cplusplus */
870 
871 #endif /* __ARDUINO_CORE_H__ */
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
Measures the duration of a pulse (in us) on the specified pin.
Definition: wiring_pulse.c:59
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
Shifts in data on a specified data pin using a specified clock pin and bit order.
Definition: wiring_shift.c:37
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
Measures the duration of a pulse (in us) on the specified pin.
Definition: wiring_pulse.c:108
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
Sends out a byte of data on the specified data pin using the specified clock and bit order.
Definition: wiring_shift.c:52
void analogReadResolution(uint8_t bits)
Sets the number of bits of resolution for the analogRead function.
Definition: wiring_analog.c:47
void analogWriteResolution(uint8_t bits)
Sets the number of bits of resolution for the analogWrite function.
Definition: wiring_analog.c:57
void analogWriteFrequency(uint32_t frequency)
Sets the frequency of the PWM on a pin.
Definition: wiring_analog.c:67
int analogRead(uint8_t pin)
Reads the analog input on a specified analog pin.
Definition: wiring_analog.c:144
void analogReference(uint8_t mode)
Configures the reference voltage used for analog input (e.g., external, internal, etc....
Definition: wiring_analog.c:35
void analogWrite(uint8_t pin, int val)
Writes an analog value (PWM wave) to a pin.
Definition: wiring_analog.c:83
unsigned int word
Represents a 16-bit unsigned integer.
Definition: Arduino.h:150
bool boolean
Represents a boolean value (true or false).
Definition: Arduino.h:157
uint8_t byte
Represents an 8-bit unsigned integer.
Definition: Arduino.h:164
void digitalWrite(uint8_t pin, uint8_t val)
Writes a HIGH or a LOW value to a digital pin.
int digitalRead(uint8_t pin)
Reads the value from a specified digital pin, either HIGH or LOW.
void pinMode(uint8_t pin, uint8_t mode)
Configures the specified pin to behave either as an input or an output.
void interrupts(void)
Enables interrupts, allowing the execution of interrupt service routines.
Definition: WInterrupts.c:22
void detachInterrupt(uint8_t interruptNum)
Detaches an interrupt service routine from a specified interrupt.
void noInterrupts(void)
Disables interrupts, preventing the execution of interrupt service routines.
Definition: WInterrupts.c:31
void attachInterrupt(uint8_t interruptNum, void(*userFunc)(void), int mode)
Attaches an interrupt service routine to a specified interrupt.
uint8_t digitalPinToInterrupt(uint8_t pin)
Converts a digital pin number to the corresponding interrupt number.
long map(long x, long in_min, long in_max, long out_min, long out_max)
Maps a value from one range to another.
Definition: WMath_c.c:44
#define min(a, b)
Returns the smaller of two values.
Definition: Arduino.h:298
#define max(a, b)
Returns the larger of two values.
Definition: Arduino.h:307
void randomSeed(unsigned long seed)
Initializes the pseudo-random number generator.
Definition: WMath_c.c:36
void loop(void)
Main loop function for the Arduino program.
void initVariant(void)
Variant-specific initialization function.
Definition: RTduino.cpp:142
void setup(void)
Initialization function for the Arduino environment.
unsigned long millis(void)
Returns the number of milliseconds since the Arduino board began running the current program.
Definition: wiring.c:25
void delayMicroseconds(unsigned int us)
Delays the program for the specified number of microseconds.
Definition: wiring.c:64
void delay(unsigned long ms)
Delays the program for the specified number of milliseconds.
Definition: wiring.c:59
unsigned long micros(void)
Returns the number of microseconds since the Arduino board began running the current program.
Definition: wiring.c:30
void yield(void)
Pauses the execution of the current task, allowing other tasks to run.
Definition: wiring.c:69
RTduino pin map structure.
Definition: Arduino.h:138
uint8_t arduino_pin
Definition: Arduino.h:139
rt_base_t rt_pin
Definition: Arduino.h:140
const char * device_name
Definition: Arduino.h:141
rt_int8_t channel
Definition: Arduino.h:142