Skip to content
Snippets Groups Projects
mini.c 5.69 KiB
Newer Older
/******************************************************************************
 *
 *  m i n i . c
 *  Minimal module for EtherCAT.
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
 *
 *  This file is part of the IgH EtherCAT Master.
 *
 *  The IgH EtherCAT Master 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.
 *
 *  The IgH EtherCAT Master 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 the IgH EtherCAT Master; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  The right to use EtherCAT Technology is granted and comes free of
 *  charge under condition of compatibility of product made by
 *  Licensee. People intending to distribute/sell products based on the
 *  code, have to sign an agreement to guarantee that products using
 *  software based on IgH EtherCAT master stay compatible with the actual
 *  EtherCAT specification (which are released themselves as an open
 *  standard) as the (only) precondition to have the right to use EtherCAT
 *  Technology, IP and trade marks.
 *
 *****************************************************************************/

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include "../../include/ecrt.h" // EtherCAT realtime interface
#include "../../include/ecdb.h" // EtherCAT slave database

/*****************************************************************************/

struct timer_list timer;

// EtherCAT
ec_master_t *master = NULL;
ec_domain_t *domain1 = NULL;
spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
ec_pdo_reg_t domain1_pdos[] = {
Florian Pose's avatar
Florian Pose committed
    {"2", Beckhoff_EL4132_Output1, &r_ana_out},
    {"3", Beckhoff_EL5001_Value, NULL},

/*****************************************************************************/
void run(unsigned long data)
    spin_lock(&master_lock);

    ecrt_master_receive(master);
    ecrt_domain_process(domain1);
    //k_pos = EC_READ_U32(r_ssi);

    ecrt_master_run(master);
    ecrt_master_send(master);
    spin_unlock(&master_lock);

    if (counter) {
        counter--;
    }
    else {
        //printk(KERN_INFO "input = ");
        //for (i = 0; i < 22; i++)
        //    printk("%02X ", *((uint8_t *) r_kbus_in + i));
        //printk("\n");
    // restart timer
    timer.expires += HZ / FREQUENCY;
/*****************************************************************************/
int request_lock(void *data)
{
    spin_lock_bh(&master_lock);
    return 0; // access allowed
}

/*****************************************************************************/

void release_lock(void *data)
{
    spin_unlock_bh(&master_lock);
}

/*****************************************************************************/

    printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");

    if ((master = ecrt_request_master(0)) == NULL) {
        printk(KERN_ERR "Requesting master 0 failed!\n");
    ecrt_master_callbacks(master, request_lock, release_lock, NULL);

    if (!(domain1 = ecrt_master_create_domain(master)))
        printk(KERN_ERR "Domain creation failed!\n");
    printk(KERN_INFO "Registering PDOs...\n");
    if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) {
        printk(KERN_ERR "PDO registration failed!\n");
Florian Pose's avatar
Florian Pose committed
    if (!(slave = ecrt_master_get_slave(master, "3")))
        goto out_release_master;

    if (ecrt_slave_conf_sdo8(slave, 0x4061, 1, 0))
        goto out_release_master;

    if (ecrt_master_activate(master)) {
        printk(KERN_ERR "Failed to activate master!\n");
        goto out_release_master;
    }
    ecrt_master_prepare(master);
    printk("Starting cyclic sample thread.\n");
    init_timer(&timer);
    timer.function = run;
    timer.expires = jiffies + 10;
    add_timer(&timer);

    printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
    return 0;

 out_release_master:
    ecrt_release_master(master);
/*****************************************************************************/

void __exit cleanup_mini_module(void)
{
    printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n");

    if (master) {
        ecrt_master_deactivate(master);
        ecrt_release_master(master);
    }

    printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
}

/*****************************************************************************/

MODULE_LICENSE("GPL");
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
MODULE_DESCRIPTION ("EtherCAT minimal test environment");

module_init(init_mini_module);
module_exit(cleanup_mini_module);

/*****************************************************************************/