123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include "myUart.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_usart.h"
- #include "stm32f10x_it.h"
- #include "misc.h"
- UART_CALLBACK uartCallBack;
-
- uint8_t USART_RX_BUF[USART_REC_LEN];
- uint16_t USART_RX_STA=0;
- static irqCallback_ts myIrqCallback_uart1;
- void uart1_callback(uint8_t status, uint32_t param)
- {
- uint8_t Res;
- switch (status)
- {
- case 0:
- {
- uartCallBack(USART_RX_BUF, USART_RX_STA);
- memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF));
- USART_RX_STA=0;
- }
- break;
- case 1:
- {
-
- USART_RX_BUF[USART_RX_STA] = param;
- USART_RX_STA++;
- if(USART_RX_STA > (USART_REC_LEN - 1))
- {
- USART_RX_STA=0;
- }
- }
- break;
-
- default:
- break;
- }
- }
- void myUart1_init(uint32_t baudrate, UART_CALLBACK cb)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- myIrqCallback_uart1.thisCb = uart1_callback;
- USART1_callbackRegiste(&myIrqCallback_uart1);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- USART_InitStructure.USART_BaudRate = baudrate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
-
- USART_Cmd(USART1, ENABLE);
- uartCallBack = cb;
- }
- void myUart1_sendByte(uint8_t src)
- {
- while(!USART_GetFlagStatus(USART1, USART_FLAG_TC));
- USART_SendData(USART1, src);
- }
- void myUart1_sendArray(uint8_t *src, uint16_t srclen)
- {
- while(srclen --)
- {
- myUart1_sendByte(*src);
- src ++;
- }
- }
- #if 1
- #pragma import(__use_no_semihosting)
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- void _sys_exit(int x)
- {
- x = x;
- }
- int fputc(int ch, FILE *f)
- {
- while((USART1->SR&0X40)==0);
- USART1->DR = (uint8_t) ch;
- return ch;
- }
- #endif
|