{"id":88,"date":"2012-05-24T08:44:12","date_gmt":"2012-05-24T08:44:12","guid":{"rendered":"http:\/\/www.electronic-products-development.com\/?p=88"},"modified":"2022-02-18T15:37:50","modified_gmt":"2022-02-18T15:37:50","slug":"constant-pointers-in-program-memory","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic24\/c30-compiler\/constant-pointers-in-program-memory","title":{"rendered":"Constant Pointers In Program Memory"},"content":{"rendered":"<h4>Constants In Program Memory Issues<\/h4>\n<p>Although PIC24 has 3 bytes per instruction, you can only store data in 2 of them.  Therefore program memory usage is 3x actaul data size.<\/p>\n<p>C30 uses 16 bits for pointers.  By default it expects all constants to be in 32K of program memory address space.  This is a limitation but its done because its a 16 bit device.  Actually the compiler should provide an option to just deal with storing and accessing constants in > 32K, but it doesn&#8217;t so get over it.  The help section you want is &#8216;Managed PSV Access &#8216;.  This is a good respource: http:\/\/www.microchip.com\/forums\/printable.aspx?m=460709<\/p>\n<p>Working example of usage:<\/p>\n<pre><code>\r\n__psv__ const BYTE __attribute__((space(psv))) table1[] = {1, 3, 5, 7, 9};\r\n__psv__ const BYTE *p_character_data;\r\n\r\np_character_data = &data_table[0];\r\np_character_data += 200;\r\n      \r\nb_temp = *p_character_data++;\r\nb_temp = *p_character_data++;\r\n<\/code><\/pre>\n<p>Will read the values correctly.  You can also pass pointers as long as all are declared as __psv___ (even though when viewing the value of the pointer in a watch windows it will be shown as 16 bit &#8211; it is actually 32bit)<\/p>\n<p>Doing this you can use > 32K of constants, but remember each constant must be < 32K.\nAlso bear in mind that there is an irq limitation - see help if your using constants in an IRQ.\n\n\n\n<h4>Simple Pointers Declaration Solution<\/h4>\n<p>Change the CONSTANT define to this:<\/p>\n<pre><code>\r\n#define  CONSTANT       __psv__ const\r\n<\/code><\/pre>\n<p>You still need to deal with the other issues, but at least all the pointers are now being declared right.<\/p>\n<h4>Pointers To Functions<\/h4>\n<p>Again, is you need to deal with the 16 bit issue<\/p>\n<p>Use this of the function definition:<\/p>\n<pre><code>\r\n      CONSTANT BYTE get_font_values_arial_14 (WORD character) __attribute__((space(prog)));\r\n<\/code><\/pre>\n<p>or this for the function itself<\/p>\n<pre><code>\r\n      CONSTANT BYTE __attribute__((space(prog))) get_font_values_arial_14 (WORD character)\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Constants In Program Memory Issues Although PIC24 has 3 bytes per instruction, you can only store data in 2 of them. Therefore program memory usage is 3x actaul data size. C30 uses 16 bits for pointers. By default it expects all constants to be in 32K of program memory address space. This is a limitation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-c30-compiler"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/88\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}