2023-12-28 01:19:05 +08:00
|
|
|
# ADC Device
|
|
|
|
|
|
|
|
|
|
[中文](adc.md)
|
|
|
|
|
|
|
|
|
|
<!-- TOC -->
|
|
|
|
|
* [ADC Device](#adc-device)
|
|
|
|
|
* [Open ADC Device](#open-adc-device)
|
|
|
|
|
* [Close ADC Device](#close-adc-device)
|
|
|
|
|
* [Control ADC Device](#control-adc-device)
|
|
|
|
|
* [Set/Get Channel Number](#setget-channel-number)
|
2024-01-31 22:39:30 +08:00
|
|
|
* [Set/Get Channel Configure](#setget-channel-configure)
|
2023-12-28 01:19:05 +08:00
|
|
|
* [Read ADC Device Channel Value](#read-adc-device-channel-value)
|
|
|
|
|
* [Usage Example:](#usage-example)
|
|
|
|
|
<!-- TOC -->
|
|
|
|
|
|
|
|
|
|
## Open ADC Device
|
|
|
|
|
|
|
|
|
|
```c
|
2024-01-31 22:39:30 +08:00
|
|
|
int mr_dev_open(const char *path, int flags);
|
2023-12-28 01:19:05 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
| Parameter | Description |
|
|
|
|
|
|:----------------:|:-----------------:|
|
2024-01-31 22:39:30 +08:00
|
|
|
| path | Device path |
|
|
|
|
|
| flags | Open device flags |
|
2023-12-28 01:19:05 +08:00
|
|
|
| **Return Value** | |
|
|
|
|
|
| `>=0` | Device descriptor |
|
|
|
|
|
| `<0` | Error code |
|
|
|
|
|
|
2024-01-31 22:39:30 +08:00
|
|
|
- `path`: ADC device path usually is: `adcx`, `adc1`, `adc2`.
|
|
|
|
|
- `flags`: Open device flags, support `MR_O_RDONLY`.
|
2023-12-28 01:19:05 +08:00
|
|
|
|
2024-01-16 04:03:40 +08:00
|
|
|
Note: When using, the ADC device should be opened separately for different tasks according to actual situations, and the
|
2024-01-31 22:39:30 +08:00
|
|
|
appropriate `flags` should be used for management and permission control to ensure they will not interfere with each
|
2024-01-16 04:03:40 +08:00
|
|
|
other.
|
2023-12-28 01:19:05 +08:00
|
|
|
|
|
|
|
|
## Close ADC Device
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
int mr_dev_close(int desc);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
| Parameter | Description |
|
|
|
|
|
|------------------|--------------------|
|
|
|
|
|
| desc | Device descriptor |
|
|
|
|
|
| **Return Value** | |
|
|
|
|
|
| `=0` | Close successfully |
|
|
|
|
|
| `<0` | Error code |
|
|
|
|
|
|
2024-01-16 04:03:40 +08:00
|
|
|
Note: When closing the device, all channels will be automatically restored to the default state. The channel needs to be
|
|
|
|
|
reconfigured after reopening(This feature can be turned off).
|
2023-12-28 01:19:05 +08:00
|
|
|
|
|
|
|
|
## Control ADC Device
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
int mr_dev_ioctl(int desc, int cmd, void *args);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
| Parameter | Description |
|
|
|
|
|
|------------------|--------------------|
|
|
|
|
|
| desc | Device descriptor |
|
|
|
|
|
| cmd | Command code |
|
|
|
|
|
| args | Command parameters |
|
|
|
|
|
| **Return Value** | |
|
|
|
|
|
| `=0` | Set successfully |
|
|
|
|
|
| `<0` | Error code |
|
|
|
|
|
|
|
|
|
|
- `cmd`: Command code, supports the following commands:
|
2024-01-31 22:39:30 +08:00
|
|
|
- `MR_IOC_ADC_SET_CHANNEL`: Set channel number.
|
|
|
|
|
- `MR_IOC_ADC_SET_CHANNEL_CONFIG`: Set channel configure.
|
|
|
|
|
- `MR_IOC_ADC_GET_CHANNEL`: Get channel number.
|
|
|
|
|
- `MR_IOC_ADC_GET_CHANNEL_CONFIG`: Get channel configure.
|
2023-12-28 01:19:05 +08:00
|
|
|
|
|
|
|
|
### Set/Get Channel Number
|
|
|
|
|
|
|
|
|
|
Channel number range: `0` ~ `31`.
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
/* Define channel number */
|
|
|
|
|
#define CHANNEL_NUMBER 5
|
|
|
|
|
|
|
|
|
|
/* Set channel number */
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(ds, MR_IOC_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
2023-12-28 01:19:05 +08:00
|
|
|
|
|
|
|
|
/* Get channel number */
|
|
|
|
|
int number;
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(ds, MR_IOC_ADC_GET_CHANNEL, &number);
|
2023-12-28 01:19:05 +08:00
|
|
|
```
|
|
|
|
|
|
2024-01-18 03:29:46 +08:00
|
|
|
Independent of ADC interface:
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
/* Define channel number */
|
|
|
|
|
#define CHANNEL_NUMBER 5
|
|
|
|
|
|
|
|
|
|
/* Set channel number */
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
2024-01-18 03:29:46 +08:00
|
|
|
|
|
|
|
|
/* Get channel number */
|
|
|
|
|
int number;
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(ds, MR_IOC_GPOS, &number);
|
2024-01-18 03:29:46 +08:00
|
|
|
```
|
|
|
|
|
|
2024-01-16 04:03:40 +08:00
|
|
|
### Set/Get Channel Configure
|
2023-12-28 01:19:05 +08:00
|
|
|
|
2024-01-16 04:03:40 +08:00
|
|
|
Channel configure:
|
2023-12-28 01:19:05 +08:00
|
|
|
|
2024-01-16 04:03:40 +08:00
|
|
|
- `MR_DISABLE`: Disable channel.
|
|
|
|
|
- `MR_ENABLE`: Enable channel.
|
2023-12-28 01:19:05 +08:00
|
|
|
|
|
|
|
|
```c
|
2024-01-16 04:03:40 +08:00
|
|
|
/* Set channel configure */
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(ds, MR_IOC_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
|
2023-12-28 01:19:05 +08:00
|
|
|
|
2024-01-16 04:03:40 +08:00
|
|
|
/* Get channel configure */
|
2023-12-28 01:19:05 +08:00
|
|
|
int state;
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(ds, MR_IOC_ADC_GET_CHANNEL_CONFIG, &state);
|
2023-12-28 01:19:05 +08:00
|
|
|
```
|
|
|
|
|
|
2024-01-18 03:29:46 +08:00
|
|
|
Independent of ADC interface:
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
/* Set channel configure */
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(ds, MR_IOC_SCFG, MR_MAKE_LOCAL(int, MR_ENABLE));
|
2024-01-18 03:29:46 +08:00
|
|
|
|
|
|
|
|
/* Get channel configure */
|
|
|
|
|
int state;
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(ds, MR_IOC_GCFG, &state);
|
2024-01-18 03:29:46 +08:00
|
|
|
```
|
|
|
|
|
|
2023-12-28 01:19:05 +08:00
|
|
|
## Read ADC Device Channel Value
|
|
|
|
|
|
|
|
|
|
```c
|
2024-01-31 22:39:30 +08:00
|
|
|
ssize_t mr_dev_read(int desc, void *buf, size_t count);
|
2023-12-28 01:19:05 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
| Parameter | Description |
|
|
|
|
|
|------------------|-------------------|
|
|
|
|
|
| | |
|
|
|
|
|
| desc | Device descriptor |
|
|
|
|
|
| buf | Read data buffer |
|
2024-02-02 16:38:44 +08:00
|
|
|
| count | Read data size |
|
2023-12-28 01:19:05 +08:00
|
|
|
| **Return Value** | |
|
|
|
|
|
| `>=0` | Read data size |
|
|
|
|
|
| `<0` | Error code |
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
/* Read channel value */
|
|
|
|
|
uint32_t data;
|
|
|
|
|
int ret = mr_dev_read(ds, &data, sizeof(data));
|
|
|
|
|
/* Check if read successfully */
|
|
|
|
|
if (ret != sizeof(data))
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Note: The read data is the raw ADC data. The minimum read unit is `uint32_t`, that is, 4 bytes each time.
|
|
|
|
|
|
|
|
|
|
## Usage Example:
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
#include "include/mr_lib.h"
|
|
|
|
|
|
|
|
|
|
/* Define channel number */
|
|
|
|
|
#define CHANNEL_NUMBER 5
|
|
|
|
|
|
|
|
|
|
/* Define ADC device descriptor */
|
|
|
|
|
int adc_ds = -1;
|
|
|
|
|
|
2024-01-31 22:39:30 +08:00
|
|
|
void adc_init(void)
|
2023-12-28 01:19:05 +08:00
|
|
|
{
|
|
|
|
|
int ret = MR_EOK;
|
|
|
|
|
|
|
|
|
|
/* Initialize ADC */
|
2024-01-31 22:39:30 +08:00
|
|
|
adc_ds = mr_dev_open("adc1", MR_O_RDONLY);
|
2023-12-28 01:19:05 +08:00
|
|
|
if (adc_ds < 0)
|
|
|
|
|
{
|
|
|
|
|
mr_printf("ADC1 open failed: %s\r\n", mr_strerror(adc_ds));
|
2024-01-31 22:39:30 +08:00
|
|
|
return;
|
2023-12-28 01:19:05 +08:00
|
|
|
}
|
|
|
|
|
/* Print ADC descriptor */
|
|
|
|
|
mr_printf("ADC1 desc: %d\r\n", adc_ds);
|
|
|
|
|
/* Set to channel 5 */
|
2024-01-31 22:39:30 +08:00
|
|
|
mr_dev_ioctl(adc_ds, MR_IOC_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
2023-12-28 01:19:05 +08:00
|
|
|
/* Set channel enable */
|
2024-01-31 22:39:30 +08:00
|
|
|
ret = mr_dev_ioctl(adc_ds, MR_IOC_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
|
2023-12-28 01:19:05 +08:00
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Export to automatic initialization (APP level) */
|
2023-12-30 03:28:33 +08:00
|
|
|
MR_INIT_APP_EXPORT(adc_init);
|
2023-12-28 01:19:05 +08:00
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
/* Automatic initialization (adc_init function will be automatically called here) */
|
|
|
|
|
mr_auto_init();
|
|
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
uint32_t data;
|
|
|
|
|
int ret = mr_dev_read(adc_ds, &data, sizeof(data));
|
|
|
|
|
if (ret != sizeof(data))
|
|
|
|
|
{
|
|
|
|
|
mr_printf("Read failed: %s\r\n", mr_strerror(ret));
|
|
|
|
|
}
|
|
|
|
|
mr_printf("ADC value: %d\r\n", data);
|
|
|
|
|
mr_delay_ms(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Enable ADC1 channel 5, read the ADC value every second and print it.
|