<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/17031/avoiding-hardfault-and-optimizing-code-size-with-twi-transaction-manager</link><description>I recently experimented with the TWI Transaction Manager and found the following: 
 This function would cause HardFault Error 
 void _BAD_read_func(app_twi_t* param_p_app_twi, 
 uint8_t* param_p_reg_addr, 
 uint8_t* param_p_buffer, 
 uint8_t byte_cnt</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 16 Nov 2016 08:44:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/17031/avoiding-hardfault-and-optimizing-code-size-with-twi-transaction-manager" /><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65329?ContentTypeID=1</link><pubDate>Wed, 16 Nov 2016 08:44:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:60be1110-1bf5-4fd1-983a-53ffbc48f975</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;No worries, thanks :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65330?ContentTypeID=1</link><pubDate>Wed, 16 Nov 2016 02:09:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fc1dc1fa-0e00-48ba-ae52-f5740d4f110b</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;Got that. Sorry for forgetting it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65328?ContentTypeID=1</link><pubDate>Tue, 15 Nov 2016 14:16:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7e026aea-9c6f-4714-b114-b55625c45615</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Please accept the answer if you think the issue is resolved.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65327?ContentTypeID=1</link><pubDate>Thu, 20 Oct 2016 05:50:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4894f71-0ee2-41e8-a7bc-1c091f215f6d</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;You are right. With the current badly optimized version of our firmware, we are already at 20kB code size, that is without Dual Bank, Persistent Storage, Device Manager or Peer Manager, all of which we do hope to add. Decreasing the code size has been a struggle. So any tips would be much appreciated.&lt;/p&gt;
&lt;p&gt;Also thanks for your idea with DFU retry. I didn&amp;#39;t think about it. I will let our smartphone app engineers know.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65325?ContentTypeID=1</link><pubDate>Tue, 18 Oct 2016 07:50:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7d9301f9-62d4-4b44-8506-f8431a143bdb</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;It depends on how you handle it in the application. If you automatically retry it will only increase the DFU time. Also, unless you actively try to interrupt the DFU process, or the battery is almost depleted, it shouldn&amp;#39;t fail.&lt;/p&gt;
&lt;p&gt;Dual bank DFU is better than single bank for sure, but when trying to squeeze a SoftDevice, bootloader and 2 times the application into 128kB you might have to sacrifice a lot of functionality to get there.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65324?ContentTypeID=1</link><pubDate>Tue, 18 Oct 2016 02:27:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9aa52541-0229-4a0d-a3d7-dca4e60aebdc</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;I think it is fairly important. Mostly for user experience. We imagine the more non-tech consumers would be confused if a failed update semi-brick their device.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65326?ContentTypeID=1</link><pubDate>Mon, 17 Oct 2016 08:54:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0cc54f27-0c6e-493b-8545-4e0d32bfc9ef</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Keil defines the following data types: RO (read only), RW (read write) and ZI (zero initialized)
The only difference between RW and ZI is that RW data is initialized to something different than zero.&lt;/p&gt;
&lt;p&gt;RO variables only take up flash space
RW variables take up both RAM and flash (the flash is used to store the init values)
ZI variables only take up RAM, and are initialized to 0 automatically at startup.&lt;/p&gt;
&lt;p&gt;How critical is it to use dual bank DFU?
The only difference between single and dual bank is that if something fails when doing single bank update you have to retry the DFU operation until it succeeds.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65321?ContentTypeID=1</link><pubDate>Fri, 14 Oct 2016 08:50:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9a7660a9-f6a2-4795-8cdf-326438a9385b</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;I just read &lt;a href="https://devzone.nordicsemi.com/question/23059/how-to-monitor-flash-and-ram-usage-after-compilation/"&gt;this question&lt;/a&gt; and I think I grasp things a little better now.&lt;/p&gt;
&lt;p&gt;So overall trying to avoid global/static variable is not going to benefit me in anyway because both Code, RO and RW Data takes Flash space...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65323?ContentTypeID=1</link><pubDate>Fri, 14 Oct 2016 08:34:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:301abfb4-a771-4582-8b2a-35597f54caec</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;The second is the code size limit by MDK-Lite. For this I believe I only need to make sure (Code + RO Data) to be less than 32kB, and I believe that global and static local variables increase RO Data size.&lt;/p&gt;
&lt;p&gt;I think I understand what you meant by converting multiple global/static variables into one managed by a state machine. That should be just converting size taken by RW Data into taken by Code, and thus actually badly impact me for MDK-Lite limitation. I didn&amp;#39;t think that through.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65322?ContentTypeID=1</link><pubDate>Fri, 14 Oct 2016 08:33:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0066d309-c0e9-41d5-a090-b791da1bae60</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;Hi Torbjørn,&lt;/p&gt;
&lt;p&gt;I must first admit that I am not well versed on how memory is allocated on flash and on RAM while running. So please advise me. From what you said I already feel like some of what I understood is wrong.&lt;/p&gt;
&lt;p&gt;There are two size constraints that I need to consider.&lt;/p&gt;
&lt;p&gt;First we use an nRF51822 QFAB, with S110, and plan to have Dual Bank DFU and some persistent storage, so I think I need to use less than 12kB of flash to be safe. Regarding this, I have little knowledge and am now starting to research how my code impact Flash and RAM consumption. So any tips or keywords to push me in the right direction is really appreciated.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Avoiding HardFault and Optimizing Code Size with TWI Transaction Manager</title><link>https://devzone.nordicsemi.com/thread/65320?ContentTypeID=1</link><pubDate>Fri, 14 Oct 2016 07:20:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5acf254e-34b3-484b-845a-b9751b861bfb</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Your guess is correct. If you want to declare the transfer or transaction structs inside a function other than main() you have to make them static, otherwise the data will be overwritten by the time the TWI driver uses them. The TWI driver runs various asserts on the data to ensure it is valid, and this is likely to fail if the data has been overwritten.&lt;/p&gt;
&lt;p&gt;Is it code size, RAM size or both that you need to optimize for?
Having all your structures global or static will only affect your RAM consumption, not your code consumption. Even if you only define one structure at a time you still need to have that data in the code somewhere.&lt;/p&gt;
&lt;p&gt;If you do want to pass one transaction at a time to the TWI library you probably need some kind of state machine in the application, where you wait for TWI events and feed one transaction at a time into the library. That being said I think this will have a larger impact on code usage than simply having all the transactions global/static, so I wouldn&amp;#39;t recommend it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>