2014-11-13 23:10:35 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2014-11-13 23:10:35 -04:00
|
|
|
|
|
|
|
#include "RCOutput_ZYNQ.h"
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2014-11-13 23:10:35 -04:00
|
|
|
#include <dirent.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/spi/spidev.h>
|
|
|
|
#include <signal.h>
|
2014-11-13 23:10:35 -04:00
|
|
|
#include <stdint.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-13 23:10:35 -04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-11-13 23:10:35 -04:00
|
|
|
using namespace Linux;
|
|
|
|
|
2016-07-29 20:26:07 -03:00
|
|
|
#define PWM_CHAN_COUNT 8
|
|
|
|
#define RCOUT_ZYNQ_PWM_BASE 0x43c00000
|
|
|
|
#define PWM_CMD_CONFIG 0 /* full configuration in one go */
|
|
|
|
#define PWM_CMD_ENABLE 1 /* enable a pwm */
|
|
|
|
#define PWM_CMD_DISABLE 2 /* disable a pwm */
|
|
|
|
#define PWM_CMD_MODIFY 3 /* modify a pwm */
|
|
|
|
#define PWM_CMD_SET 4 /* set a pwm output explicitly */
|
|
|
|
#define PWM_CMD_CLR 5 /* clr a pwm output explicitly */
|
|
|
|
#define PWM_CMD_TEST 6 /* various crap */
|
|
|
|
|
2014-11-13 23:10:35 -04:00
|
|
|
|
|
|
|
static void catch_sigbus(int sig)
|
|
|
|
{
|
2018-07-09 23:26:49 -03:00
|
|
|
AP_HAL::panic("RCOutput.cpp:SIGBUS error generated\n");
|
2014-11-13 23:10:35 -04:00
|
|
|
}
|
2015-12-02 11:14:20 -04:00
|
|
|
void RCOutput_ZYNQ::init()
|
2014-11-13 23:10:35 -04:00
|
|
|
{
|
|
|
|
uint32_t mem_fd;
|
|
|
|
signal(SIGBUS,catch_sigbus);
|
2016-10-30 10:22:29 -03:00
|
|
|
mem_fd = open("/dev/mem", O_RDWR|O_SYNC|O_CLOEXEC);
|
2014-11-13 23:10:35 -04:00
|
|
|
sharedMem_cmd = (struct pwm_cmd *) mmap(0, 0x1000, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_SHARED, mem_fd, RCOUT_ZYNQ_PWM_BASE);
|
|
|
|
close(mem_fd);
|
|
|
|
|
|
|
|
// 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_ZYNQ::set_freq(uint32_t chmask, uint16_t freq_hz) //LSB corresponds to CHAN_1
|
2014-11-13 23:10:35 -04:00
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
unsigned long tick=TICK_PER_S/(unsigned long)freq_hz;
|
|
|
|
|
|
|
|
for (i=0;i<PWM_CHAN_COUNT;i++) {
|
|
|
|
if (chmask & (1U<<i)) {
|
|
|
|
sharedMem_cmd->periodhi[i].period=tick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint16_t RCOutput_ZYNQ::get_freq(uint8_t ch)
|
2014-11-13 23:10:35 -04:00
|
|
|
{
|
2017-04-05 12:05:10 -03:00
|
|
|
if (ch >= PWM_CHAN_COUNT) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TICK_PER_S/sharedMem_cmd->periodhi[ch].period;
|
2014-11-13 23:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_ZYNQ::enable_ch(uint8_t ch)
|
2014-11-13 23:10:35 -04:00
|
|
|
{
|
|
|
|
// sharedMem_cmd->enmask |= 1U<<chan_pru_map[ch];
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_ZYNQ::disable_ch(uint8_t ch)
|
2014-11-13 23:10:35 -04:00
|
|
|
{
|
|
|
|
// sharedMem_cmd->enmask &= !(1U<<chan_pru_map[ch]);
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_ZYNQ::write(uint8_t ch, uint16_t period_us)
|
2014-11-13 23:10:35 -04:00
|
|
|
{
|
2017-04-05 12:05:10 -03:00
|
|
|
if (ch >= PWM_CHAN_COUNT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-11 21:19:16 -03:00
|
|
|
if (corked) {
|
|
|
|
pending[ch] = period_us;
|
|
|
|
pending_mask |= (1U << ch);
|
|
|
|
} else {
|
|
|
|
sharedMem_cmd->periodhi[ch].hi = TICK_PER_US*period_us;
|
|
|
|
}
|
2014-11-13 23:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint16_t RCOutput_ZYNQ::read(uint8_t ch)
|
2014-11-13 23:10:35 -04:00
|
|
|
{
|
2017-04-05 12:05:10 -03:00
|
|
|
if (ch >= PWM_CHAN_COUNT) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sharedMem_cmd->periodhi[ch].hi/TICK_PER_US;
|
2014-11-13 23:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCOutput_ZYNQ::read(uint16_t* period_us, uint8_t len)
|
2014-11-13 23:10:35 -04:00
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
if(len>PWM_CHAN_COUNT){
|
|
|
|
len = PWM_CHAN_COUNT;
|
|
|
|
}
|
|
|
|
for(i=0;i<len;i++){
|
|
|
|
period_us[i] = sharedMem_cmd->periodhi[i].hi/TICK_PER_US;
|
|
|
|
}
|
|
|
|
}
|
2016-10-11 21:19:16 -03:00
|
|
|
|
|
|
|
void RCOutput_ZYNQ::cork(void)
|
|
|
|
{
|
|
|
|
corked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RCOutput_ZYNQ::push(void)
|
|
|
|
{
|
2017-04-17 21:01:54 -03:00
|
|
|
if (!corked) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-11 21:19:16 -03:00
|
|
|
corked = false;
|
|
|
|
for (uint8_t i=0; i<MAX_ZYNQ_PWMS; i++) {
|
|
|
|
if (pending_mask & (1U << i)) {
|
|
|
|
write(i, pending[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pending_mask = 0;
|
|
|
|
}
|