0

I want to put a table in Flash and read it directly from my C program. According to Microchip this is done by __attribute__((space(psv))) However, as the most things around microchip, their own examples doesn't work that well (usually obsolete and not updated): https://microchipdeveloper.com/16bit:psv

So this is what I'm trying to do:

uint16_t __attribute__((space(psv))) ConfDiskImage[] = {
    /*AOUT*/ 0x01E0,0x0004,0x3333,0x4053,
    /*   1*/ 0x01E1,0x0012,0x0005,0x0000,0x0000,0x0000,0x4120,0x0000,0x0000,0x0000,0x4120,
    /*   2*/ 0x01E2,0x0012,0x0006,0x0000,0x0000,0x0000,0x4120,0x0000,0x0000,0x0000,0x4120,
    /*   3*/ 0x01E3,0x0012,0x0007,0x0000,0x0000,0x0000,0x4120,0x0000,0x0000,0x0000,0x4120,
    /*EOD */ 0x0000,0x0000
};

When I compile I get: "warning: ignoring space attribute applied to automatic ConfDiskImage"

I'm using MPLAB X IDE 5.45 with XC16-gcc v1.50. Microcontroller: dsPIC33EP256MC506

Any ideas of how to get it to stay in Flash (not being copied to RAM) and read it directly from flash with a pointer?

Max Kielland
  • 5,627
  • 9
  • 60
  • 95
  • 1
    Try adding `const` specifier. – Oleg Mazurov Feb 19 '21 at 18:35
  • 1
    Without the context [more of your code], I'm guessing ... You're defining it as a function scope variable (from the "automatic"). What I think you have is (e.g.): `int main(void) { uint16_t __attribute__((space(psv))) ConfDiskImage[] = { ... }; }` What you may need is global scope (e.g.): `uint16_t __attribute__((space(psv))) ConfDiskImage[] = { ... }; int main(void) { }` Your original _might_ work if you added `static` – Craig Estey Feb 19 '21 at 18:36
  • @Craig Estey DOH!!! I'm pretty sure this is the problem. I throw it in as a test concept so I'm pretty sure it's in the main function. To my defence, it was late on a Friday night at the office :) I will get right to it on Monday. – Max Kielland Feb 20 '21 at 11:34

1 Answers1

0

I suspect what you really need is perhaps more involved but the simple answer to the question you asked is to use the const storage class.

For example:

/*
 * File:   main.c
 * Author: dan1138
 * Target: dsPIC33EP256MC506
 * Compiler: XC16 v1.61
 * IDE: MPLABX v5.45
 *
 * Created on February 19, 2021, 11:56 PM
 */

#pragma config ICS = PGD2, JTAGEN = OFF, ALTI2C1 = OFF, ALTI2C2 = OFF, WDTWIN = WIN25
#pragma config WDTPOST = PS32768, WDTPRE = PR128, PLLKEN = ON, WINDIS = OFF
#pragma config FWDTEN = OFF, POSCMD = NONE, OSCIOFNC = ON, IOL1WAY = OFF, FCKSM = CSECMD
#pragma config FNOSC = FRCDIVN, PWMLOCK = OFF, IESO = ON, GWRP = OFF, GCP = OFF

#include "xc.h"

const uint16_t ConfDiskImage[] = {
    /*AOUT*/ 0x01E0,0x0004,0x3333,0x4053,
    /*   1*/ 0x01E1,0x0012,0x0005,0x0000,0x0000,0x0000,0x4120,0x0000,0x0000,0x0000,0x4120,
    /*   2*/ 0x01E2,0x0012,0x0006,0x0000,0x0000,0x0000,0x4120,0x0000,0x0000,0x0000,0x4120,
    /*   3*/ 0x01E3,0x0012,0x0007,0x0000,0x0000,0x0000,0x4120,0x0000,0x0000,0x0000,0x4120,
    /*EOD */ 0x0000,0x0000
};

int main(void) 
{
    const uint16_t *pData;
    
    pData = ConfDiskImage; /* point to the start of the image */
    /*
     * Walk the image
     */
    for(;;)
    {
        if((!*pData) && (!*pData+1))
        {
            /* At end of image point to the start */
            pData = ConfDiskImage;
        }
        pData++; /* skip record type */
        pData += 1+*pData/2;
    }
    return 0;
}
Dan1138
  • 1,150
  • 8
  • 11
  • I did that too but as @Craig Estey pointed out, it's probably because it's in the main as a local scope. I will check on Monday. – Max Kielland Feb 20 '21 at 11:38
  • 1
    Well you didn't post a complete, buildable example program. I posted a full example that does what you asked for. – Dan1138 Feb 21 '21 at 01:55