ADC Device Driver¶
This document describes use of the generic ADC device driver in Nano-RK. For a running example please refer to the basic_adc project.
Adding the Driver to a Project
When adding the sensor driver to a project make sure to do the following:- Update NRK_MAX_DRIVER_CNT in nrk_cfg.h
- Add the driver source to the local makefile
- SRC += $(ROOT_DIR)/src/drivers/platform/$(PLATFORM_TYPE)/source/adc_driver.c
- Register the driver before nrk_start() is called
1 // Register the ADC device driver 2 val=nrk_register_driver( &dev_manager_adc,ADC_DEV_MANAGER); 3 if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to load my ADC driver\r\n") );
- Make sure ADC_DEV_MANAGER is uniquely defined in /src/drivers/include/nrk_driver_list.h
- Make sure to add the following includes to your source
- #include <nrk_driver_list.h>
- #include <nrk_driver.h>
- #include <adc_driver.h>
Opening the Device
The following sample of code will open an instance of the driver. This will initialize the ADC on the processor.
1int8_t fd;
2...
3fd=nrk_open(ADC_DEV_MANAGER,READ);
4 if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open adc driver\r\n"));
Selecting an ADC Channel
Using nrk_set_status( device, value, key) allows the application to configure which sensor value will be read by the nrk_read() command. device is the descriptor returned by nrk_open(). value is the command parameter you wish to set, in this case ADC_CHAN. key is the value of the channel you wish to select and can take the following values:- uint8_t with value 0-7
- CHAN_0
- CHAN_1
- CHAN_2
- CHAN_3
- CHAN_4
- CHAN_5
- CHAN_6
- CHAN_7
1val=nrk_set_status(fd,ADC_CHAN,5);
Reading the ADC
After a channel has been selected, the value can be read using the nrk_read(device,uint8_t *buf, uint8_t size) function. device is the device file descriptor returned by nrk_open(). buf is a pointer to the memory where the driver will write the value. size is the max size of the buffer (in this case 1 byte). Below is an example of reading the adc:
1uint8_t buf;
2...
3val=nrk_read(fd,&buf,1);
4printf( "ADC value=%d",buf);
Closing the Device
Closing the device is potentially important for saving on power and allowing multiple tasks to access the resource.
1nrk_close(fd);