Several fixes:

- CRC32 is stored at the end of the EEPROM.
- Fixes for volume/pan storing.
- small fixes.
pull/4/head
Holger Wirtz 6 years ago
parent 3124205143
commit 1a63a4e58e
  1. 119
      MicroDexed.ino
  2. 12
      config.h
  3. 2
      dexed.cpp
  4. 3
      dexed_sysex.cpp

@ -29,6 +29,7 @@
#include <SD.h> #include <SD.h>
#include <MIDI.h> #include <MIDI.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <limits.h>
#include "dexed.h" #include "dexed.h"
#include "dexed_sysex.h" #include "dexed_sysex.h"
#include "config.h" #include "config.h"
@ -68,11 +69,11 @@ uint32_t xrun = 0;
uint32_t overload = 0; uint32_t overload = 0;
uint32_t peak = 0; uint32_t peak = 0;
uint16_t render_time_max = 0; uint16_t render_time_max = 0;
uint8_t bank; uint8_t bank = 0;
uint8_t voice; uint8_t voice = 0;
float vol; float vol = VOLUME;
float vol_right; float vol_right = 1.0;
float vol_left; float vol_left = 1.0;
#ifdef MASTER_KEY_MIDI #ifdef MASTER_KEY_MIDI
bool master_key_enabled = false; bool master_key_enabled = false;
@ -100,9 +101,7 @@ void setup()
{ {
//while (!Serial) ; // wait for Arduino Serial Monitor //while (!Serial) ; // wait for Arduino Serial Monitor
Serial.begin(SERIAL_SPEED); Serial.begin(SERIAL_SPEED);
delay(180); delay(220);
initial_values_from_eeprom();
#ifndef MASTER_KEY_MIDI #ifndef MASTER_KEY_MIDI
lcd.init(); lcd.init();
@ -122,6 +121,8 @@ void setup()
Serial.println(F("https://github.com/dcoredump/MicroDexed")); Serial.println(F("https://github.com/dcoredump/MicroDexed"));
Serial.println(F("<setup start>")); Serial.println(F("<setup start>"));
initial_values_from_eeprom();
// start up USB host // start up USB host
#ifdef USE_ONBOARD_USB_HOST #ifdef USE_ONBOARD_USB_HOST
usb_host.begin(); usb_host.begin();
@ -163,11 +164,12 @@ void setup()
// load default SYSEX data // load default SYSEX data
load_sysex(bank, voice); load_sysex(bank, voice);
#ifdef DEBUG #ifdef DEBUG
Serial.print(F("Bank/Voice from EEPROM [")); Serial.print(F("Bank/Voice from EEPROM ["));
Serial.print(EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_BANK_ADDR), DEC); Serial.print(EEPROM.read(EEPROM_OFFSET + EEPROM_BANK_ADDR), DEC);
Serial.print(F("/")); Serial.print(F("/"));
Serial.print(EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_VOICE_ADDR), DEC); Serial.print(EEPROM.read(EEPROM_OFFSET + EEPROM_VOICE_ADDR), DEC);
Serial.println(F("]")); Serial.println(F("]"));
show_patch(); show_patch();
#endif #endif
@ -333,6 +335,7 @@ bool handle_master_key(uint8_t data)
Serial.println(num, DEC); Serial.println(num, DEC);
#endif #endif
EEPROM.update(EEPROM_OFFSET + EEPROM_VOICE_ADDR, num); EEPROM.update(EEPROM_OFFSET + EEPROM_VOICE_ADDR, num);
update_eeprom_checksum();
} }
#ifdef DEBUG #ifdef DEBUG
else else
@ -356,6 +359,7 @@ bool handle_master_key(uint8_t data)
{ {
bank = num - 10; bank = num - 10;
EEPROM.update(EEPROM_OFFSET + EEPROM_BANK_ADDR, bank); EEPROM.update(EEPROM_OFFSET + EEPROM_BANK_ADDR, bank);
update_eeprom_checksum();
#ifdef DEBUG #ifdef DEBUG
Serial.print(F("Bank switch to: ")); Serial.print(F("Bank switch to: "));
Serial.println(bank, DEC); Serial.println(bank, DEC);
@ -483,38 +487,39 @@ int8_t num_key_base_c(uint8_t midi_note)
} }
#endif #endif
void set_volume(float master_volume, float volume_right, float volume_left) void set_volume(float master_vol, float vol_right, float vol_left)
{ {
EEPROM.update(EEPROM_OFFSET + 4 + EEPROM_MASTER_VOLUME_ADDR, uint8_t(master_volume * 256)); EEPROM.update(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR, master_vol * UCHAR_MAX);
EEPROM.update(EEPROM_OFFSET + 4 + EEPROM_VOLUME_RIGHT_ADDR, uint8_t(volume_right * 256)); EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR, vol_right * UCHAR_MAX);
EEPROM.update(EEPROM_OFFSET + 4 + EEPROM_VOLUME_LEFT_ADDR, uint8_t(volume_left * 256)); EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR, vol_left * UCHAR_MAX);
update_eeprom_checksum();
#ifdef DEBUG #ifdef DEBUG
uint8_t tmp; uint8_t tmp;
Serial.print(F("Setting volume: VOL=")); Serial.print(F("Setting volume: VOL="));
Serial.print(master_volume, DEC); Serial.print(master_vol, DEC);
Serial.print(F("[")); Serial.print(F("["));
tmp = EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_MASTER_VOLUME_ADDR); tmp = EEPROM.read(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR);
Serial.print(tmp, DEC); Serial.print(tmp, DEC);
Serial.print(F("/")); Serial.print(F("/"));
Serial.print(float(tmp) / 256, DEC); Serial.print(float(tmp) / UCHAR_MAX, DEC);
Serial.print(F("] VOL_L=")); Serial.print(F("] VOL_L="));
Serial.print(volume_left, DEC); Serial.print(vol_left, DEC);
Serial.print(F("[")); Serial.print(F("["));
tmp = EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_VOLUME_LEFT_ADDR); tmp = EEPROM.read(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR);
Serial.print(tmp, DEC); Serial.print(tmp, DEC);
Serial.print(F("/")); Serial.print(F("/"));
Serial.print(float(tmp) / 256, DEC); Serial.print(float(tmp) / UCHAR_MAX, DEC);
Serial.print(F("] VOL_R=")); Serial.print(F("] VOL_R="));
Serial.print(volume_right, DEC); Serial.print(vol_right, DEC);
Serial.print(F("[")); Serial.print(F("["));
tmp = EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_VOLUME_RIGHT_ADDR); tmp = EEPROM.read(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR);
Serial.print(tmp, DEC); Serial.print(tmp, DEC);
Serial.print(F("/")); Serial.print(F("/"));
Serial.print(float(tmp) / 256, DEC); Serial.print(float(tmp) / UCHAR_MAX, DEC);
Serial.println(F("]")); Serial.println(F("]"));
#endif #endif
sgtl5000_1.dacVolume(master_volume * volume_left, master_volume * volume_right); sgtl5000_1.dacVolume(master_vol * vol_left, master_vol * vol_right);
} }
void handle_sysex_parameter(const uint8_t* sysex, uint8_t len) void handle_sysex_parameter(const uint8_t* sysex, uint8_t len)
@ -585,49 +590,55 @@ void handle_sysex_parameter(const uint8_t* sysex, uint8_t len)
void initial_values_from_eeprom(void) void initial_values_from_eeprom(void)
{ {
uint32_t crc_eeprom = read_eeprom_checksum(); uint32_t crc_eeprom = read_eeprom_checksum();
uint32_t crc = eeprom_crc32(EEPROM_OFFSET + 4, EEPROM_DATA_LENGTH); uint32_t crc = eeprom_crc32(EEPROM_OFFSET, EEPROM_DATA_LENGTH);
if (crc_eeprom != crc)
{
#ifdef DEBUG #ifdef DEBUG
Serial.print(F("EEPROM checksum mismatch: 0x")); Serial.print(F("EEPROM checksum: 0x"));
Serial.print(crc_eeprom, HEX); Serial.print(crc_eeprom, HEX);
Serial.print(F("!= 0x")); Serial.print(F("!= 0x"));
Serial.print(crc, HEX); Serial.print(crc, HEX);
Serial.println(F(" - initializing EEPROM!"));
#endif #endif
if (crc_eeprom != crc)
uint8_t bank = 0; {
EEPROM.update(EEPROM_OFFSET + 4 + EEPROM_BANK_ADDR, bank); #ifdef DEBUG
uint8_t voice = 0; Serial.print(F(" - mismatch -> initializing EEPROM!"));
EEPROM.update(EEPROM_OFFSET + 4 + EEPROM_VOICE_ADDR, voice); #endif
float vol = VOLUME; EEPROM.update(EEPROM_OFFSET + EEPROM_BANK_ADDR, bank);
EEPROM.update(EEPROM_OFFSET + 4 + EEPROM_MASTER_VOLUME_ADDR, uint8_t(vol / 256)); EEPROM.update(EEPROM_OFFSET + EEPROM_VOICE_ADDR, voice);
float vol_right = 1.0; EEPROM.update(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR, uint8_t(vol * UCHAR_MAX));
EEPROM.update(EEPROM_OFFSET + 4 + EEPROM_VOLUME_RIGHT_ADDR, uint8_t(vol_right / 256)); EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR, uint8_t(vol_right * UCHAR_MAX));
float vol_left = 1.0; EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR, uint8_t(vol_left * UCHAR_MAX));
EEPROM.update(EEPROM_OFFSET + 4 + EEPROM_VOLUME_LEFT_ADDR, uint8_t(vol_left / 256)); update_eeprom_checksum();
// write crc32
uint32_t crc = eeprom_crc32(EEPROM_OFFSET + 4, EEPROM_DATA_LENGTH); // recalculate
EEPROM.write(EEPROM_OFFSET + EEPROM_CRC32_ADDR, (crc & 0xff000000) >> 24);
EEPROM.write(EEPROM_OFFSET + EEPROM_CRC32_ADDR + 1, (crc & 0x00ff0000) >> 16);
EEPROM.write(EEPROM_OFFSET + EEPROM_CRC32_ADDR + 2, (crc & 0x0000ff00) >> 8);
EEPROM.write(EEPROM_OFFSET + EEPROM_CRC32_ADDR + 3, crc & 0x000000ff);
} }
else else
{ {
bank = EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_BANK_ADDR); bank = EEPROM.read(EEPROM_OFFSET + EEPROM_BANK_ADDR);
voice = EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_VOICE_ADDR); voice = EEPROM.read(EEPROM_OFFSET + EEPROM_VOICE_ADDR);
vol = float(EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_MASTER_VOLUME_ADDR)) / 256; vol = float(EEPROM.read(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR)) / UCHAR_MAX;
vol_right = float(EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_VOLUME_RIGHT_ADDR)) / 256; vol_right = float(EEPROM.read(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR)) / UCHAR_MAX;
vol_left = float(EEPROM.read(EEPROM_OFFSET + 4 + EEPROM_VOLUME_LEFT_ADDR)) / 256; vol_left = float(EEPROM.read(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR)) / UCHAR_MAX;
} }
#ifdef DEBUG
Serial.println();
#endif
} }
uint32_t read_eeprom_checksum(void) uint32_t read_eeprom_checksum(void)
{ {
return (EEPROM[EEPROM_OFFSET + EEPROM_CRC32_ADDR] << 24 | EEPROM[EEPROM_OFFSET + EEPROM_CRC32_ADDR + 1] << 16 | EEPROM[EEPROM_OFFSET + EEPROM_CRC32_ADDR + 2] << 8 | EEPROM[EEPROM_OFFSET + EEPROM_CRC32_ADDR + 3]); return (EEPROM.read(EEPROM_CRC32_ADDR) << 24 | EEPROM.read(EEPROM_CRC32_ADDR + 1) << 16 | EEPROM.read(EEPROM_CRC32_ADDR + 2) << 8 | EEPROM.read(EEPROM_CRC32_ADDR + 3));
}
void update_eeprom_checksum(void)
{
write_eeprom_checksum(eeprom_crc32(EEPROM_OFFSET, EEPROM_DATA_LENGTH)); // recalculate crc and write to eeprom
}
void write_eeprom_checksum(uint32_t crc)
{
EEPROM.update(EEPROM_CRC32_ADDR, (crc & 0xff000000) >> 24);
EEPROM.update(EEPROM_CRC32_ADDR + 1, (crc & 0x00ff0000) >> 16);
EEPROM.update(EEPROM_CRC32_ADDR + 2, (crc & 0x0000ff00) >> 8);
EEPROM.update(EEPROM_CRC32_ADDR + 3, crc & 0x000000ff);
} }
uint32_t eeprom_crc32(uint16_t calc_start, uint16_t calc_bytes) // base code from https://www.arduino.cc/en/Tutorial/EEPROMCrc uint32_t eeprom_crc32(uint16_t calc_start, uint16_t calc_bytes) // base code from https://www.arduino.cc/en/Tutorial/EEPROMCrc
@ -644,7 +655,7 @@ uint32_t eeprom_crc32(uint16_t calc_start, uint16_t calc_bytes) // base code fro
if (calc_start + calc_bytes > EEPROM.length()) if (calc_start + calc_bytes > EEPROM.length())
calc_bytes = EEPROM.length() - calc_start; calc_bytes = EEPROM.length() - calc_start;
for (uint16_t index = calc_start ; index < calc_start + calc_bytes ; ++index) for (uint16_t index = calc_start ; index < (calc_start + calc_bytes) ; ++index)
{ {
crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4); crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4); crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);

@ -85,10 +85,10 @@
#define EEPROM_OFFSET 0 #define EEPROM_OFFSET 0
#define EEPROM_DATA_LENGTH 5 #define EEPROM_DATA_LENGTH 5
#define EEPROM_CRC32_ADDR 0 // uint32_t (= 4bytes) #define EEPROM_CRC32_ADDR EEPROM.length()-sizeof(uint32_t)
#define EEPROM_BANK_ADDR 4 #define EEPROM_BANK_ADDR 0
#define EEPROM_VOICE_ADDR 5 #define EEPROM_VOICE_ADDR 1
#define EEPROM_MASTER_VOLUME_ADDR 6 #define EEPROM_MASTER_VOLUME_ADDR 2
#define EEPROM_VOLUME_RIGHT_ADDR 7 #define EEPROM_VOLUME_RIGHT_ADDR 3
#define EEPROM_VOLUME_LEFT_ADDR 8 #define EEPROM_VOLUME_LEFT_ADDR 4

@ -150,7 +150,7 @@ void Dexed::getSamples(uint16_t n_samples, int16_t* buffer)
int32_t clip_val = val < -(1 << 24) ? 0x8000 : val >= (1 << 24) ? 0x7fff : val >> 9; int32_t clip_val = val < -(1 << 24) ? 0x8000 : val >= (1 << 24) ? 0x7fff : val >> 9;
#endif #endif
#ifdef SUM_UP_AS_INT #ifdef SUM_UP_AS_INT
//sum = buffer[i + j] + (clip_val >> REDUCE_LOUDNESS)*(float(data[DEXED_GLOBAL_PARAMETER_OFFSET+DEXED_VOICE_VOLUME])/256); //sum = buffer[i + j] + (clip_val >> REDUCE_LOUDNESS)*(float(data[DEXED_GLOBAL_PARAMETER_OFFSET+DEXED_VOICE_VOLUME])/255);
sum = buffer[i + j] + (clip_val >> REDUCE_LOUDNESS); sum = buffer[i + j] + (clip_val >> REDUCE_LOUDNESS);
if (buffer[i + j] > 0 && clip_val > 0 && sum < 0) if (buffer[i + j] > 0 && clip_val > 0 && sum < 0)
{ {

@ -39,9 +39,6 @@ bool load_sysex(uint8_t b, uint8_t v)
v %= 32; v %= 32;
b %= 10; b %= 10;
if(b==bank && v==voice)
return(true);
if (sd_card_available) if (sd_card_available)
{ {
char bankdir[3]; char bankdir[3];

Loading…
Cancel
Save