diff options
author | Marti Bolivar <mbolivar@mit.edu> | 2010-10-25 21:15:28 -0400 |
---|---|---|
committer | Marti Bolivar <mbolivar@mit.edu> | 2010-11-17 12:44:28 -0500 |
commit | 95783b750fda95f5f4c1fac00ab24da03b31b517 (patch) | |
tree | 2b0bf89c101aa58af5796fbe76c7ec98eebbb0a5 /wirish | |
parent | 3a9a119e9a8ce72c0e1b8fa4d3904bdf84ce355c (diff) | |
download | librambutan-95783b750fda95f5f4c1fac00ab24da03b31b517.tar.gz librambutan-95783b750fda95f5f4c1fac00ab24da03b31b517.zip |
arduino language reference nearing completion, properly CC-BY-SA 3.0 attributed
Diffstat (limited to 'wirish')
-rw-r--r-- | wirish/ext_interrupts.h | 8 | ||||
-rw-r--r-- | wirish/io.h | 14 | ||||
-rw-r--r-- | wirish/pwm.c | 2 | ||||
-rw-r--r-- | wirish/pwm.h | 11 | ||||
-rw-r--r-- | wirish/time.c | 1 | ||||
-rw-r--r-- | wirish/time.h | 36 | ||||
-rw-r--r-- | wirish/wirish_digital.c | 6 | ||||
-rw-r--r-- | wirish/wirish_math.h | 100 |
8 files changed, 154 insertions, 24 deletions
diff --git a/wirish/ext_interrupts.h b/wirish/ext_interrupts.h index 80e2e9e..d44978a 100644 --- a/wirish/ext_interrupts.h +++ b/wirish/ext_interrupts.h @@ -34,7 +34,7 @@ #define _EXT_INTERRUPTS_H_ /** - * The kind transition on an external pin which should trigger an + * The kind of transition on an external pin which should trigger an * interrupt. */ typedef enum ExtIntTriggerMode_ { @@ -56,13 +56,16 @@ extern "C"{ * * The interrupt will be triggered on a given transition on the pin, * as specified by the mode parameter. The handler runs in interrupt - * context. + * context. The new handler will replace whatever handler is + * currently registered for the pin, if any. * * @param pin Maple pin number * @param handler Function to run upon external interrupt trigger. + * The handler should take no arguments, and have void return type. * @param mode Type of transition to trigger on, e.g. falling, rising, etc. * * @sideeffect Registers a handler + * @see detachInterrupt() */ void attachInterrupt(uint8 pin, voidFuncPtr handler, ExtIntTriggerMode mode); @@ -70,6 +73,7 @@ void attachInterrupt(uint8 pin, voidFuncPtr handler, ExtIntTriggerMode mode); * @brief Disable any registered external interrupt. * @param pin Maple pin number * @sideeffect unregisters external interrupt handler + * @see attachInterrupt() */ void detachInterrupt(uint8 pin); diff --git a/wirish/io.h b/wirish/io.h index f82e414..f01efcb 100644 --- a/wirish/io.h +++ b/wirish/io.h @@ -148,13 +148,17 @@ uint32 digitalRead(uint8); /** * Read an analog value from pin. This function blocks during ADC - * conversion. The pin must have its mode set to INPUT_ANALOG. + * conversion, and has 12 bits of resolution. The pin must have its + * mode set to INPUT_ANALOG. Ignoring function call overhead, + * conversion time is 55.5 cycles. * - * @param pin Pin to read from. One of: 0-38 (pin numbers as labeled - * on silkscreen), or D0-D38 (symbols for same) - * @return ADC-converted voltage, in the range 0--4095, inclusive. + * @param pin Pin to read from. One of: 0, 1, 2, 3, 10, 11, 12, 13, + * 15, 16, 17, 18, 19, 20, 27, 28. + + * @return ADC-converted voltage, in the range 0--4095, inclusive + * (i.e. a 12-bit ADC conversion). + * @see pinMode() - * @see analogReference() */ uint32 analogRead(uint8 pin); diff --git a/wirish/pwm.c b/wirish/pwm.c index 0bf27aa..dc5cfab 100644 --- a/wirish/pwm.c +++ b/wirish/pwm.c @@ -46,5 +46,3 @@ void pwmWrite(uint8 pin, uint16 duty_cycle) { timer_pwm_write_ccr(ccr, duty_cycle); } - - diff --git a/wirish/pwm.h b/wirish/pwm.h index 6d0ddaf..d0bc9e0 100644 --- a/wirish/pwm.h +++ b/wirish/pwm.h @@ -35,15 +35,18 @@ extern "C"{ #endif +/** + * As a convenience, analogWrite is an alias of pwmWrite to ease + * porting Arduino code. However, period and duty will have to be + * recalibrated. + */ #define analogWrite pwmWrite /** - * Set the PWM duty. + * Set the PWM duty on the given pin. * * User code is expected to determine and honor the maximum value - * (based on the configured period). As a convenience, analogWrite is - * an alias of pwmWrite to ease porting Arduino code, though period - * and duty will have to be recalibrated + * (based on the configured period). */ void pwmWrite(uint8 pin, uint16 duty_cycle); diff --git a/wirish/time.c b/wirish/time.c index 3a48197..c0a0649 100644 --- a/wirish/time.c +++ b/wirish/time.c @@ -43,7 +43,6 @@ void delayMicroseconds(uint32 us) { /* fudge for function call overhead */ us--; - int x = 4; asm volatile(" mov r0, %[us] \n\t" "1: subs r0, #1 \n\t" " bhi 1b \n\t" diff --git a/wirish/time.h b/wirish/time.h index fad47a4..18aef9a 100644 --- a/wirish/time.h +++ b/wirish/time.h @@ -23,7 +23,8 @@ *****************************************************************************/ /** - * @brief Timing and delay functions. + * @file time.h + * @brief Timing and delay functions. */ #ifndef _TIME_H @@ -41,12 +42,20 @@ extern "C"{ extern volatile uint32 systick_timer_millis; -/* time in milliseconds since boot */ +/** + * Returns time (in milliseconds) since the beginning of program + * execution. On overflow, restarts at 0. + * @see micros() + */ static inline uint32 millis(void) { return systick_timer_millis; } -/* Time in microseconds since boot */ +/** + * Returns time (in microseconds) since the beginning of program + * execution. On overflow, restarts at 0. + * @see millis() + */ static inline uint32 micros(void) { uint32 ms; uint32 cycle_cnt; @@ -67,7 +76,28 @@ static inline uint32 micros(void) { return res; } +/** + * Delay for at least the given number of milliseconds. + * + * Interrupts, etc. may cause the actual number of milliseconds to + * exceed ms. However, this function will return no less than ms + * milliseconds from the time it is called. + * + * @param ms the number of milliseconds to delay. + * @see delayMicroseconds() + */ void delay(unsigned long ms); + +/** + * Delay for at least the given number of microseconds. + * + * Interrupts, etc. may cause the actual number of microseconds to + * exceed us. However, this function will return no less than us + * microseconds from the time it is called. + * + * @param us the number of microseconds to delay. + * @see delay() + */ void delayMicroseconds(uint32 us); #ifdef __cplusplus diff --git a/wirish/wirish_digital.c b/wirish/wirish_digital.c index aa22196..d7da81f 100644 --- a/wirish/wirish_digital.c +++ b/wirish/wirish_digital.c @@ -31,7 +31,7 @@ void pinMode(uint8 pin, WiringPinMode mode) { uint8 outputMode; - uint32 pwm = 0; + boolean pwm = false; if (pin >= NR_GPIO_PINS) { return; @@ -59,11 +59,11 @@ void pinMode(uint8 pin, WiringPinMode mode) { break; case PWM: outputMode = GPIO_MODE_AF_OUTPUT_PP; - pwm = 1; + pwm = true; break; case PWM_OPEN_DRAIN: outputMode = GPIO_MODE_AF_OUTPUT_OD; - pwm = 1; + pwm = true; break; default: ASSERT(0); diff --git a/wirish/wirish_math.h b/wirish/wirish_math.h index 4543c1b..9578eb4 100644 --- a/wirish/wirish_math.h +++ b/wirish/wirish_math.h @@ -22,6 +22,11 @@ * THE SOFTWARE. *****************************************************************************/ +/** + * @file wirish_math.h + * @brief Includes cmath; provides Arduino-compatible math routines. + */ + #ifndef _WIRING_MATH_H_ #define _WIRING_MATH_H_ @@ -29,10 +34,53 @@ #ifdef __cplusplus -void randomSeed(unsigned int); -long random(long); -long random(long, long); -long map(long, long, long, long, long); +/** + * @brief Initialize the pseudo-random number generator. + * @param seed the number used to initialize the seed; cannot be zero. + */ +void randomSeed(unsigned int seed); + +/** + * @brief Generate a pseudo-random number with upper bound. + * @param max An upper bound on the returned value, exclusive. + * @return A pseudo-random number in the range [0,max). + * @see randomSeed() + */ +long random(long max); + +/** + * @brief Generate a pseudo-random number with lower and upper bounds. + * @param min Lower bound on the returned value, inclusive. + * @param max Upper bound on the returned value, exclusive. + * @return A pseudo-random number in the range [min, max). + * @see randomSeed() + */ +long random(long min, long max); + +/** + * @brief Remap a number from one range to another. + * + * That is, a value equal to fromStart gets mapped to toStart, a value + * of fromEnd to toEnd, and other values are mapped proportionately. + * + * Does not constrain value to lie within [fromStart, fromEnd]. + * + * If a "start" value is larger than its corresponding "end", the + * ranges are reversed, so map(n, 1, 10, 10, 1) would reverse the + * range [1,10]. + * + * Negative numbers may appear as any argument. + * + * @param value the value to map. + * @param fromStart the beginning of the value's current range. + * @param fromEnd the end of the value's current range. + * @param toStart the beginning of the value's mapped range. + * @param toEnd the end of the value's mapped range. + * @return the mapped value. + */ +long map(long value, + long fromStart, long fromEnd, + long toStart, long toEnd); #define PI 3.1415926535897932384626433832795 #define HALF_PI 1.5707963267948966192313216916398 @@ -56,4 +104,48 @@ long map(long, long, long, long, long); #endif +/* Following are duplicate declarations (with Doxygen comments) for + * some of the math.h functions; this is for the convenience of the + * Sphinx docs. + */ + +/** + * Compute the cosine of an angle, in radians. + * @param x The radian measure of the angle. + * @return The cosine of x. This value will be between -1 and 1. + */ +double cos(double x); + +/** + * Compute the sine of an angle, in radians. + * @param x The radian measure of the angle. + * @return The sine of x. This value will be between -1 and 1. + */ +double sin(double x); + +/** + * Compute the tangent of an angle, in radians. + * @param x The radian measure of the angle. + * @return The tangent of x. There are no limits on the return value + * of this function. + */ +double tan(double x); + +/** + * Compute the square root of a number. + * @param x The number whose square root to find. This value cannot + * be negative. + * @return The square root of x. The return value is never negative. + */ +double sqrt(double x); + +/** + * Compute an exponentiation. + * @param x the base. This value cannot be zero if y <= 0. This value + * cannot be negative if y is not an integral value. + * @param y the exponent. + * @return x raised to the power y. + */ +double pow(double x, double y); + #endif |