void imx233_lradc_init(void) { arbiter_init(&channel_arbiter, HW_LRADC_NUM_CHANNELS); arbiter_init(&delay_arbiter, HW_LRADC_NUM_DELAYS); // enable block imx233_reset_block(&HW_LRADC_CTRL0); // disable ground ref __REG_CLR(HW_LRADC_CTRL0) = HW_LRADC_CTRL0__ONCHIP_GROUNDREF; // disable temperature sensors __REG_CLR(HW_LRADC_CTRL2) = HW_LRADC_CTRL2__TEMP_SENSOR_IENABLE0 | HW_LRADC_CTRL2__TEMP_SENSOR_IENABLE1; __REG_SET(HW_LRADC_CTRL2) = HW_LRADC_CTRL2__TEMPSENSE_PWD; // set frequency __REG_CLR(HW_LRADC_CTRL3) = HW_LRADC_CTRL3__CYCLE_TIME_BM; __REG_SET(HW_LRADC_CTRL3) = HW_LRADC_CTRL3__CYCLE_TIME__6MHz; // setup battery battery_chan = 7; imx233_lradc_reserve_channel(battery_chan); /* setup them for the simplest use: no accumulation, no division*/ imx233_lradc_setup_channel(battery_chan, false, false, 0, HW_LRADC_CHANNEL_BATTERY); /* setup delay channel for battery for automatic reading and scaling */ battery_delay_chan = 0; imx233_lradc_reserve_delay(battery_delay_chan); /* setup delay to trigger battery channel and retrigger itself. * The counter runs at 2KHz so a delay of 200 will trigger 10 * conversions per seconds */ imx233_lradc_setup_delay(battery_delay_chan, 1 << battery_chan, 1 << battery_delay_chan, 0, 200); imx233_lradc_kick_delay(battery_delay_chan); /* enable automatic conversion, use Li-Ion type battery */ imx233_lradc_setup_battery_conversion(true, HW_LRADC_CONVERSION__SCALE_FACTOR__LI_ION); }
int imx233_lradc_sense_ext_temperature(int chan, int sensor) { #define EXT_TEMP_ACC_COUNT 5 /* setup channel */ imx233_lradc_setup_channel(chan, false, false, 0, sensor); /* set current source to 300µA */ imx233_lradc_set_temp_isrc(sensor, HW_LRADC_CTRL2__TEMP_ISRC__300uA); /* read value and accumulate */ int a = 0; for(int i = 0; i < EXT_TEMP_ACC_COUNT; i++) { imx233_lradc_clear_channel(chan); imx233_lradc_kick_channel(chan); imx233_lradc_wait_channel(chan); a += imx233_lradc_read_channel(chan); } /* setup channel for small accumulation */ /* set current source to 20µA */ imx233_lradc_set_temp_isrc(sensor, HW_LRADC_CTRL2__TEMP_ISRC__20uA); /* read value */ int b = 0; for(int i = 0; i < EXT_TEMP_ACC_COUNT; i++) { imx233_lradc_clear_channel(chan); imx233_lradc_kick_channel(chan); imx233_lradc_wait_channel(chan); b += imx233_lradc_read_channel(chan); } /* disable sensor current */ imx233_lradc_set_temp_isrc(sensor, HW_LRADC_CTRL2__TEMP_ISRC__0uA); return (abs(b - a) / EXT_TEMP_ACC_COUNT) * 1104 / 1000; }
int imx233_lradc_sense_die_temperature(int nmos_chan, int pmos_chan) { imx233_lradc_setup_channel(nmos_chan, false, false, 0, HW_LRADC_CHANNEL_NMOS_THIN); imx233_lradc_setup_channel(pmos_chan, false, false, 0, HW_LRADC_CHANNEL_PMOS_THIN); // mux sensors __REG_CLR(HW_LRADC_CTRL2) = HW_LRADC_CTRL2__TEMPSENSE_PWD; imx233_lradc_clear_channel(nmos_chan); imx233_lradc_clear_channel(pmos_chan); // schedule both channels imx233_lradc_kick_channel(nmos_chan); imx233_lradc_kick_channel(pmos_chan); // wait completion imx233_lradc_wait_channel(nmos_chan); imx233_lradc_wait_channel(pmos_chan); // mux sensors __REG_SET(HW_LRADC_CTRL2) = HW_LRADC_CTRL2__TEMPSENSE_PWD; // do the computation int diff = imx233_lradc_read_channel(nmos_chan) - imx233_lradc_read_channel(pmos_chan); // return diff * 1.012 / 4 return (diff * 1012) / 4000; }
static void kick_measure(bool pull_x, bool pull_y, bool detect, int lradc_phys) { /* setup measurement: x- pull down and x+ pull up */ imx233_lradc_setup_touch(pull_x, pull_y, pull_x, pull_y, detect); imx233_lradc_enable_touch_detect_irq(false); imx233_lradc_enable_channel_irq(touch_chan, true); /* measure channel, no accumulation */ imx233_lradc_setup_channel(touch_chan, false, false, 0, lradc_phys); /* use a delay */ imx233_lradc_setup_delay(touch_delay, 1 << touch_chan, 0, 0, 8); imx233_lradc_kick_delay(touch_delay); }
void lradc_setup_channels_for_polling() { unsigned char i; int trigger_bitmap = 0; imx233_lradc_init(); for (i=0; i<MAX_USER_LRADC; i++) { imx233_lradc_setup_channel(i, 1, 0, 0, i); imx233_lradc_enable_channel_irq(i, false); imx233_lradc_clear_channel_irq(i); trigger_bitmap |= 1<<i; } imx233_lradc_setup_delay(LRADC_DELAY_INDEX, trigger_bitmap, 1<<LRADC_DELAY_INDEX, 0, 100); imx233_lradc_kick_delay(LRADC_DELAY_INDEX); }