📄 etpu_qd.c
字号:
int32_t fs_etpu_qd_disable( uint8_t channel_primary,
uint8_t channel_home,
uint8_t channel_index,
uint8_t signals)
{
/****************************************
* Parameters bounds check.
***************************************/
#ifdef FS_ETPU_MC_PARAM_CHECK
if(((channel_primary>31)&&(channel_primary<64))||(channel_primary>95)||
((signals==FS_ETPU_QD_PRIM_SEC_INDEX_HOME)||
(signals==FS_ETPU_QD_PRIM_SEC_HOME))&&
(((channel_home>31)&&(channel_home<64))||(channel_home>95))||
((signals==FS_ETPU_QD_PRIM_SEC_INDEX_HOME)||
(signals==FS_ETPU_QD_PRIM_SEC_INDEX))&&
(((channel_index>31)&&(channel_index<64))||(channel_index>95)))
{
return(FS_ETPU_ERROR_VALUE);
}
#endif
/****************************************
* Disable channel.
***************************************/
fs_etpu_disable(channel_primary);
fs_etpu_disable((uint8_t)(channel_primary+1));
if((signals==FS_ETPU_QD_PRIM_SEC_INDEX_HOME)||
(signals==FS_ETPU_QD_PRIM_SEC_HOME))
fs_etpu_disable(channel_home);
if((signals==FS_ETPU_QD_PRIM_SEC_INDEX_HOME)||
(signals==FS_ETPU_QD_PRIM_SEC_INDEX))
fs_etpu_disable(channel_index);
return(0);
}
/*******************************************************************************
* FUNCTION : fs_etpu_qd_enable
* PURPOSE : To enable an eTPU Quadrature Decoder channels.
* INPUTS NOTES : This function has 5 parameters:
* channel_primary - This is the Primary channel number (Phase A).
* 0-31 for ETPU_A and 64-95 for ETPU_B.
* channel_home - If a Home signal is processed, this is the Home channel
* number.
* 0-31 for ETPU_A and 64-95 for ETPU_B.
* channel_index - If an Index signal is processed, this is the Index channel
* number.
* 0-31 for ETPU_A and 64-95 for ETPU_B.
* signals - This parameter determines which QD signals are used.
* This parameter should be assigned a value of:
* FS_ETPU_QD_PRIM_SEC
* FS_ETPU_QD_PRIM_SEC_INDEX
* FS_ETPU_QD_PRIM_SEC_HOME
* FS_ETPU_QD_PRIM_SEC_INDEX_HOME.
* priority - This is the priority to assign to the QD channels.
* This parameter should be assigned a value of:
* FS_ETPU_PRIORITY_HIGH, FS_ETPU_PRIORITY_MIDDLE or
* FS_ETPU_PRIORITY_LOW.
*
* RETURNS NOTES: Error code which can be returned is: FS_ETPU_ERROR_VALUE
*******************************************************************************/
int32_t fs_etpu_qd_enable( uint8_t channel_primary,
uint8_t channel_home,
uint8_t channel_index,
uint8_t signals,
uint8_t priority)
{
/****************************************
* Parameters bounds check.
***************************************/
#ifdef FS_ETPU_MC_PARAM_CHECK
if(((channel_primary>31)&&(channel_primary<64))||(channel_primary>95)||
((signals==FS_ETPU_QD_PRIM_SEC_INDEX_HOME)||
(signals==FS_ETPU_QD_PRIM_SEC_HOME))&&
(((channel_home>31)&&(channel_home<64))||(channel_home>95))||
((signals==FS_ETPU_QD_PRIM_SEC_INDEX_HOME)||
(signals==FS_ETPU_QD_PRIM_SEC_INDEX))&&
(((channel_index>31)&&(channel_index<64))||(channel_index>95))||
(priority>FS_ETPU_PRIORITY_HIGH))
{
return(FS_ETPU_ERROR_VALUE);
}
#endif
/****************************************
* Enable channel.
***************************************/
fs_etpu_enable(channel_primary, priority);
fs_etpu_enable((uint8_t)(channel_primary+1), priority);
if((signals==FS_ETPU_QD_PRIM_SEC_INDEX_HOME)||
(signals==FS_ETPU_QD_PRIM_SEC_HOME))
fs_etpu_enable(channel_home, priority);
if((signals==FS_ETPU_QD_PRIM_SEC_INDEX_HOME)||
(signals==FS_ETPU_QD_PRIM_SEC_INDEX))
fs_etpu_enable(channel_index, priority);
return(0);
}
/*******************************************************************************
*FUNCTION : fs_etpu_qd_align
*PURPOSE : This function changes the Position Counter value to ensure
* its divisibility by 4 when leading edge occurs. This depends
* on QD Configuration, and of the QD Primary and
* Secondary pin states.
* Position Counter dedicated for SC is reset to 0.
*INPUTS NOTES : This function has 2 parameters:
* channel_primary - This is the Primary channel number (Phase A).
* 0-31 for ETPU_A and 64-95 for ETPU_B.
* pc - This is the Position Counter value to be set.
* The actual value set is in range from pc-1 to pc+2,
* to ensure PC divisibility by 4 on the leading edge.
*
*RETURNS NOTES: Error code that can be returned is: FS_ETPU_ERROR_VALUE.
*******************************************************************************/
int32_t fs_etpu_qd_align(uint8_t channel_primary,
int24_t pc)
{
uint8_t pins;
/* Parameters bounds check */
#ifdef FS_ETPU_MC_PARAM_CHECK
if(((channel_primary>31)&&(channel_primary<64))||(channel_primary>95))
{
return(FS_ETPU_ERROR_VALUE);
}
#endif
eTPU->CHAN[channel_primary].HSRR.R = FS_ETPU_QD_INIT;
eTPU->CHAN[channel_primary+1].HSRR.R = FS_ETPU_QD_INIT;
/* Wait till initialization is finished */
while (eTPU->CHAN[channel_primary+1].HSRR.R != 0) {}
/* Read actual pins state */
pins = (uint8_t)(fs_etpu_get_chan_local_8(channel_primary,
FS_ETPU_QD_PINS_OFFSET) & 0x07);
/* Adjust pc so that the leading edge counts a multiple of 4 */
if ((pins == 0x01) || (pins == 0x06))
pc += 1;
else if ((pins == 0x02) || (pins == 0x05))
pc -= 1;
else if ((pins == 0x03) || (pins == 0x04))
pc += 2;
/* Write pc and reset pc_sc */
fs_etpu_set_chan_local_24(channel_primary,FS_ETPU_QD_PC_OFFSET,(uint24_t)pc);
fs_etpu_set_chan_local_24(channel_primary,FS_ETPU_QD_PC_SC_OFFSET, 0);
return(0);
}
/*******************************************************************************
*FUNCTION : fs_etpu_qd_set_pc_interrupts
*PURPOSE : This function changes the pc_interrupt values.
*INPUTS NOTES : This function has 3 parameters:
* channel_primary - This is the Primary channel number (Phase A).
* 0-31 for ETPU_A and 64-95 for ETPU_B.
* pc_interrupt1 - This is the pc_interrupt1 value to be set.
* pc_interrupt2 - This is the pc_interrupt2 value to be set.
*
*RETURNS NOTES: Error code that can be returned is: FS_ETPU_ERROR_VALUE.
*******************************************************************************/
int32_t fs_etpu_qd_set_pc_interrupts(uint8_t channel_primary,
int24_t pc_interrupt1,
int24_t pc_interrupt2)
{
/* Parameters bounds check */
#ifdef FS_ETPU_MC_PARAM_CHECK
if(((channel_primary>31)&&(channel_primary<64))||(channel_primary>95))
{
return(FS_ETPU_ERROR_VALUE);
}
#endif
fs_etpu_set_chan_local_24(channel_primary, FS_ETPU_QD_PCINTERRUPT1_OFFSET,
(uint24_t)pc_interrupt1);
fs_etpu_set_chan_local_24(channel_primary, FS_ETPU_QD_PCINTERRUPT2_OFFSET,
(uint24_t)pc_interrupt2);
return(0);
}
/*******************************************************************************
*FUNCTION : fs_etpu_qd_enable_pc_interrupts
*PURPOSE : This function enables the pc_interrupt processing.
*INPUTS NOTES : This function has 1 parameter:
* channel_primary - This is the Primary channel number (Phase A).
* 0-31 for ETPU_A and 64-95 for ETPU_B.
*
*RETURNS NOTES: Error code that can be returned is: FS_ETPU_ERROR_VALUE.
*******************************************************************************/
int32_t fs_etpu_qd_enable_pc_interrupts(uint8_t channel_primary)
{
uint8_t options;
/* Parameters bounds check */
#ifdef FS_ETPU_MC_PARAM_CHECK
if(((channel_primary>31)&&(channel_primary<64))||(channel_primary>95))
{
return(FS_ETPU_ERROR_VALUE);
}
#endif
options = fs_etpu_get_chan_local_8(channel_primary,
FS_ETPU_QD_OPTIONS_OFFSET);
options |= FS_ETPU_QD_PC_INTERRUPT_ENABLED;
fs_etpu_set_chan_local_8(channel_primary,FS_ETPU_QD_OPTIONS_OFFSET, options);
return(0);
}
/*******************************************************************************
*FUNCTION : fs_etpu_qd_disable_pc_interrupts
*PURPOSE : This function disables the pc_interrupt processing.
*INPUTS NOTES : This function has 1 parameter:
* channel_primary - This is the Primary channel number (Phase A).
* 0-31 for ETPU_A and 64-95 for ETPU_B.
*
*RETURNS NOTES: Error code that can be returned is: FS_ETPU_ERROR_VALUE.
*******************************************************************************/
int32_t fs_etpu_qd_disable_pc_interrupts(uint8_t channel_primary)
{
uint8_t options;
/* Parameters bounds check */
#ifdef FS_ETPU_MC_PARAM_CHECK
if(((channel_primary>31)&&(channel_primary<64))||(channel_primary>95))
{
return(FS_ETPU_ERROR_VALUE);
}
#endif
options = fs_etpu_get_chan_local_8(channel_primary,
FS_ETPU_QD_OPTIONS_OFFSET);
options &= (uint8_t)(~FS_ETPU_QD_PC_INTERRUPT_ENABLED);
fs_etpu_set_chan_local_8(channel_primary,FS_ETPU_QD_OPTIONS_OFFSET, options);
return(0);
}
/*******************************************************************************
*FUNCTION : fs_etpu_qd_set_pc
*PURPOSE : This function changes the Position Counter value.
*INPUTS NOTES : This function has 2 parameters:
* channel_primary - This is the Primary channel number (Phase A).
* 0-31 for ETPU_A and 64-95 for ETPU_B.
* pc - This is the Position Counter value to be set.
*
*RETURNS NOTES: Error code that can be returned is: FS_ETPU_ERROR_VALUE.
*******************************************************************************/
int32_t fs_etpu_qd_set_pc(uint8_t channel_primary,
int24_t pc)
{
/* Parameters bounds check */
#ifdef FS_ETPU_MC_PARAM_CHECK
if(((channel_primary>31)&&(channel_primary<64))||(channel_primary>95))
{
return(FS_ETPU_ERROR_VALUE);
}
#endif
fs_etpu_set_chan_local_24(channel_primary,FS_ETPU_QD_PC_OFFSET,(uint24_t)pc);
return(0);
}
/*******************************************************************************
*FUNCTION : fs_etpu_qd_set_rc
*PURPOSE : This function changes the Revolution Counter value.
*INPUTS NOTES : This function has 2 parameters:
* channel_primary - This is the Primary channel number (Phase A).
* 0-31 for ETPU_A and 64-95 for ETPU_B.
* rc - This is the Revolution Counter value to be set.
*
*RETURNS NOTES: Error code that can be returned is: FS_ETPU_ERROR_VALUE.
*******************************************************************************/
int32_t fs_etpu_qd_set_rc(uint8_t channel_primary,
int24_t rc)
{
/* Parameters bounds check */
#ifdef FS_ETPU_MC_PARAM_CHECK
if(((channel_primary>31)&&(channel_primary<64))||(channel_primary>95))
{
return(FS_ETPU_ERROR_VALUE);
}
#endif
fs_etpu_set_chan_local_24(channel_primary,FS_ETPU_QD_RC_OFFSET,(uint24_t)rc);
return(0);
}
/*******************************************************************************
*FUNCTION : fs_etpu_qd_set_slow_mode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -