// This is a component for EMC2 HAL // Copyright 2006 Jeff Epler // // 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA component edge "Edge detector"; pin in bit in; pin out bit out "Goes high when the desired edge is seen on 'in'"; pin out bit out_invert "Goes low when the desired edge is seen on 'in'"; param rw bit both=FALSE "If TRUE, selects both edges. Otherwise, selects one edge according to in-edge"; param rw bit in_edge=TRUE "If both is FALSE, selects the one desired edge: TRUE means falling, FALSE means rising"; param rw signed out_width_ns=0 "Time in nanoseconds of the output pulse"; param r signed time_left_ns "Time left in this output pulse"; param r bit last_in "Previous input value"; variable int first = 1; function _ nofp "Produce output pulses from input edges"; license "GPL"; ;; FUNCTION(_){ int new_in = in; if (time_left_ns > 0) { time_left_ns -= period; } if ( !first ) { int rise = new_in && !last_in; int fall = !new_in && last_in; int desired_edge = both ? rise || fall : in_edge ? fall : rise; if(desired_edge) { time_left_ns = out_width_ns; out = 1; } else if(time_left_ns > 0) { out = 1; } else { time_left_ns = 0; out = 0; } } else { first = 0; } last_in = new_in; out_invert = !out; }