1.在上一编文章中,我们测试了库文件的USB虚拟串口功能,也分析了为什么DEMO中要求短接PA2和PA3(USART2)来测试虚拟串口的数据收发,这一次我们根据上一次测试的分析结果来尝试修改硬件USART配置,将虚拟口数据转到其它的硬件串口上,比喻USART1(板载有单独引出USART1来做DEBUG调试信息输出)。
下面进入程序修改环节:
1。修改代码中USART端口初始化函数,将IO口改为相应的端口,这里我演示改为USART1,端口分别是PA9和PA10(即板子上引出的USART1调试端口),修改后的代码如下:
void usart_gpio_config(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
/* configure the usart2 tx, rx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = GPIO_PINS_9 | GPIO_PINS_10;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_7);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_7);
}
注意,在端口复用功能设置这里参考用户手册IOMUX功能定义,选择正确的MUX号,这里根据手册上,PA9和PA10的USART1功能是MUX7,所以代码里要改为MUX7
然后修改USB转UART的定义函数为对应的USART1:void usb_usart_config( linecoding_type linecoding)
{
usart_stop_bit_num_type usart_stop_bit;
usart_data_bit_num_type usart_data_bit;
usart_parity_selection_type usart_parity_select;
/* enable the usart2 and gpio clock */
crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, FALSE);
crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
/* stop bit */
switch(linecoding.format)
{
case 0x0:
usart_stop_bit = USART_STOP_1_BIT;
break;
/* to be used when transmitting and receiving data in smartcard mode */
case 0x1:
usart_stop_bit = USART_STOP_1_5_BIT;
break;
case 0x2:
usart_stop_bit = USART_STOP_2_BIT;
break;
default :
break;
}
/* parity */
switch(linecoding.parity)
{
case 0x0:
usart_parity_select = USART_PARITY_NONE;
break;
case 0x1:
usart_parity_select = USART_PARITY_ODD;
break;
case 0x2:
usart_parity_select = USART_PARITY_EVEN;
break;
/* hardware usart not support parity for mark and space */
case 0x3:
case 0x4:
break;
default :
break;
}
if(USART_PARITY_NONE == usart_parity_select)
{
/* data bits */
switch(linecoding.data)
{
/* hardware usart not support data bits for 5/6 */
case 0x5:
case 0x6:
break;
case 0x7:
usart_data_bit = USART_DATA_7BITS;
break;
case 0x8:
usart_data_bit = USART_DATA_8BITS;
break;
/* hardware usart not support data bits for 16 */
case 0x10:
break;
default :
break;
}
}
else
{
/* data bits */
switch(linecoding.data)
{
/* hardware usart not support data bits for 5/6 */
case 0x5:
case 0x6:
break;
case 0x7:
usart_data_bit = USART_DATA_8BITS;
break;
case 0x8:
usart_data_bit = USART_DATA_9BITS;
break;
/* hardware usart not support data bits for 16 */
case 0x10:
break;
default :
break;
}
}
nvic_irq_enable(USART1_IRQn, 0, 0);
/* configure usart2 param */
usart_init(USART1, linecoding.bitrate, usart_data_bit, usart_stop_bit);
usart_parity_selection_config(USART1, usart_parity_select);
usart_transmitter_enable(USART1, TRUE);
usart_receiver_enable(USART1, TRUE);
/* enable usart2 interrupt */
usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
usart_enable(USART1, TRUE);
}
第三步修改相应的USART中断配置为USART1:
void USART1_IRQHandler(void)
{
if(usart_flag_get(USART1, USART_RDBF_FLAG) != RESET)
{
/* read one byte from the receive data register */
usart_rx_buffer[hw_usart_rx_index] = usart_data_receive(USART1);
hw_usart_rx_index = ( (hw_usart_rx_index+1) & (usart_buffer_size-1) );
}
}
第四步,修改相应的USART数据发送函数为USART1:
void usart_send_data(uint8_t *send_data, uint16_t len)
{
uint16_t index = 0;
for(index = 0; index < len; index++ )
{
do
{
;
}while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
usart_data_transmit(USART1, send_data[index]);
}
}
第五步,代码修改完成后,用跳线短接开发板上J8排针口的TXD和RXD脚,用USB线插入开发板的TYPE-C口,给开发板供电后,PC上会弹出新的虚拟串口号出来,用串口调试助手进行串口数据发送接收测试,测试结果正常。
最后,讲一下这次尝试修改出现的一个小插曲,我开始是尝试修改为USART6,使用PA11和PA12引脚,测试发现USART6没有反应,在这个地方卡住了几天,查看代码没有找到任何问题,今天仔细看用户手册,发现了一个小问题,其中有讲PA11和PA12复用功能,需要注意的事项。在这里提个醒,**以后有刚好想用PA11和PA12端口的伙伴们注意,所以改为USART1后测试正常才发贴子。
|