#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "../global.h"
volatile uint8_t COM2SendBuf[COM2SENDBUFLEN];
volatile uint8_t COM2ReceBuf[COM2RECEBUFLEN];
volatile uint16_t COM2ReceBufPtr=0;
volatile bool COM2isReceOK=false;
volatile uint16_t COM2isBusy=0;
volatile uint16_t COM2SendBufPtr=0;
volatile uint16_t COM2SendLen=0;
void USART2_init( uint32_t baud_rate , usart_parity_selection_type ptype , usart_data_bit_num_type data_bit , usart_stop_bit_num_type stop_bit )
{
/* enable dma2 periph clock */
usart_reset(USART2);
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK,TRUE);
crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
//gpio_pin_remap_config(USART2_MUX ,TRUE);
gpio_init_type gpio_init_struct;
gpio_default_para_init(&gpio_init_struct);
/* configure the TX pin */
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
//gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_2;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
/* configure the RX pin */
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_3;
gpio_init_struct.gpio_pull = GPIO_PULL_UP;//GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
/* configure param */
usart_init(USART2 , baud_rate , data_bit , stop_bit);
usart_transmitter_enable(USART2, TRUE);
usart_receiver_enable(USART2, TRUE);
usart_parity_selection_config(USART2, ptype);
usart_hardware_flow_control_set(USART2, USART_HARDWARE_FLOW_NONE);
nvic_irq_enable(USART2_IRQn, 2, 2);
usart_interrupt_enable ( USART2, USART_IDLE_INT , TRUE );
usart_interrupt_enable ( USART2, USART_RDBF_INT , TRUE );
usart_enable(USART2, TRUE);
}
void USART2_SendOut(const uint8_t *buf,int16_t len)
{
#ifndef __BOOTLOADER__
while(COM2isBusy) taskYIELD();
#else
//while(COM2isBusy) {};
#endif
COM2isBusy=4000;
memcpy((void*)COM2SendBuf,buf,len);
COM2SendBufPtr=0;
COM2SendLen=len;
usart_interrupt_enable ( USART2, USART_TDBE_INT , TRUE ); //寄存器空
usart_interrupt_enable ( USART2, USART_TDC_INT , TRUE ); //发完成
}
void USART2_IRQHandler(void)
{
if ( usart_flag_get ( USART2, USART_TDBE_FLAG ) !=RESET )
{
if( COM2SendBufPtr >= COM2SendLen )
{
usart_interrupt_enable ( USART2, USART_TDBE_INT , FALSE ); //寄存器空
}
else
{
usart_data_transmit(USART2, COM2SendBuf[COM2SendBufPtr] );
COM2SendBufPtr++;
}
};
if ( usart_flag_get ( USART2, USART_TDBE_INT ) !=RESET )
{
usart_interrupt_enable ( USART2, USART_TDC_INT , FALSE );
COM2isBusy=COM2SendInterval;
};
if ( usart_flag_get ( USART2, USART_RDBF_FLAG ) !=RESET )
{
COM2ReceBuf[COM2ReceBufPtr] = usart_data_receive(USART2);
COM2ReceBufPtr ++;
};
//接收完一个数据包中断
if ( usart_flag_get ( USART2, USART_IDLEF_FLAG ) !=RESET )
{
uint8_t clear=clear;
clear= USART2->sts;
clear= USART2->dt;
//按收包完成
COM2isReceOK=true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//USART2_init(4800 , USART_PARITY_NONE , USART_DATA_8BITS , USART_STOP_1_5_BIT );
USART2_init(4800 , USART_PARITY_NONE , USART_DATA_8BITS , USART_STOP_2_BIT );
//USART2_init(4800 , USART_PARITY_NONE , USART_DATA_9BITS , USART_STOP_0_5_BIT );
通讯目标为BL0939, 需求为 4800,N,8,1.5
AT32F403ARCT7 用USART2 当停止位设置为 USART_STOP_0_5_BIT 或 USART_STOP_1_5_BIT 时
1: TX Pin 无任何信号输出 或 输出乱码 ,但配 4800,N,8,1 / 4800,N,8,2 可以有输出
2: 可产生 USART_TDBE_INT 和 USART_TDBE_INT 中断
3: 配置成4800,N,8,2 时竟然与目标 4800,N,8,1.5 正常通讯
4: 同板换上GD32F303配 4800,N,8,1.5 完成正常
|
|