Skip to content

Commit

Permalink
Add 0_Template example
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
MCUdude committed Oct 6, 2020
1 parent d027cf2 commit 1f588ed
Show file tree
Hide file tree
Showing 18 changed files with 8,975 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ There are some rules you need to follow in order for the script to work as inten


### Exporting a SigmaStudio project
This guide assumes you created your project based on the __Tutorial_0__ project file, which is a blank project with a few pre-configured settings.
This guide assumes you created your project based on the __0_Template__ project file, which is a blank project with a few pre-configured settings.
* Start out by adding all the blocks you want in your project
* Locate your SigmaStudio project folder. Open this and create a folder called **export**
* **(Skip this step if not using EEPROM)** Open the Hardware configuration tab and add an external EEPROM device by draging the E2PROM symbol into the work space and connect it to USBi's second wire connector. Make sure the EEPROM size and address matches the actual address of the EEPROM. **The EEPROM module has to be named IC_2!**
Expand Down
69 changes: 69 additions & 0 deletions examples/0_Template/0_Template.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**********************************************|
| SigmaDSP library |
| https://github.com/MCUdude/SigmaDSP |
| |
| 0_Template. |
| This example is ment to be used as a |
| template for your own projects. It brings no |
| functionality other than connecting the two |
| analog inputs directly to output 0 and 1. |
|**********************************************/

// Include SigmaDSP library
#include <SigmaDSP.h>

// Include generated parameter file
#include "SigmaDSP_parameters.h"


// The first parameter is the i2c address, which is defined in the parameter file.
// The next parameter is the SigmaDSP type
// An optional third parameter is the pin to physically reset the DSP
SigmaDSP dsp(DSP_I2C_ADDRESS, ADAU1701 /*,12*/);


// Only needed if an external i2c EEPROM is present + the DSP is in selfboot mode
// The first parameter is the i2c address, which is defined in the parameter file.
// The second parameter is the EEPROM size in kilobits
// An optional third parameter is the pin to toggle while writing content to EEPROM
//DSPEEPROM ee(EEPROM_I2C_ADDRESS, 256, LED_BUILTIN);


void setup()
{
Serial.begin(9600);
Serial.println(F("SigmaDSP 0_Template example\n"));

// dsp and ee supports re-mapping of the SDA and SCL signals, but
// can only be done if the microcontroller supports it (ESP8266 for example)
dsp.begin(/* &Wire, SDA, SCL */);
//ee.begin(/* &Wire, SDA, SCL */);

delay(2000);


Serial.println(F("Pinging i2c lines...\n0 -> deveice is present\n2 -> device is not present"));
Serial.print(F("DSP response: "));
Serial.println(dsp.ping());
//Serial.print(F("EEPROM ping: "));
//Serial.println(ee.ping());


// Use this step if no EEPROM is present
Serial.print(F("\nLoading DSP program... "));
loadProgram(dsp);
Serial.println("Done!\n");


// Comment out the three code lines above and use this step instead if EEPROM is present
// The last parameter in writeFirmware is the FW version, and prevents the MCU from overwriting on every reboot
//ee.writeFirmware(DSP_eeprom_firmware, sizeof(DSP_eeprom_firmware), 0);
//dsp.reset();
//delay(2000); // Wait for the FW to load from the EEPROM
}


void loop()
{
// Nothing to do here!
}
275 changes: 275 additions & 0 deletions examples/0_Template/DSP_Parameter_generator.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
#####################################################################
# SIGMADSP PARAMETER FILE GENERATOR SCRIPT #
# Created by MCUdude - https://github.com/MCUdude/SigmaDSP #
# #
# This script will search for the following files in the current #
# directory and all sub directories: #
# [ProjectName]_IC1_PARAM.h #
# [ProjectName]_IC1.h #
# [ProjectName]_IC2.h #
# E2Prom.Hex #
# #
# Here's an example on how you may arrange your project structure: #
# My_Arduino_DSP_Project/ #
# L My_Arduino_DSP_Project.ino #
# L DSP_parameter_generator.ps1 #
# L Sigma_studio_files/ #
# L [ProjectName].dspproj #
# L export/ #
# L [ProjectName]_IC1_PARAM.h #
# L [ProjectName]_IC1.h #
# L [ProjectName]_IC2.h #
# L E2Prom.Hex #
# #
# It's important that the DSP is named IC1 and the EEPROM IC2 in #
# Sigma Studio, or else this script won't work. #
# #
# Note that [ProjectName]_IC2.h and E2Prom.Hex isn't mandatory #
# files. You will not be able to load firmware to the EEPROM #
# without these files though. #
# #
# If you're not able to execute this script you have to enable #
# execution of unsigned Powershell scripts. Search for Powershell, #
# right click and select "Run as administrator". run the following #
# line: #
# #
# set-executionpolicy remotesigned #
# #
# You should now be able to run this script by double clicking it. #
#####################################################################


