00001
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include <avr32/io.h>
00049 #include "compiler.h"
00050 #include "qdec.h"
00051
00052
00053 int qdec_get_interrupt_settings(volatile avr32_qdec_t *qdec)
00054 {
00055 return qdec->imr;
00056 }
00057
00058
00059 int qdec_configure_interrupts(volatile avr32_qdec_t *qdec, const qdec_interrupt_t *bitfield)
00060 {
00061
00062 AVR32_ENTER_CRITICAL_REGION( );
00063
00064 qdec->ier = bitfield->ovr << AVR32_QDEC_IER_OVR_OFFSET |
00065 bitfield->dirinv << AVR32_QDEC_IER_DIRINV_OFFSET |
00066 bitfield->idexerr << AVR32_QDEC_IER_IDXERR_OFFSET |
00067 bitfield->rcro << AVR32_QDEC_IER_RCRO_OFFSET |
00068 bitfield->pcro << AVR32_QDEC_IER_PCRO_OFFSET |
00069 bitfield->cap << AVR32_QDEC_IER_CAP_OFFSET |
00070 bitfield->cmp << AVR32_QDEC_IER_CMP_OFFSET |
00071 bitfield->qepi << AVR32_QDEC_IER_QEPI_OFFSET;
00072
00073
00074 qdec->idr = (~bitfield->ovr & 1) << AVR32_QDEC_IER_OVR_OFFSET |
00075 (~bitfield->dirinv & 1) << AVR32_QDEC_IER_DIRINV_OFFSET |
00076 (~bitfield->idexerr & 1) << AVR32_QDEC_IER_IDXERR_OFFSET |
00077 (~bitfield->rcro & 1) << AVR32_QDEC_IER_RCRO_OFFSET |
00078 (~bitfield->pcro & 1) << AVR32_QDEC_IER_PCRO_OFFSET |
00079 (~bitfield->cap & 1) << AVR32_QDEC_IER_CAP_OFFSET |
00080 (~bitfield->cmp & 1) << AVR32_QDEC_IER_CMP_OFFSET |
00081 (~bitfield->qepi & 1) << AVR32_QDEC_IER_QEPI_OFFSET;
00082
00083 AVR32_LEAVE_CRITICAL_REGION( );
00084
00085 return 0;
00086 }
00087
00088 int qdec_init_quadrature_decoder_mode(volatile avr32_qdec_t *qdec, const qdec_quadrature_decoder_opt_t *opt)
00089 {
00090 qdec->cf = 0;
00091 qdec->cf = 1 << AVR32_QDEC_CF_QDEC_OFFSET |
00092 opt->idxe << AVR32_QDEC_CF_IDXE_OFFSET |
00093 opt->pcce << AVR32_QDEC_CF_PCCE_OFFSET |
00094 opt->rcce << AVR32_QDEC_CF_RCCE_OFFSET |
00095 opt->evtrge << AVR32_QDEC_CF_EVTRGE_OFFSET |
00096 opt->phsinva << AVR32_QDEC_CF_PHSINVA_OFFSET |
00097 opt->phsinvb << AVR32_QDEC_CF_PHSINVB_OFFSET |
00098 opt->idxinv << AVR32_QDEC_CF_IDXINV_OFFSET |
00099 opt->idxphs << AVR32_QDEC_CF_IDXPHS_OFFSET |
00100 opt->filten << AVR32_QDEC_CF_FILTEN_OFFSET ;
00101
00102 qdec->ctrl |= 1 << AVR32_QDEC_CTRL_CLKEN_OFFSET;
00103 return 0;
00104
00105 }
00106
00107 int qdec_init_timer_mode(volatile avr32_qdec_t *qdec, const qdec_timer_opt_t *opt)
00108 {
00109 qdec->cf = 0;
00110 qdec->cf = 0 << AVR32_QDEC_CF_QDEC_OFFSET |
00111 opt->pcce << AVR32_QDEC_CF_PCCE_OFFSET |
00112 opt->rcce << AVR32_QDEC_CF_RCCE_OFFSET |
00113 opt->evtrge << AVR32_QDEC_CF_EVTRGE_OFFSET |
00114 opt->filten << AVR32_QDEC_CF_FILTEN_OFFSET |
00115 opt->tsdir << AVR32_QDEC_CF_TSDIR_OFFSET |
00116 opt->upd << AVR32_QDEC_CF_UPD_OFFSET ;
00117
00118 qdec->ctrl |= 1 << AVR32_QDEC_CTRL_CLKEN_OFFSET;
00119 return 0;
00120 }
00121
00122 int qdec_software_trigger(volatile avr32_qdec_t *qdec)
00123 {
00124
00125 qdec->ctrl |= 1 << AVR32_QDEC_CTRL_SWTRG_OFFSET ;
00126 return 0;
00127 }
00128
00129 int qdec_stop(volatile avr32_qdec_t *qdec)
00130 {
00131
00132 qdec->ctrl &= ~(1 << AVR32_QDEC_CTRL_CLKEN_OFFSET);
00133 return 0;
00134 }
00135
00136 unsigned short int qdec_read_rc(volatile avr32_qdec_t *qdec)
00137 {
00138 return qdec->CNT.rc;
00139 }
00140
00141 unsigned short int qdec_read_pc(volatile avr32_qdec_t *qdec)
00142 {
00143 return qdec->CNT.pc;
00144 }
00145
00146 unsigned short int qdec_write_rc_cnt(volatile avr32_qdec_t *qdec,unsigned short int value)
00147 {
00148 qdec->CNT.rc = value;
00149 return 0;
00150 }
00151
00152 unsigned short int qdec_write_pc_cnt(volatile avr32_qdec_t *qdec,unsigned short int value)
00153 {
00154 qdec->CNT.pc = value;
00155 return qdec->CNT.pc;
00156 }
00157
00158 unsigned short int qdec_write_rc_top(volatile avr32_qdec_t *qdec, unsigned short int value)
00159 {
00160 qdec->TOP.rctop = value;
00161
00162 return qdec->TOP.rctop;
00163 }
00164
00165 unsigned short int qdec_write_pc_top(volatile avr32_qdec_t *qdec, unsigned short int value)
00166 {
00167 qdec->TOP.pctop = value;
00168
00169 return qdec->TOP.pctop;
00170 }
00171
00172 unsigned short int qdec_write_rc_cmp(volatile avr32_qdec_t *qdec, unsigned short int value)
00173 {
00174 qdec->CMP.rccmp = value;
00175
00176 return qdec->CMP.rccmp;
00177 }
00178
00179 unsigned short int qdec_write_pc_cmp(volatile avr32_qdec_t *qdec, unsigned short int value)
00180 {
00181 qdec->CMP.pccmp = value;
00182
00183 return qdec->CMP.pccmp;
00184 }
00185
00186 unsigned short int qdec_read_rc_cap(volatile avr32_qdec_t *qdec)
00187 {
00188 return qdec->CAP.rccap;
00189 }
00190
00191 unsigned short int qdec_read_pc_cap(volatile avr32_qdec_t *qdec)
00192 {
00193 return qdec->CAP.pccap;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206