From 31a36c20a93240394f419769d6891a8d67580513 Mon Sep 17 00:00:00 2001 From: Bastien Barriere Date: Thu, 25 Aug 2011 12:46:25 +0200 Subject: [PATCH] use of incremental encoder index signal for phase table alignment work done in Summer internship of the University of Nantes, France Signed-off-by: Bastien Barriere --- mcu_periph/edge_detector.vhd | 92 ++++++++++++++++++++ msp_motion.prj | 2 +- msp_motion.vhd | 31 ++++++- pwm | 2 +- software/msp430-virtex2-motion/app/motion.c | 43 +++++++++ software/msp430-virtex2-motion/config.target | 2 +- software/submodule/pxmc | 2 +- 7 files changed, 168 insertions(+), 6 deletions(-) create mode 100644 mcu_periph/edge_detector.vhd diff --git a/mcu_periph/edge_detector.vhd b/mcu_periph/edge_detector.vhd new file mode 100644 index 0000000..c84e7de --- /dev/null +++ b/mcu_periph/edge_detector.vhd @@ -0,0 +1,92 @@ +library ieee; +use ieee.std_logic_1164.all; +use ieee.std_logic_arith.all; + +-------------------------------------------------------------------------------- + +entity edge_detector is + generic ( + EDGE_POLARITY : std_logic := '1'; -- Event's edge polarity + W: integer := 16); + port ( + -- Peripheral bus interface + ACK_O : out std_logic; + CLK_I : in std_logic; + DAT_I : in std_logic_vector (W-1 downto 0); + DAT_O : out std_logic_vector (W-1 downto 0); + RST_I : in std_logic; + SEL_I : in std_logic; + STB_I : in std_logic; + WE_I : in std_logic; + -- Event port pins + EVENT_I : in std_logic_vector (W-1 downto 0); + EVENT_O : out std_logic_vector (W-1 downto 0)); +end edge_detector; + +-------------------------------------------------------------------------------- + +architecture behavioral of edge_detector is + + --shared variable last_state : std_logic_vector (W-1 downto 0) := (others => '1'); + signal rising_edge_detect : std_logic := '0'; + signal falling_edge_detect : std_logic := '0'; + signal event_in : std_logic_vector (W-1 downto 0); + signal dff1_in : std_logic_vector (W-1 downto 0); + signal dff1_out : std_logic_vector (W-1 downto 0); + signal dff2_in : std_logic_vector (W-1 downto 0); + signal dff2_out : std_logic_vector (W-1 downto 0); + signal write_en : std_logic; + +begin + +ACK_O <= SEL_I and STB_I; + +write_en <= SEL_I and STB_I and WE_I; + + + +dff1_in <= EVENT_I; + +DAT_O(0) <= rising_edge_detect; --(rising_edge_detect and EDGE_POLARITY) or (falling_edge_detect and not EDGE_POLARITY); + +DAT_O(1) <= falling_edge_detect; + +dff2_in <= dff1_out; + + process (CLK_I) is + begin + if falling_edge(CLK_I) then + if RST_I = '1' then + dff1_out <= (others => '0'); + else + dff1_out <= dff1_in; + end if; + end if; + end process; + + process (CLK_I) is + begin + if rising_edge(CLK_I) then + if RST_I = '1' then + dff2_out <= (others => '0'); + else + dff2_out <= dff2_in; + end if; + end if; + end process; + + + process (CLK_I) is + begin + if rising_edge(CLK_I) then + if RST_I = '1' or write_en = '1' then + rising_edge_detect <= '0'; + falling_edge_detect <= '0'; + elsif dff2_out(0) = '0' and dff1_out(0) = '1' then + rising_edge_detect <= '1'; + elsif dff2_out(0) = '1' and dff1_out(0) = '0' then + falling_edge_detect <= '1'; + end if; + end if; + end process; +end behavioral; diff --git a/msp_motion.prj b/msp_motion.prj index 0dacb38..a02c9fb 100644 --- a/msp_motion.prj +++ b/msp_motion.prj @@ -68,10 +68,10 @@ vhdl work quadcount/qcounter.vhd vhdl work mcu_periph/gpio.vhd vhdl work mcu_periph/qcounter_mcu16.vhd - vhdl work mcu_periph/event_rwc.vhd vhdl work mcu_periph/capture_reg.vhd vhdl work mcu_periph/capture_reg16.vhd +vhdl work mcu_periph/edge_detector.vhd #==============================================================================# # Top-level design file # diff --git a/msp_motion.vhd b/msp_motion.vhd index bdc15de..e5c4cc0 100644 --- a/msp_motion.vhd +++ b/msp_motion.vhd @@ -84,6 +84,14 @@ architecture rtl of msp_motion is signal EVENT_SEL : std_logic; signal event_i : std_logic_vector (15 downto 0); signal event_o : std_logic_vector (15 downto 0); + + -- Edge detector + signal EDGE_DAT_O : std_logic_vector (15 downto 0); + signal EDGE_DETECT_SEL : std_logic; + signal event_in : std_logic_vector (15 downto 0); + signal edge_detect_o : std_logic_vector (15 downto 0); + signal rst_edge : std_logic; + -- Qcounter capture signal CAPTURE_SEL : std_logic; signal CAPTURE_DAT : std_logic_vector (15 downto 0); @@ -206,14 +214,14 @@ begin QCNT_DAT_O when QCNT_SEL = '1' else EVENT_DAT_O when EVENT_SEL = '1' else CAPTURE_DAT when CAPTURE_SEL = '1' else - INDEX_DETECT_DAT_O when INDEX_DETECT_SEL ='1' else + EDGE_DAT_O when EDGE_DETECT_SEL = '1' else (others => '0'); -- MUST be 0 when nothing is addressed GPIO_SEL <= '1' when per_addr(7 downto 2) = 16#0140#/2/4 else '0'; QCNT_SEL <= '1' when per_addr(7 downto 1) = 16#0148#/2/2 else '0'; EVENT_SEL <= '1' when per_addr(7 downto 0) = 16#014C#/2 else '0'; CAPTURE_SEL <= '1' when per_addr(7 downto 1) = 16#0150#/2/2 else '0'; - INDEX_DETECT_SEL <= '1' when per_addr(7 downto 1) = 16#0152#/2/2 else '0'; + EDGE_DETECT_SEL <= '1' when per_addr(7 downto 0) = 16#0154#/2 else '0'; per_wen16 <= per_wen(0) and per_wen(1); @@ -280,6 +288,7 @@ begin count => open, event_ow => MOTOR_IRQ); + event_io_0 : entity work.event_rwc port map ( -- Peripheral bus interface @@ -299,6 +308,24 @@ begin IRC_INDEX_EVENT <= event_i(0); + edge_detector_0 : entity work.edge_detector + port map ( + -- Peripheral bus interface + ACK_O => open, + CLK_I => mclk, + DAT_I => per_din, + DAT_O => EDGE_DAT_O, + RST_I => puc, + SEL_I => EDGE_DETECT_SEL, + STB_I => per_en, + WE_I => per_wen16, + -- Event port pins + EVENT_I => event_in); + + + --rst_edge <= '0' when per_addr() + event_in(0) <= IRC_INDEX_DFF; + irc_index_dff_0 : entity work.dff port map ( clock => mclk, diff --git a/pwm b/pwm index 06ce3f1..4200b5f 160000 --- a/pwm +++ b/pwm @@ -1 +1 @@ -Subproject commit 06ce3f1bffab9080b78cafaad3503263f1f3ee4f +Subproject commit 4200b5f22e5dd50b18a27c3cabd86c005d029333 diff --git a/software/msp430-virtex2-motion/app/motion.c b/software/msp430-virtex2-motion/app/motion.c index 5a68857..a9aa484 100644 --- a/software/msp430-virtex2-motion/app/motion.c +++ b/software/msp430-virtex2-motion/app/motion.c @@ -1,8 +1,10 @@ #include +#include #include #include #include #include "msp430.h" + #include "pxmc_virtex2.h" #define MOTOR_VECTOR 0 @@ -25,11 +27,52 @@ cmd_des_t const cmd_des_val={ "VAL","use ':' or '?' to store/read value of an integer variable", cmd_do_rw_int, {(char*)&val}}; +cmd_des_t const cmd_des_halpos={0, CDESM_OPCHR|CDESM_RW, + "HALPOS?","HAL phase",cmd_do_reg_short_val, + {(char*)pxmc_state_offs(pxms_hal), + 0}}; + + + +int cmd_do_mccpwm(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) +{ + char *p; + long val,angle; + pxmc_state_t *mcs; + if(*param[2]!=':') return -CMDERR_OPCHAR; + + if((mcs=cmd_opchar_getreg(cmd_io,des,param))==NULL) return -CMDERR_BADREG; + + p=param[3]; + if(si_long(&p,&val,0)<0) return -CMDERR_BADPAR; + if(si_fndsep(&p,",")<0) return -CMDERR_BADSEP; + if(si_long(&p,&angle,0)<0) return -CMDERR_BADPAR; + si_skspace(&p); + if(*p) return -CMDERR_GARBAG; + printf("ptindx %d HAL %d\n\r",mcs->pxms_ptindx, pxmc_bdc_hal_rd(mcs)); + //printf("HAL position: %d\n",(char*)pxmc_state_offs(pxms_hal)); + pxmc_virtex2_set_valangle(mcs, val, angle); + + return 0; +} + +cmd_des_t const cmd_des_mccpwm={0, CDESM_OPCHR|CDESM_RW, + "MCCPWM?","set PWM vector value and angle",cmd_do_mccpwm, + {0}}; + +/*cmd_des_t const cmd_des_db={0, CDESM_OPCHR, + "DB","DB test",cmd_do_db, + {0, + 0}}; +*/ + cmd_des_t const *cmd_pxmc_list[] = { &cmd_des_help, &cmd_des_val, + &cmd_des_halpos, + &cmd_des_mccpwm, CMD_DES_INCLUDE_SUBLIST(cmd_pxmc_base), CMD_DES_INCLUDE_SUBLIST(cmd_pxmc_deb), NULL diff --git a/software/msp430-virtex2-motion/config.target b/software/msp430-virtex2-motion/config.target index b9105bd..1125023 100644 --- a/software/msp430-virtex2-motion/config.target +++ b/software/msp430-virtex2-motion/config.target @@ -19,7 +19,7 @@ TARGET_STDSTARTFILES = y # Set default C flags. If theese are set elsewhere (e.g. on a command # line), these default flags are not used. DEBUG ?= -ggdb -OPTIMIZE ?= -O2 +OPTIMIZE ?= -O0 # This selects linker script LD_SCRIPT=openmsp430-sstub diff --git a/software/submodule/pxmc b/software/submodule/pxmc index 59e381d..63b224c 160000 --- a/software/submodule/pxmc +++ b/software/submodule/pxmc @@ -1 +1 @@ -Subproject commit 59e381da4089d3aa08de1c408199121cc9cc424c +Subproject commit 63b224cc285fd31abeefab3ad7ac4822c1276a8b -- 2.39.2