Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

How to enable and disable DMA transmission ATxmega

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR XTINY/MEGAX/AVRX
View previous topic :: View next topic  
Author Message
cyklop1977

Bascom Member



Joined: 05 Jun 2011
Posts: 11

PostPosted: Fri Mar 01, 2024 6:15 pm    Post subject: How to enable and disable DMA transmission ATxmega Reply with quote

Hi



For the project I will use a 40kHz sine wave. It will be ultrasonic communication so the bytes will be modulated using ON/OFF. The sent byte is similar to UART. High state - generating a sin waveform, low state - no sin waveform.
I used data array + timer + DMA + DAC to generate a sine wave . I've never used DMA before and I have a problem with it .
I want to know how to turn on and off DMA just like TIMER stop/start .

Code:
$REGFILE = "XM128A1UDEF.DAT"
$CRYSTAL = 48000000   '48MHz
$HWSTACK = 200
$SWSTACK = 200
$FRAMESIZE = 200
$LIB "XMEGA.LIB"
ENABLE INTERRUPTS
'-------------------------------------------------------------------------------------------------------------------------------------------
'CONFIG OSC = ENABLED , PLLOSC = ENABLED , PLLMUL =  29
'CONFIG SYSCLOCK = PLL
'-------------------------------------------------------------------------------------------------------------------------------------------
CONFIG OSC = ENABLED , EXTOSC = ENABLED , PLLOSC = ENABLED , _
RANGE = 9MHZ_12MHZ , STARTUP = XTAL_16KCLK , PLLSOURCE = EXTCLOCK , PLLMUL = 6 '6 X 8MHZ = 48MHZ     0.000000020833 SEK
CONFIG SYSCLOCK = PLL , PRESCALEA = 1 , PRESCALEBC = 1_1
CONFIG PRIORITY = STATIC , VECTOR = APPLICATION , HI = ENABLED , MED = ENABLED , LO = ENABLED
'*******************************************************************************************************************************************
'*** DACb **********************************************************************************************************************************
'*******************************************************************************************************************************************
CONFIG DACB = ENABLED , IO0 = ENABLED, IO1 = ENABLED, CHANNEL = DUAL , REFERENCE = AVCC , INTERVAL = 64 , REFRESH = 64
START DACB
CONFIG PORTB.2 = OUTPUT
'*******************************************************************************************************************************************
'*** UART-COM5 *****************************************************************************************************************************
'*******************************************************************************************************************************************
CONFIG COM5 = 115200 , MODE = ASYNCHRONEOUS , PARITY = NONE , STOPBITS = 1 , DATABITS = 8
OPEN "COM5:" FOR BINARY AS #5
CONFIG SERIALIN4 = BUFFERED , SIZE = 30 , BYTEMATCH = 13
ENABLE USARTe0_RXC, LO
DIM RS232_5 AS STRING * 20               'PE3/RS232 TXD0
DIM COM5 AS LONG                         ' PE2/RS232 RXD0
'*******************************************************************************************************************************************
'*** SD DOS/AVR ****************************************************************************************************************************
'*******************************************************************************************************************************************
$INCLUDE "CONFIG_MMCSD_HC.BAS"          '• MISO        – pin D6   MODUL AL-XAVRB V2.0
$INCLUDE "CONFIG_AVR-DOS.BAS"           '• MOSI        – pin D5
DIM BTEMP1 AS BYTE                      '• SCK         – pin D7
DIM FF AS BYTE                          '• CS          – pin D4
BTEMP1 = INITFILESYSTEM(1)             '• CARD DETECT – pin D3
GBDRIVEERROR=0
FF =FREEFILE()
'*******************************************************************************************************************************************
'*** XRAM SDRAM 8MB MT48LC32M4A2P-75IT *****************************************************************************************************
'*******************************************************************************************************************************************
CONFIG XRAM = 3PORT , SDBUS = 4 , SDCOL = 10 , SDCAS = 3 , SDROW = 12 , REFRESH = 500 , INITDELAY = 3200 , MODEDELAY = 2 , ROWCYCLEDELAY = 7 , _
ROWPRECHARGEDELAY = 7 , WRDELAY = 1, ESRDELAY = 7 , ROWCOLDELAY = 7 , MODESEL3 = SDRAM , ADRSIZE3 = 8M , BASEADR3 = &H0000
$XRAMSIZE = 8000000
DIM RAM_TAB(720) AS XRAM WORD
' out &h8000,1    'zapis 1 do pamieci xram
' lol= inp(&h8000) 'odczyt
'---
' odczyt adresu zmiennej
' w=varptr(b)
' print w
' XRAM Start direct after the SRAM range of a XMEGA which is after 16KByte so the first XRAM address is 16384
' The maximum XRAM is in case of 8MByte SDRAM  = 8000000 - 16384 = 7983616 Byte
'*******************************************************************************************************************************************
'*** LCD 240x320 ILI9341 *******************************************************************************************************************
'*******************************************************************************************************************************************
CONFIG VPORT0 = E    'NIE TRZEBA PODLACZYC/DODACNA "-" Cs1=CS)  Rst=RESET    A0=DC  Si=SDI    Sclk=SCK
CONFIG GRAPHLCD = COLOR , CS1 = PORT0.0 , RST = PORT0.6 , A0 = PORT0.5 , SI = PORT0.4 , SCLK = PORT0.7
CONST PORTRAIT=0
CONST ROTATE_180 = 1
DIM OFFSET256 AS BYTE
$LIB "glcd-ILI9341_2R8_240x320.lib"
INITLCD
CLS
CONST BLACK = &B0000_0000
CONST RED = &B1110_0000
CONST GREEN = &B0001_1100
CONST BLUE = &B0000_0011
CONST MAGENTA = &B1110_0011
CONST YELLOW = &B1111_1100
CONST CYAN = &B0001_1111
CONST WHITE = &B1111_1111
CONST ORANGE = &B1111_0100
CONST BRIGHTGREEN = &B0011_1110
CONST BRIGHTBLUE = &B0001_1111
CONST DARKRED = &B1100_0000
CONST DARKGREEN = &B0001_0100
CONST DARKBLUE = &B0000_0010
OFFSET256 = 1
BOXFILL (0 , 0) - (64 , 240) , BLACK
OFFSET256 = 0
BOXFILL (0 , 0) - (255 , 240) , BLACK
SETFONT COLOR8X8
'SETFONT COLOR16X16
'******************************************************************************************************************************************
'*** ZMIENNE ******************************************************************************************************************************
'******************************************************************************************************************************************
DIM KAT AS WORD
DIM SINUS AS WORD
DIM DANE(180) AS WORD
dim skacz as word
'******************************************************************************************************************************************
'*** PROGRAM ******************************************************************************************************************************
'******************************************************************************************************************************************
RESTORE SIN_DATA:
FOR KAT = 1 TO 90
READ SINUS
DANE(KAT)=SINUS
NEXT
'******************************************************************************************************************************************
'*** DMA0 *********************************************************************************************************************************
'******************************************************************************************************************************************
CONFIG TCC0 = NORMAL , PRESCALE = 1
TCC0_PER = 65535

