From 16ceb1a0562816c94eb6fed58c7a9acf2609c353 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Thu, 13 Mar 2008 13:54:46 +0100 Subject: * digital/asserv: - added counter test program and hdl file. --- digital/asserv/src/asserv/Makefile | 2 + digital/asserv/src/asserv/test_counter.c | 111 +++++++++++++++++++++ digital/asserv/src/hdlcounter/counter_index_test.v | 79 +++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 digital/asserv/src/asserv/test_counter.c create mode 100644 digital/asserv/src/hdlcounter/counter_index_test.v diff --git a/digital/asserv/src/asserv/Makefile b/digital/asserv/src/asserv/Makefile index 9e3c10ac..8a946315 100644 --- a/digital/asserv/src/asserv/Makefile +++ b/digital/asserv/src/asserv/Makefile @@ -1,9 +1,11 @@ BASE = ../../../avr PROGS = asserv +AVR_PROGS = test_counter HOST_PROGS = test_motor_model asserv_SOURCES = main.c timer.avr.c counter_ext.avr.c pwm.avr.c pos.c speed.c \ postrack.c traj.c twi_proto.c eeprom.avr.c \ simu.host.c motor_model.host.c models.host.c +test_counter_SOURCES = test_counter.c timer.avr.c test_motor_model_SOURCES = test_motor_model.c motor_model.host.c models.host.c MODULES = proto uart utils math/fixed twi test_motor_model_MODULES = diff --git a/digital/asserv/src/asserv/test_counter.c b/digital/asserv/src/asserv/test_counter.c new file mode 100644 index 00000000..f41d1295 --- /dev/null +++ b/digital/asserv/src/asserv/test_counter.c @@ -0,0 +1,111 @@ +/* test_counter.c */ +/* asserv - Position & speed motor control on AVR. {{{ + * + * Copyright (C) 2008 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * 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. + * + * }}} */ +#include "common.h" +#include "modules/utils/utils.h" +#include "modules/uart/uart.h" +#include "modules/proto/proto.h" + +#include "timer.h" +#include "misc.h" + +#include "counter_ext.avr.c" + +uint8_t read, read_cpt; +uint8_t ind, ind_cpt, ind_init; +uint8_t count, count_cpt; + +int +main (void) +{ + uint8_t old_ind = 0; + const int total = 5000; + LED_SETUP; + timer_init (); + counter_init (); + uart0_init (); + proto_send0 ('z'); + sei (); + while (1) + { + timer_wait (); + if (count) + counter_update (); + if (read && !--read_cpt) + { + proto_send2b ('r', counter_read (0), counter_read (3)); + read_cpt = read; + } + if (ind && !--ind_cpt) + { + uint8_t i = counter_read (3); + if (!ind_init && i != old_ind) + { + uint8_t eip = old_ind + total; + uint8_t eim = old_ind - total; + proto_send7b ('i', old_ind, i, eip, eim, i - eip, i - eim, + i == eip || i == eim); + } + old_ind = i; + ind_init = 0; + ind_cpt = ind; + } + if (count && !--count_cpt) + { + proto_send3w ('C', counter_left, counter_right, counter_aux0); + count_cpt = count; + } + while (uart0_poll ()) + proto_accept (uart0_getc ()); + } +} + +/** Handle incoming messages. */ +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ +#define c(cmd, size) (cmd << 8 | size) + switch (c (cmd, size)) + { + case c ('z', 0): + /* Reset. */ + utils_reset (); + break; + case c ('r', 1): + read_cpt = read = args[0]; + break; + case c ('i', 1): + ind_cpt = ind = args[0]; + ind_init = 1; + break; + case c ('C', 1): + count_cpt = count = args[0]; + break; + default: + proto_send0 ('?'); + return; + } + proto_send (cmd, size, args); +#undef c +} diff --git a/digital/asserv/src/hdlcounter/counter_index_test.v b/digital/asserv/src/hdlcounter/counter_index_test.v new file mode 100644 index 00000000..fcfc2355 --- /dev/null +++ b/digital/asserv/src/hdlcounter/counter_index_test.v @@ -0,0 +1,79 @@ +// counter_index_test.v +// hdlcounter - Incremental encoder counter on programmable logic. {{{ +// +// Copyright (C) 2007 Nicolas Schodet +// +// Robot APB Team 2008. +// Web: http://apbteam.org/ +// Email: team AT apbteam DOT org +// +// 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. +// +// }}} +`timescale 1ns / 1ps + +// This module sample the counter on index pulse. The purpose is to test +// decoding. If the decoding is working, the sampled values should always be +// distant of the total number of steps. + +module counter_top(clk, rst, q0, q1, q2, q3, ale, rd, wr, ad, i0, i1, ioa, + ioc); + parameter size = 8; + input clk; + input rst; + input [1:0] q0, q1, q2, q3; + input ale, rd, wr; + inout [size-1:0] ad; + input i0, i1; + input [3:0] ioa, ioc; + + wire [1:0] qf0; + wire [size-1:0] count0; + + // Decode encoders outputs. + noise_filter f0[1:0] (clk, rst, q0, qf0); + quad_decoder_div4 qd0 (clk, rst, qf0, count0); + + reg [size-1:0] lcount; + reg [size-1:0] sample; + + // Sample on index pulse. + always @(posedge clk or negedge rst) begin + if (!rst) + sample <= 0; + else begin + if (!i0) + sample <= count0; + else + sample <= sample; + end + end + + // Latch a counter when its address is given. + always @(negedge ale or negedge rst) begin + if (!rst) + lcount <= 0; + else begin + lcount <= ad[1:0] == 0 ? count0 : + ad[1:0] == 1 ? 0 : + ad[1:0] == 2 ? 0 : + sample; + end + end + + // Tri-state output unless rd is active (0). + assign ad = rd ? 8'bz : lcount; + +endmodule -- cgit v1.2.3