Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers debug.c Source File

debug.c

00001 /**
00002  * @file debug.c
00003  * @brief Debugging facilities
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License
00011  * as published by the Free Software Foundation; either version 2
00012  * of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software Foundation,
00021  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022  *
00023  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00024  * @version 1.7.6
00025  **/
00026 
00027 //Dependencies
00028 #include "stm32f7xx.h"
00029 #include "stm32f7xx_hal.h"
00030 #include "debug.h"
00031 
00032 //Variable declaration
00033 static UART_HandleTypeDef UART_Handle;
00034 
00035 
00036 /**
00037  * @brief Debug UART initialization
00038  * @param[in] baudrate UART baudrate
00039  **/
00040 
00041 void debugInit(uint32_t baudrate)
00042 {
00043    GPIO_InitTypeDef GPIO_InitStructure;
00044 
00045    //Enable GPIOD clock
00046    __HAL_RCC_GPIOD_CLK_ENABLE();
00047    //Enable USART3 clock
00048    __HAL_RCC_USART3_CLK_ENABLE();
00049 
00050    //Configure USART3_TX (PD8)
00051    GPIO_InitStructure.Pin = GPIO_PIN_8;
00052    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
00053    GPIO_InitStructure.Pull = GPIO_PULLUP;
00054    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
00055    GPIO_InitStructure.Alternate = GPIO_AF7_USART3;
00056    HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
00057 
00058    //Configure USART3_RX (PD9)
00059    GPIO_InitStructure.Pin = GPIO_PIN_9;
00060    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
00061    GPIO_InitStructure.Pull = GPIO_PULLUP;
00062    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
00063    GPIO_InitStructure.Alternate = GPIO_AF7_USART3;
00064    HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
00065 
00066    //Configure USART3
00067    UART_Handle.Instance = USART3;
00068    UART_Handle.Init.BaudRate = baudrate;
00069    UART_Handle.Init.WordLength = UART_WORDLENGTH_8B;
00070    UART_Handle.Init.StopBits = UART_STOPBITS_1;
00071    UART_Handle.Init.Parity = UART_PARITY_NONE;
00072    UART_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
00073    UART_Handle.Init.Mode = UART_MODE_TX_RX;
00074    HAL_UART_Init(&UART_Handle);
00075 }
00076 
00077 
00078 /**
00079  * @brief Display the contents of an array
00080  * @param[in] stream Pointer to a FILE object that identifies an output stream
00081  * @param[in] prepend String to prepend to the left of each line
00082  * @param[in] data Pointer to the data array
00083  * @param[in] length Number of bytes to display
00084  **/
00085 
00086 void debugDisplayArray(FILE *stream,
00087    const char_t *prepend, const void *data, size_t length)
00088 {
00089    uint_t i;
00090 
00091    for(i = 0; i < length; i++)
00092    {
00093       //Beginning of a new line?
00094       if((i % 16) == 0)
00095          fprintf(stream, "%s", prepend);
00096       //Display current data byte
00097       fprintf(stream, "%02" PRIX8 " ", *((uint8_t *) data + i));
00098       //End of current line?
00099       if((i % 16) == 15 || i == (length - 1))
00100          fprintf(stream, "\r\n");
00101    }
00102 }
00103 
00104 
00105 /**
00106  * @brief Write character to stream
00107  * @param[in] c The character to be written
00108  * @param[in] stream Pointer to a FILE object that identifies an output stream
00109  * @return On success, the character written is returned. If a writing
00110  *   error occurs, EOF is returned
00111  **/
00112 
00113 int_t fputc(int_t c, FILE *stream)
00114 {
00115    //Standard output or error output?
00116    if(stream == stdout || stream == stderr)
00117    {
00118       //Character to be written
00119       uint8_t ch = c;
00120 
00121       //Transmit data
00122       HAL_UART_Transmit(&UART_Handle, &ch, 1, HAL_MAX_DELAY);
00123 
00124       //On success, the character written is returned
00125       return c;
00126    }
00127    //Unknown output?
00128    else
00129    {
00130       //If a writing error occurs, EOF is returned
00131       return EOF;
00132    }
00133 }
00134