$outputFile = "./SigmaDSP_parameters.h"
$lf = [System.Environment]::NewLine

function Find-File ($pattern) {
$file = Get-ChildItem -Recurse $pattern
if ($null -eq $file) {
Write-Host -ForegroundColor Red "$pattern not found"
exit
}
if ($file -is [system.array]) {
Write-Host -ForegroundColor Red "Multiple files found for $pattern"
exit
}
return $file
}

function Find-OptionalFile ($pattern) {
$file = Get-ChildItem -Recurse $pattern
if ($null -eq $file) {
Write-Host -ForegroundColor Yellow "WARNING! $pattern not found"
}
if ($file -is [system.array]) {
Write-Host -ForegroundColor Red "Multiple files found for $pattern"
exit
}
return $file
}

$dspParamFile = Find-File "*_IC_1_PARAM.h"
$dspParamContent = Get-Content $dspParamFile

$eepromHexFile = Find-OptionalFile "*rom.hex"
if ($null -ne $eepromHexFile) {
$eepromHexContent = Get-Content $eepromHexFile
}
else {
Write-Host -ForegroundColor Yellow "EEPROM loading not possible"
}

$dspProgramFile = Find-File "*_IC_1.h"
$dspProgramContent = Get-Content $dspProgramFile

$eepromProgramFile = Find-OptionalFile "*_IC_2.h"
if ($null -ne $eepromProgramFile) {
$eepromProgramContent = Get-Content $eepromProgramFile
}

