Skip to content

Commit

Permalink
added sample code
Browse files Browse the repository at this point in the history
Signed-off-by: Sahaj Sarup <[email protected]>
  • Loading branch information
ric96 committed Oct 24, 2017
1 parent 737a61e commit bf6df4e
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BOARD ?= 96b_carbon

CONF_FILE = prj.conf

include ${ZEPHYR_BASE}/Makefile.inc
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
6 changes: 6 additions & 0 deletions prj.conf
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
obj-y = main.o
102 changes: 102 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <zephyr.h>
#include <device.h>
#include <gpio.h>
#include <misc/printk.h>
#include <shell/shell.h>

/* 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 <zephyr_base>/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);

0 comments on commit bf6df4e

Please sign in to comment.