RP2040-code/Function Generator/FunctionGenerator.cpp

688 wiersze
46 KiB
C++
Czysty Zwykły widok Historia

2023-03-27 15:58:28 +00:00
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"
#include <math.h>
#include "hardware/clocks.h"
#include "hardware/dma.h"
#include "blink.pio.h"
2023-04-29 15:08:21 +00:00
#include "DAC.pio.h"
2022-06-20 19:13:48 +00:00
2023-06-05 13:27:01 +00:00
//////////////////////////////////////
// Define GPIO connections for Pico...
//////////////////////////////////////
2022-08-17 13:22:46 +00:00
2023-03-27 15:58:28 +00:00
// Note: The SPI Port only works through specific pins, so this port is defined first.
2023-06-05 13:27:01 +00:00
// SPI Port connections... // ┌──────────┬───────────────┬─────────────┐────────────────┐
// │ PGA2040 │ Connection │ MCP41010 │ Display module │
// ├──────────┼───────────────┼─────────────┤────────────────┤
2023-06-18 14:00:47 +00:00
#define PIN_RX 16 // │ GPIO 16 │ RX/spi1_rx │ │ - │
2023-06-05 13:27:01 +00:00
//#define PIN_CS 17 // │ GPIO 17 │ CS/spi1_cs │ │ │ can this be re-defined ?
#define PIN_CLK 18 // │ GPIO 18 │ CLK/spi1_clk │ │ SCK (blue) │
#define PIN_TX 19 // │ GPIO 19 │ TX/spi1_tx │ │ SDI (green) │
2023-06-18 14:00:47 +00:00
#define Display_CS 20 // │ GPIO 20 │ Chip select │ │ SS1 (white) │
#define Level_CS 21 // │ GPIO 21 │ Chip select │ │ │
2023-06-05 13:27:01 +00:00
// └──────────┴───────────────┴─────────────┘────────────────┘
2023-03-27 15:58:28 +00:00
#define SPI_PORT spi0 // These SPI connections require the use of RP2040 SPI port 0
2023-04-01 17:42:17 +00:00
#define _A 0 // DAC channel alias
#define _B 1
2023-04-29 15:08:21 +00:00
#define _Up 1
#define _Down -1
#define _Sine_ 0 // Permited values for variable WaveForm_Type
2023-04-01 17:42:17 +00:00
#define _Square_ 1
#define _Triangle_ 2
2023-07-08 12:34:54 +00:00
//#define _DMA_ctrl_ 6
//#define _DMA_data_ 7
#define _Funct_ 3
#define _Phase_ 4
#define _Freq_ 5
#define _Level_ 6
#define _Duty_ 7
#define _Range_ 8
#define _Harmonic_ 9
2023-04-29 15:08:21 +00:00
#define eof 255 // EOF in stdio.h -is -1, but getchar returns int 255 to avoid blocking
2023-06-05 13:27:01 +00:00
#define CR 13
#define BitMapSize 256 // Match X to Y resolution
2023-04-29 15:08:21 +00:00
//#define BitMapSize 360 // won't work - DMA needs to operate as a power of 2
unsigned short DAC_channel_mask = 0 ; // Binary mask to simultaneously start all DMA channels
const uint32_t transfer_count = BitMapSize ; // Number of DMA transfers per event
2023-06-05 13:27:01 +00:00
int ParmCnt = 0, Parm[4], WaveForm_Type ; // Storage for 4 command line parameters
int SelectedChan, c, i = 0, dirn = 1, result ;
char inStr[30], outStr[2048], LastCmd[30] ; // outStr large enough to contain the HelpText string
2023-07-08 12:34:54 +00:00
const char * VerText =
"\t|--------------------|\n"
"\t| Function Generator |\n"
"\t| Version 1.0.0 |\n"
"\t| 8th July 2023 |\n"
"\t|--------------------|\n";
const char * HelpText =
2023-07-08 12:34:54 +00:00
"\tHelp...\n"
"\t ? - Help\n"
"\t V - Version\n"
2023-06-05 13:27:01 +00:00
"\t R - Resource Allocation\n"
2023-07-08 12:34:54 +00:00
"\t S - Status\n"
"\t <A/B/C>h - Frequency multiplier Hz\n"
"\t <A/B/C>k - Frequency multiplier KHz\n"
2023-06-05 13:27:01 +00:00
"\t <A/B/C>si - Sine wave\n"
"\t <A/B/C>sq - Square wave\n"
"\t <A/B/C>tr - Triangle wave\n"
"\t <A/B/C>sw - Sweep frequency (Low, High, Speed, Pause)\n"
"\t <A/B/C>frnnn - Frequency = nnn ( 0->999 )\n"
"\t <A/B/C>fr+ - Frequency + 1\n"
"\t <A/B/C>fr- - Frequency - 1\n"
"\t <A/B/C>phnnn - Phase = nnn ( 0->359 degrees )\n"
"\t <A/B/C>ph+ - Phase + 1\n"
"\t <A/B/C>ph- - Phase - 1\n"
2023-07-08 12:34:54 +00:00
"\t <A/B/C>shnnn - Sine harmonic = nnn ( 0->9 )\n"
"\t <A/B/C>sh+ - Sine harmonic + 1\n"
"\t <A/B/C>sh- - Sine harmonic - 1\n"
2023-06-18 14:00:47 +00:00
"\t <A/B/C>lennn - Level = nnn ( 0->100%% )\n"
"\t <A/B/C>le+ - Level + 1\n"
"\t <A/B/C>le- - Level - 1\n"
2023-06-05 13:27:01 +00:00
"\t <A/B/C>dunnn - Duty Cycle = nnn ( 0->100%% )\n"
"\t <A/B/C>du+ - Duty Cycle + 1\n"
"\t <A/B/C>du- - Duty Cycle - 1\n"
"\twhere...\n"
"\t <A/B/C> = DAC channel A,B or Both\n"
"\t nnn = Three digit numeric value\n";
2023-06-18 14:00:47 +00:00
static void MCP41020_Write (uint8_t _ctrl, uint8_t _data) ;
2023-06-05 13:27:01 +00:00
class DAC {
public:
PIO pio; // Class wide var to share value with setter function
2023-05-05 16:08:51 +00:00
unsigned short DAC_data[BitMapSize] __attribute__ ((aligned(2048))) ; // Align DAC data (2048d = 0800h)
2023-07-08 12:34:54 +00:00
int Phase, Funct, Harm, Freq, Level, Range, DutyC, PIOnum ;
2023-05-05 16:08:51 +00:00
uint StateMachine, ctrl_chan, data_chan, GPIO, SM_WrapBot, SM_WrapTop ; // Variabes used by the getter function...
2023-06-05 13:27:01 +00:00
char name ; // Name of this instance
2023-04-29 15:08:21 +00:00
float DAC_div ;
2022-04-13 20:21:38 +00:00
2023-06-05 13:27:01 +00:00
void StatusString () {
// Print the status line for the current DAC object.
2023-06-18 14:00:47 +00:00
char Str1[4], Str2[100] ; // ! Max line length = 100 chars !
2023-06-05 13:27:01 +00:00
Range == 1 ? strcpy(Str1,"Hz") : strcpy(Str1,"KHz") ; // Asign multiplier suffix
switch ( Funct ) { // Calculate status sting...
case _Sine_:
2023-07-08 12:34:54 +00:00
sprintf(Str2,"\tChannel %c: Freq:%03d%s Phase:%03d Level:%03d Wave:Sine Harmonic:%d\n", name, Freq, Str1, Phase, Level, Harm) ;
2023-06-05 13:27:01 +00:00
break;
case _Triangle_:
if ((DutyC == 0) || (DutyC == 100)) {
2023-06-18 14:00:47 +00:00
sprintf(Str2,"\tChannel %c: Freq:%03d%s Phase:%03d Level:%03d Wave:Sawtooth\n", name, Freq, Str1, Phase, Level) ;
2023-06-05 13:27:01 +00:00
} else {
2023-06-18 14:00:47 +00:00
sprintf(Str2,"\tChannel %c: Freq:%03d%s Phase:%03d Level:%03d Wave:Triangle Rise time:%d%%\n", name, Freq, Str1, Phase, Level, DutyC) ;
2023-06-05 13:27:01 +00:00
}
break;
case _Square_:
2023-06-18 14:00:47 +00:00
sprintf(Str2,"\tChannel %c: Freq:%03d%s Phase:%03d Level:%03d Wave:Square Duty cycle:%d%%\n", name, Freq, Str1, Phase, Level, DutyC) ;
2023-06-05 13:27:01 +00:00
}
strcat(outStr,Str2) ;
}
// Setter functions...
2023-04-06 18:32:06 +00:00
void ReInit () {
2023-04-29 15:08:21 +00:00
// Re-initialises DMA channels to their initial state.
2023-06-05 13:27:01 +00:00
// Note: 1) DMA channels are not restarted, allowing for atomic (simultaneous) restart of both DAC channels later.
2023-04-29 15:08:21 +00:00
// 2) Cannot use dma_hw->abort on chained DMA channels, so using disable and re-enable instead.
// 3) This needs to be performed across both DAC channels to ensure phase sync is maintained.
// Disable both DMA channels associated with this DAC...
hw_clear_bits(&dma_hw->ch[data_chan].al1_ctrl, DMA_CH0_CTRL_TRIG_EN_BITS);
hw_clear_bits(&dma_hw->ch[ctrl_chan].al1_ctrl, DMA_CH0_CTRL_TRIG_EN_BITS);
// Reset the data transfer DMA's to the start of the data Bitmap...
dma_channel_set_read_addr(data_chan, &DAC_data[0], false);
// Re-enable both DMA channels associated with this DAC...
hw_set_bits(&dma_hw->ch[data_chan].al1_ctrl, DMA_CH0_CTRL_TRIG_EN_BITS);
hw_set_bits(&dma_hw->ch[ctrl_chan].al1_ctrl, DMA_CH0_CTRL_TRIG_EN_BITS);
2023-04-06 18:32:06 +00:00
}
2023-06-05 13:27:01 +00:00
int Set(int _type, int _val) {
if (_type == _Freq_) {
Freq = _val ; // Frequency (numeric)
ReInit() ; // Stop and reset the DAC channel (no restart)
DACspeed(Freq * Range) ; } // Update State machine run speed
if (_type == _Range_) {
Range = _val ; // Frequency (multiplier)
ReInit() ; // Stop and reset the DAC channel (no restart)
DACspeed(Freq * Range) ; } // Update State machine run speed
if (_type == _Phase_) {
Phase = _val ; // Phase shift (0->355 degrees)
ReInit() ; // Stop and reset the DAC channel (no restart)
DataCalc() ; } // Recalc Bitmap and apply new phase value
2023-07-08 12:34:54 +00:00
if (_type == _Harmonic_) {
Harm = _val ; // Harmanic (0->9)
DataCalc() ; } // Recalc Bitmap and apply new Duty Cycle value
2023-06-05 13:27:01 +00:00
if (_type == _Duty_) {
DutyC = _val ; // Duty cycle (0->100%)
DataCalc() ; } // Recalc Bitmap and apply new Duty Cycle value
2023-06-18 14:00:47 +00:00
if (_type == _Level_) { // Level (0->100%)
if (_val > 100) _val = 100 ; // Limit max val to 100%
Level = _val ;
MCP41020_Write(SelectedChan, Level) ; // Control byte for the MCP42010 just happens to be the same value as the SelectedChan variable
StatusString () ; } // Update the terminal session
2023-06-05 13:27:01 +00:00
if (_type == _Funct_) { // Function (Sine/Triangl/Square)
Funct = _val ;
DataCalc() ; // Recalc Bitmap and apply new Function value
}
return (_val) ;
}
int Bump(int _type, int _dirn) {
2023-06-18 14:00:47 +00:00
// _type = Frequency / Phase / Level, Duty, _dirn = Up / Down (_Up = 1, _Down = -1)
2023-06-05 13:27:01 +00:00
int val = 0 ;
if (_type == _Freq_) {
Freq += _dirn ;
2023-06-18 14:00:47 +00:00
if (Freq >= 1000) Freq = 0 ; // Top Endwrap
if (Freq < 0) Freq = 999 ; // Bottom Endwrap
2023-06-05 13:27:01 +00:00
val = Freq ;
DACspeed(Freq * Range) ; }
if (_type == _Phase_) {
Phase += _dirn ;
2023-06-18 14:00:47 +00:00
if (Phase == 360) Phase = 0 ; // Top Endwrap
if (Phase < 0 ) Phase = 359 ; // Bottom Endwrap
2023-06-05 13:27:01 +00:00
val = Phase ;
DataCalc(); } // Update Bitmap data to include new DAC phase
2023-06-18 14:00:47 +00:00
if (_type == _Level_) {
Level += _dirn ;
if (Level > 100) { Level = 0 ; } // Top endwrap
if (Level < 0 ) { Level = 100 ; } // Bottom endwrap
val = Level ;
MCP41020_Write(SelectedChan, Level) ; // Control byte for the MCP42010 just happens to be the same value as the SelectedChan variable
StatusString () ; } // Update the terminal session
2023-06-05 13:27:01 +00:00
if (_type == _Duty_) {
DutyC += _dirn ;
if (DutyC > 100) { DutyC = 0 ; } // Top endwrap
if (DutyC < 0 ) { DutyC = 100 ; } // Bottom endwrap
val = DutyC ;
DataCalc(); } // Update Bitmap with new Duty Cycle value
2023-07-08 12:34:54 +00:00
if (_type == _Harmonic_) {
Harm += _dirn ;
if (Harm > 10) { Harm = 0 ; } // Top endwrap
if (Harm < 0 ) { Harm = 9 ; } // Bottom endwrap
val = Harm ;
DataCalc(); } // Update Bitmap with new Sine harmonic value
2023-06-05 13:27:01 +00:00
return (val) ;
}
2023-04-29 15:08:21 +00:00
2023-04-06 18:32:06 +00:00
void DACspeed (int _frequency) {
2023-06-05 13:27:01 +00:00
// If DAC_div exceeds 2^16 (65,536), the registers wrap around, and the State Machine clock will be incorrect.
// A slow version of the DAC State Machine is used for frequencies below 17Hz, allowing the value of DAC_div to
// be kept within range.
2023-04-01 17:42:17 +00:00
float DAC_freq = _frequency * BitMapSize; // Target frequency...
2023-04-29 15:08:21 +00:00
DAC_div = 2 * (float)clock_get_hz(clk_sys) / DAC_freq; // ...calculate the PIO clock divider required for the given Target frequency
2023-04-01 17:42:17 +00:00
float Fout = 2 * (float)clock_get_hz(clk_sys) / (BitMapSize * DAC_div); // Actual output frequency
if (_frequency >= 34) { // Fast DAC ( Frequency range from 34Hz to 999Khz )
2023-04-29 15:08:21 +00:00
SM_WrapTop = SM_WrapBot ; // SM program memory = 1 op-code
pio_sm_set_wrap (pio, StateMachine, SM_WrapBot, SM_WrapTop) ; // Fast loop (1 clock cycle)
2023-06-05 13:27:01 +00:00
// If the previous frequency was < 33Hz, we will have just shrunk the assembler from 4 op-codes down to 1.
// This leaves the State Machine program counter pointing outside of the new WRAP statement, which crashes the SM.
// To avoid this, we need to also reset the State Machine program counter...
2023-04-29 15:08:21 +00:00
pio->sm[StateMachine].instr = SM_WrapBot ; // Reset State Machine PC to start of code
pio_sm_set_clkdiv(pio, StateMachine, DAC_div); // Set the State Machine clock
} else { // Slow DAC ( 1Hz=>33Hz )
2023-04-01 17:42:17 +00:00
DAC_div = DAC_div / 64; // Adjust DAC_div to keep within useable range
DAC_freq = DAC_freq * 64;
2023-04-29 15:08:21 +00:00
SM_WrapTop = SM_WrapBot + 3 ; // SM program memory = 4 op-codes
pio_sm_set_wrap (pio, StateMachine, SM_WrapBot, SM_WrapTop) ; // slow loop (64 clock cycles)
2023-06-05 13:27:01 +00:00
// If the previous frequency was >= 34Hz, we will have just expanded the assembler code from 1 op-code up to 4.
// The State Machine program counter will still be pointing to an op-code within the new WRAP statement, so will not crash.
2023-04-29 15:08:21 +00:00
pio_sm_set_clkdiv(pio, StateMachine, DAC_div); // Set the State Machine clock speed
2023-04-01 17:42:17 +00:00
}
2023-06-18 14:00:47 +00:00
StatusString () ; // Update the terminal session
2023-04-01 17:42:17 +00:00
}
2023-04-06 18:32:06 +00:00
2023-04-01 17:42:17 +00:00
void DataCalc () {
// int i,h_index, v_offset = BitMapSize/2 - 1; // Shift sine waves up above X axis
int i,j, v_offset = 256/2 - 1; // Shift sine waves up above X axis
int _phase;
const float _2Pi = 6.283; // 2*Pi
float a,b,x1,x2,g1,g2;
// Scale the phase shift to match data size...
2023-07-08 12:34:54 +00:00
_phase = Phase * BitMapSize / 360 ; // Input range: 0 -> 360 (degrees)
2023-04-01 17:42:17 +00:00
// Output range: 0 -> 255 (bytes)
switch (Funct) {
case _Sine_:
2023-07-08 12:34:54 +00:00
Harm = Harm % 10; // Sine harmonics cycles after 7
2023-04-01 17:42:17 +00:00
for (i=0; i<BitMapSize; i++) {
2023-06-05 13:27:01 +00:00
// Add the phase offset and wrap data beyond buffer end back to the buffer start...
2023-04-06 18:32:06 +00:00
j = ( i + _phase ) % BitMapSize; // Horizontal index
2023-04-01 17:42:17 +00:00
a = v_offset * sin((float)_2Pi*i / (float)BitMapSize); // Fundamental frequency...
2023-07-08 12:34:54 +00:00
if (Harm >= 1) { a += v_offset/3 * sin((float)_2Pi*3*i / (float)BitMapSize); } // Add 3rd harmonic
if (Harm >= 2) { a += v_offset/5 * sin((float)_2Pi*5*i / (float)BitMapSize); } // Add 5th harmonic
if (Harm >= 3) { a += v_offset/7 * sin((float)_2Pi*7*i / (float)BitMapSize); } // Add 7th harmonic
if (Harm >= 4) { a += v_offset/9 * sin((float)_2Pi*9*i / (float)BitMapSize); } // Add 9th harmonic
if (Harm >= 5) { a += v_offset/11 * sin((float)_2Pi*11*i / (float)BitMapSize); } // Add 11th harmonic
if (Harm >= 6) { a += v_offset/13 * sin((float)_2Pi*13*i / (float)BitMapSize); } // Add 13th harmonic
if (Harm >= 7) { a += v_offset/15 * sin((float)_2Pi*15*i / (float)BitMapSize); } // Add 15th harmonic
if (Harm >= 8) { a += v_offset/17 * sin((float)_2Pi*17*i / (float)BitMapSize); } // Add 17th harmonic
if (Harm >= 9) { a += v_offset/19 * sin((float)_2Pi*19*i / (float)BitMapSize); } // Add 19th harmonic
2023-04-01 17:42:17 +00:00
DAC_data[j] = (int)(a)+v_offset; // Sum all harmonics and add vertical offset
}
break;
case _Square_:
b = DutyC * BitMapSize / 100; // Convert % to value
for (i=0; i<BitMapSize; i++) {
2023-04-06 18:32:06 +00:00
j = ( i + _phase ) % BitMapSize; // Horizontal index
if (b <= i) { DAC_data[j] = 0; } // First section low
else { DAC_data[j] = 255; } // Second section high
2023-04-01 17:42:17 +00:00
}
break;
case _Triangle_:
x1 = (DutyC * BitMapSize / 100) -1; // Number of data points to peak
x2 = BitMapSize - x1; // Number of data points after peak
g1 = (BitMapSize - 1) / x1; // Rising gradient (Max val = BitMapSize -1)
g2 = (BitMapSize - 1) / x2; // Falling gradient (Max val = BitMapSize -1)
for (i=0; i<BitMapSize; i++) {
2023-04-06 18:32:06 +00:00
j = ( i + _phase ) % BitMapSize; // Horizontal index
if (i <= x1) { DAC_data[j] = i * g1; } // Rising section of waveform...
if (i > x1) { DAC_data[j] = (BitMapSize - 1) - ((i - x1) * g2); } // Falling section of waveform
2023-04-01 17:42:17 +00:00
}
}
2023-06-05 13:27:01 +00:00
StatusString () ; // Update the terminal session
2023-04-01 17:42:17 +00:00
}
2022-04-20 12:32:41 +00:00
2022-06-07 18:53:53 +00:00
public:
2023-04-29 15:08:21 +00:00
// Each DAC channel consists of...
2023-06-05 13:27:01 +00:00
// BitMap data => DMA => FIFO => State Machine => GPIO pins => R-2-R module
2023-04-29 15:08:21 +00:00
// Note: The PIO clock dividers are 16-bit integer, 8-bit fractional, with first-order delta-sigma for the fractional divider.
// This means the clock divisor can vary between 1 and 65536, in increments of 1/256.
// If DAC_div exceeds 2^16 (65,536), the registers will wrap around, and the State Machine clock will be incorrect.
// For frequencies below 34Hz, an additional 63 op-code delay is inserted into the State Machine assembler code. This slows
// down the State Machine operation by a factor of 64, keeping the value of DAC_div within range.
// Parameters...
2023-06-05 13:27:01 +00:00
// _name = Name of this DAC channel instance
// _pio = Required PIO channel
// _GPIO = Port connecting to the MSB of the R-2-R resistor network.
2023-04-29 15:08:21 +00:00
// Constructor
2023-06-05 13:27:01 +00:00
int DAC_chan(char _name, PIO _pio, uint _GPIO) {
pio = _pio, GPIO = _GPIO, name = _name ; // Copy parameters to class vars
PIOnum = pio_get_index(pio) ; // Print friendly value
2023-07-08 12:34:54 +00:00
Funct = _Sine_, Freq=100, Range=1, Harm=0, Level=50, DutyC=50 ; // Start-up default values
name == 'A' ? Phase=0 : Phase=180 ; // Set Phase difference between channels
2022-06-07 18:53:53 +00:00
int _offset;
2023-04-29 15:08:21 +00:00
StateMachine = pio_claim_unused_sm(_pio, true); // Find a free state machine on the specified PIO - error if there are none.
ctrl_chan = dma_claim_unused_channel(true); // Find 2 x free DMA channels for the DAC (12 available)
data_chan = dma_claim_unused_channel(true);
// Configure the state machine to run the DAC program...
_offset = pio_add_program(_pio, &pio_DAC_program); // Use helper function included in the .pio file.
SM_WrapBot = _offset;
pio_DAC_program_init(_pio, StateMachine, _offset, _GPIO);
2022-06-07 18:53:53 +00:00
// Setup the DAC control channel...
// The control channel transfers two words into the data channel's control registers, then halts. The write address wraps on a two-word
// (eight-byte) boundary, so that the control channel writes the same two registers when it is next triggered.
dma_channel_config fc = dma_channel_get_default_config(ctrl_chan); // default configs
channel_config_set_transfer_data_size(&fc, DMA_SIZE_32); // 32-bit txfers
channel_config_set_read_increment(&fc, false); // no read incrementing
channel_config_set_write_increment(&fc, false); // no write incrementing
dma_channel_configure(
ctrl_chan,
&fc,
&dma_hw->ch[data_chan].al1_transfer_count_trig, // txfer to transfer count trigger
&transfer_count,
1,
false
);
// Setup the DAC data channel...
// 32 bit transfers. Read address increments after each transfer.
fc = dma_channel_get_default_config(data_chan);
channel_config_set_transfer_data_size(&fc, DMA_SIZE_32); // 32-bit txfers
2022-06-20 19:13:48 +00:00
channel_config_set_read_increment(&fc, true); // increment the read adddress
channel_config_set_write_increment(&fc, false); // don't increment write address
2023-04-29 15:08:21 +00:00
channel_config_set_dreq(&fc, pio_get_dreq(_pio, StateMachine, true)); // Transfer when PIO SM TX FIFO has space
2022-06-07 18:53:53 +00:00
channel_config_set_chain_to(&fc, ctrl_chan); // chain to the controller DMA channel
2022-06-20 19:13:48 +00:00
channel_config_set_ring(&fc, false, 9); // 8 bit DAC 1<<9 byte boundary on read ptr. This is why we needed alignment!
2022-06-07 18:53:53 +00:00
dma_channel_configure(
data_chan, // Channel to be configured
&fc, // The configuration we just created
2023-04-29 15:08:21 +00:00
&_pio->txf[StateMachine], // Write to FIFO
2023-04-01 17:42:17 +00:00
DAC_data, // The initial read address (AT NATURAL ALIGNMENT POINT)
2022-06-20 19:13:48 +00:00
BitMapSize, // Number of transfers; in this case each is 2 byte.
2023-03-27 15:58:28 +00:00
false // Don't start immediately. All 4 control channels need to start simultaneously
// to ensure the correct phase shift is applied.
2022-06-07 18:53:53 +00:00
);
2023-04-29 15:08:21 +00:00
DAC_channel_mask += (1u << ctrl_chan) ; // Save details of DMA control channel to global variable. This facilitates
// atomic restarts of both channels, and ensures phase lock between channels.
2023-06-05 13:27:01 +00:00
DataCalc() ; // Populate bitmap data.
DACspeed(Freq * Range) ; // Initialise State MAchine clock speed.
2022-06-07 18:53:53 +00:00
2023-04-29 15:08:21 +00:00
return(StateMachine);
2022-08-17 13:22:46 +00:00
}
2022-06-07 18:53:53 +00:00
};
2023-03-27 15:58:28 +00:00
class blink_forever { // Class to initialise a state machine to blink a GPIO pin
PIO pio ; // Class wide variables to share value with setter function
public:
2023-06-05 13:27:01 +00:00
uint pioNum, StateMachine, Freq, _offset ;
2023-04-01 17:42:17 +00:00
blink_forever(PIO _pio) {
2023-03-27 15:58:28 +00:00
pio = _pio; // transfer parameter to class wide var
2023-04-01 17:42:17 +00:00
pioNum = pio_get_index(_pio);
2023-03-27 15:58:28 +00:00
StateMachine = pio_claim_unused_sm(_pio, true); // Find a free state machine on the specified PIO - error if there are none.
_offset = pio_add_program(_pio, &pio_blink_program);
2023-06-05 13:27:01 +00:00
blink_program_init(_pio, StateMachine, _offset, PICO_DEFAULT_LED_PIN );
2023-03-27 15:58:28 +00:00
pio_sm_set_enabled(_pio, StateMachine, true);
}
2022-04-13 16:49:54 +00:00
2023-04-01 17:42:17 +00:00
// Setter function...
2023-03-27 15:58:28 +00:00
void Set_Frequency(int _frequency){
2023-04-01 17:42:17 +00:00
Freq = _frequency; // Copy parm to class var
2023-04-29 15:08:21 +00:00
// Frequency scaled by 2000 as blink.pio requires this number of cycles to complete...
2023-03-27 15:58:28 +00:00
float DAC_div = (float)clock_get_hz(clk_sys) /((float)_frequency*2000);
pio_sm_set_clkdiv(pio, StateMachine, DAC_div); // Set the State Machine clock speed
2022-05-03 18:34:04 +00:00
}
2023-04-01 17:42:17 +00:00
};
2023-03-27 15:58:28 +00:00
2023-06-05 13:27:01 +00:00
void SysInfo ( DAC DAC[], blink_forever LED_blinky) {
2023-04-29 15:08:21 +00:00
// Print system and resource allocation details...
2023-06-05 13:27:01 +00:00
sprintf(outStr,"\t|-----------------------------------------------------------|\n"
2023-07-08 12:34:54 +00:00
"\t| Resource allocation... |\n"
"\t| RP2040 Clock: %4dMHz |\n"
2023-06-05 13:27:01 +00:00
"\t|-----------------------------|-----------------------------|\n"
"\t| LED blinker | |\n"
"\t|-----------------------------| |\n"
2023-07-08 12:34:54 +00:00
"\t| PIO: %2d | |\n"
"\t| State machine: %2d | |\n"
"\t| GPIO: %2d | |\n"
"\t| Frequency: %2dHz | |\n"
2023-06-05 13:27:01 +00:00
"\t|-----------------------------|-----------------------------|\n"
"\t| DAC Channel A | DAC Channel B |\n"
"\t|-----------------------------|-----------------------------|\n"
2023-07-08 12:34:54 +00:00
"\t| Frequency: %3d | Frequency: %3d |\n"
"\t| Divider: %10.3f | Divider: %10.3f |\n"
"\t| Phase: %3d | Phase: %3d |\n"
"\t| Duty cycle: %3d | Duty cycle: %3d |\n"
"\t| Sine harmonic: %1d | Sine harmonic: %1d |\n"
2023-06-05 13:27:01 +00:00
"\t|-----------------------------|-----------------------------|\n"
2023-07-08 12:34:54 +00:00
"\t| PIO: %d | PIO: %d |\n"
"\t| State machine: %d | State machine: %d |\n"
"\t| GPIO: %d->%d | GPIO: %d->%d |\n"
"\t| *BM size: %8d | *BM size: %8d |\n"
"\t| *BM start: %x | *BM start: %x |\n"
"\t| Wrap Bottom: %2x | Wrap Bottom: %2x |\n"
"\t| Wrap Top: %2x | Wrap Top: %2x |\n"
"\t| DMA ctrl: %2d | DMA ctrl: %2d |\n"
"\t| DMA data: %2d | DMA data: %2d |\n"
"\t|-----------------------------|-----------------------------|\n"
"\t *BM = Bit map\n",
(int)clock_get_hz(clk_sys)/1000000,
2023-06-05 13:27:01 +00:00
LED_blinky.pioNum, LED_blinky.StateMachine, PICO_DEFAULT_LED_PIN, LED_blinky.Freq,
DAC[_A].Freq, DAC[_B].Freq,
2023-07-08 12:34:54 +00:00
DAC[_A].DAC_div, DAC[_B].DAC_div,
2023-06-05 13:27:01 +00:00
DAC[_A].Phase, DAC[_B].Phase,
DAC[_A].DutyC, DAC[_B].DutyC,
2023-07-08 12:34:54 +00:00
DAC[_A].Harm, DAC[_B].Harm,
2023-06-05 13:27:01 +00:00
DAC[_A].PIOnum, DAC[_B].PIOnum,
2023-07-08 12:34:54 +00:00
DAC[_A].StateMachine, DAC[_B].StateMachine,
2023-06-05 13:27:01 +00:00
DAC[_A].GPIO, DAC[_A].GPIO+7, DAC[_B].GPIO, DAC[_B].GPIO+7,
BitMapSize, BitMapSize,
(int)&DAC[_A].DAC_data[0], (int)&DAC[_B].DAC_data[0],
DAC[_A].SM_WrapBot, DAC[_B].SM_WrapBot,
DAC[_A].SM_WrapTop, DAC[_B].SM_WrapTop,
DAC[_A].ctrl_chan, DAC[_B].ctrl_chan,
DAC[_A].data_chan, DAC[_B].data_chan );
2022-05-16 18:51:47 +00:00
}
2023-06-18 14:00:47 +00:00
static inline void cs_select(int _gpio) {
2022-05-28 12:58:58 +00:00
asm volatile("nop \n nop \n nop");
2023-06-18 14:00:47 +00:00
gpio_put(_gpio, 0); // Active low
2022-05-28 12:58:58 +00:00
asm volatile("nop \n nop \n nop");
}
2023-06-18 14:00:47 +00:00
static inline void cs_deselect(int _gpio) {
2022-05-28 12:58:58 +00:00
asm volatile("nop \n nop \n nop");
2023-06-18 14:00:47 +00:00
gpio_put(_gpio, 1);
2022-05-28 12:58:58 +00:00
asm volatile("nop \n nop \n nop");
}
2023-06-05 13:27:01 +00:00
static void SPI_Display_Write(int _data) {
2022-05-28 12:58:58 +00:00
uint8_t buff[2];
2023-03-27 15:58:28 +00:00
buff[0] = _data / 256; // MSB data
buff[1] = _data % 256; // LSB data
2023-06-18 14:00:47 +00:00
cs_select(Display_CS);
2022-05-28 12:58:58 +00:00
spi_write_blocking(SPI_PORT, buff, 2);
2023-06-18 14:00:47 +00:00
cs_deselect(Display_CS);
2022-05-28 12:58:58 +00:00
}
2023-06-18 14:00:47 +00:00
static void MCP41020_Write (uint8_t _ctrl, uint8_t _data) {
// Adds a control byte and converts level from percentage to absolute value, before
// transmitting over SPI bus.
uint8_t buff[2];
buff[0] = _ctrl | 0x10 ; // Set command bit to Write data
buff[1] = _data * 2.55 ; // Data byte (100%->255)
cs_select(Level_CS) ;
spi_write_blocking(SPI_PORT, buff, 2) ;
cs_deselect(Level_CS) ;
}
2023-06-05 13:27:01 +00:00
static void getLine() {
char *pPos = (char *)inStr ; // Pointer to start of Global input string
2023-03-27 15:58:28 +00:00
while(1) {
2023-06-05 13:27:01 +00:00
c = getchar();
if (c == eof || c == '\n' || c == '\r') break ; // Non blocking exit
putchar(c); // FullDuplex echo
*pPos++ = c ; // Bump pointer, store character
}
2023-06-05 13:27:01 +00:00
*pPos = '\0'; // Set string end mark
return ;
}
2022-04-13 16:49:54 +00:00
int main() {
2023-07-08 12:34:54 +00:00
set_sys_clock_khz(280000, true); // 2 x overclocking gives 1Hz=>999KHz range
2023-03-27 15:58:28 +00:00
stdio_init_all();
2023-03-27 15:58:28 +00:00
// Set SPI0 at 0.5MHz.
spi_init(SPI_PORT, 500 * 1000);
gpio_set_function(PIN_CLK, GPIO_FUNC_SPI);
gpio_set_function(PIN_TX, GPIO_FUNC_SPI);
2022-04-20 12:32:41 +00:00
2023-03-27 15:58:28 +00:00
// Chip select is active-low, so initialise to a driven-high state...
2023-06-05 13:27:01 +00:00
gpio_init(Display_CS);
gpio_set_dir(Display_CS, GPIO_OUT);
gpio_put(Display_CS, 1);
2023-06-18 14:00:47 +00:00
gpio_init(Level_CS);
gpio_set_dir(Level_CS, GPIO_OUT);
gpio_put(Level_CS, 1);
2023-07-08 12:34:54 +00:00
// Makes no noticable difference to R2R output...
2023-06-18 14:00:47 +00:00
for (int i=0; i<8; i++) {
gpio_set_slew_rate(i, GPIO_SLEW_RATE_FAST);
gpio_set_drive_strength(i, GPIO_DRIVE_STRENGTH_12MA);
2023-07-08 12:34:54 +00:00
}
2023-03-27 15:58:28 +00:00
// Initialise remaining SPI connections...
gpio_set_dir(PIN_CLK, GPIO_OUT);
gpio_set_dir(PIN_TX, GPIO_OUT);
2023-07-08 12:34:54 +00:00
DAC DAC[2]; // Array to hold the two DAC channel objects
2023-03-27 15:58:28 +00:00
2023-06-05 13:27:01 +00:00
// Instantiate objects to control the various State Machines...
// Note: Both DAC channels need to be on the same PIO to achieve
// Atomic restarts for accurate phase sync.
2023-07-08 12:34:54 +00:00
DAC[_A].DAC_chan('A',pio1,0); // First DAC channel object in array - resistor network connected to GPIO0->8
DAC[_B].DAC_chan('B',pio1,8); // Second DAC channel object in array - resistor network connected to GPIO8->16
blink_forever LED_blinky(pio0); // Onboard LED blinky object
2023-03-27 15:58:28 +00:00
2023-07-08 12:34:54 +00:00
strcpy(LastCmd,"?") ; // Hitting return will give 'Help'
2023-04-29 15:08:21 +00:00
2023-07-08 12:34:54 +00:00
SPI_Display_Write(0) ; // Zero => SPI display
MCP41020_Write(0x3, 50) ; // Both channels -> 50% output level
2023-04-29 15:08:21 +00:00
2023-07-08 12:34:54 +00:00
LED_blinky.Set_Frequency(1); // Flash LED at 1Hz- waiting for USB connection
2023-03-27 15:58:28 +00:00
2023-07-08 12:34:54 +00:00
while (!stdio_usb_connected()) { sleep_ms(100); } // Wait for USB connection...
2023-03-27 15:58:28 +00:00
2023-07-08 12:34:54 +00:00
LED_blinky.Set_Frequency(10); // Flash LED at 10Hz - USB connected.
SPI_Display_Write(DAC[_A].Freq) ; // Frequency => SPI display
2023-03-27 15:58:28 +00:00
2023-07-08 12:34:54 +00:00
// Send (optional) start-up messages to terminal...
printf(VerText) ; // Version text
// printf(HelpText) ; // Help text
// SysInfo(DAC, LED_blinky) ; printf(outStr) ; // Resource allocation table
2023-03-27 15:58:28 +00:00
2023-06-05 13:27:01 +00:00
// Atomic Restart - starting all 4 DMA channels simultaneously ensures phase sync between both DAC channels
2023-03-27 15:58:28 +00:00
dma_start_channel_mask(DAC_channel_mask);
while(1) {
2023-06-05 13:27:01 +00:00
ParmCnt=0, Parm[0]=0, Parm[1]=0, Parm[2]=0, Parm[3]=0; // Reset all command line parameters
printf(">") ; // Command prompt
2023-05-05 16:08:51 +00:00
2023-06-05 13:27:01 +00:00
getLine() ;
2023-05-05 16:08:51 +00:00
// Zero length string = 'CR' pressed...
2023-06-05 13:27:01 +00:00
if (strlen(inStr) == 0) { strcpy(inStr,LastCmd) ; // Repeat last command
printf("%s", inStr) ; }
// One character commands...
if (strlen(inStr) == 1) {
if (inStr[0] == '?') sprintf(outStr,HelpText); // Help text
2023-07-08 12:34:54 +00:00
if (inStr[0] == 'V') sprintf(outStr,VerText); // Version text
2023-06-05 13:27:01 +00:00
if (inStr[0] == 'S') { DAC[_A].StatusString() ; DAC[_B].StatusString() ; }
if (inStr[0] == 'R') SysInfo(DAC, LED_blinky);
}
// For all remaining commands, the first character selects DAC channel A or B...
if (inStr[0] == 'A') { SelectedChan = 0b0001; } // Channel A only
if (inStr[0] == 'B') { SelectedChan = 0b0010; } // Channel B only
if (inStr[0] == 'C') { SelectedChan = 0b0011; } // Channel A & B
2023-06-18 14:00:47 +00:00
// ...and if we aren't bumping a value, there will be one or more numeric parameters...
2023-06-05 13:27:01 +00:00
if ((inStr[2] != '+') && (inStr[2] != '-')) {
i = 2 ; // Skip chars 0, 1 and 2
while (i++ < strlen(inStr)-1 ) { // Starts at char 3
if ( inStr[i] == ',' ) { ParmCnt++ ; } // Next parameter
else { Parm[ParmCnt] *= 10; // Next digit. Bump the existing decimal digits
Parm[ParmCnt] += inStr[i] - '0'; } // Convert character to integer and add
2023-05-05 16:08:51 +00:00
}
}
2023-06-05 13:27:01 +00:00
if (strlen(inStr) == 2) {
if (inStr[1] == 'h') { // Set Hz
if (SelectedChan & 0b01) DAC[_A].Set(_Range_,1) ;
if (SelectedChan & 0b10) DAC[_B].Set(_Range_,1) ;
dma_start_channel_mask(DAC_channel_mask); // Atomic restart both DAC channels
}
if (inStr[1] == 'k') { // Set KHz
if (SelectedChan & 0b01) DAC[_A].Set(_Range_,1000) ;
if (SelectedChan & 0b10) DAC[_B].Set(_Range_,1000) ;
dma_start_channel_mask(DAC_channel_mask); // Atomic restart both DAC channels
}
}
if ((inStr[1] == 'f') & (inStr[2] == 'r')) { // Set Frequency
if (inStr[3] == '+') { // Bump up and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Freq_,_Up) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Freq_,_Up) ;
} else if (inStr[3] == '-') { // Bump down and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Freq_,_Down) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Freq_,_Down) ;
} else { // Not a bump, so set the absolute value from Parm[0]...
if (SelectedChan & 0b01) result = DAC[_A].Set(_Freq_,Parm[0]) ;
if (SelectedChan & 0b10) result = DAC[_B].Set(_Freq_,Parm[0]) ;
dma_start_channel_mask(DAC_channel_mask); // Atomic restart both DAC channels
}
}
if ((inStr[1] == 'p') & (inStr[2] == 'h')) { // Set Phase
if (inStr[3] == '+') { // Bump up and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Phase_,_Up) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Phase_,_Up) ;
} else if (inStr[3] == '-') { // Bump down and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Phase_,_Down) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Phase_,_Down) ;
} else { // Not a bump, so set the absolute value from Parm[0]...
if (SelectedChan & 0b01) result = DAC[_A].Set(_Phase_,Parm[0]) ;
if (SelectedChan & 0b10) result = DAC[_B].Set(_Phase_,Parm[0]) ;
dma_start_channel_mask(DAC_channel_mask); // Atomic restart both DAC channels
}
}
if ((inStr[1] == 'd') & (inStr[2] == 'u')) { // Set Duty cycle
if (inStr[3] == '+') { // Bump up and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Duty_,_Up) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Duty_,_Up) ;
} else if (inStr[3] == '-') { // Bump down and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Duty_,_Down) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Duty_,_Down) ;
} else { // Not a bump, so set the absolute value from Parm[0]...
if ( Parm[0] > 100 ) Parm[0] = 100; // Hard limit @ 100%
2023-06-18 14:00:47 +00:00
if (SelectedChan & 0b01) result = DAC[_A].Set(_Duty_,Parm[0]) ;
if (SelectedChan & 0b10) result = DAC[_B].Set(_Duty_,Parm[0]) ;
}
}
2023-07-08 12:34:54 +00:00
if ((inStr[1] == 's') & (inStr[2] == 'h')) { // Set Sine Harmonics
if (inStr[3] == '+') { // Bump up and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Harmonic_,_Up) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Harmonic_,_Up) ;
} else if (inStr[3] == '-') { // Bump down and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Harmonic_,_Down) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Harmonic_,_Down) ;
} else { // Not a bump, so set the absolute value from Parm[0]...
if ( Parm[0] > 10 ) Parm[0] = 9; // Hard limit @ 9
if (SelectedChan & 0b01) result = DAC[_A].Set(_Harmonic_,Parm[0]) ;
if (SelectedChan & 0b10) result = DAC[_B].Set(_Harmonic_,Parm[0]) ;
}
}
2023-06-18 14:00:47 +00:00
if ((inStr[1] == 'l') & (inStr[2] == 'e')) { // Set level
if (inStr[3] == '+') { // Bump up and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Level_,_Up) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Level_,_Up) ;
} else if (inStr[3] == '-') { // Bump down and grab result for SPI display...
if (SelectedChan & 0b01) result = DAC[_A].Bump(_Level_,_Down) ;
if (SelectedChan & 0b10) result = DAC[_B].Bump(_Level_,_Down) ;
} else { // Not a bump, so set the absolute value from Parm[0]...
if ( Parm[0] > 255 ) Parm[0] = 255; // Hard limit @ 100%
if (SelectedChan & 0b01) result = DAC[_A].Set(_Level_,Parm[0]) ;
if (SelectedChan & 0b10) result = DAC[_B].Set(_Level_,Parm[0]) ;
2023-06-05 13:27:01 +00:00
}
}
// Next two characters select the command...
if (strlen(inStr) == 3) { // TBD - is this needed ???
if ((inStr[1] == 's') & (inStr[2] == 'i')) { // Set Sine
if (SelectedChan & 0b01) DAC[_A].Set(_Funct_,_Sine_) ;
if (SelectedChan & 0b10) DAC[_B].Set(_Funct_,_Sine_) ;
} else if ((inStr[1] == 's') & (inStr[2] == 'q')) { // Set Square
if (SelectedChan & 0b01) DAC[_A].Set(_Funct_,_Square_) ;
if (SelectedChan & 0b10) DAC[_B].Set(_Funct_,_Square_) ;
} else if ((inStr[1] == 't') & (inStr[2] == 'r')) { // Set Triangle
if (SelectedChan & 0b01) DAC[_A].Set(_Funct_,_Triangle_) ;
if (SelectedChan & 0b10) DAC[_B].Set(_Funct_,_Triangle_) ;
}
2022-05-12 19:04:40 +00:00
}
2023-06-05 13:27:01 +00:00
if ((inStr[1] == 's') & (inStr[2] == 'w')) { // Sweep
// Parm[0]=Low frequency, Parm[1]=High frequency, Parm[2]=Scan speed, Parm[3]=Low/High pause
i = Parm[0];
for (;;) {
outStr[0] = '\0' ; // Reset the string variable
if (SelectedChan & 0b01) result = DAC[_A].Set(_Freq_,i) ; // Set frequency, display status
if (SelectedChan & 0b10) result = DAC[_B].Set(_Freq_,i) ; // Set frequency, display status
dma_start_channel_mask(DAC_channel_mask); // Atomic restart all 4 DMA channels...
printf(outStr) ; // Update terminal
SPI_Display_Write(i); // Update SPI display
if (i==Parm[0]) { dirn = 1;
sleep_ms(Parm[3]); }
if (i>=Parm[1]) { dirn =-1;
sleep_ms(Parm[3]); }
dma_start_channel_mask(DAC_channel_mask); // Atomic restart both DAC channels
i = i + dirn;
c = getchar_timeout_us (0); // Non-blocking char input
if ((c>=32) & (c<=126)) { break; } // exit on keypress
sleep_ms(Parm[2]); // Speed of scan
}
}
2023-06-18 14:00:47 +00:00
if (strlen(outStr) == 0) strcpy(outStr,"\t?\n") ; // If buffer still empty indiates unknown command
2023-06-05 13:27:01 +00:00
printf(outStr) ; // Update terminal
outStr[0] = '\0' ; // Clear (reset) the string variable
SPI_Display_Write(result) ; // Update SPI display
strcpy(LastCmd, inStr) ; // Preserve last command
2022-04-13 16:49:54 +00:00
}
2023-03-27 15:58:28 +00:00
return 0;
2022-05-03 18:34:04 +00:00
}