5.USART_UART//串口中断接收
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
#include "sensor.h"
#include "RS232.h"
#include "sys_config.h"
#include "FreeRTOS.h"
/* USART init function */
void GD_USART_UART_Init(uint32_t usart_periph, uint32_t baudval)
{
if(usart_periph == USART0)
{
GD_USART_UART_MspInit(USART0);
usart_baudrate_set(USART0, baudval);
/* configure USART0 transmitter */
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
/* configure USART0 receiver */
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
/* enable USART0 */
usart_enable(USART0);
}
else if(usart_periph == USART1)
{
GD_USART_UART_MspInit(USART1);
usart_baudrate_set(USART1, baudval);
/* configure USART1 transmitter */
usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
/* configure USART1 receiver */
usart_receive_config(USART1, USART_RECEIVE_ENABLE);
/* enable USART1 */
usart_enable(USART1);
}
else if(usart_periph == USART2)
{
GD_USART_UART_MspInit(USART2);
usart_baudrate_set(USART2, baudval);
/* configure USART2 transmitter */
usart_transmit_config(USART2, USART_TRANSMIT_ENABLE);
/* configure USART2 receiver */
usart_receive_config(USART2, USART_RECEIVE_ENABLE);
/* enable USART2 */
usart_enable(USART2);
}
else if(usart_periph == UART4)
{
GD_USART_UART_MspInit(UART4);
usart_baudrate_set(UART4, baudval);
/* configure USART2 transmitter */
usart_transmit_config(UART4, USART_TRANSMIT_ENABLE);
/* configure USART2 receiver */
usart_receive_config(UART4, USART_RECEIVE_ENABLE);
/* enable USART2 */
usart_enable(UART4);
}
else if(usart_periph == USART5)
{
GD_USART_UART_MspInit(USART5);
usart_baudrate_set(USART5, baudval);
/* configure USART5 transmitter */
usart_transmit_config(USART5, USART_TRANSMIT_ENABLE);
/* configure USART5 receiver */
usart_receive_config(USART5, USART_RECEIVE_ENABLE);
/* enable USART5 */
usart_enable(USART5);
}
}
void GD_USART_UART_MspInit(uint32_t usart_periph)
{
if(usart_periph == USART0)
{
usart_deinit(USART0);
usart_disable(USART0);
/**USART0 GPIO Configuration
PA9 ------> USART0_TX
PA10 ------> USART0_RX
*/
rcu_periph_clock_enable(RCU_GPIOA);
/* USART0 clock enable */
rcu_periph_clock_enable(RCU_USART0);
/* configure USART0 Tx as alternate function push-pull */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9 | GPIO_PIN_10);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9 | GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9 | GPIO_PIN_10);
// /* USART0 interrupt configuration */
// nvic_irq_enable(USART0_IRQn, 5, 0);
//
// /* enable USART0 TBE interrupt */
// usart_interrupt_enable(USART0, USART_INT_RBNE);
}
else if(usart_periph == USART1)
{
usart_deinit(USART1);
usart_disable(USART1);
/**USART1 GPIO Configuration
PD5 ------> USART1_TX
PD6 ------> USART1_RX
*/
rcu_periph_clock_enable(RCU_GPIOD);
/* USART1 clock enable */
rcu_periph_clock_enable(RCU_USART1);
/* configure USART1 Tx as alternate function push-pull */
gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_5 | GPIO_PIN_6);
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_5 | GPIO_PIN_6);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_6);
/* USART1 interrupt configuration */
nvic_irq_enable(USART1_IRQn, 5, 0);
/* enable USART1 TBE interrupt */
usart_interrupt_enable(USART1, USART_INT_RBNE);
}
else if(usart_periph == USART2)
{
usart_deinit(USART2);
usart_disable(USART2);
/**USART2 GPIO Configuration
PD8 ------> USART2_TX
PD9 ------> USART2_RX
*/
rcu_periph_clock_enable(RCU_GPIOD);
/* USART2 clock enable */
rcu_periph_clock_enable(RCU_USART2);
/* configure USART2 Tx as alternate function push-pull */
gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_8 | GPIO_PIN_9);
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_8 | GPIO_PIN_9);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8 | GPIO_PIN_9);
/* USART2 interrupt configuration */
nvic_irq_enable(USART2_IRQn, 5, 0);
/* enable USART2 TBE interrupt */
usart_interrupt_enable(USART2, USART_INT_RBNE);
}
else if(usart_periph == UART4)
{
usart_deinit(UART4);
usart_disable(UART4);
/**UART4 GPIO Configuration
PC12 ------> UART4_TX
PD2 ------> UART4_RX
*/
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_GPIOD);
/* UART4 clock enable */
rcu_periph_clock_enable(RCU_UART4);
/* configure UART4 Tx as alternate function push-pull */
gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_12);
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_12);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
gpio_af_set(GPIOD, GPIO_AF_8, GPIO_PIN_2);
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
/* UART4 interrupt configuration */
nvic_irq_enable(UART4_IRQn, 5, 0);
/* enable UART4 TBE interrupt */
usart_interrupt_enable(UART4, USART_INT_RBNE);
}
else if(usart_periph == USART5)
{
usart_deinit(USART5);
usart_disable(USART5);
/**USART5 GPIO Configuration
PC6 ------> USART5_TX
PC7 ------> USART5_RX
*/
rcu_periph_clock_enable(RCU_GPIOC);
/* USART5 clock enable */
rcu_periph_clock_enable(RCU_USART5);
/* configure USART5 Tx as alternate function push-pull */
gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_6 | GPIO_PIN_7);
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6 | GPIO_PIN_7);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
/* USART5 interrupt configuration */
nvic_irq_enable(USART5_IRQn, 5, 0);
/* enable USART5 TBE interrupt */
usart_interrupt_enable(USART5, USART_INT_RBNE);
}
}
void GD_USART_UART_MspDeInit(uint32_t usart_periph)
{
if(usart_periph == USART0)
{
usart_deinit(USART0);
usart_disable(USART0);
/**USART0 GPIO Configuration
PA9 ------> USART0_TX
PA10 ------> USART0_RX
*/
rcu_periph_clock_enable(RCU_GPIOA);
/* Peripheral clock disable */
rcu_periph_clock_disable(RCU_USART0);
/* configure USART0 Tx as input function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_9 | GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9 | GPIO_PIN_10);
/* USART0 interrupt Deinit */
nvic_irq_disable(USART0_IRQn);
}
else if(usart_periph == USART1)
{
usart_deinit(USART1);
usart_disable(USART1);
/**USART1 GPIO Configuration
PD5 ------> USART1_TX
PD6 ------> USART1_RX
*/
rcu_periph_clock_enable(RCU_GPIOD);
/* Peripheral clock disable */
rcu_periph_clock_disable(RCU_USART1);
/* configure USART1 Tx as input function push-pull */
gpio_mode_set(GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_5 | GPIO_PIN_6);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_6);
/* USART1 interrupt Deinit */
nvic_irq_disable(USART1_IRQn);
}
else if(usart_periph == USART2)
{
usart_deinit(USART2);
usart_disable(USART2);
/**USART2 GPIO Configuration
PD8 ------> USART2_TX
PD9 ------> USART2_RX
*/
rcu_periph_clock_enable(RCU_GPIOD);
/* Peripheral clock disable */
rcu_periph_clock_disable(RCU_USART2);
/* configure USART2 Tx as input function push-pull */
gpio_mode_set(GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_8 | GPIO_PIN_9);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8 | GPIO_PIN_9);
/* USART2 interrupt Deinit */
nvic_irq_disable(USART2_IRQn);
}
else if(usart_periph == UART4)
{
usart_deinit(UART4);
usart_disable(UART4);
/**UART4 GPIO Configuration
PC12 ------> UART4_TX
PD2 ------> UART4_RX
*/
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_GPIOD);
/* Peripheral clock disable */
rcu_periph_clock_disable(RCU_UART4);
/* configure USART2 Tx as input function push-pull */
gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_12);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
gpio_mode_set(GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_2);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
/* USART2 interrupt Deinit */
nvic_irq_disable(UART4_IRQn);
}
else if(usart_periph == USART5)
{
usart_deinit(USART5);
usart_disable(USART5);
/**USART5 GPIO Configuration
PC6 ------> USART5_TX
PC7 ------> USART5_RX
*/
rcu_periph_clock_enable(RCU_GPIOC);
/* Peripheral clock disable */
rcu_periph_clock_disable(RCU_USART5);
/* configure USART5 Tx as input function push-pull */
gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_6 | GPIO_PIN_7);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
/* USART5 interrupt Deinit */
nvic_irq_disable(USART5_IRQn);
}
}
#if USE_MAX3485
void MAX3485_RxHandler(void)
{
if(usart_flag_get(USART0, USART_FLAG_ORERR) != RESET)
{
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_ERR_ORERR);
/* only restart*/
MAX3485_ClearFlag();
usart_disable(USART0);
usart_enable(USART0);
}
if(usart_flag_get(USART0, USART_FLAG_FERR) != RESET)
{
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_ERR_FERR);
}
if(usart_flag_get(USART0, USART_FLAG_RBNE) != RESET)
{
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
MAX3485_RecvHandler(usart_data_receive(USART0));
}
}
#endif
uint8_t uartdata0;
uint8_t uartdata1;
uint8_t uartdata2;
uint8_t uartdata4;
uint8_t uartdata5;
void USART0_IRQHandler(void)
{
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_ERR_ORERR) != RESET)
{
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_ERR_ORERR);
}
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET)
{
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
uartdata0 = usart_data_receive(USART0); // Read data to clear interrupt
}
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE) != RESET)
{
// usart_data_transmit(USART0, (BYTE)*uart0PTrxData);
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TBE);
}
}
void USART1_IRQHandler(void)
{
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_ERR_ORERR) != RESET)
{
usart_interrupt_flag_clear(USART1, USART_INT_FLAG_ERR_ORERR);
}
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE) != RESET)
{
usart_interrupt_flag_clear(USART1, USART_INT_FLAG_RBNE);
uartdata1 = usart_data_receive(USART1); // Read data to clear interrupt
}
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_TBE) != RESET)
{
// usart_data_transmit(USART1, (BYTE)*uart0PTrxData);
usart_interrupt_flag_clear(USART1, USART_INT_FLAG_TBE);
}
}
void USART2_IRQHandler(void)
{
if(usart_interrupt_flag_get(USART2, USART_INT_FLAG_ERR_ORERR) != RESET)
{
usart_interrupt_flag_clear(USART2, USART_INT_FLAG_ERR_ORERR);
}
if(usart_interrupt_flag_get(USART2, USART_INT_FLAG_RBNE) != RESET)
{
usart_interrupt_flag_clear(USART2, USART_INT_FLAG_RBNE);
uartdata2 = usart_data_receive(USART2);
}
if(usart_interrupt_flag_get(USART2, USART_INT_FLAG_TBE) != RESET)
{
// usart_data_transmit(USART2, (BYTE)*uart0PTrxData);
usart_interrupt_flag_clear(USART2, USART_INT_FLAG_TBE);
}
}
void UART4_IRQHandler(void)
{
if(usart_interrupt_flag_get(UART4, USART_INT_FLAG_ERR_ORERR) != RESET)
{
usart_interrupt_flag_clear(UART4, USART_INT_FLAG_ERR_ORERR);
}
if(usart_interrupt_flag_get(UART4, USART_INT_FLAG_RBNE) != RESET)
{
usart_interrupt_flag_clear(UART4, USART_INT_FLAG_RBNE);
uartdata4 = usart_data_receive(UART4);
}
if(usart_interrupt_flag_get(UART4, USART_INT_FLAG_TBE) != RESET)
{
// usart_data_transmit(UART4, (BYTE)*uart0PTrxData);
usart_interrupt_flag_clear(UART4, USART_INT_FLAG_TBE);
}
}
void USART5_IRQHandler(void)
{
if(usart_interrupt_flag_get(USART5, USART_INT_FLAG_ERR_ORERR) != RESET)
{
usart_interrupt_flag_clear(USART5, USART_INT_FLAG_ERR_ORERR);
}
if(usart_interrupt_flag_get(USART5, USART_INT_FLAG_RBNE) != RESET)
{
usart_interrupt_flag_clear(USART5, USART_INT_FLAG_RBNE);
uartdata5 = usart_data_receive(USART5);
}
if(usart_interrupt_flag_get(USART5, USART_INT_FLAG_TBE) != RESET)
{
// usart_data_transmit(USART5, (BYTE)*uart0PTrxData);
usart_interrupt_flag_clear(USART5, USART_INT_FLAG_TBE);
}
}
uint16_t UART_W(uint32_t usart_periph, uint8_t *pbuf, uint16_t len)
{
if(usart_periph == USART0)
{
while (len--)
{
usart_data_transmit(USART0, *pbuf);
while( usart_flag_get( USART0, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
}
else if(usart_periph == USART1)
{
while (len--)
{
usart_data_transmit(USART1, *pbuf);
while( usart_flag_get( USART1, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
}
else if(usart_periph == USART2)
{
while (len--)
{
usart_data_transmit(USART2, *pbuf);
while( usart_flag_get( USART2, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
}
else if(usart_periph == UART4)
{
while (len--)
{
usart_data_transmit(UART4, *pbuf);
while( usart_flag_get( UART4, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
}
else if(usart_periph == USART5)
{
while (len--)
{
usart_data_transmit(USART5, *pbuf);
while( usart_flag_get( USART5, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
}
return len ;
}
uint16_t uart0_w(uint8_t *pbuf,uint16_t len)
{
// portENTER_CRITICAL();
while (len--)
{
usart_data_transmit(USART0, *pbuf);
while( usart_flag_get( USART0, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
// portEXIT_CRITICAL();
return len ;
}
uint16_t uart1_w(uint8_t *pbuf,uint16_t len)
{
// portENTER_CRITICAL();
while (len--)
{
usart_data_transmit(USART1, *pbuf);
while( usart_flag_get( USART1, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
// portEXIT_CRITICAL();
return len ;
}
uint16_t uart2_w(uint8_t *pbuf,uint16_t len)
{
// portENTER_CRITICAL();
while (len--)
{
usart_data_transmit(USART2, *pbuf);
while( usart_flag_get( USART2, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
// portEXIT_CRITICAL();
return len ;
}
uint16_t uart4_w(uint8_t *pbuf,uint16_t len)
{
// portENTER_CRITICAL();
while (len--)
{
usart_data_transmit(UART4, *pbuf);
while( usart_flag_get( UART4, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
// portEXIT_CRITICAL();
return len ;
}
uint16_t uart5_w(uint8_t *pbuf,uint16_t len)
{
// portENTER_CRITICAL();
while (len--)
{
usart_data_transmit(USART5, *pbuf);
while( usart_flag_get( USART5, USART_FLAG_TBE ) == RESET );
pbuf ++ ;
}
// portEXIT_CRITICAL();
return len ;
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART2, (uint8_t)ch);
while(RESET == usart_flag_get(USART2, USART_FLAG_TBE));
return ch;
}
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
//PUTCHAR_PROTOTYPE
//{
// /* Place your implementation of fputc here */
// /* e.g. write a character to the USART */
// usart_flag_clear(USART2, USART_FLAG_TBE);
// usart_data_transmit(USART2, (uint8_t)ch);
//
// /* Loop until transmit data register is empty */
// while( usart_flag_get( USART2, USART_FLAG_TBE ) == RESET )
// {}
// return ch;
//}
/*****************************END OF FILE****/
|
|