Using flashx io_ioctl FLASH_IOCTL_SWAP_FLASH_AND_RESET for bootloader

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Using flashx io_ioctl FLASH_IOCTL_SWAP_FLASH_AND_RESET for bootloader

5,854 次查看
nancyb
Contributor III

I am implementing a bootloader for a K60FX512VLQ12 processor. For my initial effort I used the swap_demo code from application note AN4533. I successfully implemented a bootloader in a non-MQX environment. Next I sought to incorporate the lessons learned in my application that uses MQX 3.8.1. I could not even access the swap status with MQX; I assume MQX has enabled protection for these registers.

Then I found an MQX example demo dual_flash_update for a M51 processor that I adapted to my K60 processor. I can successfully update program flash bank 1 with the code from bank 0 but when I execute

_io_ioctl(flash_hdl,  FLASH_IOCTL_SWAP_FLASH_AND_RESET, NULL);

the swap and reset does not occur.

Is this capability supported for my processor and MQX version?

The swap demo code from AN4533 stepped through the swap process and it was easy to debug each step, including the erasure of bank 1 and the actual swap step. It's not clear to me what actually went wrong with my MQX example and how to proceed, so I thought I would check to see if there are any known problems or issues.

Also, is it possible to disable whatever MQX protection settings that are preventing me from implementing the bootloader developed using the AN4533 example in the context of MQX?

18 回复数

3,630 次查看
davidwhite
Contributor II

Nancy,

This is probably too late to help you but here goes anyway.

I Revised MQX 4.0 BSP to support a new flashx ioctl(), namely FLASH_IOCTL_SWAP_FLASH.This  gives you support of the swap state transitions for swapping the active flash bank. Called like this :   ioctl(flash_file, FLASH_IOCTL_SWAP_FLASH, &swap_status). The "swap_status"  parameter is used for I/O with the low level swap driver: sends the swap control code and returns the swap status. See UpdateFirmwareDemo.c  in attached code for details.

3,630 次查看
artur73
Contributor I

Hi David,

Thanks for sharing your project. Sadly, I can not compile it, since FLASH_IOCTL_SWAP_FLASH is missing in my MQX 4.0 build. How did you added it? Can you share your library too?

Thanks,

Artur

0 项奖励
回复

3,630 次查看
davidwhite
Contributor II

Hi Artur, I think the files were in the archive but I will attach them again. You need to replace them in the MQX_4_0 source and rebuild.  See notes below on files to replace:

They are located in the bsp "mqx\source\io\flashx" and some in "mqx\source\io\flashx\freescale"

This support was added by modifying below files in BSP_TWR_K60D100M.

* -----------------------------------------------------------------------

* flashx.h: revised flashx_device_if_struct to add

* boolean (_CODE_PTR_ CHIP_ERASE)(struct io_flashx_struct *, char_ptr);

*

* flashx.c revised _io_flashx_install() to initialize the CHIP_SWAP pointer

* revised _io_flashx_ioctl() to add a case for FLASH_IOCTL_SWAP_FLASH

*

* flashprv.h

* revised io_flashx_struct to add

* boolean (_CODE_PTR_ CHIP_ERASE)(struct io_flashx_struct *, char_ptr);

*

* flash_ftfl_prv.h

* added prototype for function ftfl_flash_swap()

*

* flash_ftfl.c

* added function ftfl_flash_swap() to perform the bank swap

* revised const FLASHX_DEVICE_IF_STRUCT _flashx_ftfl_if to add ftfl_flash_swap

*

0 项奖励
回复

3,629 次查看
davidwhite
Contributor II

swapsupport.zip has the mqx source files that were modified to add the IOCTL that supports flash swap. See the demo in the earlier post for examples of how to use it.

3,629 次查看
artur73
Contributor I

Hi David,

Eventually, I made everything working under IAR, thanks.

Now I have a question - is it possible to change sector size?

Or, is it possible to write data by pieces of 256 bytes instead of 2k?

Thanks,

Artur

0 项奖励
回复

3,629 次查看
davidwhite
Contributor II

Hi Artur,

Good Job! Yes, I used 2k because that is the FLASH sector size on my K60. You can use arbitrary sizes.

I've seen examples from Freescale where they write one long word (32-bit) at a time.

You should be able to use the cache to improve performance.

   /* Enable the sector cache */
   ioctl(flash_file, FLASH_IOCTL_ENABLE_SECTOR_CACHE, NULL);

Thank You,

David

0 项奖励
回复

3,629 次查看
artur73
Contributor I

Hi David,

Actually, I use the same TWR-K60D100M, so it has to have 2k sector size.

So, the write block size has nothing to do with the sector size?

Thanks,

Artur

0 项奖励
回复

3,629 次查看
davidwhite
Contributor II

Hi Artur,

Correct. The I/O system manages the FLASH for you. That is a large benefit of using MQX.  I think you can pick an arbitrary block size. I have not tested with anything larger than 2k. I picked 2k because it seemed optimal.

Whatever you do, test it thoroughly to be sure you're getting the results you expect.

Best regards,

David 

0 项奖励
回复

3,629 次查看
artur73
Contributor I

Thanks for the reply, David.

So, what is the meaning of "sector" in this case?

0 项奖励
回复

3,629 次查看
davidwhite
Contributor II

The K60 with 512kB  has 256 2kB sectors. A sector is the smallest part of FLASH that can be erased by the FLASH controller. That is, you can't erase only one byte if you are coding on the "bare metal". The smallest region you can erase is 2kB.

That being said you may not need to know or care what a sector is when you use the RTOS for flashing because it handles many of the low level details automatically.

0 项奖励
回复

3,629 次查看
artur73
Contributor I

Hi David,

Thanks a lot, I implemented flash uploading and saving, everything is fine.

I have another issue. Let say, for some reasons corrupted firmware was uploaded and after swap/reboot we have "bricked" MCU. Is there any way to swap back to the working flash part?

Thanks.

0 项奖励
回复

3,629 次查看
artur73
Contributor I

Hello David,

Thanks a lot for the code and project. I added your code to the default MQX library and recompiled it.

Actually, I've encountered some issues with it.

I tried to create similar application under IAR. Everything looks OK except reading data from the active bank.

When debugger's pointer second time reaches line in the read2ks function:


len = read(fl_bank0, chbuf,_CHBUF_SIZE_); // copy flash block to the buffer

it for some reasons jumps to boot.s and starts to cycle on these lines (see below) and execution stacked there:

  ldr r0, =0xe000e180 /* NVIC_ICER0 - Interrupt Clear-Enable Registers */
  ldr r1, =0xe000e280 /* NVIC_ICPR0 - Interrupt Clear-Pending Registers */

  ldr r2, =0xffffffff

  mov r3, #8



Any help on this will be appreciated.

I'm trying to copy from active to inactive the software itself, also I use Int Flash Debug configuration.

That way Swap example works fine, but I would like to use MQX libraries.

0 项奖励
回复

3,629 次查看
davidwhite
Contributor II

Hi Artur,

Sorry, I have not tried IAR. The only configuration I have tested is CW 10.3 with MQX 4.0 for TWRK60N100.

I don't understand your comment about Int Flash Debug configuration. Are you saying that Int Flash Debug works correctly but Int Ram Debug does not?

David

0 项奖励
回复

3,629 次查看
artur73
Contributor I

Hi David,

I was able to make working normally the AN4533 application note from Freescale under IAR, which uses Int Flash Debug configuration. Now I want to use yours, but also with Int Flash Debug configuration, but so far no luck.

I am really not sure, why the execution pointer jumps to that Boot.s and eventually calls reset, so debugging session restarts.

Thanks,

Artur

0 项奖励
回复

3,629 次查看
nancyb
Contributor III

Thank you, David, for sharing your solution. Your MQX supported solution is succinct and elegant. I did end up modifying the code from AN4533 to implement my bootloader and it works without exception. One artifact of my solution is that I cannot execute the swap while MQX is running; I observed hard faults and system resets. I worked around this by splicing into mqx_main.c to perform the bootloader processing prior to starting MQX.

Does your solution require MQX 4.0? I am using MQX 3.8.1 and don't anticipate upgrading to 4.0 anytime soon. I don't want to go looking for trouble; trouble seems to find me just fine without my help :smileylaugh:

0 项奖励
回复

3,629 次查看
davidwhite
Contributor II

You're welcome. It is a common need so I though it good to share with the community. I asked Freescale to build it into their next release. I think it does require MQX 4.0. I did not try it with MQX 3.8.1.

0 项奖励
回复

3,629 次查看
davidwhite
Contributor II

I had similar problem and single stepped into the code.  FLASH_IOCTL_SWAP_FLASH_AND_RESET is not implemented in MQX 4.0 or MQX 3.8.1. We could not find it implemented in any of the Kinetis BSPs.

3,629 次查看
nancyb
Contributor III

Thank you, David, for this information. Unless I hear different, I won't bother updating to MQX 4.0 right now, and I will pursue the alternate path; implement the swap using the code derived from AN4533.

I tried a couple times stepping into the io_ioctl routine but the debugger gets lost very quickly, I am debugging on a Windows 7 PC and the only recovery is to quit CodeWarrior and use TaskManager to kill DE.exe.

0 项奖励
回复