UveosOnNation_VEGA_RawHwTes.../uartTest/main.c

170 lines
3.6 KiB
C

#include <stdio.h>
#include "main.h"
USART_InitType USART_InitStructure;
void RCC_Configuration1(void);
void GPIO_Configuration1(void);
void NVIC_Configuration1(void);
void send(uint8_t *data, size_t size) {
uint8_t *end = data + size;
while (data < end) {
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
USART_SendData(USARTx, *data);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
++data;
}
}
void delay(uint32_t ms) {
ms = ms * SystemCoreClock / 1000;
while (ms) {
--ms;
}
}
void USART1_IRQHandler() {
uint8_t byte;
while (USART_GetFlagStatus(USARTx, USART_FLAG_RXDNE) == SET) {
byte = USART_ReceiveData(USARTx);
send(&byte, 1);
}
}
void USART2_IRQHandler() {
uint8_t byte;
while (USART_GetFlagStatus(USARTx, USART_FLAG_RXDNE) == SET) {
byte = USART_ReceiveData(USARTx);
send(&byte, 1);
}
}
void UART4_IRQHandler() {
uint8_t byte;
while (USART_GetFlagStatus(USARTx, USART_FLAG_RXDNE) == SET) {
byte = USART_ReceiveData(USARTx);
send(&byte, 1);
}
}
void NVIC_Configuration1(void) {
NVIC_InitType NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = UARTx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void) {
RCC_Configuration1();
NVIC_Configuration1();
GPIO_Configuration1();
USART_InitStructure.BaudRate = 115200;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USARTx, &USART_InitStructure);
USART_ConfigInt(USARTx, USART_INT_RXDNE, ENABLE);
// USART_ConfigInt(USARTx, USART_INT_TXDE, ENABLE);
USART_Enable(USARTx, ENABLE);
char msg[] = "0 helo world\n";
#define STR(VAL) (uint8_t *)VAL,(sizeof (VAL)-1)
for (;;) {
++*msg;
if (*msg > '9') {
*msg = '0';
}
// send(STR(msg));
delay(3000);
}
}
/**
* @brief Configures the different system clocks.
*/
void RCC_Configuration1(void) {
GPIO_APBxClkCmd(USARTx_GPIO_CLK | RCC_APB2_PERIPH_AFIO, ENABLE);
USART_APBxClkCmd(USARTx_CLK, ENABLE);
}
void GPIO_Configuration1(void) {
GPIO_InitType GPIO_InitStructure;
GPIO_InitStructure.Pin = USARTx_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.Pin = USARTx_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure);
GPIO_ConfigPinRemap(GPIO_RMP_USART1, ENABLE);
// GPIO_ConfigPinRemap(GPIO_RMP1_USART2, ENABLE);
}
#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
*/
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
/* 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) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/