-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_tests2.c
141 lines (113 loc) · 3.05 KB
/
uart_tests2.c
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
#include <main.h>
#include <stdio.h>
#include <string.h>
UART_HandleTypeDef huart1 = {0}; //using uart 1 --manejador
void GPIO_Init(void);
void Error_Handler(void);
void Clock_Config(void);
void UART1_Init(void);
char mess[] = "UART\r\n";
uint8_t byte, idx = 0;
uint8_t buffer[32];
int main(void)
{
System_Init();
HAL_UART_Transmit(&huart1, (uint8_t*)mess, strlen(mess), HAL_MAX_DELAY);
//HAL_Delay(1000); // Delay for 1 second
while(1)
{
HAL_UART_Receive(&huart1, &byte, 1, HAL_MAX_DELAY);
if(byte == '\n')
{
buffer[idx++] = byte;
HAL_UART_Transmit(&huart1, buffer, idx, HAL_MAX_DELAY);
idx = 0;
}
else if(byte >= 'a' && byte <= 'z')
{
buffer[idx++] = byte - ('a'-'A');
}
else
{
buffer[idx++] = byte;
}
}
return 0;
}
void System_Init(void)
{
HAL_Init();
Clock_Config();
GPIO_Init();
UART1_Init();
}
void UART1_Init(void)
{
//Habilitar reloj de periferico
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
//Configuracion de bajo nivel
GPIO_InitTypeDef tx_pin = {0};
tx_pin.Pin = GPIO_PIN_9 | GPIO_PIN_10;
tx_pin.Mode = GPIO_MODE_AF_PP;
tx_pin.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &tx_pin);
//Habilitacion de la interrupcion
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.Mode = UART_MODE_TX_RX;
if(HAL_UART_Init(&huart1) != HAL_OK)
Error_Handler();
//Configuracion de alto nivel
//Arrancar el periferico
}
void Clock_Config(void)
{
RCC_OscInitTypeDef osc_init = {0};
osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSE; //Configure oscillator type as HSE
osc_init.HSEState = RCC_HSE_ON;
osc_init.LSEState = RCC_LSE_OFF;
osc_init.HSIState = RCC_HSI_OFF;
osc_init.LSIState = RCC_LSI_OFF;
osc_init.PLL.PLLState = RCC_PLL_ON; //Turn on PLL
osc_init.PLL.PLLSource = RCC_PLLSOURCE_HSE; //Set PLL source as HSE
//SYSCLK = HSE * (N / M)
osc_init.PLL.PLLM = 8;
osc_init.PLL.PLLN = 200;
osc_init.PLL.PLLP = RCC_PLLP_DIV2; //2
osc_init.PLL.PLLQ = 2;
//osc_init.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&osc_init) != HAL_OK)
Error_Handler();
RCC_ClkInitTypeDef clock_init = {0};
clock_init.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clock_init.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
clock_init.AHBCLKDivider = RCC_SYSCLK_DIV1;
clock_init.APB1CLKDivider = RCC_HCLK_DIV2;
clock_init.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&clock_init, FLASH_LATENCY_4) != HAL_OK)
Error_Handler();
}
void Error_Handler(void)
{
while(1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(100);
}
}
void GPIO_Init(void)
{
/* Configurar pin PA5 como salida digital */
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef led_pin = {0};
led_pin.Pin = GPIO_PIN_5;
led_pin.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOA, &led_pin);
}
void SysTick_Handler(void)
{
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
}