I/O Controls (ioctls)

This topic covers Linux ioctls that are part of the header files shipped with the Azure Sphere SDK. The Linux ioctls are exposed for analog-to-digital converter (ADC) and pulse-width modulation (PWM) peripherals, general purpose I/O (gpio), universal asynchronous receiver-transmitters (UARTs), and CPU performance scaling (CPUFreq).

If you prefer to not use Linux ioctls, use the APIs available in the Azure Sphere runtime libraries.

The following file paths are supported in the Linux open() call for creating files and file descriptors for I/O functions:

  /dev/gpiochip<number>
  /dev/tty<affix><number>
  /dev/pwm<number>
  /dev/adc<number>

CPUFreq

The behavior of this ioctl is equivalent to setting the scaling governor policy attribute in sysfs. See scaling_governor. The azure_sphere_cpufreq_dev_scaling_governor_for_cpu struct definition can be found in usr/include/linux/cpufreq_dev.h

#define CPUFREQ_SET_SCALING_GOVERNOR_FOR_CPU _IOW('p', 0x0A, struct azure_sphere_cpufreq_dev_scaling_governor_for_cpu)

Parameters

  • Fd the file descriptor to open
  • CPUFREQ_SET_SCALING_GOVERNOR_FOR_CPU the request
  • &Sgn struct details

Sample code

int fd = open("/dev/cpufreq", O_WRONLY | O_CLOEXEC, 0);
if (fd >= 0) {
    struct azure_sphere_cpufreq_dev_scaling_governor_for_cpu sgn;
    sgn.cpu = 0;
    sgn.governor_name = "ondemand" //allowed values are conservative, ondemand, and performance
    int res = ioctl(fd, CPUFREQ_SET_SCALING_GOVERNOR_FOR_CPU, &sgn);
}

ADC

The following industrial I/O (IIO) ioctls expose ADC in Azure Sphere, and are defined in usr/include/linux/iio/ioctl.h:

  #define IIO_GET_DEVICE_INFO_BUFFER_TOTAL_SIZE_IOCTL _IOR('i', 0xD0, unsigned int)
  #define IIO_GET_DEVICE_INFO_BUFFER_IOCTL _IOWR('i', 0xD1, struct iio_ioctl_dev_info_buffer)
  #define IIO_GET_CHANNEL_SPEC_BUFFER_TOTAL_SIZE_IOCTL _IOWR('i', 0xD2, struct iio_ioctl_chan_spec_buffer_size)
  #define IIO_GET_CHANNEL_SPEC_BUFFER_IOCTL _IOWR('i', 0xD3, struct iio_ioctl_chan_spec_buffer)
  #define IIO_READ_RAW_CHANNEL_INFO_IOCTL _IOWR('i', 0xD4, struct iio_ioctl_raw_channel_info)
  #define IIO_WRITE_RAW_CHANNEL_INFO_IOCTL _IOWR('i', 0xD5, struct iio_ioctl_raw_channel_info)
  #define IIO_READ_CHANNEL_EXT_INFO_IOCTL _IOWR('i', 0xD6, struct iio_ioctl_read_chan_ext_info)
  #define IIO_WRITE_CHANNEL_EXT_INFO_IOCTL _IOWR('i', 0xD7, struct iio_ioctl_write_chan_ext_info)
  #define IIO_SCAN_MASK_QUERY_BIT_IOCTL _IOW('i', 0xD8, unsigned int)
  #define IIO_SCAN_MASK_SET_BIT_IOCTL_IOW('i', 0xD9, unsigned int)
  #define IIO_SCAN_MASK_CLEAR_BIT_IOCTL _IOW('i', 0xDA, unsigned int)
  #define IIO_BUFFER_GET_ENABLE_IOCTL _IO('i', 0xDB)
  #define IIO_BUFFER_SET_ENABLE_IOCTL _IOW('i', 0xDC, unsigned int)
  #define IIO_BUFFER_GET_LENGTH_IOCTL_IO('i', 0xDD)
  #define IIO_BUFFER_SET_LENGTH_IOCTL _IOW('i', 0xDE, unsigned int)
  #define IIO_BUFFER_GET_WATERMARK_IOCTL _IO('i', 0xDF)
  #define IIO_BUFFER_SET_WATERMARK_IOCTL _IOW('i', 0xE0, unsigned int)

For more information, see Use ADCs in high-level applications.

PWM

The following ioctls are exposed for PWM in the header file Sysroots/ApiSet/usr/include/linux/pwm.h (Linux OS):

 #define PWM_APPLY_STATE _IOW(0xf7, 0x01, struct pwm_chardev_params)
 #define PWM_GET_STATE _IOWR(0xf7, 0x02, struct pwm_chardev_params)

For more information on setting up the application manifest to gain access to these ioctls, see Application manifest settings and Use PWMs in high-level applications.

GPIO

The following ioctls are exposed for GPIO in the header file usr/include/linux/gpio.h:

  #define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data)
  #define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data)

  #define GPIO_GET_CHIPINFO_IOCTL _IOR(0xB4, 0x01, struct gpiochip_info)
  #define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info)
  #define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request)
  #define GPIO_GET_LINEEVENT_IOCTL _IOWR(0xB4, 0x04, struct gpioevent_request)

For more information on setting up the application manifest to gain access to these ioctls for GPIOs, see Application manifest settings, and Use GPIOs in high-level applications.

UART

For the list of ioctls exposed for terminals and serial lines relying on universal asynchronous receiver-transmitters (UARTs), see the Linux ioctls for terminals and serial lines.

For more information on setting up the application manifest to gain access to these ioctls for UARTs, see Application manifest settings and Use UARTs in high-level applications.