LP-602 significant change to USB layer. force complete USB stack reset on replug...
[librepilot.git] / flight / pios / stm32f4xx / pios_debug.c
bloba7c9bf53a3ea6de4b36674345da4d9d91235a410
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
5 * @defgroup PIOS_DEBUG Debugging Functions
6 * @brief Debugging functionality
7 * @{
9 * @file pios_debug.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2013.
11 * @brief Debugging Functions
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "pios.h"
33 // Global variables
34 const char *PIOS_DEBUG_AssertMsg = "ASSERT FAILED";
36 #ifdef PIOS_ENABLE_DEBUG_PINS
37 static const struct pios_tim_channel *debug_channels;
38 static uint8_t debug_num_channels;
39 #endif /* PIOS_ENABLE_DEBUG_PINS */
41 /**
42 * Initialise Debug-features
44 void PIOS_DEBUG_Init(__attribute__((unused)) const struct pios_tim_channel *channels, __attribute__((unused)) uint8_t num_channels)
46 #ifdef PIOS_ENABLE_DEBUG_PINS
47 PIOS_Assert(channels);
48 PIOS_Assert(num_channels);
50 /* Store away the GPIOs we've been given */
51 debug_channels = channels;
52 debug_num_channels = num_channels;
54 /* Configure the GPIOs we've been given */
55 for (uint8_t i = 0; i < num_channels; i++) {
56 const struct pios_tim_channel *chan = &channels[i];
58 // Initialise pins as standard output pins
59 GPIO_InitTypeDef GPIO_InitStructure;
60 GPIO_StructInit(&GPIO_InitStructure);
61 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
62 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
63 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
64 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
65 GPIO_InitStructure.GPIO_Pin = chan->pin.init.GPIO_Pin;
67 /* Initialize the GPIO */
68 GPIO_Init(chan->pin.gpio, &GPIO_InitStructure);
70 /* Set the pin low */
71 GPIO_WriteBit(chan->pin.gpio, chan->pin.init.GPIO_Pin, Bit_RESET);
73 #endif // PIOS_ENABLE_DEBUG_PINS
76 /**
77 * Set debug-pin high
78 * \param pin 0 for S1 output
80 void PIOS_DEBUG_PinHigh(__attribute__((unused)) uint8_t pin)
82 #ifdef PIOS_ENABLE_DEBUG_PINS
83 if (!debug_channels || pin >= debug_num_channels) {
84 return;
87 const struct pios_tim_channel *chan = &debug_channels[pin];
89 GPIO_WriteBit(chan->pin.gpio, chan->pin.init.GPIO_Pin, Bit_SET);
91 #endif // PIOS_ENABLE_DEBUG_PINS
94 /**
95 * Set debug-pin low
96 * \param pin 0 for S1 output
98 void PIOS_DEBUG_PinLow(__attribute__((unused)) uint8_t pin)
100 #ifdef PIOS_ENABLE_DEBUG_PINS
101 if (!debug_channels || pin >= debug_num_channels) {
102 return;
105 const struct pios_tim_channel *chan = &debug_channels[pin];
107 GPIO_WriteBit(chan->pin.gpio, chan->pin.init.GPIO_Pin, Bit_RESET);
109 #endif // PIOS_ENABLE_DEBUG_PINS
113 void PIOS_DEBUG_PinValue8Bit(__attribute__((unused)) uint8_t value)
115 #ifdef PIOS_ENABLE_DEBUG_PINS
116 if (!debug_channels) {
117 return;
120 #pragma message("This code is not portable and should be revised")
121 PIOS_Assert(0);
123 uint32_t bsrr_l = (((~value) & 0x0F) << (16 + 6)) | ((value & 0x0F) << 6);
124 uint32_t bsrr_h = (((~value) & 0xF0) << (16 + 6 - 4)) | ((value & 0xF0) << (6 - 4));
126 PIOS_IRQ_Disable();
129 * This is sketchy since it assumes a particular ordering
130 * and bitwise layout of the channels provided to the debug code.
132 // debug_channels[0].pin.gpio->BSRR = bsrr_l;
133 // debug_channels[4].pin.gpio->BSRR = bsrr_h;
135 PIOS_IRQ_Enable();
136 #endif // PIOS_ENABLE_DEBUG_PINS
139 void PIOS_DEBUG_PinValue4BitL(__attribute__((unused)) uint8_t value)
141 #ifdef PIOS_ENABLE_DEBUG_PINS
142 if (!debug_channels) {
143 return;
146 #pragma message("This code is not portable and should be revised")
147 PIOS_Assert(0);
150 * This is sketchy since it assumes a particular ordering
151 * and bitwise layout of the channels provided to the debug code.
153 uint32_t bsrr_l = ((~(value & 0x0F) << (16 + 6))) | ((value & 0x0F) << 6);
154 // debug_channels[0].pin.gpio->BSRR = bsrr_l;
155 #endif // PIOS_ENABLE_DEBUG_PINS
160 * Report a serious error and halt
162 void PIOS_DEBUG_Panic(__attribute__((unused)) const char *msg)
164 #ifdef PIOS_INCLUDE_DEBUG_CONSOLE
165 register int *lr asm ("lr"); // Link-register holds the PC of the caller
166 DEBUG_PRINTF(0, "\r%s @0x%x\r", msg, lr);
167 #endif
169 // Stay put
170 while (1) {
176 * @}
177 * @}