⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hal_motenc.c

📁 Source code for an Numeric Cmputer
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************** * * Copyright (C) 2005 Peter G. Vavaroutsos <pete AT vavaroutsos DOT com> * License: GPL Version 2 * * $RCSfile: hal_motenc.c,v $ * $Author: jmkasunich $ * $Locker:  $ * $Revision: 1.12 $ * $State: Exp $ * $Date: 2006/01/20 02:31:32 $ * * This is the driver for the Vital Systems MOTENC-100 board. * The board includes 8 quadrature decoders, 8 analog inputs, * 8 analog outputs, 68 digital inputs, 32 digital outputs, * programable timer interrupts, a watch dog timer, and a hardware * E-STOP circuit. * * Installation of the driver (realtime only): * * insmod hal_motenc * * * The following items are exported to the HAL. <boardId> is read * from the jumper settings on the MOTENC-100 board and is formated * as "%d". <channel> is formated as "%02d". * * Encoders: *   Parameters: *	float	motenc.<boardId>.enc-<channel>-scale * *   Pins: *	s32	motenc.<boardId>.enc-<channel>-count *	float	motenc.<boardId>.enc-<channel>-position *	bit	motenc.<boardId>.enc-<channel>-index *	bit	motenc.<boardId>.enc-<channel>-idx-latch *	bit	motenc.<boardId>.enc-<channel>-latch-index *	bit	motenc.<boardId>.enc-<channel>-reset-count * *   Functions: *	void    motenc.<boardId>.encoder-read * * * DACs: *   Parameters: *	float	motenc.<boardId>.dac-<channel>-offset *	float	motenc.<boardId>.dac-<channel>-gain * *   Pins: *	float	motenc.<boardId>.dac-<channel>-value * *   Functions: *	void    motenc.<boardId>.dac-write * * * ADC: *   Parameters: *	float	motenc.<boardId>.adc-<channel>-offset *	float	motenc.<boardId>.adc-<channel>-gain * *   Pins: *	float	motenc.<boardId>.adc-<channel>-value * *   Functions: *	void    motenc.<boardId>.adc-read * * * Digital In: *   Pins: *	bit	motenc.<boardId>.pin-<channel>-in *	bit	motenc.<boardId>.pin-<channel>-in-not * *   Functions: *	void    motenc.<boardId>.digital-in-read * * * Digital Out: *   Parameters: *	bit	motenc.<boardId>.pin-<channel>-out-invert * *   Pins: *	bit	motenc.<boardId>.pin-<channel>-out * *   Functions: *	void    motenc.<boardId>.digital-out-write * * * Miscellaneous: *   Parameters: *	u32	motenc.<boardId>.watchdog-control *		    MOTENC_WATCHDOG_CTL_8MS	    0x00000000 *		    MOTENC_WATCHDOG_CTL_16MS	    0x00000001 *		    MOTENC_WATCHDOG_CTL_ENABLE	    0x00000004 *		    MOTENC_WATCHDOG_CTL_AUTO_RESET  0x00000010 // Reset by DAC writes. * *   Pins: *	bit	motenc.<boardId>.estop-in *	bit	motenc.<boardId>.estop-in-not *	bit	motenc.<boardId>.watchdog-reset * *   Functions: *	void    motenc.<boardId>.misc-update * ****************************************************************************** * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2.1 of the GNU General * Public License as published by the Free Software Foundation. * This library 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 library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111 USA * * THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR * ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE * TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of * harming persons must have provisions for completely removing power * from all motors, etc, before persons enter any danger area.  All * machinery must be designed to comply with local and national safety * codes, and the authors of this software can not, and do not, take * any responsibility for such compliance. * * This code was written as part of the EMC HAL project.  For more * information, go to www.linuxcnc.org. * ******************************************************************************/#ifndef RTAPI#error This is a realtime component only!#endif#include <linux/pci.h>#include "rtapi.h"			// RTAPI realtime OS API.#include "rtapi_app.h"			// RTAPI realtime module decls.#include "hal.h"			// HAL public API decls.#include "motenc.h"			// Hardware dependent defines.#ifndef MODULE#define MODULE#endif#ifdef MODULE// Module information.MODULE_AUTHOR("Pete Vavaroutsos");MODULE_DESCRIPTION("Driver for Vital Systems MOTENC-100 for EMC HAL");#ifdef MODULE_LICENSEMODULE_LICENSE("GPL");#endif // MODULE_LICENSEstatic long				period = 0;// Thread period (0 = no thread).MODULE_PARM(period, "l");MODULE_PARM_DESC(period, "thread period (nsecs)");#endif // MODULE/****************************************************************************** * DEVICE OBJECT * * This object contains all the data for one card. A device object is * dynamically allocated in shmem for each card during initialization. * ******************************************************************************/typedef struct {    // Pins.    hal_s32_t				*pCount;	// Captured binary count value.    hal_float_t				*pPosition;	// Scaled position (floating point).    hal_bit_t				*pIndex;	// Current state of index.    hal_bit_t				*pIndexLatch;    hal_bit_t				*pLatchIndex;	// Setting this pin enables the index							// latch. When the next index pulse is							// seen, Count is cleared and IndexLatch							// is set. Clearing this pin resets the							// index latch. Use this feature at your							// own risk as the PID loop may get upset.    hal_bit_t				*pResetCount;	// Setting this pin causes Count to be reset.							// This pin is self clearing.    // Parameters.    hal_float_t				scale;		// Scaling factor for position.    // internal values    float old_scale;		/* stored scale value */    double scale_recip;		/* reciprocal value used for scaling */} EncoderPinsParams;typedef struct {    // Pins.    hal_float_t				*pValue;	// Desired value.    // Parameters.    hal_float_t				offset;    hal_float_t				gain;} DacPinsParams;typedef struct {    // Pins.    hal_float_t				*pValue;	// Converted value.    // Parameters.    hal_float_t				offset;    hal_float_t				gain;} AdcPinsParams;typedef struct {    // Pins.    hal_bit_t				*pValue;    hal_bit_t				*pValueNot;} DigitalInPinsParams;typedef struct {    // Pins.    hal_bit_t				*pValue;    // Parameters.    hal_bit_t				invert;} DigitalOutPinsParams;typedef struct {    // Pins.    hal_bit_t				*pEstopIn;    hal_bit_t				*pEstopInNot;    hal_bit_t				*pWatchdogReset;// This pin is self clearing.    // Parameters.    hal_u32_t				watchdogControl;} MiscPinsParams;typedef struct {    MotencRegMap			*pCard;    int					adcState;    hal_u32_t				watchdogControl;// Shadow HW register.    // private data    int					boardType;    char				*typeName;    int					boardID;    int					numFpga;    // Exported to HAL.    EncoderPinsParams			encoder[MOTENC_NUM_ENCODER_CHANNELS];    DacPinsParams			dac[MOTENC_NUM_DAC_CHANNELS];    AdcPinsParams			adc[MOTENC_NUM_ADC_CHANNELS];    DigitalInPinsParams			in[MOTENC_NUM_DIGITAL_INPUTS];    DigitalOutPinsParams		out[MOTENC_NUM_DIGITAL_OUTPUTS];    MiscPinsParams			misc;} Device;// These methods are used for initialization.static int Device_Init(Device *this, MotencRegMap *pCard);static int Device_ExportPinsParametersFunctions(Device *this, int componentId);static int Device_ExportEncoderPinsParametersFunctions(Device *this, int componentId, int boardId);static int Device_ExportDacPinsParametersFunctions(Device *this, int componentId, int boardId);static int Device_ExportAdcPinsParametersFunctions(Device *this, int componentId, int boardId);static int Device_ExportDigitalInPinsParametersFunctions(Device *this, int componentId, int boardId);static int Device_ExportDigitalOutPinsParametersFunctions(Device *this, int componentId, int boardId);static int Device_ExportMiscPinsParametersFunctions(Device *this, int componentId, int boardId);// These methods are exported to the HAL.static void Device_EncoderRead(void *this, long period);static void Device_DacWrite(void *this, long period);static void Device_AdcRead(void *this, long period);static void Device_DigitalInRead(void *this, long period);static void Device_DigitalOutWrite(void *this, long period);static void Device_MiscUpdate(void *this, long period);// Private helper methods.static int Device_AdcRead4(Device *this, int startChannel);/****************************************************************************** * DRIVER OBJECT * * This object contains all the data for this HAL component. * ******************************************************************************/#define MAX_DEVICES			4		// Since there are 2 board id bits.typedef struct {    int					componentId;	// HAL component ID.    Device				*deviceTable[MAX_DEVICES];    unsigned char			idPresent[MAX_DEVICES];} Driver;static Driver				driver;/****************************************************************************** * INIT AND EXIT CODE ******************************************************************************/intrtapi_app_main(void){    int					i, j;    struct pci_dev			*pDev = NULL;    MotencRegMap			*pCard = NULL;    Device				*pDevice;    // Connect to the HAL.    driver.componentId = hal_init("hal_motenc");    if (driver.componentId < 0) {	rtapi_print_msg(RTAPI_MSG_ERR, "MOTENC: ERROR: hal_init() failed\n");	return(-1);    }    for(i = 0; i < MAX_DEVICES; i++){	driver.deviceTable[i] = NULL;	driver.idPresent[i] = 0;    }    i = 0;    // Find a MOTENC card.    while((i < MAX_DEVICES) && ((pDev = pci_find_device(MOTENC_VENDOR_ID, MOTENC_DEVICE_ID, pDev)) != NULL)){	// Allocate memory for device object.	pDevice = hal_malloc(sizeof(Device));	if (pDevice == 0) {	    rtapi_print_msg(RTAPI_MSG_ERR, "MOTENC: ERROR: hal_malloc() failed\n");	    hal_exit(driver.componentId);	    return(-1);	}	// Save pointer to device object.	driver.deviceTable[i++] = pDevice;		// Map card into memory.	pCard = (MotencRegMap *)ioremap_nocache(pci_resource_start(pDev, 2), pci_resource_len(pDev, 2));	rtapi_print_msg(RTAPI_MSG_INFO, "MOTENC: Card detected in slot %2x\n", PCI_SLOT(pDev->devfn));	rtapi_print_msg(RTAPI_MSG_INFO, "MOTENC: Card address @ %x, Len = %d\n", (int)pCard, (int)pci_resource_len(pDev, 2));	// Initialize device.	Device_Init(pDevice, pCard);	rtapi_print_msg(RTAPI_MSG_INFO, "MOTENC: Card is %s, ID: %d\n", pDevice->typeName, pDevice->boardID);        if ( pDevice->boardType == 0 ) {	    rtapi_print_msg(RTAPI_MSG_ERR, "MOTENC: ERROR, unknown card detected\n");	    hal_exit(driver.componentId);	    return(-1);	}		if ( driver.idPresent[pDevice->boardID] != 0 ) {	    // duplicate ID... a strict driver would bail out, but	    // we are nice, we try to find an unused ID	    j = 0;	    while ( driver.idPresent[j] != 0 ) {		j++;	        if ( j >= MAX_DEVICES ) {		    rtapi_print_msg(RTAPI_MSG_ERR, "MOTENC: ERROR, duplicate ID, can't remap\n");		    hal_exit(driver.componentId);		    return(-1);		}	    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -