Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

Adding of Atmega328pb

3 posters

Go to page : 1, 2, 3  Next

Go down  Message [Page 1 of 3]

1Adding of Atmega328pb Empty Adding of Atmega328pb Mon Jan 17, 2022 10:54 am

kellneka



For an university Project im trying to implement the atmega328pb based on the existing atmega328.
Using simulide_1.0.0-RC1_Sources.

I added second instances of UART, SPI and TWI.
Alle seem to have problems regarding their busses.
Using the added UART the UDR1 register isn't being written/functioning.
Using SPI the Bus seems to be busy all the Time.

Is their more definition to be done beside in the creation of the microcontroller?
Or could be there another reason?


Best regards

2Adding of Atmega328pb Empty Re: Adding of Atmega328pb Mon Jan 17, 2022 2:18 pm

arcachofo

arcachofo

Hi.

I don't know what you exactly did, bu you need to define the new Registers and Interrupts.

And some things are changing fast, better use last builds in tester section.

3Adding of Atmega328pb Empty Re: Adding of Atmega328pb Wed Jan 19, 2022 1:09 pm

kellneka



I changed to the newest Version.

What I did was:
I copied the atmega328 and adapted the registers, interrupts, appearance and periphery to match the 328pb.

So therefor I also added more instances for UART, SPI, TWI, Timer.
Though copying the existing once and changing the according registers for them.

Then I started testing of my added instances.
Timer was fine.
UART & SPI seem to have problems with the bus.
UART had a clear bus, but didn't send anything.
SPI seems to always have a busy bus.


Therefore I wanted to ask if I need to define anything else when having multiple instances of the serial interfaces.
Because If not, the problem is probably the program running on the microcontroller.

Best regards

4Adding of Atmega328pb Empty Re: Adding of Atmega328pb Wed Jan 19, 2022 4:18 pm

arcachofo

arcachofo


Therefore I wanted to ask if I need to define anything else when having multiple instances of the serial interfaces.
Because If not, the problem is probably the program running on the microcontroller.
I think you did everything.
But could be errors in your modifications and could be errors in simulide.

If you share your files I will have a look and see if I find some issue.

5Adding of Atmega328pb Empty Re: Adding of Atmega328pb Wed Jan 19, 2022 4:38 pm

kellneka



I attached the package file as well as the xml-Source files.

Attachments
Adding of Atmega328pb Attachmentatmega328pb.zip
You don't have permission to download attachments.
(6 Kb) Downloaded 6 times

arcachofo likes this post

6Adding of Atmega328pb Empty Re: Adding of Atmega328pb Wed Jan 19, 2022 5:06 pm

arcachofo

arcachofo

Everything looks OK at first glance, not sure if second Usart is different from the old ones.
At least there is a new Interrupt: "USART1_Start".
But all registers and bits are the same.

I will do some testing.

7Adding of Atmega328pb Empty Re: Adding of Atmega328pb Thu Jan 20, 2022 3:04 am

arcachofo

arcachofo

Hey Kellneka.. do you have the package file?

8Adding of Atmega328pb Empty Re: Adding of Atmega328pb Thu Jan 20, 2022 5:13 am

arcachofo

arcachofo

arcachofo wrote:Hey Kellneka.. do you have the package file?
Don't worry, I created a package file.

Both Usarts are working.
The error was in the address of UDR1.
There are more errors in registers file, for example TWI registers are not numbered, at least for TWI0:
TWAR should be TWAR0, and so on.

When testing is useful to open Mcu Monitor and watch registers you are using.
There you can find some issues, for example: UDR1 not getting the value it should, or TWAR0 doesn't exist.

arcachofo wrote:not sure if second Usart is different from the old ones.
At least there is a new Interrupt: "USART1_Start".
Looks like this interrupt is to wake up from sleep.
This will not work, but does not affect normal Usart usage.

9Adding of Atmega328pb Empty Re: Adding of Atmega328pb Thu Jan 20, 2022 8:15 am

kellneka



Ok thanks.

Where is the problem with the address of UDR1, I took it from the datasheet?

10Adding of Atmega328pb Empty Re: Adding of Atmega328pb Thu Jan 20, 2022 5:45 pm

arcachofo

arcachofo

Where is the problem with the address of UDR1, I took it from the datasheet?
In megax8pb_regsxml: ( the good address is 0x00CE )
Code:
    <register  name="UDR1"    addr="0x00C7"  reset=""  mask=""
It's very easy to make some errors is these files.

About TWI:
I realized that there is no support for several TWIs.
I will implement it and let you know when it is ready.

Same for SPI.


11Adding of Atmega328pb Empty Re: Adding of Atmega328pb Thu Jan 20, 2022 6:15 pm

kellneka



ok thx.

Regarding TWI and SPI.
Can I help?
I already tried implementing the recognition of the numbered registers,
but I guess thats not all that has to be done?

12Adding of Atmega328pb Empty Re: Adding of Atmega328pb Thu Jan 20, 2022 6:53 pm

arcachofo

arcachofo

Regarding TWI and SPI.
Can I help?
I already tried implementing the recognition of the numbered registers,
but I guess thats not all that has to be done?
It's already done.

I added atmega328pb with TWI and SPI registers and bits corrected.
Also implemented support for several TWIs and SPIs. Without this, numbered registers and bits don't work.

Not sure if you can compile from launchpad repository.
If so, you can update your local copy and compile (keep a copy of your 328pb files just in case).
If not, I will upload new builds later today.

I only tested Usart0 and 1, but not TWI or SPI.
So you can test these if you want.

13Adding of Atmega328pb Empty Re: Adding of Atmega328pb Thu Jan 20, 2022 8:21 pm

arcachofo

arcachofo

New builds uploaded.

14Adding of Atmega328pb Empty Re: Adding of Atmega328pb Mon Jan 24, 2022 12:02 pm

kellneka



Thanks


I've got an general question.
Is there a official guideline regarding syntax, Hierarchie or procedure for adding new microcontroller functions.


Best regards

15Adding of Atmega328pb Empty Re: Adding of Atmega328pb Mon Jan 24, 2022 4:00 pm

arcachofo

arcachofo

I've got an general question.
Is there a official guideline regarding syntax, Hierarchie or procedure for adding new microcontroller functions.
No, there is not official guideline.

About sintax:
For sintax, you can just have a look to the source code.
My main concern is readability, keeping the code compact, but first it must be as easily readable as possible.

For example this is more readable and more compact at the same time:
Code:
void AvrTimer800::configureClock()
{
    if( m_prIndex > 5 ) AvrTimer::configureExtClock();
    else                AvrTimer::configureClock();
}
Than this
Code:
void AvrTimer800::configureClock(){
    if (m_prIndex > 5){
        AvrTimer::configureExtClock();
    }
    else{
        AvrTimer::configureClock();
    }
}
In general brackects "{" open in next line, so funtion name or whatever is easily readable.
Spaces inside parenthesis, not outside, so the content is readable, not the "if" or "for" or "while" (those I already know so are readable).
Brackets "{ }" only if needed.
Aligning things helps readability.

Another thing: Names are not full descriptions.
Names should be as short as possible while keeping some meaning.
For example the above: configureExtClock() is already too long.
It could be: configExtClock()


About hierarchy and procedure:
New microcontroller simulation is not yet fully defined, I'm still doing changes to fit new features as they are needed.
If you wish to add new things, just let me know and I can give you some idea, then we can discuss the better aproach.

A general description:
There are some base classes for all microntrollers: core and perifericals.
And different variants are built on top of that.

For example, there is a base class "McuCore" with data structures and methods common to all microcontrollers.
Then the is a subclass "AvrCore" with the instruction decoder and little more.

For Pics the structure is more complex, there is "PicMrCore" subclassing "McuCore" for all Pic Mid Range devices, with instrunction decoder and some other stuff.
Then there is "Pic14Core" subclassing "PicMrCore" with just a few things, and "Pic14eCore" also subclassing "PicMrCore" with the extended intructions for enhanced devices.
Pics 16 bits is not yet implemented, but it will probably derive directly from "McuCore", or maybe a new common class for all Pics is needed, I will see when I get into it.

Similar thing for perifericals, for example there is a common class for all Timers.
Then for AVRs the structure is like this:
McuTimer
--------AvrTimer
----------------AvrTimer8bit
------------------------AvrTimer800
------------------------AvrTimer801
------------------------AvrTimer810
------------------------AvrTimer820
----------------AvrTimer16bit

By now there is only one variant for AVR 16 bit Timer, but if some other variant needs to be implemented, then AvrTimer16bit class will keep everything that is common and new subclasses will be added with the new variants.

General rule is never write the same code 2 times: reuse, subclass, override.
But there are special cases, if reusing increases the complexity too much, then better write the same few lines in 2 places.

16Adding of Atmega328pb Empty Re: Adding of Atmega328pb Mon Jan 24, 2022 5:21 pm

TimFisch

TimFisch

Out of personal curiosity: why is the register 0x00CE better than 0x00C7 for UDR1? 0x00C7 seem to be the register given in the datasheet.

https://wiki.mexle.hs-heilbronn.de/

17Adding of Atmega328pb Empty Re: Adding of Atmega328pb Mon Jan 24, 2022 10:29 pm

arcachofo

arcachofo

Out of personal curiosity: why is the register 0x00CE better than 0x00C7 for UDR1? 0x00C7 seem to be the register given in the datasheet.
Not better, but ar far as I know is the correct one.
First because that's the one in the datasheet (at least the datasheet I have).

And you can se the pattern:

UCSR0A
UCSR0B
UCSR0C
------------reserved
UBRR0L
UBRR0H
UDR0
------------reserved ( 0xC7 )
UCSR1A
UCSR1B
UCSR1C
------------reserved
UBRR1L
UBRR1H
UDR1-------------- ( 0xCE )
------------reserved

Adding of Atmega328pb Reg10

But mainly because this is the one that works if you run a program compiled for atmega328pb.
I found this issue because UDR1( 0xC7 ) was not written when I execute: UDR1 = x;

Unless there is some confussion and I'm not using the same device...

18Adding of Atmega328pb Empty Re: Adding of Atmega328pb Tue Jan 25, 2022 4:27 am

arcachofo

arcachofo

I did a basic tutorial to create MCU perifericals:
https://simulide.forumotion.com/t447-implementing-new-mcu-perifericals

19Adding of Atmega328pb Empty Re: Adding of Atmega328pb Tue Jan 25, 2022 9:47 am

TimFisch

TimFisch

The interesting thing here is: it look like, that the newest datasheet is wrong here and the old one is correct...

I used the following version:
https://ww1.microchip.com/downloads/en/DeviceDoc/40001906C.pdf

This one is also available for public:
https://ww1.microchip.com/downloads/en/DeviceDoc/40001906A.pdf

In the "iom328pb.h" used in microchip studio the 0xCE is used for UDR1.
Well, "read the f...abulous Manual" doesn't beat reality...

Since it looks like @kellneka also found bugs in some code examples, I'll aggregate the issues an forward it to microchip - maybe it's time for another corrected release of the datasheet... The local Microchip engineers used to use the university canteen, I could have had a chit-chat before corona. Now, it's time for email.

https://wiki.mexle.hs-heilbronn.de/

20Adding of Atmega328pb Empty Re: Adding of Atmega328pb Tue Jan 25, 2022 10:57 am

arcachofo

arcachofo

The interesting thing here is: it look like, that the newest datasheet is wrong here and the old one is correct...
An error in a Microchip datasheet? what a surprise!! Rolling Eyes

And it's not like a "typo", in page 282 (version 40001906C):
Name: UDR
Offset 0xC6 + n*0x01 [n=0..1]

While in page 238, version 40001906A (the correct one?)
Name: UDR
Offset 0xC6 + n*0x08 [n=0..1]

At least the mistake is well done...  Adding of Atmega328pb 1f605

The funny thing is that I dowloaded the datasheet several times, and looks like I managed to always download the same one.
I'm glad that the mistery is solved.

21Adding of Atmega328pb Empty Re: Adding of Atmega328pb Tue Jan 25, 2022 11:12 am

kellneka



I tested UART, SPI and TWI for Version -R872.

UART and TWI are were functioning in my test.

I testet through adapting an example made for atmega328.
With using SPI it got stuck while waiting for data was transmitted.

Code:
SPDR0  = data;
while (!(SPSR0 & (1 << SPIF0)));

This also happend with the project for atmega328 and the adapted one for atmega328pb.
Going back to -R853 fixed the version for atmega328.

22Adding of Atmega328pb Empty Re: Adding of Atmega328pb Tue Jan 25, 2022 11:18 am

arcachofo

arcachofo

kellneka wrote:I tested UART, SPI and TWI for Version -R872.

UART and TWI are were functioning in my test.
Nice to know.

kellneka wrote:I testet through adapting an example made for atmega328.
With using SPI it got stuck while waiting for data was transmitted.
Ok, I will have a look and let you know when it's fixed.

kellneka wrote:Going back to -R853 fixed the version for atmega328.
Thanks, that's a good clue.

23Adding of Atmega328pb Empty Re: Adding of Atmega328pb Tue Jan 25, 2022 11:19 am

TimFisch

TimFisch

Based on chapter "39. Revision History" the "C type" should be the newest..
I did a small research (excel one-to-one comparison of the datasheets and iom328bp). The newer version at least seems to be "more correct".

Besides the UDR1 issue, both datasheets lack two other register desctriptions (nearly) completely. The bits/flags are kind-of-described, but not the whole byte/register.

Code:
#define UCSR0D  _SFR_MEM8(0xC3)
#define SFDE    5
#define RXS     6
#define RXSIE   7

#define UCSR1D  _SFR_MEM8(0xCB)
#define SFDE1   5
#define RXS1    6
#define RXSIE1  7

Entering Level 2: Debugging datasheets... yeah..  Adding of Atmega328pb 1f61d

https://wiki.mexle.hs-heilbronn.de/

24Adding of Atmega328pb Empty Re: Adding of Atmega328pb Tue Jan 25, 2022 11:44 am

arcachofo

arcachofo

I did a small research (excel one-to-one comparison of the datasheets and iom328bp). The newer version at least seems to be "more correct".
Good to know... now I have both versions at hand just in case.

Besides the UDR1 issue, both datasheets lack two other register desctriptions (nearly) completely.
Yes, I remember to be searching for some flag in the entire datasheet without success..
And was exactly that: RXSIE, there is another flag with similar name but with "C" instead of "S": RXCIE I think.
At some point I realized that those are 2 different Interrupts, but could not find the "S" one.
So another mistery solved.

Entering Level 2: Debugging datasheets... yeah..
Adding of Atmega328pb 1f605

With PICs I always end searching the web... It's ussually more reliable that datasheets.

25Adding of Atmega328pb Empty Re: Adding of Atmega328pb Wed Jan 26, 2022 12:58 am

arcachofo

arcachofo

kellneka wrote:I testet through adapting an example made for atmega328.
With using SPI it got stuck while waiting for data was transmitted.
Looks like I forgot something when I numbered SPI0 registers and bits:
There is an error in megax8pb_int.xml: first SPI name and bits are not numbered:
Code:
<interrupt name="SPI"      enable="SPIE"   flag="SPIF"   priority="28" vector="0x0022"/>
should be:
Code:
<interrupt name="SPI0"     enable="SPIE0"  flag="SPIF0"  priority="28" vector="0x0022"/>

Also in megax8pb_perif.xml, interrupt name should be SPI0 instead of SPI.

kellneka wrote:This also happend with the project for atmega328 and the adapted one for atmega328pb.
SPI in 328 is working for me.
Can you share your code for both 328 and 328pb?

Sponsored content



Back to top  Message [Page 1 of 3]

Go to page : 1, 2, 3  Next

Permissions in this forum:
You cannot reply to topics in this forum