CONFIG EVENT_SYSTEM = DUMMY , MUX0 = TCC0_OVF
CONFIG DACB = ENABLED , Io0 = ENABLED , Io1 = ENABLED , CHANNEL = DUAL , REFERENCE = AVCC , INTERVAL = 4 , REFRESH = 16 , TRIGGER_CH0 = ENABLED , EVENT_CHANNEL = 0

CONFIG DMA = ENABLED , DOUBLEBUF = DISABLED , CPM = RR
CONFIG DMACH0 = ENABLED , BURSTLEN = 2 , CHANRPT = ENABLED , TCI = OFF , EIL = OFF , SINGLESHOT = ENABLED , SAR = BLOCK , _
SAM = INC , DAR = BURST , DAM = INC , TRIGGER = &H25  , BTC = 180 , REPEAT = 0 , SADR = VARPTR(DANE(1)) , DADR = VARPTR(DACB_CH0DATAL)

COM5=65535
DO

LCDAT      10 , 0 , "DMA_CTRL         :" ;BIN(DMA_CTRL)          ;"   ", WHITE , BLACK
LCDAT      20 , 0 , "DMA_INTFLAGS     :" ;BIN(DMA_INTFLAGS)      ;"   ", WHITE , BLACK
LCDAT      30 , 0 , "DMA_STATUS       :" ;BIN(DMA_STATUS)        ;"   ", WHITE , BLACK
LCDAT      40 , 0 , "DMA_TEMP         :" ;BIN(DMA_TEMP)          ;"   ", WHITE , BLACK
LCDAT      50 , 0 , "DMA_CH0_CTRLA    :" ;BIN(DMA_CH0_CTRLA)     ;"   ", WHITE , BLACK
LCDAT      60 , 0 , "DMA_CH0_CTRLB    :" ;BIN(DMA_CH0_CTRLB)     ;"   ", WHITE , BLACK
LCDAT      70 , 0 , "DMA_CH0_ADDRCTRL :" ;BIN(DMA_CH0_ADDRCTRL)  ;"   ", WHITE , BLACK
LCDAT      80 , 0 , "DMA_CH0_TRIGSRC  :" ;BIN(DMA_CH0_TRIGSRC)   ;"   ", WHITE , BLACK
LCDAT     110 , 0 , "DMA_CH0_TRFCNT   :" ;DMA_CH0_TRFCNT         ;"   ", WHITE , BLACK
LCDAT     120 , 0 , "DMA_CH0_REPCNT   :" ;DMA_CH0_REPCNT         ;"   ", WHITE , BLACK
LCDAT     130 , 0 , "DMA_CH0_SRCADDR0 :" ;DMA_CH0_SRCADDR0       ;"   ", WHITE , BLACK
LCDAT     140 , 0 , "DMA_CH0_SRCADDR1 :" ;DMA_CH0_SRCADDR1       ;"   ", WHITE , BLACK
LCDAT     150 , 0 , "DMA_CH0_SRCADDR2 :" ;DMA_CH0_SRCADDR2       ;"   ", WHITE , BLACK
LCDAT     160 , 0 , "DMA_CH0_DESTADDR0:" ;DMA_CH0_DESTADDR0      ;"   ", WHITE , BLACK
LCDAT     170 , 0 , "DMA_CH0_DESTADDR0:" ;DMA_CH0_DESTADDR1      ;"   ", WHITE , BLACK
LCDAT     180 , 0 , "DMA_CH0_DESTADDR0:" ;DMA_CH0_DESTADDR2      ;"   ", WHITE , BLACK

