5.7 Read chip ID through hardware UART
1.Applicable device
The applicable device is PW200/PW300.
PWX1 does not support UART to read chip ID when writing at current document time, and UART function will be opened in the future.
2.Preparatory work
2.1 Upgrade firmware
Menu bar help, update software and firmware, upgrade firmware to 1.01.31 or above.
2.2 Test
Special note: Please select the erase mode as No Erase, the interface level as 5V, and the option word mode as No Operation to No Operation, so as to avoid accidentally pressing the button and causing the program of the chip to be overwritten!
After getting the device PW200/PW300, use the PowerWriter client to connect the devices:
- Select the chip model to be read.
- If prompted for firmware update, update the firmware to > 1.01.31, refer to 8.2.1
- Connect chips with reference to the chip connection diagram.
- Confirm whether the chip wiring is correct (indicating that the target chip is connected)
As shown in the figure below:
After the connection is successful, you can use the ID reading function of PowerWriter client to try to read the ID, as shown below:
2.3 Enable AT function
Turn on the AT function (UART) in the menu bar, setting and device preferences.
2.4 Load project to devices
After selecting the chip, load the blank project into the device, as shown below:
Because only the chip ID needs to be read, only an empty project is needed, and there is no need to add data.
3.Open source AT API access
3.1 PowerWriter Open source AT code
Reference document address:4.3 AT open source API | PowerWriter文档中心,Download the source package, as shown in the following figure:
After downloading and decompressing, you can see the following directory, in which the source directory has cross-platform API source code:
Powerwriter_at_core is a cross-platform implementation, which can run on almost all platforms only by adapting serial port transceiver and timestamp implementation interface. Currently, the demo provided is windows version, and other platforms have not provided demos yet, so you can refer to powerwriter_at_samples.c for implementation.
3.2 Interface integration
3.2.1 Connecting target chip
According to the above description and documentation manual, directly integrate powerwriter_at_core, and make API calls with reference to sample. Call powerwriter_at_target_connect to send a command requesting to connect to the target chip, and then get the connection status of the target chip through powerwriter_at_target_status. If the connection to the target chip is successful, you can proceed to the next step for reference. If it fails, try again. If the chip cannot be connected after timeout, please check the wiring to confirm whether the chip model is correct, and contact technical support if necessary.
The sequence of connection requests is (when unencrypted):
//50 57 41 54 18 00 00 00 64 00 00 00 04 00 00 00 00 00 00 00 4c 29 75 98
// 固定头部 | 帧长度 |0x64为请求连接|0x04命令长度|命令保留值 | crc32 |
The return value is ATCmdStatusOK or ATCmdStatusError (when unencrypted):
/* OK 命令的帧结构为 */
//50 57 41 54 18 00 00 00 9b ff ff 7f 04 00 00 00 00 00 00 00 93 10 0a 7a
// 固定头部 | 帧长度 |ATCmdStatusOK| 0x04命令长度| 命令保留值 | crc32 |
/* ERROR 命令的帧结构为 */
//50 57 41 54 18 00 00 00 9c ff ff 7f 04 00 00 00 xx xx xx xx xx xx xx xx
// 固定头部 | 帧长度 |ATCmdStatusError| 0x04命令长度| 错误码 | crc32 |
Because it takes time to connect chips, different PCBs and different operating environments, the length of returned events varies, so there needs to be a timeout waiting time, which is generally set to 5-10 seconds (In general, tens of ms will return success ). If it fails to return, try several times more, and after several retries, it still gives an error, which means that connecting chips has failed, so it needs to be checked and technical support is requested if necessary.
C language reference code is as follows:
bool powerwriter_at_connect_target(S_ATChannel *channel)
{
bool connect = false;
powerwriter_at_log(LOGD, ">>>Target online bechmark ...\r\n");
/* Init target connnect */
if (!powerwriter_at_target_connect(channel))
{
powerwriter_at_log(LOGE, "[%08X]:powerwriter initial connect target failure ...\r\n",
powerwriter_at_last_error(channel));
return false;
}
powerwriter_at_log(LOGD, "powerwriter initial connect target successfully ...\r\n");
/* Get target status */
uint32_t ts = GetSystemTick();
uint32_t te = ts;
powerwriter_at_log(LOGD, "Target connecting >");
do
{
ATSleep(50);
if (powerwriter_at_target_status(channel))
{
powerwriter_at_log(LOGN, "powerwriter target connected...");
connect = true;
break;
}
powerwriter_at_log(LOGN, ">>");
te = GetSystemTick();
} while (te - ts < 10000);
powerwriter_at_log(LOGN, "\r\n");
return connect;
}
3.2.2 Read the target chip ID
Call powerwriter_at_target_ID to send the command to read the target chip ID. If the reading is normal, it will return the command of ATCmdRspTargetChipID, and if it fails, it will return the command of ATCmdStatusError (see the previous section for the structure).
Read the target chip ID (when unencrypted):
//50 57 41 54 18 00 00 00 66 00 00 00 04 00 00 00 00 00 00 00 d3 b7 4e 74
// 固定头部 | 帧长度 |0x66为读取ID | 0x04命令长度 | 命令保留值 | crc32 |
The return value is ATCmdRspTargetChipID or ATCmdStatusError (when unencrypted):
#define PW_TARGET_ID_MAX 16 // Target chip ID MAX size
typedef struct S_ATCmdRspTargetChipID
{
uint8_t m_CIDSize; // Target chip ID size
uint8_t m_CIDData[PW_TARGET_ID_MAX]; // Target chip ID data
} S_ATCmdRspTargetChipID;
/* ATCmdRspTargetChipID */
/*
50 57 41 54 25 00 00 00 67 00 00 00 11 00 00 00 10 32 30 53 41 13 33 32 33 33
固定头部 | 帧长度 |ATCmdRspTargetChipID | 0x11 命令长度|ID 长度|ID 值
22 6e 10 78 b1 01 56 84 6b 94 1e
| CRC32 |
*/
/* Get target id */
S_ATCmdRspTargetChipID m_target_id;
if (!powerwriter_at_target_id(channel, &m_target_id))
{
powerwriter_at_log(LOGE, "[%08X]:powerwriter get target id failure ...\r\n",
powerwriter_at_last_error(channel));
return false;
}
object_print(m_target_id.m_CIDData, m_target_id.m_CIDSize, "Target chip id");
powerwriter_at_log(LOGD, "powerwriter get target id successfully ...\r\n");
3.2.3 Reset chip
Because the operation of the chip will be suspended when reading the chip ID, after reading the CID, it is necessary to reset the function of the product, power off the equipment, or manually restart the equipment to restore the function of the product.
Reset target chip (when unencrypted):
//50 57 41 54 18 00 00 00 72 00 00 00 04 00 00 00 03 00 00 00 be ce e5 69
// 固定头部 | 帧长度 |0x72为复位芯片| 0x04命令长度 | E_resetType | crc32 |
The return value is ATCmdStatusOK or ATCmdStatusError (when unencrypted), refer to the error code when connecting the chip:
// 复位类型
typedef enum E_resetType
{
HWKeepLow, //硬件复位引脚保持为低
HWNoneReset, //硬件引脚为高阻态
HWReset, //执行硬件复位
HWCoreReset, //执行硬件复位和内核复位
CoreReset, //执行内核复位
VectorReset, //执行向量复位
POROnly, //执行上电重启(必须由PowerWriter 供电才可以)
PORAndRelease, //执行上电复位并释放端口(必须由PowerWriter 供电才可以)
_TARGET_RESET_ = PW_ENUM_MAX
}E_resetType;
/* Reset Target */
#ifdef AT_ONLINE_RESET_TARGET_SAMPLE
if (!powerwriter_at_target_reset(channel, HWCoreReset))
{
powerwriter_at_log(LOGE, "[%08X]:powerwriter AT reset target failure...\r\n",
powerwriter_at_last_error(channel));
return false;
}
powerwriter_at_log(LOGD, "powerwriter AT reset target passed ...\r\n");
/* connect target again */
if (!powerwriter_at_connect_target(channel)) {
return false;
}
#endif
Special note: If the reset operation is performed, the resources of PowerWriter will be released so that the target chip can be restarted.
4.Wiring picture
4.1 System wiring schematic reference
4.2 PowerWriter device interface reference
See the official online manual of PowerWriter for details:2.1 Characteristic | PowerWriter文档中心