From a4982e6b369dfec8f0ab4a132048f8c0a34de9a7 Mon Sep 17 00:00:00 2001 From: Eric Fortin <21023555+Josbleuet@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:05:46 -0500 Subject: [PATCH] opto_ac5: bring the driver back, ported to rtapi_pci for uspace The Opto22 PCI-AC5 driver was removed in dd394d90d0 because it only built for RTAI kernels and nobody was known to use or be able to test it. It is in daily use on a milling machine here, so port it instead. The PCI discovery and mapping change: pci_get_device() / ioremap_nocache() become a rtapi_pci driver (probe/remove, rtapi_pci_ioremap_bar), and readl()/writel() go through ac5_read() / ac5_write(), which keep readl()/writel() in a kernel build and do a volatile 32 bit access on the mapped BAR in uspace. hal_export_functf() follows the current signature. The uspace rtapi_pci matches subvendor/subdevice exactly (no PCI_ANY_ID), so the id table lists the subsystem IDs the card reports (148a:ac05). From review: each HAL pin and the invert parameter are read once per cycle, and the sources use spaces only with consistent spacing. The driver is built like hal_gm, for uspace and RTAI. Its docs, man page, component-list row and index entry are restored. The doc page is renamed opto_ac5.adoc to match the driver, and both docs now give the real pin names (opto-ac5.N.portP...), bool types, IN for the LED pins, RW for the invert parameter and the actual meaning of -not and -invert. --- docs/po4a.cfg | 2 + docs/src/Submakefile | 1 + docs/src/drivers/opto_ac5.adoc | 151 ++++++++++ docs/src/hal/components.adoc | 1 + docs/src/index.tmpl | 1 + docs/src/man/man9/opto_ac5.9.adoc | 75 +++++ src/Makefile | 3 + src/Makefile.inc.in | 1 + src/hal/drivers/opto_ac5.c | 482 ++++++++++++++++++++++++++++++ src/hal/drivers/opto_ac5.h | 73 +++++ 10 files changed, 790 insertions(+) create mode 100644 docs/src/drivers/opto_ac5.adoc create mode 100644 docs/src/man/man9/opto_ac5.9.adoc create mode 100644 src/hal/drivers/opto_ac5.c create mode 100644 src/hal/drivers/opto_ac5.h diff --git a/docs/po4a.cfg b/docs/po4a.cfg index 0193f140fd2..0845450e193 100644 --- a/docs/po4a.cfg +++ b/docs/po4a.cfg @@ -65,6 +65,7 @@ [type: AsciiDoc_def] src/drivers/hal_gpio.adoc $lang:build/adoc/$lang/drivers/hal_gpio.adoc [type: AsciiDoc_def] src/drivers/mb2hal.adoc $lang:build/adoc/$lang/drivers/mb2hal.adoc [type: AsciiDoc_def] src/drivers/mitsub-vfd.adoc $lang:build/adoc/$lang/drivers/mitsub-vfd.adoc +[type: AsciiDoc_def] src/drivers/opto_ac5.adoc $lang:build/adoc/$lang/drivers/opto_ac5.adoc [type: AsciiDoc_def] src/drivers/pico-ppmc.adoc $lang:build/adoc/$lang/drivers/pico-ppmc.adoc [type: AsciiDoc_def] src/drivers/pmx485.adoc $lang:build/adoc/$lang/drivers/pmx485.adoc [type: AsciiDoc_def] src/drivers/shuttle.adoc $lang:build/adoc/$lang/drivers/shuttle.adoc @@ -358,6 +359,7 @@ [type: AsciiDoc_def] src/man/man9/lcd.9.adoc $lang:build/adoc/$lang/man/man9/lcd.9.adoc [type: AsciiDoc_def] src/man/man9/lineardeltakins.9.adoc $lang:build/adoc/$lang/man/man9/lineardeltakins.9.adoc [type: AsciiDoc_def] src/man/man9/motion.9.adoc $lang:build/adoc/$lang/man/man9/motion.9.adoc +[type: AsciiDoc_def] src/man/man9/opto_ac5.9.adoc $lang:build/adoc/$lang/man/man9/opto_ac5.9.adoc [type: AsciiDoc_def] src/man/man9/rosekins.9.adoc $lang:build/adoc/$lang/man/man9/rosekins.9.adoc [type: AsciiDoc_def] src/man/man9/sampler.9.adoc $lang:build/adoc/$lang/man/man9/sampler.9.adoc [type: AsciiDoc_def] src/man/man9/setsserial.9.adoc $lang:build/adoc/$lang/man/man9/setsserial.9.adoc diff --git a/docs/src/Submakefile b/docs/src/Submakefile index 5bd1092d01f..ba42fa84414 100644 --- a/docs/src/Submakefile +++ b/docs/src/Submakefile @@ -169,6 +169,7 @@ DOC_SRCS_EN := \ drivers/hal_pi_gpio.adoc \ drivers/hostmot2.adoc \ drivers/mb2hal.adoc \ + drivers/opto_ac5.adoc \ drivers/pico-ppmc.adoc \ drivers/pmx485.adoc \ drivers/shuttle.adoc \ diff --git a/docs/src/drivers/opto_ac5.adoc b/docs/src/drivers/opto_ac5.adoc new file mode 100644 index 00000000000..ee0b5d68d46 --- /dev/null +++ b/docs/src/drivers/opto_ac5.adoc @@ -0,0 +1,151 @@ +:lang: en +:toc: + +[[cha:opto-ac5]] += Opto22 PCI-AC5 Driver + +*PCI AC5 ADAPTER CARD / HAL DRIVER* + +== The Adapter Card + +This is a card made by Opto22 for adapting the PCI port to solid state +relay racks such as their standard or G4 series. It has 2 ports that +can control up to 24 points each and has 4 on board LEDs. The ports use +50 pin connectors the same as Mesa boards. Any relay racks/breakout +boards that work with Mesa Cards should work with this card with the +understanding any encoder counters, PWM, etc., would have to be done in +software. The AC5 does not have any 'smart' logic on board, it is just +an adapter. + +See the manufacturer's website for more info: + +https://www.opto22.com/site/pr_details.aspx?cid=4&item=PCI-AC5 + +I would like to thank Opto22 for releasing info in their manual, +easing the writing of this driver! + +== The Driver + +This driver is for the PCI AC5 card and will not work with the ISA AC5 +card. The HAL driver is a realtime module. It will support 4 cards as +is (more cards are possible with a change in the source code). Load the +basic driver like so: + +[source,hal] +---- +loadrt opto_ac5 +---- + +This will load the driver which will search for max 4 boards. It will +set I/O of each board's 2 ports to a default setting. The default +configuration is for 12 inputs then 12 outputs. The pin name numbers +correspond to the position on the relay rack. For example the pin names +for the default I/O setting of port 0 would be: + +* 'opto-ac5.0.port0.in-00' - + They would be numbered from 00 to 11 +* 'opto-ac5.0.port0.out-12' - + They would be numbered 12 to 23 port 1 would be the same. + +== Pins + + * 'opto-ac5.[BOARDNUMBER].port[PORTNUMBER].in-[PINNUMBER] OUT bool' - + + * 'opto-ac5.[BOARDNUMBER].port[PORTNUMBER].in-[PINNUMBER]-not OUT bool' - + Connect a HAL bool signal to this pin to read an I/O point from the + card. The PINNUMBER represents the position in the relay rack. Eg. + PINNUMBER 0 is position 0 in a Opto22 relay rack and would be pin 47 + on the 50 pin header connector. The -in pin is TRUE when the input + is on (the line is pulled LOW). The -not pin is always the inverse of + the -in pin. + + * 'opto-ac5.[BOARDNUMBER].port[PORTNUMBER].out-[PINNUMBER] IN bool' - + Connect a HAL bool signal to this pin to write to an I/O point of the + card. The PINNUMBER represents the position in the relay rack. Eg. + PINNUMBER 23 is position 23 in a Opto22 relay rack and would be pin 1 + on the 50 pin header connector. TRUE turns the output on (the line is + driven LOW). + + * 'opto-ac5.[BOARDNUMBER].led[NUMBER] IN bool' - + Turns one of the 4 onboard LEDs on/off. LEDs are numbered 0 to 3. + +BOARDNUMBER can be 0-3 PORTNUMBER can be 0 or 1. Port 0 is closest to +the card bracket. + +== Parameters + +* 'opto-ac5.[BOARDNUMBER].port[PORTNUMBER].out-[PINNUMBER]-invert RW bool' - + When TRUE, invert the meaning of the corresponding -out pin so that + TRUE turns the output off and FALSE turns it on. + +== Functions + +* 'opto-ac5.[BOARDNUMBER].digital-read' - + Add this to a thread to read all the input points. + +* 'opto-ac5.[BOARDNUMBER].digital-write' - + Add this to a thread to write all the output points and LEDs. + +== Configuring I/O Ports + +To change the default setting load the driver something like so: + +[source,hal] +---- +loadrt opto_ac5 portconfig0=0xffff portconfig1=0xff0000 +---- + +Of course changing the numbers to match the I/O you would like. Each +port can be set up different. + +Here's how to figure out the number: The configuration number +represents a 32 bit long code to tell the card which I/O points are +output vrs input. The lower 24 bits are the I/O points of one port. The +2 highest bits are for 2 of the on board LEDs. A one in any bit +position makes the I/O point an output. The two highest bits must be +output for the LEDs to work. The driver will automatically set the two +highest bits for you, we won't talk about them. + +The easiest way to do this is to fire up the calculator under +APPLICATIONS/ACCESSORIES. Set it to scientific (click view). Set it +BINARY (radio button Bin). Press 1 for every output you want and/or +zero for every input. Remember that HAL pin 00 corresponds to the +rightmost bit. 24 numbers represent the 24 I/O points of one port. So +for the default setting (12 inputs then 12 outputs) you would push 1 +twelve times (that's the outputs) then 0 twelve times (that's the +inputs). Notice the first I/O point is the lowest (rightmost) bit. +(that bit corresponds to HAL pin 00 .looks backwards) You should have +24 digits on the screen. Now push the Hex radio button. The displayed +number (fff000) is the configport number ( put a '0x' in front of it +designating it as a HEX number). + +Another example: To set the port for 8 outputs and 16 inputs (the +same as a Mesa card). Here is the 24 bits represented in a BINARY +number. Bit 1 is the rightmost number: + +.16 zeros for the 16 inputs and 8 ones for the 8 outputs +---- +000000000000000011111111 +---- + +This converts to FF on the calculator, so 0xff is the number to use +for portconfig0 and/or portconfig1 when loading the driver. + +== Pin Numbering + +HAL pin 00 corresponds to bit 1 (the rightmost) which represents +position 0 on an Opto22 relay rack. HAL pin 01 corresponds to bit 2 +(one spot to the left of the rightmost) which represents position 1 on +an Opto22 relay rack. HAL pin 23 corresponds to bit 24 (the +leftmost) which represents position 23 on an Opto22 relay rack. + +HAL pin 00 connects to pin 47 on the 50 pin connector of each port. +HAL pin 01 connects to pin 45 on the 50 pin connector of each port. +HAL pin 23 connects to pin 1 on the 50 pin connector of each port. + +Note that Opto22 and Mesa use opposite numbering systems: Opto22 +position 23 = connector pin 1, and the position goes down as the +connector pin number goes up. Mesa Hostmot2 position 1 = connector pin +1, and the position number goes up as the connector pin number goes up. + +// vim: set syntax=asciidoc: diff --git a/docs/src/hal/components.adoc b/docs/src/hal/components.adoc index 748856db517..4bdc8c63b0d 100644 --- a/docs/src/hal/components.adoc +++ b/docs/src/hal/components.adoc @@ -127,6 +127,7 @@ To search in the man pages, use the UNIX tool `apropos`. | link:../man/man9/mesa_7i65.9.html[mesa_7i65] |Mesa Electronics driver for the 7I65 eight-axis servo card. (See the man page for more information) || | link:../man/man9/mesa_pktgyro_test.9.html[mesa_pktgyro_test] |PktUART simple test with Microstrain 3DM-GX3-15 gyro || | link:../man/man9/mesa_uart.9.html[mesa_uart] |An example component demonstrating how to access the Hostmot2 UART || +| link:../man/man9/opto_ac5.9.html[opto_ac5] |Realtime driver for opto22 PCI-AC5 cards || | link:../man/man9/serport.9.html[serport] |Hardware driver for the digital I/O bits of the 8250 and 16550 serial port || | link:../man/man9/setsserial.9.html[setsserial] |An utility for setting Smart Serial NVRAM parameters || | link:../man/man9/sserial.9.html[sserial] |hostmot2 - Smart Serial LinuxCNC HAL driver for the Mesa Electronics HostMot2 Smart-Serial remote cards || diff --git a/docs/src/index.tmpl b/docs/src/index.tmpl index 2b0d800ae21..59650533e22 100644 --- a/docs/src/index.tmpl +++ b/docs/src/index.tmpl @@ -114,6 +114,7 @@
  • Mesa HostMot2 Driver
  • Modbus to HAL Driver
  • Mitsubishi VFD Driver
  • +
  • Opto22 PCI-AC5 Driver
  • Pico Drivers
  • Powermax Modbus Driver
  • ShuttleXpress and ShuttlePRO Driver
  • diff --git a/docs/src/man/man9/opto_ac5.9.adoc b/docs/src/man/man9/opto_ac5.9.adoc new file mode 100644 index 00000000000..8526ed505c8 --- /dev/null +++ b/docs/src/man/man9/opto_ac5.9.adoc @@ -0,0 +1,75 @@ += opto_ac5(9) + +== NAME + +opto_ac5 - Realtime driver for opto22 PCI-AC5 cards + +== SYNOPSIS + +*loadrt opto_ac5* [**portconfig0=0x**_N_] [**portconfig1=0x**_N_] + +== DESCRIPTION + +These pins and parameters are created by the realtime *opto_ac5* module. +The portconfig0 and portconfig1 variables are used to configure the two +ports of each card. The first 24 bits of a 32 bit number represent the +24 i/o points of each port. The lowest (rightmost) bit would be HAL pin +0 which is header connector pin 47. Then next bit to the left would be +HAL pin 1, header connector pin 45 and so on, until bit 24 would be HAL +pin 23, header connector pin 1. "1" bits represent output points. So +channel 0..11 as inputs and 12..23 as outputs would be represented by +(in binary) 111111111111000000000000 which is 0xfff000 in hexadecimal. +That is the number you would use, e.g. + +[source,hal] +---- +loadrt opto_ac5 portconfig0=0xfff000 +---- + +If no portconfig variable is specified the default configuration is 12 +inputs then 12 outputs. + +Up to 4 boards are supported. Boards are numbered starting at 0. + +Portnumber can be 0 or 1. Port 0 is closest to the card bracket. + +== PINS + +**opto-ac5.**_BOARDNUMBER_**.port**_PORTNUMBER_**.in-**_PINNUMBER_ OUT bool:: + + + +**opto-ac5.**_BOARDNUMBER_**.port**_PORTNUMBER_**.in-**_PINNUMBER_**-not** OUT bool:: + Connect a HAL bool signal to this pin to read an i/o point from the card. + The PINNUMBER represents the position in the relay rack. E.g., PINNUMBER 0 is + position 0 in a opto22 relay rack and would be pin 47 on the 50 pin header connector. + The *-in* pin is TRUE when the input is on (the line is pulled LOW). The *-not* + pin is always the inverse of the *-in* pin. +**opto-ac5.**_BOARDNUMBER_**.port**_PORTNUMBER_**.out-**_PINNUMBER_ IN bool:: + Connect a HAL bool signal to this pin to write to an i/o point of the + card. The PINNUMBER represents the position in the relay rack. E.g., + PINNUMBER 23 is position 23 in a opto22 relay rack and would be pin 1 + on the 50 pin header connector. TRUE turns the output on (the line is + driven LOW). +**opto-ac5.**_BOARDNUMBER_**.led**_NUMBER_ IN bool:: + Turns one of the on board LEDS on/off. LEDS are numbered 0 to 3. + +== PARAMETERS + +**opto-ac5.**_BOARDNUMBER_**.port**_PORTNUMBER_**.out-**_PINNUMBER_**-invert** RW bool:: + When TRUE, invert the meaning of the corresponding *-out* pin so that + TRUE turns the output off and FALSE turns it on. + +== FUNCTIONS + +**opto-ac5.**_BOARDNUMBER_**.digital-read**:: + Add this to a thread to read all the input points. +**opto-ac5.**_BOARDNUMBER_**.digital-write**:: + Add this to a thread to write all the output points and LEDS. + +== BUGS + +All boards are loaded with the same port configurations as the first board. + +== SEE ALSO + +https://wiki.linuxcnc.org/cgi-bin/wiki.pl?OptoPciAc5 diff --git a/src/Makefile b/src/Makefile index 0ca47e84e21..d46373780be 100644 --- a/src/Makefile +++ b/src/Makefile @@ -933,6 +933,8 @@ hal_speaker-objs := hal/drivers/hal_speaker.o $(MATHSTUB) endif obj-$(CONFIG_HAL_GM) += hal_gm.o hal_gm-objs := hal/drivers/hal_gm.o $(MATHSTUB) +obj-$(CONFIG_OPTO_AC5) += opto_ac5.o +opto_ac5-objs := hal/drivers/opto_ac5.o $(MATHSTUB) obj-$(CONFIG_HAL_PPMC) += hal_ppmc.o hal_ppmc-objs := hal/drivers/hal_ppmc.o $(MATHSTUB) @@ -1403,6 +1405,7 @@ endif ../rtlib/rosekins$(MODULE_EXT): $(addprefix objects/rt,$(rosekins-objs)) ../rtlib/scorbot-kins$(MODULE_EXT): $(addprefix objects/rt,$(scorbot-kins-objs)) ../rtlib/hal_gm$(MODULE_EXT): $(addprefix objects/rt,$(hal_gm-objs)) +../rtlib/opto_ac5$(MODULE_EXT): $(addprefix objects/rt,$(opto_ac5-objs)) ../rtlib/hostmot2$(MODULE_EXT): $(addprefix objects/rt,$(hostmot2-objs)) ../rtlib/hm2_test$(MODULE_EXT): $(addprefix objects/rt,$(hm2_test-objs)) ../rtlib/hm2_pci$(MODULE_EXT): $(addprefix objects/rt,$(hm2_pci-objs)) diff --git a/src/Makefile.inc.in b/src/Makefile.inc.in index ffaaa804292..d97660ee03b 100644 --- a/src/Makefile.inc.in +++ b/src/Makefile.inc.in @@ -215,6 +215,7 @@ CONFIG_HAL_SPEAKER=m CONFIG_HAL_PPMC=m CONFIG_HOSTMOT2=m CONFIG_HAL_GM=m +CONFIG_OPTO_AC5=m CONFIG_HAL_BB_GPIO=m CONFIG_HAL_PI_GPIO=m CONFIG_HAL_GPIO=m diff --git a/src/hal/drivers/opto_ac5.c b/src/hal/drivers/opto_ac5.c new file mode 100644 index 00000000000..66bea9d009e --- /dev/null +++ b/src/hal/drivers/opto_ac5.c @@ -0,0 +1,482 @@ +#include // RTAPI realtime OS API. +#include // RTAPI realtime module decls. +#include +#include // HAL public API decls. +#include "opto_ac5.h" // Hardware dependent defines. + +#ifndef MODULE +#define MODULE +#endif + +#ifdef MODULE +// Module information. +MODULE_AUTHOR("Chris Morley"); +MODULE_DESCRIPTION("Driver for opto22 pci AC5 for EMC HAL"); +MODULE_LICENSE("GPL"); +#endif // MODULE + + +/************************************************************************* + typedefs and defines +*************************************************************************/ + + +// Vendor and device ID. "magic numbers" +#define opto22_VENDOR_ID 0x148a // opto22 corp +#define opto22_pci_AC5_DEVICE_ID 0xac05 // AC5 relay board interface +#define MAX_BOARDS 4 // could be higher uses more memory + +// OFFSETS FROM BASE ADDRESS FOR COMMANDS TO OPTO CARD +// for port 0 +#define CONFIG_WRITE_OFFSET_0 0x00000004 +#define CONFIG_READ_OFFSET_0 0x00000100 +#define DATA_WRITE_OFFSET_0 0X00000008 +#define DATA_READ_OFFSET_0 0X00000300 + +// for port 1 +#define CONFIG_WRITE_OFFSET_1 0x00000014 +#define CONFIG_READ_OFFSET_1 0x00000500 +#define DATA_WRITE_OFFSET_1 0X00000018 +#define DATA_READ_OFFSET_1 0X00000700 + +/* 32 bit register access. readl()/writel() in a kernel (RTAI) build, a + * volatile access to the BAR mapped by rtapi_pci_ioremap_bar() in uspace. */ +#ifdef __KERNEL__ +#define ac5_read(b, off) readl((b)->base + (off)) +#define ac5_write(b, off, v) writel((v), (b)->base + (off)) +#else +static inline rtapi_u32 ac5_read(board_data_t *b, unsigned long off) +{ + return *(volatile rtapi_u32 *)((volatile char *)b->base + off); +} +static inline void ac5_write(board_data_t *b, unsigned long off, rtapi_u32 v) +{ + *(volatile rtapi_u32 *)((volatile char *)b->base + off) = v; +} +#endif + +// These methods are used for initialization. +static int Device_Init(board_data_t *pboard); +static int Device_ExportPinsParametersFunctions(board_data_t *pboard, int comp_id, int boardId); +static int Device_ExportDigitalInPinsParametersFunctions(board_data_t *pboard, int comp_id, int boardId); +static int Device_ExportDigitalOutPinsParametersFunctions(board_data_t *pboard, int comp_id, int boardId); +// These methods are exported to the HAL. +static void Device_DigitalInRead(void *this, long period); +static void Device_DigitalOutWrite(void *this, long period); +/************************************************************************* + Globals +*************************************************************************/ + + +typedef struct { + int comp_id; /* HAL component ID */ + board_data_t *boards[MAX_BOARDS]; + int num_of_boards; +} Driver_t; + +static Driver_t driver; + +// in case no portconfig is given when loading driver +// sets 12 inputs then 12 outputs per port and sets leds (bit 31,32) for output +// could define more variables so the ports of extra cards could be configured differently but +// I doubt if anyone will use more then one card + +static unsigned long portconfig0 = 0Xc0fff000; +RTAPI_MP_LONG(portconfig0, "port 0 i/o configuration number"); +static unsigned long portconfig1 = 0Xc0fff000; +RTAPI_MP_LONG(portconfig1, "port 1 i/o configuration number"); + +/****************************************************************************** + PCI driver + ******************************************************************************/ + +static int failed_errno = 0; /* errno of the last failed probe */ + +/* The uspace rtapi_pci matches subvendor/subdevice exactly (no PCI_ANY_ID + * wildcard), so list the subsystem IDs the card reports (lspci -nn). */ +static struct rtapi_pci_device_id opto_ac5_pci_tbl[] = { + { + .vendor = opto22_VENDOR_ID, + .device = opto22_pci_AC5_DEVICE_ID, + .subvendor = opto22_VENDOR_ID, + .subdevice = opto22_pci_AC5_DEVICE_ID, + }, + {0,}, +}; + +static int opto_ac5_pci_probe(struct rtapi_pci_dev *dev, const struct rtapi_pci_device_id *id) +{ + board_data_t *pboard; + int n = driver.num_of_boards; + + (void)id; + if (n >= MAX_BOARDS) { + rtapi_print_msg(RTAPI_MSG_ERR, + "ERROR OPTO_AC5--- skipping board at %s, this driver handles %d boards\n", + rtapi_pci_name(dev), MAX_BOARDS); + return -EINVAL; + } + if (rtapi_pci_enable_device(dev)) { + rtapi_print_msg(RTAPI_MSG_ERR, + "ERROR OPTO_AC5--- failed to enable PCI device %s\n", rtapi_pci_name(dev)); + return failed_errno = -ENODEV; + } + + /* Allocate HAL memory for the board */ + pboard = (board_data_t *)(hal_malloc(sizeof(board_data_t))); + if (pboard == NULL) { + rtapi_print_msg(RTAPI_MSG_ERR, "ERROR OPTO_AC5--- hal_malloc() failed\n"); + return failed_errno = -ENOMEM; + } + pboard->pci_dev = dev; + pboard->num = n; + + /* region 0 is the 32 bit memory mapped I/O region */ + pboard->len = rtapi_pci_resource_len(dev, 0); + pboard->base = rtapi_pci_ioremap_bar(dev, 0); + if (pboard->base == NULL) { + rtapi_print_msg(RTAPI_MSG_ERR, + "ERROR OPTO_AC5--- could not map board %d (%s) PCI region 0\n", n, rtapi_pci_name(dev)); + return failed_errno = -ENODEV; + } + rtapi_print("INFO OPTO_AC5--- board %d at %s mapped, Len = %ld\n", + n, rtapi_pci_name(dev), (long)pboard->len); + + // Initialize device. + if (Device_Init(pboard)) + return failed_errno = -EINVAL; + + // Export pins, parameters, and functions. + if (Device_ExportPinsParametersFunctions(pboard, driver.comp_id, n)) + return failed_errno = -EINVAL; + + driver.boards[n] = pboard; + driver.num_of_boards++; + rtapi_pci_set_drvdata(dev, pboard); + return 0; +} + +// here we turn the leds and outputs off and unmap the board +static void opto_ac5_pci_remove(struct rtapi_pci_dev *dev) +{ + int n; + + for (n = 0; n < MAX_BOARDS; n++) { + board_data_t *pboard = driver.boards[n]; + if (pboard == NULL || pboard->pci_dev != dev) + continue; + rtapi_print("INFO OPTO_AC5--- board %i driver removed.\n", n); + ac5_write(pboard, DATA_WRITE_OFFSET_0, 0XC0FFFFFF); + ac5_write(pboard, DATA_WRITE_OFFSET_1, 0XC0FFFFFF); + rtapi_iounmap(pboard->base); + pboard->base = NULL; + driver.boards[n] = NULL; + rtapi_pci_disable_device(dev); + rtapi_pci_set_drvdata(dev, NULL); + } +} + +static struct rtapi_pci_driver opto_ac5_pci_driver = { + .name = "opto_ac5", + .id_table = opto_ac5_pci_tbl, + .probe = opto_ac5_pci_probe, + .remove = opto_ac5_pci_remove, +}; + +/****************************************************************************** + Init and exit code + ******************************************************************************/ + +int rtapi_app_main(void) +{ + int n, r; + + // Connect to the HAL. + driver.comp_id = hal_init("opto_ac5"); + if (driver.comp_id < 0) { + rtapi_print_msg(RTAPI_MSG_ERR, " ERROR OPTO_AC5--- hal_init() failed\n"); + return(-1); + } + for (n = 0; n < MAX_BOARDS; n++) { + driver.boards[n] = NULL; + } + driver.num_of_boards = 0; + + r = rtapi_pci_register_driver(&opto_ac5_pci_driver); + if (r != 0) { + rtapi_print_msg(RTAPI_MSG_ERR, "ERROR OPTO_AC5--- error registering PCI driver\n"); + hal_exit(driver.comp_id); + return r; + } + if (failed_errno || driver.num_of_boards == 0) { + if (driver.num_of_boards == 0) + rtapi_print_msg(RTAPI_MSG_ERR, "ERROR OPTO_AC5--- No opto PCI-AC5 card(s) detected\n"); + rtapi_pci_unregister_driver(&opto_ac5_pci_driver); + hal_exit(driver.comp_id); + return failed_errno ? failed_errno : -ENODEV; + } + + hal_ready(driver.comp_id); + return(0); +} + +void rtapi_app_exit(void) +{ + rtapi_pci_unregister_driver(&opto_ac5_pci_driver); + hal_exit(driver.comp_id); +} + +/****************************************************************************** + * DEVICE OBJECT FUNCTION DEFINITIONS + ******************************************************************************/ + +/* + * LOCAL FUNCTIONS + */ +static int Device_Init(board_data_t *pboard) +{ + + // Initialize hardware. + // portconfig[0 or 1] are either the default number from global or mapped from the command line + // make sure last two bits are set for output so leds will work + + if ((portconfig0 & 0x80000000) == 0) { + portconfig0 |= 0x80000000; + } + if ((portconfig0 & 0x40000000) == 0) { + portconfig0 |= 0x40000000; + } + if ((portconfig1 & 0x80000000) == 0) { + portconfig1 |= 0x80000000; + } + if ((portconfig1 & 0x40000000) == 0) { + portconfig1 |= 0x40000000; + } + ac5_write(pboard, CONFIG_WRITE_OFFSET_0, portconfig0); + ac5_write(pboard, CONFIG_WRITE_OFFSET_1, portconfig1); + + // Initialize digital I/O structure mask. + + pboard->port[0].mask = portconfig0; + pboard->port[1].mask = portconfig1; + + return(0); +} + +static int Device_ExportPinsParametersFunctions(board_data_t *this, int componentId, int boardId) +{ + int msgLevel, error = 0; + + // This function exports a lot of stuff, which results in a lot of + // logging if msg_level is at INFO or ALL. So we save the current value + // of msg_level and restore it later. If you actually need to log this + // function's actions, change the second line below. + + msgLevel = rtapi_get_msg_level(); + rtapi_set_msg_level(RTAPI_MSG_WARN); + + // Export digital I/O. + + if (!error) error = Device_ExportDigitalInPinsParametersFunctions(this, componentId, boardId); + if (!error) error = Device_ExportDigitalOutPinsParametersFunctions(this, componentId, boardId); + + // Restore saved message level. + + rtapi_set_msg_level(msgLevel); + + return(error); +} + +// we check each ports mask (port[portnum].mask) against a mask bit to see which of the 24 points of i/o are inputs (a false bit is an input) +// then export HAL pins mapped to the proper io structure +// HAL pin numbers represent position in an opto22 relay rack (eg. pin 00 is position 0) +// this way you can configure the i/o in any way for all the 24 points of each port + +static int Device_ExportDigitalInPinsParametersFunctions(board_data_t *this, int comp_id, int boardId) +{ + int halError = 0, channel, mask, portnum = 0; + + // Export pins and parameters. + while (portnum < 2) { + mask = 1; + for (channel = 0; channel < 24; channel++) { + if ((this->port[portnum].mask & mask) == 0) { // physical input? + // Pins. + if ((halError = hal_pin_new_bool(comp_id, HAL_OUT, &(this->port[portnum].io[channel].pValue), + 0, "opto-ac5.%d.port%d.in-%02d", boardId, portnum, channel)) != 0) + break; + + if ((halError = hal_pin_new_bool(comp_id, HAL_OUT, &(this->port[portnum].io[channel].pValueNot), + 1, "opto-ac5.%d.port%d.in-%02d-not", boardId, portnum, channel)) != 0) + break; + } + mask <<= 1; + } + + portnum ++; + } + + // Export functions. + if (!halError) { + halError = hal_export_functf(Device_DigitalInRead, this, 0, comp_id, "opto-ac5.%d.digital-read", boardId); + } + + if (halError) { + rtapi_print_msg(RTAPI_MSG_ERR, "ERROR OPTO22_AC5---exporting digital inputs to HAL failed\n"); + return(-1); + } + + return(0); +} + + +// we check each ports mask (port[portnum].mask) against a mask bit to see which of the 24 points of i/o are outputs (a true bit is an output) +// then export HAL pins mapped to the proper io structure +// this way you can configure the i/o in any way for all the 24 points of each port +// HAL pin numbers represent position in an opto22 relay rack (eg. pin 00 is position 0) +// the LEDS are bits 31 and 32 of the portmask but are mapped to 24 and 25 of the i0 structure + +static int Device_ExportDigitalOutPinsParametersFunctions(board_data_t *this, int comp_id, int boardId) +{ + int halError = 0, channel, mask, portnum = 0; + + // Export pins and parameters. + + while (portnum < 2) { + mask = 1; + for (channel = 0; channel < 24; channel++) { + if ((this->port[portnum].mask & mask) != 0) { // physical output? + // Pins. + if ((halError = hal_pin_new_bool(comp_id, HAL_IN, &(this->port[portnum].io[channel].pValue), + 0, "opto-ac5.%d.port%d.out-%02d", boardId, portnum, channel)) != 0) + break; + + // Parameters. + if ((halError = hal_param_new_bool(comp_id, HAL_RW, &(this->port[portnum].io[channel].invert), + 0, "opto-ac5.%d.port%d.out-%02d-invert", boardId, portnum, channel)) != 0) + break; + } + mask <<= 1; + } + portnum++; + } + // led Pins. + portnum = 0; + for (channel = 0; channel < 2; channel++) { + if ((halError = hal_pin_new_bool(comp_id, HAL_IN, &(this->port[portnum].io[24].pValue), + 0, "opto-ac5.%d.led%d", boardId, channel + portnum)) != 0) + break; + + if ((halError = hal_pin_new_bool(comp_id, HAL_IN, &(this->port[portnum].io[25].pValue), + 0, "opto-ac5.%d.led%d", boardId, channel + portnum + 1)) != 0) + break; + portnum++; + } + // Export functions. + if (!halError) { + halError = hal_export_functf(Device_DigitalOutWrite, this, 0, comp_id, "opto-ac5.%d.digital-write", boardId); + } + + if (halError) { + rtapi_print_msg(RTAPI_MSG_ERR, "ERROR OPTO_AC5---exporting digital outputs to HAL failed\n"); + return(-1); + } + + return(0); +} + +// here we read inputs from board +// we read the current data (variable 'pins') of the first port. Then for each of the 24 points +// we compare to the mask of the first port to see which of the 24 io points are inputs (the bits that are false) +// if it is an input then check 'pins' against the mask to see if input bit is true +// update the HAL pin and not-pin accordingly. shift the mask then do the next point (of 24 io points) +// after all pins done-increase 'portnum' to 1 set offset to the offset for port1 +// then do it all again on the second port + +static void +Device_DigitalInRead(void *arg, long period) +{ + (void)period; + board_data_t *pboard = (board_data_t *)arg; + DigitalPinsParams *pDigital; + int i, portnum = 0; + unsigned long pins, offset = DATA_READ_OFFSET_0, mask; + + // For each port. + while (portnum < 2) { + mask = 1; + pDigital = &pboard->port[portnum].io[0]; + + // Read digital I/O register. + pins = ac5_read(pboard, offset); + for (i = 0; i < 24; i++, pDigital++) { + if ((pboard->port[portnum].mask & mask) == 0) { // is it an input bit ? + // A 0 read from the hardware means the input is on. + rtapi_bool b = (pins & mask) == 0; + hal_set_bool(pDigital->pValue, b); + hal_set_bool(pDigital->pValueNot, !b); + } + mask <<= 1; // shift mask + } + portnum++; + offset = DATA_READ_OFFSET_1; // change to offset for port1 + } +} + +// here we output data and update LEDS +// we look at the mask of the first port to see which of the 24 io points are outputs (the bits that are true) +// then check the OUT HAL pins to see if output should be on and if it should - OR the bit (using 'mask') to the variable 'pins' +// then set 'mask' to the last bit (32) and check the HAL led pin to see if true- OR the bit to 'pins' if it is +// set 'mask to second to second to last bit (31) do the same +// have to remember that a 1 sent to the hardware turns an output OFF +// finally write the variable 'pins' to the boards first port +// reset offset to the next port offset, increase portnum for port 1 +// then do it all again on the second port + +static void +Device_DigitalOutWrite(void *arg, long period) +{ + (void)period; + board_data_t *pboard = (board_data_t *)arg; + DigitalPinsParams *pDigital; + int portnum = 0; + unsigned int i, j; + unsigned long pins, offset = DATA_WRITE_OFFSET_0, mask; + + // For each port. + while (portnum < 2) { + pDigital = &pboard->port[portnum].io[0]; + mask = 1; + pins = 0; + + // For each pin. + for (j = 0; j < 24; j++, pDigital++) { + if ((pboard->port[portnum].mask & mask) != 0) { // is it an output? + // A 1 written to the hardware turns the output off, so set + // the bit when the pin is off, or on with invert set. + rtapi_bool val = hal_get_bool(pDigital->pValue); + rtapi_bool inv = hal_get_bool(pDigital->invert); + if (val == inv) { + pins |= mask; + } + } + mask <<= 1; // shift mask + + } + + // CHECK LED PINS + pDigital = &pboard->port[portnum].io[23]; // one before what we want to check + for (i = 0; i < 2; i++) { + mask = (unsigned int) 1 << (31 - i); + pDigital++; + + if (hal_get_bool(pDigital->pValue) == 0) { + pins |= mask; + } + } + // Write digital I/O register. + ac5_write(pboard, offset, pins); + portnum++; + offset = DATA_WRITE_OFFSET_1; // set to port1 offset + } +} diff --git a/src/hal/drivers/opto_ac5.h b/src/hal/drivers/opto_ac5.h new file mode 100644 index 00000000000..f128930d7af --- /dev/null +++ b/src/hal/drivers/opto_ac5.h @@ -0,0 +1,73 @@ +// Copyright 2005-2008, various authors +// +// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +/************************************************************************* + +Header for opto22 pci AC5 board driver + +Copyright (C) 2008 Chris Morley + +*************************************************************************/ + +#include +#include + +/* code assumes that PINS_PER_PORT is <= 24 */ +//there are 2 ports on a opto22 pci AC5 card +//number of i/o per board = 48 + 4 LEDS +#define OPTO_NUM_DIGITAL 52 + +/************************************************************************* + Data Structures +*************************************************************************/ +typedef struct DigitalPinsParams { + // Pins. + hal_bool_t pValue; + hal_bool_t pValueNot; + // Parameters. + hal_bool_t invert; +} DigitalPinsParams; + + + +typedef struct port_t { + rtapi_u32 mask; + struct DigitalPinsParams io[OPTO_NUM_DIGITAL / 2]; +} port_t; + + + + +/* master board structure */ +typedef struct board_data_t { + struct rtapi_pci_dev *pci_dev; /* PCI device */ + int num; /* HAL board number */ + void rtapi__iomem *base; /* base address */ + int len; /* length of address space*/ + struct port_t port[2]; + + +} board_data_t; + +/************************************************************************* + Globals +*************************************************************************/ + +//extern int comp_id; /* HAL component ID */ + + +/************************************************************************* + Functions +*************************************************************************/