173 lines
5.2 KiB
C
173 lines
5.2 KiB
C
/*!
|
|
* \file xc_drv_qdec.c
|
|
*
|
|
* \brief Target xc qdec driver implementation
|
|
*
|
|
* \copyright Revised BSD License, see section \ref LICENSE.
|
|
*
|
|
* \code
|
|
*
|
|
* _ __ _ ________ _
|
|
* | |/ /(_)___ / ____/ /_ (_)___
|
|
* | // / __ \/ / / __ \/ / __ \
|
|
* / |/ / / / / /___/ / / / / /_/ /
|
|
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
|
* /_/
|
|
* (C) 2022-2025 XinChip
|
|
*
|
|
* \endcode
|
|
*
|
|
* \author ( XinChip ) Alex-J
|
|
*
|
|
* \author ( XinChip )
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------
|
|
INCLUDE HEADE FILES
|
|
------------------------------------------------------------------------------------*/
|
|
#include "xc_drv_qdec.h"
|
|
|
|
/*------------------------------------------------------------------------------------
|
|
Local Variables
|
|
-------------------------------------------------------------------------------------*/
|
|
|
|
/*------------------------------------------------------------------------------------
|
|
Func Prototype
|
|
-------------------------------------------------------------------------------------*/
|
|
|
|
/*------------------------------------------------------------------------------------
|
|
Functions
|
|
-------------------------------------------------------------------------------------*/
|
|
/**
|
|
****************************************************************************************
|
|
* @brief
|
|
*
|
|
* @param[in]
|
|
* @param[in]
|
|
****************************************************************************************
|
|
*/
|
|
void xc_qdec_init(Qdec_InitCfg_t *init_cfg)
|
|
{
|
|
xc_gpio_mux_ctl(GPIO_21, GPIO_Mux2);
|
|
xc_gpio_mux_ctl(GPIO_20, GPIO_Mux2);
|
|
|
|
// DEBUG("0_CTLAPBCLKEN_GRCTL: %08x\n", cpr_ctlapbclken_grctl_get());
|
|
cpr_ctlapbclken_grctl__qdec_pclk_en__setf(ENABLE);
|
|
// DEBUG("1_CTLAPBCLKEN_GRCTL: %08x\n", cpr_ctlapbclken_grctl_get());
|
|
cpr_rstctl_ctlapb_sw__qdec_rstn__setf(~ENABLE);
|
|
cpr_rstctl_ctlapb_sw__qdec_rstn__setf(~DISABLE);
|
|
DEBUG("0_QDEC_CLK_CTL: %08x\n", cpr_qdec_clk_ctl_get());
|
|
cpr_qdec_clk_ctl__qdec_clk_div__setf(0x07);
|
|
cpr_qdec_clk_ctl__qdec_clk_en__setf(ENABLE);
|
|
DEBUG("1_QDEC_CLK_CTL: %08x\n", cpr_qdec_clk_ctl_get());
|
|
if(ENABLE == init_cfg->auto_clr_en) {
|
|
qdec_ctl__auto_clr_en__setf(ENABLE);
|
|
} else {
|
|
qdec_ctl__auto_clr_en__setf(DISABLE);
|
|
}
|
|
|
|
if(ENABLE == init_cfg->single_sample_rst_en) {
|
|
qdec_ctl__single_sample_rst_en__setf(ENABLE);
|
|
} else {
|
|
qdec_ctl__single_sample_rst_en__setf(DISABLE);
|
|
}
|
|
|
|
if(ENABLE == init_cfg->db_filter_en) {
|
|
qdec_ctl__db_filter_en__setf(ENABLE);
|
|
} else {
|
|
qdec_ctl__db_filter_en__setf(DISABLE);
|
|
}
|
|
QDEC_CTL_REG = 0x40;
|
|
qdec_samp_ctl_pack(init_cfg->db_sample_div,
|
|
init_cfg->num_pts_sampled,
|
|
init_cfg->acc_sample_div);
|
|
|
|
qdec_int_msk_pack(ENABLE, ENABLE, ENABLE, DISABLE);
|
|
}
|
|
|
|
/**
|
|
****************************************************************************************
|
|
* @brief
|
|
*
|
|
* @param[in]
|
|
* @param[in]
|
|
****************************************************************************************
|
|
*/
|
|
void xc_qdec_enable(void)
|
|
{
|
|
qdec_ctl__qdec_en__setf(ENABLE);
|
|
}
|
|
|
|
/**
|
|
****************************************************************************************
|
|
* @brief
|
|
*
|
|
* @param[in]
|
|
* @param[in]
|
|
****************************************************************************************
|
|
*/
|
|
void xc_qdec_disable(void)
|
|
{
|
|
qdec_ctl__qdec_en__setf(DISABLE);
|
|
}
|
|
|
|
/**
|
|
****************************************************************************************
|
|
* @brief
|
|
*
|
|
* @param[in]
|
|
* @param[in]
|
|
****************************************************************************************
|
|
*/
|
|
void xc_qdec_start(void)
|
|
{
|
|
qdec_ctl__qdec_start__setf(ENABLE);
|
|
}
|
|
|
|
/**
|
|
****************************************************************************************
|
|
* @brief
|
|
*
|
|
* @param[in]
|
|
* @param[in]
|
|
****************************************************************************************
|
|
*/
|
|
void xc_qdec_stop(void)
|
|
{
|
|
qdec_ctl__qdec_start__setf(DISABLE);
|
|
}
|
|
|
|
/**
|
|
****************************************************************************************
|
|
* @brief
|
|
*
|
|
* @param[in]
|
|
* @param[in]
|
|
****************************************************************************************
|
|
*/
|
|
void QDEC_Handler(void)
|
|
{
|
|
uint8_t qdec_int_mask = qdec_int_msk_get( );
|
|
uint8_t qdec_int = qdec_int_get( );
|
|
uint8_t qdec_int_sta = (qdec_int & qdec_int_mask);
|
|
qdec_int_set(qdec_int);
|
|
|
|
if(SINGLE_SAMPLE_MSK_BIT == (qdec_int_sta & SINGLE_SAMPLE_MSK_BIT)) {
|
|
DEBUG("__SINGLE_SAMPLE_INT__\n");
|
|
}
|
|
|
|
if(SAMPLE_END_MSK_BIT == (qdec_int_sta & SAMPLE_END_MSK_BIT)) {
|
|
DEBUG("__SAMPLE_END_INT__\n");
|
|
DEBUG("Sample: %04x\n", (uint16_t)qdec_acc_get( ));
|
|
DEBUG("DB: %02x\n", (uint8_t)qdec_db_get( ));
|
|
}
|
|
|
|
if(ACC_OF_MSK_BIT == (qdec_int_sta & ACC_OF_MSK_BIT)) {
|
|
DEBUG("__ACC_INT__\n");
|
|
}
|
|
|
|
if(DB_OF_MSK_BIT == (qdec_int_sta & DB_OF_MSK_BIT)) {
|
|
DEBUG("__DB_INT__\n");
|
|
}
|
|
}
|