# Skip EEPROM array if file is not present
if ($null -eq $eepromHexFile) {
$hexContent = ""
}
else {
# Generate C style array out from passed hex file
$arrayLen = $eepromHexContent.Split(",", [System.StringSplitOptions]::RemoveEmptyEntries).Count
$hexContent = $eepromHexContent -Join $lf | Out-String
$hexContent = "/* This array contains the entire DSP program,
and should be loaded into the external i2c EEPROM */
const uint8_t PROGMEM DSP_eeprom_firmware[$arrayLen] = {`n$hexContent};"
}

# Extract relevant comments and macros
$paramContent =
$dspParamContent |
Where-Object {
$temp = $_.Split(' ')[1]; $_ -match "/* Module" -or $temp -match "_COUNT$" -or $temp -match "_FIXPT$" -or $temp -match "_VALUES$" -or $temp -match "_ADDR$"
}

$paramContent = $paramContent -join $lf | Out-String

# Track down the DSP and EEPROM i2c addresses
$addressLine = $dspProgramContent |
Where-Object {$_ -match "#define DEVICE_ADDR_IC_1" }
$hexNumberIndex = $addressLine.LastIndexOf("0x")

$dspProgAddress = $addressLine.Substring($hexNumberIndex, 4)

if ($null -ne $eepromProgramFile) {
$addressLine = $eepromProgramContent |
Where-Object {$_ -match "#define DEVICE_ADDR_IC_2" }

$hexNumberIndex = $addressLine.LastIndexOf("0x")
$eepromProgAddress = $addressLine.Substring($hexNumberIndex, 4)
$eepromProgAddressLine = "#define EEPROM_I2C_ADDRESS ($eepromProgAddress >> 1) & 0xFE"
}
else {
$eepromProgAddressLine = ""
}

# Generate program data
$dspProgramContentSlice = $dspProgramContent[26..($dspProgramContent.length - 2)]

$dspProgramData = ""
foreach ($line in $dspProgramContentSlice) {
$parts = $line.Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries)
$last = $parts[-1]

if ($line -match "/* DSP" -or $line -match "/* Register") {
$dspProgramData += "$line$lf"
}

if ($line -match "#define PROGRAM_SIZE_IC_1") {
$dspProgramData += "#define PROGRAM_SIZE $last $lf"
}

if ($line -match "#define PROGRAM_ADDR_IC_1") {
$dspProgramData += "
#define PROGRAM_ADDR $last $lf#define PROGRAM_REGSIZE 5 $lf"
}

if ($line -match "ADI_REG_TYPE Program_Data") {
$dspProgramData += "const uint8_t PROGMEM DSP_program_data[PROGRAM_SIZE] = $lf{"
}

if ($line -match "#define PARAM_SIZE_IC_1") {
$dspProgramData += "#define PARAMETER_SIZE $last $lf"
}

if ($line -match "#define PARAM_ADDR_IC_1") {
$dspProgramData += "#define PARAMETER_ADDR $last $lf#define PARAMETER_REGSIZE 4$lf"
}

if ($line -match "ADI_REG_TYPE Param_Data") {
$dspProgramData += "const uint8_t PROGMEM DSP_parameter_data[PARAMETER_SIZE] = $lf{"
}

if ($line -match "ADI_REG_TYPE R0_COREREGISTER") {
$dspProgramData += "#define CORE_REGISTER_R0_SIZE 2$lf"
$dspProgramData += "#define CORE_REGISTER_R0_ADDR 0x081C$lf"
$dspProgramData += "#define CORE_REGISTER_R0_REGSIZE 2$lf"
$dspProgramData += "const uint8_t PROGMEM DSP_core_register_R0_data[CORE_REGISTER_R0_SIZE] = $lf{$lf"
}

if ($line -match "#define R3_HWCONFIGURATION_IC_1_SIZE") {
$dspProgramData += "#define HARDWARE_CONF_SIZE $last$lf"
$dspProgramData += "#define HARDWARE_CONF_ADDR 0x081C$lf"
$dspProgramData += "#define HARDWARE_CONF_REGSIZE 1$lf$lf"
}

if ($line -match "ADI_REG_TYPE R3_HWCONFIGURATION") {
$dspProgramData += "const uint8_t PROGMEM DSP_hardware_conf_data[HARDWARE_CONF_SIZE] = $lf{"
}

if ($line -match "ADI_REG_TYPE R4_COREREGISTER") {
$dspProgramData += "#define CORE_REGISTER_R4_SIZE 2$lf"
$dspProgramData += "#define CORE_REGISTER_R4_ADDR 0x081C$lf"
$dspProgramData += "#define CORE_REGISTER_R4_REGSIZE 2$lf"
$dspProgramData += "const uint8_t PROGMEM DSP_core_register_R4_data[CORE_REGISTER_R4_SIZE] = $lf{$lf"
}


if ($line.StartsWith("0x")) {
$dspProgramData += $line
}

if ($line.StartsWith("};")) {
$dspProgramData += "};$lf$lf"
}


}

$date = get-date -Format "dd.MM.yyyy HH.mm.ss"
$output = "#ifndef SIGMADSP_PARAMETERS_H
#define SIGMADSP_PARAMETERS_H
#include <SigmaDSP.h>
/****************************************************************************
| File name: SigmaDSP_parameters.h |
| Generation tool: Powershell |
| Date and time: $date |
| |
| ADAU1701 parameter and program file header |
| SigmaDSP library and its content is developed and maintained by MCUdude. |
| https://github.com/MCUdude/SigmaDSP |
| |
| Huge thanks to the Aida DSP team who have reverse engineered a lot of the |
| Sigma DSP algorithms and made them open source and available to everyone. |
| This library would never have existed if it weren't for the Aida DSP team |
| and their incredible work. |
| |
| This file have been generated with the Sigmastudio_project_formatter.sh |
| script. This file contains all the DSP function block parameters and |
| addresses. It also contains the program that will be loaded to the |
| external EEPROM. |
| |
| The *_COUNT macro holds the number of addresses in memory each complete |
| module takes. |
| |
| The *_ADDR macro holds the current address for the module. Use this macro |
| when changing the behaviour of the modules (EQs, volume etc.). |
| |
| The *_FIXFT macros holds the default value of the module. Use this when |
| restoring the default parameters. |
| |
| The DSP_eeprom_firmware[] array contains the DSP firmware, and can be |
| loaded using the writeFirmware method in the DSPEEPROM class. |
| When stored in the external i2c EEPROM, the firmware is automatically |
| loaded into the DSP on boot if the SELFBOOT pin is tied to Vcc. |
| |
| If you want to load the DSP firmware directly without using an external |
| EEPROM, you can simply run loadProgram() (located at the bottom of this |
| file) where you pass the SigmaDSP object as the only parameter. |
| |
****************************************************************************/
/* 7-bit i2c addresses */
#define DSP_I2C_ADDRESS ($dspProgAddress >> 1) & 0xFE
$eepromProgAddressLine
// Define readout macro as empty
#define SIGMASTUDIOTYPE_SPECIAL(x) (x)
$paramContent
$hexContent
$dspProgramData
/* Run this function to load DSP firmware directly */
void loadProgram(SigmaDSP &myDSP)
{
myDSP.writeRegister(CORE_REGISTER_R0_ADDR, CORE_REGISTER_R0_SIZE, DSP_core_register_R0_data);
myDSP.writeRegisterBlock(PROGRAM_ADDR, PROGRAM_SIZE, DSP_program_data, PROGRAM_REGSIZE);
myDSP.writeRegisterBlock(PARAMETER_ADDR, PARAMETER_SIZE, DSP_parameter_data, PARAMETER_REGSIZE);
myDSP.writeRegister(HARDWARE_CONF_ADDR, HARDWARE_CONF_SIZE, DSP_hardware_conf_data);
myDSP.writeRegister(CORE_REGISTER_R4_ADDR, CORE_REGISTER_R4_SIZE, DSP_core_register_R4_data);
}
#endif
"

Set-Content -Path $outputFile -Value $output.Replace(",}", "}")
Write-Host -ForegroundColor Green "Output written to $outputFile"

Loading

0 comments on commit 1f588ed

Please sign in to comment.