LCDAT 200 , 0 , "COM5 ";COM5 , WHITE, BLACK
TCC0_PER=COM5
LOOP
END
'-------------------------------------------------------------------------------------------------------------------------------------------
'$INCLUDE "COLOR16x16.FONT"
$INCLUDE "COLOR8x8.FONT"
'-------------------------------------------------------------------------------------------------------------------------------------------
SERIAL4CHARMATCH:
PUSHALL
INPUT #5 , RS232_5 NOECHO
COM5 = VAL(RS232_5)
POPALL
RETURN

'DMA_INT:
'RETURN

SIN_DATA:
DATA 2081%,2216%,2352%,2485%,2617%,2745%,2871%,2992%,3109%,3220%,3326%,3425%,3518%,3604%,3682%,3752%
DATA 3814%,3867%,3911%,3947%,3972%,3989%,3996%,3994%,3982%,3961%,3930%,3890%,3841%,3784%,3718%,3644%
DATA 3562%,3473%,3376%,3274%,3165%,3051%,2932%,2808%,2681%,2551%,2419%,2284%,2149%,2012%,1877%,1741%
DATA 1608%,1476%,1348%,1222%,1101%,984%,873%,767%,668%,575%,489%,411%,341%,279%,226%,182%
DATA 146%,121%,104%,97%,99%,111%,132%,163%,203%,252%,309%,375%,449%,531%,620%,717%
DATA 819%,928%,1042%,1161%,1285%,1412%,1542%,1674%,1809%,1944%
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Fri Mar 01, 2024 9:33 pm    Post subject: Reply with quote

