Atmega328PB and USART1

Hi Folks,

I have bought Arduino Nano clones with Atmega328PB and all works fine apart one thing - I need to free PD0 and PD1 pins used by USART0, as they are the ones capable of playing as second 16-bit PWM output. I tried simply connecting CH340 to pins PB3 and PB4, which are TXD1 and RXD1 pins for USART1, hoping that this will work, but it didn't. What else needs to be done in order to make USART1 default serial interface for MCU? Datasheet is not clear to me.

zdravke:
hoping that this will work, but it didn't.

Please explain exactly what the problem is.

zdravke:
What else needs to be done in order to make USART1 default serial interface for MCU?

What you you mean by "default serial interface"?

Do you use an Arduino package which supports the MCU?

Yes, everything works fine with USART0. When I connect serial from CH340 to pins for USART1, there is no serial comms.

Please give a proper description of the problem. No serial comms is too broad. During upload? At runtime?

I'm not familiar with the PB version, but others might be able to point you on the right direction.

sterretje:
Please give a proper description of the problem. No serial comms is too broad. During upload? At runtime?

I'm not familiar with the PB version, but others might be able to point you on the right direction.

During upload, so probably with everything else too, but cannot get to any other point at all. I'm guessing MCU has been setup to have USART0 ready for comms after boot, unlike USART1.

zdravke:
Yes, everything works fine with USART0. When I connect serial from CH340 to pins for USART1, there is no serial comms.

Do you use an Arduino package which supports the MCU?
Which one?
Did you flash the bootloader or is there one from factory?

When I connect serial from CH340 to pins for USART1...

On-board CH340?
Maybe schematics could help.

Juraj:
Do you use an Arduino package which supports the MCU?
Which one?

I'm using Wattuino package. As I said, everything works with USART0.

zdravke:
I'm guessing MCU has been setup to have USART0 ready for comms after boot, unlike USART1.

I think that's an incorrect assumption. It has nothing to do with the MCU. It's about the bootloader.

Probably the easiest way to switch to a bootloader that uses USART1 is to use MiniCore:

It comes with ATmega328PB bootloaders that are compiled for both serial ports. You only need to open its boards.txt file in a text editor, change the upload.port property to UART1, save the file, restart the Arduino IDE, burn the bootloader.

You need to make sure the upload.port property you change is the one associated with the Tools > Clock menu selection you're using in the Arduino IDE. For example, if you are using Tools > Clock > 16 MHz external then you would change the line:

328.menu.clock.16MHz_external.upload.port=UART0

to:

328.menu.clock.16MHz_external.upload.port=UART1

If you want to stick with the wattuino hardware package then it will be more complex.

Thanks. Unfortunately, burning bootloader from IDE never worked for me. I tried burning bootloader directly with avrdude (with Arduino as ISP), but I probably have incorrect settings. Flash went fine, but board does not react to upload requests from usb. I tried this bootloader:

https://github.com/MCUdude/MiniCore/blob/master/avr/bootloaders/optiboot_flash/atmega328pb/1000000L/optiboot_flash_atmega328pb_UART0_9600_1000000L.hex

I tried to make sure USART0 works fine before burning USART1 and switching wires, but it does not. Does anyone know what are the avrdude settings for burning this bootlader? I don't even know if this is the right bootloader...

What programmer do you use? Arduino as ISP?

Juraj:
What programmer do you use? Arduino as ISP?

Yes. Same programmer and all the connections work just fine with avrdude, never worked with IDE. I just do not know the settings for this specific bootloader.

Unfortunately, burning bootloader from IDE never worked for me. I tried burning bootloader directly with avrdude (with Arduino as ISP), but I probably have incorrect settings. Flash went fine, but board does not react to upload requests from usb.

Well, you should figure out how to get that working... If you want to bootload over USART1, you MUST replace the bootloader. You can't just dismiss it as "I can't get that working." There are many tutorials on burning bootloaders. Pick one, follow it, and let us know exactly where you have problems.
Preferably, figure out what's going on...

I tried this bootloader:
https://github.com/MCUdude/MiniCore/blob/master/avr/bootloaders/optiboot_flash/atmega328pb/1000000L/optiboot_flash_atmega328pb_UART0_9600_1000000L.hex

Do you intend to run at 1MHz? And, that's a UART0 bootloader (see: it says "UART0" in the file name!)

the common error is to choose Arduino ISP instead of Arduino as ISP

Ok, I took the command line from IDE after trying to burn bootloader and changed programmer type from -carduino to -cavrisp, and it went through. However, the bootloader does not seem to work -can't upload anything over USB. This is the original command line constructed by IDE:

avrdude -CC:..\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.0.1/avrdude.conf -v -patmega328pb -carduino -e -Ulock:w:0x3f:m -Uefuse:w:0xfc:m -Uhfuse:w:0xd6:m -Ulfuse:w:0b11111111:m

I don't see here any flash switch. Is this supposed to burn bootloader at all?

zdravke:
Ok, I took the command line from IDE after trying to burn bootloader and changed programmer type from -carduino to -cavrisp, and it went through. However, the bootloader does not seem to work -can't upload anything over USB. This is the original command line constructed by IDE:

avrdude -CC:..\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.0.1/avrdude.conf -v -patmega328pb -carduino -e -Ulock:w:0x3f:m -Uefuse:w:0xfc:m -Uhfuse:w:0xd6:m -Ulfuse:w:0b11111111:m

I don't see here any flash switch. Is this supposed to burn bootloader at all?

this is the erase command

flashing command follows

Ok, I don't get it. Avrdude flashes fine with switch "-c avrisp", but not with "-c arduino" (Arduino as ISP) which is the one IDE uses. My programmer is arduino nano with ArduinoISP sketch.

To use the -carduino switch, you need to put a 10 uF capacitor between the reset and ground pin on the programmer board to prevent it from resetting when the upload starts.

Thanks, I have the cap now. Still not working though:

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Users\zdravke\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.0.1/avrdude.conf -v -patmega328pb -carduino -PCOM4 -b19200 -e -Ulock:w:0x3f:m -Uefuse:w:0xfd:m -Uhfuse:w:0xd6:m -Ulfuse:w:0b11111111:m

avrdude: Version 6.3-20171130
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "C:\Users\zdravke\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.0.1/avrdude.conf"

Using Port : COM4
Using Programmer : arduino
Overriding Baud Rate : 19200
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x74
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x74
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x74