The MCU API provides an interface that allows you to do the following:

  • Control the peripherals that connect to the MCU, including GPIO, I2C, and UART.
  • Communicate with the host system.
  • Perform auxiliary functions.

For more information on the MCU API, see MCU_SDK_HOME/docs/api_doc/html/index.html, where MCU_SDK_HOME is your MCU SDK installation folder.

Note: The MCU API does not support floating point. Refer to mcu_api.h for detailed definitions of each function.

APIs to access HW resources

APIs to access GPIO

gpio_setup

Set up the input or output mode for one GPIO port. It is okay to call this API with different parameters for the same GPIO port. For example, you can set up a GPIO as an input and later as an output.

void gpio_setup(int gpio, int direction)

...where:

  • gpio: GPIO port number
  • direction: 0 = input, 1 = output

gpio_register_interrupt

Register an interrupt handler for one GPIO port.

int gpio_register_interrupt(int gpio, int mode, irq_handler_t isr);

...where:

  • gpio: GPIO port number
  • mode: 0 = falling edge interrupt, 1 = rising edge interrupt
  • isr: interrupt handler function
  • Return value: 0 if successful

gpio_read

Read the value of one GPIO port.

int gpio_read(int gpio)

...where:

  • gpio: GPIO port number
  • return value: 0 = low voltage, 1 = high voltage

gpio_write

Write the value of one GPIO port.

void gpio_write(int gpio, int value)

...where:

  • gpio: GPIO port number
  • value: 0 = low voltage, 1 = high voltage

APIs to access I2C

MCU can access I2C devices that connect to I2C8 bus, which is muxed with the I2C6 bus. You can easily get the raw data from the I2C device via MCU API. The MCU uses I2C-8, whereas the Intel® Atom™ processor uses I2C-6. You must program pin muxing before using I2C from Intel® Atom™ or MCU. By default, muxing is I2C-6, for example, for the Intel® Atom™ chip. For details, see Getting debug messages from the MCU.

i2c_read

Read the value of the register from a I2C device.

int i2c_read(int address, int reg, unsigned char *buf, int length)

...where:

  • address: I2C device address
  • reg: register address
  • buf: preallocated buffer for output values
  • length: length of the buffer
  • return value: 0 if successful

Note: We strongly recommend you do not use malloc in Viper. For MCU application development, we only support static buffers.

i2c_write

Write the value to the register of an I2C device.

int i2c_write(int address, int reg, unsigned char *buf, int length)

...where:

  • address: I2C device address
  • reg: register address
  • buf: buffer containing input values
  • length: length of the buffer
  • return value: 0 if successful

Note: MCU application can only access I2C8.

APIs to access UART

The MCU can access UART1 as well as UART2, which may be used as Linux kernel console as well. UART2 (ttyMFD2) is shared by both the host and the MCU. You can disable the output from the host side by running systemctl stopserial-getty@ttyMFD2.service on the Linux side.

uart_setup

Set up the baud rate of a UART device.

int uart_setup(unsigned char port, int baudrate)

...where:

  • port: UART port number:1 or 2
  • baudrate: baud rate of the UART
  • return value: 0 if successful

Supported baud rate values are:

  • 115,200
  • 57,600
  • 38,400
  • 28,800
  • 19,200
  • 14,400
  • 9600
  • 7200
  • 4800

The other UART parameters are as follows. These cannot be modified.

  • 8 bits data
  • 1 stop bit
  • no parity
  • no flow control

uart_read

Reads from the UART device.

int uart_read(unsigned char port, unsigned char *buf, int length)

...where:

  • port: UART port number:1 or 2
  • buf: the buffer to be read
  • length: length of the buffer
  • return value: 0 if successfu
  • l

uart_write

Writes to the UART device.

int uart_write(unsigned char port, unsigned char *buf, int length)

...where:

  • port: UART port number:1 or 2
  • buf: the buffer to be written
  • length: length of the buffer
  • return value: 0 if successful

APIs to communicate with host

host_send

Send raw data from the MCU to the host (/dev/ttymcu0).

int host_send(unsigned char *buf, int length)

...where:

  • buf: buffer containing the data to be sent
  • length: length of the buffer
  • return value: 0 if successful. Returns an error code in case of failure

host_receive

Receive raw data from the host (/dev/ttymcu0). This synchronous API fetches the data from the buffer. The buffer’s size is limited. New data will replace the old data if the data exceeds the buffer’s size limit.

int host_receive(unsigned char *buf, int length)

...where:

  • buf: the buffer to be received
  • length: length of the buffer
  • return value: size of data that received

APIs to control PWM

The MCU can access all PWM ports, from PWM 0 to PWM 3. The PWM period can vary from 104 ns to 218,453,000 ns.

pwm_configure

This API configures the PWM port.

int pwm_configure (unsigned char port, int duty_ns, int period_ns)

...where:

  • port: PWM port to configure: 0, 1, 2, 3
  • duty_ns: active pulse duration of the PWM waveform, in nanoseconds
  • period_ns: the whole period of the PWM waveform, in nanoseconds

pwm_enable

This API enables the PWM port.

int pwm_enable (unsigned char port)

...where:

  • port: PWM port to enable: 0, 1, 2, 3

pwm_disable

This API disables the PWM port.

int pwm_disable (unsigned char port)

...where:

  • port: PWM port to disable: 0, 1, 2, 3

Auxiliary API

debug_print

This API prints the MCU debug message to the host debug console, for example /dev/ttymcu1.

void debug_print(int level, const char *fmt, ...)

...where:

  • level: debug level of the output message, which could be one of the following: DBG_FATAL | DBG_ERROR | DBG_WARNING | DBG_INFO | DBG_DEBUG
  • fmt: format string

mcu_sleep

This API sleeps the MCU application.

void mcu_sleep(int ticks)

...where:

  • ticks: one tick is 10 ms for Edison MCU

time_ms

This API returns the number of milliseconds since the MCU has booted. This number will overflow (go back to zero) after 1193 hours.

unsigned long time_ms()

...where:

  • return value: the number of milliseconds.

time_us

This API returns the number of microseconds since the MCU has booted. This number will overflow (go back to zero) after 71 minutes.

unsigned long time_us()

...where:

  • return value: the number of microseconds.

mcu_delay

This API pauses the application for a period of time; it does not make the application sleep. Always call mcu_sleep if an application needs to sleep for more than one tick (10 ms).

int mcu_delay(int delay_us)

...where:

  • delay_us: the number of microsecond to pause.
  • return value: 0 if successful

mcu_snprintf

A simplified version of snprintf.

void mcu_snprintf(char *buf, unsigned int size, const char *fmt, ...)

...where:

  • buf: the buffer point to contain the data
  • size: size of buffer
  • fmt: format string
  • return value: the number of characters that have been written

Note: Only %d, %x, %s are supported in this API.

api_version

This API returns the version of the API.

int api_version()

...where:

  • return value: 100 multiples major version number plus minor version number
有关编译器优化的更完整信息,请参阅优化通知