1 /*******************************************************************
2 Motion and Robotic System (MARS) aplication components.
4 appl_pxmc.c - position controller subsystem core generic
5 and LP_MPW1 hardware specific support
7 Copyright (C) 2001-2013 by Pavel Pisa - originator
9 (C) 2001-2013 by PiKRON Ltd. - originator
12 This file can be used and copied according to next
14 - GPL - GNU Public License
15 - other license provided by project originators
17 *******************************************************************/
20 #include <system_def.h>
22 #include <pxmc_lpc_qei.h>
23 #include <pxmc_internal.h>
24 #include <pxmc_inp_common.h>
25 #include <pxmc_gen_info.h>
27 #include <hal_machperiph.h>
33 #include "appl_defs.h"
34 #include "appl_fpga.h"
36 int pxmc_ptofs_from_index(pxmc_state_t *mcs, unsigned long irc,
37 unsigned long index_irc, int diff2err);
39 #ifndef pxmc_fast_tick_time
40 #define pxmc_fast_tick_time() (LPC_TIM0->TC)
43 #define PXML_MAIN_CNT 8
45 #define PXMC_WITH_PT_ZIC 1
46 #define PXMC_PT_ZIC_MASK 0x8000
48 #define LPCPWM1_FREQ 20000
50 #define HAL_ERR_SENSITIVITY 20
51 #define HAL_ERR_MAX_COUNT 5
53 #define LXPWR_WITH_SIROLADC 1
55 #define LX_MASTER_DATA_OFFS 8
57 unsigned pxmc_rocon_pwm_magnitude = 2500;
59 long pxmc_rocon_irc_offset[PXML_MAIN_CNT];
60 unsigned pxmc_rocon_mark_filt[PXML_MAIN_CNT];
62 const uint8_t onesin10bits[1024]={
63 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
64 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
65 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
66 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
67 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
68 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
69 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
70 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
71 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
72 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
73 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
74 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
75 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
76 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
77 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
78 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9,
79 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
80 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
81 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
82 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
83 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
84 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
85 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
86 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9,
87 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
88 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
89 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
90 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9,
91 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
92 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9,
93 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9,
94 5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9,6,7,7,8,7,8,8,9,7,8,8,9,8,9,9,10
98 pxmc_inp_rocon_inp(struct pxmc_state *mcs)
100 int chan=mcs->pxms_inp_info;
104 /* read position from hardware */
105 irc = fpga_irc[chan]->count;
106 irc += pxmc_rocon_irc_offset[chan];
107 pos = irc << PXMC_SUBDIV(mcs);
108 mcs->pxms_as = pos - mcs->pxms_ap;
111 /* Running of the motor commutator */
112 if (mcs->pxms_flg & PXMS_PTI_m)
113 pxmc_irc_16bit_commindx(mcs, irc);
119 pxmc_inp_rocon_is_mark(pxmc_state_t *mcs)
124 int chan=mcs->pxms_inp_info;
126 irc_state = *fpga_irc_state[chan];
128 mark = ((irc_state >> (ffs(FPGA_IRC_STATE_MARK_MASK) - 1)) ^
129 (mcs->pxms_cfg >> PXMS_CFG_HPS_b)) & 1;
131 filt = pxmc_rocon_mark_filt[chan];
132 filt = (filt << 1) | mark;
133 pxmc_rocon_mark_filt[chan] = filt;
135 return onesin10bits[filt & 0x03ff];
139 pxmc_inp_rocon_is_index_edge(pxmc_state_t *mcs)
143 int chan=mcs->pxms_inp_info;
145 irc_state = *fpga_irc_state[chan];
146 *fpga_irc_state[chan] = FPGA_IRC_STATE_INDEX_EVENT_MASK;
148 index = (irc_state >> (ffs(FPGA_IRC_STATE_INDEX_EVENT_MASK) - 1)) & 1;
154 pxmc_inp_rocon_adjust_to_irc_change(struct pxmc_state *mcs, long irc_diff)
156 long pos_diff = irc_diff << PXMC_SUBDIV(mcs);
158 mcs->pxms_ptofs += irc_diff;
160 mcs->pxms_ap += pos_diff;
161 mcs->pxms_rp += pos_diff;
166 pxmc_inp_rocon_irc_offset_from_index(struct pxmc_state *mcs)
168 int chan=mcs->pxms_inp_info;
172 irc_offset = -fpga_irc[chan]->count_index;
173 irc_diff = irc_offset - pxmc_rocon_irc_offset[chan];
174 pxmc_rocon_irc_offset[chan] = irc_offset;
175 return pxmc_inp_rocon_adjust_to_irc_change(mcs, irc_diff);
179 pxmc_inp_rocon_ap_zero(struct pxmc_state *mcs)
181 int chan=mcs->pxms_inp_info;
185 irc_offset = -fpga_irc[chan]->count;
186 irc_diff = irc_offset - pxmc_rocon_irc_offset[chan];
187 pxmc_rocon_irc_offset[chan] = irc_offset;
188 return pxmc_inp_rocon_adjust_to_irc_change(mcs, irc_diff);
192 pxmc_inp_rocon_ap2hw(struct pxmc_state *mcs)
194 int chan=mcs->pxms_inp_info;
198 irc = fpga_irc[chan]->count;
199 pos_diff = mcs->pxms_ap - (irc << PXMC_SUBDIV(mcs));
201 irc = pos_diff >> PXMC_SUBDIV(mcs);
203 /* Adjust phase table alignemt to modified IRC readout */
204 mcs->pxms_ptofs += irc - pxmc_rocon_irc_offset[chan];
206 pxmc_rocon_irc_offset[chan] = irc;
211 pxmc_inp_rocon_ptofs_from_index_poll(struct pxmc_state *mcs, int diff2err)
213 int chan=mcs->pxms_inp_info;
217 if (!(*fpga_irc_state[chan] & FPGA_IRC_STATE_INDEX_EVENT_MASK))
220 irc = fpga_irc[chan]->count + pxmc_rocon_irc_offset[chan];
221 index_irc = fpga_irc[chan]->count_index + pxmc_rocon_irc_offset[chan];
223 return pxmc_ptofs_from_index(mcs, irc, index_irc, diff2err);
226 uint32_t pxmc_rocon_receiver_dummy_reg;
228 static inline volatile uint32_t *
229 pxmc_rocon_receiver_chan2reg(unsigned chan)
231 volatile uint32_t *rec_reg;
234 return &pxmc_rocon_receiver_dummy_reg;
236 rec_reg = fpga_lx_master_receiver_base;
238 #ifdef LXPWR_WITH_SIROLADC
239 rec_reg += LX_MASTER_DATA_OFFS + 1 + (chan >> 8) * 3 + chan * 2;
240 #else /*LXPWR_WITH_SIROLADC*/
241 rec_reg += LX_MASTER_DATA_OFFS + chan;
242 #endif /*LXPWR_WITH_SIROLADC*/
248 pxmc_rocon_bldc_hal_rd(pxmc_state_t *mcs)
251 volatile uint32_t *rec_reg_a, *rec_reg_b, *rec_reg_c;
252 int chan = mcs->pxms_out_info;
255 #ifdef LXPWR_WITH_SIROLADC
257 #else /*LXPWR_WITH_SIROLADC*/
259 #endif /*LXPWR_WITH_SIROLADC*/
261 rec_reg_a = pxmc_rocon_receiver_chan2reg(chan + 0);
262 rec_reg_b = pxmc_rocon_receiver_chan2reg(chan + 1);
263 rec_reg_c = pxmc_rocon_receiver_chan2reg(chan + 2);
265 h = (rec_reg_a[hal_offs] >> 14) & 1;
266 h |= (rec_reg_b[hal_offs] >> 13) & 2;
267 h |= (rec_reg_c[hal_offs] >> 12) & 4;
269 /* return 3 bits corresponding to the HAL senzor input */
274 const unsigned char pxmc_lpc_bdc_hal_pos_table[8] =
286 const unsigned char pxmc_lpc_bdc_hal_pos_table[8] =
299 uint32_t pxmc_rocon_pwm_dummy_reg;
301 static inline volatile uint32_t *
302 pxmc_rocon_pwm_chan2reg(unsigned chan)
304 volatile uint32_t *pwm_reg;
307 return &pxmc_rocon_pwm_dummy_reg;
309 pwm_reg = fpga_lx_master_transmitter_base;
311 #ifdef LXPWR_WITH_SIROLADC
312 pwm_reg += LX_MASTER_DATA_OFFS + 1 + (chan >> 8) + chan;
313 #else /*LXPWR_WITH_SIROLADC*/
314 pwm_reg += LX_MASTER_DATA_OFFS + chan;
315 #endif /*LXPWR_WITH_SIROLADC*/
321 * pxmc_rocon_pwm3ph_wr - Output of the 3-phase PWM to the hardware
322 * @mcs: Motion controller state information
324 /*static*/ inline void
325 pxmc_rocon_pwm3ph_wr(pxmc_state_t *mcs, short pwm1, short pwm2, short pwm3)
327 volatile uint32_t *pwm_reg_a, *pwm_reg_b, *pwm_reg_c;
328 int chan = mcs->pxms_out_info;
330 pwm_reg_c = pxmc_rocon_pwm_chan2reg(chan + 0);
331 pwm_reg_b = pxmc_rocon_pwm_chan2reg(chan + 1);
332 pwm_reg_a = pxmc_rocon_pwm_chan2reg(chan + 2);
340 pxmc_rocon_process_hal_error(struct pxmc_state *mcs)
342 if (mcs->pxms_halerc >= HAL_ERR_SENSITIVITY * HAL_ERR_MAX_COUNT)
344 pxmc_set_errno(mcs, PXMS_E_HAL);
349 mcs->pxms_halerc += HAL_ERR_SENSITIVITY;
353 * pxmc_rocon_pwm3ph_out - Phase output for brush-less 3-phase motor
354 * @mcs: Motion controller state information
357 pxmc_rocon_pwm3ph_out(pxmc_state_t *mcs)
359 typeof(mcs->pxms_ptvang) ptvang;
361 unsigned char hal_pos;
369 if (!(mcs->pxms_flg & PXMS_PTI_m) || !(mcs->pxms_flg & PXMS_PHA_m) ||
370 (mcs->pxms_flg & PXMS_PRA_m))
373 short ptirc = mcs->pxms_ptirc;
374 short divisor = mcs->pxms_ptper * 6;
377 pxmc_irc_16bit_commindx(mcs, mcs->pxms_rp >> PXMC_SUBDIV(mcs));
382 res = pxmc_inp_rocon_ptofs_from_index_poll(mcs, 0);
384 pxmc_set_errno(mcs, PXMS_E_I2PT_TOOBIG);
386 pxmc_set_flag(mcs, PXMS_PTI_b);
387 pxmc_set_flag(mcs, PXMS_PHA_b);
392 hal_pos = pxmc_lpc_bdc_hal_pos_table[pxmc_rocon_bldc_hal_rd(mcs)];
397 pxmc_rocon_process_hal_error(mcs);
401 if (mcs->pxms_halerc)
404 ptindx = (hal_pos * ptirc + divisor / 2) / divisor;
406 if (!(mcs->pxms_flg & PXMS_PTI_m) || (mcs->pxms_flg & PXMS_PRA_m))
408 if (((hal_pos != mcs->pxms_hal) && (mcs->pxms_hal != 0x40)) && 1)
410 short ptindx_prev = (mcs->pxms_hal * ptirc + divisor / 2) / divisor;;
412 if ((ptindx > ptindx_prev + ptirc / 2) ||
413 (ptindx_prev > ptindx + ptirc / 2))
415 ptindx = (ptindx_prev + ptindx - ptirc) / 2;
422 ptindx = (ptindx_prev + ptindx) / 2;
425 mcs->pxms_ptindx = ptindx;
427 mcs->pxms_ptofs = (mcs->pxms_ap >> PXMC_SUBDIV(mcs)) + mcs->pxms_ptshift - ptindx;
429 pxmc_set_flag(mcs, PXMS_PTI_b);
430 pxmc_clear_flag(mcs, PXMS_PRA_b);
434 if (!(mcs->pxms_flg & PXMS_PTI_m))
435 mcs->pxms_ptindx = ptindx;
438 /* if phase table position to mask is know do fine phase table alignment */
439 if (mcs->pxms_cfg & PXMS_CFG_I2PT_m) {
441 res = pxmc_inp_rocon_ptofs_from_index_poll(mcs, 0);
443 pxmc_set_errno(mcs, PXMS_E_I2PT_TOOBIG);
445 pxmc_set_flag(mcs, PXMS_PTI_b);
446 pxmc_set_flag(mcs, PXMS_PHA_b);
450 mcs->pxms_hal = hal_pos;
456 /*wind_current[0]=(ADC->ADDR0 & 0xFFF0)>>4;*/
457 /* FIXME - check winding current against limit */
458 /* pxmc_set_errno(mcs, PXMS_E_WINDCURRENT); */
462 ptvang = mcs->pxms_ptvang;
466 ene = mcs->pxms_flg & PXMS_BSY_m? mcs->pxms_me: 0;
470 indx = mcs->pxms_ptindx;
472 /* tuning of magnetic field/voltage advance angle */
473 indx += (mcs->pxms_s1 * mcs->pxms_as) >> (PXMC_SUBDIV(mcs) + 8);
478 /* Generating direction of stator mag. field for backward torque */
481 if ((indx -= ptvang) < 0)
482 indx += mcs->pxms_ptirc;
486 /* Generating direction of stator mag. field for forward torque */
487 if ((indx += ptvang) >= mcs->pxms_ptirc)
488 indx -= mcs->pxms_ptirc;
491 if (mcs->pxms_ptscale_mult)
492 indx = ((unsigned long)indx * mcs->pxms_ptscale_mult) >> mcs->pxms_ptscale_shift;
494 pwm1 = mcs->pxms_ptptr1[indx];
495 pwm2 = mcs->pxms_ptptr2[indx];
496 pwm3 = mcs->pxms_ptptr3[indx];
498 #ifdef PXMC_WITH_PT_ZIC
499 if (labs(mcs->pxms_as) < (10 << PXMC_SUBDIV(mcs)))
501 pwm1 &= ~PXMC_PT_ZIC_MASK;
502 pwm2 &= ~PXMC_PT_ZIC_MASK;
503 pwm3 &= ~PXMC_PT_ZIC_MASK;
505 #endif /*PXMC_WITH_PT_ZIC*/
507 /* Default phase-table amplitude is 0x7fff, ene max is 0x7fff */
508 /* Initialized CTM4 PWM period is 0x200 => divide by value about 2097024 */
510 unsigned long pwm_dc = pxmc_rocon_pwm_magnitude * (unsigned long)ene;
511 if (pwm1 & PXMC_PT_ZIC_MASK)
514 pwm1 = (((unsigned long long)pwm1 * pwm_dc) >> (15 + 15)) | 0x4000;
515 if (pwm2 & PXMC_PT_ZIC_MASK)
518 pwm2 = (((unsigned long long)pwm2 * pwm_dc) >> (15 + 15)) | 0x4000;
519 if (pwm3 & PXMC_PT_ZIC_MASK)
522 pwm3 = (((unsigned long long)pwm3 * pwm_dc) >> (15 + 15)) | 0x4000;
524 pxmc_rocon_pwm3ph_wr(mcs, pwm1, pwm2, pwm3);
528 pxmc_rocon_pwm3ph_wr(mcs, 0, 0, 0);
535 * pxmc_rocon_pwm2ph_wr - Output of the 2-phase stepper motor PWM to the hardware
536 * @mcs: Motion controller state information
538 /*static*/ inline void
539 pxmc_rocon_pwm2ph_wr(pxmc_state_t *mcs, short pwm1, short pwm2)
541 volatile uint32_t *pwm_reg_ap, *pwm_reg_an, *pwm_reg_bp, *pwm_reg_bn;
542 int chan = mcs->pxms_out_info;
544 pwm_reg_bn = pxmc_rocon_pwm_chan2reg(chan + 0);
545 pwm_reg_bp = pxmc_rocon_pwm_chan2reg(chan + 1);
546 pwm_reg_an = pxmc_rocon_pwm_chan2reg(chan + 2);
547 pwm_reg_ap = pxmc_rocon_pwm_chan2reg(chan + 3);
550 *pwm_reg_bp = pwm2 | 0x4000;
554 *pwm_reg_bn = -pwm2 | 0x4000;
558 *pwm_reg_ap = pwm1 | 0x4000;
562 *pwm_reg_an = -pwm1 | 0x4000;
567 * pxmc_rocon_pwm2ph_out - Phase output of the 2-phase stepper motor PWM
568 * @mcs: Motion controller state information
571 pxmc_rocon_pwm2ph_out(pxmc_state_t *mcs)
573 typeof(mcs->pxms_ptvang) ptvang;
581 if(!(mcs->pxms_flg&PXMS_PTI_m) || !(mcs->pxms_flg&PXMS_PHA_m) ||
582 (mcs->pxms_flg&PXMS_PRA_m)){
584 short ptirc=mcs->pxms_ptirc;
587 pxmc_irc_16bit_commindx(mcs, mcs->pxms_rp >> PXMC_SUBDIV(mcs));
592 res = pxmc_inp_rocon_ptofs_from_index_poll(mcs, 0);
594 pxmc_set_errno(mcs, PXMS_E_I2PT_TOOBIG);
596 pxmc_set_flag(mcs, PXMS_PTI_b);
597 pxmc_set_flag(mcs, PXMS_PHA_b);
602 ptindx = mcs->pxms_ptindx;
604 if(0 && (!(mcs->pxms_flg&PXMS_PTI_m) || (mcs->pxms_flg&PXMS_PRA_m))) {
606 mcs->pxms_ptindx = ptindx;
608 mcs->pxms_ptofs = (mcs->pxms_ap >> PXMC_SUBDIV(mcs)) + mcs->pxms_ptshift - ptindx;
610 pxmc_set_flag(mcs, PXMS_PTI_b);
611 pxmc_clear_flag(mcs, PXMS_PRA_b);
613 /* if phase table position to mask is know do fine phase table alignment */
614 //if(mcs->pxms_cfg & PXMS_CFG_I2PT_m)
615 // lpc_qei_setup_index_catch(&lpc_qei_state);
618 if(!(mcs->pxms_flg&PXMS_PTI_m))
619 mcs->pxms_ptindx = ptindx;
625 /*wind_current[0]=(ADC->ADDR0 & 0xFFF0)>>4;*/
626 /* FIXME - check winding current against limit */
627 /* pxmc_set_errno(mcs, PXMS_E_WINDCURRENT); */
631 ptvang = mcs->pxms_ptvang;
635 ene = mcs->pxms_flg & PXMS_BSY_m? mcs->pxms_me: 0;
639 indx = mcs->pxms_ptindx;
641 /* tuning of magnetic field/voltage advance angle */
642 indx += (mcs->pxms_s1 * mcs->pxms_as) >> (PXMC_SUBDIV(mcs) + 8);
645 /* Generating direction of stator mag. field for backward torque */
647 if ((indx -= ptvang)<0)
648 indx += mcs->pxms_ptirc;
650 /* Generating direction of stator mag. field for forward torque */
651 if ((indx += ptvang) >= mcs->pxms_ptirc)
652 indx -= mcs->pxms_ptirc;
655 if (mcs->pxms_ptscale_mult)
656 indx = ((unsigned long)indx * mcs->pxms_ptscale_mult) >> mcs->pxms_ptscale_shift;
658 pwm1 = mcs->pxms_ptptr1[indx];
659 pwm2 = mcs->pxms_ptptr2[indx];
661 /* Default phase-table amplitude is 0x7fff, ene max is 0x7fff */
662 /* Initialized PWM period is 0x200 => divide by value about 2097024 */
664 unsigned long pwm_dc = pxmc_rocon_pwm_magnitude * (unsigned long)ene;
665 pwm1 = ((unsigned long long)pwm1 * pwm_dc) >> (15+15);
666 pwm2 = ((unsigned long long)pwm2 * pwm_dc) >> (15+15);
668 pxmc_rocon_pwm2ph_wr(mcs, pwm1, pwm2);
670 /*pxmc_lpc_wind_current_over_cnt = 0;*/
671 pxmc_rocon_pwm2ph_wr(mcs, 0, 0);
678 * pxmc_rocon_pwm_dc_out - DC motor CW and CCW PWM output
679 * @mcs: Motion controller state information
682 pxmc_rocon_pwm_dc_out(pxmc_state_t *mcs)
684 volatile uint32_t *pwm_reg_a, *pwm_reg_b;
685 int chan = mcs->pxms_out_info;
686 int ene = mcs->pxms_ene;
688 pwm_reg_a = pxmc_rocon_pwm_chan2reg(chan + 0);
689 pwm_reg_b = pxmc_rocon_pwm_chan2reg(chan + 1);
695 ene = (ene * (pxmc_rocon_pwm_magnitude + 5)) >> 15;
697 *pwm_reg_b = ene | 0x4000;
701 ene = (ene * (pxmc_rocon_pwm_magnitude + 5)) >> 15;
703 *pwm_reg_a = ene | 0x4000;
709 volatile void *pxmc_rocon_rx_data_hist_buff;
710 volatile void *pxmc_rocon_rx_data_hist_buff_end;
712 uint32_t pxmc_rocon_rx_last_irq;
713 uint32_t pxmc_rocon_rx_cycle_time;
714 uint32_t pxmc_rocon_rx_irq_latency;
715 uint32_t pxmc_rocon_rx_irq_latency_max;
717 IRQ_HANDLER_FNC(pxmc_rocon_rx_done_isr)
721 ir = ROCON_RX_TIM->IR & LPC_TIM_IR_ALL_m;
722 ROCON_RX_TIM->IR = ir;
723 if (ir & LPC_TIM_IR_CR1INT_m) {
725 cr0 = ROCON_RX_TIM->CR0;
726 cr1 = ROCON_RX_TIM->CR1;
728 pxmc_rocon_rx_cycle_time = cr1 - pxmc_rocon_rx_last_irq;
729 pxmc_rocon_rx_last_irq = cr1;
731 hal_gpio_set_value(T2MAT0_PIN, 1);
732 hal_gpio_set_value(T2MAT1_PIN, 0);
733 hal_gpio_set_value(T2MAT0_PIN, 0);
735 if (pxmc_rocon_rx_data_hist_buff >= pxmc_rocon_rx_data_hist_buff_end)
736 pxmc_rocon_rx_data_hist_buff = NULL;
738 if (pxmc_rocon_rx_data_hist_buff != NULL) {
740 volatile uint32_t *pwm_reg = fpga_lx_master_transmitter_base + 8;
741 volatile uint32_t *rec_reg = fpga_lx_master_receiver_base + 8;
742 uint16_t *pbuf = (uint16_t *)pxmc_rocon_rx_data_hist_buff;
743 for (i = 0; i < 8; i++) {
744 *(pbuf++) = *(rec_reg++);
746 for (i = 0; i < 8; i++) {
747 *(pbuf++) = *(pwm_reg++);
749 pxmc_rocon_rx_data_hist_buff = pbuf;
752 pxmc_rocon_rx_irq_latency = ROCON_RX_TIM->TC - cr1;
753 if (pxmc_rocon_rx_irq_latency > pxmc_rocon_rx_irq_latency_max)
754 pxmc_rocon_rx_irq_latency_max = pxmc_rocon_rx_irq_latency;
756 #ifdef PXMC_ROCON_TIMED_BY_RX_DONE
759 #endif /*PXMC_ROCON_TIMED_BY_RX_DONE*/
766 pxmc_rocon_rx_done_isr_setup(irq_handler_t rx_done_isr_handler)
769 disable_irq(ROCON_RX_IRQn);
771 hal_pin_conf_set(T2MAT0_PIN, PORT_CONF_GPIO_OUT_LO);
772 hal_pin_conf_set(T2MAT1_PIN, PORT_CONF_GPIO_OUT_LO);
773 hal_pin_conf(T2CAP0_PIN);
774 hal_pin_conf(T2CAP1_PIN);
776 hal_gpio_direction_output(T2MAT0_PIN, 1);
777 hal_gpio_direction_output(T2MAT1_PIN, 0);
778 hal_gpio_set_value(T2MAT0_PIN, 0);
780 /* Enable CLKOUT pin function, source CCLK, divide by 1 */
781 LPC_SC->CLKOUTCFG = 0x0100;
783 request_irq(ROCON_RX_IRQn, rx_done_isr_handler, 0, NULL,NULL);
785 ROCON_RX_TIM->TCR = 0;
786 ROCON_RX_TIM->CTCR = 0;
787 ROCON_RX_TIM->PR = 0; /* Divide by 1 */
789 ROCON_RX_TIM->CCR = LPC_TIM_CCR_CAP0RE_m | LPC_TIM_CCR_CAP1FE_m |
792 ROCON_RX_TIM->EMR = __val2mfld(LPC_TIM_EMR_EMC0_m, LPC_TIM_EMR_NOP) |
793 __val2mfld(LPC_TIM_EMR_EMC1_m, LPC_TIM_EMR_NOP);
795 ROCON_RX_TIM->MCR = 0; /* No IRQ on MRx */
796 ROCON_RX_TIM->TCR = LPC_TIM_TCR_CEN_m; /* Enable timer counting */
797 enable_irq(ROCON_RX_IRQn); /* Enable interrupt */
804 pxmc_rocon_pwm_master_init(void)
810 unsigned receiver_done_div = 1;
811 #ifdef LXPWR_WITH_SIROLADC
812 unsigned lxpwr_header = 1;
813 unsigned lxpwr_words = 1 + 8 * 2 + 2;
814 unsigned lxpwr_chips = 2;
815 unsigned lxpwr_chip_pwm_cnt = 8;
816 #else /*LXPWR_WITH_SIROLADC*/
817 unsigned lxpwr_header = 0;
818 unsigned lxpwr_words = 8;
819 unsigned lxpwr_chips = 2;
820 unsigned lxpwr_chip_pwm_cnt = 8;
821 #endif /*LXPWR_WITH_SIROLADC*/
823 #ifdef PXMC_ROCON_TIMED_BY_RX_DONE
824 receiver_done_div = 5;
825 #endif /*PXMC_ROCON_TIMED_BY_RX_DONE*/
827 *fpga_lx_master_reset = 1;
828 *fpga_lx_master_transmitter_reg = 0;
829 *fpga_lx_master_transmitter_cycle = 2500; /* 50 MHz -> 20 kHz */
830 *fpga_lx_master_receiver_done_div = receiver_done_div << 8;
832 for (i = 0; i < LX_MASTER_DATA_OFFS + lxpwr_words * lxpwr_chips; i++)
833 fpga_lx_master_receiver_base[i] = 0;
835 word_slot = LX_MASTER_DATA_OFFS;
836 fpga_lx_master_receiver_base[grp_in++] = (word_slot << 8) | lxpwr_words;
837 fpga_lx_master_receiver_base[grp_in++] = 0x0000;
839 for (i = 0; i < LX_MASTER_DATA_OFFS + lxpwr_words * lxpwr_chips; i++)
840 fpga_lx_master_transmitter_base[i] = 0;
842 word_slot = LX_MASTER_DATA_OFFS + lxpwr_header + lxpwr_chip_pwm_cnt;
843 fpga_lx_master_transmitter_base[grp_out++] = (word_slot << 8) | lxpwr_words;
844 #ifdef LXPWR_WITH_SIROLADC
845 fpga_lx_master_transmitter_base[word_slot] = 0xc100 | (lxpwr_words - 1);
846 #endif /*LXPWR_WITH_SIROLADC*/
848 word_slot = LX_MASTER_DATA_OFFS + 0;
849 fpga_lx_master_transmitter_base[grp_out++] = (word_slot << 8) | lxpwr_words;
850 #ifdef LXPWR_WITH_SIROLADC
851 fpga_lx_master_transmitter_base[word_slot] = 0xc100 | (lxpwr_words - 1);
852 #endif /*LXPWR_WITH_SIROLADC*/
854 fpga_lx_master_transmitter_base[grp_out++] = 0x0000;
856 *fpga_lx_master_reset = 0;
857 *fpga_lx_master_transmitter_cycle = 2500; /* 50 MHz -> 20 kHz */
858 *fpga_lx_master_receiver_done_div = receiver_done_div << 8;
863 int pxmc_ptofs_from_index(pxmc_state_t *mcs, unsigned long irc,
864 unsigned long index_irc, int diff2err)
869 ofs = ofsl = index_irc - mcs->pxms_ptmark;
873 diff = (unsigned short)ofs - (unsigned short)mcs->pxms_ptofs;
874 if (diff >= mcs->pxms_ptirc / 2)
875 diff -= mcs->pxms_ptirc;
876 if (diff <= -mcs->pxms_ptirc / 2)
877 diff += mcs->pxms_ptirc;
880 if(diff >= mcs->pxms_ptirc / 6) {
885 diff = (unsigned long)ofsl - irc;
886 ofs = ofsl - (diff / mcs->pxms_ptirc) * mcs->pxms_ptirc;
889 mcs->pxms_ptofs = ofs;
895 * pxmc_dummy_con - Dummy controller for synchronous BLDC/PMSM/steper drive
896 * @mcs: Motion controller state information
899 pxmc_dummy_con(pxmc_state_t *mcs)
904 int pxmc_rocon_hh_gd10(pxmc_state_t *mcs);
905 int pxmc_rocon_hh_gd20(pxmc_state_t *mcs);
906 int pxmc_rocon_hh_gd30(pxmc_state_t *mcs);
907 int pxmc_rocon_hh_gd40(pxmc_state_t *mcs);
908 int pxmc_rocon_hh_gd50(pxmc_state_t *mcs);
909 int pxmc_rocon_hh_gd90(pxmc_state_t *mcs);
911 /* initialize for hard home */
913 pxmc_rocon_hh_gi(pxmc_state_t *mcs)
915 pxmc_set_flag(mcs,PXMS_BSY_b);
916 #if 0 /* config and speed already set in pxmc_hh */
918 spd >>= mcs->pxms_cfg & PXMS_CFG_HSPD_m;
920 if(mcs->pxms_cfg & PXMS_CFG_HDIR_m)
922 mcs->pxms_gen_hsp = spd;
925 mcs->pxms_gen_htim = 16;
926 mcs->pxms_do_gen = pxmc_rocon_hh_gd10;
927 return pxmc_rocon_hh_gd10(mcs);
930 /* fill initial mark filter and then decide */
932 pxmc_rocon_hh_gd10(pxmc_state_t *mcs)
936 if(mcs->pxms_flg & PXMS_ERR_m)
937 return pxmc_spdnext_gend(mcs);
939 pxmc_spd_gacc(&(mcs->pxms_rs), 0, mcs->pxms_ma);
940 mcs->pxms_rp += mcs->pxms_rs;
942 mark = pxmc_inp_rocon_is_mark(mcs);
944 if (mcs->pxms_gen_htim) {
945 mcs->pxms_gen_htim--;
949 if (!(mcs->pxms_cfg & PXMS_CFG_HLS_m)) {
950 /* limit switch is not used */
951 pxmc_inp_rocon_is_index_edge(mcs);
952 mcs->pxms_do_gen = pxmc_rocon_hh_gd50;
957 /* go out from switch */
958 mcs->pxms_do_gen = pxmc_rocon_hh_gd20;
960 /* switch is off -> look for it */
961 mcs->pxms_do_gen = pxmc_rocon_hh_gd30;
966 /* go out from switch */
968 pxmc_rocon_hh_gd20(pxmc_state_t *mcs)
972 if(mcs->pxms_flg & PXMS_ERR_m)
973 return pxmc_spdnext_gend(mcs);
975 mark = pxmc_inp_rocon_is_mark(mcs);
978 /* switch is off -> look for it again */
979 mcs->pxms_do_gen = pxmc_rocon_hh_gd30;
982 pxmc_spd_gacc(&(mcs->pxms_rs), -mcs->pxms_gen_hsp, mcs->pxms_ma);
983 mcs->pxms_rp += mcs->pxms_rs;
988 /* switch is off -> look for it */
990 pxmc_rocon_hh_gd30(pxmc_state_t *mcs)
995 if (mcs->pxms_flg & PXMS_ERR_m)
996 return pxmc_spdnext_gend(mcs);
998 pxmc_spd_gacc(&(mcs->pxms_rs), mcs->pxms_gen_hsp, mcs->pxms_ma);
999 mcs->pxms_rp += mcs->pxms_rs;
1001 mark = pxmc_inp_rocon_is_mark(mcs);
1004 spd = mcs->pxms_gen_hsp >> 2; /* slow down */
1007 mcs->pxms_gen_hsp = spd;
1008 mcs->pxms_do_gen = pxmc_rocon_hh_gd40;
1014 /* switch is on -> find edge */
1016 pxmc_rocon_hh_gd40(pxmc_state_t *mcs)
1020 if (mcs->pxms_flg & PXMS_ERR_m)
1021 return pxmc_spdnext_gend(mcs);
1023 mark = pxmc_inp_rocon_is_mark(mcs);
1026 if (mcs->pxms_cfg & PXMS_CFG_HRI_m) {
1027 pxmc_inp_rocon_is_index_edge(mcs);
1028 mcs->pxms_do_gen = pxmc_rocon_hh_gd50;
1030 pxmc_inp_rocon_ap_zero(mcs);
1031 mcs->pxms_do_gen = pxmc_stop_gi;
1035 pxmc_spd_gacc(&(mcs->pxms_rs), -mcs->pxms_gen_hsp, mcs->pxms_ma);
1036 mcs->pxms_rp += mcs->pxms_rs;
1041 /* calibrate on revolution index */
1043 pxmc_rocon_hh_gd50(pxmc_state_t *mcs)
1045 if (mcs->pxms_flg & PXMS_ERR_m)
1046 return pxmc_spdnext_gend(mcs);
1048 pxmc_spd_gacc(&(mcs->pxms_rs), mcs->pxms_gen_hsp, mcs->pxms_ma);
1049 mcs->pxms_rp += mcs->pxms_rs;
1051 if (pxmc_inp_rocon_is_index_edge(mcs)){
1052 pxmc_inp_rocon_irc_offset_from_index(mcs);
1053 mcs->pxms_do_gen = pxmc_stop_gi;
1059 pxmc_call_t *pxmc_get_hh_gi_4axis(pxmc_state_t *mcs)
1061 return pxmc_rocon_hh_gi;
1071 pxmc_pid_con /*pxmc_dummy_con*/,
1073 pxmc_rocon_pwm_dc_out /*pxmc_rocon_pwm3ph_out*/,
1077 pxmc_inp_rocon_ap2hw,
1078 pxms_ap: 0, pxms_as: 0,
1079 pxms_rp: 55 * 256, pxms_rs: 0, pxms_subdiv: 8,
1080 pxms_md: 800 << 8, pxms_ms: 1000, pxms_ma: 10,
1083 pxms_ene: 0, pxms_erc: 0,
1084 pxms_p: 80, pxms_i: 30, pxms_d: 200, pxms_s1: 200, pxms_s2: 0,
1085 pxms_me: 0x7e00/*0x7fff*/,
1087 PXMS_CFG_SMTH_m | PXMS_CFG_MD2E_m | PXMS_CFG_HLS_m | PXMS_CFG_HPS_m * 0 |
1088 PXMS_CFG_HRI_m * 0 | PXMS_CFG_HDIR_m * 0 |
1089 PXMS_CFG_I2PT_m * 0 | 0x2,
1094 /*pxms_ptamp: 0x7fff,*/
1108 pxmc_rocon_pwm_dc_out,
1112 pxmc_inp_rocon_ap2hw,
1113 pxms_ap: 0, pxms_as: 0,
1114 pxms_rp: 55 * 256, pxms_rs: 0, pxms_subdiv: 8,
1115 pxms_md: 800 << 8, pxms_ms: 1000, pxms_ma: 10,
1118 pxms_ene: 0, pxms_erc: 0,
1119 pxms_p: 80, pxms_i: 30, pxms_d: 200, pxms_s1: 200, pxms_s2: 0,
1120 pxms_me: 0x7e00/*0x7fff*/,
1122 PXMS_CFG_SMTH_m | PXMS_CFG_MD2E_m | PXMS_CFG_HLS_m | PXMS_CFG_HPS_m * 0 |
1123 PXMS_CFG_HRI_m * 0 | PXMS_CFG_HDIR_m * 0 |
1124 PXMS_CFG_I2PT_m * 0 | 0x2,
1128 /*pxms_ptamp: 0x7fff,*/
1142 pxmc_rocon_pwm_dc_out,
1146 pxmc_inp_rocon_ap2hw,
1147 pxms_ap: 0, pxms_as: 0,
1148 pxms_rp: 55 * 256, pxms_rs: 0, pxms_subdiv: 8,
1149 pxms_md: 800 << 8, pxms_ms: 1000, pxms_ma: 10,
1152 pxms_ene: 0, pxms_erc: 0,
1153 pxms_p: 80, pxms_i: 30, pxms_d: 200, pxms_s1: 200, pxms_s2: 0,
1154 pxms_me: 0x7e00/*0x7fff*/,
1156 PXMS_CFG_SMTH_m | PXMS_CFG_MD2E_m | PXMS_CFG_HLS_m | PXMS_CFG_HPS_m * 0 |
1157 PXMS_CFG_I2PT_m * 0 | PXMS_CFG_HRI_m |
1158 PXMS_CFG_HDIR_m | 0x2,
1162 /*pxms_ptamp: 0x7fff,*/
1176 pxmc_rocon_pwm_dc_out,
1180 pxmc_inp_rocon_ap2hw,
1181 pxms_ap: 0, pxms_as: 0,
1182 pxms_rp: 55 * 256, pxms_rs: 0, pxms_subdiv: 8,
1183 pxms_md: 800 << 8, pxms_ms: 1000, pxms_ma: 10,
1186 pxms_ene: 0, pxms_erc: 0,
1187 pxms_p: 80, pxms_i: 30, pxms_d: 200, pxms_s1: 200, pxms_s2: 0,
1188 pxms_me: 0x7e00/*0x7fff*/,
1190 PXMS_CFG_SMTH_m | PXMS_CFG_MD2E_m | PXMS_CFG_HLS_m | PXMS_CFG_HPS_m * 0 |
1191 PXMS_CFG_I2PT_m * 0 | PXMS_CFG_HRI_m |
1192 PXMS_CFG_HDIR_m * 0 | 0x2,
1196 /*pxms_ptamp: 0x7fff,*/
1210 pxmc_rocon_pwm_dc_out,
1214 pxmc_inp_rocon_ap2hw,
1215 pxms_ap: 0, pxms_as: 0,
1216 pxms_rp: 55 * 256, pxms_rs: 0, pxms_subdiv: 8,
1217 pxms_md: 800 << 8, pxms_ms: 1000, pxms_ma: 10,
1220 pxms_ene: 0, pxms_erc: 0,
1221 pxms_p: 80, pxms_i: 30, pxms_d: 200, pxms_s1: 200, pxms_s2: 0,
1222 pxms_me: 0x7e00/*0x7fff*/,
1224 PXMS_CFG_SMTH_m | PXMS_CFG_MD2E_m | PXMS_CFG_HLS_m | PXMS_CFG_HPS_m * 0 |
1225 PXMS_CFG_HRI_m | PXMS_CFG_I2PT_m * 0 |
1226 PXMS_CFG_HDIR_m | 0x2,
1230 /*pxms_ptamp: 0x7fff,*/
1244 pxmc_rocon_pwm_dc_out,
1248 pxmc_inp_rocon_ap2hw,
1249 pxms_ap: 0, pxms_as: 0,
1250 pxms_rp: 55 * 256, pxms_rs: 0, pxms_subdiv: 8,
1251 pxms_md: 800 << 8, pxms_ms: 1000, pxms_ma: 10,
1254 pxms_ene: 0, pxms_erc: 0,
1255 pxms_p: 80, pxms_i: 30, pxms_d: 200, pxms_s1: 200, pxms_s2: 0,
1256 pxms_me: 0x7e00/*0x7fff*/,
1258 PXMS_CFG_SMTH_m | PXMS_CFG_MD2E_m | PXMS_CFG_HLS_m | PXMS_CFG_HPS_m |
1259 PXMS_CFG_HRI_m | PXMS_CFG_I2PT_m * 0 |
1260 PXMS_CFG_HDIR_m | 0x2,
1264 /*pxms_ptamp: 0x7fff,*/
1278 pxmc_rocon_pwm_dc_out,
1282 pxmc_inp_rocon_ap2hw,
1283 pxms_ap: 0, pxms_as: 0,
1284 pxms_rp: 55 * 256, pxms_rs: 0, pxms_subdiv: 8,
1285 pxms_md: 800 << 8, pxms_ms: 1000, pxms_ma: 10,
1288 pxms_ene: 0, pxms_erc: 0,
1289 pxms_p: 80, pxms_i: 30, pxms_d: 200, pxms_s1: 200, pxms_s2: 0,
1290 pxms_me: 0x7e00/*0x7fff*/,
1292 PXMS_CFG_SMTH_m | PXMS_CFG_MD2E_m | PXMS_CFG_HLS_m | PXMS_CFG_I2PT_m * 0 |
1297 /*pxms_ptamp: 0x7fff,*/
1311 pxmc_rocon_pwm_dc_out,
1315 pxmc_inp_rocon_ap2hw,
1316 pxms_ap: 0, pxms_as: 0,
1317 pxms_rp: 55 * 256, pxms_rs: 0, pxms_subdiv: 8,
1318 pxms_md: 800 << 8, pxms_ms: 1000, pxms_ma: 10,
1321 pxms_ene: 0, pxms_erc: 0,
1322 pxms_p: 80, pxms_i: 30, pxms_d: 200, pxms_s1: 200, pxms_s2: 0,
1323 pxms_me: 0x7e00/*0x7fff*/,
1325 PXMS_CFG_SMTH_m | PXMS_CFG_MD2E_m | PXMS_CFG_HLS_m | PXMS_CFG_I2PT_m * 0 |
1330 /*pxms_ptamp: 0x7fff,*/
1336 pxmc_state_t *pxmc_main_arr[PXML_MAIN_CNT] =
1337 {&mcs0, &mcs1, &mcs2, &mcs3, &mcs4, &mcs5, &mcs6, &mcs7};
1340 pxmc_state_list_t pxmc_main_list =
1347 void pxmc_lpc_qei_callback_index(struct lpc_qei_state_t *qst, void *context)
1349 pxmc_state_t *mcs = (pxmc_state_t *)context;
1352 irc = qst->index_pos;
1354 if ((mcs->pxms_cfg & PXMS_CFG_I2PT_m) && (mcs->pxms_flg & PXMS_PTI_m))
1357 ofs = irc - mcs->pxms_ptmark;
1358 diff = ofs - mcs->pxms_ptofs;
1360 if (diff >= mcs->pxms_ptirc / 2)
1361 diff -= mcs->pxms_ptirc;
1363 if (diff <= -mcs->pxms_ptirc / 2)
1364 diff += mcs->pxms_ptirc;
1369 if (diff >= mcs->pxms_ptirc / 6)
1371 pxmc_set_errno(mcs, PXMS_E_I2PT_TOOBIG);
1375 mcs->pxms_ptofs = ofs;
1376 pxmc_set_flag(mcs, PXMS_PHA_b);
1380 ofs=irc-mcs->pxms_ptofs;
1381 if((unsigned short)ofs>=(unsigned short)mcs->pxms_ptirc) {
1383 ofs-=mcs->pxms_ptirc;
1385 ofs+=mcs->pxms_ptirc;
1388 mcs->pxms_ptmark=ofs;
1391 /*lpc_qei_set_cmpos(qst, 0, qst->index_pos-4000);*/
1394 int pxmc_lpc_pthalalign_callback(pxmc_state_t *mcs)
1398 lpc_qei_state_t *qst = &lpc_qei_state;
1399 int irc = lpc_qei_get_pos(qst);
1402 if (!qst->index_occ)
1405 idx_rel = qst->index_pos - irc;
1406 idx_rel %= mcs->pxms_ptirc;
1409 idx_rel += mcs->pxms_ptirc;
1411 ptofs = irc - mcs->pxms_ptofs;
1412 ptmark = ptofs + idx_rel;
1414 if ((unsigned short)ptmark >= mcs->pxms_ptirc)
1417 ptmark -= mcs->pxms_ptirc;
1419 ptmark += mcs->pxms_ptirc;
1422 if ((unsigned short)ptmark < mcs->pxms_ptirc)
1424 mcs->pxms_ptmark = ptmark;
1430 int pxmc_lpc_pthalalign(pxmc_state_t *mcs, int periods)
1435 pxmc_call_t *fin_fnc = pxmc_lpc_pthalalign_callback;
1436 lpc_qei_state_t *qst = &lpc_qei_state;
1438 mcs->pxms_cfg &= ~PXMS_CFG_I2PT_m;
1439 lpc_qei_setup_index_catch(qst);
1441 r2acq = ((long)mcs->pxms_ptirc << PXMC_SUBDIV(mcs)) * periods;
1442 spd = 1 << PXMC_SUBDIV(mcs);
1444 res = pxmc_pthalalign(mcs, r2acq, spd, fin_fnc);
1449 int pxmc_lpc_pthalalign_run(void)
1451 return pxmc_lpc_pthalalign(pxmc_main_list.pxml_arr[0], 20);
1454 static inline void pxmc_sfi_input(void)
1458 pxmc_for_each_mcs(var, mcs)
1460 /* PXMS_ENI_m - check if input (IRC) update is enabled */
1461 if (mcs->pxms_flg & PXMS_ENI_m)
1463 pxmc_call(mcs, mcs->pxms_do_inp);
1468 static inline void pxmc_sfi_controller_and_output(void)
1472 pxmc_for_each_mcs(var, mcs)
1474 /* PXMS_ENR_m - check if controller is enabled */
1475 if (mcs->pxms_flg & PXMS_ENR_m || mcs->pxms_flg & PXMS_ENO_m)
1478 /* If output only is enabled, we skip the controller */
1479 if (mcs->pxms_flg & PXMS_ENR_m)
1482 pxmc_call(mcs, mcs->pxms_do_con);
1484 /* PXMS_ERR_m - if axis in error state */
1485 if (mcs->pxms_flg & PXMS_ERR_m)
1489 /* for bushless motors, it is necessary to call do_out
1490 even if the controller is not enabled and PWM should be provided. */
1491 pxmc_call(mcs, mcs->pxms_do_out);
1496 static inline void pxmc_sfi_generator(void)
1500 pxmc_for_each_mcs(var, mcs)
1502 /* PXMS_ENG_m - check if requested value (position) generator is enabled */
1503 if (mcs->pxms_flg & PXMS_ENG_m)
1505 pxmc_call(mcs, mcs->pxms_do_gen);
1510 static inline void pxmc_sfi_dbg(void)
1514 pxmc_for_each_mcs(var, mcs)
1516 if (mcs->pxms_flg & PXMS_DBG_m)
1518 pxmc_call(mcs, mcs->pxms_do_deb);
1523 int pxmc_rocon_pthalalign_callback(pxmc_state_t *mcs)
1527 int inp_chan = mcs->pxms_inp_info;
1529 long irc = fpga_irc[inp_chan]->count;
1531 if (!pxmc_inp_rocon_is_index_edge(mcs))
1534 idx_rel = fpga_irc[inp_chan]->count_index - irc;
1535 idx_rel %= mcs->pxms_ptirc;
1537 idx_rel += mcs->pxms_ptirc;
1539 ptofs = irc-mcs->pxms_ptofs;
1540 ptmark = ptofs+idx_rel;
1542 if((unsigned short)ptmark >= mcs->pxms_ptirc) {
1544 ptmark -= mcs->pxms_ptirc;
1546 ptmark += mcs->pxms_ptirc;
1549 if((unsigned short)ptmark < mcs->pxms_ptirc) {
1550 mcs->pxms_ptmark = ptmark;
1555 int pxmc_rocon_pthalalign(pxmc_state_t *mcs, int periods)
1560 pxmc_call_t *fin_fnc = pxmc_rocon_pthalalign_callback;
1562 mcs->pxms_cfg&=~PXMS_CFG_I2PT_m;
1564 r2acq=((long)mcs->pxms_ptirc<<PXMC_SUBDIV(mcs))*periods;
1565 spd=1<<PXMC_SUBDIV(mcs);
1567 /* clear bit indicating previous index */
1568 pxmc_inp_rocon_is_index_edge(mcs);
1570 res=pxmc_pthalalign(mcs, r2acq, spd, fin_fnc);
1575 int pxmc_axis_rdmode(pxmc_state_t *mcs)
1577 if (mcs->pxms_do_out == pxmc_rocon_pwm2ph_out)
1578 return PXMC_AXIS_MODE_STEPPER_WITH_IRC;
1579 if (mcs->pxms_do_out == pxmc_rocon_pwm_dc_out)
1580 return PXMC_AXIS_MODE_DC;
1581 if (mcs->pxms_do_out == pxmc_rocon_pwm3ph_out)
1582 return PXMC_AXIS_MODE_BLDC;
1588 pxmc_axis_pt4mode(pxmc_state_t *mcs, int mode)
1590 static const typeof(*mcs->pxms_ptptr1) dummy0 = 0;
1593 if (mode == PXMC_AXIS_MODE_NOCHANGE)
1594 mode = pxmc_axis_rdmode(mcs);
1599 /*case PXMC_AXIS_MODE_STEPPER:*/
1600 case PXMC_AXIS_MODE_STEPPER_WITH_IRC:
1601 res = pxmc_ptable_set_profile(mcs, &pxmc_ptprofile_sin, 0, 0);
1603 /*case PXMC_AXIS_MODE_STEPPER_WITH_PWM:*/
1604 case PXMC_AXIS_MODE_DC:
1605 /*profive some sane dummy values*/
1606 mcs->pxms_ptptr1 = (typeof(mcs->pxms_ptptr1))&dummy0;
1607 mcs->pxms_ptptr2 = (typeof(mcs->pxms_ptptr1))&dummy0;
1608 mcs->pxms_ptptr3 = (typeof(mcs->pxms_ptptr1))&dummy0;
1610 mcs->pxms_ptscale_mult=1;
1611 mcs->pxms_ptscale_shift=15;
1613 case PXMC_AXIS_MODE_BLDC:
1614 /* res = pxmc_init_ptable(mcs, PXMC_PTPROF_SIN3FUP); */
1615 #ifndef PXMC_WITH_PT_ZIC
1616 res = pxmc_ptable_set_profile(mcs, &pxmc_ptprofile_sin3phup, 0, 0);
1617 #else /*PXMC_WITH_PT_ZIC*/
1618 res = pxmc_ptable_set_profile(mcs, &pxmc_ptprofile_sin3phup_zic, 0, 0);
1619 #endif /*PXMC_WITH_PT_ZIC*/
1625 mcs->pxms_ptvang = pxmc_ptvang_deg2irc(mcs, 90);
1631 * pxmc_axis_mode - Sets axis mode.[extern API]
1632 * @mcs: Motion controller state information
1633 * @mode: 0 .. previous mode, 1 .. stepper motor mode,
1634 * 2 .. stepper motor with IRC feedback and PWM ,
1635 * 3 .. stepper motor with PWM control
1636 * 4 .. DC motor with IRC feedback and PWM
1640 pxmc_axis_mode(pxmc_state_t *mcs, int mode)
1644 pxmc_set_const_out(mcs, 0);
1645 pxmc_clear_flag(mcs, PXMS_ENI_b);
1646 pxmc_clear_flags(mcs,PXMS_ENO_m);
1647 /* Clear possible stall index flags from hardware */
1648 pxmc_inp_rocon_is_index_edge(mcs);
1649 pxmc_clear_flag(mcs, PXMS_PHA_b);
1650 pxmc_clear_flag(mcs, PXMS_PTI_b);
1653 if (mode == PXMC_AXIS_MODE_NOCHANGE)
1654 mode = pxmc_axis_rdmode(mcs);
1658 mode = PXMC_AXIS_MODE_DC;
1660 res = pxmc_axis_pt4mode(mcs, mode);
1665 /*case PXMC_AXIS_MODE_STEPPER:*/
1666 case PXMC_AXIS_MODE_STEPPER_WITH_IRC:
1667 mcs->pxms_do_out = pxmc_rocon_pwm2ph_out;
1669 /*case PXMC_AXIS_MODE_STEPPER_WITH_PWM:*/
1670 case PXMC_AXIS_MODE_DC:
1671 mcs->pxms_do_out = pxmc_rocon_pwm_dc_out;
1673 case PXMC_AXIS_MODE_BLDC:
1674 mcs->pxms_do_out = pxmc_rocon_pwm3ph_out;
1680 /* Clear possible stall index flags from hardware */
1681 pxmc_inp_rocon_is_index_edge(mcs);
1682 /* Request new phases alignment for changed parameters */
1683 pxmc_clear_flag(mcs, PXMS_PHA_b);
1684 pxmc_clear_flag(mcs, PXMS_PTI_b);
1685 pxmc_set_flag(mcs, PXMS_ENI_b);
1689 void pxmc_sfi_isr(void)
1691 unsigned long spent = pxmc_fast_tick_time();
1694 pxmc_sfi_controller_and_output();
1695 pxmc_sfi_generator();
1697 /* Kick LX Master watchdog */
1698 if (pxmc_main_list.pxml_cnt != 0)
1699 *fpga_lx_master_transmitter_wdog = 1;
1701 spent = pxmc_fast_tick_time() - spent;
1703 if(spent > pxmc_sfi_spent_time_max)
1704 pxmc_sfi_spent_time_max = spent;
1708 int pxmc_clear_power_stop(void)
1713 int pxmc_process_state_check(unsigned long *pbusy_bits,
1714 unsigned long *perror_bits)
1717 unsigned short chan;
1718 unsigned long busy_bits = 0;
1719 unsigned long error_bits = 0;
1722 pxmc_for_each_mcs(chan, mcs) {
1724 flg |= mcs->pxms_flg;
1725 if (mcs->pxms_flg & PXMS_BSY_m)
1726 busy_bits |= 1 << chan;
1727 if (mcs->pxms_flg & PXMS_ERR_m)
1728 error_bits |= 1 << chan;
1731 if (appl_errstop_mode) {
1732 if((flg & PXMS_ENG_m) && (flg & PXMS_ERR_m)) {
1733 pxmc_for_each_mcs(chan, mcs) {
1734 if(mcs&&(mcs->pxms_flg & PXMS_ENG_m)) {
1741 if (pbusy_bits != NULL)
1742 *pbusy_bits = busy_bits;
1743 if (error_bits != NULL)
1744 *perror_bits = error_bits;
1754 if (!pxmc_main_list.pxml_cnt)
1757 pxmc_for_each_mcs(var, mcs)
1759 pxmc_set_const_out(mcs,0);
1762 pxmc_main_list.pxml_cnt = 0;
1768 int pxmc_initialize(void)
1773 pxmc_state_t *mcs = &mcs0;
1774 lpc_qei_state_t *qst = &lpc_qei_state;
1776 *fpga_irc_reset = 1;
1778 for (i = 0; i < 8; i++) {
1779 fpga_irc[i]->count = 0;
1780 fpga_irc[i]->count_index = 0;
1781 *fpga_irc_state[i] = FPGA_IRC_STATE_INDEX_EVENT_MASK;
1784 /* Initialize QEI module for IRC counting */
1785 pxmc_inp_lpc_qei_init(mcs->pxms_inp_info);
1787 /* Initialize QEI events processing */
1788 if (lpc_qei_setup_irq(qst) < 0)
1791 *fpga_irc_reset = 0;
1793 //res = pxmc_axis_pt4mode(mcs, PXMC_AXIS_MODE_BLDC);
1795 /*pxmc_ctm4pwm3f_wr(mcs, 0, 0, 0);*/
1796 //pxmc_rocon_pwm3ph_wr(mcs, 0, 0, 0);
1798 pxmc_rocon_pwm_master_init();
1799 #ifdef PXMC_ROCON_TIMED_BY_RX_DONE
1800 pxmc_rocon_rx_done_isr_setup(pxmc_rocon_rx_done_isr);
1801 #endif /*PXMC_ROCON_TIMED_BY_RX_DONE*/
1803 pxmc_main_list.pxml_cnt = 0;
1804 pxmc_dbg_hist = NULL;
1806 pxmc_main_list.pxml_cnt = PXML_MAIN_CNT;