From d401252c9eb684e045cecd89d5b30cad4488b710 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Mon, 2 Jan 2017 14:45:38 +0100 Subject: [PATCH] IO firmware: Convert magic numbers to defines This is necessary to allow more and better unit testing. --- src/modules/px4iofirmware/mixer.cpp | 2 +- src/modules/px4iofirmware/mixer.h | 42 +++++++++++++++++++++++++++ src/modules/px4iofirmware/protocol.h | 11 +++++-- src/modules/px4iofirmware/px4io.h | 3 +- src/modules/px4iofirmware/registers.c | 4 +-- 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 src/modules/px4iofirmware/mixer.h diff --git a/src/modules/px4iofirmware/mixer.cpp b/src/modules/px4iofirmware/mixer.cpp index 47586b4590..12468515cd 100644 --- a/src/modules/px4iofirmware/mixer.cpp +++ b/src/modules/px4iofirmware/mixer.cpp @@ -465,7 +465,7 @@ mixer_callback(uintptr_t handle, * not loaded faithfully. */ -static char mixer_text[200]; /* large enough for one mixer */ +static char mixer_text[PX4IO_MAX_MIXER_LENGHT]; /* large enough for one mixer */ static unsigned mixer_text_length = 0; int diff --git a/src/modules/px4iofirmware/mixer.h b/src/modules/px4iofirmware/mixer.h new file mode 100644 index 0000000000..62df8d9819 --- /dev/null +++ b/src/modules/px4iofirmware/mixer.h @@ -0,0 +1,42 @@ +/**************************************************************************** + * + * Copyright (c) 2017 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 mixer.h + * + * PX4IO mixer definitions + * + * @author Lorenz Meier + */ +#pragma once +#define PX4IO_MAX_MIXER_LENGHT 230 diff --git a/src/modules/px4iofirmware/protocol.h b/src/modules/px4iofirmware/protocol.h index 7559da9b75..a787a9cc17 100644 --- a/src/modules/px4iofirmware/protocol.h +++ b/src/modules/px4iofirmware/protocol.h @@ -82,16 +82,17 @@ #define PX4IO_PROTOCOL_MAX_CONTROL_COUNT 8 /**< The protocol does not support more than set here, individual units might support less - see PX4IO_P_CONFIG_CONTROL_COUNT */ /* static configuration page */ -#define PX4IO_PAGE_CONFIG 0 +#define PX4IO_PAGE_CONFIG 0 #define PX4IO_P_CONFIG_PROTOCOL_VERSION 0 /* PX4IO_PROTOCOL_VERSION */ #define PX4IO_P_CONFIG_HARDWARE_VERSION 1 /* magic numbers TBD */ #define PX4IO_P_CONFIG_BOOTLOADER_VERSION 2 /* get this how? */ -#define PX4IO_P_CONFIG_MAX_TRANSFER 3 /* maximum I2C transfer size */ +#define PX4IO_P_CONFIG_MAX_TRANSFER 3 /* maximum I2C transfer size */ #define PX4IO_P_CONFIG_CONTROL_COUNT 4 /* hardcoded max control count supported */ #define PX4IO_P_CONFIG_ACTUATOR_COUNT 5 /* hardcoded max actuator output count */ #define PX4IO_P_CONFIG_RC_INPUT_COUNT 6 /* hardcoded max R/C input count supported */ #define PX4IO_P_CONFIG_ADC_INPUT_COUNT 7 /* hardcoded max ADC inputs */ -#define PX4IO_P_CONFIG_RELAY_COUNT 8 /* hardcoded # of relay outputs */ +#define PX4IO_P_CONFIG_RELAY_COUNT 8 /* hardcoded # of relay outputs */ +#define PX4IO_MAX_TRANSFER_LEN 64 /* dynamic status page */ #define PX4IO_PAGE_STATUS 1 @@ -338,6 +339,10 @@ struct IOPacket { }; #pragma pack(pop) +#if (PX4IO_MAX_TRANSFER_LEN > PKT_MAX_REGS * 2) +#error The max transfer length of the IO protocol must not be larger than the IO packet size +#endif + #define PKT_CODE_READ 0x00 /* FMU->IO read transaction */ #define PKT_CODE_WRITE 0x40 /* FMU->IO write transaction */ #define PKT_CODE_SUCCESS 0x00 /* IO->FMU success reply */ diff --git a/src/modules/px4iofirmware/px4io.h b/src/modules/px4iofirmware/px4io.h index edec29ed02..5ca8564a79 100644 --- a/src/modules/px4iofirmware/px4io.h +++ b/src/modules/px4iofirmware/px4io.h @@ -57,7 +57,8 @@ /* * Constants and limits. */ -#define PX4IO_SERVO_COUNT 8 +#define PX4IO_BL_VERSION 3 +#define PX4IO_SERVO_COUNT 8 #define PX4IO_CONTROL_CHANNELS 8 #define PX4IO_CONTROL_GROUPS 4 #define PX4IO_RC_INPUT_CHANNELS 18 diff --git a/src/modules/px4iofirmware/registers.c b/src/modules/px4iofirmware/registers.c index fd0b484f5b..5b5696836b 100644 --- a/src/modules/px4iofirmware/registers.c +++ b/src/modules/px4iofirmware/registers.c @@ -69,8 +69,8 @@ static const uint16_t r_page_config[] = { #else [PX4IO_P_CONFIG_HARDWARE_VERSION] = 1, #endif - [PX4IO_P_CONFIG_BOOTLOADER_VERSION] = 3, /* XXX hardcoded magic number */ - [PX4IO_P_CONFIG_MAX_TRANSFER] = 64, /* XXX hardcoded magic number */ + [PX4IO_P_CONFIG_BOOTLOADER_VERSION] = PX4IO_BL_VERSION, + [PX4IO_P_CONFIG_MAX_TRANSFER] = PX4IO_MAX_TRANSFER_LEN, [PX4IO_P_CONFIG_CONTROL_COUNT] = PX4IO_CONTROL_CHANNELS, [PX4IO_P_CONFIG_ACTUATOR_COUNT] = PX4IO_SERVO_COUNT, [PX4IO_P_CONFIG_RC_INPUT_COUNT] = PX4IO_RC_INPUT_CHANNELS,