dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_debug.c
Go to the documentation of this file.
1 
15 /*
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.
20  *
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.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 /* Project Includes */
31 #include "pios.h"
32 
33 // Global variables
34 const char *PIOS_DEBUG_AssertMsg = "ASSERT FAILED";
35 
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 */
40 
44 void PIOS_DEBUG_Init(const struct pios_tim_channel * channels, uint8_t num_channels)
45 {
46 #ifdef PIOS_ENABLE_DEBUG_PINS
47  PIOS_Assert(channels);
48  PIOS_Assert(num_channels);
49 
50  /* Store away the GPIOs we've been given */
51  debug_channels = channels;
52  debug_num_channels = num_channels;
53 
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];
57 
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_PP;
62  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
63  GPIO_InitStructure.GPIO_Pin = chan->init->GPIO_Pin;
64 
65  /* Initialize the GPIO */
66  GPIO_Init(chan->init->port, &GPIO_InitStructure);
67 
68  /* Set the pin low */
69  GPIO_WriteBit(chan->init->port, chan->init->GPIO_Pin, Bit_RESET);
70  }
71 #endif // PIOS_ENABLE_DEBUG_PINS
72 }
73 
78 void PIOS_DEBUG_PinHigh(uint8_t pin)
79 {
80 #ifdef PIOS_ENABLE_DEBUG_PINS
81  if (!debug_channels || pin >= debug_num_channels) {
82  return;
83  }
84 
85  const struct pios_tim_channel * chan = &debug_channels[pin];
86 
87  GPIO_WriteBit(chan->init->port, chan->init->GPIO_Pin, Bit_Set);
88 
89 #endif // PIOS_ENABLE_DEBUG_PINS
90 }
91 
96 void PIOS_DEBUG_PinLow(uint8_t pin)
97 {
98 #ifdef PIOS_ENABLE_DEBUG_PINS
99  if (!debug_channels || pin >= debug_num_channels) {
100  return;
101  }
102 
103  const struct pios_tim_channel * chan = &debug_channels[pin];
104 
105  GPIO_WriteBit(chan->init->port, chan->init->GPIO_Pin, Bit_RESET);
106 
107 #endif // PIOS_ENABLE_DEBUG_PINS
108 }
109 
110 
111 void PIOS_DEBUG_PinValue8Bit(uint8_t value)
112 {
113 #ifdef PIOS_ENABLE_DEBUG_PINS
114  if (!debug_channels) {
115  return;
116  }
117 
118  uint32_t bsrr_l = ( ((~value)&0x0F)<<(16+6) ) | ((value & 0x0F)<<6);
119  uint32_t bsrr_h = ( ((~value)&0xF0)<<(16+6-4) ) | ((value & 0xF0)<<(6-4));
120 
122 
123  /*
124  * This is sketchy since it assumes a particular ordering
125  * and bitwise layout of the channels provided to the debug code.
126  */
127  debug_channels[0].init.port->BSRR = bsrr_l;
128  debug_channels[4].init.port->BSRR = bsrr_h;
129 
130  PIOS_IRQ_Enable();
131 #endif // PIOS_ENABLE_DEBUG_PINS
132 }
133 
134 void PIOS_DEBUG_PinValue4BitL(uint8_t value)
135 {
136 #ifdef PIOS_ENABLE_DEBUG_PINS
137  if (!debug_channels) {
138  return;
139  }
140 
141  /*
142  * This is sketchy since it assumes a particular ordering
143  * and bitwise layout of the channels provided to the debug code.
144  */
145  uint32_t bsrr_l = ((~(value & 0x0F)<<(16+6))) | ((value & 0x0F)<<6);
146  debug_channels[0].init.port->BSRR = bsrr_l;
147 #endif // PIOS_ENABLE_DEBUG_PINS
148 }
149 
150 
154 void PIOS_DEBUG_Panic(const char *msg)
155 {
156 #if defined(PIOS_INCLUDE_COM) && defined(PIOS_COM_DEBUG)
157  register int *lr asm("lr"); // Link-register holds the PC of the caller
159 #endif
160 
162 
163  // Stay put
164  while (1) ;
165 }
166 
#define PIOS_COM_DEBUG
Definition: pios_board.h:67
int32_t PIOS_IRQ_Enable(void)
Definition: pios_irq.c:53
Main PiOS header to include all the compiled in PiOS options.
int32_t PIOS_COM_SendFormattedStringNonBlocking(uintptr_t com_id, const char *format,...)
void PIOS_DEBUG_PinValue8Bit(uint8_t value)
Definition: pios_debug.c:62
void PIOS_DEBUG_PinHigh(uint8_t pin)
Definition: pios_debug.c:49
struct stm32_gpio pin
Definition: pios_tim_priv.h:17
const char * PIOS_DEBUG_AssertMsg
Definition: pios_debug.c:34
void PIOS_DEBUG_Init(const struct pios_tim_channel *channels, uint8_t num_channels)
Definition: pios_debug.c:44
void PIOS_DEBUG_PinLow(uint8_t pin)
Definition: pios_debug.c:57
uint8_t i
Definition: msp_messages.h:97
uint16_t value
Definition: storm32bgc.c:155
int32_t PIOS_IRQ_Disable(void)
Definition: pios_irq.c:40
void PIOS_DEBUG_PinValue4BitL(uint8_t value)
Definition: pios_debug.c:66
void PIOS_DEBUG_Panic(const char *msg) __attribute__((noreturn))
Definition: pios_debug.c:74
#define PIOS_Assert(test)
Definition: pios_debug.h:52