restore UAVCAN bootloader support

- essentially reverting #7878 minus the obsolete board support
This commit is contained in:
Daniel Agar
2020-04-19 16:10:09 -04:00
committed by GitHub
parent 7c533e5a53
commit a3ad710623
30 changed files with 6658 additions and 3 deletions
+11
View File
@@ -60,6 +60,17 @@ if("${PX4_BOARD_LABEL}" STREQUAL "bootloader")
bootloader_lib
drivers_board
)
elseif("${PX4_BOARD_LABEL}" STREQUAL "canbootloader")
set(SCRIPT_PREFIX ${PX4_BOARD_LABEL}_)
add_subdirectory(src/canbootloader)
list(APPEND nuttx_libs
canbootloader
drivers_board
)
target_link_libraries(px4 PRIVATE
-Wl,-wrap,sched_process_timer -Wl,-wrap,sem_post -Wl,-wrap,sem_wait
)
else()
if(NOT "${PX4_BOARD_LINKER_PREFIX}" STREQUAL "")
set(SCRIPT_PREFIX ${PX4_BOARD_LINKER_PREFIX}-)
@@ -0,0 +1,48 @@
############################################################################
#
# Copyright (c) 2020 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.
#
############################################################################
px4_add_library(canbootloader
arch/stm32/board_identity.c
arch/stm32/drivers/can/driver.c
common/boot_app_shared.c
fs/flash.c
protocol/uavcan.c
sched/timer.c
uavcan/main.c
util/crc.c
util/random.c
)
include_directories(include)
target_include_directories(canbootloader INTERFACE include)
target_compile_options(canbootloader PRIVATE -Wno-error)
@@ -0,0 +1,56 @@
/****************************************************************************
*
* Copyright (C) 2017 PX4 Development Team. All rights reserved.
* Author: @author David Sidrane <david_s5@nscdg.com>
*
* 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 board_identity.c
* Implementation of STM32 based Board identity API
*/
#include <px4_config.h>
#include <stdio.h>
#include <string.h>
#define SWAP_UINT32(x) (((x) >> 24) | (((x) & 0x00ff0000) >> 8) | (((x) & 0x0000ff00) << 8) | ((x) << 24))
int board_get_mfguid(mfguid_t mfgid)
{
uint32_t *chip_uuid = (uint32_t *) STM32_SYSMEM_UID;
uint32_t *rv = (uint32_t *) &mfgid[0];
for (int i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) {
*rv++ = SWAP_UINT32(chip_uuid[(PX4_CPU_UUID_WORD32_LENGTH - 1) - i]);
}
return PX4_CPU_MFGUID_BYTE_LENGTH;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,204 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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 <nuttx/config.h>
#include <stdint.h>
#include <string.h>
#include "chip.h"
#include "stm32.h"
#include <errno.h>
#include "boot_app_shared.h"
#include "crc.h"
#define BOOTLOADER_COMMON_APP_SIGNATURE 0xB0A04150u
#define BOOTLOADER_COMMON_BOOTLOADER_SIGNATURE 0xB0A0424Cu
/* CAN_FiRx where (i=0..27|13, x=1, 2)
* STM32_CAN1_FIR(i,x)
* Using i = 2 does not requier there block
* to be enabled nor FINIT in CAN_FMR to be set.
* todo:Validate this claim on F2, F3
*/
#define crc_HiLOC STM32_CAN1_FIR(2,1)
#define crc_LoLOC STM32_CAN1_FIR(2,2)
#define signature_LOC STM32_CAN1_FIR(3,1)
#define bus_speed_LOC STM32_CAN1_FIR(3,2)
#define node_id_LOC STM32_CAN1_FIR(4,1)
#define CRC_H 1
#define CRC_L 0
inline static void read(bootloader_app_shared_t *pshared)
{
pshared->signature = getreg32(signature_LOC);
pshared->bus_speed = getreg32(bus_speed_LOC);
pshared->node_id = getreg32(node_id_LOC);
pshared->crc.ul[CRC_L] = getreg32(crc_LoLOC);
pshared->crc.ul[CRC_H] = getreg32(crc_HiLOC);
}
inline static void write(bootloader_app_shared_t *pshared)
{
putreg32(pshared->signature, signature_LOC);
putreg32(pshared->bus_speed, bus_speed_LOC);
putreg32(pshared->node_id, node_id_LOC);
putreg32(pshared->crc.ul[CRC_L], crc_LoLOC);
putreg32(pshared->crc.ul[CRC_H], crc_HiLOC);
}
static uint64_t calulate_signature(bootloader_app_shared_t *pshared)
{
uint64_t crc;
crc = crc64_add_word(CRC64_INITIAL, pshared->signature);
crc = crc64_add_word(crc, pshared->bus_speed);
crc = crc64_add_word(crc, pshared->node_id);
crc ^= CRC64_OUTPUT_XOR;
return crc;
}
static void bootloader_app_shared_init(bootloader_app_shared_t *pshared, eRole_t role)
{
memset(pshared, 0, sizeof(bootloader_app_shared_t));
if (role != Invalid) {
pshared->signature =
(role ==
App ? BOOTLOADER_COMMON_APP_SIGNATURE :
BOOTLOADER_COMMON_BOOTLOADER_SIGNATURE);
}
}
/****************************************************************************
* Name: bootloader_app_shared_read
*
* Description:
* Based on the role requested, this function will conditionally populate
* a bootloader_app_shared_t structure from the physical locations used
* to transfer the shared data to/from an application (internal data) .
*
* The functions will only populate the structure and return a status
* indicating success, if the internal data has the correct signature as
* requested by the Role AND has a valid crc.
*
* Input Parameters:
* shared - A pointer to a bootloader_app_shared_t return the data in if
* the internal data is valid for the requested Role
* role - An eRole_t of App or BootLoader to validate the internal data
* against. For a Bootloader this would be the value of App to
* read the application passed data.
*
* Returned value:
* OK - Indicates that the internal data has been copied to callers
* bootloader_app_shared_t structure.
*
* -EBADR - The Role or crc of the internal data was not valid. The copy
* did not occur.
*
****************************************************************************/
__EXPORT int bootloader_app_shared_read(bootloader_app_shared_t *shared, eRole_t role)
{
int rv = -EBADR;
bootloader_app_shared_t working;
read(&working);
if ((role == App ? working.signature == BOOTLOADER_COMMON_APP_SIGNATURE
: working.signature == BOOTLOADER_COMMON_BOOTLOADER_SIGNATURE)
&& (working.crc.ull == calulate_signature(&working))) {
*shared = working;
rv = OK;
}
return rv;
}
/****************************************************************************
* Name: bootloader_app_shared_write
*
* Description:
* Based on the role, this function will commit the data passed
* into the physical locations used to transfer the shared data to/from
* an application (internal data) .
*
* The functions will populate the signature and crc the data
* based on the provided Role.
*
* Input Parameters:
* shared - A pointer to a bootloader_app_shared_t data to commit to
* the internal data for passing to/from an application.
* role - An eRole_t of App or BootLoader to use in the internal data
* to be passed to/from an application. For a Bootloader this
* would be the value of Bootloader to write to the passed data.
* to the application via the internal data.
*
* Returned value:
* None.
*
****************************************************************************/
__EXPORT void bootloader_app_shared_write(bootloader_app_shared_t *shared, eRole_t role)
{
bootloader_app_shared_t working = *shared;
working.signature = (role == App ? BOOTLOADER_COMMON_APP_SIGNATURE : BOOTLOADER_COMMON_BOOTLOADER_SIGNATURE);
working.crc.ull = calulate_signature(&working);
write(&working);
}
/****************************************************************************
* Name: bootloader_app_shared_invalidate
*
* Description:
* Invalidates the data passed the physical locations used to transfer
* the shared data to/from an application (internal data) .
*
* The functions will invalidate the signature and crc and shoulf be used
* to prevent deja vu.
*
* Input Parameters:
* None.
*
* Returned value:
* None.
*
****************************************************************************/
__EXPORT void bootloader_app_shared_invalidate(void)
{
bootloader_app_shared_t working;
bootloader_app_shared_init(&working, Invalid);
write(&working);
}
@@ -0,0 +1,146 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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 <nuttx/config.h>
#include "boot_config.h"
#include <stdint.h>
#include <stdlib.h>
#include <nuttx/progmem.h>
#include "chip.h"
#include "stm32.h"
#include "flash.h"
#include "blsched.h"
/****************************************************************************
* Name: bl_flash_erase
*
* Description:
* This function erases the flash starting at address and ending at
* address + nbytes.
*
* Input Parameters:
* address - A word-aligned address within the first page of flash to erase
* nbytes - The number of bytes to erase, rounding up to the next page.
*
*
*
* Returned value:
* On success FLASH_OK On Error one of the flash_error_t
*
****************************************************************************/
flash_error_t bl_flash_erase(size_t address, size_t nbytes)
{
/*
* FIXME (?): this may take a long time, and while flash is being erased it
* might not be possible to execute interrupts, send NodeStatus messages etc.
* We can pass a per page callback or yeild */
flash_error_t status = FLASH_ERROR_AFU;
ssize_t bllastpage = up_progmem_getpage(address - 1);
if (bllastpage < 0) {
return FLASH_ERROR_AFU;
}
ssize_t appstartpage = up_progmem_getpage(address);
ssize_t appendpage = up_progmem_getpage(address + nbytes - 4);
if (appendpage < 0 || appstartpage < 0) {
return FLASH_ERROR_AFU;
}
status = FLASH_ERROR_SUICIDE;
if (bllastpage >= 0 && appstartpage > bllastpage) {
/* Erase the whole application flash region */
status = FLASH_OK;
while (status == FLASH_OK && appstartpage <= appendpage) {
bl_sched_yield();
ssize_t ps = up_progmem_eraseblock(appstartpage++);
if (ps <= 0) {
status = FLASH_ERASE_ERROR;
}
}
}
return status;
}
/****************************************************************************
* Name: bl_flash_write
*
* Description:
* This function writes the flash starting at the given address
*
* Input Parameters:
* flash_address - The address of the flash to write
* must be word aligned
* data - A pointer to a buffer count bytes to be written
* to the flash.
* count - Number of bytes to write
*
* Returned value:
* On success FLASH_OK On Error one of the flash_error_t
*
****************************************************************************/
flash_error_t bl_flash_write(uint32_t flash_address, uint8_t *data, ssize_t count)
{
flash_error_t status = FLASH_ERROR;
if (flash_address >= APPLICATION_LOAD_ADDRESS &&
(flash_address + count) <= (uint32_t) APPLICATION_LAST_8BIT_ADDRRESS) {
if (count ==
up_progmem_write((size_t) flash_address, (void *)data, count)) {
status = FLASH_OK;
}
}
return status;
}
@@ -0,0 +1,51 @@
/****************************************************************************
*
* Copyright (c) 2012-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.
*
****************************************************************************/
/*
* @file board.h
*
* bootloader board interface
* This file contains the common interfaces that all boards
* have to supply
*/
#pragma once
#define BIT(pos) (1ull<<(pos))
#define BIT_MASK(length) (BIT(length)-1)
#define BITFEILD_MASK(lsb_pos, length) ( BIT_MASK(length) << (lsb_pos))
#define BITFEILD_ISOLATE(x, lsb_pos, length) ((x) & (BITFEILD_MASK((lsb_pos), (length))))
#define BITFEILD_EXCLUDE(x, lsb_pos, length) ((x) & ~(BITFEILD_MASK((lsb_pos), (length))))
#define BITFEILD_GET(y, lsb_pos, length) (((y)>>(lsb_pos)) & BIT_MASK(length))
#define BITFEILD_SET(y, x, lsb_pos, length) ( y= ((y) & ~BF_MASK(lsb_pos, length)) | BITFEILD_ISOLATE(x, lsb_pos, length))
@@ -0,0 +1,59 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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
/****************************************************************************
* Name: sched_yield()
*
* Description:
* This function should be called in situation were the cpu may be
* busy for a long time without interrupts or the ability to run code
* to insure that the timer based process will be run.
*
*
* Input Parameters:
* None
*
* Returned value:
* None
*
****************************************************************************/
#if defined(OPT_USE_YIELD)
void bl_sched_yield(void);
#else
# define bl_sched_yield()
#endif
@@ -0,0 +1,129 @@
/****************************************************************************
*
* Copyright (c) 2012-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.
*
****************************************************************************/
/*
* @file board.h
*
* bootloader board interface
* This file contains the common interfaces that all boards
* have to supply
*/
#pragma once
#include "uavcan.h"
#include <nuttx/compiler.h>
#include <stdint.h>
typedef enum {
off,
reset,
autobaud_start,
autobaud_end,
allocation_start,
allocation_end,
fw_update_start,
fw_update_erase_fail,
fw_update_invalid_response,
fw_update_timeout,
fw_update_invalid_crc,
jump_to_app,
} uiindication_t;
#ifndef __ASSEMBLY__
/************************************************************************************
* Name: stm32_boarddeinitialize
*
* Description:
* This function is called by the bootloader code priore to booting
* the application. Is should place the HW into an benign initialized state.
*
************************************************************************************/
void stm32_boarddeinitialize(void);
/****************************************************************************
* Name: board_get_product_name
*
* Description:
* Called to retrive the product name. The retuned alue is a assumed
* to be written to a pascal style string that will be length prefixed
* and not null terminated
*
* Input Parameters:
* product_name - A pointer to a buffer to write the name.
* maxlen - The imum number of chatater that can be written
*
* Returned Value:
* The length of charaacters written to the buffer.
*
****************************************************************************/
uint8_t board_get_product_name(uint8_t *product_name, size_t maxlen);
/****************************************************************************
* Name: board_get_hardware_version
*
* Description:
* Called to retrieve the hardware version information. The function
* will first initialize the the callers struct to all zeros.
*
* Input Parameters:
* hw_version - A pointer to a uavcan_hardwareversion_t.
*
* Returned Value:
* Length of the unique_id
*
****************************************************************************/
size_t board_get_hardware_version(uavcan_HardwareVersion_t *hw_version);
/****************************************************************************
* Name: board_indicate
*
* Description:
* Provides User feedback to indicate the state of the bootloader
* on board specific hardware.
*
* Input Parameters:
* indication - A member of the uiindication_t
*
* Returned Value:
* None
*
****************************************************************************/
void board_indicate(uiindication_t indication);
#endif /* __ASSEMBLY__ */
@@ -0,0 +1,77 @@
/****************************************************************************
*
* Copyright (C) 2016 PX4 Development Team. All rights reserved.
* Author: David Sidrane <david_s5@nscdg.com>
*
* 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 board_common.h
*
* Provide the the common board interfaces
*/
#pragma once
/* Defined the types used for board UUID and MFG UID
*
* A type suitable for holding the byte format of the UUID
*
* The original PX4 stm32 (legacy) based implementation **displayed** the
* UUID as: ABCD EFGH IJKL
* Where:
* A was bit 31 and D was bit 0
* E was bit 63 and H was bit 32
* I was bit 95 and L was bit 64
*
* Since the string was used by some manufactures to identify the units
* it must be preserved.
*
* For new targets moving forward we will use
* IJKL EFGH ABCD
*/
/* A type suitable for defining the 8 bit format of the MFG UID
* This is always returned as MSD @ index 0 -LSD @ index PX4_CPU_MFGUID_BYTE_LENGTH-1
*/
typedef uint8_t mfguid_t[PX4_CPU_MFGUID_BYTE_LENGTH];
/************************************************************************************
* Name: board_get_mfguid
*
* Description:
* All boards either provide a way to retrieve a manafactuers Uniqe ID or
* define BOARD_OVERRIDE_MFGUID.
* The MFGUID is returned as an array of bytes in
* MSD @ index 0 - LSD @ index PX4_CPU_MFGUID_BYTE_LENGTH-1
*
************************************************************************************/
int board_get_mfguid(mfguid_t mfgid);
@@ -0,0 +1,216 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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
#include <nuttx/compiler.h>
__BEGIN_DECLS
/* Define the signature for the Application descriptor as 'APDesc' and a
* revision number of 00 used in app_descriptor_t
*/
#define APP_DESCRIPTOR_SIGNATURE_ID 'A','P','D','e','s','c'
#define APP_DESCRIPTOR_SIGNATURE_REV '0','0'
#define APP_DESCRIPTOR_SIGNATURE APP_DESCRIPTOR_SIGNATURE_ID, APP_DESCRIPTOR_SIGNATURE_REV
/* N.B. the .ld file must emit this sections */
# define boot_app_shared_section __attribute__((section(".app_descriptor")))
/* eRole defines the role of the bootloader_app_shared_t structure */
typedef enum eRole {
Invalid,
App,
BootLoader
} eRole_t;
/****************************************************************************
*
* Bootloader and Application shared structure.
*
* The data in this structure is passed in SRAM or the the CAN filter
* registers from bootloader to application and application to bootloader.
*
* Do not assume any mapping or location for the passing of this data
* that is done in the read and write routines and is abstracted by design.
*
* For reference, the following is performed based on eRole in API calls
* defined below:
*
* The application must write BOOTLOADER_COMMON_APP_SIGNATURE to the
* signature field when passing data to the bootloader; when the
* bootloader passes data to the application, it must write
* BOOTLOADER_COMMON_BOOTLOADER_SIGNATURE to the signature field.
*
* The CRC is calculated over the structure from signature to the
* last byte. The resulting values are then copied to the CAN filter
* registers by bootloader_app_shared_read and
* bootloader_app_shared_write.
*
****************************************************************************/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
typedef begin_packed_struct struct bootloader_app_shared_t {
union {
uint64_t ull;
uint32_t ul[2];
uint8_t valid;
} crc;
uint32_t signature;
uint32_t bus_speed;
uint32_t node_id;
} end_packed_struct bootloader_app_shared_t;
#pragma GCC diagnostic pop
/****************************************************************************
*
* Application firmware descriptor.
*
* This structure located by the linker script somewhere after the vector table.
* (within the first several kilobytes of the beginning address of the
* application);
*
* This structure must be aligned on an 8-byte boundary.
*
* The bootloader will scan through the application FLASH image until it
* finds the signature.
*
* The image_crc is calculated as follows:
* 1) All fields of this structure must be initialized with the correct
* information about the firmware image bin file
* (Here after refereed to as image)
* 2) image_crc set to 0;
* 3) The CRC 64 is calculated over the image from offset 0 up to and including the
* last byte of the image file.
* 4) The calculated CRC 64 is stored in image_crc
* 5) The new image file is then written to a file a ".img" extension.
*
****************************************************************************/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#pragma GCC diagnostic ignored "-Wpacked"
typedef begin_packed_struct struct app_descriptor_t {
uint8_t signature[sizeof(uint64_t)];
uint64_t image_crc;
uint32_t image_size;
uint32_t vcs_commit;
uint8_t major_version;
uint8_t minor_version;
uint8_t reserved[6];
} end_packed_struct app_descriptor_t;
#pragma GCC diagnostic pop
/****************************************************************************
* Name: bootloader_app_shared_read
*
* Description:
* Based on the role requested, this function will conditionally populate
* a bootloader_app_shared_t structure from the physical locations used
* to transfer the shared data to/from an application (internal data) .
*
* The functions will only populate the structure and return a status
* indicating success, if the internal data has the correct signature as
* requested by the Role AND has a valid crc.
*
* Input Parameters:
* shared - A pointer to a bootloader_app_shared_t return the data in if
* the internal data is valid for the requested Role
* role - An eRole_t of App or BootLoader to validate the internal data
* against. For a Bootloader this would be the value of App to
* read the application passed data.
*
* Returned value:
* OK - Indicates that the internal data has been copied to callers
* bootloader_app_shared_t structure.
*
* -EBADR - The Role or crc of the internal data was not valid. The copy
* did not occur.
*
****************************************************************************/
int bootloader_app_shared_read(bootloader_app_shared_t *shared, eRole_t role);
/****************************************************************************
* Name: bootloader_app_shared_write
*
* Description:
* Based on the role, this function will commit the data passed
* into the physical locations used to transfer the shared data to/from
* an application (internal data) .
*
* The functions will populate the signature and crc the data
* based on the provided Role.
*
* Input Parameters:
* shared - A pointer to a bootloader_app_shared_t data to commit to
* the internal data for passing to/from an application.
* role - An eRole_t of App or BootLoader to use in the internal data
* to be passed to/from an application. For a Bootloader this
* would be the value of Bootloader to write to the passed data.
* to the application via the internal data.
*
* Returned value:
* None.
*
****************************************************************************/
void bootloader_app_shared_write(bootloader_app_shared_t *shared, eRole_t role);
/****************************************************************************
* Name: bootloader_app_shared_invalidate
*
* Description:
* Invalidates the data passed the physical locations used to transfer
* the shared data to/from an application (internal data) .
*
* The functions will invalidate the signature and crc and should be used
* to prevent deja vu.
*
* Input Parameters:
* None.
*
* Returned value:
* None.
*
****************************************************************************/
void bootloader_app_shared_invalidate(void);
__END_DECLS
@@ -0,0 +1,218 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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
#include "timer.h"
typedef enum {
CanPayloadLength = 8,
} can_const_t;
typedef enum {
CAN_OK = 0,
CAN_BOOT_TIMEOUT,
CAN_ERROR
} can_error_t;
typedef enum {
CAN_UNKNOWN = 0,
CAN_125KBAUD = 1,
CAN_250KBAUD = 2,
CAN_500KBAUD = 3,
CAN_1MBAUD = 4
} can_speed_t;
typedef enum {
CAN_Mode_Normal = 0, // Bits 30 and 31 00
CAN_Mode_LoopBack = 1, // Bit 30: Loop Back Mode (Debug)
CAN_Mode_Silent = 2, // Bit 31: Silent Mode (Debug)
CAN_Mode_Silent_LoopBack = 3 // Bits 30 and 31 11
} can_mode_t;
/*
* Receive from FIFO 1 -- filters are configured to push the messages there,
* and there are send/receive functions called off the SysTick ISR so
* we partition the usage of the CAN hardware to avoid the same FIFOs/mailboxes
* as the rest of the application uses.
*/
typedef enum {
Fifo0 = 0,
MailBox0 = 0,
Fifo1 = 1,
MailBox1 = 1,
Fifo2 = 2,
MailBox2 = 2,
FifoNone = 3,
MailBoxNone = 3,
} can_fifo_mailbox_t;
/****************************************************************************
* Name: can_speed2freq
*
* Description:
* This function maps a can_speed_t to a bit rate in Hz
*
* Input Parameters:
* can_speed - A can_speed_t from CAN_125KBAUD to CAN_1MBAUD
*
* Returned value:
* Bit rate in Hz
*
****************************************************************************/
int can_speed2freq(can_speed_t speed);
/****************************************************************************
* Name: can_speed2freq
*
* Description:
* This function maps a frequency in Hz to a can_speed_t in the range
* CAN_125KBAUD to CAN_1MBAUD.
*
* Input Parameters:
* freq - Bit rate in Hz
*
* Returned value:
* A can_speed_t from CAN_125KBAUD to CAN_1MBAUD
*
****************************************************************************/
can_speed_t can_freq2speed(int freq);
/****************************************************************************
* Name: can_tx
*
* Description:
* This function is called to transmit a CAN frame using the supplied
* mailbox. It will busy wait on the mailbox if not available.
*
* Input Parameters:
* message_id - The CAN message's EXID field
* length - The number of bytes of data - the DLC field
* message - A pointer to 8 bytes of data to be sent (all 8 bytes will be
* loaded into the CAN transmitter but only length bytes will
* be sent.
* mailbox - A can_fifo_mailbox_t MBxxx value to choose the outgoing
* mailbox.
*
* Returned value:
* The CAN_OK of the data sent or CAN_ERROR if a time out occurred
*
****************************************************************************/
uint8_t can_tx(uint32_t message_id, size_t length, const uint8_t *message, uint8_t mailbox);
/****************************************************************************
* Name: can_rx
*
* Description:
* This function is called to receive a CAN frame from a supplied fifo.
* It does not block if there is not available, but returns 0
*
* Input Parameters:
* message_id - A pointer to return the CAN message's EXID field
* length - A pointer to return the number of bytes of data - the DLC field
* message - A pointer to return 8 bytes of data to be sent (all 8 bytes will
* be written from the CAN receiver but only length bytes will be sent.
* fifo A can_fifo_mailbox_t fifixxx value to choose the incoming fifo.
*
* Returned value:
* The length of the data read or 0 if the fifo was empty
*
****************************************************************************/
uint8_t can_rx(uint32_t *message_id, size_t *length, uint8_t *message, uint8_t fifo);
/****************************************************************************
* Name: can_init
*
* Description:
* This function is used to initialize the CAN block for a given bit rate and
* mode.
*
* Input Parameters:
* speed - A can_speed_t from CAN_125KBAUD to CAN_1MBAUD
* mode - One of the can_mode_t of Normal, LoopBack and Silent or
* combination thereof.
*
* Returned value:
* OK - on Success or a negate errno value
*
****************************************************************************/
int can_init(can_speed_t speed, can_mode_t mode);
/****************************************************************************
* Name: can_autobaud
*
* Description:
* This function will attempt to detect the bit rate in use on the CAN
* interface until the timeout provided expires or the successful detection
* occurs.
*
* It will initialize the CAN block for a given bit rate
* to test that a message can be received. The CAN interface is left
* operating at the detected bit rate and in CAN_Mode_Normal mode.
*
* Input Parameters:
* can_speed - A pointer to return detected can_speed_t from CAN_UNKNOWN to
* CAN_1MBAUD
* timeout - The timer id of a timer to use as the maximum time to wait for
* successful bit rate detection. This timer may be not running
* in which case the auto baud code will try indefinitely to
* detect the bit rate.
*
* Returned value:
* CAN_OK - on Success or a CAN_BOOT_TIMEOUT
*
****************************************************************************/
int can_autobaud(can_speed_t *can_speed, bl_timer_id timeout);
/****************************************************************************
* Name: can_cancel_on_error
*
* Description:
* This function will test for transition completion or any error.
* If the is a error the the transmit will be aborted.
*
* Input Parameters:
* mailbox - A can_fifo_mailbox_t MBxxx value to choose the outgoing
* mailbox.
*
* Returned value:
* None
*
****************************************************************************/
void can_cancel_on_error(uint8_t mailbox);
@@ -0,0 +1,95 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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
#define CRC16_INITIAL 0xFFFFu
#define CRC16_OUTPUT_XOR 0x0000u
#define CRC64_INITIAL 0xFFFFFFFFFFFFFFFFull
#define CRC64_OUTPUT_XOR 0xFFFFFFFFFFFFFFFFull
/****************************************************************************
* Name: crc16_add
*
* Description:
* Use to calculates a CRC-16-CCITT using the polynomial of
* 0x1021 by adding a value successive values.
*
* Input Parameters:
* crc - The running total of the crc 16
* value - The value to add
*
* Returned Value:
* The current crc16 with the value processed.
*
****************************************************************************/
uint16_t crc16_add(uint16_t crc, uint8_t value);
/****************************************************************************
* Name: crc16_signature
*
* Description:
* Calculates a CRC-16-CCITT using the crc16_add
* function
*
* Input Parameters:
* initial - The Initial value to uses as the crc's starting point
* length - The number of bytes to add to the crc
* bytes - A pointer to any array of length bytes
*
* Returned Value:
* The crc16 of the array of bytes
*
****************************************************************************/
uint16_t crc16_signature(uint16_t initial, size_t length, const uint8_t *bytes);
/****************************************************************************
* Name: crc64_add_word
*
* Description:
* Calculates a CRC-64-WE using the polynomial of 0x42F0E1EBA9EA3693
* See http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat-bits.64
* Check: 0x62EC59E3F1A4F00A
*
* Input Parameters:
* crc - The running total of the crc 64
* value - The value to add
*
* Returned Value:
* The current crc64 with the value processed.
*
****************************************************************************/
uint64_t crc64_add_word(uint64_t crc, uint32_t value);
@@ -0,0 +1,85 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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
typedef enum {
FLASH_OK = 0,
FLASH_ERROR,
FLASH_ERASE_ERROR,
FLASH_ERASE_VERIFY_ERROR,
FLASH_ERROR_SUICIDE,
FLASH_ERROR_AFU,
} flash_error_t;
/****************************************************************************
* Name: bl_flash_erase
*
* Description:
* This function erases the flash starting at address and ending at
* address + nbytes.
*
* Input Parameters:
* address - A word-aligned address within the first page of flash to erase
* nbytes - The number of bytes to erase, rounding up to the next page.
*
*
*
* Returned value:
* On success FLASH_OK On Error one of the flash_error_t
*
****************************************************************************/
flash_error_t bl_flash_erase(size_t address, size_t nbytes);
/****************************************************************************
* Name: bl_flash_write
*
* Description:
* This function writes the flash starting at the given address
*
* Input Parameters:
* flash_address - The address of the flash to write
* must be word aligned
* data - A pointer to a buffer count bytes to be written
* to the flash.
* count - Number of bytes to write
*
* Returned value:
* On success FLASH_OK On Error one of the flash_error_t
*
****************************************************************************/
flash_error_t bl_flash_write(uint32_t flash_address, uint8_t *data, ssize_t count);
@@ -0,0 +1,51 @@
/****************************************************************************
*
* Copyright (C) 2015 Mark Charlebois. 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_config.h
Configuration flags used in code.
*/
#pragma once
#if defined(__PX4_NUTTX)
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include "px4_micro_hal.h"
#include <board_config.h>
#elif defined (__PX4_POSIX)
# include <board_config.h>
#endif
@@ -0,0 +1,116 @@
/****************************************************************************
*
* 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_macros.h
*
* A set of useful macros for enhanced runtime and compile time
* error detection and warning suppression.
*
* Define NO_BLOAT to reduce bloat from file name inclusion.
*
* The arraySize() will compute the size of an array regardless
* it's type
*
* INVALID_CASE(c) should be used is case statements to ferret out
* unintended behavior
*
* UNUSED(var) will suppress compile time warnings of unused
* variables
*
* CCASSERT(predicate) Will generate a compile time error it the
* predicate is false
*/
#include <assert.h>
#ifndef _PX4_MACROS_H
#define _PX4_MACROS_H
#if !defined(arraySize)
#define arraySize(a) (sizeof((a))/sizeof((a[0])))
#endif
#if !defined(NO_BLOAT)
#if defined(__BASE_FILE__)
#define _FILE_NAME_ __BASE_FILE__
#else
#define _FILE_NAME_ __FILE__
#endif
#else
#define _FILE_NAME_ ""
#endif
#if !defined(INVALID_CASE)
#define INVALID_CASE(c) printf("Invalid Case %d, %s:%d",(c),__BASE_FILE__,__LINE__) /* todo use PANIC */
#endif
#if !defined(UNUSED)
#define UNUSED(var) (void)(var)
#endif
#if !defined(CAT)
#if !defined(_CAT)
#define _CAT(a, b) a ## b
#endif
#define CAT(a, b) _CAT(a, b)
#endif
#if !defined(FREEZE_STR)
# define FREEZE_STR(s) #s
#endif
#if !defined(STRINGIFY)
#define STRINGIFY(s) FREEZE_STR(s)
#endif
#if !defined(CCASSERT)
#if defined(static_assert)
# define CCASSERT(predicate) static_assert(predicate, STRINGIFY(predicate))
# else
# define CCASSERT(predicate) _x_CCASSERT_LINE(predicate, __LINE__)
# if !defined(_x_CCASSERT_LINE)
# define _x_CCASSERT_LINE(predicate, line) typedef char CAT(constraint_violated_on_line_,line)[2*((predicate)!=0)-1] __attribute__ ((unused)) ;
# endif
# endif
#endif
#if !defined(DO_PRAGMA)
# define DO_PRAGMA(x) _Pragma (#x)
#endif
#if !defined(TODO)
# define TODO(x) DO_PRAGMA(message ("TODO - " #x))
#endif
#endif /* _PX4_MACROS_H */
@@ -0,0 +1,56 @@
/****************************************************************************
*
* Copyright (c) 2016 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
/*
* This file is a shim to bridge to nuttx_v3
*/
__BEGIN_DECLS
# define PX4_CPU_UUID_BYTE_LENGTH 12
# define PX4_CPU_UUID_WORD32_LENGTH (PX4_CPU_UUID_BYTE_LENGTH/sizeof(uint32_t))
/* The mfguid will be an array of bytes with
* MSD @ index 0 - LSD @ index PX4_CPU_MFGUID_BYTE_LENGTH-1
*
* It will be converted to a string with the MSD on left and LSD on the right most position.
*/
# define PX4_CPU_MFGUID_BYTE_LENGTH PX4_CPU_UUID_BYTE_LENGTH
/* Separator nnn:nnn:nnnn 2 char per byte term */
# define PX4_CPU_UUID_WORD32_FORMAT_SIZE (PX4_CPU_UUID_WORD32_LENGTH-1+(2*PX4_CPU_UUID_BYTE_LENGTH)+1)
# define PX4_CPU_MFGUID_FORMAT_SIZE ((2*PX4_CPU_MFGUID_BYTE_LENGTH)+1)
#include <arch/board/board.h>
__END_DECLS
@@ -0,0 +1,75 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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
#define CRC16_INITIAL 0xFFFFu
#define CRC16_OUTPUT_XOR 0x0000u
#define CRC64_INITIAL 0xFFFFFFFFFFFFFFFFull
#define CRC64_OUTPUT_XOR 0xFFFFFFFFFFFFFFFFull
/****************************************************************************
* Name: util_srand
*
* Description:
* This function seeds the random number generator
*
*
* Input Parameters:
* seed - The seed
*
* Returned value:
* None
*
****************************************************************************/
void util_srand(uint16_t seed);
/****************************************************************************
* Name: util_random
*
* Description:
* This function returns a random number between min and max
*
*
* Input Parameters:
* min - The minimum value the return value can be.
* max - The maximum value the return value can be.
*
* Returned value:
* A random number
*
****************************************************************************/
uint16_t util_random(uint16_t min, uint16_t max);
@@ -0,0 +1,419 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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
/*
* We support two classes of timer interfaces. The first one is for structured
* timers that have an API for life cycle management and use. (timer_xx)
* The Second type of methods are for interfacing to a high resolution
* counter with fast access and are provided via an in line API (timer_hrt)
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdbool.h>
#include <stdint.h>
#include "stm32.h"
#include "nvic.h"
#define TIMER_HRT_CYCLES_PER_US (STM32_HCLK_FREQUENCY/1000000)
#define TIMER_HRT_CYCLES_PER_MS (STM32_HCLK_FREQUENCY/1000)
/* Types for timer access */
typedef uint8_t bl_timer_id; /* A timer handle */
typedef uint32_t time_ms_t; /* A timer value */
typedef volatile time_ms_t *time_ref_t; /* A pointer to the internal
counter in the structure of a timer
used to do a time out test value */
typedef uint32_t time_hrt_cycles_t; /* A timer value type of the hrt */
/*
* Timers
*
* There are 3 modes of operation for the timers.
* All modes support a call back on expiration.
*
*/
typedef enum {
/* Specifies a one-shot timer. After notification timer is discarded. */
modeOneShot = 1,
/* Specifies a repeating timer. */
modeRepeating = 2,
/* Specifies a persistent start / stop timer. */
modeTimeout = 3,
/* Or'ed in to start the timer when allocated */
modeStarted = 0x40
} bl_timer_modes_t;
/* The call back function signature type */
typedef void (*bl_timer_ontimeout)(bl_timer_id id, void *context);
/*
* A helper type for timer allocation to setup a callback
* There is a null_cb object (see below) that can be used to
* a bl_timer_cb_t.
*
* Typical usage is:
*
* void my_process(bl_timer_id id, void *context) {
* ...
* };
*
* bl_timer_cb_t mycallback = null_cb;
* mycallback.cb = my_process;
* bl_timer_id mytimer = timer_allocate(modeRepeating|modeStarted, 100, &mycallback);
*/
typedef struct {
void *context;
bl_timer_ontimeout cb;
} bl_timer_cb_t;
extern const bl_timer_cb_t null_cb;
/****************************************************************************
* Name: timer_init
*
* Description:
* Called early in os_start to initialize the data associated with
* the timers
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
void timer_init(void);
/****************************************************************************
* Name: timer_allocate
*
* Description:
* Is used to allocate a timer. Allocation does not involve memory
* allocation as the data for the timer are compile time generated.
* See OPT_BL_NUMBER_TIMERS
*
* There is an inherent priority to the timers in that the first timer
* allocated is the first timer run per tick.
*
* There are 3 modes of operation for the timers. All modes support an
* optional call back on expiration.
*
* modeOneShot - Specifies a one-shot timer. After notification timer
* is resource is freed.
* modeRepeating - Specifies a repeating timer that will reload and
* call an optional.
* modeTimeout - Specifies a persistent start / stop timer.
*
* modeStarted - Or'ed in to start the timer when allocated
*
*
* Input Parameters:
* mode - One of bl_timer_modes_t with the Optional modeStarted
* msfromnow - The reload and initial value for the timer in Ms.
* fc - A pointer or NULL (0). If it is non null it can be any
* of the following:
*
* a) A bl_timer_cb_t populated on the users stack or
* in the data segment. The values are copied into the
* internal data structure of the timer and therefore do
* not have to persist after the call to timer_allocate
*
* b) The address of null_cb. This is identical to passing
* null for the value of fc.
*
* Returned Value:
* On success a value from 0 - OPT_BL_NUMBER_TIMERS-1 that is
* the bl_timer_id for subsequent timer operations
* -1 on failure. This indicates there are no free timers.
*
****************************************************************************/
bl_timer_id timer_allocate(bl_timer_modes_t mode, time_ms_t msfromnow, bl_timer_cb_t *fc);
/****************************************************************************
* Name: timer_free
*
* Description:
* Is used to free a timer. Freeing a timer does not involve memory
* deallocation as the data for the timer are compile time generated.
* See OPT_BL_NUMBER_TIMERS
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* None.
*
****************************************************************************/
void timer_free(bl_timer_id id);
/****************************************************************************
* Name: timer_start
*
* Description:
* Is used to Start a timer. The reload value is copied to the counter.
* And the running bit it set. There is no problem in Starting a running
* timer. But it will restart the timeout.
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* None.
*
****************************************************************************/
void timer_start(bl_timer_id id);
/****************************************************************************
* Name: timer_restart
*
* Description:
* Is used to re start a timer with a new reload count. The reload value
* is copied to the counter and the running bit it set. There is no
* problem in restarting a running timer.
*
* Input Parameters:
* id - Returned from timer_allocate;
* ms - Is a time_ms_t and the new reload value to use.
*
* Returned Value:
* None.
*
****************************************************************************/
void timer_restart(bl_timer_id id, time_ms_t ms);
/****************************************************************************
* Name: timer_stop
*
* Description:
* Is used to stop a timer. It is Ok to stop a stopped timer.
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* None.
*
****************************************************************************/
void timer_stop(bl_timer_id id);
/****************************************************************************
* Name: timer_expired
*
* Description:
* Test if a timer that was configured as a modeTimeout timer is expired.
* To be expired the time has to be running and have a count of 0.
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* No Zero if the timer is expired otherwise zero.
*
****************************************************************************/
int timer_expired(bl_timer_id id);
/****************************************************************************
* Name: timer_ref
*
* Description:
* Returns an time_ref_t that is a reference (pointer) to the internal counter
* of the timer selected by id. It should only be used with calls to
* timer_ref_expired.
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* An internal reference that should be treated as opaque by the caller and
* should only be used with calls to timer_ref_expired.
* There is no reference counting on the reference and therefore does not
* require any operation to free it.
*
****************************************************************************/
time_ref_t timer_ref(bl_timer_id id);
/****************************************************************************
* Name: timer_ref_expired
*
* Description:
* Test if a timer, that was configured as a modeTimeout timer is expired
* based on the reference provided.
*
* Input Parameters:
* ref - Returned timer_ref;
*
* Returned Value:
* Non Zero if the timer is expired otherwise zero.
*
****************************************************************************/
static inline int timer_ref_expired(time_ref_t ref)
{
return *ref == 0;
}
/****************************************************************************
* Name: timer_tic
*
* Description:
* Returns the system tic counter that counts in units of
* (CONFIG_USEC_PER_TICK/1000). By default 10 Ms.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
time_ms_t timer_tic(void);
/****************************************************************************
* Name: timer_hrt_read
*
* Description:
* Returns the hardware SysTic counter that counts in units of
* STM32_HCLK_FREQUENCY. This file defines TIMER_HRT_CYCLES_PER_US
* and TIMER_HRT_CYCLES_PER_MS that should be used to define times.
*
* Input Parameters:
* None
*
* Returned Value:
* The current value of the HW counter in the type of time_hrt_cycles_t.
*
****************************************************************************/
static inline time_hrt_cycles_t timer_hrt_read(void)
{
return getreg32(NVIC_SYSTICK_CURRENT);
}
/****************************************************************************
* Name: timer_hrt_clear_wrap
*
* Description:
* Clears the wrap flag by reading the timer
*
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
static inline void timer_hrt_clear_wrap(void)
{
(void)timer_hrt_read();
}
/****************************************************************************
* Name: timer_hrt_wrap
*
* Description:
* Returns true if SysTic counted to 0 since last time it was
* read.
*
* Input Parameters:
* None
*
* Returned Value:
* Returns true if timer counted to 0 since last time this was read.
*
****************************************************************************/
static inline bool timer_hrt_wrap(void)
{
uint32_t rv = getreg32(NVIC_SYSTICK_CTRL);
return ((rv & NVIC_SYSTICK_CTRL_COUNTFLAG) ? true : false);
}
/****************************************************************************
* Name: timer_hrt_max
*
* Description:
* Returns the hardware SysTic reload value +1
*
* Input Parameters:
* None
*
* Returned Value:
* The current SysTic reload of the HW counter in the type of
* time_hrt_cycles_t.
*
****************************************************************************/
static inline time_hrt_cycles_t timer_hrt_max(void)
{
return getreg32(NVIC_SYSTICK_RELOAD) + 1;
}
/****************************************************************************
* Name: timer_hrt_elapsed
*
* Description:
* Returns the difference between 2 time values, taking into account
* the way the timer wrap.
*
* Input Parameters:
* begin - Beginning timer count.
* end - Ending timer count.
*
* Returned Value:
* The difference from begin to end
*
****************************************************************************/
static inline time_hrt_cycles_t timer_hrt_elapsed(time_hrt_cycles_t begin, time_hrt_cycles_t end)
{
/* It is a down count from NVIC_SYSTICK_RELOAD */
time_hrt_cycles_t elapsed = begin - end;
time_hrt_cycles_t reload = timer_hrt_max();
/* Did it wrap */
if (elapsed > reload) {
elapsed += reload;
}
return elapsed;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,66 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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.
*
****************************************************************************/
/* This file not a typical h file, is defines the UAVCAN dsdl
* usage and may be included several times in header or source
* file
*/
/* UAVCAN_BIT_DEFINE( field_name, lsb_pos, length) */
/* Message Frame Format */
UAVCAN_BIT_DEFINE(UavCanMessagePriority, 24, 5)
UAVCAN_BIT_DEFINE(UavCanMessageTypeID, 8, 16)
UAVCAN_BIT_DEFINE(UavCanMessageServiceNotMessage, 7, 1)
UAVCAN_BIT_DEFINE(UavCanMessageSourceNodeID, 0, 7)
/* Anonymous message Frame Format */
UAVCAN_BIT_DEFINE(UavCanAnonMessagePriority, 24, 5)
UAVCAN_BIT_DEFINE(UavCanAnonMessageDiscriminator, 10, 14)
UAVCAN_BIT_DEFINE(UavCanAnonMessageTypeID, 8, 2)
UAVCAN_BIT_DEFINE(UavCanAnonMessageServiceNotMessage, 7, 1)
UAVCAN_BIT_DEFINE(UavCanAnonMessageSourceNodeID, 0, 7)
/* Service Frame Format */
UAVCAN_BIT_DEFINE(UavCanServicePriority, 24, 5)
UAVCAN_BIT_DEFINE(UavCanServiceTypeID, 16, 8)
UAVCAN_BIT_DEFINE(UavCanServiceRequestNotResponse, 15, 1)
UAVCAN_BIT_DEFINE(UavCanServiceDestinationNodeID, 8, 7)
UAVCAN_BIT_DEFINE(UavCanServiceServiceNotMessage, 7, 1)
UAVCAN_BIT_DEFINE(UavCanServiceSourceNodeID, 0, 7)
/* Tail Format */
UAVCAN_BIT_DEFINE(UavCanStartOfTransfer, 7, 1)
UAVCAN_BIT_DEFINE(UavCanEndOfTransfer, 6, 1)
UAVCAN_BIT_DEFINE(UavCanToggle, 5, 1)
UAVCAN_BIT_DEFINE(UavCanTransferID, 0, 5)
@@ -0,0 +1,187 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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.
*
****************************************************************************/
/* This file not a typical h file, is defines the UAVCAN dsdl
* usage and may be included several times in header or source
* file
*/
/* Components */
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(CRC, lsb, 0, 8, 0, 1)
UAVCAN_DSDL_BIT_DEF(CRC, msb, 0, 8, 1, 1)
UAVCAN_DSDL_BIT_DEF(CRC, data, 0, 8, 2, 4)
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_TYPE_DEF(Path, 0, 0, 200, NA, NA,
NA, NA)
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(Path, path, 0, 8, 0,
200)
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_TYPE_DEF(SoftwareVersion, 0, 0, 15, NA, NA,
NA, NA)
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(SoftwareVersion, major, 0, 8, 0,
1)
UAVCAN_DSDL_BIT_DEF(SoftwareVersion, minor, 0, 8, 1,
1)
UAVCAN_DSDL_BIT_DEF(SoftwareVersion, optional_field_flags, 0, 8, 2,
1)
UAVCAN_DSDL_BIT_DEF(SoftwareVersion, vcs_commit, 0, 32, 3,
4)
UAVCAN_DSDL_BIT_DEF(SoftwareVersion, image_crc, 0, NA, 7,
8) // NA becuase bit mask is 64
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_TYPE_DEF(HardwareVersion, 0, 0, NA, NA, NA,
NA, NA)
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(HardwareVersion, unique_id, 0, 8, 0,
16)
UAVCAN_DSDL_BIT_DEF(HardwareVersion, certificate_of_authenticity, 0, 8, 0,
255)
END_COMPONENTS
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_MESG_DEF(Allocation, 1, 0xf258, 0, MailBox0, Fifo0,
MultiFrameTailInit, SingleFrameTailInit) /* Message */
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(Allocation, node_id, 1, 7, 0,
1)
UAVCAN_DSDL_BIT_DEF(Allocation, first_part_of_unique_id, 0, 1, 0,
1)
UAVCAN_DSDL_BIT_DEF(Allocation, unique_id, 0, 8, 1,
16)
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_MESG_DEF(NodeStatus, 341, 0xbe5f, 7, MailBox1, FifoNone,
SingleFrameTailInit, SingleFrameTailInit) /* Message */
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(NodeStatus, uptime_sec, 0, 32, 0,
4)
UAVCAN_DSDL_BIT_DEF(NodeStatus, health, 6, 2, 5,
1)
UAVCAN_DSDL_BIT_DEF(NodeStatus, mode, 3, 3, 5,
1)
UAVCAN_DSDL_BIT_DEF(NodeStatus, sub_mode, 0, 3, 5,
1)
UAVCAN_DSDL_BIT_DEF(NodeStatus, vendor_specific_status_code, 0, 16, 6,
2)
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_SREQ_DEF(GetNodeInfo, 1, 0xd9a7, 0, MailBox1, Fifo1,
SingleFrameTailInit, MultiFrameTailInit) /* Request */
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) Empty */
UAVCAN_DSDL_SRSP_DEF(GetNodeInfo, 1, 0xd9a7, 0, MailBox1, Fifo1,
SingleFrameTailInit, MultiFrameTailInit) /* Response */
UAVCAN_DSDL_BIT_DEF(GetNodeInfo, status, 0, 8, 0,
PackedSizeMsgNodeStatus)
UAVCAN_DSDL_BIT_DEF(GetNodeInfo, software_version, 0, 8, NA,
NA)
UAVCAN_DSDL_BIT_DEF(GetNodeInfo, hardware_version, 0, 8, NA,
NA)
UAVCAN_DSDL_BIT_DEF(GetNodeInfo, name, 0, 8, NA,
80)
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_SREQ_DEF(BeginFirmwareUpdate, 40, 0x729e, 0, MailBox0, Fifo0,
MultiFrameTailInit, SingleFrameTailInit) /* Request */
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(BeginFirmwareUpdate, source_node_id, 0, 8, 0,
1)
UAVCAN_DSDL_BIT_DEF(BeginFirmwareUpdate, image_file_remote_path, 0, 8, 1,
PayloadLengthPathpath)
UAVCAN_DSDL_SRSP_DEF(BeginFirmwareUpdate, 40, 0x729e, 0, MailBox0, Fifo0,
MultiFrameTailInit, SingleFrameTailInit) /* Response */
UAVCAN_DSDL_BIT_DEF(BeginFirmwareUpdate, error, 0, 8, 0,
1)
UAVCAN_DSDL_BIT_DEF(BeginFirmwareUpdate, optional_error_message, 0, 8, 1,
128)
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_SREQ_DEF(GetInfo, 45, 0x14b9, 0, MailBox0, Fifo0,
SingleFrameTailInit, MultiFrameTailInit) /* Request */
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(GetInfo, path, 0, 8, 0,
PayloadLengthPathpath)
UAVCAN_DSDL_SRSP_DEF(GetInfo, 45, 0x14b9, 0, MailBox0, Fifo0,
SingleFrameTailInit, MultiFrameTailInit) /* Response */
UAVCAN_DSDL_BIT_DEF(GetInfo, size, 0, 32, 0,
4) /* Response */
UAVCAN_DSDL_BIT_DEF(GetInfo, sizemsb, 0, 8, 4,
1)
UAVCAN_DSDL_BIT_DEF(GetInfo, error, 0, 16, 5,
2)
UAVCAN_DSDL_BIT_DEF(GetInfo, entry_type, 0, 8, 7,
1)
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_SREQ_DEF(Read, 48, 0x2f12, 0, MailBox0, Fifo0,
SingleFrameTailInit, MultiFrameTailInit) /* Request */
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(Read, offset, 0, 32, 0,
4) /* Request */
UAVCAN_DSDL_BIT_DEF(Read, msboffset, 0, 8, 4,
1)
UAVCAN_DSDL_BIT_DEF(Read, path, 0, 8, 5,
PayloadLengthPathpath)
UAVCAN_DSDL_SRSP_DEF(Read, 48, 0x2f12, 0, MailBox0, Fifo0,
SingleFrameTailInit, MultiFrameTailInit) // Responce
UAVCAN_DSDL_BIT_DEF(Read, error, 0, 16, 0,
2) /* Response */
UAVCAN_DSDL_BIT_DEF(Read, data, 0, 8, 2,
256)
/*UAVCAN_DSDL_TYPE_DEF(name, dtid, signature, packed size mailbox, fifo, inbound, outbound) */
UAVCAN_DSDL_MESG_DEF(LogMessage, 16383, 0x4570, 7, MailBox0, Fifo0,
NA, SingleFrameTailInit) /* Message */
/* UAVCAN_DSDL_BIT_DEF(data_typ_name, field_name, lsb_pos, length, payload_offset, payload_length) */
UAVCAN_DSDL_BIT_DEF(LogMessage, level, 5, 3, 0,
1)
UAVCAN_DSDL_BIT_DEF(LogMessage, source_length, 0, 5, 0,
1)
UAVCAN_DSDL_BIT_DEF(LogMessage, source, 0, 8, 1,
4) // Bootloader specific uses is 4
UAVCAN_DSDL_BIT_DEF(LogMessage, text, 0, 8, 5,
2) // Bootloader specific uses is 2
@@ -0,0 +1,72 @@
/****************************************************************************
*
* Copyright (C) 2012 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 visibility.h
*
* Definitions controlling symbol naming and visibility.
*
* This file is normally included automatically by the build system.
*/
#pragma once
#ifdef __EXPORT
# undef __EXPORT
#endif
#define __EXPORT __attribute__ ((visibility ("default")))
#ifdef __PRIVATE
# undef __PRIVATE
#endif
#define __PRIVATE __attribute__ ((visibility ("hidden")))
#ifdef __cplusplus
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
#else
# define __BEGIN_DECLS
# define __END_DECLS
#endif
#ifdef __PX4_NUTTX
/* On NuttX we call clearenv() so we cannot use getenv() and others (see px4_task_spawn_cmd() in px4_nuttx_tasks.c).
* We need to include the headers declaring getenv() before the pragma, otherwise it will trigger a poison error.
*/
#include <stdlib.h>
#ifdef __cplusplus
#include <cstdlib>
#endif
#pragma GCC poison getenv setenv putenv
#endif /* __PX4_NUTTX */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,434 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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 <nuttx/config.h>
// Turn off Probes in this module
#undef CONFIG_BOARD_USE_PROBES
#include <boot_config.h>
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <nuttx/arch.h>
#include <arch/irq.h>
#include <arch/board/board.h>
#include "px4_macros.h"
#include "timer.h"
#include "nvic.h"
typedef enum {
OneShot = modeOneShot,
Repeating = modeRepeating,
Timeout = modeTimeout,
modeMsk = 0x3,
running = modeStarted,
inuse = 0x80,
} bl_timer_ctl_t;
typedef struct {
bl_timer_cb_t usr;
time_ms_t count;
time_ms_t reload;
bl_timer_ctl_t ctl;
} bl_timer_t;
static time_ms_t sys_tic;
static bl_timer_t timers[OPT_BL_NUMBER_TIMERS];
/* Use to initialize */
const bl_timer_cb_t null_cb = { 0, 0 };
/* We use the linker --wrap ability to wrap the NuttX stm32 call out to
* the sceduler's sched_process_timer and service it here. Thus replacing
* the NuttX scheduler with are timer driven scheduling.
*/
void __wrap_sched_process_timer(void);
/****************************************************************************
* Name: timer_tic
*
* Description:
* Returns the system tic counter that counts in units of
* (CONFIG_USEC_PER_TICK/1000). By default 10 Ms.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
time_ms_t timer_tic(void)
{
return sys_tic;
}
/****************************************************************************
* Name: sched_process_timer
*
* Description:
* Called by Nuttx on the ISR of the SysTic. This function run the list of
* timers. It deducts that amount of the time of a system tick from the
* any timers that are in use and running.
*
* Depending on the mode of the timer, the appropriate actions is taken on
* expiration.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
__EXPORT void __wrap_sched_process_timer(void)
{
//PROBE(1, true);
//PROBE(1, false);
/* Increment the per-tick system counter */
sys_tic++;
/* todo:May need a X tick here is threads run long */
time_ms_t ms_elapsed = (CONFIG_USEC_PER_TICK / 1000);
/* Walk the time list from High to low and */
bl_timer_id t;
for (t = arraySize(timers) - 1; (int8_t) t >= 0; t--) {
/* Timer in use and running */
if ((timers[t].ctl & (inuse | running)) == (inuse | running)) {
/* Is it NOT already expired nor about to expire ?*/
if (timers[t].count != 0) {
/* Is it off in future */
if (timers[t].count > ms_elapsed) {
/* Just remove the amount attributed to the tick */
timers[t].count -= ms_elapsed;
continue;
}
/* it has expired now or less than a tick ago */
/* Mark it expired */
timers[t].count = 0;
/* Now perform action based on mode */
switch (timers[t].ctl & ~(inuse | running)) {
case OneShot: {
bl_timer_cb_t user = timers[t].usr;
memset(&timers[t], 0, sizeof(timers[t]));
if (user.cb) {
user.cb(t, user.context);
}
}
break;
case Repeating:
timers[t].count = timers[t].reload;
/* FALLTHROUGH */
/* to callback */
case Timeout:
if (timers[t].usr.cb) {
timers[t].usr.cb(t, timers[t].usr.context);
}
break;
default:
break;
}
}
}
}
}
/****************************************************************************
* Name: timer_allocate
*
* Description:
* Is used to allocate a timer. Allocation does not involve memory
* allocation as the data for the timer are compile time generated.
* See OPT_BL_NUMBER_TIMERS
*
* There is an inherent priority to the timers in that the first timer
* allocated is the first timer run per tick.
*
* There are 3 modes of operation for the timers. All modes support an
* optional call back on expiration.
*
* modeOneShot - Specifies a one-shot timer. After notification timer
* is resource is freed.
* modeRepeating - Specifies a repeating timer that will reload and
* call an optional.
* modeTimeout - Specifies a persistent start / stop timer.
*
* modeStarted - Or'ed in to start the timer when allocated
*
*
* Input Parameters:
* mode - One of bl_timer_modes_t with the Optional modeStarted
* msfromnow - The reload and initial value for the timer in Ms.
* fc - A pointer or NULL (0). If it is non null it can be any
* of the following:
*
* a) A bl_timer_cb_t populated on the users stack or
* in the data segment. The values are copied into the
* internal data structure of the timer and therefore do
* not have to persist after the call to timer_allocate
*
* b) The address of null_cb. This is identical to passing
* null for the value of fc.
*
* Returned Value:
* On success a value from 0 - OPT_BL_NUMBER_TIMERS-1 that is
* the bl_timer_id for subsequent timer operations
* -1 on failure. This indicates there are no free timers.
*
****************************************************************************/
bl_timer_id timer_allocate(bl_timer_modes_t mode, time_ms_t msfromnow, bl_timer_cb_t *fc)
{
bl_timer_id t;
irqstate_t s = enter_critical_section();
for (t = arraySize(timers) - 1; (int8_t)t >= 0; t--) {
if ((timers[t].ctl & inuse) == 0) {
timers[t].reload = msfromnow;
timers[t].count = msfromnow;
timers[t].usr = fc ? *fc : null_cb;
timers[t].ctl = (mode & (modeMsk | running)) | (inuse);
break;
}
}
leave_critical_section(s);
return t;
}
/****************************************************************************
* Name: timer_free
*
* Description:
* Is used to free a timer. Freeing a timer does not involve memory
* deallocation as the data for the timer are compile time generated.
* See OPT_BL_NUMBER_TIMERS
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* None.
*
****************************************************************************/
void timer_free(bl_timer_id id)
{
DEBUGASSERT(id >= 0 && id < arraySize(timers));
irqstate_t s = enter_critical_section();
memset(&timers[id], 0, sizeof(timers[id]));
leave_critical_section(s);
}
/****************************************************************************
* Name: timer_start
*
* Description:
* Is used to Start a timer. The reload value is copied to the counter.
* And the running bit it set. There is no problem in Starting a running
* timer. But it will restart the timeout.
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* None.
*
****************************************************************************/
void timer_start(bl_timer_id id)
{
DEBUGASSERT(id >= 0 && id < arraySize(timers) && (timers[id].ctl & inuse));
irqstate_t s = enter_critical_section();
timers[id].count = timers[id].reload;
timers[id].ctl |= running;
leave_critical_section(s);
}
/****************************************************************************
* Name: timer_stop
*
* Description:
* Is used to stop a timer. It is Ok to stop a stopped timer.
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* None.
*
****************************************************************************/
void timer_stop(bl_timer_id id)
{
DEBUGASSERT(id >= 0 && id < arraySize(timers) && (timers[id].ctl & inuse));
irqstate_t s = enter_critical_section();
timers[id].ctl &= ~running;
leave_critical_section(s);
}
/****************************************************************************
* Name: timer_expired
*
* Description:
* Test if a timer that was configured as a modeTimeout timer is expired.
* To be expired the time has to be running and have a count of 0.
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* Non Zero if the timer is expired otherwise zero.
*
****************************************************************************/
int timer_expired(bl_timer_id id)
{
DEBUGASSERT(id >= 0 && id < arraySize(timers) && (timers[id].ctl & inuse));
irqstate_t s = enter_critical_section();
int rv = ((timers[id].ctl & running) && timers[id].count == 0);
leave_critical_section(s);
return rv;
}
/****************************************************************************
* Name: timer_restart
*
* Description:
* Is used to re start a timer with a new reload count. The reload value
* is copied to the counter and the running bit it set. There is no
* problem in restarting a running timer.
*
* Input Parameters:
* id - Returned from timer_allocate;
* ms - Is a time_ms_t and the new reload value to use.
*
* Returned Value:
* None.
*
****************************************************************************/
void timer_restart(bl_timer_id id, time_ms_t ms)
{
DEBUGASSERT(id >= 0 && id < arraySize(timers) && (timers[id].ctl & inuse));
irqstate_t s = enter_critical_section();
timers[id].count = timers[id].reload = ms;
timers[id].ctl |= running;
leave_critical_section(s);
}
/****************************************************************************
* Name: timer_ref
*
* Description:
* Returns an time_ref_t that is a reference (ponter) to the internal counter
* of the timer selected by id. It should only be used with calls to
* timer_ref_expired.
*
* Input Parameters:
* id - Returned from timer_allocate;
*
* Returned Value:
* An internal reference that should be treated as opaque by the caller and
* should only be used with calls to timer_ref_expired.
* There is no reference counting on the reference and therefore does not
* require any operation to free it.
*
*************************************************************************/
time_ref_t timer_ref(bl_timer_id id)
{
DEBUGASSERT(id >= 0 && id < arraySize(timers) && (timers[id].ctl & inuse));
return (time_ref_t) &timers[id].count;
}
/****************************************************************************
* Name: timer_init
*
* Description:
* Called early in os_start to initialize the data associated with
* the timers
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
__EXPORT void timer_init(void)
{
/* For system timing probing see bord.h and
* CONFIG_BOARD_USE_PROBES
*/
//PROBE_INIT(7);
//PROBE(1, true);
//PROBE(2, true);
//PROBE(3, true);
//PROBE(1, false);
//PROBE(2, false);
//PROBE(3, false);
/* This is the lowlevel IO if needed to instrument timing
* with the smallest impact
* *((uint32_t *)0x40011010) = 0x100; // PROBE(3,true);
* *((uint32_t *)0x40011014) = 0x100; // PROBE(3,false);
*/
/* Initialize timer data */
sys_tic = 0;
memset(timers, 0, sizeof(timers));
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,138 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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 <stdint.h>
#include <stdlib.h>
#include "crc.h"
/****************************************************************************
* Name: crc16_add
*
* Description:
* Use to calculates a CRC-16-CCITT using the polynomial of
* 0x1021 by adding a value successive values.
*
* Input Parameters:
* crc - The running total of the crc 16
* value - The value to add
*
* Returned Value:
* The current crc16 with the value processed.
*
****************************************************************************/
uint16_t crc16_add(uint16_t crc, uint8_t value)
{
uint32_t i;
const uint16_t poly = 0x1021u;
crc ^= (uint16_t)((uint16_t) value << 8u);
for (i = 0; i < 8; i++) {
if (crc & (1u << 15u)) {
crc = (uint16_t)((crc << 1u) ^ poly);
} else {
crc = (uint16_t)(crc << 1u);
}
}
return crc;
}
/****************************************************************************
* Name: crc16_signature
*
* Description:
* Calculates a CRC-16-CCITT using the crc16_add
* function
*
* Input Parameters:
* initial - The Initial value to uses as the crc's starting point
* length - The number of bytes to add to the crc
* bytes - A pointer to any array of length bytes
*
* Returned Value:
* The crc16 of the array of bytes
*
****************************************************************************/
uint16_t crc16_signature(uint16_t initial, size_t length, const uint8_t *bytes)
{
size_t i;
for (i = 0u; i < length; i++) {
initial = crc16_add(initial, bytes[i]);
}
return initial ^ CRC16_OUTPUT_XOR;
}
/****************************************************************************
* Name: crc64_add_word
*
* Description:
* Calculates a CRC-64-WE using the polynomial of 0x42F0E1EBA9EA3693
* See http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat-bits.64
* Check: 0x62EC59E3F1A4F00A
*
* Input Parameters:
* crc - The running total of the crc 64
* value - The value to add
*
* Returned Value:
* The current crc64 with the value processed.
*
****************************************************************************/
__EXPORT uint64_t crc64_add_word(uint64_t crc, uint32_t value)
{
uint32_t i, j;
uint8_t byte;
const uint64_t poly = 0x42F0E1EBA9EA3693ull;
for (j = 0; j < 4; j++) {
byte = ((uint8_t *) &value)[j];
crc ^= (uint64_t) byte << 56u;
for (i = 0; i < 8; i++) {
if (crc & (1ull << 63u)) {
crc = (uint64_t)(crc << 1u) ^ poly;
} else {
crc = (uint64_t)(crc << 1u);
}
}
}
return crc;
}
@@ -0,0 +1,82 @@
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
* Author: Ben Dyer <ben_dyer@mac.com>
* Pavel Kirienko <pavel.kirienko@zubax.com>
* David Sidrane <david_s5@nscdg.com>
*
* 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 <stdint.h>
#include "random.h"
#define RAND_MAX_32 ((1U << 31) - 1)
static uint32_t rseed;
/****************************************************************************
* Name: util_srand
*
* Description:
* This function seeds the random number generator
*
*
* Input Parameters:
* seed - The seed
*
* Returned value:
* None
*
****************************************************************************/
void util_srand(uint16_t seed)
{
rseed = seed;
}
/****************************************************************************
* Name: util_random
*
* Description:
* This function returns a random number between min and max
*
*
* Input Parameters:
* min - The minimum value the return value can be.
* max - The maximum value the return value can be.
*
* Returned value:
* A random number
*
****************************************************************************/
uint16_t util_random(uint16_t min, uint16_t max)
{
uint16_t rand = (rseed = (rseed * 214013 + 2531011) & RAND_MAX_32) >> 16;
return rand % (max - min) + min;
}