STM32CubePrg API(example project)

前言

這邊因協助客戶需要把ST更新軟體鑲嵌在客戶自訂的軟體上面,因此這邊就深挖STM32CubePrg原始的接口,並把他逐步整合到客戶指定的樣板上,基本上架構是以STM32CubePrg為主,這邊interFace會以USB DFU為主介紹(HID等客製化方式不再此介紹範圍)

STM32CubePrg Project 結構

這邊主要會拆成2部分一部份是設定連接MCU的資料與連接上更新的interface,首先我們會先到以下路徑開啟project

該資料夾是在安裝STM32CubePrg後就會存在C曹個人資料夾中

第一部分是Set MCU更新資料
int main()
{
	int ret = 0;
    const char* loaderPath = "./.";
    displayCallBacks vsLogMsg;

    /* Set device loaders path that contains FlashLoader and ExternalLoader folders*/
	setLoadersPath(loaderPath);

    /* Set the progress bar and message display functions callbacks */
	vsLogMsg.logMessage = DisplayMessage;
	vsLogMsg.initProgressBar = InitPBar;
	vsLogMsg.loadBar = lBar;
    setDisplayCallbacks(vsLogMsg);

    /* Set DLL verbosity level */
    setVerbosityLevel(verbosityLevel = VERBOSITY_LEVEL_1);
    ret = USB_Example();
    std::cout << "\n" << "Press enter to continue...";
	std::cin.get() ;
	return ret;
}

這邊幾個注意事項這邊已USB為例

  • 如果使用SW DFU模式,setLoadersPath這個就不是必須,因為setLoadersPath是在讓系統知道該顆MCU的架構,如果是寫SW DFU這部分會事寫在MCU上會有反饋資料
  • 如果使用System bootloader在setLoadersPath這塊就必須要注意,連結的MCU資料必須在該目錄底下

該路徑事需要指向Database內,確認需要被更新MCU為哪顆保留一個該檔案即可,也可以先使用ST-LINKV2連線該資料會出現在STM32CubePrg Information上,可以不用全部都採納,但指向路徑必須設定正確

更新主軸API(USB為例)

再來是選擇InterFace去完成,這邊選用USB

int USB_Example(void) {

	logMessage(Title, "\n+++ USB Bootloader Example +++\n\n");


	generalInf* genInfo;
	dfuDeviceInfo *dfuList;

	int getDfuListNb = getDfuDeviceList(&dfuList, 0xdf11, 0x0483);

	if (getDfuListNb == 0)
	{
		logMessage(Error, "No USB DFU available\n");
		return 0;
	}
	else {
		logMessage(Title, "\n------------- USB DFU List --------------\n");
		for (int i = 0; i < getDfuListNb; i++)
		{
			logMessage(Normal, "USB Port %d \n",i);
			logMessage(Info, "	USB index   : %s \n", dfuList[i].usbIndex);
			logMessage(Info, "	USB SN      : %s \n", dfuList[i].serialNumber);
			logMessage(Info, "	DFU version : 0x%02X ", dfuList[i].dfuVersion);
		}
		logMessage(Title, "\n-----------------------------------------\n\n");
	}

	/* Target connect, choose the adequate USB port by indicating its index that is already mentioned in USB DFU List above */
	int usbConnectFlag = connectDfuBootloader(dfuList[0].usbIndex); 
	if (usbConnectFlag != 0) 
	{
		disconnect();
		return 0;
	}
	else {
		logMessage(GreenInfo, "\n--- Device Connected --- \n");
	}

	/* Display device informations */
	genInfo = getDeviceGeneralInf();
	logMessage(Normal, "\nDevice name : %s ", genInfo->name);
	logMessage(Normal, "\nDevice type : %s ", genInfo->type);
	logMessage(Normal, "\nDevice CPU  : %s \n", genInfo->cpu);

	/* Download File + verification */
#ifdef _WIN32
	const wchar_t* filePath = L"../test file/VL53L7_USB_HID_PresV4_ALS_V519_withDFU.BIN";
#else
	const wchar_t* filePath = L"../api/test file/data.hex";
#endif

	unsigned int isVerify = 1; //add verification step	
	unsigned int isSkipErase = 0; // no skip erase
	int downloadFileFlag = downloadFile(filePath, 0x08000000, isSkipErase, isVerify, L"");
	if (downloadFileFlag != 0)
	{
		logMessage(Normal, "\ndownloadFileFlag :%d\n", downloadFileFlag);
		disconnect();
		return 0;
	}
	

	/* Process successfully Done */
	disconnect();
	deleteInterfaceList();
	return 1;
}

這邊基本上做FW download Function就是downloadFile 其他都是在檢測是否有正常連線到,這邊有測試該Function是否能帶入絕對路徑,結果是部分會fail,可能在內部最佳化部分絕對路徑相對資源所以會建議使用相對路徑去執行

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart