123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932 |
- #include "gd32f10x_adc.h"
- #define ADC_CHANNEL_LENGTH_SUBTRACT_ONE ((uint8_t)1U)
- #define ADC_REGULAR_CHANNEL_RANK_SIX ((uint8_t)6U)
- #define ADC_REGULAR_CHANNEL_RANK_TWELVE ((uint8_t)12U)
- #define ADC_REGULAR_CHANNEL_RANK_SIXTEEN ((uint8_t)16U)
- #define ADC_REGULAR_CHANNEL_RANK_LENGTH ((uint8_t)5U)
- #define ADC_CHANNEL_SAMPLE_TEN ((uint8_t)10U)
- #define ADC_CHANNEL_SAMPLE_EIGHTEEN ((uint8_t)18U)
- #define ADC_CHANNEL_SAMPLE_LENGTH ((uint8_t)3U)
- #define ADC_INSERTED_CHANNEL_RANK_LENGTH ((uint8_t)5U)
- #define ADC_INSERTED_CHANNEL_SHIFT_LENGTH ((uint8_t)15U)
- #define ADC_OFFSET_LENGTH ((uint8_t)3U)
- #define ADC_OFFSET_SHIFT_LENGTH ((uint8_t)4U)
- void adc_deinit(uint32_t adc_periph)
- {
- switch(adc_periph){
- case ADC0:
-
- rcu_periph_reset_enable(RCU_ADC0RST);
- rcu_periph_reset_disable(RCU_ADC0RST);
- break;
- case ADC1:
-
- rcu_periph_reset_enable(RCU_ADC1RST);
- rcu_periph_reset_disable(RCU_ADC1RST);
- break;
- #ifndef GD32F10X_CL
- case ADC2:
- rcu_periph_reset_enable(RCU_ADC2RST);
- rcu_periph_reset_disable(RCU_ADC2RST);
- break;
- #endif
- default:
- break;
- }
- }
- void adc_mode_config(uint32_t mode)
- {
- ADC_CTL0(ADC0) &= ~(ADC_CTL0_SYNCM);
- ADC_CTL0(ADC0) |= mode;
- }
- void adc_special_function_config(uint32_t adc_periph, uint32_t function, ControlStatus newvalue)
- {
- if(newvalue){
- if(0U != (function & ADC_SCAN_MODE)){
-
- ADC_CTL0(adc_periph) |= ADC_SCAN_MODE;
- }
- if(0U != (function & ADC_INSERTED_CHANNEL_AUTO)){
-
- ADC_CTL0(adc_periph) |= ADC_INSERTED_CHANNEL_AUTO;
- }
- if(0U != (function & ADC_CONTINUOUS_MODE)){
-
- ADC_CTL1(adc_periph) |= ADC_CONTINUOUS_MODE;
- }
- }else{
- if(0U != (function & ADC_SCAN_MODE)){
-
- ADC_CTL0(adc_periph) &= ~ADC_SCAN_MODE;
- }
- if(0U != (function & ADC_INSERTED_CHANNEL_AUTO)){
-
- ADC_CTL0(adc_periph) &= ~ADC_INSERTED_CHANNEL_AUTO;
- }
- if(0U != (function & ADC_CONTINUOUS_MODE)){
-
- ADC_CTL1(adc_periph) &= ~ADC_CONTINUOUS_MODE;
- }
- }
- }
- void adc_data_alignment_config(uint32_t adc_periph, uint32_t data_alignment)
- {
- if(ADC_DATAALIGN_RIGHT != data_alignment){
-
- ADC_CTL1(adc_periph) |= ADC_CTL1_DAL;
- }else{
-
- ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DAL);
- }
- }
- void adc_enable(uint32_t adc_periph)
- {
- if(RESET == (ADC_CTL1(adc_periph) & ADC_CTL1_ADCON)){
-
- ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_ADCON;
- }
- }
- void adc_disable(uint32_t adc_periph)
- {
-
- ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ADCON);
- }
- void adc_calibration_enable(uint32_t adc_periph)
- {
-
- ADC_CTL1(adc_periph) |= (uint32_t) ADC_CTL1_RSTCLB;
-
- while(RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_RSTCLB)){
- }
-
- ADC_CTL1(adc_periph) |= ADC_CTL1_CLB;
-
- while(RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_CLB)){
- }
- }
- void adc_tempsensor_vrefint_enable(void)
- {
-
- ADC_CTL1(ADC0) |= ADC_CTL1_TSVREN;
- }
- void adc_tempsensor_vrefint_disable(void)
- {
-
- ADC_CTL1(ADC0) &= ~ADC_CTL1_TSVREN;
- }
- void adc_dma_mode_enable(uint32_t adc_periph)
- {
-
- ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_DMA);
- }
- void adc_dma_mode_disable(uint32_t adc_periph)
- {
-
- ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DMA);
- }
- void adc_discontinuous_mode_config(uint32_t adc_periph, uint8_t adc_channel_group, uint8_t length)
- {
-
- ADC_CTL0(adc_periph) &= ~((uint32_t)(ADC_CTL0_DISRC | ADC_CTL0_DISIC));
- switch(adc_channel_group){
- case ADC_REGULAR_CHANNEL:
-
- ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_DISNUM);
- ADC_CTL0(adc_periph) |= CTL0_DISNUM(((uint32_t)length - ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
-
- ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISRC;
- break;
- case ADC_INSERTED_CHANNEL:
-
- ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISIC;
- break;
- case ADC_CHANNEL_DISCON_DISABLE:
-
- default:
- break;
- }
- }
- void adc_channel_length_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t length)
- {
- switch(adc_channel_group){
- case ADC_REGULAR_CHANNEL:
-
- ADC_RSQ0(adc_periph) &= ~((uint32_t)ADC_RSQ0_RL);
- ADC_RSQ0(adc_periph) |= RSQ0_RL((uint32_t)(length-ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
- break;
- case ADC_INSERTED_CHANNEL:
-
- ADC_ISQ(adc_periph) &= ~((uint32_t)ADC_ISQ_IL);
- ADC_ISQ(adc_periph) |= ISQ_IL((uint32_t)(length-ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
- break;
- default:
- break;
- }
- }
- void adc_regular_channel_config(uint32_t adc_periph, uint8_t rank, uint8_t adc_channel, uint32_t sample_time)
- {
- uint32_t rsq,sampt;
-
-
- if(rank < ADC_REGULAR_CHANNEL_RANK_SIX){
-
- rsq = ADC_RSQ2(adc_periph);
- rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*rank)));
-
- rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*rank));
- ADC_RSQ2(adc_periph) = rsq;
- }else if(rank < ADC_REGULAR_CHANNEL_RANK_TWELVE){
-
- rsq = ADC_RSQ1(adc_periph);
- rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_SIX))));
-
- rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_SIX)));
- ADC_RSQ1(adc_periph) = rsq;
- }else if(rank < ADC_REGULAR_CHANNEL_RANK_SIXTEEN){
-
- rsq = ADC_RSQ0(adc_periph);
- rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_TWELVE))));
-
- rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_TWELVE)));
- ADC_RSQ0(adc_periph) = rsq;
- }else{
- }
-
-
- if(adc_channel < ADC_CHANNEL_SAMPLE_TEN){
-
- sampt = ADC_SAMPT1(adc_periph);
- sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel)));
-
- sampt |= (uint32_t)(sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel));
- ADC_SAMPT1(adc_periph) = sampt;
- }else if(adc_channel < ADC_CHANNEL_SAMPLE_EIGHTEEN){
-
- sampt = ADC_SAMPT0(adc_periph);
- sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN))));
-
- sampt |= (uint32_t)(sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN)));
- ADC_SAMPT0(adc_periph) = sampt;
- }else{
- }
- }
- void adc_inserted_channel_config(uint32_t adc_periph, uint8_t rank, uint8_t adc_channel, uint32_t sample_time)
- {
- uint8_t inserted_length;
- uint32_t isq,sampt;
-
- inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
-
- isq = ADC_ISQ(adc_periph);
- isq &= ~((uint32_t)(ADC_ISQ_ISQN << (ADC_INSERTED_CHANNEL_SHIFT_LENGTH-(inserted_length-rank)*ADC_INSERTED_CHANNEL_RANK_LENGTH)));
- isq |= ((uint32_t)adc_channel << (ADC_INSERTED_CHANNEL_SHIFT_LENGTH-(inserted_length-rank)*ADC_INSERTED_CHANNEL_RANK_LENGTH));
- ADC_ISQ(adc_periph) = isq;
-
- if(adc_channel < ADC_CHANNEL_SAMPLE_TEN){
-
- sampt = ADC_SAMPT1(adc_periph);
- sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel)));
-
- sampt |= (uint32_t) sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel);
- ADC_SAMPT1(adc_periph) = sampt;
- }else if(adc_channel < ADC_CHANNEL_SAMPLE_EIGHTEEN){
-
- sampt = ADC_SAMPT0(adc_periph);
- sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN))));
-
- sampt |= ((uint32_t)sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN)));
- ADC_SAMPT0(adc_periph) = sampt;
- }else{
- }
- }
- void adc_inserted_channel_offset_config(uint32_t adc_periph, uint8_t inserted_channel, uint16_t offset)
- {
- uint8_t inserted_length;
- uint32_t num = 0U;
- inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
- num = ((uint32_t)ADC_OFFSET_LENGTH - ((uint32_t)inserted_length - (uint32_t)inserted_channel));
-
- if(num <= ADC_OFFSET_LENGTH){
-
- num = num * ADC_OFFSET_SHIFT_LENGTH;
-
- REG32((adc_periph) + 0x14U + num) = IOFFX_IOFF((uint32_t)offset);
- }
- }
- void adc_external_trigger_source_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t external_trigger_source)
- {
- switch(adc_channel_group){
- case ADC_REGULAR_CHANNEL:
-
- ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSRC);
- ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
- break;
- case ADC_INSERTED_CHANNEL:
-
- ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSIC);
- ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
- break;
- default:
- break;
- }
- }
- void adc_external_trigger_config(uint32_t adc_periph, uint8_t adc_channel_group, ControlStatus newvalue)
- {
- if(newvalue){
- if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
-
- ADC_CTL1(adc_periph) |= ADC_CTL1_ETERC;
- }
- if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
-
- ADC_CTL1(adc_periph) |= ADC_CTL1_ETEIC;
- }
- }else{
- if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
-
- ADC_CTL1(adc_periph) &= ~ADC_CTL1_ETERC;
- }
- if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
-
- ADC_CTL1(adc_periph) &= ~ADC_CTL1_ETEIC;
- }
- }
- }
- void adc_software_trigger_enable(uint32_t adc_periph, uint8_t adc_channel_group)
- {
- if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
-
- ADC_CTL1(adc_periph) |= ADC_CTL1_SWRCST;
- }
- if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
-
- ADC_CTL1(adc_periph) |= ADC_CTL1_SWICST;
- }
- }
- uint16_t adc_regular_data_read(uint32_t adc_periph)
- {
- return (uint16_t)(ADC_RDATA(adc_periph));
- }
- uint16_t adc_inserted_data_read(uint32_t adc_periph, uint8_t inserted_channel)
- {
- uint32_t idata;
-
- switch(inserted_channel){
- case ADC_INSERTED_CHANNEL_0:
-
- idata = ADC_IDATA0(adc_periph);
- break;
- case ADC_INSERTED_CHANNEL_1:
-
- idata = ADC_IDATA1(adc_periph);
- break;
- case ADC_INSERTED_CHANNEL_2:
-
- idata = ADC_IDATA2(adc_periph);
- break;
- case ADC_INSERTED_CHANNEL_3:
-
- idata = ADC_IDATA3(adc_periph);
- break;
- default:
- idata = 0U;
- break;
- }
- return (uint16_t)idata;
- }
- uint32_t adc_sync_mode_convert_value_read(void)
- {
-
- return ADC_RDATA(ADC0);
- }
- void adc_watchdog_single_channel_enable(uint32_t adc_periph, uint8_t adc_channel)
- {
- ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC | ADC_CTL0_WDCHSEL);
-
- ADC_CTL0(adc_periph) |= (uint32_t)adc_channel;
- ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC);
- }
- void adc_watchdog_group_channel_enable(uint32_t adc_periph, uint8_t adc_channel_group)
- {
- ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC);
-
- switch(adc_channel_group){
- case ADC_REGULAR_CHANNEL:
-
- ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_RWDEN;
- break;
- case ADC_INSERTED_CHANNEL:
-
- ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_IWDEN;
- break;
- case ADC_REGULAR_INSERTED_CHANNEL:
-
- ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN);
- break;
- default:
- break;
- }
- }
- void adc_watchdog_disable(uint32_t adc_periph)
- {
- ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC | ADC_CTL0_WDCHSEL);
- }
- void adc_watchdog_threshold_config(uint32_t adc_periph, uint16_t low_threshold, uint16_t high_threshold)
- {
- ADC_WDLT(adc_periph) = (uint32_t)WDLT_WDLT(low_threshold);
- ADC_WDHT(adc_periph) = (uint32_t)WDHT_WDHT(high_threshold);
- }
- FlagStatus adc_flag_get(uint32_t adc_periph, uint32_t adc_flag)
- {
- FlagStatus reval = RESET;
- if(ADC_STAT(adc_periph) & adc_flag){
- reval = SET;
- }
- return reval;
- }
- void adc_flag_clear(uint32_t adc_periph, uint32_t adc_flag)
- {
- ADC_STAT(adc_periph) &= ~((uint32_t)adc_flag);
- }
- FlagStatus adc_regular_software_startconv_flag_get(uint32_t adc_periph)
- {
- FlagStatus reval = RESET;
- if((uint32_t)RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_SWRCST)){
- reval = SET;
- }
- return reval;
- }
- FlagStatus adc_inserted_software_startconv_flag_get(uint32_t adc_periph)
- {
- FlagStatus reval = RESET;
- if((uint32_t)RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_SWICST)){
- reval = SET;
- }
- return reval;
- }
- FlagStatus adc_interrupt_flag_get(uint32_t adc_periph, uint32_t adc_interrupt)
- {
- FlagStatus interrupt_flag = RESET;
- uint32_t state;
-
- switch(adc_interrupt){
- case ADC_INT_FLAG_WDE:
-
- state = ADC_STAT(adc_periph) & ADC_STAT_WDE;
- if((ADC_CTL0(adc_periph) & ADC_CTL0_WDEIE) && state){
- interrupt_flag = SET;
- }
- break;
- case ADC_INT_FLAG_EOC:
-
- state = ADC_STAT(adc_periph) & ADC_STAT_EOC;
- if((ADC_CTL0(adc_periph) & ADC_CTL0_EOCIE) && state){
- interrupt_flag = SET;
- }
- break;
- case ADC_INT_FLAG_EOIC:
-
- state = ADC_STAT(adc_periph) & ADC_STAT_EOIC;
- if((ADC_CTL0(adc_periph) & ADC_CTL0_EOICIE) && state){
- interrupt_flag = SET;
- }
- break;
- default:
- break;
- }
- return interrupt_flag;
- }
- void adc_interrupt_flag_clear(uint32_t adc_periph, uint32_t adc_interrupt)
- {
- ADC_STAT(adc_periph) &= ~((uint32_t)adc_interrupt);
- }
- void adc_interrupt_enable(uint32_t adc_periph, uint32_t adc_interrupt)
- {
-
- if(0U != (adc_interrupt & ADC_INT_WDE)){
- ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_WDEIE;
- }
-
- if(0U != (adc_interrupt & ADC_INT_EOC)){
- ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOCIE;
- }
-
- if(0U != (adc_interrupt & ADC_INT_EOIC)){
- ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOICIE;
- }
- }
- void adc_interrupt_disable(uint32_t adc_periph, uint32_t adc_interrupt)
- {
-
- if(0U != (adc_interrupt & ADC_INT_WDE)){
- ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_WDEIE;
- }
-
- if(0U != (adc_interrupt & ADC_INT_EOC)){
- ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_EOCIE;
- }
-
- if(0U != (adc_interrupt & ADC_INT_EOIC)){
- ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_EOICIE;
- }
- }
|