Configuration for DMA as well for DAC is reusable, you can issue more than one config, at one you enable, at the other disable it.
As a most simple approach you can try to switch the DAC pin to input. This is a solid way to modulate PWM output in many AVRs.
If it won't work, next obvious way is to disable/enable the DAC via config.
Back to top
View user's profile
cyklop1977

Bascom Member



Joined: 05 Jun 2011
Posts: 11

PostPosted: Sat Mar 02, 2024 10:42 am    Post subject: Reply with quote

I tried turning off/on the DAC or TIMER and it works. Personally, I want to do it using DMA. I also want to know how DMA works. Configuration itself is not a problem, it's about the simplest DMA START/STOP control (similar to TIMER)
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sat Mar 02, 2024 1:51 pm    Post subject: Reply with quote

cyklop1977 wrote:
Configuration itself is not a problem, it's about the simplest DMA START/STOP control (similar to TIMER)

Instead of reusing the Config-command you can look up the appropriate register in the datasheet and enable/disable DMA that way.
You won't find a command like Start/Stop as provided for timers.
Quote:
I tried turning off/on the DAC or TIMER and it works. Personally, I want to do it using DMA.

You want to use DMA to stop DMA?
There may be interrupts attached, which allow such.
Quote:
I also want to know how DMA works.

It's unlikely you will get a complete explanation of DMA here, but the Internet is full of it.
For details look into the processor's datasheet or application notes.

Basically Direct Memory Access is a separate processor unit like a timer, but one which allows processing of data without putting load on the main processing unit.
It can shift data into a DAC working in the background like in your case, while in the foreground other things are done, like updating the display.

Which btw. is updated at unnecessary speed, also using COM5 as dim'd value is at best questionable.
Back to top
View user's profile
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 586
Location: Cleveland, OH

usa.gif
PostPosted: Sat Mar 02, 2024 6:54 pm    Post subject: Reply with quote

The Xmegas are such great micros.

I was just chatting with Mark this past week about some projects requiring more processing power than the usual 16 or 20 or 24 MHz AVR !

I see that you are over-clocking your Xmega at 48 MHz, (beyond the spec'd 32 MHz).
I did that, (at 48 MHz) for a DDS signal generator and for a project that was monitoring another bus.
On AVR-Freaks Atomic Zombie overclocked them at 65 or 68 (I don't recall) MHz for generating video and audio, (making it a display driver chip).
I never had reliable operations beyond 48 MHz.
You probably already know this, but the analog functions and the EEPROMs don't overclock well, just the core digital logic does.
So be careful how fast you feed the DAC, etc.
(Or at least verify that it is meeting your performance needs).

It has been many years since I looked at uC architectures, and I ought to stay out of this, but...
As you read up on DMA know that on the Xmega the DMA controller and the core processor do not both run simultaneously, (IIRC).
They still share a data bus.
What that means is that the DMA is still much faster at reading and writing data to the memory than using the core processor to R/W to an address, store the data, increment an address, and do it again.... That takes the processor many clock cycles. The DMA hardware does it much more efficiently, (faster).
But while the DMA controller is doing that, I don't think that on the Xmegas that the core processor is simultaneously running other code.

IIRC, there are other processors where the DMA controller is, essentially, running simultaneously with the core processor, and is making its memory accesses between those of the core processor.

Understanding that difference might help as you read up on how DMA works, and how it works on the XMega.

Others can correct me if I have misunderstood how the XMega DMA works.

Good luck with your project!

JC
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR XTINY/MEGAX/AVRX All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum