STM32: Added basic OS abstraction (ChibiOS event), test app for this, minor changes in Makefile

This commit is contained in:
Pavel Kirienko 2014-04-02 21:56:31 +04:00
parent b465c0a303
commit 9c08f54e65
7 changed files with 203 additions and 4 deletions

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#pragma once
#if UAVCAN_STM32_CHIBIOS
# include <hal.h>
#else
# error "Unknown OS"
#endif
#include <uavcan/driver/can.hpp>
namespace uavcan_stm32
{
enum { CanFiltersPerIface = 14 };
class CanIface : public uavcan::ICanIface
{
CAN_TypeDef* can_;
uavcan::uint64_t error_cnt_;
public:
virtual uavcan::int16_t send(const uavcan::CanFrame& frame, uavcan::MonotonicTime tx_deadline,
uavcan::CanIOFlags flags);
virtual uavcan::int16_t receive(uavcan::CanFrame& out_frame, uavcan::MonotonicTime& out_ts_monotonic,
uavcan::UtcTime& out_ts_utc, uavcan::CanIOFlags& out_flags);
virtual uavcan::int16_t configureFilters(const uavcan::CanFilterConfig* filter_configs,
uavcan::uint16_t num_configs);
virtual uavcan::uint16_t getNumFilters() const { return CanFiltersPerIface; }
virtual uavcan::uint64_t getErrorCount() const { return error_cnt_; }
};
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#pragma once
#if UAVCAN_STM32_CHIBIOS
# include <ch.hpp>
#else
# error "Unknown OS"
#endif
#include <uavcan/uavcan.hpp>
namespace uavcan_stm32
{
#if UAVCAN_STM32_CHIBIOS
class Event
{
chibios_rt::CounterSemaphore sem_;
public:
Event() : sem_(0) { }
bool wait(uavcan::MonotonicDuration duration);
void signal();
void signalFromInterrupt();
};
#endif
}

View File

@ -0,0 +1,8 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#pragma once
#include <uavcan_stm32/can.hpp>
#include <uavcan_stm32/thread.hpp>

View File

@ -0,0 +1,5 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#include <uavcan_stm32/can.hpp>

View File

@ -0,0 +1,38 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#include <uavcan_stm32/thread.hpp>
namespace uavcan_stm32
{
#if UAVCAN_STM32_CHIBIOS
bool Event::wait(uavcan::MonotonicDuration duration)
{
msg_t ret = msg_t();
if (duration.isZero())
{
sem_.waitTimeout(TIME_IMMEDIATE);
}
else
{
sem_.waitTimeout(US2ST(duration.toUSec()));
}
return ret == RDY_OK;
}
void Event::signal()
{
sem_.signal();
}
void Event::signalFromInterrupt()
{
sem_.signalI();
}
#endif
}

View File

@ -10,6 +10,8 @@ PROJECT = uavcan_test_stm32f107
CPPSRC = src/main.cpp src/dummy.cpp
UDEFS = -DUAVCAN_STM32_CHIBIOS=1
#
# UAVCAN library
#
@ -23,7 +25,7 @@ CPPSRC += $(LIBUAVCAN_STM32_SRC)
UINCDIR += $(LIBUAVCAN_STM32_INC)
# TODO: add target for this
$(info $(shell $(LIBUAVCAN_DSDLC) $(UAVCAN_DSDL_DIR) -v))
$(info $(shell $(LIBUAVCAN_DSDLC) $(UAVCAN_DSDL_DIR)))
UINCDIR += dsdlc_generated
#
@ -36,7 +38,4 @@ UINCDIR += src/sys
SERIAL_CLI_PORT_NUMBER = 2
RELEASE_OPT = -Os -fomit-frame-pointer
DEBUG_OPT = -Os -g3
include crdr_chibios/rules_stm32f105_107.mk

View File

@ -2,6 +2,77 @@
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#include <unistd.h>
#include <crdr_chibios/sys/sys.h>
#include <uavcan_stm32/uavcan_stm32.hpp>
namespace app
{
namespace
{
void ledSet(bool state)
{
palWritePad(GPIO_PORT_LED, GPIO_PIN_LED, state);
}
int init()
{
halInit();
chibios_rt::System::init();
sdStart(&STDOUT_SD, NULL);
return 0;
}
__attribute__((noreturn))
void die(int status)
{
lowsyslog("Now I am dead x_x %i\n", status);
while (1) {
ledSet(false);
sleep(1);
ledSet(true);
sleep(1);
}
}
uavcan_stm32::Event led_turn_off_event;
class : public chibios_rt::BaseStaticThread<512>
{
msg_t main()
{
while (true)
{
led_turn_off_event.wait(uavcan::MonotonicDuration::getInfinite());
ledSet(false);
}
return msg_t();
}
} led_turn_off_executor;
}
}
int main()
{
const int init_res = app::init();
if (init_res != 0)
{
app::die(init_res);
}
app::led_turn_off_executor.start(LOWPRIO);
puts("Hello world");
while (true)
{
sleep(1);
app::ledSet(true);
sleep(1);
app::led_turn_off_event.signal();
}
}