Skip to content

Instantly share code, notes, and snippets.

@dayjaby
Created June 14, 2021 17:54
Show Gist options
  • Save dayjaby/886d0d734acbefbca890863d8b551630 to your computer and use it in GitHub Desktop.
Save dayjaby/886d0d734acbefbca890863d8b551630 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2015-2016 Wind River Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file C++ Synchronization demo. Uses basic C++ functionality.
*/
#define LOG_LEVEL LOG_LEVEL_DBG
#include <logging/log.h>
#include <stdio.h>
#include <zephyr.h>
#include <arch/cpu.h>
#include <sys/printk.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
#include <drivers/spi.h>
#include <console/console.h>
#include <net/net_config.h>
/**
* @class semaphore the basic pure virtual semaphore class
*/
class semaphore {
public:
virtual int wait(void) = 0;
virtual int wait(int timeout) = 0;
virtual void give(void) = 0;
};
/* specify delay between greetings (in ms); compute equivalent in ticks */
#define SLEEPTIME 5500
#define STACKSIZE 2000
struct k_thread coop_thread;
K_THREAD_STACK_DEFINE(coop_stack, STACKSIZE);
/*
* @class cpp_semaphore
* @brief Semaphore
*
* Class derives from the pure virtual semaphore class and
* implements it's methods for the semaphore
*/
class cpp_semaphore: public semaphore {
protected:
struct k_sem _sema_internal;
public:
cpp_semaphore();
virtual ~cpp_semaphore() {}
virtual int wait(void);
virtual int wait(int timeout);
virtual void give(void);
};
/*
* @brief cpp_semaphore basic constructor
*/
cpp_semaphore::cpp_semaphore()
{
printk("Create semaphore %p\n", this);
k_sem_init(&_sema_internal, 0, UINT_MAX);
}
/*
* @brief wait for a semaphore
*
* Test a semaphore to see if it has been signaled. If the signal
* count is greater than zero, it is decremented.
*
* @return 1 when semaphore is available
*/
int cpp_semaphore::wait(void)
{
k_sem_take(&_sema_internal, K_FOREVER);
return 1;
}
/*
* @brief wait for a semaphore within a specified timeout
*
* Test a semaphore to see if it has been signaled. If the signal
* count is greater than zero, it is decremented. The function
* waits for timeout specified
*
* @param timeout the specified timeout in ticks
*
* @return 1 if semaphore is available, 0 if timed out
*/
int cpp_semaphore::wait(int timeout)
{
return k_sem_take(&_sema_internal, K_MSEC(timeout));
}
/**
*
* @brief Signal a semaphore
*
* This routine signals the specified semaphore.
*
* @return N/A
*/
void cpp_semaphore::give(void)
{
k_sem_give(&_sema_internal);
}
cpp_semaphore sem_main;
cpp_semaphore sem_coop;
void coop_thread_entry(void)
{
struct k_timer timer;
k_timer_init(&timer, NULL, NULL);
while (1) {
/* wait for main thread to let us have a turn */
sem_coop.wait();
/* say "hello" */
printk("%s: Hello World!\n", __FUNCTION__);
/* wait a while, then let main thread have a turn */
k_timer_start(&timer, K_MSEC(SLEEPTIME), K_NO_WAIT);
k_timer_status_sync(&timer);
sem_main.give();
}
}
void main(void)
{
// console_init();
struct net_if *iface = net_if_get_default();
net_if_up(iface);
// net_config_init();
struct k_timer timer;
k_thread_create(&coop_thread, coop_stack, STACKSIZE,
(k_thread_entry_t) coop_thread_entry,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
k_timer_init(&timer, NULL, NULL);
while (1) {
printk("%s: Hello World!\n", __FUNCTION__);
k_timer_start(&timer, K_MSEC(SLEEPTIME), K_NO_WAIT);
printk("%s: Hello World!\n", __FUNCTION__);
k_timer_status_sync(&timer);
sem_coop.give();
/* Wait for coop thread to let us have a turn */
sem_main.wait();
//gpio_pin_set(led_tx, LED_RX_PIN, (int)0);
}
}
CONFIG_LOG=y
CONFIG_LOG_STRDUP_BUF_COUNT=32
CONFIG_CPLUSPLUS=y
CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=128
CONFIG_GPIO=y
CONFIG_SPI=y
CONFIG_SERIAL=y
CONFIG_UART_STM32=y
CONFIG_GPIO_STM32=y
CONFIG_SPI_STM32=y
CONFIG_SPI_STM32_USE_HW_SS=y
CONFIG_SPI_STM32_INTERRUPT=n
CONFIG_SOC_SERIES_STM32F4X=y
CONFIG_SOC_STM32F446XX=y
CONFIG_SHELL_BACKEND_SERIAL=y
CONFIG_SHELL_BACKENDS=y
CONFIG_SHELL=y
CONFIG_UART_SHELL_ON_DEV_NAME="UART_3"
CONFIG_BT=n
# Disable TCP and IPv4 (TCP disabled to avoid heavy traffic)
CONFIG_NET_TCP=n
CONFIG_NET_IPV4=n
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_SHELL=y
CONFIG_NET_LOG=y
CONFIG_NET_PKT_LOG_LEVEL_DBG=y
CONFIG_NET_CONFIG_NEED_IPV4=n
CONFIG_NET_CONFIG_MY_IPV4_ADDR=""
CONFIG_NET_CONFIG_PEER_IPV4_ADDR=""
CONFIG_NET_CONFIG_NEED_IPV6=y
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
CONFIG_NETWORKING=y
CONFIG_NET_CONFIG_AUTO_INIT=y
CONFIG_NET_CONFIG_INIT_PRIO=80
CONFIG_IEEE802154=y
CONFIG_IEEE802154_DW1000=y
CONFIG_NET_L2_IEEE802154=y
CONFIG_NET_L2_IEEE802154_SHELL=y
CONFIG_NET_L2_IEEE802154_LOG_LEVEL_INF=y
CONFIG_NET_CONFIG_IEEE802154_DEV_NAME="dw1000_spi_if"
CONFIG_NET_CONFIG_IEEE802154_CHANNEL=2
CONFIG_NET_PKT_RX_COUNT=6
CONFIG_NET_PKT_TX_COUNT=6
CONFIG_NET_BUF_DATA_SIZE=128
CONFIG_TEST_RANDOM_GENERATOR=y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment