From fab109b4dcea05293c196947741e53cad67e9684 Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Fri, 20 Nov 2015 09:40:09 -0800 Subject: [PATCH] Added a page describing how I use GPIOs to instrument some code. --- Instrumenting-code-using-GPIOs.md | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Instrumenting-code-using-GPIOs.md diff --git a/Instrumenting-code-using-GPIOs.md b/Instrumenting-code-using-GPIOs.md new file mode 100644 index 0000000..702d7d4 --- /dev/null +++ b/Instrumenting-code-using-GPIOs.md @@ -0,0 +1,36 @@ +I often find that I want to instrument things (like ISR routines) so I get some detailed timing information. So I thought I would document the process I use. + +###Initialize the Pins### + +I typically use pins X1, X2, X3, and X4, because they happen to correspond to CPU pins A0, A1, A2, and A3. We need to initialize the pins as outputs. A reasonable place to put the initialization code is in main.c, just after the GPIO clocks are initialized (search for ```__GPIOD_CLK_ENABLE```). The following snippet is what I would use to initialize 4 pins: +```C + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStructure.Pull = GPIO_NOPULL; + GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; + GPIO_InitStructure.Alternate = 0; + GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3; + HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); +``` +main.c already includes enough headers that nothing extra is needed for the above. + +###Toggle the pins### + +Let's suppose that I want to instrument the IRQ Handler for UART6. In the ```stm32_it.c``` file, we'll need to pull in some header files. So add the following #includes: +```C +#include "py/mphal.h" +#include "pin.h" +#include "genhdr/pins.h" +``` +and now we can toggle a GPIO in the ISR handler like so: +```C +void USART6_IRQHandler(void) { + GPIO_set_pin(pin_A0.gpio, pin_A0.pin_mask); + uart_irq_handler(6); + GPIO_clear_pin(pin_A0.gpio, pin_A0.pin_mask); +} +``` + +###Capture### +Here's what a typical capture might look like. This was done using the above code and captured while receiving UART data at 1 Mbit/sec. +![UART6 1 Mbit/sec capture](https://www.dropbox.com/s/2gnnh9o2rsis4bz/UART-1Mbit.png?dl=0) \ No newline at end of file