Azure Sphere Code Snippets

HLApp

Code snippets for high-level application.

Digital In

Include:

#include <applibs/gpio.h>
#include <unistd.h>

Code:

// Initialize
int fd = GPIO_OpenAsInput(4);
if (fd < 0) /* Error handing */;

// Input
GPIO_Value_Type value;
if (GPIO_GetValue(fd, &value) != 0) /* Error handing */;
// value is GPIO_Value_High or GPIO_Value_Low

// Terminate
if (close(fd) != 0) /* Error handing */;

See also:

Digital Out

Include:

#include <applibs/gpio.h>
#include <unistd.h>

Code:

// Initialize
int fd = GPIO_OpenAsOutput(4, GPIO_OutputMode_PushPull, GPIO_Value_Low);
if (fd < 0) /* Error handing */;

// Output low
if (GPIO_SetValue(fd, GPIO_Value_Low) != 0) /* Error handing */;

// Terminate
if (close(fd) != 0) /* Error handing */;

See also:

I2C

Include:

#include <applibs/i2c.h>
#include <unistd.h>

Code:

// Initialize
int fd = I2CMaster_Open(1);
if (fd < 0) /* Error handing */;
if (I2CMaster_SetBusSpeed(fd, I2C_BUS_SPEED_STANDARD) != 0) /* Error handing */;
if (I2CMaster_SetTimeout(fd, 100) != 0) /* Error handing */;

// Write
const uint8_t writeData[] = { 0x2d, 0x08 };
if (I2CMaster_Write(fd, 0x53, writeData, sizeof(writeData)) != sizeof(writeData)) /* Error handing */;

// Read
uint8_t readData[6];
ssize_t readSize = I2CMaster_Read(fd, 0x53, readData, sizeof(readData));
if (readSize < 0) /* Error handing */;

// Write and read
const uint8_t write2Data[] = { 0x32 };
uint8_t read2Data[6];
ssize_t read2Size = I2CMaster_WriteThenRead(fd, 0x53, write2Data, sizeof(write2Data), read2Data, sizeof(read2Data));
if (read2Size < 0) /* Error handing */;

// Terminate
if (close(fd) != 0) /* Error handing */;

See also:

RTApp

Code snippets for real-time capable application.