...

반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// 버튼을 클릭시 uart1로 데이터 송신
 
// HAL_UART_Receive_IT는 타임아웃 설정 안함, 인터럽트 관련인듯
// HAL_UART_Receive 는 타임아웃 설정 할수 있음
 
#include "stm32f4xx_hal.h"
//#include "stm32f4xx_hal_uart.h" // UART를 사용하기 위해서 INCLUDE 해야 됨
 
 
 
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
 
void HAL_UART_MspInit(UART_HandleTypeDef *huart);
void Uart_Config();
 
 
static void EXTILine0_Config(void);
void EXTI0_IRQHandler(void);
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);
 
 
 
UART_HandleTypeDef UartHandle;
uint8_t RxBuffer[100]; 
 
 
uint8_t UARTStatus;
 
 
int main(void)
{
 
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();
 
    /* Configure the system clock */
    SystemClock_Config();
 
    MX_GPIO_Init();       // 인터럽트 발생시 출력확인을 위한 GPIO 설정
    EXTILine0_Config();   // 인터럽트 설정
    Uart_Config();
 
    //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, GPIO_PIN_SET);
 
    HAL_UART_Transmit(&UartHandle, "START"55000);
 
    while (1)
    {
        //&UartHandle : 설정한(사용할) uart 구조체 변수, RxBuffer : 데이터를 받을 버퍼, 10 : 받을 데이터수, 5000 : 타임아웃 시간 
        
        /*
        if(HAL_UART_Receive(&UartHandle, (uint8_t *)RxBuffer, 10, 5000) != HAL_OK)
        {
            printf("aaa\r\n");
            //Error_Handler();    
        }
        */
 
        HAL_UART_Receive(&huart1, (uint8_t *)RxBuffer, 5100); // 이런식으로 폴링 방식으로 데이터 받는데는 성공
 
        //UARTStatus = HAL_UART_Receive(&UartHandle, (uint8_t *)RxBuffer, 10, 5000);
 
 
 
 
 
    }
 
}
 
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
 
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
    __PWR_CLK_ENABLE();
 
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 4;
    RCC_OscInitStruct.PLL.PLLN = 180;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    RCC_OscInitStruct.PLL.PLLQ = 4;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
    HAL_PWREx_ActivateOverDrive();
 
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
        |RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
 
    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
 
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 
    /* SysTick_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(SysTick_IRQn, 00);
}
 
 
 
void MX_GPIO_Init(void)
{
 
    GPIO_InitTypeDef GPIO_InitStruct;
 
    /* GPIO Ports Clock Enable */
    __GPIOG_CLK_ENABLE();
 
    /*Configure GPIO pins : PG13 PG14 */
    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
    HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
 
}
 
 
 
 
 
 
//---------- 인터럽트 관련 ----------//
static void EXTILine0_Config(void)
{
    GPIO_InitTypeDef   GPIO_InitStructure;
 
    /* Enable GPIOA clock */
    __HAL_RCC_GPIOA_CLK_ENABLE(); // 클록 ENABLE
 
    /* Configure PA0 pin as input floating */
    GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;       // 해당핀을 인터럽트 폴링시 발생시킴
    GPIO_InitStructure.Pull = GPIO_NOPULL;                
    GPIO_InitStructure.Pin = GPIO_PIN_0;                  
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    /* Enable and set EXTI Line0 Interrupt to the lowest priority */
    HAL_NVIC_SetPriority(EXTI0_IRQn, 20);               // 0번 라인을 우선순위 설정, 버튼이 PA0번이므로               
    HAL_NVIC_EnableIRQ(EXTI0_IRQn);                       // 0번 라인 인터럽트 활성화
}
 
void EXTI0_IRQHandler(void)     // EXTI0 인터럽트 발생시 호출되는 함수
{
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); // 이 함수 안에서 해당 인터럽트를 클리어 하고 콜백함수를 호출해줌
}
 
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)  // HAL_GPIO_EXTI_IRQHandler함수에서 해당 인터럽트를 클리어하고 호출되는 콜백 함수
{
    if(HAL_UART_Transmit(&UartHandle, RxBuffer, 105000)!= HAL_OK)  // 버튼을 누르면 uart1번으로 데이터 송신
    {                                                               // &UartHandle : 설정한 uart 구조체 변수, "siwnho" : 보낼 데이터, 6 : 보낼 데이터 수, 5000 : 타임아웃 시간
        //Error_Handler();   
    }
}
//---------- 인터럽트 관련 ----------//
 
 
 
 
 
 
//---------- UART ----------//
void Uart_Config()
{
 
    UartHandle.Instance          = USART1;        // 사용할 USART 번호
    UartHandle.Init.BaudRate     = 9600;          // BAUDRATE
    UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
    UartHandle.Init.StopBits     = UART_STOPBITS_1;
    UartHandle.Init.Parity       = UART_PARITY_NONE;
    UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;   // 하드웨어 제어
    UartHandle.Init.Mode         = UART_MODE_TX_RX;       // 
    UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
 
    if(HAL_UART_Init(&UartHandle) != HAL_OK)      // HAL_UART_Init 함수에서 초기화시 HAL_UART_MspInit 함수를 호출해서 GPIO 설정을 함
    {
        //Error_Handler();
    }
 
}
 
void HAL_UART_MspInit(UART_HandleTypeDef *huart)        // UART 관련 GPIO 설정
{  
    GPIO_InitTypeDef  GPIO_InitStruct;
 
    /*##-1- Enable peripherals and GPIO Clocks #################################*/
    /* Enable GPIO TX/RX clock */
    __HAL_RCC_GPIOA_CLK_ENABLE(); // UART 관련 GPIO 클럭 ENABLE
 
    /* Enable USART1 clock */
    __HAL_RCC_USART1_CLK_ENABLE(); // 사용할 UART1 클럭 ENABLE
 
    /*##-2- Configure peripheral GPIO ##########################################*/  
    /* UART TX GPIO pin configuration  */
    GPIO_InitStruct.Pin       = GPIO_PIN_9;       // UART1 TX 핀
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;  // 모드 설정
    GPIO_InitStruct.Pull      = GPIO_NOPULL;
    GPIO_InitStruct.Speed     = GPIO_SPEED_FAST;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;  // 해당핀을 USART1로 사용하겠다고 선언
 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);       
 
    /* UART RX GPIO pin configuration  */
    GPIO_InitStruct.Pin = GPIO_PIN_10;            // UART1 RX 핀
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;  // 해당핀을 USART1로 사용하겠다고 선언
 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
//---------- UART ----------//
 
 
 
 
 
 
 
 
 
 
 
#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 CODE BEGIN 6 */
    /* 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) */
    /* USER CODE END 6 */
 
}
 
#endif
 
/**
* @}
*/ 
 
/**
* @}
*/ 
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
 
cs


반응형