-
-
Save anonymous/bc7141ae8593bdf3598dc5db923e1704 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UART_HandleTypeDef Device_UART_Handle; | |
int main(void) { | |
HAL_Init(); | |
RCC_Init(); | |
Debug_UART_Init(); | |
printf("\n\n\nBooting uart-test / %d\r\n", RANDOM); | |
printf("Enabling device UART\r\n"); | |
// Start clocks | |
__GPIOA_CLK_ENABLE(); | |
__AFIO_CLK_ENABLE(); | |
__USART2_CLK_ENABLE(); | |
// Enable GPIOs for alternate function | |
GPIO_InitTypeDef GPIO_Config = { | |
.Pull = GPIO_NOPULL, | |
.Speed = GPIO_SPEED_HIGH, | |
}; | |
GPIO_Config.Pin = GPIO_PIN_2; | |
GPIO_Config.Mode = GPIO_MODE_AF_PP; | |
HAL_GPIO_Init(GPIOA, &GPIO_Config); | |
GPIO_Config.Pin = GPIO_PIN_3; | |
GPIO_Config.Mode = GPIO_MODE_INPUT; | |
HAL_GPIO_Init(GPIOA, &GPIO_Config); | |
UART_InitTypeDef UART_Config = { | |
.BaudRate = 115200, | |
.Mode = UART_MODE_TX_RX, | |
.WordLength = UART_WORDLENGTH_8B, | |
.HwFlowCtl = UART_HWCONTROL_NONE, | |
.StopBits = UART_STOPBITS_1, | |
.Parity = UART_PARITY_NONE, | |
}; | |
Device_UART_Handle.Instance = USART2; | |
Device_UART_Handle.Init = UART_Config; | |
HAL_UART_Init(&Device_UART_Handle); | |
char *cmd = "AT\r\n"; | |
uint32_t len = strlen(cmd); | |
printf("Sending %d bytes: %s\n\r Echo:\r\n", (unsigned int)len, cmd); | |
// Send 1 byte at a time, receiving the echo | |
for(uint32_t i = 0; i < len; i++) { | |
uint8_t echoChar; | |
if (HAL_UART_Transmit(&Device_UART_Handle, (uint8_t *)cmd + i, 1, 5000) != HAL_OK) { | |
printf(" Send fail\r\n"); | |
while(1) {} | |
} | |
if (HAL_UART_Receive(&Device_UART_Handle, &echoChar, 1, 5000) != HAL_OK) { | |
printf(" Echo recv fail\r\n"); | |
while(1) {} | |
} | |
printf(" Byte %d: sent %d, echo %d (ORE %d)\r\n", (unsigned int)i, *(cmd + i), echoChar, __HAL_UART_GET_FLAG(&Device_UART_Handle, UART_FLAG_ORE)); | |
} | |
printf(" Receive:\n\r"); | |
// Attempt to read the remaining data | |
// 7 chars left: \n \r \n O K \r \n | |
// ints: 10 13 10 79 75 13 10 | |
while (1) { | |
uint8_t recvChar; | |
if (HAL_UART_Receive(&Device_UART_Handle, &recvChar, 1, 5000) != HAL_OK) { | |
printf(" Recv fail\r\n"); | |
printf(" RXNE: %d ORE: %d NE: %d FE: %d PE: %d\r\n", | |
__HAL_UART_GET_FLAG(&Device_UART_Handle, UART_FLAG_RXNE), | |
__HAL_UART_GET_FLAG(&Device_UART_Handle, UART_FLAG_ORE), | |
__HAL_UART_GET_FLAG(&Device_UART_Handle, UART_FLAG_NE), | |
__HAL_UART_GET_FLAG(&Device_UART_Handle, UART_FLAG_FE), | |
__HAL_UART_GET_FLAG(&Device_UART_Handle, UART_FLAG_PE)); | |
while(1) {} | |
} | |
printf(" Recv %d\r\n", recvChar); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment