Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f0xx_hal.h"
  3.  
  4. /* Private variables ---------------------------------------------------------*/
  5. SPI_HandleTypeDef hspi1;
  6.  
  7. /* USER CODE BEGIN 0 */
  8.  
  9. /* USER CODE END 0 */
  10.  
  11. /* Private function prototypes -----------------------------------------------*/
  12. void SystemClock_Config(void);
  13. static void MX_GPIO_Init(void);
  14. static void MX_SPI1_Init(void);
  15.  
  16. int main(void)
  17. {
  18. uint8_t aTxBuffer[] = "Rohalamin";
  19.  
  20. /* USER CODE BEGIN 1 */
  21.  
  22. /* USER CODE END 1 */
  23.  
  24. /* MCU Configuration----------------------------------------------------------*/
  25.  
  26. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  27. HAL_Init();
  28.  
  29. /* Configure the system clock */
  30. SystemClock_Config();
  31.  
  32. /* System interrupt init*/
  33. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  34.  
  35. /* Initialize all configured peripherals */
  36. MX_GPIO_Init();
  37. MX_SPI1_Init();
  38.  
  39. /* USER CODE BEGIN 2 */
  40. HAL_GPIO_WritePin( GPIOA , GPIO_PIN_4 , GPIO_PIN_SET );
  41. /* USER CODE END 2 */
  42.  
  43. /* USER CODE BEGIN 3 */
  44. /* Infinite loop */
  45. while (1)
  46. {
  47. HAL_GPIO_WritePin( GPIOA , GPIO_PIN_4 , GPIO_PIN_RESET );
  48. HAL_SPI_Transmit_IT( &hspi1 , (uint8_t*)aTxBuffer , 10 );
  49. HAL_GPIO_WritePin( GPIOA , GPIO_PIN_4 , GPIO_PIN_SET );
  50.  
  51. }
  52. /* USER CODE END 3 */
  53.  
  54. }
  55.  
  56. /** System Clock Configuration
  57. */
  58. void SystemClock_Config(void)
  59. {
  60.  
  61. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  62. RCC_OscInitTypeDef RCC_OscInitStruct;
  63.  
  64. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  65. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  66. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  67. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  68. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  69. RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  70. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  71.  
  72. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  73. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  74. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  75. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  76. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
  77.  
  78. __SYSCFG_CLK_ENABLE();
  79.  
  80. }
  81.  
  82. /* SPI1 init function */
  83. void MX_SPI1_Init(void)
  84. {
  85.  
  86. HAL_SPI_MspInit(&hspi1);
  87.  
  88. hspi1.Instance = SPI1;
  89. hspi1.Init.Mode = SPI_MODE_MASTER;
  90. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  91. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  92. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  93. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  94. hspi1.Init.NSS = SPI_NSS_SOFT;
  95. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  96. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  97. hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  98. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  99. HAL_SPI_Init(&hspi1);
  100.  
  101. }
  102.  
  103. /** Configure pins as
  104. * Analog
  105. * Input
  106. * Output
  107. * EVENT_OUT
  108. * EXTI
  109. */
  110. void MX_GPIO_Init(void)
  111. {
  112.  
  113. GPIO_InitTypeDef GPIO_InitStruct;
  114.  
  115. /* GPIO Ports Clock Enable */
  116. __GPIOF_CLK_ENABLE();
  117. __GPIOA_CLK_ENABLE();
  118.  
  119. /*Configure GPIO pin : PA0 */
  120. GPIO_InitStruct.Pin = GPIO_PIN_0;
  121. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  122. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  123. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  124.  
  125. /*Configure GPIO pin : PA4 */
  126. GPIO_InitStruct.Pin = GPIO_PIN_4;
  127. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  128. GPIO_InitStruct.Pull = GPIO_NOPULL;
  129. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  130. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  131.  
  132. /* EXTI interrupt init*/
  133. HAL_NVIC_SetPriority(EXTI0_1_IRQn, 0, 0);
  134. HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
  135.  
  136. }
  137.  
  138. /* Includes ------------------------------------------------------------------*/
  139. #include "stm32f0xx_hal.h"
  140. #include "stm32f0xx.h"
  141. #include "stm32f0xx_it.h"
  142.  
  143. /* External variables --------------------------------------------------------*/
  144.  
  145. extern SPI_HandleTypeDef hspi1;
  146.  
  147. uint8_t aTxBuffer[] = "R";
  148.  
  149. /******************************************************************************/
  150. /* Cortex-M4 Processor Interruption and Exception Handlers */
  151. /******************************************************************************/
  152.  
  153. /**
  154. * @brief This function handles System tick timer.
  155. */
  156. void SysTick_Handler(void)
  157. {
  158. HAL_IncTick();
  159. HAL_SYSTICK_IRQHandler();
  160. }
  161.  
  162. /**
  163. * @brief This function handles SPI1 global interrupt.
  164. */
  165. void SPI1_IRQHandler(void)
  166. {
  167. HAL_NVIC_ClearPendingIRQ(SPI1_IRQn);
  168. HAL_SPI_IRQHandler(&hspi1);
  169. }
  170.  
  171. /**
  172. * @brief This function handles EXTI Line 0 and Line 1 interrupts.
  173. */
  174. void EXTI0_1_IRQHandler(void)
  175. {
  176. HAL_NVIC_ClearPendingIRQ(EXTI0_1_IRQn);
  177. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  178. HAL_SPI_Transmit_IT( &hspi1 , (uint8_t*)aTxBuffer , 2 );
  179. }
  180.  
  181. /* Includes ------------------------------------------------------------------*/
  182. #include "stm32f0xx_hal.h"
  183.  
  184. /* USER CODE BEGIN 0 */
  185.  
  186. /* USER CODE END 0 */
  187.  
  188. void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  189. {
  190.  
  191. GPIO_InitTypeDef GPIO_InitStruct;
  192. if(hspi->Instance==SPI1)
  193. {
  194. /* Peripheral clock enable */
  195. __SPI1_CLK_ENABLE();
  196.  
  197. /**SPI1 GPIO Configuration
  198. PA4 ------> SPI1_NSS
  199. PA5 ------> SPI1_SCK
  200. PA6 ------> SPI1_MISO
  201. PA7 ------> SPI1_MOSI
  202. */
  203. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  204. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  205. GPIO_InitStruct.Pull = GPIO_NOPULL;
  206. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  207. GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
  208. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  209.  
  210. /* Peripheral interrupt init*/
  211. HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
  212. HAL_NVIC_EnableIRQ(SPI1_IRQn);
  213. }
  214.  
  215. }
  216.  
  217. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
  218. {
  219.  
  220. if(hspi->Instance==SPI1)
  221. {
  222. /* Peripheral clock disable */
  223. __SPI1_CLK_DISABLE();
  224.  
  225. /**SPI1 GPIO Configuration
  226. PA4 ------> SPI1_NSS
  227. PA5 ------> SPI1_SCK
  228. PA6 ------> SPI1_MISO
  229. PA7 ------> SPI1_MOSI
  230. */
  231. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
  232.  
  233. /* Peripheral interrupt Deinit*/
  234. HAL_NVIC_DisableIRQ(SPI1_IRQn);
  235. }
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement