ARMbasic problem to read/write DAC registers

Questions about the BASICtools and MakeItC
Post Reply
C_hater
Posts: 9
Joined: Wed Sep 02, 2020 8:55 am
Location: Regensburg, Germany

ARMbasic problem to read/write DAC registers

Post by C_hater »

Hi,
during my evaluation of the SAMD21 (Board is similar to RobotDynM0) I wanted to test the on board D/A converter.
As there is no direct statement in ARMBasic to access this peripheral ( May be something is planned by Coridium for an update) I tried the way via the registers.
Simple first step by step test: read the initial value of the register , modify it with a register write and read again.
Here is the simple program:

#include "pins_RobotDynM0.bas" ' Pin definitions for RobotDyn M0
#include "samd21.bas"
Dim Reg as Integer
Main:
Reg = RD_Byte(DAC_CTRLA)
Print "DAC_CTRLA-Register: "; Reg
'Reg=245
'WR_BYTE(DAC_CTRLA, 245)
'Reg = RD_Byte(DAC_CTRLA)
'Print "DAC_CTRLA-Register: "; Reg
End

Ok fine, the IDE showed:
DAC_CTRLA-Register: 244

... Finished in 18 ms
Now when uncommenting the lines in the program to write and read again:
the register content is still printed:

DAC_CTRLA-Register: 244

but then the program stucks, no further reaction. The "stop buttom" of the IDE works but then the IDE does not "react" for a certain time and when closing the IDE the Windows message appears that the program gives no feedback.
The board still blocks the com port and has to be unplugged and plugged in again.

Any idea what happened and how I can access the registers properly ???
By the way the same effect showed up when I in a modified program tried to read out two registers successively, even with others than the DAC registers



C_hater
Posts: 9
Joined: Wed Sep 02, 2020 8:55 am
Location: Regensburg, Germany

Re: ARMbasic problem to read/write DAC registers

Post by C_hater »

Addendum:
also tried to define Reg as a Byte Array ( ARM Basic allows only Byte values within an array ) thinking the root cause is using integer values to work on the registers. But still the problem occurs.

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: ARMbasic problem to read/write DAC registers

Post by basicchip »

There is an index item for Register Access in the Help files (using the C pointer syntax)

https://www.coridium.us/ARMhelp/scr/HWaccess.html

Make sure the clocks are enabled for the DAC.

* access must be word aligned (addresses multiples of 4)
WR_HALF/RD_HALF have to be half word aligned (addresses multiples of 2)

Always double check our register definition file (if we have not used a register, the address may be wrong, please let us know when that is the case)

Notes in SAMD21 user manual, says many DAC registers can only be written when the DAC is disabled

C_hater
Posts: 9
Joined: Wed Sep 02, 2020 8:55 am
Location: Regensburg, Germany

Re: ARMbasic problem to read/write DAC registers

Post by C_hater »

Bruce, I still have no chance to directly access in general the registers using my robodynM0. When doing so in combination with a print command he program and the IDE execution stuck and the port connected to my computer freezes.
I obeyed your advice and used the correct register access depending on the length of the register as noted in SAMD21.bas.
I compared the register addresses in the file with the datasheet. Correct!
Last attempt to exclude that I did in general something wrong with the initialization of registers ( not enabling the peripheral clock etc..) I added a register access command line in a sample file printing ADC results based on the ARMBasic ADC Access commands.
This assures that all corresponding registers are first initialized properly by ARMbaisc. When using only the ARMbasic command: AD(A0) the conversion results are printed properly. But when adding the lines with the direct register access to the 16bit ADC_result register and its printing in the next line the program again stucks immediately after having printed out the first conversation result from the original Basicchip command.

Here is the code:
#include "pins_RobotDynM0.bas" ' Pin definitions for RobotDyn M0
#include "SAMD21.bas"
dim x,directvalue as integer
dim Datafield(512) as integer
main:
print "SAMD21 ADC Test"
Do
for x = 0 to 511
Datafield(x) = AD(A0)
Print x,Datafield(x)
Print "ADC Result Access via Register"
directvalue = RD_HALF(ADC_RESULT)
Print directvalue
next x
Wait(1000)
Loop
end

Could you please verify. Especially because the com port stucks when I print register content. Could it be that there is is a bug in your SAMD21G Basic or does the used board probably causes the problem?

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: ARMbasic problem to read/write DAC registers

Post by basicchip »

The SAMD21 uses a lot of byte and half-word registers, something that most other ARMs do NOT use.

Our SAMD21.bas file is written as if all the registers are word oriented. We should change that to define JUST the address.

We have defined them as *&H42004800 for the DAC for instance. What that is is a 32 bit read to the address &H42004800.

So your code is doing--
RD_BYTE( * &H4200401A ) ' this is trying to read a byte by whatever is at the memory location

So this is our error, we should define registers as just addresses to accommodate the many byte registers of the SAMD21. That file is posted below and will be included in the next installer release.

With that change we will have to get use to accessing registers as one of the following--

*REGISTER = something
WR_HALF(REGISTER, something16bits)
WR_BYTE(REGISTER, something8bits)

Where for all our other ARMs that would be

REGISTER = something ' all ARMs we have looked at from other vendors word align registers
Attachments
SAMD21.bas
(47.4 KiB) Downloaded 350 times

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: ARMbasic problem to read/write DAC registers

Post by basicchip »

I've updated the library in the installer for the SAMD21, as well as the SAMD21_timer example.

There is a way you might have seen this. One option in BASICtools is to show the pre-processor output, which loads that into your editor.

It would have shown -

directvalue = RD_HALF(*&H4200401A) ' note the * is equivalent to RD_HALF(RD_WORD(&H4200401A))

when you actually want

directvalue = RD_HALF(&H4200401A)

C_hater
Posts: 9
Joined: Wed Sep 02, 2020 8:55 am
Location: Regensburg, Germany

Re: ARMbasic problem to read/write DAC registers

Post by C_hater »

The direct ADC register readout in my example works now!
I understand your first approach to handle the SAMD21 in a similar way like the other ARMs you use in your products. But comming from the "8-bit Atmel AVR world like me it was not foreseeable that all registers independent from their real length should be addressed as word.
But anyhow thanks for your update. Hope it is helpful for other users not to step in the same trap.
regards C_hater

Post Reply