Andy
please have a look at the attached file. I took supply.c and adjusted it
as a basis to start working from. I am trying to get a pin to toggle in
the update function (run_i2c) but it seems to ignore my pleas. Id I can
get this sorted then I have a good start.
On 2014-05-15 13:48, andy pugh wrote:
On 15 May 2014 12:43, Marius Liebenberg <[email protected]> wrote:
If you consider the pins as local variables to the FUNCTION(_)
function then I believe you can do anything that you would normally
do, but you need to pass the pins to the functions as values or
pointers, not treat them as globals.
I will try this.
The results remain the same. Fault with vec=14, signal=11, ip= xxxxx
Be aware that you can't use the same names in the external function,
as all sorts of macro subsitition goes on.
--
Regards /Groete
Marius D. Liebenberg
+27 82 698 3251
+27 12 743 6064
QQ 1767394877
/********************************************************************
* Description: i2c.c
* This file, 'i2c.c', is a HAL component used to communicate
* with several generic i2c devices.
*
* Author: Marius Liebenberg
* License: GPL Version 2
*
* Copyright (c) 2014 All rights reserved.
*
* Last change:
********************************************************************/
/** This file, 'i2c.c', is a HAL component use to communicate to i2c
devices through general purpose IO pins. It is a realtime component.
*/
/** Copyright (C) 2014 Marius Liebenberg
<marius AT mastercut DOT co DOT za>
*/
/** This program is free software; you can redistribute it and/or
modify it under the terms of version 2 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.
*/
#include "rtapi.h" /* RTAPI realtime OS API */
#include "rtapi_app.h" /* RTAPI realtime module decls */
#include "hal.h" /* HAL public API decls */
/* module information */
MODULE_AUTHOR("Marius Liebenberg");
MODULE_DESCRIPTION("I2C Component for Linuxcnc HAL");
MODULE_LICENSE("GPL");
static int num_chan = 1; /* number of channels - default = 1 */
RTAPI_MP_INT(num_chan, "number of channels");
/***********************************************************************
* STRUCTURES AND GLOBAL VARIABLES *
************************************************************************/
/** This structure contains the runtime data.
*/
typedef struct {
hal_bit_t *SDO; /* pin: serial data out */
hal_bit_t *SDI; /* pin: serial data in */
hal_bit_t *SCL; /* pin: serial clock */
} hal_i2c_t;
/* pointer to supply_t struct */
static hal_i2c_t *i2c_array;
/* other globals */
static int comp_id; /* component ID */
static int tb;
/***********************************************************************
* LOCAL FUNCTION DECLARATIONS *
************************************************************************/
static int export_i2c(int num, hal_i2c_t * addr);
static void run_i2c(void *arg, long l);
/***********************************************************************
* INIT AND EXIT CODE *
************************************************************************/
#define MAX_CHAN 16
int rtapi_app_main(void)
{
int n, retval;
/* test for number of channels */
if ((num_chan <= 0) || (num_chan > MAX_CHAN)) {
rtapi_print_msg(RTAPI_MSG_ERR,
"I2C: ERROR: invalid num_chan: %d\n", num_chan);
return -1;
}
/* have good config info, connect to the HAL */
comp_id = hal_init("i2c");
if (comp_id < 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "I2C: ERROR: hal_init() failed\n");
return -1;
}
/* allocate shared memory for i2c data */
i2c_array = hal_malloc(num_chan * sizeof(hal_i2c_t));
if (i2c_array == 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"I2C: ERROR: hal_malloc() failed\n");
hal_exit(comp_id);
return -1;
}
/* export variables and functions for each driver */
for (n = 0; n < num_chan; n++) {
retval = export_i2c(n, &(i2c_array[n]));
if (retval != 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"I2C: ERROR: var export failed\n");
hal_exit(comp_id);
return -1;
}
}
rtapi_print_msg(RTAPI_MSG_INFO, "I2C:installed %d drivers\n",
num_chan);
hal_ready(comp_id);
return 0;
}
void rtapi_app_exit(void)
{
hal_exit(comp_id);
}
/***********************************************************************
* REALTIME FUNCTIONS *
************************************************************************/
static void run_i2c(void *arg, long l)
{
hal_i2c_t *i2c;
/* point to the data */
i2c = arg;
/* set pin = param */
if(tb == 0){
*(i2c->SCL) = 1;
tb = 1;
}else{
*(i2c->SCL) = 0;
tb = 0;
}
/*
if(*(i2c->SCL) == 0){
*(i2c->SCL) = 1;
}else{
*(i2c->SCL) = 0;
}
*/
/* done */
}
/***********************************************************************
* LOCAL FUNCTION DEFINITIONS *
************************************************************************/
static int export_i2c(int num, hal_i2c_t * addr)
{
int retval;
char buf[HAL_NAME_LEN + 1];
/* export pins */
retval = hal_pin_bit_newf(HAL_OUT, &(addr->SDO), comp_id, "i2c.%d.SDO",
num);
if (retval != 0) {
return retval;
}retval = hal_pin_bit_newf(HAL_OUT, &(addr->SDI), comp_id, "i2c.%d.SDI",
num);
if (retval != 0) {
return retval;
}retval = hal_pin_bit_newf(HAL_OUT, &(addr->SCL), comp_id, "i2c.%d.SCL",
num);
if (retval != 0) {
return retval;
}
/*
retval = hal_pin_bit_newf(HAL_OUT, &(addr->q), comp_id, "i2c.%d.q",
num);
if (retval != 0) {
return retval;
}
retval = hal_pin_bit_newf(HAL_OUT, &(addr->_q), comp_id, "i2c.%d._q", num);
if (retval != 0) {
return retval;
}
retval = hal_pin_float_newf(HAL_OUT, &(addr->variable),
comp_id,"i2c.%d.variable", num);
if (retval != 0) {
return retval;
}
retval = hal_pin_float_newf(HAL_OUT, &(addr->_variable), comp_id,
"i2c.%d._variable", num);
if (retval != 0) {
return retval;
}
*/
/* export parameters */
/*
retval = hal_pin_bit_newf(HAL_IO, &(addr->d), comp_id, "i2c.%d.d", num);
if (retval != 0) {
return retval;
}
retval = hal_pin_float_newf(HAL_IO, &(addr->value), comp_id,
"i2c.%d.value", num);
if (retval != 0) {
return retval;
}
*/
/* init all structure members */
*(addr->SDO) = 0;
*(addr->SDI) = 0;
*(addr->SCL) = 1;
/* export function for this loop */
rtapi_snprintf(buf, sizeof(buf), "i2c.%d.update", num);
retval = hal_export_funct(buf, run_i2c, &(i2c_array[num]), 1, 0, comp_id);
if (retval != 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"I2C: ERROR: update funct export failed\n");
hal_exit(comp_id);
return -1;
}
return 0;
}
------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Emc-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/emc-developers