Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file GPIO/GPIO_IOToggle/main.c
- * @author MCD Application Team
- * @version V1.4.0
- * @date 04-August-2014
- * @brief Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
- *
- * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.st.com/software_license_agreement_liberty_v2
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f4xx.h"
- #include "string.h"
- #include "stdio.h"
- /* Private typedef -----------------------------------------------------------*/
- GPIO_InitTypeDef GPIOHandle;
- USART_InitTypeDef USARTHandle;
- NVIC_InitTypeDef NVICHandle;
- EXTI_InitTypeDef EXTIHandle;
- /* Private define ------------------------------------------------------------*/
- //static void GPIO_Config(void);
- static void USART2_Config(void);
- static void EXTI9_5_Config(void);
- static void TM_Delay_Init(void);
- static void TM_DelayMicros(uint32_t micros);
- static void TM_DelayMillis(uint32_t millis);
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- uint32_t multiplier;
- int pulsesv;
- /* We need to implement own __FILE struct */
- /* FILE struct is used from __FILE */
- struct __FILE {
- int dummy;
- };
- /* You need this if you want use printf */
- /* Struct FILE is implemented in stdio.h */
- FILE __stdout;
- int fputc(int ch, FILE *f) {
- /* Do your stuff here */
- /* Send your custom byte */
- /* Send byte to USART */
- USART_SendData(USART2, (uint8_t) ch);
- /* Loop until the end of transmission */
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
- {}
- /* If everything is OK, you have to return character written */
- return ch;
- /* If character is not correct, you can return EOF (-1) to stop writing */
- //return -1;
- }
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program
- * @param None
- * @retval None
- */
- int main(void)
- {
- USART2_Config();
- EXTI9_5_Config();
- TM_Delay_Init();
- printf("Value pulse:");
- printf("%d\n",pulsesv);
- TM_DelayMicros(100);
- while (1)
- {
- }
- }
- /**
- * @brief Congig USART2
- * @param None
- * @retval None
- */
- void USART2_Config()
- {
- /* ADDED BY Tilen start */
- GPIO_InitTypeDef GPIO_InitStruct;
- /* Enable clock for GPIOA */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- /* Set alternating function to USART 2, PA2: TX; PA3: RX */
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
- /* Set pin settings, alternate function */
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- /* Initialize pins for USART2 */
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* Enable clock for USART2 */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- /* ADDED BY Tilen end */
- /* USARTx configuration ------------------------------------------------------*/
- /* USARTx configured as follows:
- - BaudRate = 9600 baud
- - Word Length = 8 Bits
- - Two Stop Bit
- - Odd parity
- - Hardware flow control disabled (RTS and CTS signals)
- - Receive and transmit enabled
- */
- USARTHandle.USART_BaudRate = 115200;
- USARTHandle.USART_WordLength = USART_WordLength_8b;
- USARTHandle.USART_StopBits = USART_StopBits_2;
- USARTHandle.USART_Parity = USART_Parity_Odd;
- USARTHandle.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USARTHandle.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART2, &USARTHandle);
- USART_Cmd(USART2, ENABLE);
- }
- /**
- * @brief Congig EXTI9-15 PA8 and PA9
- * @param None
- * @retval None
- */
- void EXTI9_5_Config()
- {
- /* Enable GPIOA clock */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- /* Enable SYSCFG clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
- /* Configure PA8 and PA9 pin as input floating */
- GPIOHandle.GPIO_Mode = GPIO_Mode_IN;
- GPIOHandle.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIOHandle.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
- GPIO_Init(GPIOA, &GPIOHandle);
- /* Connect EXTI Line0 to PA8 and PA9 pin */
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource8);
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource9);
- /* Configure EXTI Line 8 and Line 9*/
- EXTIHandle.EXTI_Line = EXTI_Line8 | EXTI_Line9;
- EXTIHandle.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTIHandle.EXTI_Trigger = EXTI_Trigger_Rising;
- EXTIHandle.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTIHandle);
- /* Enable and set EXTI Line 8 & 9 Interrupt to the lowest priority */
- NVICHandle.NVIC_IRQChannel = EXTI9_5_IRQn;
- NVICHandle.NVIC_IRQChannelPreemptionPriority = 0;
- NVICHandle.NVIC_IRQChannelSubPriority = 0;
- NVICHandle.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVICHandle);
- }
- /**
- * @brief Congig delay ms and us
- * @param None
- * @retval None
- */
- void TM_Delay_Init(void) {
- RCC_ClocksTypeDef RCC_Clocks;
- /* Get system clocks */
- RCC_GetClocksFreq(&RCC_Clocks);
- /* While loop takes 4 cycles */
- /* For 1 us delay, we need to divide with 4M */
- multiplier = RCC_Clocks.HCLK_Frequency / 4000000;
- }
- void TM_DelayMicros(uint32_t micros) {
- /* Multiply micros with multipler */
- /* Substract 10 */
- micros = micros * multiplier - 10;
- /* 4 cycles for one loop */
- while (micros--);
- }
- void TM_DelayMillis(uint32_t millis) {
- /* Multiply millis with multipler */
- /* Substract 10 */
- millis = 1000 * millis * multiplier - 10;
- /* 4 cycles for one loop */
- while (millis--);
- }
- #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 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) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement