EEPROM.read before void setup()

How to move EEPROM.read before void setup()

#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <EEPROM.h>


int S;
int p = PB14;
int n = PB12; // right +
int ap = PB13; //left, -
int an = PB15;
int E = PB11;

///////////////////////////
int EEPROM.read(0, (uint16*)&S);
//////////////////////////

void setup() {
  
  //////////////////////////////////
 // EEPROM.read(0, (uint16*)&S);
  /////////////////////////////////

  
  lcd.begin(16, 2);
  pinMode(p, INPUT_PULLUP);
  pinMode(n, INPUT_PULLUP);
  pinMode(ap, INPUT_PULLUP);
  pinMode(an, INPUT_PULLUP);
  pinMode(E, INPUT_PULLUP);

}

void loop() {

  if (digitalRead(p) == LOW)

  {
    (S++ );
  }
  if (digitalRead(n) == LOW)
  {
    (S-- );
  }
  if (digitalRead(E) == LOW)
  {
    EEPROM.write(0, S);
  }
  lcd.setCursor(0, 0);
  lcd.print("S ");
  lcd.print(S);
  delay (10);
}

This approach is giving error
expected initializer before '.' token

You don't. Put it in setup().

You cannot execute code statements outside of setup() or loop() functions.
Put this as the first statement inside setup.

I have in setup(). , I was thinking maybe it is possible to move it.

Please provide a detailed explanation of why you want to move it.

The idea is to replace "#define SAMPLES 77" by it to regulate frequency of generator


///////////////////////
#define SAMPLES 77
////////////////////////

#include <libmaple/dma.h>
dma_tube_config dma_cfg, dma_cfg2;
int flag = 0;
int flag1 = 0;
int out = PB6;
int out1 = PB7;
int val[SAMPLES];
int val1[SAMPLES];

int16 shift = 0;

int amp = 40;
int cnt = 0;
int time_track = 0;
float stp = 6.2831 / SAMPLES;
int ret = 17;

timer_dev *dev = PIN_MAP[out].timer_device;
uint8 cc_channel = PIN_MAP[out].timer_channel;
timer_dev *dev1 = PIN_MAP[out1].timer_device;
uint8 cc_channel1 = PIN_MAP[out1].timer_channel;

void fun1()
{
  flag1++;
}

void timer_conf()
{
  /////////////
  timer_dma_set_base_addr(dev, TIMER_DMA_BASE_CCR2);
  timer_dma_set_burst_len(dev1, 1);
  timer_dma_enable_req(dev, cc_channel1);
  timer_set_reload(dev, 102);
  timer_set_prescaler(dev, 0);
  ///////////////
  timer_dma_set_base_addr(dev1, TIMER_DMA_BASE_CCR2);
  timer_dma_set_burst_len(dev1, 1);
  timer_dma_enable_req(dev1, cc_channel1);
  timer_set_reload(dev1, 102);
  timer_set_prescaler(dev1, 0);
}

void dma_conf()
{
  dma_init(DMA1);
  /* T4C2 DMA C4 */
  dma_cfg.tube_dst = &(dev->regs.gen->DMAR);
  dma_cfg.tube_dst = &(dev1->regs.gen->DMAR);
  dma_cfg.tube_dst_size = DMA_SIZE_32BITS;
  dma_cfg.tube_src = val;
  dma_cfg.tube_src = val1;
  dma_cfg.tube_src_size = DMA_SIZE_32BITS;
  dma_cfg.tube_nr_xfers = SAMPLES;
  dma_cfg.tube_flags = DMA_CFG_SRC_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH1;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH2;
  dma_cfg.target_data = 0;

  ret = dma_tube_cfg(DMA1, DMA_CH4, &dma_cfg);
}

void dma_start()
{
 
  dma_enable(DMA1, DMA_CH4);
  timer_resume(dev1);
  dma_enable(DMA1, DMA_CH1);
  dma_enable(DMA1, DMA_CH2);

}

void init_wave()
{
  int i;
  for (i = 0; i < SAMPLES; i++)
  {
    val1[i] = 40 + amp * sin(stp * i);
  }
}

void setup()
{
  pinMode(out1, PWM);

  timer_conf();
  dma_conf();
  dma_start();
  init_wave();
}

void loop()

{


}

Then don't use a #define

Declare a global variable then read EEPROM in setup() and set the value of the variable

Which would probably not work because the SAMPLES define may be used in "libmaple/dma.h".

I changed

///////////////////////
//#define SAMPLES 77
int SAMPLES (77 );
////////////////////////

error: array bound is not an integer constant before ']' token
int val1[SAMPLES];

from this line

int val1[SAMPLES];

Should be declared as:

int SAMPLES = 123;

Your next problem is that your arrays (eg. "val1") cannot be statically allocated if the value of "SAMPLES" is supposed to be read from EEPROM.

You need to use

int SAMPLES = 77;
int SAMPLES =123 ;

= error
exit status 1
array bound is not an integer constant before ']' token
from this line

int val1[SAMPLES];

That what I trying to do

You cannot initialise the size of a global array with a variable, you must use a constant, but then you cannot change the value of the constant

What you can do is to initialise the global array with the maximum size that it will ever be, then read the size to be used from EEPROM and use that as the limit of how many elements of the array that you actually use

There's always dynamic allocation ......

Why complicate things ? The maximum size of the array must be known and enough memory must be available to accommodate it.

It is a fair bet that the number of elements in the array will be used as a limit somewhere in the program to avoid straying out of bounds, so why not make that the value loaded from EEPROM ?

1 Like

Set "MaxArraySize" to a value such that the sketch uses less than 75% of available RAM. Then you can set SAMPLES to any value equal to or less than that.

const int MaxArraySize = 100;

uint16_t SAMPLES = 77; // Must be <= MaxArraySize

int val[MaxArraySize];
int val1[MaxArraySize];

void setup()
{
  SAMPLES = (EEPROM.read(0) << 8) | EEPROM.read(1);
1 Like

I did this way


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <EEPROM.h>


//////////////
const int MaxArraySize = 100;

uint16_t SAMPLES = 77; // Must be <= MaxArraySize

int val[MaxArraySize];
int val1[MaxArraySize];

///////////////////////
//#define SAMPLES 77
////////////////////////
//++++++++++++++++++++++++++++++++++++++++++++++++


int S;



int p = PB14;
int n = PB12; // right +
int ap = PB13; //left, -
int an = PB15;
int E = PB11;



#include <libmaple/dma.h>
dma_tube_config dma_cfg, dma_cfg2;
int flag = 0;
int flag1 = 0;
int out = PB6;
int out1 = PB7;

int val1[SAMPLES];

int16 shift = 0;

int amp = 40;
int cnt = 0;
int time_track = 0;
float stp = 6.2831 / SAMPLES;
int ret = 17;

timer_dev *dev = PIN_MAP[out].timer_device;
uint8 cc_channel = PIN_MAP[out].timer_channel;
timer_dev *dev1 = PIN_MAP[out1].timer_device;
uint8 cc_channel1 = PIN_MAP[out1].timer_channel;

void fun1()
{
  flag1++;
}

void timer_conf()
{
  /////////////
  timer_dma_set_base_addr(dev, TIMER_DMA_BASE_CCR2);
  timer_dma_set_burst_len(dev1, 1);
  timer_dma_enable_req(dev, cc_channel1);
  timer_set_reload(dev, 102);
  timer_set_prescaler(dev, 0);
  ///////////////
  timer_dma_set_base_addr(dev1, TIMER_DMA_BASE_CCR2);
  timer_dma_set_burst_len(dev1, 1);
  timer_dma_enable_req(dev1, cc_channel1);
  timer_set_reload(dev1, 102);
  timer_set_prescaler(dev1, 0);
}

void dma_conf()
{
  dma_init(DMA1);
  /* T4C2 DMA C4 */
  dma_cfg.tube_dst = &(dev->regs.gen->DMAR);
  dma_cfg.tube_dst = &(dev1->regs.gen->DMAR);
  dma_cfg.tube_dst_size = DMA_SIZE_32BITS;
  dma_cfg.tube_src = val;
  dma_cfg.tube_src = val1;
  dma_cfg.tube_src_size = DMA_SIZE_32BITS;
  dma_cfg.tube_nr_xfers = SAMPLES;
  dma_cfg.tube_flags = DMA_CFG_SRC_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH1;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH2;
  dma_cfg.target_data = 0;

  ret = dma_tube_cfg(DMA1, DMA_CH4, &dma_cfg);
}

void dma_start()
{

  dma_enable(DMA1, DMA_CH4);
  timer_resume(dev1);
  dma_enable(DMA1, DMA_CH1);
  dma_enable(DMA1, DMA_CH2);

}

void init_wave()
{
  int i;
  for (i = 0; i < SAMPLES; i++)
  {
    val1[i] = 40 + amp * sin(stp * i);
  }
}

void setup()
{

  //////////////////////////////////
  SAMPLES = (EEPROM.read(0) << 8) | EEPROM.read(1);

  // EEPROM.read(0, (uint16*)&S);
  /////////////////////////////////


  pinMode(out1, PWM);

  timer_conf();
  dma_conf();
  dma_start();
  init_wave();
}

void loop()

{
  if (digitalRead(p) == LOW)

  {
    (S++ );
  }
  if (digitalRead(n) == LOW)
  {
    (S-- );
  }
  if (digitalRead(E) == LOW)
  {
    EEPROM.write(0, S);
  }
  lcd.setCursor(0, 0);
  lcd.print("S ");
  lcd.print(S);
  delay (10);

}

and error is
error: array bound is not an integer constant before ']' token
int val1[SAMPLES];

from this line

int val1[SAMPLES];
int val1[SAMPLES];

Why is that line even in the code ? The idea is to declare the arrays with the maximum possible number of elements but only to use as many elements as are needed. Hence

int val[MaxArraySize];
int val1[MaxArraySize];

I just clean up the code " val" is not used anymore, only "val1"