梦溪开物 发表于 2021-5-4 18:10

【HC32F460开发板测评】(第四篇)通信端口映射测评—I2C自由映射到GPIO端口

本帖最后由 梦溪开物 于 2021-5-10 20:03 编辑

<p><span style="font-size:26px;">一、引言</span></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;评估产品简介上写道:所有串口通信(UART,I2C,SPI,CAN,I2S)端口可自由映射到64个GPIO,轻松应对PCB制板【1】,这句话具有一定吸引了,看了一些其它的资料,包括论坛上的一些帖子,很多中端的单片机也有重映射的功能【2】。这个特性的作用是广泛并且是显著的,尤其在一些需要人机交互应用中的PCB板的通信接口的布线上带来了极大的灵活性,这对于解决实际工程问题具有很大帮助。</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-size:26px;">二、测评内容</span></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;本次测评的主要内容是:I2C自由映射到GPIO端口测评。主要测评步骤为:1、运行官方原有的IC例程,熟悉HC32F460单片机的I2C外设资源;2、在例程上进行修改,将原有定义的I2C引脚映射到功能组内的其它GPIO端口,展现测试结果,完成测评。</p>

<p>&nbsp;</p>

<p><span style="font-size:20px;">2.1 HC32F460的I2C外设资源</span></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;HC32F460系列单片机具有3个独立的I2C外设模块,包括I2C1~I2C3,数据传输速度支持10Kbps~100Kbps,可以设定2个从机地址。I2C 模块的系统框图如图2.1所示,包含ACK 控制单元、仲裁单元、波特率控制单元、超时控制单元、中断控制单元、发送接收控制单元和仲裁单元,数据输出时,输出寄存器I2CDTR 通过移位寄存器I2CDSR 传送数据;数据输入时,输入寄存器I2CDRR 从移位寄存器I2CDSR 中得到输入数据【3】。I2C外设模块的硬件连接图如图2.2所示,由图中可以看到,本系列的I2C模块支持一主多从的模式。</p>

<p></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;图2.1 I2C系统框图</p>

<p>&nbsp;</p>

<p></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 图2.2&nbsp;I2C总线结构图</p>

<p>&nbsp;</p>

<p><span style="font-size:20px;">2.1 I2C自由映射GPIO端口测评</span></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;I2C自由映射GPIO端口是有前提的,它只支持64个GPIO引脚,这64个GPIO引脚支持Func32~63功能选择,其中 Func32~63主要为串行通信功能(包含 USART,SPI,I2C,I2S,CAN),所以HC32F460系列单片机的I2C必须要在功能组内任意端口进行映射【4】。 Func32~63表如图2-3所示,另外,HC32F460系列单片机的数据手册的引脚功能表太长了,这里就不进行粘贴了。</p>

<p></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 图2.3 Func32~63表</p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;打开样例程序hc32f46x_ddl_Rev1.3.0,进入example文件夹,打开里面的i2c文件夹,使用IAR打开i2c_at24c02工程,本来里面是有5个工程的,但是除了i2c_at24c02的其它四个工程都是需要2个及2个以上的HC32F460开发板,所以选择在i2c_at24c02的基础上进行测评。i2c_at24c02工程下的主程序里包含了几个宏定义,用来定义HC32F460的I2C输出引脚(HC32F460引脚图如图2.4所示),包括组数位数,如下所示:</p>

<p>&nbsp;</p>

<p>#define I2C1_SCL_PORT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (PortC)<br />
#define I2C1_SCL_PIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(Pin03)<br />
#define I2C1_SDA_PORT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (PortC)<br />
#define I2C1_SDA_PIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(Pin04)</p>

<p>&nbsp;</p>

<p></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 图2.4&nbsp;HC32F460引脚图</p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;其中一个函数可以用来实现映射功能,该函数为:PORT_SetFunc(I2C1_SCL_PORT, I2C1_SCL_PIN, Func_I2c1_Scl, Disable),可以根据HC32F460系列单片机的数据手册的引脚功能表,改变I2C1_SCL_PORT、I2C1_SCL_PIN的宏定义来选择要映射到的GPIO端口。该函数的定义如下图所示:</p>

<p>&nbsp;</p>

