101 lines
2.6 KiB
C
101 lines
2.6 KiB
C
#include <signal.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
/* USBSIO header */
|
|
#include <lpcusbsio.h>
|
|
|
|
#define LINK_PRO_DIO1_PORT 0
|
|
#define LINK_PRO_DIO1_PIN 26
|
|
|
|
static bool s_running = true;
|
|
|
|
static void run_blinky(LPC_HANDLE hUsbSio);
|
|
static int gpio_config(LPC_HANDLE hUsbSio, uint8_t port, uint8_t pin, bool output);
|
|
static int gpio_write(LPC_HANDLE hUsbSio, uint8_t port, uint8_t pin, bool value);
|
|
|
|
static void signal_handler(int sig);
|
|
|
|
int main(int argc, const char *argv[]) {
|
|
int ret;
|
|
LPC_HANDLE dev_handle;
|
|
|
|
fprintf(stdout, "USBSIO example - Blinky\n");
|
|
|
|
signal(SIGINT, signal_handler);
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
/* Find all SIO devices. */
|
|
ret = LPCUSBSIO_GetNumPorts(LPCUSBSIO_VID, MCULINKSIO_PID);
|
|
if (ret == 0) {
|
|
fprintf(stderr, "No MCU-Link devices found.\n");
|
|
return -1;
|
|
}
|
|
|
|
/* Open device */
|
|
dev_handle = LPCUSBSIO_Open(0); /* For now, open the first device available. */
|
|
if (dev_handle == NULL) {
|
|
fprintf(stderr, "Failed to open device #0\n");
|
|
return -2;
|
|
}
|
|
|
|
fprintf(stdout, "%s\n", LPCUSBSIO_GetVersion(dev_handle));
|
|
|
|
uint32_t gpio_num_ports = LPCUSBSIO_GetNumGPIOPorts(dev_handle);
|
|
if (gpio_num_ports == 0) {
|
|
fprintf(stderr, "This device does not have GPIO ports.\n");
|
|
}
|
|
|
|
run_blinky(dev_handle);
|
|
|
|
LPCUSBSIO_Close(dev_handle);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void run_blinky(LPC_HANDLE hUsbSio) {
|
|
if (gpio_config(hUsbSio, LINK_PRO_DIO1_PORT, LINK_PRO_DIO1_PIN, true) < 0) {
|
|
fprintf(stderr, "Failed to configure GPIO.\r\n");
|
|
return;
|
|
}
|
|
|
|
fprintf(stdout, "The DIO1 (MOSI) pin on J19 should be blinking now.\n");
|
|
|
|
while (s_running) {
|
|
gpio_write(hUsbSio, LINK_PRO_DIO1_PORT, LINK_PRO_DIO1_PIN, true);
|
|
usleep(500 * 1000);
|
|
gpio_write(hUsbSio, LINK_PRO_DIO1_PORT, LINK_PRO_DIO1_PIN, false);
|
|
usleep(500 * 1000);
|
|
}
|
|
}
|
|
|
|
static int gpio_config(LPC_HANDLE hUsbSio, uint8_t port, uint8_t pin, bool output) {
|
|
int ret = 0;
|
|
|
|
ret = GPIO_ConfigIOPin(hUsbSio, port, pin, 0x100); /* This value is directly written into IOCON */
|
|
if (ret < 0) return ret;
|
|
|
|
if (output) {
|
|
ret = GPIO_SetPortOutDir(hUsbSio, port, (1U << pin));
|
|
} else {
|
|
ret = GPIO_SetPortInDir(hUsbSio, port, (1U << pin));
|
|
}
|
|
|
|
if (ret < 0) return ret;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int gpio_write(LPC_HANDLE hUsbSio, uint8_t port, uint8_t pin, bool value) {
|
|
if (value) {
|
|
return GPIO_SetPin(hUsbSio, port, pin);
|
|
} else {
|
|
return GPIO_ClearPin(hUsbSio, port, pin);
|
|
}
|
|
}
|
|
|
|
static void signal_handler(int sig) {
|
|
s_running = false;
|
|
} |