|
- #include "imxrt.h"
- #include "core_pins.h"
- #include "debug/printf.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,
- 128,
- 128,
- 128,
- 128,
- 7,
- 8,
- 12,
- 11,
- 6,
- 5,
- 15,
- 0,
- 13,
- 14,
- 1,
- 2
-
-
- };
-
-
- static void wait_for_cal(void)
- {
- printf("wait_for_cal\n");
- while (ADC1_GC & ADC_GC_CAL) ;
-
- calibrating = 0;
- printf("cal complete\n");
- }
-
-
- 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 > 15) return 0;
- ADC1_HC0 = ch;
- while (!(ADC1_HS & ADC_HS_COCO0)) ;
- return ADC1_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 & ((1u << 22)-1) << 10);
- tmp32 |= (ADC1_CFG & ((1u << 2)-1) << 0);
- tmp32 |= ADC1_CFG & (((1u << 3)-1) << 5);
- tmp32 |= mode;
- ADC1_CFG = tmp32;
-
- }
-
- void analogReadAveraging(unsigned int num)
- {
- uint32_t tmp32, mode;
-
-
- tmp32 = ADC1_GC;
- ADC1_GC &= ~0x20;
-
- mode = ADC1_CFG & ~0xC000;
- if (num >= 32) {
- mode |= ADC_CFG_AVGS(3);
-
- } else if (num >= 16) {
- mode |= ADC_CFG_AVGS(2);
- } else if (num >= 8) {
- mode |= ADC_CFG_AVGS(1);
- } else {
- mode |= ADC_CFG_AVGS(0);
- }
- ADC1_CFG = mode;
-
-
- ADC1_GC = tmp32;
- }
-
- #define MAX_ADC_CLOCK 20000000
-
- void analog_init(void)
- {
- uint32_t mode, avg=0;
-
- printf("analogInit\n");
-
- CCM_CCGR1 |= CCM_CCGR1_ADC1(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;
- }
-
|