<p>en_result_t PORT_SetFunc(en_port_t enPort, uint16_t u16Pin, en_port_func_t enFuncSel,&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; en_functional_state_t enSubFunc)<br />
{<br />
&nbsp; &nbsp; stc_port_pfsr_field_t *PFSRx;<br />
&nbsp; &nbsp; uint8_t u8PinPos = 0u;</p>

<p>&nbsp; &nbsp; /* parameter check */<br />
&nbsp; &nbsp; DDL_ASSERT(IS_VALID_PORT(enPort));<br />
&nbsp; &nbsp; DDL_ASSERT(IS_VALID_FUNC(enFuncSel));<br />
&nbsp; &nbsp; DDL_ASSERT(IS_FUNCTIONAL_STATE(enSubFunc));</p>

<p>&nbsp; &nbsp; PORT_Unlock();</p>

<p>&nbsp; &nbsp; for (u8PinPos = 0u; u8PinPos &lt; 16u; u8PinPos ++)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (u16Pin &amp; (uint16_t)(1ul&lt;&lt;u8PinPos))<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PFSRx = (stc_port_pfsr_field_t *)((uint32_t)(&amp;M4_PORT-&gt;PFSRA0) \<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 0x40ul * enPort + 0x4ul * u8PinPos);</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* main function setting */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PFSRx-&gt;FSEL = enFuncSel;</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* sub function enable setting */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PFSRx-&gt;BFE = (Enable == enSubFunc ? Enable : Disable);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }</p>

<p>&nbsp; &nbsp; PORT_Lock();<br />
&nbsp; &nbsp; return Ok;<br />
}</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;i2c_at24c02工程主函数的功能流程图如图2.5所示,通过I2C实现对EEPROM芯片AT24C02D的读写,并通过不同LED灯的状态来判断是否读写成功,其中AT24C02D在开发板中的位置如图2.6所示:</p>

<p></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;如图2.5 i2c_at24c02工程功能流程图</p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 如图2.6&nbsp;AT24C02位置图</p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;下载例程程序到开发板,运行程序,开发板的现象为:绿灯LED1闪烁,实物结果如附件1所示;将原SCL引脚改为PD1,将原SDA引脚改为PD2,编译、下载和运行程序后,开发板的现象为:红灯LED0闪烁,如附件2所示;用杜邦线将PC4(原SCK引脚)与PD1引脚相连,PC4(原SDA引脚)与PD2相连,编译、下载和运行程序后,开发板的现象为:绿灯LED1闪烁,如附件3所示。PC4和PC5是 AT24C02的通信引脚,如图2.7所示,更换单片机的通信引脚后,原来PCB上的AT24C02与HC32F460的通信线路就不管用了,需要用杜邦线等引线将AT24C02的通信引脚引出来。红灯LED0闪烁说明I2C通信不成功,绿灯闪烁证明映射的端口通信成功,实现了本次I2C自由映射GPIO端口的测评。</p>

<p></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; 如图2.7&nbsp;AT24C02通信引脚图</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-size:26px;">三、测评心得</span></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;本次测评验证了HC32F460具有和市面上的STM32单片机相似的功能&mdash;&mdash;串口通信端口可以自由映射到GPIO口,在需要用到串口通信的应用中,用HC32F460系列单片机替换STM32的某些M4系列单片机未尝不失为一种选择,比较这具有成本上的优势。</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp;本次测评实现了将HC32F460单片机的I2C通信外设的输入输出自由映射到其它GPIO端口,其它通信外设的映射还没有尝试,至少I2C外设的映射是成功的。另外,以前没有接触更没有学会使用单片机的这个特性,所以学习起来比较有感触,重映射的功能在PCB布线上大有用处,虽然过孔和跳线可以解决很多问题,但是在PCB面积受限的时候该怎么办呢?或许重映射是一个不错的解决办法。</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-size:26px;">四、参考文献</span></p>

<p>【1】电子工程世界.华大M4内核单片机 HC32F460开发板.(2021).https://bbs.eeworld.com.cn/forum.php?mod=post&amp;action=newthread&amp;fid=265.</p>

<p>【2】电子工程世界,常见泽1.【AT-START-F403A测评】第5篇 F403A 复用和重映射.(2021).https://bbs.eeworld.com.cn/thread-1145885-1-1.html.</p>

<p>【3】华大半导体有限公司.HC32F460系列的集成电路总线I2C Rev1.1.pdf.(2021).https://www.hdsc.com.cn/cn/Index/downloadFile/modelid/65/id/48/key/1.</p>

<p>【4】华大半导体有限公司.HC32F460系列数据手册Rev1.21.pdf.(2020).https://www.hdsc.com.cn/cn/Index/downloadFile/modelid/65/id/47/key/0.</p>

<p>&nbsp;</p>

火辣西米秀 发表于 2021-5-5 21:29

<p>HC32F460系列单片机替换STM32的某些M4系列单片机确实具有成本上的优势。</p>

<p>参考文献都这么多,楼主的测评是真下功夫了</p>

freebsder 发表于 2021-5-6 16:16

<p>很详细!辛苦分享</p>

pijiu 发表于 2021-5-19 17:12

哪里可以买到啊
页: [1]
查看完整版本: 【HC32F460开发板测评】(第四篇)通信端口映射测评—I2C自由映射到GPIO端口