Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 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. #ifdef __cplusplus
  49. }
  50. #endif
  51.  
  52.  
  53.  
  54.  
  55. int main(void)
  56. {
  57. /*-------------------------------------------MOJE-----------------------------*/
  58. //RCC
  59. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //wlaczenietaktowaniawybranegoportu
  60. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//wlaczenietaktowaniawybranegoukaduUSART
  61.  
  62. //Tx
  63. GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3);
  64. GPIO_InitTypeDef GPIO_InitStructure;
  65. GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
  66. GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
  67. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
  68. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
  69. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  70. GPIO_Init(GPIOC,&GPIO_InitStructure);
  71.  
  72. //Rx
  73. GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3);
  74. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
  75. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
  76. GPIO_Init(GPIOC,&GPIO_InitStructure);
  77.  
  78. //USART
  79. USART_InitTypeDef USART_InitStructure;
  80. USART_InitStructure.USART_BaudRate=115200; // predkosc transmisji (mozliwe standardowe opcje: 9600, 19200, 38400, 57600,115200,...)
  81. USART_InitStructure.USART_WordLength=USART_WordLength_8b; //dlugosc slowa(USART_WordLength_8blubUSART_WordLength_9b)
  82. USART_InitStructure.USART_StopBits=USART_StopBits_1; // (USART_StopBits_2,USART_StopBits_1_5)
  83. USART_InitStructure.USART_Parity=USART_Parity_No; // sprawdzanie parzystoci (USART_Parity_No, USART_Parity_Even,USART_Parity_Odd)
  84. USART_InitStructure.USART_HardwareFlowControl; // sprztowa kontrola przepywu (USART_HardwareFlowControl_None, USART_HardwareFlowControl_RTS, USART_HardwareFlowControl_CTS, USART_HardwareFlowControl_RTS_CTS)
  85. USART_HardwareFlowControl_None;
  86. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //trybnadawania/odbierania(USART_Mode_Rx,USART_Mode_Rx)
  87. USART_Init(USART3,&USART_InitStructure);
  88. USART_Cmd(USART3,ENABLE);
  89.  
  90. //NVIC
  91. NVIC_InitTypeDef NVIC_InitStructure; //strukturadokonfiguracjikontroleraNVIC
  92. USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
  93. NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;
  94. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  95. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
  96. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  97. NVIC_Init(&NVIC_InitStructure); //konfiguracjakontroleraprzerwan
  98. NVIC_EnableIRQ(USART3_IRQn); //wlaczenieprzerwanodukladuUSART
  99.  
  100.  
  101. /*---------------------------------------------------------------------------*/
  102.  
  103. /* Set up the system clocks */
  104. SystemInit();
  105.  
  106. /* Initialize USB, IO, SysTick, and all those other things you do in the morning */
  107. init();
  108.  
  109.  
  110. while (1)
  111. {
  112. /* Blink the orange LED at 1Hz */
  113. if (500 == ticker)
  114. {
  115. GPIOD->BSRRH = GPIO_Pin_13;
  116. }
  117. else if (1000 == ticker)
  118. {
  119. ticker = 0;
  120. GPIOD->BSRRL = GPIO_Pin_13;
  121. }
  122.  
  123.  
  124. /* If there's data on the virtual serial port:
  125. * - Echo it back
  126. * - Turn the green LED on for 10ms
  127. */
  128. uint8_t theByte;
  129. if (VCP_get_char(&theByte))
  130. {
  131.  
  132.  
  133. //czekajnaoprĂłnieniebuforawyjciowego
  134. while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
  135.  
  136. USART_SendData(USART3, theByte);
  137.  
  138. while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET);
  139.  
  140.  
  141. GPIOD->BSRRL = GPIO_Pin_12;
  142. downTicker = 10;
  143. }
  144. if (0 == downTicker)
  145. {
  146. GPIOD->BSRRH = GPIO_Pin_12;
  147. }
  148. }
  149.  
  150.  
  151. return 0;
  152. }
  153.  
  154.  
  155. void init()
  156. {
  157. /* STM32F4 discovery LEDs */
  158. GPIO_InitTypeDef LED_Config;
  159.  
  160. /* Always remember to turn on the peripheral clock... If not, you may be up till 3am debugging... */
  161. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  162. LED_Config.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  163. LED_Config.GPIO_Mode = GPIO_Mode_OUT;
  164. LED_Config.GPIO_OType = GPIO_OType_PP;
  165. LED_Config.GPIO_Speed = GPIO_Speed_25MHz;
  166. LED_Config.GPIO_PuPd = GPIO_PuPd_NOPULL;
  167. GPIO_Init(GPIOD, &LED_Config);
  168.  
  169.  
  170.  
  171. /* Setup SysTick or CROD! */
  172. if (SysTick_Config(SystemCoreClock / 1000))
  173. {
  174. ColorfulRingOfDeath();
  175. }
  176.  
  177.  
  178. /* Setup USB */
  179. USBD_Init(&USB_OTG_dev,
  180. USB_OTG_FS_CORE_ID,
  181. &USR_desc,
  182. &USBD_CDC_cb,
  183. &USR_cb);
  184.  
  185. return;
  186. }
  187.  
  188. /*
  189. * Call this to indicate a failure. Blinks the STM32F4 discovery LEDs
  190. * in sequence. At 168Mhz, the blinking will be very fast - about 5 Hz.
  191. * Keep that in mind when debugging, knowing the clock speed might help
  192. * with debugging.
  193. */
  194. void ColorfulRingOfDeath(void)
  195. {
  196. uint16_t ring = 1;
  197. while (1)
  198. {
  199. uint32_t count = 0;
  200. while (count++ < 500000);
  201.  
  202. GPIOD->BSRRH = (ring << 12);
  203. ring = ring << 1;
  204. if (ring >= 1<<4)
  205. {
  206. ring = 1;
  207. }
  208. GPIOD->BSRRL = (ring << 12);
  209. }
  210. }
  211.  
  212. /*
  213. * Interrupt Handlers
  214. */
  215.  
  216. void SysTick_Handler(void)
  217. {
  218. ticker++;
  219. if (downTicker > 0)
  220. {
  221. downTicker--;
  222. }
  223. }
  224.  
  225. void NMI_Handler(void) {}
  226. void HardFault_Handler(void) { ColorfulRingOfDeath(); }
  227. void MemManage_Handler(void) { ColorfulRingOfDeath(); }
  228. void BusFault_Handler(void) { ColorfulRingOfDeath(); }
  229. void UsageFault_Handler(void){ ColorfulRingOfDeath(); }
  230. void SVC_Handler(void) {}
  231. void DebugMon_Handler(void) {}
  232. void PendSV_Handler(void) {}
  233.  
  234. void OTG_FS_IRQHandler(void)
  235. {
  236. USBD_OTG_ISR_Handler (&USB_OTG_dev);
  237. }
  238.  
  239. void OTG_FS_WKUP_IRQHandler(void)
  240. {
  241. if(USB_OTG_dev.cfg.low_power)
  242. {
  243. *(uint32_t *)(0xE000ED10) &= 0xFFFFFFF9 ;
  244. SystemInit();
  245. USB_OTG_UngateClock(&USB_OTG_dev);
  246. }
  247. EXTI_ClearITPendingBit(EXTI_Line18);
  248. }
  249.  
  250. uint8_t usartGetChar(void)
  251. {
  252. //czekajnaodebraniedanych
  253. while(USART_GetFlagStatus(USART3,USART_FLAG_RXNE)==RESET);
  254. return USART_ReceiveData(USART3);
  255. }
  256.  
  257. void USART3_IRQHandler(void)
  258. {
  259. //sprawdzenieflagizwiazanejzodebraniemdanychprzezUSART
  260. if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET)
  261. {
  262. usartGetChar();
  263. VCP_put_char(USART3->DR);
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement