Пример #1
0
        /******************************************************************************
        * 向下位机烧写固件
        ******************************************************************************/
        private async Task UploadFile(string portName, uint baudRate,
                                      byte[] bin, uint address, uint jumpAddress)
        {
            /* 获得页的大小 */
            uint psize = uint.Parse(cbPSize.SelectedItem as string);

            /* 初始化ST boot对象 */
            using (var uc = new STBoot()) {
                /* 打开设备 */
                uc.Open(portName, baudRate);
                /* 通信初始化 */
                await uc.Initialize();

                /* 更新状态栏信息 */
                UpdateStatus(false, string.Format("Connected: Ver: {0}, PID: 0x{1:X4}",
                                                  uc.Version, uc.ProductID));
                /* 延时一会儿,使状态的信息可以被看见 */
                await Task.Delay(500);

                /* 擦除信息 */
                UpdateStatus(false, "Erasing...");

                /* 全局擦除是否被勾选 */
                if (cbxErase.Checked)
                {
                    await uc.GlobalErase();
                }
                else
                {
                    /* 擦除操作 */
                    for (uint i = 0; i < bin.Length; i += psize)
                    {
                        /* 擦除页 */
                        await uc.ErasePage((i + address - 0x08000000) / psize);

                        /* 更新精度条 */
                        UpdateProgress((int)i * 100 / bin.Length);
                    }
                }

                /* 状态栏显示 编程 提示信息 */
                UpdateStatus(false, "Programming...");
                /* 创建进度报告对象 */
                var p = new Progress <STBootProgress>(UpdateProgress);
                /* 写Flash */
                await uc.WriteMemory(address, bin, 0, bin.Length, p,
                                     CancellationToken.None);

                /* 烧写成功 */
                UpdateStatus(false, string.Format("Success: {0} bytes written",
                                                  bin.Length));

                /* 跳转到用户程序地址! */
                await uc.Jump(jumpAddress);

                /* 结束通信 */
                uc.Close();
            }
        }
Пример #2
0
        /* upload a binary image to uC */
        private async Task UploadFile(string portName, uint baudRate,
                                      byte[] bin, uint address, uint jumpAddress)
        {
            /* get page size */
            uint psize = uint.Parse(cbPSize.SelectedItem as string);

            /* create new programming interface object */
            using (var uc = new STBoot()) {
                /* open device */
                uc.Open(portName, baudRate);
                /* initialize communication */
                await uc.Initialize();

                /* update the status */
                UpdateStatus(false, string.Format("Connected: Ver: {0}, PID: 0x{1:X4}",
                                                  uc.Version, uc.ProductID));
                /* give some chance see the message */
                await Task.Delay(500);

                /* apply new message */
                UpdateStatus(false, "Erasing...");

                /* checked? */
                if (cbxErase.Checked)
                {
                    await uc.GlobalErase();
                }
                else
                {
                    /* erase operation */
                    for (uint i = 0; i < bin.Length; i += psize)
                    {
                        /* erase page */
                        await uc.ErasePage((i + address - 0x08000000) / psize);

                        /* update progress bar */
                        UpdateProgress((int)i * 100 / bin.Length);
                    }
                }

                /* apply new message */
                UpdateStatus(false, "Programming...");
                /* progress reporter */
                var p = new Progress <STBootProgress>(UpdateProgress);
                /* write memory */
                await uc.WriteMemory(address, bin, 0, bin.Length, p,
                                     CancellationToken.None);

                /* update the status */
                UpdateStatus(false, string.Format("Success: {0} bytes written",
                                                  bin.Length));

                /* go! */
                await uc.Jump(jumpAddress);

                /* end communication */
                uc.Close();
            }
        }
