diff --git a/libuavcan_drivers/stm32/test_stm32f107/Makefile b/libuavcan_drivers/stm32/test_stm32f107/Makefile index c2d8ab678b..dd7fb36a2f 100644 --- a/libuavcan_drivers/stm32/test_stm32f107/Makefile +++ b/libuavcan_drivers/stm32/test_stm32f107/Makefile @@ -10,8 +10,9 @@ PROJECT = uavcan_test_stm32f107 MAIN ?= main.cpp -CPPSRC = src/$(MAIN) \ - src/dummy.cpp +CPPSRC = src/$(MAIN) \ + src/dummy.cpp \ + src/board/board.cpp # # UAVCAN library @@ -37,10 +38,15 @@ $(info $(shell $(LIBUAVCAN_DSDLC) $(UAVCAN_DSDL_DIR))) UINCDIR += dsdlc_generated # -# Platform +# Git commit hash # -CSRC += src/sys/board.c +GIT_HASH := $(shell git rev-parse --short HEAD) +UDEFS += -DGIT_HASH=0x$(GIT_HASH) + +# +# Platform +# UINCDIR += src/sys diff --git a/libuavcan_drivers/stm32/test_stm32f107/src/board/board.cpp b/libuavcan_drivers/stm32/test_stm32f107/src/board/board.cpp new file mode 100644 index 0000000000..28779986db --- /dev/null +++ b/libuavcan_drivers/stm32/test_stm32f107/src/board/board.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2015 Pavel Kirienko + */ + +#include "board.hpp" +#include +#include +#include +#include +#include +#include + +/** + * GPIO config for ChibiOS PAL driver + */ +const PALConfig pal_default_config = +{ + { VAL_GPIOAODR, VAL_GPIOACRL, VAL_GPIOACRH }, + { VAL_GPIOBODR, VAL_GPIOBCRL, VAL_GPIOBCRH }, + { VAL_GPIOCODR, VAL_GPIOCCRL, VAL_GPIOCCRH }, + { VAL_GPIODODR, VAL_GPIODCRL, VAL_GPIODCRH }, + { VAL_GPIOEODR, VAL_GPIOECRL, VAL_GPIOECRH } +}; + +namespace board +{ + +void init() +{ + halInit(); + chibios_rt::System::init(); + sdStart(&STDOUT_SD, NULL); +} + +__attribute__((noreturn)) +void die(int error) +{ + lowsyslog("Fatal error %i\n", error); + while (1) + { + setLed(false); + ::sleep(1); + setLed(true); + ::sleep(1); + } +} + +void setLed(bool state) +{ + palWritePad(GPIO_PORT_LED, GPIO_PIN_LED, state); +} + +void restart() +{ + NVIC_SystemReset(); +} + +void readUniqueID(std::uint8_t bytes[UniqueIDSize]) +{ + std::memcpy(bytes, reinterpret_cast(0x1FFFF7E8), UniqueIDSize); +} + +} + +/* + * Early init from ChibiOS + */ +extern "C" +{ + +void __early_init(void) +{ + stm32_clock_init(); +} + +void boardInit(void) +{ + AFIO->MAPR |= + AFIO_MAPR_CAN_REMAP_REMAP3 | + AFIO_MAPR_CAN2_REMAP | + AFIO_MAPR_USART2_REMAP; +} + +} diff --git a/libuavcan_drivers/stm32/test_stm32f107/src/board/board.hpp b/libuavcan_drivers/stm32/test_stm32f107/src/board/board.hpp new file mode 100644 index 0000000000..8b69596455 --- /dev/null +++ b/libuavcan_drivers/stm32/test_stm32f107/src/board/board.hpp @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Pavel Kirienko + */ + +#pragma once + +#include + +namespace board +{ + +void init(); + +__attribute__((noreturn)) +void die(int error); + +void setLed(bool state); + +void restart(); + +constexpr unsigned UniqueIDSize = 12; + +void readUniqueID(std::uint8_t bytes[UniqueIDSize]); + +} diff --git a/libuavcan_drivers/stm32/test_stm32f107/src/main.cpp b/libuavcan_drivers/stm32/test_stm32f107/src/main.cpp index a5481c94a1..c8c2c113e6 100644 --- a/libuavcan_drivers/stm32/test_stm32f107/src/main.cpp +++ b/libuavcan_drivers/stm32/test_stm32f107/src/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "board/board.hpp" namespace app { @@ -27,16 +28,9 @@ Node& getNode() return *node_; } -void ledSet(bool state) -{ - palWritePad(GPIO_PORT_LED, GPIO_PIN_LED, state); -} - void init() { - halInit(); - chibios_rt::System::init(); - sdStart(&STDOUT_SD, NULL); + board::init(); int res = 0; do @@ -54,37 +48,58 @@ void init() while (res < 0); } -#if __GNUC__ -__attribute__((noreturn)) -#endif -void die(int status) -{ - lowsyslog("Initialization failure %i\n", status); - while (1) - { - ledSet(false); - sleep(1); - ledSet(true); - sleep(1); - } -} - class : public chibios_rt::BaseStaticThread<8192> { + void configureNodeInfo() + { + Node& node = app::getNode(); + + node.setNodeID(64); + node.setName("org.uavcan.stm32_test_stm32f107"); + + /* + * Software version + * TODO: Fill other fields too + */ + uavcan::protocol::SoftwareVersion swver; + + swver.vcs_commit = GIT_HASH; + swver.optional_field_flags = swver.OPTIONAL_FIELD_FLAG_VCS_COMMIT; + + node.setSoftwareVersion(swver); + + lowsyslog("Git commit hash: 0x%08x\n", GIT_HASH); + + /* + * Hardware version + * TODO: Fill other fields too + */ + uavcan::protocol::HardwareVersion hwver; + + std::uint8_t uid[board::UniqueIDSize] = {}; + board::readUniqueID(uid); + std::copy(std::begin(uid), std::end(uid), std::begin(hwver.unique_id)); + + node.setHardwareVersion(hwver); + + lowsyslog("UDID:"); + for (auto b : hwver.unique_id) + { + lowsyslog(" %02x", unsigned(b)); + } + lowsyslog("\n"); + } + public: msg_t main() { /* * Setting up the node parameters */ + configureNodeInfo(); + Node& node = app::getNode(); - node.setNodeID(64); - node.setName("org.uavcan.stm32_test_stm32f107"); - - // TODO: fill software version info (version number, VCS commit hash, ...) - // TODO: fill hardware version info (version number, unique ID) - /* * Initializing the UAVCAN node - this may take a while */ @@ -93,25 +108,10 @@ public: // Calling start() multiple times is OK - only the first successfull call will be effective int res = node.start(); -#if !UAVCAN_TINY - uavcan::NetworkCompatibilityCheckResult ncc_result; - if (res >= 0) - { - lowsyslog("Checking network compatibility...\n"); - res = node.checkNetworkCompatibility(ncc_result); - } -#endif - if (res < 0) { lowsyslog("Node initialization failure: %i, will try agin soon\n", res); } -#if !UAVCAN_TINY - else if (!ncc_result.isOk()) - { - lowsyslog("Network conflict with %u, will try again soon\n", ncc_result.conflicting_node.get()); - } -#endif else { break; @@ -127,7 +127,7 @@ public: const int res = time_sync_slave.start(); if (res < 0) { - die(res); + board::die(res); } } @@ -146,8 +146,10 @@ public: lowsyslog("Time sync master: %u\n", unsigned(time_sync_slave.getMasterNodeID().get())); - lowsyslog("Memory usage: used=%u free=%u\n", - node.getAllocator().getNumUsedBlocks(), node.getAllocator().getNumFreeBlocks()); + lowsyslog("Memory usage: free=%u used=%u worst=%u\n", + node.getAllocator().getNumFreeBlocks(), + node.getAllocator().getNumUsedBlocks(), + node.getAllocator().getPeakNumUsedBlocks()); lowsyslog("CAN errors: %lu %lu\n", static_cast(can.driver.getIface(0)->getErrorCount()), @@ -179,7 +181,7 @@ int main() { for (int i = 0; i < 200; i++) { - app::ledSet(app::can.driver.hadActivity()); + board::setLed(app::can.driver.hadActivity()); ::usleep(25000); } diff --git a/libuavcan_drivers/stm32/test_stm32f107/src/sys/board.c b/libuavcan_drivers/stm32/test_stm32f107/src/sys/board.c deleted file mode 100644 index 9243aebdd1..0000000000 --- a/libuavcan_drivers/stm32/test_stm32f107/src/sys/board.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2014 Pavel Kirienko - */ - -#include -#include - -const PALConfig pal_default_config = { - { VAL_GPIOAODR, VAL_GPIOACRL, VAL_GPIOACRH }, - { VAL_GPIOBODR, VAL_GPIOBCRL, VAL_GPIOBCRH }, - { VAL_GPIOCODR, VAL_GPIOCCRL, VAL_GPIOCCRH }, - { VAL_GPIODODR, VAL_GPIODCRL, VAL_GPIODCRH }, - { VAL_GPIOEODR, VAL_GPIOECRL, VAL_GPIOECRH } -}; - -void __early_init(void) -{ - stm32_clock_init(); -} - -void boardInit(void) -{ - AFIO->MAPR |= - AFIO_MAPR_CAN_REMAP_REMAP3 | - AFIO_MAPR_CAN2_REMAP | - AFIO_MAPR_USART2_REMAP; -}