Skip to content
Snippets Groups Projects
Select Git revision
  • a345b8cf788c00afc69d2f7b125e7a7afa1fdcbf
  • master default protected
  • fix-warnings
  • tvbgone-fixes
  • genofire/ble-follow-py
  • schneider/ble-stability-new-phy-adv
  • schneider/ble-stability
  • msgctl/gfx_rle
  • schneider/ble-stability-new-phy
  • add_menu_vibration
  • plaetzchen/ios-workaround
  • blinkisync-as-preload
  • schneider/max30001-pycardium
  • schneider/max30001-epicaridum
  • schneider/max30001
  • schneider/stream-locks
  • schneider/fundamental-test
  • schneider/ble-buffers
  • schneider/maxim-sdk-update
  • ch3/splashscreen
  • koalo/bhi160-works-but-dirty
  • v1.11
  • v1.10
  • v1.9
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0
  • release-1
  • bootloader-v1
  • v0.0
36 results

light_sensor.c

Blame
  • Forked from card10 / firmware
    Source project has a limited visibility.
    light_sensor.c 1.93 KiB
    #include "epicardium.h"
    #include "modules/log.h"
    #include "modules/modules.h"
    
    #include "mxc_config.h"
    #include "led.h"
    #include "adc.h"
    #include "gpio.h"
    
    #include "FreeRTOS.h"
    #include "timers.h"
    
    #define READ_FREQ pdMS_TO_TICKS(100)
    
    static uint16_t last_value;
    static TimerHandle_t poll_timer;
    static StaticTimer_t poll_timer_buffer;
    
    static int light_sensor_init()
    {
    	const sys_cfg_adc_t sys_adc_cfg =
    		NULL; /* No system specific configuration needed. */
    	if (ADC_Init(0x9, &sys_adc_cfg) != E_NO_ERROR) {
    		return -EINVAL;
    	}
    	GPIO_Config(&gpio_cfg_adc7);
    	return 0;
    }
    
    uint16_t epic_light_sensor_read()
    {
    	ADC_StartConvert(ADC_CH_7, 0, 0);
    	ADC_GetData(&last_value);
    	return last_value;
    }
    
    static void readAdcCallback()
    {
    	if (hwlock_acquire(HWLOCK_ADC, 0) != 0) {
    		/* Can't do much about this here ... Retry next time */
    		return;
    	}
    
    	ADC_StartConvert(ADC_CH_7, 0, 0);
    	ADC_GetData(&last_value);
    
    	hwlock_release(HWLOCK_ADC);
    }
    
    int epic_light_sensor_run()
    {
    	int ret = 0;
    
    	if (hwlock_acquire(HWLOCK_ADC, pdMS_TO_TICKS(500)) != 0) {
    		return -EBUSY;
    	}
    
    	light_sensor_init();
    
    	if (!poll_timer) {
    		poll_timer = xTimerCreateStatic(
    			"light_sensor_adc",
    			READ_FREQ,
    			pdTRUE,
    			NULL,
    			readAdcCallback,
    			&poll_timer_buffer
    		);
    		// since &poll_timer_buffer is not NULL, xTimerCreateStatic should allways succeed, so
    		// we don't need to check for poll_timer being NULL.
    	}
    	if (xTimerIsTimerActive(poll_timer) == pdFALSE) {
    		if (xTimerStart(poll_timer, 0) != pdPASS) {
    			ret = -EBUSY;
    		}
    	}
    
    	hwlock_release(HWLOCK_ADC);
    	return ret;
    }
    
    int epic_light_sensor_stop()
    {
    	if (!poll_timer || xTimerIsTimerActive(poll_timer) == pdFALSE) {
    		// timer wasn't running (or never started), just silently pass
    		return 0;
    	}
    
    	if (xTimerStop(poll_timer, 0) != pdPASS) {
    		return -EBUSY;
    	}
    
    	return 0;
    }
    
    int epic_light_sensor_get(uint16_t *value)
    {
    	if (!poll_timer || !xTimerIsTimerActive(poll_timer)) {
    		return -ENODATA;
    	}
    	*value = last_value;
    	return 0;
    }