2015-02-27 17:37:37 -04:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#include "RCOutput_AioPRU.h"
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2015-02-27 17:37:37 -04:00
|
|
|
#include <fcntl.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <signal.h>
|
2015-02-27 17:37:37 -04:00
|
|
|
#include <stdint.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-02-27 17:37:37 -04:00
|
|
|
#include <sys/mman.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-02-27 17:37:37 -04:00
|
|
|
|
2016-12-29 10:37:03 -04:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE
|
|
|
|
#include "../../Tools/Linux_HAL_Essentials/pru/aiopru/RcAioPRU_BBBLUE_bin.h"
|
2017-12-13 15:35:47 -04:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET
|
|
|
|
#include "../../Tools/Linux_HAL_Essentials/pru/aiopru/RcAioPRU_POCKET_bin.h"
|
2016-12-29 10:37:03 -04:00
|
|
|
#else
|
|
|
|
#include "../../Tools/Linux_HAL_Essentials/pru/aiopru/RcAioPRU_BBBMINI_bin.h"
|
|
|
|
#endif
|
2015-02-27 17:37:37 -04:00
|
|
|
|
|
|
|
using namespace Linux;
|
|
|
|
|
|
|
|
static void catch_sigbus(int sig)
|
|
|
|
{
|
2024-12-13 05:15:44 -04:00
|
|
|
AP_HAL::panic("RCOutputAioPRU.cpp:SIGBUS error generated");
|
2015-02-27 17:37:37 -04:00
|
|
|
}
|
2015-12-02 11:14:20 -04:00
|
|
|
void RCOutput_AioPRU::init()
|
2015-02-27 17:37:37 -04:00
|
|
|
{
|
2015-06-13 16:15:38 -03:00
|
|
|
uint32_t mem_fd;
|
2015-02-27 17:37:37 -04:00
|
|
|
uint32_t *iram;
|
|
|
|
uint32_t *ctrl;
|
|
|
|
|
|
|
|
signal(SIGBUS,catch_sigbus);
|
|
|
|
|
2016-10-30 10:22:29 -03:00
|
|
|
mem_fd = open("/dev/mem", O_RDWR|O_SYNC|O_CLOEXEC);
|
2015-02-27 17:37:37 -04:00
|
|
|
|
|
|
|
pwm = (struct pwm*) mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, RCOUT_PRUSS_RAM_BASE);
|
|
|
|
iram = (uint32_t*)mmap(0, 0x2000, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, RCOUT_PRUSS_IRAM_BASE);
|
|
|
|
ctrl = (uint32_t*)mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, RCOUT_PRUSS_CTRL_BASE);
|
|
|
|
|
|
|
|
close(mem_fd);
|
|
|
|
|
2018-04-11 17:37:41 -03:00
|
|
|
// Reset PRU
|
2015-02-27 17:37:37 -04:00
|
|
|
*ctrl = 0;
|
|
|
|
|
|
|
|
// Load firmware
|
2015-06-13 16:15:38 -03:00
|
|
|
memcpy(iram, PRUcode, sizeof(PRUcode));
|
2015-02-27 17:37:37 -04:00
|
|
|
|
2018-04-11 17:37:41 -03:00
|
|
|
// Start PRU
|
2015-11-12 15:56:50 -04:00
|
|
|
*ctrl |= 2;
|
2015-02-27 17:37:37 -04:00
|
|
|
|
|
|
|
// all outputs default to 50Hz, the top level vehicle code
|
|
|
|
// overrides this when necessary
|
|
|
|
set_freq(0xFFFFFFFF, 50);
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_AioPRU::set_freq(uint32_t chmask, uint16_t freq_hz)
|
2015-02-27 17:37:37 -04:00
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
uint32_t tick = TICK_PER_S / freq_hz;
|
|
|
|
|
|
|
|
for(i = 0; i < PWM_CHAN_COUNT; i++) {
|
|
|
|
if(chmask & (1U << i)) {
|
|
|
|
pwm->channel[i].time_t = tick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint16_t RCOutput_AioPRU::get_freq(uint8_t ch)
|
2015-02-27 17:37:37 -04:00
|
|
|
{
|
|
|
|
uint16_t ret = 0;
|
|
|
|
|
|
|
|
if(ch < PWM_CHAN_COUNT) {
|
|
|
|
ret = TICK_PER_S / pwm->channel[ch].time_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_AioPRU::enable_ch(uint8_t ch)
|
2015-02-27 17:37:37 -04:00
|
|
|
{
|
|
|
|
if(ch < PWM_CHAN_COUNT) {
|
|
|
|
pwm->channelenable |= 1U << ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_AioPRU::disable_ch(uint8_t ch)
|
2015-02-27 17:37:37 -04:00
|
|
|
{
|
|
|
|
if(ch < PWM_CHAN_COUNT) {
|
|
|
|
pwm->channelenable &= !(1U << ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_AioPRU::write(uint8_t ch, uint16_t period_us)
|
2015-02-27 17:37:37 -04:00
|
|
|
{
|
|
|
|
if(ch < PWM_CHAN_COUNT) {
|
2016-10-11 21:05:17 -03:00
|
|
|
if (corked) {
|
|
|
|
pending_mask |= (1U << ch);
|
|
|
|
pending[ch] = period_us;
|
|
|
|
} else {
|
|
|
|
pwm->channel[ch].time_high = TICK_PER_US * period_us;
|
|
|
|
}
|
2015-02-27 17:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint16_t RCOutput_AioPRU::read(uint8_t ch)
|
2015-02-27 17:37:37 -04:00
|
|
|
{
|
|
|
|
uint16_t ret = 0;
|
|
|
|
|
|
|
|
if(ch < PWM_CHAN_COUNT) {
|
|
|
|
ret = (pwm->channel[ch].time_high / TICK_PER_US);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_AioPRU::read(uint16_t* period_us, uint8_t len)
|
2015-02-27 17:37:37 -04:00
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
if(len > PWM_CHAN_COUNT) {
|
|
|
|
len = PWM_CHAN_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
period_us[i] = pwm->channel[i].time_high / TICK_PER_US;
|
|
|
|
}
|
|
|
|
}
|
2016-10-11 21:05:17 -03:00
|
|
|
|
|
|
|
void RCOutput_AioPRU::cork(void)
|
|
|
|
{
|
|
|
|
corked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RCOutput_AioPRU::push(void)
|
|
|
|
{
|
2017-04-17 21:01:54 -03:00
|
|
|
if (!corked) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-11 21:05:17 -03:00
|
|
|
corked = false;
|
|
|
|
for (uint8_t i=0; i<PWM_CHAN_COUNT; i++) {
|
|
|
|
if (pending_mask & (1U<<i)) {
|
|
|
|
write(i, pending[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pending_mask = 0;
|
|
|
|
}
|