📄 hostmot2.h
字号:
//// Copyright (C) 2007-2008 Sebastian Kuzminsky//// 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 2 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, write to the Free Software// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA//#ifndef RTAPI#error This is a realtime component only!#endif#include "rtapi.h"#include "hal.h"#include "hostmot2-lowlevel.h"#define HM2_VERSION "0.15"#define HM2_NAME "hm2"//// Note: PRINT() and PRINT_NO_LL() use rtapi_print(), all the others use rtapi_print_msg()//#define PRINT_NO_LL(fmt, args...) rtapi_print(HM2_NAME ": " fmt, ## args);#define ERR_NO_LL(fmt, args...) rtapi_print_msg(RTAPI_MSG_ERR, HM2_NAME ": " fmt, ## args);#define WARN_NO_LL(fmt, args...) rtapi_print_msg(RTAPI_MSG_WARN, HM2_NAME ": " fmt, ## args);#define INFO_NO_LL(fmt, args...) rtapi_print_msg(RTAPI_MSG_INFO, HM2_NAME ": " fmt, ## args);#define DBG_NO_LL(fmt, args...) rtapi_print_msg(RTAPI_MSG_DBG, HM2_NAME ": " fmt, ## args);#define PRINT(fmt, args...) rtapi_print(HM2_NAME "/%s: " fmt, hm2->llio->name, ## args);#define ERR(fmt, args...) rtapi_print_msg(RTAPI_MSG_ERR, HM2_NAME "/%s: " fmt, hm2->llio->name, ## args);#define WARN(fmt, args...) rtapi_print_msg(RTAPI_MSG_WARN, HM2_NAME "/%s: " fmt, hm2->llio->name, ## args);#define INFO(fmt, args...) rtapi_print_msg(RTAPI_MSG_INFO, HM2_NAME "/%s: " fmt, hm2->llio->name, ## args);#define DBG(fmt, args...) rtapi_print_msg(RTAPI_MSG_DBG, HM2_NAME "/%s: " fmt, hm2->llio->name, ## args);// // idrom addresses & constants// #define HM2_ADDR_IOCOOKIE (0x0100)#define HM2_IOCOOKIE (0x55AACAFE)#define HM2_ADDR_CONFIGNAME (0x0104)#define HM2_CONFIGNAME "HOSTMOT2"#define HM2_CONFIGNAME_LENGTH (8)#define HM2_ADDR_IDROM_OFFSET (0x010C)#define HM2_MAX_MODULE_DESCRIPTORS (48)#define HM2_MAX_PIN_DESCRIPTORS (128)// // Pin Descriptor constants// #define HM2_PIN_SOURCE_IS_PRIMARY (0x00000000)#define HM2_PIN_SOURCE_IS_SECONDARY (0x00000001)#define HM2_PIN_DIR_IS_INPUT (0x00000002)#define HM2_PIN_DIR_IS_OUTPUT (0x00000004)// // Module Descriptor constants// #define HM2_GTAG_WATCHDOG (2)#define HM2_GTAG_IOPORT (3)#define HM2_GTAG_ENCODER (4)#define HM2_GTAG_STEPGEN (5)#define HM2_GTAG_PWMGEN (6)#define HM2_GTAG_TRANSLATIONRAM (11)//// IDROM and MD structs//typedef struct { u32 idrom_type; u32 offset_to_modules; u32 offset_to_pin_desc; u8 board_name[8]; // ascii string, but not NULL terminated! u32 fpga_size; u32 fpga_pins; u32 io_ports; u32 io_width; u32 port_width; u32 clock_low; u32 clock_high; u32 instance_stride_0; u32 instance_stride_1; u32 register_stride_0; u32 register_stride_1;} hm2_idrom_t;typedef struct { u8 gtag; u8 version; u8 clock_tag; u32 clock_freq; // this one's not in the MD struct in the device, it's set from clock_tag and the idrom header for our convenience u8 instances; u16 base_address; u8 num_registers; u32 register_stride; u32 instance_stride; u32 multiple_registers;} hm2_module_descriptor_t;// // these structures keep track of the FPGA's I/O pins; and for I/O pins// used as GPIOs, keep track of the HAL state of the pins//// Pins and GPIOs are closely tied to the IOPort Function//typedef struct { struct { struct { hal_bit_t *in; hal_bit_t *in_not; hal_bit_t *out; } pin; struct { hal_bit_t is_output; hal_bit_t is_opendrain; hal_bit_t invert_output; } param; } hal;} hm2_gpio_instance_t;typedef struct { // these are from the Pin Descriptor in the HM2 IDROM u8 sec_pin; u8 sec_tag; u8 sec_unit; u8 primary_tag; // // below here is how the driver keeps track of each pin // // the actual function using this pin int gtag; // either HM2_PIN_DIR_IS_INPUT or HM2_PIN_DIR_IS_OUTPUT // if gtag != gpio, how the owning module instance configured it at load-time // if gtag == gpio, this gets copied from the .is_output parameter int direction; // if the driver decides to make this pin a gpio, it'll allocate the // instance struct to manage it, otherwise instance is NULL hm2_gpio_instance_t *instance;} hm2_pin_t;//// these structures translate between HostMot2 Functions and HAL objects////// encoders//#define HM2_ENCODER_FILTER (1<<11)#define HM2_ENCODER_COUNTER_MODE (1<<10)#define HM2_ENCODER_INDEX_MASK (1<<9)#define HM2_ENCODER_INDEX_MASK_POLARITY (1<<8)#define HM2_ENCODER_INDEX_JUSTONCE (1<<6)#define HM2_ENCODER_CLEAR_ON_INDEX (1<<5)#define HM2_ENCODER_LATCH_ON_INDEX (1<<4)#define HM2_ENCODER_INDEX_POLARITY (1<<3)#define HM2_ENCODER_CONTROL_MASK (0x0000ffff)typedef struct { s32 raw_count; s32 raw_timestamp; hal_float_t dt_s; // time between this datapoint and previous datapoint, in seconds hal_float_t velocity; // velocity computed for this datapoint} hm2_encoder_datapoint_t;typedef struct { struct { struct { hal_s32_t *rawcounts; // raw encoder counts hal_s32_t *count; // (rawcounts - zero_offset) hal_float_t *position; hal_float_t *velocity; hal_bit_t *reset; hal_bit_t *index_enable; } pin; struct { hal_float_t scale; hal_bit_t index_invert; hal_bit_t index_mask; hal_bit_t index_mask_invert; hal_bit_t counter_mode; hal_bit_t filter; hal_float_t vel_timeout; } param; } hal; s32 zero_offset; // *hal.pin.counts == (*hal.pin.rawcounts - zero_offset) u16 prev_reg_count; u16 prev_reg_timestamp; u32 prev_control; s32 tsc_num_rollovers; enum { HM2_ENCODER_STOPPED, HM2_ENCODER_MOVING } state; hm2_encoder_datapoint_t cur, prev;} hm2_encoder_instance_t;typedef struct { int num_instances; hm2_encoder_instance_t *instance; u32 stride; u32 clock_frequency; u8 version; // hw registers u32 counter_addr; u32 *counter_reg; u32 latch_control_addr; u32 *control_reg; u32 timestamp_div_addr; u32 timestamp_div_reg; // one register for the whole Function hal_float_t seconds_per_tsdiv_clock; u32 timestamp_count_addr; u32 *timestamp_count_reg; u32 prev_timestamp_count_reg; // gets set to 2 when TSC Register rollover is detected // gets decremented whenever rollover is *not* detected, but not smaller than 0 // this catches a rare corner case where the encoder updates between reading the encoder count/ts register and reading the tsc register int tsc_rollover_flag; u32 filter_rate_addr;} hm2_encoder_t;//// pwmgen// #define HM2_PWMGEN_OUTPUT_TYPE_PWM 1 // this is the same value that the software pwmgen component uses#define HM2_PWMGEN_OUTPUT_TYPE_UP_DOWN 2 // this is the same value that the software pwmgen component uses#define HM2_PWMGEN_OUTPUT_TYPE_PDM 3 // software pwmgen does not support pdm as an output type#define HM2_PWMGEN_OUTPUT_TYPE_PWM_SWAPPED 4 // software pwmgen does not support pwm/swapped output type because it doesnt need to typedef struct { struct { struct { hal_float_t *value; hal_bit_t *enable; } pin; struct { hal_float_t scale; hal_s32_t output_type; } param; } hal; // this keeps track of the output_type that we've told the FPGA, so we // know if we need to update it s32 written_output_type; // this keeps track of the enable bit for this instance that we've told // the FPGA, so we know if we need to update it s32 written_enable;} hm2_pwmgen_instance_t;// these hal params affect all pwmgen instancestypedef struct { struct { hal_u32_t pwm_frequency; hal_u32_t pdm_frequency; } param;} hm2_pwmgen_module_global_t;typedef struct { int num_instances; hm2_pwmgen_instance_t *instance; u32 clock_frequency; u8 version; // module-global HAL objects... hm2_pwmgen_module_global_t *hal; // these keep track of the most recent hal->param.p{d,w}m_frequency // that we've told the FPGA about, so we know if we need to update it u32 written_pwm_frequency; u32 written_pdm_frequency; // number of bits of resolution of the PWM signal (PDM is fixed at 12 bits) int pwm_bits; u32 pwm_value_addr; u32 *pwm_value_reg; u32 pwm_mode_addr; u32 *pwm_mode_reg; u32 pwmgen_master_rate_dds_addr; u32 pwmgen_master_rate_dds_reg; // one register for the whole Function u32 pdmgen_master_rate_dds_addr; u32 pdmgen_master_rate_dds_reg; // one register for the whole Function u32 enable_addr; u32 enable_reg; // one register for the whole Function} hm2_pwmgen_t;// // ioport// typedef struct { int num_instances;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -