|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- #include "imxrt.h"
- #include "core_pins.h"
- #include "debug/printf.h"
- #include "avr/pgmspace.h"
-
- static uint8_t calibrating;
- static uint8_t analog_config_bits = 10;
- static uint8_t analog_num_average = 4;
-
-
- const uint8_t pin_to_channel[] = {
- 7,
- 8,
- 12,
- 11,
- 6,
- 5,
- 15,
- 0,
- 13,
- 14,
- 1,
- 2,
- 128+3,
- 128+4,
- 7,
- 8,
- 12,
- 11,
- 6,
- 5,
- 15,
- 0,
- 13,
- 14,
- 1,
- 2,
- 128+3,
- 128+4
- };
-
-
- static void wait_for_cal(void)
- {
-
- while (ADC1_GC & ADC_GC_CAL) ;
- while (ADC2_GC & ADC_GC_CAL) ;
-
- calibrating = 0;
-
- }
-
-
- int analogRead(uint8_t pin)
- {
- if (pin > sizeof(pin_to_channel)) return 0;
- if (calibrating) wait_for_cal();
- uint8_t ch = pin_to_channel[pin];
-
-
- if(!(ch & 0x80)) {
- ADC1_HC0 = ch;
- while (!(ADC1_HS & ADC_HS_COCO0)) ;
- return ADC1_R0;
- } else {
- ADC2_HC0 = ch & 0x7f;
- while (!(ADC2_HS & ADC_HS_COCO0)) ;
- return ADC2_R0;
- }
- }
-
- void analogReference(uint8_t type)
- {
- }
-
- void analogReadRes(unsigned int bits)
- {
- uint32_t tmp32, mode;
-
- if (bits == 8) {
-
- mode = ADC_CFG_MODE(0) | ADC_CFG_ADSTS(3);
- } else if (bits == 10) {
-
- mode = ADC_CFG_MODE(1) | ADC_CFG_ADSTS(2) | ADC_CFG_ADLSMP;
- } else {
-
- mode = ADC_CFG_MODE(2) | ADC_CFG_ADSTS(3) | ADC_CFG_ADLSMP;
- }
-
- tmp32 = (ADC1_CFG & (0xFFFFFC00));
- tmp32 |= (ADC1_CFG & (0x03));
- tmp32 |= (ADC1_CFG & (0xE0));
-
- tmp32 |= mode;
- ADC1_CFG = tmp32;
-
- tmp32 = (ADC2_CFG & (0xFFFFFC00));
- tmp32 |= (ADC2_CFG & (0x03));
- tmp32 |= (ADC2_CFG & (0xE0));
-
- tmp32 |= mode;
- ADC2_CFG = tmp32;
- }
-
- void analogReadAveraging(unsigned int num)
- {
- uint32_t mode, mode1;
-
-
- ADC1_GC &= ~0x20;
- mode = ADC1_CFG & ~0xC000;
- ADC2_GC &= ~0x20;
- mode1 = ADC2_CFG & ~0xC000;
-
- if (num >= 32) {
- mode |= ADC_CFG_AVGS(3);
- mode1 |= ADC_CFG_AVGS(3);
-
- } else if (num >= 16) {
- mode |= ADC_CFG_AVGS(2);
- mode1 |= ADC_CFG_AVGS(2);
-
- } else if (num >= 8) {
- mode |= ADC_CFG_AVGS(1);
- mode1 |= ADC_CFG_AVGS(1);
-
- } else if (num >= 4) {
- mode |= ADC_CFG_AVGS(0);
- mode1 |= ADC_CFG_AVGS(0);
-
- } else {
- mode |= 0;
- mode1 |= 0;
- }
-
- ADC1_CFG = mode;
- ADC2_CFG = mode1;
-
- if(num >= 4){
- ADC1_GC |= ADC_GC_AVGE;
- ADC2_GC |= ADC_GC_AVGE;
- }
- }
-
- #define MAX_ADC_CLOCK 20000000
-
- FLASHMEM void analog_init(void)
- {
- uint32_t mode, avg=0;
-
- printf("analogInit\n");
-
- CCM_CCGR1 |= CCM_CCGR1_ADC1(CCM_CCGR_ON);
- CCM_CCGR1 |= CCM_CCGR1_ADC2(CCM_CCGR_ON);
-
- if (analog_config_bits == 8) {
-
- mode = ADC_CFG_MODE(0) | ADC_CFG_ADSTS(3);
- } else if (analog_config_bits == 10) {
-
- mode = ADC_CFG_MODE(1) | ADC_CFG_ADSTS(2) | ADC_CFG_ADLSMP;
- } else {
-
- mode = ADC_CFG_MODE(2) | ADC_CFG_ADSTS(3) | ADC_CFG_ADLSMP;
- }
- if (analog_num_average >= 4) {
- if (analog_num_average >= 32) {
- mode |= ADC_CFG_AVGS(3);
- } else if (analog_num_average >= 16) {
- mode |= ADC_CFG_AVGS(2);
- } else if (analog_num_average >= 8) {
- mode |= ADC_CFG_AVGS(1);
- }
- avg = ADC_GC_AVGE;
- }
- #if 1
- mode |= ADC_CFG_ADIV(1) | ADC_CFG_ADICLK(3);
- #else
- uint32_t clock = F_BUS;
- if (clock > MAX_ADC_CLOCK*8) {
- mode |= ADC_CFG_ADIV(3) | ADC_CFG_ADICLK(1);
- } else if (clock > MAX_ADC_CLOCK*4) {
- mode |= ADC_CFG_ADIV(2) | ADC_CFG_ADICLK(1);
- } else if (clock > MAX_ADC_CLOCK*2) {
- mode |= ADC_CFG_ADIV(1) | ADC_CFG_ADICLK(1);
- } else if (clock > MAX_ADC_CLOCK) {
- mode |= ADC_CFG_ADIV(0) | ADC_CFG_ADICLK(1);
- } else {
- mode |= ADC_CFG_ADIV(0) | ADC_CFG_ADICLK(0);
- }
- #endif
-
- ADC1_CFG = mode | ADC_HC_AIEN | ADC_CFG_ADHSC;
- ADC1_GC = avg | ADC_GC_CAL;
- calibrating = 1;
- while (ADC1_GC & ADC_GC_CAL) ;
- calibrating = 0;
-
- ADC2_CFG = mode | ADC_HC_AIEN | ADC_CFG_ADHSC;
- ADC2_GC = avg | ADC_GC_CAL;
- calibrating = 1;
- while (ADC2_GC & ADC_GC_CAL) ;
- calibrating = 0;
- }
-
-
|