diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..65788ff --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +BOARD ?= 96b_carbon + +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/README.md b/README.md new file mode 100644 index 0000000..d360b61 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# 96Boards Carbon Basic Multi Thread Example +## Overview + +This example demonstrates spawning of multiple threads using K_THREAD_DEFINE. + +The example works by spawning three threads, each controlling a separate LED +on the 96Boards Carbon board. Each of these LEDs (USR1, USR2 and BT) have +their individual loop control and timing logic defined by separate functions. + +- blink1() controls the USR1 LED that has a 100ms sleep cycle +- blink2() controls the USR2 LED that has a 1000ms sleep cycle +- blink3() controls the BT LED that increases sleep by 100ms every cycle + upto 600ms and then resets to 100ms + + +Each thread is then defined at compile time using K_THREAD_DEFINE. + +Apart from that, a main() thread is also present that provides a basic shell +based on the shell sample at zephyr_base/samples/subsys/shell/shell/ + +The sample has been written keeping the 96Boards Carbon in mind, feel free to +modify it accordingly. +*** + +## Building + +### COMPILE +```shell +$ cd zephyr_base/path/to/sample/zephyr_multithread_blinky +$ make +``` + +### Flashing via OTG +```shell +$ sudo dfu-util -d [0483:df11] -a 0 -D outdir/96b_carbon/zephyr.bin -s 0x08000000 +``` +### Flash via UART +```shell +$ sudo stm32flash -b 115200 -g 0x0 -w outdir/96b_carbon/zephyr.bin /dev/ttyUSB0 +``` diff --git a/prj.conf b/prj.conf new file mode 100644 index 0000000..bb6a919 --- /dev/null +++ b/prj.conf @@ -0,0 +1,6 @@ +CONFIG_PRINTK=y +CONFIG_CONSOLE_SHELL=y +CONFIG_KERNEL_SHELL=y +CONFIG_OBJECT_TRACING=y +CONFIG_THREAD_MONITOR=y +CONFIG_INIT_STACKS=y diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..00066e1 --- /dev/null +++ b/src/Makefile @@ -0,0 +1 @@ +obj-y = main.o diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..cef15fe --- /dev/null +++ b/src/main.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include + +/* size of stack area used by each thread */ +#define STACKSIZE 128 + +/* scheduling priority used by each thread */ +#define PRIORITY 7 + +#define MY_SHELL_MODULE "sample_module" + +/*-----shell code fom /samples/subsys/shell/shell/------*/ +static int shell_cmd_ping(int argc, char *argv[]) +{ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + + printk("pong\n"); + + return 0; +} + +static int shell_cmd_params(int argc, char *argv[]) +{ + int cnt; + + printk("argc = %d\n", argc); + for (cnt = 0; cnt < argc; cnt++) { + printk(" argv[%d] = %s\n", cnt, argv[cnt]); + } + return 0; +} + +static struct shell_cmd commands[] = { + { "ping", shell_cmd_ping, NULL }, + { "params", shell_cmd_params, "print argc" }, + { NULL, NULL, NULL } +}; + +void main(void) +{ + SHELL_REGISTER(MY_SHELL_MODULE, commands); +} +/*-------------------------------------------------------------------*/ + + +void blink1(void) +{ + int cnt = 0; + struct device *gpioa; + + gpioa = device_get_binding("GPIOA"); + gpio_pin_configure(gpioa, 15, GPIO_DIR_OUT); + while (1) { + gpio_pin_write(gpioa, 15, (cnt + 1) % 2); + k_sleep(100); + cnt++; + } +} + +void blink2(void) +{ + int cnt = 0; + struct device *gpiod; + + gpiod = device_get_binding("GPIOD"); + gpio_pin_configure(gpiod, 2, GPIO_DIR_OUT); + while (1) { + gpio_pin_write(gpiod, 2, cnt % 2); + k_sleep(1000); + cnt++; + } +} + +void blink3(void) +{ + int cnt = 0, cnt2 = 0, sleep = 100; + struct device *gpiob; + + gpiob = device_get_binding("GPIOB"); + gpio_pin_configure(gpiob, 5, GPIO_DIR_OUT); + while (1) { + while (cnt2 != 5) { + gpio_pin_write(gpiob, 5, cnt2 % 2); + k_sleep(sleep); + sleep += 100; + cnt2++; + } + cnt2 = 0; + sleep = 100; + } +} + +K_THREAD_DEFINE(blink1_id, STACKSIZE, blink1, NULL, NULL, NULL, + PRIORITY, 0, K_NO_WAIT); +K_THREAD_DEFINE(blink2_id, STACKSIZE, blink2, NULL, NULL, NULL, + PRIORITY, 0, K_NO_WAIT); +K_THREAD_DEFINE(blink3_id, STACKSIZE, blink3, NULL, NULL, NULL, + PRIORITY, 0, K_NO_WAIT);