platform: restructure (NuttX) architecture-specific code

updated: tone_alarm, px4io_serial, px4_micro_hal
This commit is contained in:
Beat Küng
2019-08-30 07:59:44 +02:00
parent 2fa3ee9336
commit ab43a83bed
111 changed files with 1640 additions and 404 deletions
@@ -0,0 +1,53 @@
############################################################################
#
# Copyright (c) 2015 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
# skip for px4_layer support on an IO board
if (NOT ${PX4_BOARD} MATCHES "px4_io")
add_library(px4_layer
console_buffer.cpp
px4_nuttx_tasks.c
px4_nuttx_impl.cpp
px4_init.cpp
)
target_link_libraries(px4_layer
PRIVATE
nuttx_apps # up_cxxinitialize
nuttx_sched
drivers_boards_common_arch
px4_work_queue
)
else()
add_library(px4_layer ${PX4_SOURCE_DIR}/src/platforms/empty.c)
endif()
add_dependencies(px4_layer prebuild_targets)
@@ -0,0 +1,242 @@
/****************************************************************************
*
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include <px4_config.h>
#include <px4_console_buffer.h>
#include <px4_defines.h>
#include <px4_sem.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>
#ifdef BOARD_ENABLE_CONSOLE_BUFFER
#ifndef BOARD_CONSOLE_BUFFER_SIZE
# define BOARD_CONSOLE_BUFFER_SIZE (1024*4) // default buffer size
#endif
static ssize_t console_buffer_write(struct file *filep, const char *buffer, size_t buflen);
class ConsoleBuffer
{
public:
void write(const char *buffer, size_t len);
void print(bool follow);
int size();
int read(char *buffer, int buffer_length, int *offset);
private:
void lock() { do {} while (px4_sem_wait(&_lock) != 0); }
void unlock() { px4_sem_post(&_lock); }
char _buffer[BOARD_CONSOLE_BUFFER_SIZE];
int _head{0};
int _tail{0};
px4_sem_t _lock = SEM_INITIALIZER(1);
bool _is_printing{false};
pthread_t _printing_task;
};
void ConsoleBuffer::print(bool follow)
{
lock();
_printing_task = pthread_self();
int i = _head;
while (true) {
_is_printing = true;
if (i < _tail) {
::write(1, _buffer + i, _tail - i);
} else if (_tail < i) {
::write(1, _buffer + i, BOARD_CONSOLE_BUFFER_SIZE - i);
::write(1, _buffer, _tail);
}
i = _tail;
_is_printing = false;
if (follow) {
unlock();
usleep(10000);
lock();
} else {
break;
}
}
unlock();
}
void ConsoleBuffer::write(const char *buffer, size_t len)
{
if (_is_printing && pthread_self() == _printing_task) { // avoid adding to the buffer while we are printing it
return;
}
lock(); // same rule as for printf: this cannot be used from IRQ handlers
for (size_t i = 0; i < len; ++i) {
_buffer[_tail] = buffer[i];
_tail = (_tail + 1) % BOARD_CONSOLE_BUFFER_SIZE;
if (_tail == _head) {
_head = (_head + 1) % BOARD_CONSOLE_BUFFER_SIZE;
}
}
unlock();
}
int ConsoleBuffer::size()
{
lock();
int size;
if (_head <= _tail) {
size = _tail - _head;
} else {
size = BOARD_CONSOLE_BUFFER_SIZE - (_head - _tail);
}
unlock();
return size;
}
int ConsoleBuffer::read(char *buffer, int buffer_length, int *offset)
{
lock();
if (*offset == -1) {
*offset = _head;
}
int size = 0;
if (*offset < _tail) {
size = _tail - *offset;
if (size > buffer_length) {
size = buffer_length;
}
memcpy(buffer, _buffer + *offset, size);
} else if (_tail < *offset) {
size = BOARD_CONSOLE_BUFFER_SIZE - *offset;
if (size > buffer_length) {
size = buffer_length;
}
memcpy(buffer, _buffer + *offset, size);
buffer += size;
buffer_length -= size;
int size_secondary = _tail;
if (size_secondary > buffer_length) {
size_secondary = buffer_length;
}
if (size_secondary > 0) {
memcpy(buffer, _buffer, size_secondary);
size += size_secondary;
}
}
unlock();
*offset = (*offset + size) % BOARD_CONSOLE_BUFFER_SIZE;
return size;
}
static ConsoleBuffer g_console_buffer;
void px4_console_buffer_print(bool follow)
{
g_console_buffer.print(follow);
}
ssize_t console_buffer_write(struct file *filep, const char *buffer, size_t len)
{
g_console_buffer.write(buffer, len);
// stderr still points to our original console and is available from everywhere, so we output to that.
// We could use up_putc() as well, but it is considerably less efficient.
// The drawback here is that a module writing to stderr bypasses the console buffer.
write(2, buffer, len);
fsync(2);
return len;
}
static const struct file_operations g_console_buffer_fops = {
NULL, /* open */
NULL, /* close */
NULL, /* read */
console_buffer_write, /* write */
NULL, /* seek */
NULL /* ioctl */
#ifndef CONFIG_DISABLE_POLL
, NULL /* poll */
#endif
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, NULL /* unlink */
#endif
};
int px4_console_buffer_init()
{
return register_driver(CONSOLE_BUFFER_DEVICE, &g_console_buffer_fops, 0666, NULL);
}
int px4_console_buffer_size()
{
return g_console_buffer.size();
}
int px4_console_buffer_read(char *buffer, int buffer_length, int *offset)
{
return g_console_buffer.read(buffer, buffer_length, offset);
}
#endif /* BOARD_ENABLE_CONSOLE_BUFFER */
+127
View File
@@ -0,0 +1,127 @@
/****************************************************************************
*
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "px4_init.h"
#include <px4_config.h>
#include <px4_console_buffer.h>
#include <px4_defines.h>
#include <drivers/drv_hrt.h>
#include <lib/parameters/param.h>
#include <px4_work_queue/WorkQueueManager.hpp>
#include <systemlib/cpuload.h>
#include <fcntl.h>
#include "platform/cxxinitialize.h"
int px4_platform_init(void)
{
#if defined(CONFIG_HAVE_CXX) && defined(CONFIG_HAVE_CXXINITIALIZE)
/* run C++ ctors before we go any further */
up_cxxinitialize();
# if defined(CONFIG_SYSTEM_NSH_CXXINITIALIZE)
# error CONFIG_SYSTEM_NSH_CXXINITIALIZE Must not be defined! Use CONFIG_HAVE_CXX and CONFIG_HAVE_CXXINITIALIZE.
# endif
#else
# error platform is dependent on c++ both CONFIG_HAVE_CXX and CONFIG_HAVE_CXXINITIALIZE must be defined.
#endif
#if !defined(CONFIG_DEV_CONSOLE) && defined(CONFIG_DEV_NULL)
/* Support running nsh on a board with out a console
* Without this the assumption that the fd 0..2 are
* std{in..err} will be wrong. NSH will read/write to the
* fd it opens for the init script or nested scripts assigned
* to fd 0..2.
*
*/
int fd = open("/dev/null", O_RDWR);
if (fd == 0) {
/* Successfully opened /dev/null as stdin (fd == 0) */
(void)fs_dupfd2(0, 1);
(void)fs_dupfd2(0, 2);
(void)fs_fdopen(0, O_RDONLY, NULL);
(void)fs_fdopen(1, O_WROK | O_CREAT, NULL);
(void)fs_fdopen(2, O_WROK | O_CREAT, NULL);
} else {
/* We failed to open /dev/null OR for some reason, we opened
* it and got some file descriptor other than 0.
*/
if (fd > 0) {
(void)close(fd);
}
return -ENFILE;
}
#endif
int ret = px4_console_buffer_init();
if (ret < 0) {
return ret;
}
// replace stdout with our buffered console
int fd_buf = open(CONSOLE_BUFFER_DEVICE, O_WRONLY);
if (fd_buf >= 0) {
dup2(fd_buf, 1);
// keep stderr(2) untouched: the buffered console will use it to output to the original console
close(fd_buf);
}
hrt_init();
param_init();
/* configure CPU load estimation */
#ifdef CONFIG_SCHED_INSTRUMENTATION
cpuload_initialize_once();
#endif
px4::WorkQueueManagerStart();
return PX4_OK;
}
@@ -0,0 +1,57 @@
/****************************************************************************
*
* Copyright (c) 2014 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file px4_nuttx_impl.cpp
*
* PX4 Middleware Wrapper NuttX Implementation
*/
#include <px4_defines.h>
#include <px4_middleware.h>
#include <drivers/drv_hrt.h>
namespace px4
{
void init(int argc, char *argv[], const char *process_name)
{
printf("process: %s\n", process_name);
}
uint64_t get_time_micros()
{
return hrt_absolute_time();
}
}
@@ -0,0 +1,115 @@
/****************************************************************************
*
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
* Author: @author Lorenz Meier <lm@inf.ethz.ch>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file px4_nuttx_tasks.c
* Implementation of existing task API for NuttX
*/
#include <nuttx/config.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sched.h>
#include <errno.h>
#include <stdbool.h>
#include <px4_config.h>
#include <px4_log.h>
#include <px4_tasks.h>
void
px4_systemreset(bool to_bootloader)
{
board_set_bootload_mode(to_bootloader ? board_reset_enter_bootloader : board_reset_normal);
board_system_reset(to_bootloader ? 1 : 0);
#if defined BOARD_HAS_NO_RESET
/* In case there is no HW support Just exit*/
PX4_WARN("System Reset Called");
exit(1);
#endif
}
int px4_task_spawn_cmd(const char *name, int scheduler, int priority, int stack_size, main_t entry, char *const argv[])
{
int pid;
sched_lock();
#if !defined(CONFIG_DISABLE_ENVIRON)
/* None of the modules access the environment variables (via getenv() for instance), so delete them
* all. They are only used within the startup script, and NuttX automatically exports them to the children
* tasks.
* This frees up a considerable amount of RAM.
*/
clearenv();
#endif
/* create the task */
pid = task_create(name, priority, stack_size, entry, argv);
if (pid > 0) {
/* configure the scheduler */
struct sched_param param;
param.sched_priority = priority;
sched_setscheduler(pid, scheduler, &param);
/* XXX do any other private task accounting here before the task starts */
}
sched_unlock();
return pid;
}
int px4_task_delete(int pid)
{
return task_delete(pid);
}
const char *px4_get_taskname(void)
{
#if CONFIG_TASK_NAME_SIZE > 0
FAR struct tcb_s *thisproc = sched_self();
return thisproc->name;
#else
return "app";
#endif
}
@@ -0,0 +1,56 @@
/****************************************************************************
*
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#pragma once
__BEGIN_DECLS
#include <nuttx/spi/spi.h>
#include <nuttx/i2c/i2c_master.h>
/* For historical reasons (NuttX STM32 numbering) PX4 bus numbering is 1 based
* All PX4 code, including, board code is written to assuming 1 based numbering.
* The following macros are used to allow the board config to define the bus
* numbers in terms of the NuttX driver numbering. 1,2,3 for one based numbering
* schemes or 0,1,2 for zero based schemes.
*/
#define PX4_BUS_NUMBER_TO_PX4(x) ((x)+PX4_BUS_OFFSET) /* Use to define Zero based to match Nuttx Driver but provide 1 based to PX4 */
#define PX4_BUS_NUMBER_FROM_PX4(x) ((x)-PX4_BUS_OFFSET) /* Use to map PX4 1 based to NuttX driver 0 based */
#define px4_enter_critical_section() enter_critical_section()
#define px4_leave_critical_section(flags) leave_critical_section(flags)
#include <arch/board/board.h>
__END_DECLS