Advertisement
Guest User

Untitled

a guest
May 5th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.68 KB | None | 0 0
  1. #define HSE_VALUE ((uint32_t)8000000) /* STM32 discovery uses a 8Mhz external crystal */
  2.  
  3. #include "stm32f4xx_conf.h"
  4. #include "stm32f4xx.h"
  5. #include "stm32f4xx_gpio.h"
  6. #include "stm32f4xx_rcc.h"
  7. #include "stm32f4xx_exti.h"
  8. #include "stm32f4xx_usart.h"
  9. #include "usbd_cdc_core.h"
  10. #include "usbd_usr.h"
  11. #include "usbd_desc.h"
  12. #include "usbd_cdc_vcp.h"
  13. #include "usb_dcd_int.h"
  14. #include "misc.h"
  15.  
  16. volatile uint32_t ticker, downTicker;
  17.  
  18. /*
  19.  * The USB data must be 4 byte aligned if DMA is enabled. This macro handles
  20.  * the alignment, if necessary (it's actually magic, but don't tell anyone).
  21.  */
  22. __ALIGN_BEGIN USB_OTG_CORE_HANDLE  USB_OTG_dev __ALIGN_END;
  23.  
  24.  
  25. void init();
  26. void ColorfulRingOfDeath(void);
  27.  
  28. /*
  29.  * Define prototypes for interrupt handlers here. The conditional "extern"
  30.  * ensures the weak declarations from startup_stm32f4xx.c are overridden.
  31.  */
  32. #ifdef __cplusplus
  33.  extern "C" {
  34. #endif
  35.  
  36. void SysTick_Handler(void);
  37. void NMI_Handler(void);
  38. void HardFault_Handler(void);
  39. void MemManage_Handler(void);
  40. void BusFault_Handler(void);
  41. void UsageFault_Handler(void);
  42. void SVC_Handler(void);
  43. void DebugMon_Handler(void);
  44. void PendSV_Handler(void);
  45. void OTG_FS_IRQHandler(void);
  46. void OTG_FS_WKUP_IRQHandler(void);
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. int main(void)
  54. {
  55.     SystemInit(); //Set up the system clocks
  56.     init(); //Initialize USB, IO, SysTick
  57.  
  58.     //RCC
  59.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable GPIO clock
  60.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //Enable UART clock
  61.  
  62.  
  63.  
  64.     GPIO_InitTypeDef GPIO_InitStructure;
  65.     USART_InitTypeDef USART_InitStructure;
  66.  
  67.     //Tx
  68.     GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
  69.     GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
  70.     GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
  71.     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
  72.     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
  73.     GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  74.     GPIO_Init(GPIOA,&GPIO_InitStructure);
  75.  
  76.     //Rx
  77.     GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
  78.     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
  79.     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
  80.     GPIO_Init(GPIOA,&GPIO_InitStructure);
  81.  
  82.     //USART2
  83.     USART_InitStructure.USART_BaudRate = 9600;
  84.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  85.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  86.     USART_InitStructure.USART_Parity = USART_Parity_No;
  87.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  88.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  89.     USART_Init(USART2, &USART_InitStructure);
  90.     USART_Cmd(USART2, ENABLE); // Enable USART
  91.  
  92.     //NVIC
  93.     NVIC_InitTypeDef NVIC_InitStructure;
  94.     USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
  95.     NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
  96.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  97.     NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
  98.     NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  99.     NVIC_Init(&NVIC_InitStructure);
  100.     NVIC_EnableIRQ(USART2_IRQn);
  101.  
  102.     while (1)
  103.         {
  104.             if (500 == ticker)
  105.             {
  106.                 GPIOD->BSRRH = GPIO_Pin_13;
  107.             }
  108.             else if (1000 == ticker)
  109.             {
  110.                 ticker = 0;
  111.                 GPIOD->BSRRL = GPIO_Pin_13;
  112.             }
  113.  
  114.             uint8_t theByte;
  115.             if (VCP_get_char(&theByte))
  116.             {
  117.                 while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
  118.  
  119.                 USART_SendData(USART2, theByte);
  120.  
  121.                 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
  122.  
  123.                 GPIOD->BSRRL = GPIO_Pin_12;
  124.                 downTicker = 10;
  125.             }
  126.             if (0 == downTicker)
  127.             {
  128.                 GPIOD->BSRRH = GPIO_Pin_12;
  129.             }
  130.         }
  131.     return 0;
  132. }
  133.  
  134.  
  135. void init()
  136. {
  137.  
  138.     GPIO_InitTypeDef LED_Config;
  139.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  140.     LED_Config.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  141.     LED_Config.GPIO_Mode = GPIO_Mode_OUT;
  142.     LED_Config.GPIO_OType = GPIO_OType_PP;
  143.     LED_Config.GPIO_Speed = GPIO_Speed_25MHz;
  144.     LED_Config.GPIO_PuPd = GPIO_PuPd_NOPULL;
  145.     GPIO_Init(GPIOD, &LED_Config);
  146.  
  147.     if (SysTick_Config(SystemCoreClock / 1000))
  148.     {
  149.         ColorfulRingOfDeath();
  150.     }
  151.  
  152.     USBD_Init(&USB_OTG_dev,
  153.                 USB_OTG_FS_CORE_ID,
  154.                 &USR_desc,
  155.                 &USBD_CDC_cb,
  156.                 &USR_cb);
  157.     return;
  158. }
  159.  
  160. void ColorfulRingOfDeath(void)
  161. {
  162.     uint16_t ring = 1;
  163.     while (1)
  164.     {
  165.         uint32_t count = 0;
  166.         while (count++ < 500000);
  167.  
  168.         GPIOD->BSRRH = (ring << 12);
  169.         ring = ring << 1;
  170.         if (ring >= 1<<4)
  171.         {
  172.             ring = 1;
  173.         }
  174.         GPIOD->BSRRL = (ring << 12);
  175.     }
  176. }
  177.  
  178. void SysTick_Handler(void)
  179. {
  180.     ticker++;
  181.     if (downTicker > 0)
  182.     {
  183.         downTicker--;
  184.     }
  185. }
  186.  
  187. void NMI_Handler(void)       {}
  188. void HardFault_Handler(void) { ColorfulRingOfDeath(); }
  189. void MemManage_Handler(void) { ColorfulRingOfDeath(); }
  190. void BusFault_Handler(void)  { ColorfulRingOfDeath(); }
  191. void UsageFault_Handler(void){ ColorfulRingOfDeath(); }
  192. void SVC_Handler(void)       {}
  193. void DebugMon_Handler(void)  {}
  194. void PendSV_Handler(void)    {}
  195.  
  196. void OTG_FS_IRQHandler(void)
  197. {
  198.   USBD_OTG_ISR_Handler (&USB_OTG_dev);
  199. }
  200.  
  201. void OTG_FS_WKUP_IRQHandler(void)
  202. {
  203.   if(USB_OTG_dev.cfg.low_power)
  204.   {
  205.     *(uint32_t *)(0xE000ED10) &= 0xFFFFFFF9 ;
  206.     SystemInit();
  207.     USB_OTG_UngateClock(&USB_OTG_dev);
  208.   }
  209.   EXTI_ClearITPendingBit(EXTI_Line18);
  210. }
  211.  
  212. uint8_t usartGetChar(void)
  213. {
  214. while(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)==RESET);
  215. return USART_ReceiveData(USART2);
  216. }
  217.  
  218. void USART2_IRQHandler(void)
  219. {
  220.     if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
  221.     {
  222.         usartGetChar();
  223.         VCP_put_char(USART2->DR);
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement