Project

General

Profile

Revision 601

Added by Kevin Woo about 16 years ago

Apparently there were changes to bom.c and bom.h since the last time the
library was compiled.

delay_ms has been rewritten to use _delay_loop_2 which is a 4 cycle
implementation of a delay written in assembly. This is questionably more
accurate but does not use floating point to make delays anymore.

This implementation should be clock frequency independent, meaning that
the same function should be able to be used across different chips on
different clock frequencies.

Tested and works within acceptable skew (at 1s).

View differences:

trunk/code/lib/include/libdragonfly/bom.h
65 65
/** @brief Turns off the selected bom leds. Only works with BOM_ALL if using the original bom. **/
66 66
void bom_leds_off(int bit_field);
67 67

  
68
/** @brief (DEPRECATED) Gets and compares all bom values.  Returns the index to the highest value element. **/
68
/** @brief (DEPRECATED) Wrapper function. See bom_refresh and bom_get_max **/
69 69
int get_max_bom(void);
70
/** @brief (DEPRECATED) Turns on all bom leds. **/
70
/** @brief (DEPRECATED) Wrapper function. See bom_leds_on. **/
71 71
void bom_on(void);
72
/** @brief (DEPRECATED) Turns off all bom leds. **/
72
/** @brief (DEPRECATED) Wrapper function. See bom_leds_off. **/
73 73
void bom_off(void);
74 74

  
75 75
/** @} **/
trunk/code/lib/src/libdragonfly/time.c
60 60
#include <util/delay.h>
61 61
#include <time.h>
62 62
#include <serial.h>
63

  
64
/* Calculate how many cycles to delay for to get 1 ms. Based on F_CPU which should be defined by the makefile */
65
#ifdef F_CPU
66
#define WAIT_CYCLES ((F_CPU / 1000) / 10)
67
#else
68
#define WAIT_CYCLES (8000 / 10)
69
#endif
63 70

  
64 71
static volatile int _rtc_val = 0;
65 72
static volatile int _rtc_pulse = 0;
66 73
static volatile int _rtc_scale = 32;	//Defaults to 1 Second per pulse
67 74
static void (*_rtc_f)(void) = 0;
75

  
76

  
68 77

  
69 78
/**
70 79
 * @defgroup time Time
......
77 86

  
78 87
/**
79 88
 * Delays for the specified number of milliseconds.
80
 * The accuracy of this function is unknown.
89
 * It depends on F_CPU to be defined in order to calculate how many cycles
90
 * it should delay. If it is not defined, a default clock of 8MHz is assumed.
91
 * 
92
 * We use _delay_loop_2 which will run assembly instructions that should be
93
 * 4 cycles long. Optimizations must be enabled for this to be true.
94
 * That function is called to ensure around 1ms per execution. To generate
95
 * multiple ms we run a for loop of how many milliseconds are desired.
96
 *
97
 * The error should be just the skew on the oscillator as the formula to 
98
 * calculate delay cycles should always be a whole number. The is some skew
99
 * in practice though it is unavoidable. Delaying for less than 1s should make
100
 * the error negligable.
81 101
 *
82 102
 * @param ms the number of milliseconds to delay for
83 103
 **/
84
void delay_ms(int ms) {
85
  for(; ms > 15; ms-=15)
86
    _delay_ms(15);
87
  _delay_ms(ms);
88
}
104
void delay_ms(int ms) {
105
    for (; ms > 0; ms--) {
106
        _delay_loop_2(WAIT_CYCLES);
107
    }
108
}
109

  
89 110

  
90 111
/* 	Prescales defined in time.h. SECOND will give you 1 second.
91 112
	More scales are defined in the time.h file.
trunk/code/lib/src/libdragonfly/bom.c
154 154
 *
155 155
 * @return the bom value
156 156
 *
157
 * see bom_refresh
157
 * @see bom_refresh
158 158
 **/
159 159
int bom_get(int which) {
160 160
    return bom_val[which];
......
162 162

  
163 163
/** 
164 164
 * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
165
 *
165
 * Returns the direction of the maximum BOM reading,as an integer in the range 0-15. 0 indicates
166
 * to the robot's right, while the rest of the sensors are numbered counterclockwise.
167
 * 
166 168
 * @return index to the lowest (max) bom value element.  -1 if no value is lower than
167 169
 *    BOM_VALUE_THRESHOLD
168 170
 **/
......
231 233

  
232 234

  
233 235
/**
234
 * (DEPRECATED) Returns the direction of the maximum BOM reading,
235
 * as an integer in the range 0-15. 0 indicates to the
236
 * robot's right, while the rest of the sensors are
237
 * numbered counterclockwise. This is useful for determining
238
 * the direction of a robot flashing its BOM, of only one
239
 * robot is currently doing so. analog_init must be called
240
 * before this function can be used.
236
 * (DEPRECATED) Wrapper function for new BOM code.  Refreshes buffer and returns the max bom value.
241 237
 *
242 238
 * @return the direction of the maximum BOM reading
243 239
 *
244
 * @see analog_init
240
 * @see bom_refresh, bom_get_max
245 241
 **/
246 242
int get_max_bom(void) {
247 243
    bom_refresh(BOM_ALL);
trunk/code/projects/libdragonfly/time.c
60 60
#include <util/delay.h>
61 61
#include <time.h>
62 62
#include <serial.h>
63

  
64
/* Calculate how many cycles to delay for to get 1 ms. Based on F_CPU which should be defined by the makefile */
65
#ifdef F_CPU
66
#define WAIT_CYCLES ((F_CPU / 1000) / 10)
67
#else
68
#define WAIT_CYCLES (8000 / 10)
69
#endif
63 70

  
64 71
static volatile int _rtc_val = 0;
65 72
static volatile int _rtc_pulse = 0;
66 73
static volatile int _rtc_scale = 32;	//Defaults to 1 Second per pulse
67 74
static void (*_rtc_f)(void) = 0;
75

  
76

  
68 77

  
69 78
/**
70 79
 * @defgroup time Time
......
77 86

  
78 87
/**
79 88
 * Delays for the specified number of milliseconds.
80
 * The accuracy of this function is unknown.
89
 * It depends on F_CPU to be defined in order to calculate how many cycles
90
 * it should delay. If it is not defined, a default clock of 8MHz is assumed.
91
 * 
92
 * We use _delay_loop_2 which will run assembly instructions that should be
93
 * 4 cycles long. Optimizations must be enabled for this to be true.
94
 * That function is called to ensure around 1ms per execution. To generate
95
 * multiple ms we run a for loop of how many milliseconds are desired.
96
 *
97
 * The error should be just the skew on the oscillator as the formula to 
98
 * calculate delay cycles should always be a whole number. The is some skew
99
 * in practice though it is unavoidable. Delaying for less than 1s should make
100
 * the error negligable.
81 101
 *
82 102
 * @param ms the number of milliseconds to delay for
83 103
 **/
84
void delay_ms(int ms) {
85
  for(; ms > 15; ms-=15)
86
    _delay_ms(15);
87
  _delay_ms(ms);
88
}
104
void delay_ms(int ms) {
105
    for (; ms > 0; ms--) {
106
        _delay_loop_2(WAIT_CYCLES);
107
    }
108
}
109

  
89 110

  
90 111
/* 	Prescales defined in time.h. SECOND will give you 1 second.
91 112
	More scales are defined in the time.h file.

Also available in: Unified diff