summaryrefslogtreecommitdiff
path: root/src/short-test/src
diff options
context:
space:
mode:
Diffstat (limited to 'src/short-test/src')
-rw-r--r--src/short-test/src/main.c270
-rw-r--r--src/short-test/src/stm32f4xx_hal_msp.c77
-rw-r--r--src/short-test/src/stm32f4xx_it.c54
-rw-r--r--src/short-test/src/stm_init.c202
4 files changed, 603 insertions, 0 deletions
diff --git a/src/short-test/src/main.c b/src/short-test/src/main.c
new file mode 100644
index 0000000..6e944c9
--- /dev/null
+++ b/src/short-test/src/main.c
@@ -0,0 +1,270 @@
+/*
+ * Test code that just sends the letters 'a' to 'z' over and
+ * over again using USART2.
+ *
+ * Toggles the BLUE LED slowly and the RED LED for every
+ * character sent.
+ */
+#include "stm_init.h"
+
+void test_for_shorts(char port, GPIO_TypeDef* GPIOx, uint16_t GPIO_Test_Pins);
+void uart_send_binary(uint32_t num, uint8_t bits);
+
+//------------------------------------------------------------------------------
+// Defines
+//------------------------------------------------------------------------------
+#define GPIO_PORT_LEDS GPIOJ
+
+#define GPIO_PIN_LED_RED GPIO_PIN_1
+#define GPIO_PIN_LED_YELLOW GPIO_PIN_2
+#define GPIO_PIN_LED_GREEN GPIO_PIN_3
+#define GPIO_PIN_LED_BLUE GPIO_PIN_4
+
+#define DELAY() HAL_Delay(100)
+
+#define DEBUG_ADJACENT 0
+
+//------------------------------------------------------------------------------
+// Macros
+//------------------------------------------------------------------------------
+#define led_on(pin) HAL_GPIO_WritePin(GPIO_PORT_LEDS, pin, GPIO_PIN_SET)
+#define led_off(pin) HAL_GPIO_WritePin(GPIO_PORT_LEDS, pin, GPIO_PIN_RESET)
+#define led_toggle(pin) HAL_GPIO_TogglePin(GPIO_PORT_LEDS, pin)
+
+
+/* These are all the pins used by the FMC interface */
+#define GPIOB_PINS (GPIO_PIN_7)
+
+#define GPIOD_PINS (GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11 \
+ |GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15 \
+ |GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_4 \
+ |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7)
+
+#define GPIOE_PINS (GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7 \
+ |GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11 \
+ |GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15)
+
+#define GPIOF_PINS (GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 \
+ |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_12|GPIO_PIN_13 \
+ |GPIO_PIN_14|GPIO_PIN_15)
+
+#define GPIOG_PINS (GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 \
+ |GPIO_PIN_4|GPIO_PIN_5)
+
+#define GPIOH_PINS (GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11 \
+ |GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15)
+
+#define GPIOI_PINS (GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_0|GPIO_PIN_1 \
+ |GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7)
+
+int
+main()
+{
+ GPIO_InitTypeDef GPIO_InitStruct;
+
+ stm_init();
+
+ // enable gpio clocks
+ __GPIOA_CLK_ENABLE();
+ __GPIOB_CLK_ENABLE();
+ __GPIOD_CLK_ENABLE();
+ __GPIOE_CLK_ENABLE();
+ __GPIOF_CLK_ENABLE();
+ __GPIOG_CLK_ENABLE();
+ __GPIOH_CLK_ENABLE();
+ __GPIOI_CLK_ENABLE();
+
+ configure_all_as_input(GPIOB, GPIOB_PINS);
+ configure_all_as_input(GPIOD, GPIOD_PINS);
+ configure_all_as_input(GPIOE, GPIOE_PINS);
+ configure_all_as_input(GPIOF, GPIOF_PINS);
+ configure_all_as_input(GPIOG, GPIOG_PINS);
+ configure_all_as_input(GPIOH, GPIOH_PINS);
+ configure_all_as_input(GPIOI, GPIOI_PINS);
+
+ /* Change one pin to output */
+ GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_11;
+ GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+ GPIO_InitStruct.Pull = GPIO_PULLUP;
+ GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
+ HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
+
+ while (1) {
+ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_SET);
+ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
+ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_RESET);
+ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
+ }
+
+ while (1) {
+ HAL_GPIO_TogglePin(LED_PORT, LED_GREEN);
+ HAL_UART_Transmit(&huart2, (uint8_t *) "\r\n\r\n\r\n\r\n\r\n", 10, 0x1);
+
+ test_for_shorts('B', GPIOB, GPIOB_PINS);
+ test_for_shorts('D', GPIOD, GPIOD_PINS);
+ test_for_shorts('E', GPIOE, GPIOE_PINS);
+ test_for_shorts('F', GPIOF, GPIOF_PINS);
+ test_for_shorts('G', GPIOG, GPIOG_PINS);
+ test_for_shorts('H', GPIOH, GPIOH_PINS);
+ test_for_shorts('I', GPIOI, GPIOI_PINS);
+
+ led_toggle(LED_BLUE);
+ HAL_Delay(2000);
+ }
+}
+
+void configure_all_as_input(GPIO_TypeDef* GPIOx, uint16_t GPIO_Test_Pins)
+{
+ GPIO_InitTypeDef GPIO_InitStruct;
+
+ /* Configure all pins as input. XXX do all pins (0xffff) instead of just GPIO_Test_Pins? */
+ GPIO_InitStruct.Pin = GPIO_Test_Pins;
+ //GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+ GPIO_InitStruct.Mode = GPIO_MODE_INPUT; /* XXX does GPIO_PULLDOWN work with GPIO_MODE_INPUT? */
+ GPIO_InitStruct.Pull = GPIO_PULLDOWN;
+ GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
+ HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
+}
+
+uint8_t check_no_input(char port, GPIO_TypeDef* GPIOx, uint16_t GPIO_Test_Pins, char wrote_port, uint16_t wrote_value)
+{
+ uint16_t read;
+
+ /* Read all pins from port at once. XXX check all pins, not just GPIO_Test_Pins? */
+ read = (GPIOx->IDR & GPIO_Test_Pins);
+
+ if (DEBUG_ADJACENT && port == 'H' && wrote_port == 'I' && wrote_value < 4) {
+ HAL_UART_Transmit(&huart2, (uint8_t *) "DEBUG R GPIOH ", 14, 0x1);
+ uart_send_binary(GPIO_Test_Pins, 16);
+ HAL_UART_Transmit(&huart2, (uint8_t *) " -> ", 4, 0x1);
+ uart_send_binary(read, 16);
+ HAL_UART_Transmit(&huart2, (uint8_t *) "\r\n", 2, 0x1);
+ }
+
+ if (! read) {
+ /* No unexpected pins read as HIGH */
+ return 0;
+ }
+
+ led_on(LED_RED);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) "Wrote ", 6, 0x1);
+ uart_send_binary(wrote_value, 16);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) " to port GPIO", 13, 0x1);
+ HAL_UART_Transmit(&huart2, (uint8_t *) &wrote_port, 1, 0x1);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) ", read ", 7, 0x1);
+ uart_send_binary(read, 16);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) " from GPIO", 10, 0x1);
+ HAL_UART_Transmit(&huart2, (uint8_t *) &port, 1, 0x1);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) "\r\n", 2, 0x1);
+
+ return 1;
+}
+
+void test_for_shorts(char port, GPIO_TypeDef* GPIOx, uint16_t GPIO_Test_Pins)
+{
+ GPIO_InitTypeDef GPIO_InitStruct;
+ uint16_t i, fail = 0, Test_Pin, read;
+
+ configure_all_as_input(GPIOB, GPIOB_PINS);
+ configure_all_as_input(GPIOD, GPIOD_PINS);
+ configure_all_as_input(GPIOE, GPIOE_PINS);
+ configure_all_as_input(GPIOF, GPIOF_PINS);
+ configure_all_as_input(GPIOG, GPIOG_PINS);
+ configure_all_as_input(GPIOH, GPIOH_PINS);
+ configure_all_as_input(GPIOI, GPIOI_PINS);
+
+ check_no_input('B', GPIOB, GPIOB_PINS, 'x', 0);
+ check_no_input('D', GPIOD, GPIOD_PINS, 'x', 0);
+ check_no_input('E', GPIOE, GPIOE_PINS, 'x', 0);
+ check_no_input('F', GPIOF, GPIOF_PINS, 'x', 0);
+ check_no_input('G', GPIOG, GPIOG_PINS, 'x', 0);
+ check_no_input('H', GPIOH, GPIOH_PINS, 'x', 0);
+ check_no_input('I', GPIOI, GPIOI_PINS, 'x', 0);
+
+ for (i = 0; i < 31; i++) {
+ Test_Pin = 1 << i;
+ if (! (GPIO_Test_Pins & Test_Pin)) continue;
+
+ configure_all_as_input(GPIOx, GPIO_Test_Pins);
+
+ /* Change one pin to output */
+ GPIO_InitStruct.Pin = Test_Pin;
+ GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+ GPIO_InitStruct.Pull = GPIO_PULLDOWN;
+ GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
+ HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
+
+ HAL_GPIO_WritePin(GPIOx, Test_Pin, GPIO_PIN_SET);
+
+ /* Read all pins from port at once. XXX check all pins, not just GPIO_Test_Pins? */
+ read = GPIOx->IDR & GPIO_Test_Pins;
+
+ if (DEBUG_ADJACENT && port == 'I' && Test_Pin < 4) {
+ HAL_UART_Transmit(&huart2, (uint8_t *) "DEBUG W GPIOI ", 14, 0x1);
+ uart_send_binary(Test_Pin, 16);
+ HAL_UART_Transmit(&huart2, (uint8_t *) " -> ", 4, 0x1);
+ uart_send_binary(read, 16);
+ HAL_UART_Transmit(&huart2, (uint8_t *) "\r\n", 2, 0x1);
+ }
+
+ if (read == Test_Pin) {
+ /* No unexpected pins read as HIGH */
+ led_toggle(LED_GREEN);
+ } else {
+ led_on(LED_RED);
+ HAL_UART_Transmit(&huart2, (uint8_t *) "GPIO", 4, 0x1);
+ HAL_UART_Transmit(&huart2, (uint8_t *) &port, 1, 0x1);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) " exp ", 5, 0x1);
+ uart_send_binary(Test_Pin, 16);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) " got ", 5, 0x1);
+ uart_send_binary(read, 16);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) " diff ", 6, 0x1);
+ uart_send_binary(read ^ Test_Pin, 16);
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) "\r\n", 2, 0x1);
+
+ fail++;
+ }
+
+ /* Check there is no input on any of the other GPIO ports (adjacent pins might live on different ports) */
+ if (port != 'B') fail += check_no_input('B', GPIOB, GPIOB_PINS, port, Test_Pin);
+ if (port != 'D') fail += check_no_input('D', GPIOD, GPIOD_PINS, port, Test_Pin);
+ if (port != 'E') fail += check_no_input('E', GPIOE, GPIOE_PINS, port, Test_Pin);
+ if (port != 'F') fail += check_no_input('F', GPIOF, GPIOF_PINS, port, Test_Pin);
+ if (port != 'G') fail += check_no_input('G', GPIOG, GPIOG_PINS, port, Test_Pin);
+ if (port != 'H') fail += check_no_input('H', GPIOH, GPIOH_PINS, port, Test_Pin);
+ if (port != 'I') fail += check_no_input('I', GPIOI, GPIOI_PINS, port, Test_Pin);
+
+ HAL_GPIO_WritePin(GPIOx, Test_Pin, GPIO_PIN_RESET);
+ }
+
+ if (fail) {
+ HAL_UART_Transmit(&huart2, (uint8_t *) "\r\n", 2, 0x1);
+ }
+}
+
+void uart_send_binary(uint32_t num, uint8_t bits) {
+ uint32_t i;
+ unsigned char ch;
+
+ bits--; /* bits 4 should give i = 1000, not 10000 */
+
+ i = 1 << bits;
+ while (i) {
+ ch = '0';
+ if (num & i) {
+ ch = '1';
+ }
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1);
+ i = i >> 1;
+ }
+}
diff --git a/src/short-test/src/stm32f4xx_hal_msp.c b/src/short-test/src/stm32f4xx_hal_msp.c
new file mode 100644
index 0000000..6bf0b9d
--- /dev/null
+++ b/src/short-test/src/stm32f4xx_hal_msp.c
@@ -0,0 +1,77 @@
+/**
+******************************************************************************
+* File Name : stm32f4xx_hal_msp.c
+* Date : 08/07/2015 17:46:00
+* Description : This file provides code for the MSP Initialization
+* and de-Initialization codes.
+******************************************************************************
+*
+* COPYRIGHT(c) 2015 STMicroelectronics
+*
+* 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 of STMicroelectronics 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 HOLDER 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.
+*
+******************************************************************************
+*/
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f4xx_hal.h"
+#include "stm_init.h"
+
+void HAL_UART_MspInit(UART_HandleTypeDef* huart)
+{
+ GPIO_InitTypeDef GPIO_InitStruct;
+ int i;
+
+ if (huart->Instance == USART2) {
+ /* Peripheral clock enable */
+ __USART2_CLK_ENABLE();
+ __GPIOA_CLK_ENABLE();
+
+ /**USART2 GPIO Configuration
+ PA2 ------> USART2_TX
+ PA3 ------> USART2_RX
+ */
+ GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+ GPIO_InitStruct.Pull = GPIO_PULLUP;
+ GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
+ GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
+ HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+ }
+
+}
+
+void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
+{
+ if (huart->Instance == USART2) {
+ /* Peripheral clock disable */
+ __USART2_CLK_DISABLE();
+
+ /**USART2 GPIO Configuration
+ PA2 ------> USART2_TX
+ PA3 ------> USART2_RX
+ */
+ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
+ }
+}
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/src/short-test/src/stm32f4xx_it.c b/src/short-test/src/stm32f4xx_it.c
new file mode 100644
index 0000000..b816d0d
--- /dev/null
+++ b/src/short-test/src/stm32f4xx_it.c
@@ -0,0 +1,54 @@
+/**
+ ******************************************************************************
+ * @file stm32f4xx_it.c
+ * @date 08/07/2015 17:45:59
+ * @brief Interrupt Service Routines.
+ ******************************************************************************
+ *
+ * COPYRIGHT(c) 2015 STMicroelectronics
+ *
+ * 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 of STMicroelectronics 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 HOLDER 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.
+ *
+ ******************************************************************************
+ */
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f4xx_hal.h"
+#include "stm32f4xx.h"
+#include "stm32f4xx_it.h"
+
+/* External variables --------------------------------------------------------*/
+
+/******************************************************************************/
+/* Cortex-M4 Processor Interruption and Exception Handlers */
+/******************************************************************************/
+
+/**
+* @brief This function handles System tick timer.
+*/
+void SysTick_Handler(void)
+{
+ HAL_IncTick();
+ HAL_SYSTICK_IRQHandler();
+}
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/src/short-test/src/stm_init.c b/src/short-test/src/stm_init.c
new file mode 100644
index 0000000..b6edf3f
--- /dev/null
+++ b/src/short-test/src/stm_init.c
@@ -0,0 +1,202 @@
+/**
+ ******************************************************************************
+ * File Name : main.c
+ * Date : 08/07/2015 17:46:00
+ * Description : Main program body
+ ******************************************************************************
+ *
+ * COPYRIGHT(c) 2015 STMicroelectronics
+ *
+ * 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 of STMicroelectronics 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 HOLDER 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.
+ *
+ ******************************************************************************
+ */
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm_init.h"
+#include "stm32f4xx_hal.h"
+
+UART_HandleTypeDef huart2;
+
+/* Private variables ---------------------------------------------------------*/
+static GPIO_InitTypeDef GPIO_InitStruct;
+
+/* Private function prototypes -----------------------------------------------*/
+void SystemClock_Config(void);
+static void MX_GPIO_Init(void);
+static void MX_USART2_UART_Init(void);
+
+void stm_init(void)
+{
+
+ /* MCU Configuration----------------------------------------------------------*/
+
+ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
+ HAL_Init();
+
+ /* Configure the system clock */
+ SystemClock_Config();
+
+ /* System interrupt init*/
+ /* Sets the priority grouping field */
+ HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
+ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
+
+ /* Initialize all configured peripherals */
+ MX_GPIO_Init();
+ MX_USART2_UART_Init();
+}
+
+/** System Clock Configuration
+ *
+ * HSE crystal at 25 MHz, end result is 180 MHz clock.
+ */
+void SystemClock_Config(void)
+{
+ RCC_ClkInitTypeDef RCC_ClkInitStruct;
+ RCC_OscInitTypeDef RCC_OscInitStruct;
+
+ __PWR_CLK_ENABLE();
+
+ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
+
+ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
+ RCC_OscInitStruct.HSEState = RCC_HSE_ON;
+ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+ RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
+ RCC_OscInitStruct.PLL.PLLM = 25;
+ RCC_OscInitStruct.PLL.PLLN = 360;
+ RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
+ RCC_OscInitStruct.PLL.PLLQ = 4;
+ HAL_RCC_OscConfig(&RCC_OscInitStruct);
+
+ HAL_PWREx_ActivateOverDrive();
+
+ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
+ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
+ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
+ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
+ RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
+ HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
+}
+
+/** System Clock Configuration
+ *
+ * HSI source, end result is 16 MHz SYSCLK
+ *
+*/
+void old_SystemClock_Config(void)
+{
+
+ RCC_OscInitTypeDef RCC_OscInitStruct;
+
+ __PWR_CLK_ENABLE();
+
+ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
+
+ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
+ RCC_OscInitStruct.HSIState = RCC_HSI_ON;
+ RCC_OscInitStruct.HSICalibrationValue = 6;
+ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
+ HAL_RCC_OscConfig(&RCC_OscInitStruct);
+
+}
+
+/* USART2 init function */
+void MX_USART2_UART_Init(void)
+{
+ huart2.Instance = USART2;
+ huart2.Init.BaudRate = USART2_BAUD_RATE;
+ huart2.Init.WordLength = UART_WORDLENGTH_8B;
+ huart2.Init.StopBits = UART_STOPBITS_1;
+ huart2.Init.Parity = UART_PARITY_NONE;
+ huart2.Init.Mode = UART_MODE_TX_RX;
+ huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+ huart2.Init.OverSampling = UART_OVERSAMPLING_16;
+
+ if (HAL_UART_Init(&huart2) != HAL_OK) {
+ /* Initialization Error */
+ Error_Handler();
+ }
+}
+
+/** Configure pins as
+ * Analog
+ * Input
+ * Output
+ * EVENT_OUT
+ * EXTI
+*/
+void MX_GPIO_Init(void)
+{
+ /* GPIO Ports Clock Enable */
+ __GPIOJ_CLK_ENABLE();
+
+ /* Configure LED GPIO pins PJ1==red, PJ2==yellow, PJ3==green, PJ4==blue */
+ GPIO_InitStruct.Pin = LED_RED | LED_YELLOW | LED_GREEN | LED_BLUE;
+ GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
+ HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
+}
+
+/**
+ * @brief This function is executed in case of error occurrence.
+ * @param None
+ * @retval None
+ */
+void Error_Handler(void)
+{
+ HAL_GPIO_WritePin(LED_PORT, LED_RED, GPIO_PIN_SET);
+ while (1) { ; }
+}
+
+#ifdef USE_FULL_ASSERT
+
+/**
+ * @brief Reports the name of the source file and the source line number
+ * where the assert_param error has occurred.
+ * @param file: pointer to the source file name
+ * @param line: assert_param error line source number
+ * @retval None
+ */
+void assert_failed(uint8_t* file, uint32_t line)
+{
+ /* USER CODE BEGIN 6 */
+ /* User can add his own implementation to report the file name and line number,
+ ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
+ /* USER CODE END 6 */
+
+}
+
+#endif
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+*/
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/