单片机:STM32F103RE
接口:SDIO
Keil 5工程下载链接:https://pan.baidu.com/s/1yIgUJUZcwWOL7xnwA9Rw2Q?pwd=ftxd
Wi-Fi模块电源引脚的连接方法:
【RTL8189ES读取片内MAC地址的代码】
/* 显示WiFi模块参数信息 */
void WiFi_ShowInfo(void)
{
uint32_t temp32;
WiFi_EFuseInfo efuse_info;
temp32 = WiFi_ReadReg32(1, WIFI_SYS_CFG);
printf("SYS_CFG=0x%08x\n", temp32);
printf("IC version: %c_CUT\n", 'A' + ((temp32 >> 12) & 0x0f));
WiFi_ParseEFuseTable(&efuse_info);
printf("eFuse section count: %u\n", efuse_info.efuse_sec_cnt);
printf("eFuse data size: %u\n", efuse_info.efuse_size);
printf("eFuse usage: %u%%\n", efuse_info.efuse_usage);
printf("MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_info.mac_addr[0], efuse_info.mac_addr[1], efuse_info.mac_addr[2], efuse_info.mac_addr[3], efuse_info.mac_addr[4], efuse_info.mac_addr[5]);
}
/* 读取并解析片内eFuse数据 */
void WiFi_ParseEFuseTable(WiFi_EFuseInfo *info)
{
uint8_t table[36][WIFI_EFUSE_SECTION_SIZE];
info->efuse_sec_cnt = sizeof(table) / sizeof(table[0]);
info->efuse_size = WiFi_LoadEFuseTable(table, &info->efuse_sec_cnt);
info->efuse_usage = (uint8_t)((info->efuse_size * 100) / WIFI_EFUSE_CAPACITY);
memcpy(info->mac_addr, &table[35][2], WIFI_MACADDR_LEN);
}
/* 读取全部片内eFuse数据并解压缩 */
uint16_t WiFi_LoadEFuseTable(uint8_t table[][WIFI_EFUSE_SECTION_SIZE], uint8_t *sec_num)
{
uint8_t header[2];
uint8_t i, n = 0;
uint8_t section, mask;
uint16_t addr = 0;
memset(table, 0xff, *sec_num * 8);
while ((header[0] = WiFi_ReadEFuse(addr)) != 0xff)
{
addr++;
if (addr >= WIFI_EFUSE_CAPACITY)
break; // 头部字段后没有更多数据了, 退出
if ((header[0] & 0x1f) == 0x0f)
{
/* 扩展头部 */
// 低5位为01111, 说明头部字段有两个字节
header[1] = WiFi_ReadEFuse(addr);
addr++;
if (addr >= WIFI_EFUSE_CAPACITY)
break;
if ((header[1] & 0x0f) != 0x0f)
{
// header[1][7:4] -> section[6:3]
// header[1][3:0] -> mask
// header[0][7:5] -> section[2:0]
// header[0][4:0] -> 01111
section = ((header[1] & 0xf0) >> 1) | (header[0] >> 5);
mask = header[1] & 0x0f;
}
else
{
/* 无效头部 (数据出错) */
continue;
}
}
else
{
/* 普通头部 */
// header[0][7:4] -> section
// header[0][3:0] -> mask
section = header[0] >> 4;
mask = header[0] & 0x0f;
}
if (n < section + 1)
n = section + 1;
for (i = 0; i < WIFI_EFUSE_SECTION_SIZE; i += 2)
{
if ((mask & 1) == 0)
{
if (section < *sec_num)
table[section][i] = WiFi_ReadEFuse(addr);
addr++;
if (addr >= WIFI_EFUSE_CAPACITY)
break;
if (section < *sec_num)
table[section][i + 1] = WiFi_ReadEFuse(addr);
addr++;
if (addr >= WIFI_EFUSE_CAPACITY)
break;
}
mask >>= 1;
}
}
*sec_num = n;
return addr;
}
/* 读取单字节片内eFuse数据 */
uint8_t WiFi_ReadEFuse(uint16_t addr)
{
uint8_t temp;
WiFi_WriteReg(1, WIFI_EFUSE_CTRL + 1, addr & 0xff); // 地址低8位
temp = WiFi_ReadReg(1, WIFI_EFUSE_CTRL + 2);
WiFi_WriteReg(1, WIFI_EFUSE_CTRL + 2, (temp & 0xfc) | ((addr >> 8) & 0x03)); // 地址高2位
temp = WiFi_ReadReg(1, WIFI_EFUSE_CTRL + 3);
WiFi_WriteReg(1, WIFI_EFUSE_CTRL + 3, temp & ~_BV(7)); // 清除read-ready标志
while ((WiFi_ReadReg(1, WIFI_EFUSE_CTRL + 3) & _BV(7)) == 0); // 等待read-ready置1
HAL_Delay(1); // 延时, 防止读取错误
temp = WiFi_ReadReg(1, WIFI_EFUSE_CTRL); // 读数据
return temp;
}
【RTL8189ES固件下载代码】
先EnableCard,再DownloadFirmware。
/* 执行WiFi模块上电操作 */
void WiFi_EnableCard(void)
{
uint8_t temp;
uint16_t temp16;
// Call power on sequence to enable card
WiFi_WriteReg(1, WIFI_RSV_CTRL, 0); // unlock ISO/CLK/Power control register
// RTL8188E_TRANS_CARDDIS_TO_CARDEMU
temp = WiFi_ReadReg(1, 0x86);
WiFi_WriteReg(1, 0x86, temp & ~_BV(0)); // set SDIO suspend local register
while ((WiFi_ReadReg(1, 0x86) & _BV(1)) == 0); // wait power state to suspend
temp = WiFi_ReadReg(1, 0x10005);
WiFi_WriteReg(1, 0x10005, temp & ~(_BV(3) | _BV(4))); // enable WL suspend
// RTL8188E_TRANS_CARDEMU_TO_ACT
while ((WiFi_ReadReg(1, 0x10006) & _BV(1)) == 0); // wait till power ready
temp = WiFi_ReadReg(1, 0x10002);
WiFi_WriteReg(1, 0x10002, temp & ~(_BV(0) | _BV(1))); // reset BB
temp = WiFi_ReadReg(1, 0x10026);
WiFi_WriteReg(1, 0x10026, temp | _BV(7)); // schmit trigger
temp = WiFi_ReadReg(1, 0x10005);
temp &= ~_BV(7);
WiFi_WriteReg(1, 0x10005, temp); // disable HWPDN (control by DRV)
temp &= ~(_BV(4) | _BV(3));
WiFi_WriteReg(1, 0x10005, temp); // disable WL suspend
temp |= _BV(0);
WiFi_WriteReg(1, 0x10005, temp); // polling until return 0
while (WiFi_ReadReg(1, 0x10005) & _BV(0));
temp = WiFi_ReadReg(1, 0x10023);
WiFi_WriteReg(1, 0x10023, temp & ~_BV(4)); // LDO normal mode
// Enable power down and GPIO interrupt
temp16 = WiFi_ReadReg16(1, WIFI_APS_FSMCO);
WiFi_WriteReg16(1, WIFI_APS_FSMCO, temp16 | WIFI_APS_FSMCO_ENPDN);
// Enable MAC DMA/WMAC/SCHEDULE/SEC block
temp16 = WiFi_ReadReg16(1, WIFI_CR);
temp16 |= WIFI_CR_HCI_TXDMA_EN | WIFI_CR_HCI_RXDMA_EN | WIFI_CR_TXDMA_EN | WIFI_CR_RXDMA_EN;
temp16 |= WIFI_CR_PROTOCOL_EN | WIFI_CR_SCHEDULE_EN | WIFI_CR_ENSEC | WIFI_CR_CALTMR_EN;
WiFi_WriteReg16(1, WIFI_CR, temp16);
}
/* 下载固件 */
void WiFi_DownloadFirmware(void)
{
int curr, page;
int i, j;
uint8_t value;
uint32_t value32;
WiFi_FirmwareHeader *header = (WiFi_FirmwareHeader *)firmware_mp_8188e_t_fw_nic;
// 显示固件信息
printf("Firmware version: 0x%02x\n", header->version);
printf("Firmware subversion: 0x%02x\n", header->subversion);
printf("Firmware signature: 0x%04x\n", header->signature);
printf("Firmware release time: %02x-%02x %02x:%02x\n", header->month, header->date, header->hour, header->minute);
// 复位8051内核
value = WiFi_ReadReg(1, WIFI_MCUFWDL);
if (value & WIFI_MCUFWDL_RAM_DL_SEL)
{
WiFi_WriteReg(1, WIFI_MCUFWDL, 0x00);
WiFi_Reset8051();
}
// 开始下载固件
value = WiFi_ReadReg(1, WIFI_MCUFWDL);
WiFi_WriteReg(1, WIFI_MCUFWDL, value | WIFI_MCUFWDL_EN); // MCU firmware download enable
value = WiFi_ReadReg(1, WIFI_MCUFWDL + 2);
WiFi_WriteReg(1, WIFI_MCUFWDL + 2, value & 0xf7); // 8051 reset
// 重置校验值
value = WiFi_ReadReg(1, WIFI_MCUFWDL);
WiFi_WriteReg(1, WIFI_MCUFWDL, value | WIFI_MCUFWDL_CHKSUM_RPT);
// 发送固件数据
page = 0;
for (i = 32; i < sizeof(firmware_mp_8188e_t_fw_nic); i += curr)
{
// 设置页号
value = WiFi_ReadReg(1, WIFI_MCUFWDL + 2);
value &= ~WIFI_BYTE2(WIFI_MCUFWDL_PAGE);
value |= WIFI_BYTE2((page << WIFI_MCUFWDL_PAGE_Pos) & WIFI_MCUFWDL_PAGE);
WiFi_WriteReg(1, WIFI_MCUFWDL + 2, value);
page++;
// 计算当前页的数据量
curr = sizeof(firmware_mp_8188e_t_fw_nic) - i;
if (curr > WIFI_FW_8188E_END_ADDRESS - WIFI_FW_8188E_START_ADDRESS + 1)
curr = WIFI_FW_8188E_END_ADDRESS - WIFI_FW_8188E_START_ADDRESS + 1;
// 发送当前页的数据
for (j = 0; j < curr; j++)
WiFi_WriteReg(1, WIFI_FW_8188E_START_ADDRESS + j, firmware_mp_8188e_t_fw_nic[i + j]);
printf("Downloaded %d bytes of firmware (%d-%d)\n", j, i, i + j - 1);
}
// 等待固件校验完毕
while ((WiFi_ReadReg32(1, WIFI_MCUFWDL) & WIFI_MCUFWDL_CHKSUM_RPT) == 0);
// 结束下载固件
value = WiFi_ReadReg(1, WIFI_MCUFWDL);
WiFi_WriteReg(1, WIFI_MCUFWDL, value & ~WIFI_MCUFWDL_EN); // MCU firmware download disable
WiFi_WriteReg(1, WIFI_MCUFWDL + 1, 0x00); // reserved for fw extension
// 启动固件
value32 = WiFi_ReadReg32(1, WIFI_MCUFWDL);
value32 |= WIFI_MCUFWDL_RDY;
value32 &= ~WIFI_MCUFWDL_WINTINI_RDY;
WiFi_WriteReg32(1, WIFI_MCUFWDL, value32);
WiFi_Reset8051();
while ((WiFi_ReadReg32(1, WIFI_MCUFWDL) & WIFI_MCUFWDL_WINTINI_RDY) == 0);
printf("Firmware is successfully downloaded\n");
}
【程序运行结果】
STM32F103RE RTL8189ES
[Clock] freq=400.0kHz, requested=400.0kHz, divider=178
Ignored CMD5 CRC error
RESPCMD63, RESP1_90fc0000
Ignored CMD5 CRC error
RESPCMD63, RESP1_90fc0000
Number of I/O Functions: 1
Memory Present: 0
Relative Card Address: 0x0001
Card selected. RESP1_00001e00
[Clock] freq=24000.0kHz, requested=25000.0kHz, divider=1
[CIS] func=0, ptr=0x00001000
Manufacturer Code: 0x024c
Manufacturer Information: 0x8179
Card Function Code: 0x0c
System Initialization Bit Mask: 0x00
Maximum Block Size: 8
Maximum Transfer Rate Code: 0x32
[CIS] func=1, ptr=0x00001100
Card Function Code: 0x0c
System Initialization Bit Mask: 0x00
Maximum Block Size: 512
SYS_CFG=0xc4403710
IC version: D_CUT
eFuse section count: 36
eFuse data size: 160
eFuse usage: 62%
MAC Address: 30:1B:97:41:C6:65
Firmware version: 0x1c
Firmware subversion: 0x00
Firmware signature: 0x88e1
Firmware release time: 05-05 14:27
Downloaded 4096 bytes of firmware (32-4127)
Downloaded 4096 bytes of firmware (4128-8223)
Downloaded 4096 bytes of firmware (8224-12319)
Downloaded 2942 bytes of firmware (12320-15261)
Firmware is successfully downloaded
SDIO->STA=0x00000000
【兼容最新的只有AC6编译器的Keil 5.38a版本】
1. 在ST官网下载en.stm32cubef1.zip(V1.8.0版本),将STM32Cube_FW_F1_V1.8.0/Drivers/CMSIS/Include/cmsis_armclang.h解压到项目的STM32F1xx_HAL_Driver目录中。(https://pan.baidu.com/s/1_tM1bBlx2Brg9mDPDWdVUg?pwd=sx6t)
2. 在项目属性中Target选项卡的ARM Compiler选择Use default compiler version 6,然后在C/C++ (AC6)选项卡的Define文本框中定义宏__FILE_INCOMPLETE=1。
3. 把WiFi.h里面的typedef __packed struct改为typedef __PACKED_STRUCT。
4. 把common.c中的#pragma import(__use_no_semihosting)改为:
// 禁用半主机模式 (不然调用printf就会进HardFault)
#ifdef __clang__
__asm(".global __use_no_semihosting\n\t");
#else
#pragma import(__use_no_semihosting)
#endif