|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
-
-
- #include "core_pins.h"
-
-
- static uint8_t calibrating;
- static uint8_t analog_right_shift = 0;
- static uint8_t analog_config_bits = 10;
- static uint8_t analog_num_average = 4;
- static uint8_t analog_reference_internal = 0;
-
-
-
-
- #if F_BUS == 128000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(3) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 120000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(3) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 108000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(3) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 96000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 90000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 80000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 72000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 64000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 60000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 56000000 || F_BUS == 54000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 48000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 40000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 36000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1)
- #elif F_BUS == 24000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #elif F_BUS == 16000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #elif F_BUS == 8000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #elif F_BUS == 4000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #elif F_BUS == 2000000
- #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
- #else
- #error "F_BUS must be 128, 120, 108, 96, 90, 80, 72, 64, 60, 56, 54, 48, 40, 36, 24, 4 or 2 MHz"
- #endif
-
- void analog_init(void)
- {
- uint32_t num;
-
- #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
- VREF_TRM = 0x60;
- VREF_SC = 0xE1;
- #endif
-
- if (analog_config_bits == 8) {
- ADC0_CFG1 = ADC_CFG1_8BIT + ADC_CFG1_MODE(0);
- ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
- #ifdef HAS_KINETIS_ADC1
- ADC1_CFG1 = ADC_CFG1_8BIT + ADC_CFG1_MODE(0);
- ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
- #endif
- } else if (analog_config_bits == 10) {
- ADC0_CFG1 = ADC_CFG1_10BIT + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
- ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
- #ifdef HAS_KINETIS_ADC1
- ADC1_CFG1 = ADC_CFG1_10BIT + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
- ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
- #endif
- } else if (analog_config_bits == 12) {
- ADC0_CFG1 = ADC_CFG1_12BIT + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
- ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
- #ifdef HAS_KINETIS_ADC1
- ADC1_CFG1 = ADC_CFG1_12BIT + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
- ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
- #endif
- } else {
- ADC0_CFG1 = ADC_CFG1_16BIT + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
- ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
- #ifdef HAS_KINETIS_ADC1
- ADC1_CFG1 = ADC_CFG1_16BIT + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
- ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
- #endif
- }
-
- #if defined(__MK20DX128__)
- if (analog_reference_internal) {
- ADC0_SC2 = ADC_SC2_REFSEL(1);
- } else {
- ADC0_SC2 = ADC_SC2_REFSEL(0);
- }
- #elif defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
- if (analog_reference_internal) {
- ADC0_SC2 = ADC_SC2_REFSEL(1);
- ADC1_SC2 = ADC_SC2_REFSEL(1);
- } else {
- ADC0_SC2 = ADC_SC2_REFSEL(0);
- ADC1_SC2 = ADC_SC2_REFSEL(0);
- }
- #elif defined(__MKL26Z64__)
- if (analog_reference_internal) {
- ADC0_SC2 = ADC_SC2_REFSEL(0);
- } else {
- ADC0_SC2 = ADC_SC2_REFSEL(1);
- }
- #endif
-
- num = analog_num_average;
- if (num <= 1) {
- ADC0_SC3 = ADC_SC3_CAL;
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_CAL;
- #endif
- } else if (num <= 4) {
- ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
- #endif
- } else if (num <= 8) {
- ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
- #endif
- } else if (num <= 16) {
- ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
- #endif
- } else {
- ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
- #endif
- }
- calibrating = 1;
- }
-
- static void wait_for_cal(void)
- {
- uint16_t sum;
-
-
- #if defined(HAS_KINETIS_ADC0) && defined(HAS_KINETIS_ADC1)
- while ((ADC0_SC3 & ADC_SC3_CAL) || (ADC1_SC3 & ADC_SC3_CAL)) {
-
- }
- #elif defined(HAS_KINETIS_ADC0)
- while (ADC0_SC3 & ADC_SC3_CAL) {
-
- }
- #endif
- __disable_irq();
- if (calibrating) {
-
- sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
- sum = (sum / 2) | 0x8000;
- ADC0_PG = sum;
-
-
-
- sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
- sum = (sum / 2) | 0x8000;
- ADC0_MG = sum;
-
-
-
- #ifdef HAS_KINETIS_ADC1
- sum = ADC1_CLPS + ADC1_CLP4 + ADC1_CLP3 + ADC1_CLP2 + ADC1_CLP1 + ADC1_CLP0;
- sum = (sum / 2) | 0x8000;
- ADC1_PG = sum;
- sum = ADC1_CLMS + ADC1_CLM4 + ADC1_CLM3 + ADC1_CLM2 + ADC1_CLM1 + ADC1_CLM0;
- sum = (sum / 2) | 0x8000;
- ADC1_MG = sum;
- #endif
- calibrating = 0;
- }
- __enable_irq();
- }
-
-
-
-
-
- #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
- #define DEFAULT 0
- #define INTERNAL 2
- #define INTERNAL1V2 2
- #define INTERNAL1V1 2
- #define EXTERNAL 0
-
- #elif defined(__MKL26Z64__)
- #define DEFAULT 0
- #define INTERNAL 0
- #define EXTERNAL 1
- #endif
-
- void analogReference(uint8_t type)
- {
- if (type) {
-
- if (!analog_reference_internal) {
- analog_reference_internal = 1;
- if (calibrating) {
- ADC0_SC3 = 0;
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = 0;
- #endif
- }
- analog_init();
- }
- } else {
-
- if (analog_reference_internal) {
- analog_reference_internal = 0;
- if (calibrating) {
- ADC0_SC3 = 0;
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = 0;
- #endif
- }
- analog_init();
- }
- }
- }
-
-
- void analogReadRes(unsigned int bits)
- {
- unsigned int config;
-
- if (bits >= 13) {
- if (bits > 16) bits = 16;
- config = 16;
- } else if (bits >= 11) {
- config = 12;
- } else if (bits >= 9) {
- config = 10;
- } else {
- config = 8;
- }
- analog_right_shift = config - bits;
- if (config != analog_config_bits) {
- analog_config_bits = config;
- if (calibrating) {
- ADC0_SC3 = 0;
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = 0;
- #endif
- }
- analog_init();
- }
- }
-
- void analogReadAveraging(unsigned int num)
- {
-
- if (calibrating) wait_for_cal();
- if (num <= 1) {
- num = 0;
- ADC0_SC3 = 0;
- } else if (num <= 4) {
- num = 4;
- ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(0);
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(0);
- #endif
- } else if (num <= 8) {
- num = 8;
- ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(1);
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(1);
- #endif
- } else if (num <= 16) {
- num = 16;
- ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(2);
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(2);
- #endif
- } else {
- num = 32;
- ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(3);
- #ifdef HAS_KINETIS_ADC1
- ADC1_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(3);
- #endif
- }
- analog_num_average = num;
- }
-
-
-
- #if defined(__MK20DX128__)
- static const uint8_t pin2sc1a[] = {
- 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 0, 19, 3, 21,
- 5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 0, 19, 3, 21,
- 26,
- 22,
- 23
- };
- #elif defined(__MK20DX256__)
- static const uint8_t pin2sc1a[] = {
- 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 0, 19, 3, 19+128,
- 5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
- 255, 255,
- 5+192, 5+128, 4+128, 6+128, 7+128, 4+192,
- 255, 255,
- 0, 19, 3, 19+128,
- 26,
- 18+128,
- 23
- };
- #elif defined(__MKL26Z64__)
- static const uint8_t pin2sc1a[] = {
- 5, 14, 8, 9, 13, 12, 6, 7, 15, 11, 0, 4+64, 23,
- 255,
- 5, 14, 8, 9, 13, 12, 6, 7, 15, 11, 0, 4+64, 23,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 26,
- 27
- };
- #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
- static const uint8_t pin2sc1a[] = {
- 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 3, 19+128, 14+128, 15+128,
- 5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
- 255, 255, 255, 255, 255, 255, 255,
- 14+128, 15+128, 17, 18, 4+128, 5+128, 6+128, 7+128, 17+128,
- 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 10+128, 11+128,
- 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255,
- 3, 19+128,
- 23, 23+128,
- 1, 1+128,
- 26,
- 18+128
- };
- #endif
-
-
-
-
- static volatile uint8_t analogReadBusyADC0 = 0;
- #ifdef HAS_KINETIS_ADC1
- static volatile uint8_t analogReadBusyADC1 = 0;
- #endif
-
- int analogRead(uint8_t pin)
- {
- int result;
- uint8_t channel;
-
-
-
-
- if (pin >= sizeof(pin2sc1a)) return 0;
- channel = pin2sc1a[pin];
- if (channel == 255) return 0;
-
- if (calibrating) wait_for_cal();
-
- #ifdef HAS_KINETIS_ADC1
- if (channel & 0x80) goto beginADC1;
- #endif
-
- __disable_irq();
- startADC0:
-
- #if defined(__MKL26Z64__)
- if (channel & 0x40) {
- ADC0_CFG2 &= ~ADC_CFG2_MUXSEL;
- channel &= 0x3F;
- } else {
- ADC0_CFG2 |= ADC_CFG2_MUXSEL;
- }
- #endif
- ADC0_SC1A = channel;
- analogReadBusyADC0 = 1;
- __enable_irq();
- while (1) {
- __disable_irq();
- if ((ADC0_SC1A & ADC_SC1_COCO)) {
- result = ADC0_RA;
- analogReadBusyADC0 = 0;
- __enable_irq();
- result >>= analog_right_shift;
- return result;
- }
-
-
-
- if (!analogReadBusyADC0) goto startADC0;
- __enable_irq();
- yield();
- }
-
- #ifdef HAS_KINETIS_ADC1
- beginADC1:
- __disable_irq();
- startADC1:
-
-
- if (channel & 0x40) {
- ADC1_CFG2 &= ~ADC_CFG2_MUXSEL;
- } else {
- ADC1_CFG2 |= ADC_CFG2_MUXSEL;
- }
- ADC1_SC1A = channel & 0x3F;
- analogReadBusyADC1 = 1;
- __enable_irq();
- while (1) {
- __disable_irq();
- if ((ADC1_SC1A & ADC_SC1_COCO)) {
- result = ADC1_RA;
- analogReadBusyADC1 = 0;
- __enable_irq();
- result >>= analog_right_shift;
- return result;
- }
-
-
-
- if (!analogReadBusyADC1) goto startADC1;
- __enable_irq();
- yield();
- }
- #endif
- }
-
- typedef int16_t __attribute__((__may_alias__)) aliased_int16_t;
-
- void analogWriteDAC0(int val)
- {
- #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
- SIM_SCGC2 |= SIM_SCGC2_DAC0;
- if (analog_reference_internal) {
- DAC0_C0 = DAC_C0_DACEN;
- } else {
- DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS;
- }
- __asm__ ("usat %[value], #12, %[value]\n\t" : [value] "+r" (val));
-
- *(volatile aliased_int16_t *)&(DAC0_DAT0L) = val;
- #elif defined(__MKL26Z64__)
- SIM_SCGC6 |= SIM_SCGC6_DAC0;
- if (analog_reference_internal == 0) {
-
- DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS | DAC_C0_DACSWTRG;
- } else {
-
- DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACSWTRG;
- }
- if (val < 0) val = 0;
- else if (val > 4095) val = 4095;
-
- *(volatile aliased_int16_t *)&(DAC0_DAT0L) = val;
- #endif
- }
-
-
- #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
- void analogWriteDAC1(int val)
- {
- SIM_SCGC2 |= SIM_SCGC2_DAC1;
- if (analog_reference_internal) {
- DAC1_C0 = DAC_C0_DACEN;
- } else {
- DAC1_C0 = DAC_C0_DACEN | DAC_C0_DACRFS;
- }
- __asm__ ("usat %[value], #12, %[value]\n\t" : [value] "+r" (val));
-
- *(volatile aliased_int16_t *)&(DAC1_DAT0L) = val;
- }
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|