Added blinky example.
Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
parent
108549e5ee
commit
e83a3c6a1e
|
@ -3,5 +3,6 @@ cmake_minimum_required(VERSION 3.10)
|
||||||
project(usbsio_demos)
|
project(usbsio_demos)
|
||||||
|
|
||||||
add_subdirectory(libusbsio)
|
add_subdirectory(libusbsio)
|
||||||
|
add_subdirectory(demos/blinky)
|
||||||
add_subdirectory(demos/i2cdetect)
|
add_subdirectory(demos/i2cdetect)
|
||||||
add_subdirectory(demos/dht_sensor)
|
add_subdirectory(demos/dht_sensor)
|
|
@ -0,0 +1,10 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
project(usbsio_blinky)
|
||||||
|
|
||||||
|
set(USBSIO_BLINKY_SRCS
|
||||||
|
"src/main.c"
|
||||||
|
)
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME} ${USBSIO_BLINKY_SRCS})
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE "usbsio")
|
|
@ -0,0 +1,101 @@
|
||||||
|
#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;
|
||||||
|
}
|
|
@ -10,16 +10,13 @@
|
||||||
/* AHT10 */
|
/* AHT10 */
|
||||||
#include "aht10.h"
|
#include "aht10.h"
|
||||||
|
|
||||||
#define NXP_VID 0x1FC9
|
|
||||||
#define MCULINK_PID 0x0143
|
|
||||||
|
|
||||||
static bool s_running = true;
|
static bool s_running = true;
|
||||||
|
|
||||||
static aht10_ret_t aht_impl_xfer(void *pdev, aht10_xfer_desc_t *xfer);
|
static aht10_ret_t aht_impl_xfer(void *pdev, aht10_xfer_desc_t *xfer);
|
||||||
static aht10_ret_t aht_impl_delay(void *pdev, uint32_t msec);
|
static aht10_ret_t aht_impl_delay(void *pdev, uint32_t msec);
|
||||||
static void signal_handler(int sig);
|
static void signal_handler(int sig);
|
||||||
|
|
||||||
int main(int argc, const char *argv) {
|
int main(int argc, const char *argv[]) {
|
||||||
int ret;
|
int ret;
|
||||||
LPC_HANDLE dev_handle;
|
LPC_HANDLE dev_handle;
|
||||||
LPC_HANDLE i2c_handle;
|
LPC_HANDLE i2c_handle;
|
||||||
|
@ -30,7 +27,7 @@ int main(int argc, const char *argv) {
|
||||||
signal(SIGTERM, signal_handler);
|
signal(SIGTERM, signal_handler);
|
||||||
|
|
||||||
/* Find all SIO devices. */
|
/* Find all SIO devices. */
|
||||||
ret = LPCUSBSIO_GetNumPorts(NXP_VID, MCULINK_PID);
|
ret = LPCUSBSIO_GetNumPorts(LPCUSBSIO_VID, MCULINKSIO_PID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
fprintf(stderr, "No MCU-Link devices found.\n");
|
fprintf(stderr, "No MCU-Link devices found.\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -6,9 +6,6 @@
|
||||||
/* USBSIO header */
|
/* USBSIO header */
|
||||||
#include <lpcusbsio.h>
|
#include <lpcusbsio.h>
|
||||||
|
|
||||||
#define NXP_VID 0x1FC9
|
|
||||||
#define MCULINK_PID 0x0143
|
|
||||||
|
|
||||||
static bool s_running = true;
|
static bool s_running = true;
|
||||||
|
|
||||||
static int i2c_detect(LPC_HANDLE handle, uint8_t port_num, uint8_t start_addr, uint8_t end_addr,
|
static int i2c_detect(LPC_HANDLE handle, uint8_t port_num, uint8_t start_addr, uint8_t end_addr,
|
||||||
|
@ -18,7 +15,7 @@ static int i2c_probe_addr(LPC_HANDLE i2c_handle, uint8_t addr);
|
||||||
|
|
||||||
static void signal_handler(int sig);
|
static void signal_handler(int sig);
|
||||||
|
|
||||||
int main(int argc, const char *argv) {
|
int main(int argc, const char *argv[]) {
|
||||||
int ret;
|
int ret;
|
||||||
LPC_HANDLE dev_handle;
|
LPC_HANDLE dev_handle;
|
||||||
|
|
||||||
|
@ -28,7 +25,7 @@ int main(int argc, const char *argv) {
|
||||||
signal(SIGTERM, signal_handler);
|
signal(SIGTERM, signal_handler);
|
||||||
|
|
||||||
/* Find all SIO devices. */
|
/* Find all SIO devices. */
|
||||||
ret = LPCUSBSIO_GetNumPorts(NXP_VID, MCULINK_PID);
|
ret = LPCUSBSIO_GetNumPorts(LPCUSBSIO_VID, MCULINKSIO_PID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
fprintf(stderr, "No MCU-Link devices found.\n");
|
fprintf(stderr, "No MCU-Link devices found.\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
|
Loading…
Reference in New Issue