2014-05-13 07:22:17 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-12-30 19:00:04 -04:00
|
|
|
|
2014-07-06 23:03:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
#include "RCOutput_PRU.h"
|
2014-05-13 15:31:36 -03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/spi/spidev.h>
|
2014-06-09 09:25:15 -03:00
|
|
|
#include <sys/mman.h>
|
2014-07-08 00:21:26 -03:00
|
|
|
#include <signal.h>
|
2014-05-13 15:31:36 -03:00
|
|
|
using namespace Linux;
|
2014-05-13 07:22:17 -03:00
|
|
|
|
2014-06-09 09:25:15 -03:00
|
|
|
|
2014-05-13 07:22:17 -03:00
|
|
|
#define PWM_CHAN_COUNT 12
|
|
|
|
|
2014-07-07 23:58:14 -03:00
|
|
|
static const uint8_t chan_pru_map[]= {10,8,11,9,7,6,5,4,3,2,1,0}; //chan_pru_map[CHANNEL_NUM] = PRU_REG_R30/31_NUM;
|
|
|
|
static const uint8_t pru_chan_map[]= {11,10,9,8,7,6,5,4,1,3,0,2}; //pru_chan_map[PRU_REG_R30/31_NUM] = CHANNEL_NUM;
|
2014-05-13 07:22:17 -03:00
|
|
|
|
2014-06-09 09:25:15 -03:00
|
|
|
static const AP_HAL::HAL& hal = AP_HAL_BOARD_DRIVER;
|
2014-07-08 00:21:26 -03:00
|
|
|
static void catch_sigbus(int sig)
|
|
|
|
{
|
|
|
|
hal.scheduler->panic("RCOutput.cpp:SIGBUS error gernerated\n");
|
|
|
|
}
|
2014-08-19 00:48:56 -03:00
|
|
|
void LinuxRCOutput_PRU::init(void* machtnicht)
|
2014-05-13 07:22:17 -03:00
|
|
|
{
|
2014-07-07 23:58:14 -03:00
|
|
|
uint32_t mem_fd;
|
2014-07-08 00:21:26 -03:00
|
|
|
signal(SIGBUS,catch_sigbus);
|
2014-06-09 09:25:15 -03:00
|
|
|
mem_fd = open("/dev/mem", O_RDWR|O_SYNC);
|
2014-08-17 23:35:22 -03:00
|
|
|
sharedMem_cmd = (struct pwm_cmd *) mmap(0, 0x1000, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_SHARED, mem_fd, RCOUT_PRUSS_SHAREDRAM_BASE);
|
2014-06-09 09:25:15 -03:00
|
|
|
close(mem_fd);
|
2014-08-18 00:02:50 -03:00
|
|
|
|
|
|
|
// all outputs default to 50Hz, the top level vehicle code
|
|
|
|
// overrides this when necessary
|
|
|
|
set_freq(0xFFFFFFFF, 50);
|
2014-05-13 07:22:17 -03:00
|
|
|
}
|
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
void LinuxRCOutput_PRU::set_freq(uint32_t chmask, uint16_t freq_hz) //LSB corresponds to CHAN_1
|
2014-05-13 07:22:17 -03:00
|
|
|
{
|
2014-07-08 00:21:26 -03:00
|
|
|
uint8_t i;
|
2014-06-27 03:01:59 -03:00
|
|
|
unsigned long tick=TICK_PER_S/(unsigned long)freq_hz;
|
2014-08-18 00:02:50 -03:00
|
|
|
|
|
|
|
for (i=0;i<PWM_CHAN_COUNT;i++) {
|
|
|
|
if (chmask & (1U<<i)) {
|
2014-06-27 03:01:59 -03:00
|
|
|
sharedMem_cmd->periodhi[chan_pru_map[i]][0]=tick;
|
2014-05-13 08:21:07 -03:00
|
|
|
}
|
|
|
|
}
|
2014-05-13 07:22:17 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
uint16_t LinuxRCOutput_PRU::get_freq(uint8_t ch)
|
2014-05-13 07:22:17 -03:00
|
|
|
{
|
2014-06-27 03:01:59 -03:00
|
|
|
return TICK_PER_S/sharedMem_cmd->periodhi[chan_pru_map[ch]][0];
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
void LinuxRCOutput_PRU::enable_ch(uint8_t ch)
|
2014-05-13 07:22:17 -03:00
|
|
|
{
|
2014-07-08 00:21:26 -03:00
|
|
|
sharedMem_cmd->enmask |= 1U<<chan_pru_map[ch];
|
2014-05-13 07:22:17 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
void LinuxRCOutput_PRU::disable_ch(uint8_t ch)
|
2014-05-13 07:22:17 -03:00
|
|
|
{
|
2014-07-08 00:21:26 -03:00
|
|
|
sharedMem_cmd->enmask &= !(1U<<chan_pru_map[ch]);
|
2014-05-13 07:22:17 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
void LinuxRCOutput_PRU::write(uint8_t ch, uint16_t period_us)
|
2014-05-13 07:22:17 -03:00
|
|
|
{
|
2014-06-27 03:01:59 -03:00
|
|
|
sharedMem_cmd->periodhi[chan_pru_map[ch]][1] = TICK_PER_US*period_us;
|
2014-05-13 07:22:17 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
void LinuxRCOutput_PRU::write(uint8_t ch, uint16_t* period_us, uint8_t len)
|
2014-05-13 07:22:17 -03:00
|
|
|
{
|
2014-07-07 23:58:14 -03:00
|
|
|
uint8_t i;
|
2014-07-08 00:21:26 -03:00
|
|
|
if(len>PWM_CHAN_COUNT){
|
|
|
|
len = PWM_CHAN_COUNT;
|
|
|
|
}
|
2014-05-13 08:21:07 -03:00
|
|
|
for(i=0;i<len;i++){
|
2014-06-09 09:25:15 -03:00
|
|
|
write(ch+i,period_us[i]);
|
2014-05-13 08:21:07 -03:00
|
|
|
}
|
2014-05-13 07:22:17 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
uint16_t LinuxRCOutput_PRU::read(uint8_t ch)
|
2014-06-09 09:25:15 -03:00
|
|
|
{
|
2014-07-07 23:58:14 -03:00
|
|
|
return (sharedMem_cmd->hilo_read[chan_pru_map[ch]][1]/TICK_PER_US);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2014-08-19 00:48:56 -03:00
|
|
|
void LinuxRCOutput_PRU::read(uint16_t* period_us, uint8_t len)
|
2014-05-13 07:22:17 -03:00
|
|
|
{
|
2014-07-07 23:58:14 -03:00
|
|
|
uint8_t i;
|
2014-07-08 00:21:26 -03:00
|
|
|
if(len>PWM_CHAN_COUNT){
|
|
|
|
len = PWM_CHAN_COUNT;
|
|
|
|
}
|
2014-05-13 08:21:07 -03:00
|
|
|
for(i=0;i<len;i++){
|
2014-07-07 23:58:14 -03:00
|
|
|
period_us[i] = sharedMem_cmd->hilo_read[chan_pru_map[i]][1]/TICK_PER_US;
|
2014-05-13 08:21:07 -03:00
|
|
|
}
|
2014-05-13 07:22:17 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2014-05-13 15:31:36 -03:00
|
|
|
#endif
|