Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define HSE_VALUE ((uint32_t)8000000) /* STM32 discovery uses a 8Mhz external crystal */
- #include "stm32f4xx_conf.h"
- #include "stm32f4xx.h"
- #include "stm32f4xx_gpio.h"
- #include "stm32f4xx_rcc.h"
- #include "stm32f4xx_exti.h"
- #include "stm32f4xx_usart.h"
- #include "usbd_cdc_core.h"
- #include "usbd_usr.h"
- #include "usbd_desc.h"
- #include "usbd_cdc_vcp.h"
- #include "usb_dcd_int.h"
- #include "misc.h"
- volatile uint32_t ticker, downTicker;
- /*
- * The USB data must be 4 byte aligned if DMA is enabled. This macro handles
- * the alignment, if necessary (it's actually magic, but don't tell anyone).
- */
- __ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END;
- void init();
- void ColorfulRingOfDeath(void);
- /*
- * Define prototypes for interrupt handlers here. The conditional "extern"
- * ensures the weak declarations from startup_stm32f4xx.c are overridden.
- */
- #ifdef __cplusplus
- extern "C" {
- #endif
- void SysTick_Handler(void);
- void NMI_Handler(void);
- void HardFault_Handler(void);
- void MemManage_Handler(void);
- void BusFault_Handler(void);
- void UsageFault_Handler(void);
- void SVC_Handler(void);
- void DebugMon_Handler(void);
- void PendSV_Handler(void);
- void OTG_FS_IRQHandler(void);
- void OTG_FS_WKUP_IRQHandler(void);
- int main(void)
- {
- SystemInit(); //Set up the system clocks
- init(); //Initialize USB, IO, SysTick
- //RCC
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable GPIO clock
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //Enable UART clock
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- //Tx
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
- GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- //Rx
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- //USART2
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_Init(USART2, &USART_InitStructure);
- USART_Cmd(USART2, ENABLE); // Enable USART
- //NVIC
- NVIC_InitTypeDef NVIC_InitStructure;
- USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_EnableIRQ(USART2_IRQn);
- while (1)
- {
- if (500 == ticker)
- {
- GPIOD->BSRRH = GPIO_Pin_13;
- }
- else if (1000 == ticker)
- {
- ticker = 0;
- GPIOD->BSRRL = GPIO_Pin_13;
- }
- uint8_t theByte;
- if (VCP_get_char(&theByte))
- {
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
- USART_SendData(USART2, theByte);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
- GPIOD->BSRRL = GPIO_Pin_12;
- downTicker = 10;
- }
- if (0 == downTicker)
- {
- GPIOD->BSRRH = GPIO_Pin_12;
- }
- }
- return 0;
- }
- void init()
- {
- GPIO_InitTypeDef LED_Config;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- LED_Config.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
- LED_Config.GPIO_Mode = GPIO_Mode_OUT;
- LED_Config.GPIO_OType = GPIO_OType_PP;
- LED_Config.GPIO_Speed = GPIO_Speed_25MHz;
- LED_Config.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOD, &LED_Config);
- if (SysTick_Config(SystemCoreClock / 1000))
- {
- ColorfulRingOfDeath();
- }
- USBD_Init(&USB_OTG_dev,
- USB_OTG_FS_CORE_ID,
- &USR_desc,
- &USBD_CDC_cb,
- &USR_cb);
- return;
- }
- void ColorfulRingOfDeath(void)
- {
- uint16_t ring = 1;
- while (1)
- {
- uint32_t count = 0;
- while (count++ < 500000);
- GPIOD->BSRRH = (ring << 12);
- ring = ring << 1;
- if (ring >= 1<<4)
- {
- ring = 1;
- }
- GPIOD->BSRRL = (ring << 12);
- }
- }
- void SysTick_Handler(void)
- {
- ticker++;
- if (downTicker > 0)
- {
- downTicker--;
- }
- }
- void NMI_Handler(void) {}
- void HardFault_Handler(void) { ColorfulRingOfDeath(); }
- void MemManage_Handler(void) { ColorfulRingOfDeath(); }
- void BusFault_Handler(void) { ColorfulRingOfDeath(); }
- void UsageFault_Handler(void){ ColorfulRingOfDeath(); }
- void SVC_Handler(void) {}
- void DebugMon_Handler(void) {}
- void PendSV_Handler(void) {}
- void OTG_FS_IRQHandler(void)
- {
- USBD_OTG_ISR_Handler (&USB_OTG_dev);
- }
- void OTG_FS_WKUP_IRQHandler(void)
- {
- if(USB_OTG_dev.cfg.low_power)
- {
- *(uint32_t *)(0xE000ED10) &= 0xFFFFFFF9 ;
- SystemInit();
- USB_OTG_UngateClock(&USB_OTG_dev);
- }
- EXTI_ClearITPendingBit(EXTI_Line18);
- }
- uint8_t usartGetChar(void)
- {
- while(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)==RESET);
- return USART_ReceiveData(USART2);
- }
- void USART2_IRQHandler(void)
- {
- if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
- {
- usartGetChar();
- VCP_put_char(USART2->DR);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement