You can try to use the built-in functions __segment_begin and __segment_size or __segment_end in the IAR, which are described in the "C / C ++ Compiler Reference Guide", which can be accessed from the Help menu in the EW430 IAR. The manual says that they work with segments defined in the linker file, and many people on the Internet seem to use it that way, but I tried and got compiler errors (IAR EW430 5.40.7). If this is somehow broken, you can report it to the IAR and get a fix (provided that you have a support contract).
You can use them as follows:
sum = fast_crc16(sum, __segment_begin("CODE"), __segment_size("CODE"));
I do not know what happens to split segments. But why would you exclude your reset vectors from your checksum calculation? You can simply go from the beginning of CODE to the end and enable the reset vectors.
I think you could structure your code like this:
sum = fast_crc16(sum, __segment_begin("CODE"), (char *)__segment_begin("INTVEC") - (char *)__segment_begin("CODE") + 1); sum = fast_crc16(sum, 0x10000, (char *)__segment_end("CODE") - 0x10000);
In addition, you may or may not have noticed that the __checksum variable is placed in memory wherever it fits. I found that it was hiding after my DATA16_ID segment, which put it right in the middle of the checksum code range, and I did not know how to automate skipping memory partitions to calculate the checksum. What I did was force __checksum for the first two bytes in the flash, defining a segment for these first two bytes and putting it there.
Edit: skipped the first change. If you manually adjust the range of the IAR linker checksum procedure to be able to use segment functions from the compiler, you will need to define a user segment that uses the end of your code in your linker.
I don't know if there is a way to automate this. You may need to compile your code twice (ugh) once with an unlimited segment to get the end of the code, then use a script to extract the end of the code, and then update the script linker. You can probably start the initial build in a command line event before build and just build an IAR project with an unlimited linker file. But that seems pretty ugly.