2016-02-17 21:25:26 -04:00
|
|
|
#pragma once
|
2014-10-26 16:18:04 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL_Linux.h"
|
2014-10-26 16:18:04 -03:00
|
|
|
#include "RCInput.h"
|
2015-04-16 12:18:14 -03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <queue>
|
|
|
|
|
2023-06-01 09:38:09 -03:00
|
|
|
#include "Util_RPI.h"
|
|
|
|
|
2017-08-10 00:08:28 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BH
|
|
|
|
#define RCIN_RPI_CHN_NUM 8
|
2021-09-07 02:17:32 -03:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_OBAL_V1
|
|
|
|
#define RCIN_RPI_CHN_NUM 8
|
2017-08-10 00:08:28 -03:00
|
|
|
#else
|
|
|
|
#define RCIN_RPI_CHN_NUM 1
|
|
|
|
#endif
|
2015-04-16 12:18:14 -03:00
|
|
|
|
2016-07-29 16:14:02 -03:00
|
|
|
namespace Linux {
|
|
|
|
|
2015-04-16 12:18:14 -03:00
|
|
|
enum state_t{
|
2015-11-02 11:21:10 -04:00
|
|
|
RCIN_RPI_INITIAL_STATE = -1,
|
|
|
|
RCIN_RPI_ZERO_STATE = 0,
|
|
|
|
RCIN_RPI_ONE_STATE = 1
|
2015-04-16 12:18:14 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
//Memory table structure
|
|
|
|
typedef struct {
|
|
|
|
void **virt_pages;
|
|
|
|
void **phys_pages;
|
|
|
|
uint32_t page_count;
|
|
|
|
} memory_table_t;
|
|
|
|
|
|
|
|
|
|
|
|
//DMA control block structure
|
|
|
|
typedef struct {
|
|
|
|
uint32_t info, src, dst, length,
|
|
|
|
stride, next, pad[2];
|
|
|
|
} dma_cb_t;
|
|
|
|
|
2015-07-01 09:04:57 -03:00
|
|
|
class Memory_table {
|
2015-11-02 11:21:10 -04:00
|
|
|
// Allow RCInput_RPI access to private members of Memory_table
|
2016-07-29 16:14:02 -03:00
|
|
|
friend class RCInput_RPI;
|
|
|
|
|
2015-07-01 09:04:57 -03:00
|
|
|
private:
|
2015-04-16 12:18:14 -03:00
|
|
|
void** _virt_pages;
|
|
|
|
void** _phys_pages;
|
|
|
|
uint32_t _page_count;
|
|
|
|
|
2015-07-01 09:04:57 -03:00
|
|
|
public:
|
2015-04-16 12:18:14 -03:00
|
|
|
Memory_table();
|
2023-06-01 09:38:09 -03:00
|
|
|
Memory_table(const uint32_t, const LINUX_BOARD_TYPE);
|
2015-04-16 12:18:14 -03:00
|
|
|
~Memory_table();
|
|
|
|
|
2015-07-01 09:04:57 -03:00
|
|
|
//Get virtual address from the corresponding physical address from memory_table.
|
|
|
|
void* get_virt_addr(const uint32_t phys_addr) const;
|
2015-04-16 12:18:14 -03:00
|
|
|
|
|
|
|
// This function returns physical address with help of pointer, which is offset from the beginning of the buffer.
|
2015-07-01 09:04:57 -03:00
|
|
|
void* get_page(void **pages, const uint32_t addr) const;
|
2015-04-16 12:18:14 -03:00
|
|
|
|
2015-07-01 09:04:57 -03:00
|
|
|
// This function returns offset from the beginning of the buffer using (virtual) address in 'pages' and memory_table.
|
2022-12-15 16:14:22 -04:00
|
|
|
uint32_t get_offset(void **pages, const uint64_t addr) const;
|
2015-04-16 12:18:14 -03:00
|
|
|
|
|
|
|
//How many bytes are available for reading in circle buffer?
|
2015-07-01 09:04:57 -03:00
|
|
|
uint32_t bytes_available(const uint32_t read_addr, const uint32_t write_addr) const;
|
2015-04-16 12:18:14 -03:00
|
|
|
|
2015-07-01 09:04:57 -03:00
|
|
|
uint32_t get_page_count() const;
|
2015-04-16 12:18:14 -03:00
|
|
|
};
|
|
|
|
|
2014-10-26 16:18:04 -03:00
|
|
|
|
2016-07-29 16:14:02 -03:00
|
|
|
class RCInput_RPI : public RCInput
|
|
|
|
{
|
2015-07-01 09:04:57 -03:00
|
|
|
public:
|
2019-02-21 19:08:12 -04:00
|
|
|
void init() override;
|
|
|
|
void _timer_tick(void) override;
|
2015-11-02 11:21:10 -04:00
|
|
|
RCInput_RPI();
|
|
|
|
~RCInput_RPI();
|
2016-07-29 16:14:02 -03:00
|
|
|
|
2015-07-01 09:04:57 -03:00
|
|
|
private:
|
2015-04-16 12:18:14 -03:00
|
|
|
|
|
|
|
//Physical adresses of peripherals. Are different on different Raspberries.
|
|
|
|
uint32_t dma_base;
|
|
|
|
uint32_t clk_base;
|
|
|
|
uint32_t pcm_base;
|
|
|
|
|
|
|
|
//registers
|
|
|
|
static volatile uint32_t *pcm_reg;
|
|
|
|
static volatile uint32_t *clk_reg;
|
|
|
|
static volatile uint32_t *dma_reg;
|
|
|
|
|
|
|
|
Memory_table *circle_buffer;
|
|
|
|
Memory_table *con_blocks;
|
|
|
|
|
|
|
|
uint64_t curr_tick;
|
|
|
|
|
|
|
|
uint32_t curr_tick_inc;
|
|
|
|
uint32_t curr_pointer;
|
|
|
|
uint32_t curr_channel;
|
|
|
|
|
2017-08-10 00:08:28 -03:00
|
|
|
struct RcChannel {
|
|
|
|
RcChannel() :
|
|
|
|
prev_tick(0), delta_time(0),
|
|
|
|
width_s0(0), width_s1(0),
|
|
|
|
curr_signal(0), last_signal(0),
|
2024-09-21 21:37:45 -03:00
|
|
|
state(RCIN_RPI_INITIAL_STATE),
|
|
|
|
enable_pin(0)
|
2017-08-10 00:08:28 -03:00
|
|
|
{}
|
2015-04-16 12:18:14 -03:00
|
|
|
|
2017-08-10 00:08:28 -03:00
|
|
|
uint64_t prev_tick;
|
|
|
|
uint64_t delta_time;
|
2016-07-29 16:14:02 -03:00
|
|
|
|
2017-08-10 00:08:28 -03:00
|
|
|
uint16_t width_s0;
|
|
|
|
uint16_t width_s1;
|
|
|
|
|
|
|
|
uint8_t curr_signal;
|
|
|
|
uint8_t last_signal;
|
2016-11-18 12:23:28 -04:00
|
|
|
|
2017-08-10 00:08:28 -03:00
|
|
|
state_t state;
|
2016-07-29 16:14:02 -03:00
|
|
|
|
2017-08-10 00:08:28 -03:00
|
|
|
AP_HAL::DigitalSource *enable_pin;
|
|
|
|
} rc_channels[RCIN_RPI_CHN_NUM];
|
|
|
|
|
|
|
|
bool _initialized = false;
|
2023-06-01 09:38:09 -03:00
|
|
|
LINUX_BOARD_TYPE _version = LINUX_BOARD_TYPE::UNKNOWN_BOARD;
|
2021-04-04 06:39:20 -03:00
|
|
|
|
2015-07-01 09:04:57 -03:00
|
|
|
void init_dma_cb(dma_cb_t** cbp, uint32_t mode, uint32_t source, uint32_t dest, uint32_t length, uint32_t stride, uint32_t next_cb);
|
2015-04-16 12:18:14 -03:00
|
|
|
void* map_peripheral(uint32_t base, uint32_t len);
|
|
|
|
void init_registers();
|
|
|
|
void init_ctrl_data();
|
|
|
|
void init_PCM();
|
|
|
|
void init_DMA();
|
|
|
|
void init_buffer();
|
2015-07-09 11:07:53 -03:00
|
|
|
static void stop_dma();
|
|
|
|
static void termination_handler(int signum);
|
2015-04-16 12:18:14 -03:00
|
|
|
void set_sigaction();
|
2021-04-07 13:11:59 -03:00
|
|
|
void set_physical_addresses();
|
2016-10-26 17:41:47 -03:00
|
|
|
void teardown() override;
|
2014-10-26 16:18:04 -03:00
|
|
|
};
|
2016-07-29 16:14:02 -03:00
|
|
|
|
|
|
|
}
|