Click here to return to the Using Sigma Studio page
When you have finished designing your schematic and have compiled your project, you are ready to translate your design into microcontroller code, following these basic steps:
To access the parameters and addresses from SigmaStudio, you must have already compiled your project and exported the parameter files. After doing that, you'll have two parameter files:
You can view sample *.params and *.hex files in the Export Program and Parameters topic.
Following is an example of how you can integrate the parameters into microcontroller code (AD1940). The highlighted functions in the main function are also defined:
~~~~ Click here to view the above source code as plain text |
#include "prog_data.h" #include "param_data.h" #include "Design_IC 1.h" // *** This file is generated by SigmaStudio *** #include "Design_IC 1_REG.h" // *** This file is generated by SigmaStudio *** #include "comms.h" #define byte unsigned char // Microcontroller Code MAIN program int main(void) { // Setup SPI port this is hardware-specific SpiInit(); Mute(); // Write a program to DSP SpiWrite(PROG_START_ADR, PROGRAM_DATA, PROG_LENGTH*6); // Wait some time Wait(...); // Write a parameter file SpiWrite(PARAM_START_ADR, PARAM_DATA, PARAM_LENGTH*4); Wait(...); UnMute(); // Write a single value (0.242) to specified address // The address, MODULE_Main_Single1_VALUE, is defined in the // SigmaStudio generated header file (Design_IC 1.h). ParamWrite(MODULE_Main_Single1_VALUE, 0.242); // Example safeload of single parameter, will safeload 1.999 in 5.23 // format to address 2 // This function includes the initiate safeload to parameter RAM SafeloadSingleParamWrite(2, 1.999); // Example of a multiple parameter safeload (i.e. filter coefficients) // - loads the data to each of the five safeload locations, // - then initiates the safeload SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_B0_ADDRESS, 1.999, 0); SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_B1_ADDRESS, -.988, 1); SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_B2_ADDRESS, 1.999, 2); SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_A1_ADDRESS, -.999888, 3); SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_A2_ADDRESS, 1.999, 4); InitSafeloadParam(); return 0; } // Microcontroller Code Wait function void Wait(float time) //time to wait in milliseconds { long cycles; cycles = time / 1.422e-3; //assuming 45 MHz uC core clock speed while (cycles != 0) { --cycles; } } // Microcontroller Code Mute function void Mute() { byte corecontrol[2] = {0x00, 0x00}; //Write core control register SpiWrite(CORE_CONTROL_REG, corecontrol, 2); //Wait 20us Wait(20); } // Microcontroller Code UnMute function void UnMute() { byte corecontrol[2] = {0x02, 0x00}; //Write core control register SpiWrite(CORE_CONTROL_REG, corecontrol, 2); //Wait 20us Wait(20); } // Microcontroller SpiWrite function // The function takes the start address to be written to, an array of // data, and the length of the array (in bytes). void SpiWrite(short address, unsigned char* data, int length) { int i = 0; byte address_hi = 0; // register/RAM address high byte byte address_lo = 0; // register/RAM address lo byte address_lo = (byte)address; // get low byte of register/RAM address address_hi = (byte)(address>>8); // get high byte of address shift left by 8 bits // Fill in your write function here SPI_TX_REG = 0x00; // Write to DSP at chip address 0 SPI_TX_REG = address_hi; // Write high address byte SPI_TX_REG = address_lo; // Write lo address byte for (i=0; i<length; i++) // Write specified length of data { SPI_TX_REG = data[i]; } // Note: After each write to SPI_TX_REG, you may need to verify that the buffer // has been emptied (meaning the write has completed). This is hardware-specific. } // Microcontroller Code - functions, write a single parameter void ParamWrite(short address, float param) { byte param_hex[4] = {0x00, 0x00, 0x00, 0x00} ; // Convert decimal parameter value to 5.23 format To523(param, param_hex); SpiWrite(address, param_hex, 4); } // Microcontroller Code functions, safeload: single parameter write void SafeloadSingleParamWrite(short address, float param) { byte param_hex[4] = {0x00, 0x00, 0x00, 0x00}; //convert decimal paramater value to 5.23 format To523(param, param_hex); byte paramex_hex[5]; paramex_hex[0]=0x00; paramex_hex[1]=param_hex[0]; paramex_hex[2]=param_hex[1]; paramex_hex[3]=param_hex[2]; paramex_hex[4]=param_hex[3]; SafeParam(SAFELOAD_DATA_0, paramex_hex); SafeAddr(SAFELOAD_ADDRESS_0, address); InitSafeloadParam(); } // Microcontroller Code functions, safeload: multiple parameter write // Note: After you call this function, call InitSafeloadParam once. void SafeloadMultipleParamWrite(short address, float param, int location) { byte param_hex[4] = {0x00, 0x00, 0x00, 0x00}; //convert decimal paramater value to 5.23 format To523(param, param_hex); byte paramex_hex[5]; paramex_hex[0]=0x00; paramex_hex[1]=param_hex[0]; paramex_hex[2]=param_hex[1]; paramex_hex[3]=param_hex[2]; paramex_hex[4]=param_hex[3]; SafeParam(SAFELOAD_DATA_0+location, paramex_hex); SafeAddr(SAFELOAD_ADDRESS_0+location, address); } // Microcontroller Code - functions, safeload: Initiate the safeload // write to parameter RAM void InitSafeloadParam() { byte corecontrol[2] = {2, 0}; //Read contents of core control register SpiRead(CORE_CONTROL_REG, corecontrol, 2); //Set Initiate Safe Transfer to Param RAM bit corecontrol[1] = corecontrol[1] | 0x10; //Write back to core control register SpiWrite(CORE_CONTROL_REG, corecontrol, 2); Wait(20); //Wait 20us } //Microcontroller Code - functions, SpiRead void SpiRead(short address, unsigned char* data, int length) { int i = 0; byte address_hi = 0; //DSP register/RAM address high byte byte address_lo = 0; //DSP register/RAM address lo byte //get low byte of register/RAM address address_lo = (byte)address; //get high byte of address shift, left by 8 bits address_hi = (byte)(address>>8); //Fill in you write function here SPI_RX_REG = 0x01; //Write to DSP and set RW bit to 1 SPI_RX_REG = address_hi; //Write high address byte SPI_RX_REG = address_lo; //Write lo address byte for (i=0; i<length; i++) //read specified length of data { SPI_RX_REG = data[i]; } // Note: After each write to SPI_TX_REG, you may need to verify that the buffer has // been emptied (meaning the write has completed). This is hardware-specific. } //Microcontroller Code - functions, Safeload: SafeAddr void SafeAddr(short safe_addr, short param_addr) { byte safe_addr_lo; byte safe_addr_hi; //get low byte of register/RAM address safe_addr_lo=(byte)safe_addr; //get high byte of address shift left by 8 bits safe_addr_hi=(byte)(safe_addr>>8); byte param_addr_lo; byte param_addr_hi; //get low byte of parameter address param_addr_lo=(byte)param_addr; //get high byte of address shift left by 8 bits param_addr_hi=(byte)(param_addr>>8); SPI_TX_REG = 0x00; // Write to DSP at chip address 0 SPI_TX_REG = safe_addr_hi; //Write high byte of safeload address SPI_TX_REG = safe_addr_lo; //Write low byte of safeload address SPI_TX_REG = param_addr_hi; //Write high byte of parameter address SPI_TX_REG = param_addr_lo; //Write low byte of parameter address } //Microcontroller Code - functions, Safeload: SafeParam void SafeParam(short safe_param, unsigned char* param_data) { byte safe_param_lo; byte safe_param_hi; //get low byte of register/RAM address safe_param_lo=(byte)safe_param; //get high byte of address shift left by 8 bits safe_param_hi=(byte)(safe_param>>8); SPI_TX_REG = 0x00; //Write to DSP at chip address 0 SPI_TX_REG = safe_param_hi; //Write high byte of Safeload address SPI_TX_REG = safe_param_lo; //Write low byte of Safeload address SPI_TX_REG = param_data[4]; //Write parameter byte 4 (MSBs) SPI_TX_REG = param_data[3]; //Write parameter byte 3 SPI_TX_REG = param_data[2]; //Write parameter byte 2 SPI_TX_REG = param_data[1]; //Write parameter byte 1 SPI_TX_REG = param_data[0]; //Write parameter byte 1 (LSBs) } // Note: SafeParam and SafeAddr must be executed together. // Microcontroller code - functions: convert a float number to 5.23 format void To523(float param_dec, byte* param_hex) { long param223; long param227; //multiply decimal number by 2^23 param223 = param_dec * (1 << 23); //convert to positive binary param227 = param223 + (1 << 27); param_hex[3]=(byte)param227; //get byte 3 (LSBs) of parameter value param_hex[2]=(byte)(param227>>8); //get byte 2 of parameter value param_hex[1]=(byte)(param227>>16); //get byte 1 of parameter value //get byte 0 (MSBs) of parameter value param_hex[0]=(byte)(param227>>24); //invert sign bit to get correct sign param_hex[0] = param_hex[0] ^ 0x08; }
~~~~