![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
UART
Dependencies: BSP_DISCO_L476VG LCD_DISCO_L476VG mbed
Revision 0:7b78e040f288, committed 2016-10-05
- Comitter:
- codebreaker7
- Date:
- Wed Oct 05 10:24:06 2016 +0000
- Commit message:
- UART
Changed in this revision
diff -r 000000000000 -r 7b78e040f288 BSP_DISCO_L476VG.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BSP_DISCO_L476VG.lib Wed Oct 05 10:24:06 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/BSP_DISCO_L476VG/#792b4cbebcb6
diff -r 000000000000 -r 7b78e040f288 LCD_DISCO_L476VG.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_DISCO_L476VG.lib Wed Oct 05 10:24:06 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/LCD_DISCO_L476VG/#6ac2ed34f595
diff -r 000000000000 -r 7b78e040f288 main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.c Wed Oct 05 10:24:06 2016 +0000 @@ -0,0 +1,131 @@ +/** + ****************************************************************************** + * @file main.c + * @author Ac6 + * @version V1.0 + * @date 01-December-2013 + * @brief Default main function. + ****************************************************************************** +*/ + + +#include "stm32l4xx.h" +#include "stm32l476g_discovery.h" +#include "main.h" + +void Error_Handler(void); +void SystemClock_Config(void); + +UART_HandleTypeDef UartHandle; +uint8_t buf[2]; + +int main(void) +{ + uint8_t sbuf[] = "abcd"; + + HAL_Init(); + SystemClock_Config(); + //BSP_LED_Init(LED4); + //BSP_LED_Init(LED5); + + //UartHandle.Instance = USART1; + UartHandle.Instance = USARTx; + + UartHandle.Init.BaudRate = 9600; + UartHandle.Init.WordLength = UART_WORDLENGTH_8B; + UartHandle.Init.StopBits = UART_STOPBITS_1; + UartHandle.Init.Parity = UART_PARITY_NONE; + UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; + UartHandle.Init.Mode = UART_MODE_TX_RX; + + if (HAL_UART_Init(&UartHandle) != HAL_OK) + { + /* Initialization Error */ + Error_Handler(); + } + + //if (HAL_UART_DeInit(&usart_init) != HAL_OK) { + // Error_Handler(); + //} + //if (HAL_UART_Init(&usart_init) != HAL_OK) { + // Error_Handler(); + //} + + //if (HAL_UART_Transmit(&UartHandle, (uint8_t*)sbuf, 4, 5000) != HAL_OK) { + // Error_Handler(); + //}z + //if (HAL_UART_Receive(&UartHandle, (uint8_t*)buf, 1, 0x1fffffff) != HAL_OK) { + // Error_Handler(); + //} + if (HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)sbuf, 4) != HAL_OK) { + Error_Handler(); + } + if (HAL_UART_Receive_IT(&UartHandle, (uint8_t*)buf, 2) != HAL_OK) { + Error_Handler(); + } + + while(1); +} + +void SystemClock_Config(void) { + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + + /* MSI is enabled after System reset, activate PLL with MSI as source */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; + RCC_OscInitStruct.MSIState = RCC_MSI_ON; + RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; + RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; + RCC_OscInitStruct.PLL.PLLM = 1; + RCC_OscInitStruct.PLL.PLLN = 40; + RCC_OscInitStruct.PLL.PLLR = 2; + RCC_OscInitStruct.PLL.PLLP = 7; + RCC_OscInitStruct.PLL.PLLQ = 4; + if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) + { + /* Initialization Error */ + while(1); + } + + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 + clocks dividers */ + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) + { + /* Initialization Error */ + while(1); + } +} + +void Error_Handler(void) +{ + /* Turn LED1 on */ + //BSP_LED_On(LED4); + while(1) + { + /* Error if LED1 is slowly blinking (1 sec. period) */ + //BSP_LED_Toggle(LED4); + HAL_Delay(1000); + } +} + +void HAL_UART_RxCpltCallback(UART_HandleTypeDef * huart) { + //BSP_LED_On(LED4); +} + +void HAL_UART_TxCpltCallback(UART_HandleTypeDef * huart) { + //BSP_LED_On(LED5); +} + +void HAL_UART_ErrorCallback(UART_HandleTypeDef * huart) { + while(1) { + + } +} +
diff -r 000000000000 -r 7b78e040f288 main.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Wed Oct 05 10:24:06 2016 +0000 @@ -0,0 +1,74 @@ +/** + ****************************************************************************** + * @file UART/UART_Printf/Inc/main.h + * @author MCD Application Team + * @version V1.5.0 + * @date 29-April-2016 + * @brief Header for main.c module + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> + * + * 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. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __MAIN_H +#define __MAIN_H + +/* Includes ------------------------------------------------------------------*/ +#include "stm32l4xx_hal.h" +#include "stm32l476g_discovery.h" +#include "stdio.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* User can use this section to tailor USARTx/UARTx instance used and associated + resources */ +/* Definition for USARTx clock resources */ +#define USARTx USART1 +#define USARTx_CLK_ENABLE() __HAL_RCC_USART1_CLK_ENABLE(); +#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() +#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() + +#define USARTx_FORCE_RESET() __HAL_RCC_USART1_FORCE_RESET() +#define USARTx_RELEASE_RESET() __HAL_RCC_USART1_RELEASE_RESET() + +/* Definition for USARTx Pins */ +#define USARTx_TX_PIN GPIO_PIN_6 +#define USARTx_TX_GPIO_PORT GPIOB +#define USARTx_TX_AF GPIO_AF7_USART1 +#define USARTx_RX_PIN GPIO_PIN_7 +#define USARTx_RX_GPIO_PORT GPIOB +#define USARTx_RX_AF GPIO_AF7_USART1 + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +#endif /* __MAIN_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +
diff -r 000000000000 -r 7b78e040f288 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Oct 05 10:24:06 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/25aea2a3f4e3 \ No newline at end of file
diff -r 000000000000 -r 7b78e040f288 stm32l4xx_hal_msp.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stm32l4xx_hal_msp.c Wed Oct 05 10:24:06 2016 +0000 @@ -0,0 +1,198 @@ +/** + ****************************************************************************** + * @file stm32l4xx_hal_msp_template.c + * @author MCD Application Team + * @version V1.0.0 + * @date 26-June-2015 + * @brief HAL MSP module. + * This file template is located in the HAL folder and should be copied + * to the user folder. + * + @verbatim + =============================================================================== + ##### How to use this driver ##### + =============================================================================== + [..] + This file is generated automatically by MicroXplorer and eventually modified + by the user + + @endverbatim + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * 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 "stm32l4xx_hal.h" +#include "main.h" + +/** @addtogroup STM32L4xx_HAL_Driver + * @{ + */ + +/** @defgroup HAL_MSP HAL MSP module driver + * @brief HAL MSP module. + * @{ + */ + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/** @defgroup HAL_MSP_Private_Functions + * @{ + */ + +/** + * @brief Initialize the Global MSP. + * @param None + * @retval None + */ +//void HAL_MspInit(void) +//{ + /* NOTE : This function is generated automatically by STM32CubeMX and eventually + modified by the user + */ +//} + +/** + * @brief DeInitialize the Global MSP. + * @param None + * @retval None + */ +//void HAL_MspDeInit(void) +//{ + /* NOTE : This function is generated automatically by STM32CubeMX and eventually + modified by the user + */ +//} + +/** + * @brief Initialize the PPP MSP. + * @param None + * @retval None + */ +void HAL_UART_MspInit(UART_HandleTypeDef * usart) +{ + static DMA_HandleTypeDef hdma_tx; + static DMA_HandleTypeDef hdma_rx; + + GPIO_InitTypeDef GPIO_InitStruct; + + /*##-1- Enable peripherals and GPIO Clocks #################################*/ + /* Enable GPIO TX/RX clock */ + USARTx_TX_GPIO_CLK_ENABLE(); + USARTx_RX_GPIO_CLK_ENABLE(); + + + /* Enable USARTx clock */ + USARTx_CLK_ENABLE(); + + __HAL_RCC_DMA1_CLK_ENABLE(); + + /*##-2- Configure peripheral GPIO ##########################################*/ + /* UART TX GPIO pin configuration */ + GPIO_InitStruct.Pin = USARTx_TX_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Alternate = USARTx_TX_AF; + + HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct); + + /* UART RX GPIO pin configuration */ + GPIO_InitStruct.Pin = USARTx_RX_PIN; + GPIO_InitStruct.Alternate = USARTx_RX_AF; + + HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct); + + hdma_tx.Instance = DMA1_Channel4; + hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_tx.Init.MemInc = DMA_MINC_ENABLE; + hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_tx.Init.Mode = DMA_NORMAL; + hdma_tx.Init.Priority = DMA_PRIORITY_LOW; + hdma_tx.Init.Request = DMA_REQUEST_2; + + HAL_DMA_Init(&hdma_tx); + + hdma_rx.Instance = DMA1_Channel5; + hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_rx.Init.MemInc = DMA_MINC_ENABLE; + hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_rx.Init.Mode = DMA_NORMAL; + hdma_rx.Init.Priority = DMA_PRIORITY_LOW; + hdma_rx.Init.Request = DMA_REQUEST_2; + + HAL_DMA_Init(&hdma_rx); + + __HAL_LINKDMA(usart, hdmatx, hdma_tx); + //__HAL_LINKDMA(usart, hdmarx, hdma_rx); + + HAL_NVIC_SetPriority(USART1_IRQn, 1, 1); + HAL_NVIC_EnableIRQ(USART1_IRQn); + + HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 2, 2); + HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn); + + //HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 1, 1); + //HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn); +} + +/** + * @brief DeInitialize the PPP MSP. + * @param None + * @retval None + */ +void HAL_UART_MspDeInit(UART_HandleTypeDef *husart) +{ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2); + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_3); +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +
diff -r 000000000000 -r 7b78e040f288 stm32l4xx_it.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stm32l4xx_it.c Wed Oct 05 10:24:06 2016 +0000 @@ -0,0 +1,51 @@ +/** + ****************************************************************************** + * @file stm32l4xx_it.c + * @author Ac6 + * @version V1.0 + * @date 02-Feb-2015 + * @brief Default Interrupt Service Routines. + ****************************************************************************** +*/ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32l4xx_hal.h" +#include "stm32l4xx.h" +#include "stm32l4xx_it.h" + +extern UART_HandleTypeDef UartHandle; + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************/ +/* Processor Exceptions Handlers */ +/******************************************************************************/ + +/** + * @brief This function handles SysTick Handler. + * @param None + * @retval None + */ +void SysTick_Handler(void) +{ + HAL_IncTick(); + HAL_SYSTICK_IRQHandler(); +} + +void USART1_IRQHandler(void) { + HAL_UART_IRQHandler(&UartHandle); +} + +void DMA1_Channel4_IRQHandler(void) { + HAL_DMA_IRQHandler(UartHandle.hdmatx); +} + +//void DMA1_Channel5_IRQHandler(void) { +// HAL_DMA_IRQHandler(UartHandle.hdmarx); +//} +
diff -r 000000000000 -r 7b78e040f288 stm32l4xx_it.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stm32l4xx_it.h Wed Oct 05 10:24:06 2016 +0000 @@ -0,0 +1,67 @@ +/** + ****************************************************************************** + * @file Templates/Inc/stm32l4xx_it.h + * @author MCD Application Team + * @version V1.0.0 + * @date 26-June-2015 + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * 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. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32L4xx_IT_H +#define __STM32L4xx_IT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +void NMI_Handler(void); +void HardFault_Handler(void); +void SVC_Handler(void); +void DebugMon_Handler(void); +void PendSV_Handler(void); +void SysTick_Handler(void); +void USART1_IRQHandler(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32L4xx_IT_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +