Help with modifing PIC linker file

Discussion in 'Embedded Systems and Microcontrollers' started by spinnaker, Dec 26, 2011.

  1. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    5,097
    1,190
    I need to be able to allocate a 512 byte string in my program. From what I have leaned, I need to preallocate a 512 byte region with a custom linker file.

    I understand it would be something like this:

    Code ( (Unknown Language)):
    1.  
    2. DATABANK   NAME=myMSD      START=0x600          END=0x7FF          PROTECTED
    3.  

    But when I look at the default linker file, I am not sure what to put for the starting address of my databank.

    Can I just eliminate one or more of the gprX entries and fit it in there?

    The device has 3.8K of RAM.


    Code ( (Unknown Language)):
    1.  
    2.  
    3. // File: 18lf27j53_g.lkr
    4. // Generic linker script for the PIC18LF27J53 processor
    5.  
    6. #DEFINE _CODEEND _DEBUGCODESTART - 1
    7. #DEFINE _CEND _CODEEND + _DEBUGCODELEN
    8. #DEFINE _DATAEND _DEBUGDATASTART - 1
    9. #DEFINE _DEND _DATAEND + _DEBUGDATALEN
    10.  
    11. LIBPATH .
    12.  
    13. #IFDEF _CRUNTIME
    14.   #IFDEF _EXTENDEDMODE
    15.     FILES c018i_e.o
    16.     FILES clib_e.lib
    17.     FILES p18lf27j53_e.lib
    18.  
    19.   #ELSE
    20.     FILES c018i.o
    21.     FILES clib.lib
    22.     FILES p18lf27j53.lib
    23.   #FI
    24.  
    25. #FI
    26.  
    27. #IFDEF _DEBUGCODESTART
    28.   CODEPAGE   NAME=page       START=0x0               END=_CODEEND
    29.   CODEPAGE   NAME=debug      START=_DEBUGCODESTART   END=_CEND        PROTECTED
    30. #ELSE
    31.   CODEPAGE   NAME=page       START=0x0               END=0x1FFF7
    32. #FI
    33.  
    34. CODEPAGE   NAME=config     START=0x1FFF8           END=0x1FFFF        PROTECTED
    35. CODEPAGE   NAME=devid      START=0x3FFFFE          END=0x3FFFFF       PROTECTED
    36.  
    37. #IFDEF _EXTENDEDMODE
    38.   DATABANK   NAME=gpre       START=0x0               END=0x5F
    39. #ELSE
    40.   ACCESSBANK NAME=accessram  START=0x0               END=0x5F
    41. #FI
    42.  
    43. DATABANK   NAME=gpr0       START=0x60              END=0xFF
    44. DATABANK   NAME=gpr1       START=0x100             END=0x1FF
    45. DATABANK   NAME=gpr2       START=0x200             END=0x2FF
    46. DATABANK   NAME=gpr3       START=0x300             END=0x3FF
    47. DATABANK   NAME=gpr4       START=0x400             END=0x4FF
    48. DATABANK   NAME=gpr5       START=0x500             END=0x5FF
    49. DATABANK   NAME=gpr6       START=0x600             END=0x6FF
    50. DATABANK   NAME=gpr7       START=0x700             END=0x7FF
    51. DATABANK   NAME=gpr8       START=0x800             END=0x8FF
    52. DATABANK   NAME=gpr9       START=0x900             END=0x9FF
    53. DATABANK   NAME=gpr10      START=0xA00             END=0xAFF
    54. DATABANK   NAME=gpr11      START=0xB00             END=0xBFF
    55.  
    56. #IFDEF _DEBUGDATASTART
    57.   DATABANK   NAME=gpr12      START=0xC00             END=_DATAEND
    58.   DATABANK   NAME=dbgspr     START=_DEBUGDATASTART   END=_DEND           PROTECTED
    59. #ELSE //no debug
    60.   DATABANK   NAME=gpr12      START=0xC00             END=0xCFF
    61. #FI
    62.  
    63. DATABANK   NAME=gpr13      START=0xD00             END=0xDFF
    64. DATABANK   NAME=gpr14      START=0xE00             END=0xEAF
    65. DATABANK   NAME=sfr14      START=0xEB0             END=0xEFF          PROTECTED
    66. DATABANK   NAME=sfr15      START=0xF00             END=0xF5F          PROTECTED
    67. ACCESSBANK NAME=accesssfr  START=0xF60             END=0xFFF          PROTECTED
    68.  
    69.  
    70.  
    71. #IFDEF _CRUNTIME
    72.   SECTION    NAME=CONFIG     ROM=config
    73.   #IFDEF _DEBUGDATASTART
    74.     STACK SIZE=0x100 RAM=gpr11
    75.   #ELSE
    76.     STACK SIZE=0x100 RAM=gpr12
    77.   #FI
    78. #FI
    79.  
    80.  
     
  2. nigelwright7557

    Senior Member

    May 10, 2008
    488
    71
    Just take two of the gpr entries that run consecutively.
     
  3. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    5,097
    1,190

    So I can just basically overwrite two of themas I expected?
     
  4. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    5,097
    1,190
    What am I missing.

    Here is my modified linker file:

    Code ( (Unknown Language)):
    1.  
    2. // File: 18f27j53_g.lkr
    3. // Generic linker script for the PIC18F27J53 processor
    4.  
    5. #DEFINE _CODEEND _DEBUGCODESTART - 1
    6. #DEFINE _CEND _CODEEND + _DEBUGCODELEN
    7. #DEFINE _DATAEND _DEBUGDATASTART - 1
    8. #DEFINE _DEND _DATAEND + _DEBUGDATALEN
    9.  
    10. LIBPATH .
    11.  
    12. #IFDEF _CRUNTIME
    13.   #IFDEF _EXTENDEDMODE
    14.     FILES c018i_e.o
    15.     FILES clib_e.lib
    16.     FILES p18f27j53_e.lib
    17.  
    18.   #ELSE
    19.     FILES c018i.o
    20.     FILES clib.lib
    21.     FILES p18f27j53.lib
    22.   #FI
    23.  
    24. #FI
    25.  
    26. #IFDEF _DEBUGCODESTART
    27.   CODEPAGE   NAME=page       START=0x0               END=_CODEEND
    28.   CODEPAGE   NAME=debug      START=_DEBUGCODESTART   END=_CEND        PROTECTED
    29. #ELSE
    30.   CODEPAGE   NAME=page       START=0x0               END=0x1FFF7
    31. #FI
    32.  
    33. CODEPAGE   NAME=config     START=0x1FFF8           END=0x1FFFF        PROTECTED
    34. CODEPAGE   NAME=devid      START=0x3FFFFE          END=0x3FFFFF       PROTECTED
    35.  
    36. #IFDEF _EXTENDEDMODE
    37.   DATABANK   NAME=gpre       START=0x0               END=0x5F
    38. #ELSE
    39.   ACCESSBANK NAME=accessram  START=0x0               END=0x5F
    40. #FI
    41.  
    42. DATABANK   NAME=gpr0       START=0x60              END=0xFF
    43. DATABANK   NAME=gpr1       START=0x100             END=0x1FF
    44. DATABANK   NAME=gpr2       START=0x200             END=0x2FF
    45. DATABANK   NAME=gpr3       START=0x300             END=0x3FF
    46. DATABANK   NAME=gpr4       START=0x400             END=0x4FF
    47. DATABANK   NAME=gpr5       START=0x500             END=0x5FF
    48.  
    49.  
    50. DATABANK   NAME=myMSD      START=0x600             END=0x7FF          PROTECTED  
    51.    
    52. DATABANK   NAME=gpr8       START=0x800             END=0x8FF
    53. DATABANK   NAME=gpr9       START=0x900             END=0x9FF
    54. DATABANK   NAME=gpr10      START=0xA00             END=0xAFF
    55. DATABANK   NAME=gpr11      START=0xB00             END=0xBFF
    56.  
    57. #IFDEF _DEBUGDATASTART
    58.   DATABANK   NAME=gpr12      START=0xC00             END=_DATAEND
    59.   DATABANK   NAME=dbgspr     START=_DEBUGDATASTART   END=_DEND           PROTECTED
    60. #ELSE //no debug
    61.   DATABANK   NAME=gpr12      START=0xC00             END=0xCFF
    62. #FI
    63.  
    64. DATABANK   NAME=gpr13      START=0xD00             END=0xDFF
    65. DATABANK   NAME=gpr14      START=0xE00             END=0xEAF
    66. DATABANK   NAME=sfr14      START=0xEB0             END=0xEFF          PROTECTED
    67. DATABANK   NAME=sfr15      START=0xF00             END=0xF5F          PROTECTED
    68. ACCESSBANK NAME=accesssfr  START=0xF60             END=0xFFF          PROTECTED
    69.  
    70. #IFDEF _CRUNTIME
    71.   SECTION    NAME=CONFIG     ROM=config
    72.   #IFDEF _DEBUGDATASTART
    73.     STACK SIZE=0x100 RAM=gpr11
    74.   #ELSE
    75.     STACK SIZE=0x100 RAM=gpr12
    76.   #FI
    77. #FI
    78.  

    I modified my C code as follow:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #pragma udata myMSD=0x600
    4. volatile far char msd_buffer[512];
    5.  
    6.  
    Yet I still get

    Error - section 'myMSD' can not fit the absolute section. Section 'myMSD' start=0x00000600, length=0x0000022a


    What am I missing?
     
  5. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    5,097
    1,190
    Here is my map file:

    Code ( (Unknown Language)):
    1.  
    2. DATABANKS:
    3.     Memory      Start        End              Section    Address Size(Bytes)
    4.  ---------  ---------  ---------            ---------  ---------  ---------
    5.       gpr0     0x0060     0x00ff                                          
    6.  
    7.       gpr1     0x0100     0x01ff                                          
    8.  
    9.       gpr2     0x0200     0x02ff                                          
    10.  
    11.       gpr3     0x0300     0x03ff                                          
    12.  
    13.       gpr4     0x0400     0x04ff                                          
    14.  
    15.       gpr5     0x0500     0x05ff                                          
    16.  
    17.      myMSD     0x0600     0x07ff                                          
    18.  
    19.       gpr8     0x0800     0x08ff                                          
    20.  
    21.       gpr9     0x0900     0x09ff                                          
    22.  
    23.      gpr10     0x0a00     0x0aff                                          
    24.  
    25.      gpr11     0x0b00     0x0bff                                          
    26.  
    27.      gpr12     0x0c00     0x0cff                                          
    28.  
    29.      gpr13     0x0d00     0x0dff                                          
    30.  
    31.      gpr14     0x0e00     0x0eaf                                          
    32.  
    33.      sfr14     0x0eb0     0x0eff          SFR_BANKED0     0x0eb8     0x0005
    34.                                           SFR_BANKED1     0x0ebf     0x000f
    35.                                           SFR_BANKED2     0x0ed1     0x0002
    36.                                           SFR_BANKED3     0x0ee1     0x0004
    37.                                           SFR_BANKED4     0x0ee6     0x0005
    38.                                           SFR_BANKED5     0x0ef2     0x0003
    39.                                           SFR_BANKED6     0x0ef7     0x0002
    40.                                           SFR_BANKED7     0x0efc     0x0004
    41.  
    42.      sfr15     0x0f00     0x0f5f          SFR_BANKED8     0x0f00     0x0043
    43.                                           SFR_BANKED9     0x0f44     0x0010
    44.  
    45.  
    46. ACCESSBANKS:
    47.     Memory      Start        End              Section    Address Size(Bytes)
    48.  ---------  ---------  ---------            ---------  ---------  ---------
    49.  accessram     0x0000     0x005f                                          
    50.  
    51.  accesssfr     0x0f60     0x0fff        SFR_UNBANKED0     0x0f60     0x00a0
    52.  
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,442
    1,628
    There is a section in the "MPLAB C18 Users Guide" (found in the MPLAB Help | Topics... area) named "Application: Creating Large Data Objects and the USART" that describes how to do this.

    Change the linker script to remove two or more sections (such as 2 & 3), and make a new larger section:
    Code ( (Unknown Language)):
    1.  
    2. //DATABANK   NAME=gpr2       START=0x200             END=0x2FF
    3. //DATABANK   NAME=gpr3       START=0x300             END=0x3FF
    4.  
    5. DATABANK   NAME=big        START=0x200             END=0x3FF PROTECTED
    "PROTECTED" keeps the linker from using it unless explicitly called for.

    To use this area define a variable in that area. You then can use a pointer to get there. (Note the #pragma to set the section).
    Code ( (Unknown Language)):
    1.  
    2. #pragma udata buffer_scn
    3. static char msd_buffer[0x200];
    4. #pragma udata
    5.  
    6. void main(void)
    7. {
    8.   // access the RAM section thru a pointer:
    9.   char * buf_ptr = &msd_buffer[0];
    10.   int i;
    11.   // examples of use
    12.   buf_ptr[5] = 10;
    13.   for(i=0; i<=0x200; i++)
    14.   {
    15.     buf_ptr[i] = i;
    16.   }  
    17.   while(1);  
    18. }[/i]
     
  7. nigelwright7557

    Senior Member

    May 10, 2008
    488
    71
    You also need to setup a section at the end of the linker.

    SECTION NAME=LARGE RAM=large
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,442
    1,628
    Whoops, yep, it needs the SECTION NAME, I used:

    Code ( (Unknown Language)):
    1. SECTION NAME=buffer_scn RAM=big
    I'm attaching the entire C and linker files I used.
     
  9. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    5,097
    1,190
    How does the section name apply in my code? Or is it just there in the linker file?
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,442
    1,628
    Code ( (Unknown Language)):
    1.  
    2. #pragma udata buffer_scn
    3. static char msd_buffer[0x200];
    4. #pragma udata
    The first #pragma instructs the linker to specifically use the "buffer_scn" area.

    The second #pragma instructs the linker to use any open area.
     
  11. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    5,097
    1,190
    Thanks I understand that part. My question was, what is the purpose of the Section keyword in the linker file. Because it does seem to work for me with no Section specified in the linker file.
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,442
    1,628
    I couldn't answer that because if I remove the SECTION line I can't get it to build.
     
Loading...