ardupilot/libraries/AP_HAL_AVR/Semaphores.cpp
Gustavo Jose de Sousa a80ae0cde3 AP_HAL_AVR: standardize inclusion of libaries headers
This commit changes the way libraries headers are included in source files:

 - If the header is in the same directory the source belongs to, so the
 notation '#include ""' is used with the path relative to the directory
 containing the source.

 - If the header is outside the directory containing the source, then we use
 the notation '#include <>' with the path relative to libraries folder.

Some of the advantages of such approach:

 - Only one search path for libraries headers.

 - OSs like Windows may have a better lookup time.
2015-08-19 20:42:35 +09:00

81 lines
1.9 KiB
C++

/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
#include <AP_HAL/AP_HAL.h>
#if (CONFIG_HAL_BOARD == HAL_BOARD_APM1 || CONFIG_HAL_BOARD == HAL_BOARD_APM2)
#include <avr/io.h>
#include <avr/interrupt.h>
#include <AP_HAL/AP_HAL.h>
#include "AP_HAL_AVR.h"
#include "Semaphores.h"
#include "Scheduler.h"
using namespace AP_HAL_AVR;
extern const AP_HAL::HAL& hal;
// Constructor
AVRSemaphore::AVRSemaphore() : _taken(false) {}
bool AVRSemaphore::give() {
if (!_taken) {
return false;
} else {
_taken = false;
return true;
}
}
bool AVRSemaphore::take(uint32_t timeout_ms) {
if (hal.scheduler->in_timerprocess()) {
hal.scheduler->panic(PSTR("PANIC: AVRSemaphore::take used from "
"inside timer process"));
return false; /* Never reached - panic does not return */
}
return _take_from_mainloop(timeout_ms);
}
bool AVRSemaphore::take_nonblocking() {
if (hal.scheduler->in_timerprocess()) {
return _take_nonblocking();
} else {
return _take_from_mainloop(0);
}
}
bool AVRSemaphore::_take_from_mainloop(uint32_t timeout_ms) {
/* Try to take immediately */
if (_take_nonblocking()) {
return true;
} else if (timeout_ms == 0) {
/* Return immediately if timeout is 0 */
return false;
}
uint16_t timeout_ticks = timeout_ms*10;
do {
/* Delay 1ms until we can successfully take, or we timed out */
hal.scheduler->delay_microseconds(100);
timeout_ticks--;
if (_take_nonblocking()) {
return true;
}
} while (timeout_ticks > 0);
return false;
}
bool AVRSemaphore::_take_nonblocking() {
bool result = false;
uint8_t sreg = SREG;
cli();
if (!_taken) {
_taken = true;
result = true;
}
SREG = sreg;
return result;
}
#endif // CONFIG_HAL_BOARD