Пример #3
0
        private async void timer1_Tick(object sender, EventArgs e)
        {
            // Dont check if write is in process
            if ((WriteInProcess == false) && connected)
            {
                /* create new programming interface object */
                using (var uc = new STBoot())
                {
                    /* get port name */
                    string pName = (string)cbPorts.SelectedItem;
                    try
                    {
                        //UpdateStatus(false, "Poll ID...");

                        /* open device */
                        uc.Open(portName, baudRate);

                        /* try to handshake */
                        if (await uc.GetID())
                        {
                            UpdateStatus(false, "Unit Detected");
                            connected         = true;
                            bOpenFile.Enabled = true;
                        }
                        else
                        {
                            UpdateStatus(false, "No Unit Detected");
                            connected         = false;
                            bOpenFile.Enabled = false;
                            bWrite.Enabled    = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        /* set message */
                        // UpdateStatus(true, ex.Message);
                        UpdateStatus(false, "Port Error Occured");
                        connected = false;
                    }
                    finally
                    {
                        uc.Close();
                    }

                    // On Screen ID
                    lbUnitId.Text = uc.ProductID;
                }
            }
            else
            {
                if (WriteInProcess)
                {
                    lbUnitId.Text = "Write In Process";
                }
            }
        }
Пример #4
0
        /* connect to unit */
        private async Task Connect(string portName, uint baudRate)
        {
            /* get page size */
            // uint psize = uint.Parse(cbPSize.SelectedItem as string);
            uint psize = pageSize;

            /* create new programming interface object */
            using (var uc = new STBoot())
            {
                try
                {
                    /* open device */
                    uc.Open(portName, baudRate);
                    /* initialize communication */
                    //await uc.Initialize();
                    /* update the status */
                    //UpdateStatus(false, string.Format("Connected: Ver: {0}, PID: 0x{1:X4}",
                    //	uc.Version, uc.ProductID));
                    /* give some chance see the message */
                    //await Task.Delay(500);

                    /* apply new message */
                    UpdateStatus(false, "Connecting...");

                    /* progress reporter */
                    var p = new Progress <STBootProgress>(UpdateProgress);

                    /* connect */
                    await uc.Connect();

                    /* update the status */
                    //UpdateStatus(false, string.Format("Connected"));

                    timer1.Enabled = true;
                    bWrite.Enabled = false;
                    WriteInProcess = false;
                    connected      = true;
                }
                catch
                {
                    timer1.Enabled    = false;
                    bWrite.Enabled    = false;
                    bOpenFile.Enabled = false;
                    WriteInProcess    = false;
                    connected         = false;
                }
                finally
                {
                    /* end communication */
                    uc.Close();
                }
            }
        }
Пример #5
0
        /******************************************************************************
        * 跳转至用户代码
        ******************************************************************************/
        private async Task Jump(uint address)
        {
            /* 初始化ST boot对象 */
            using (var uc = new STBoot()) {
                /* 打开设备 */
                uc.Open(portName, baudRate);
                /* 通信初始化 */
                await uc.Initialize();

                /* 跳转! */
                await uc.Jump(address);

                /* 结束通信 */
                uc.Close();
            }
        }
Пример #6
0
        /* execute code */
        private async Task Jump(uint address)
        {
            /* create new programming interface object */
            using (var uc = new STBoot()) {
                /* open device */
                uc.Open(portName, baudRate);
                /* initialize communication */
                await uc.Initialize();

                /* go! */
                await uc.Jump(address);

                /* end communication */
                uc.Close();
            }
        }
Пример #7
0
        /* upload a binary image to uC */
        private async Task UploadFile(string portName, uint baudRate,
                                      byte[] bin, uint address, uint jumpAddress)
        {
            /* get page size */
            //uint psize = uint.Parse(cbPSize.SelectedItem as string);
            uint psize = pageSize;

            /* create new programming interface object */
            using (var uc = new STBoot()) {
                /* open device */
                uc.Open(portName, baudRate);

                ///* initialize communication */
                //await uc.Initialize();
                ///* update the status */
                //UpdateStatus(false, string.Format("Connected: Ver: {0}, PID: 0x{1:X4}",
                //    uc.Version, uc.ProductID));
                ///* give some chance see the message */
                //await Task.Delay(500);

                /* apply new message */
                UpdateStatus(false, "Erasing...");

                await uc.EraseAll();

                /* apply new message */
                UpdateStatus(false, "Programming...");

                /* progress reporter */
                var p = new Progress <STBootProgress>(UpdateProgress);

                /* write memory */
                await uc.WriteMemory(address, bin, 0, bin.Length, p,
                                     CancellationToken.None, pageSize);

                /* update the status */
                UpdateStatus(false, string.Format("Success: {0} bytes written",
                                                  bin.Length));

                ///* go! */
                //await uc.Jump(jumpAddress);

                /* end communication */
                uc.Close();
            }
        }
Пример #8
0
        /* execute code */
        private async Task Jump(uint address)
        {
            /* create new programming interface object */
            using (var uc = new STBoot()) {
                /* open device */
                uc.Open(portName, baudRate);
                ///* initialize communication */
                //await uc.Initialize();
                /* go! */
                await uc.Jump(address);

                /* end communication */
                uc.Close();
                bWrite.Enabled = false;
                WriteInProcess = false;
                timer1.Enabled = false;
                lbUnitId.Text  = "Update Complete";
                UpdateStatus(false, "Update Complete");
            }
        }
Пример #9
0
        private void cmdMCUEnterBootState_Click(object sender, EventArgs e)
        {
            /* get port name */
            string pName = (string)cbPorts.SelectedItem;
            /* get baud rate */
            uint bauds = uint.Parse((string)cbBauds.SelectedItem);

            if (pName == null || pName == "")
            {
                return;
            }

            var uc = new STBoot();

            /* open device */
            uc.Open(pName, bauds);

            /*Reset the chip via RTS and DTR --> DTR-Signal must be INVERTED!*/
            uc.STM32InitBoot();

            uc.Close();
        }
Пример #10
0
        /* write clicked */
        private async void bWrite_Click(object sender, EventArgs e)
        {
            /* binary file */
            byte[] bin;
            /* bootloader class instance */
            STBoot stb = new STBoot();

            /* disable button */
            bWrite.Enabled = false;
            /* reset progress */
            UpdateProgress(0);
            /* reset status bar */
            UpdateStatus(false, "");

            /* read file */
            try {
                /* try to open file */
                var s = new FileStream(fileName, FileMode.Open,
                                       FileAccess.Read);
                /* prepare buffer */
                bin = new byte[s.Length];
                /* read file contents */
                await s.ReadAsync(bin, 0, bin.Length);

                /* close file */
                s.Close();
                /* error during read? */
            } catch (Exception) {
                /* set message */
                UpdateStatus(true, "Error: Unable to read file");
                /* restore button operation */
                bWrite.Enabled = true;
                /* not much to do next */
                return;
            }

            /* perform the operation */
            try {
                /* open the port */
                stb.Open(portName, baudRate);
                /* initialize communication */
                await stb.Initialize();

                /* format message */
                var s = string.Format("Connected: Ver: {0}, PID: 0x{1:X4}",
                                      stb.Version, stb.ProductID);
                /* prepare message */
                UpdateStatus(false, s);

                /* accessing flash requires memory erase */
                if (cbxFlash.Checked)
                {
                    /* erase all necessary pages */
                    for (uint i = 0; i < (bin.Length + 255) / 256; i++)
                    {
                        await stb.ErasePage(i + page);
                    }
                }

                /* progress reporter */
                var p = new Progress <STBootProgress>(UpateProgress);
                /* write memory */
                await stb.WriteMemory(address, bin, 0, bin.Length, p,
                                      CancellationToken.None);

                /* set message */
                UpdateStatus(false, string.Format("Success: {0} bytes written",
                                                  bin.Length));

                /* go! */
                await stb.Jump(address);

                /* catch all the exceptions here */
            } catch (Exception ex) {
                /* set exception message */
                UpdateStatus(true, "Error: " + ex.Message);
                /* dispose of port */
            } finally {
                /* close port */
                stb.Close();

                /* re-enable button */
                bWrite.Enabled = true;
                /* set focus */
                bWrite.Focus();
            }
        }