Atmega328PB and USART1

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

Do you have the arduino IDE “programmer” set to “avrisp” when attempting “burn bootloader”?
The avrdude command generated by the burn process should never have “arduino” as the programmer type...

The protocol for the Arduino as ISP was changed from stk500v1 to arduino in Arduino AVR Boards 1.6.22 and newer:

The reason is to allow ATmega32U4 boards to be used as Arduino as ISP:

The unfortunate side effect is that the reset disable capacitor is now more likely to be needed on the other boards.

But ... I checked!

/Applications/Arduino-1.8.7.app/Contents/Java/portable/packages/arduino/tools/avrdude/6.3.0-arduino14/bin/avrdude -C/Applications/Arduino-1.8.7.app/Contents/Java/portable/packages/arduino/tools/avrdude/6.3.0-arduino14/etc/avrdude.conf -v -patmega328p[b] [color=red]-cstk500v1 [/color][/b]-P/dev/cu.usbserial-FTD61T6Q -b19200 -e -Ulock:w:0x3F:m -Uefuse:w:0xFD:m -Uhfuse:w:0xDA:m -Ulfuse:w:0xFF:m
/Applications/Arduino-1.8.5.app/Contents/Java/portable/packages/arduino/tools/avrdude/6.3.0-arduino14/bin/avrdude -C/Applications/Arduino-1.8.5.app/Contents/Java/portable/packages/arduino/tools/avrdude/6.3.0-arduino14/etc/avrdude.conf -v -patmega328p [b]-cstk500v1[/b] -P/dev/cu.usbserial-FTD61T6Q -b19200 -e -Ulock:w:0x3F:m -Uefuse:w:0xFD:m -Uhfuse:w:0xDE:m -Ulfuse:w:0xFF:m

Must be because of other boards I have installed?
This one is closer to "stock":

[tt]/Applications/Arduino-1.8.7-test.app/Contents/Java/portable/packages/arduino/tools/avrdude/6.3.0-arduino14/bin/avrdude -C/Applications/Arduino-1.8.7-test.app/Contents/Java/portable/packages/arduino/tools/avrdude/6.3.0-arduino14/etc/avrdude.conf -v -patmega328p [b]-carduino[/b] -P/dev/cu.usbserial-FTD61T6Q -b19200 -e -Ulock:w:0x3F:m -Uefuse:w:0xFD:m -Uhfuse:w:0xDE:m -Ulfuse:w:0xFF:m

[/tt]

Very mysterious...

Ah. Apparently I have installed version 1.6.207 of the AVR core in most of my versions.
While the board manager thinks that this a "much more recent" version than 1.6.23, is actually apparently some sort of interim build near 1.6.21

Yeah, that was the beta test for the new toolchain released in 1.6.22, but without the programmer update. That doesn't seem like it was an ideal versioning strategy. I don't think there's any advantage to using the 1.6.20x versions over 1.6.23.

Burning bootloader from IDE finally works. Turns out that tantalum SMD cap was not good for the job. I replaced it with through hole 10uf and now it burns! Now back to original problem - will test and report back.

Thanks everyone.

Ok, so here's what's still the problem. Pins for USART0 are still taken over with Serial.begin command, even though USART1 is now default communications device. While my code now works and I have pins 10, 9 , 1 and 0 working as native 16bit PWMs, I still cannot use serial debugging. Maybe some register settings are now needed?

Looking at the datasheet, I guess I should disable transmitter:

Bit 3 – TXENn Transmitter Enable
Writing this bit to one enables the USART Transmitter. The Transmitter will override normal port operation
for the TxDn pin when enabled. The disabling of the Transmitter (writing TXEN to zero) will not become
effective until ongoing and pending transmissions are completed, i.e., when the Transmit Shift Register
and Transmit Buffer Register does not contain data to be transmitted. When disabled, the Transmitter will
no longer override the TxDn port.

Is this correct command to achieve this:

UCSRB = _BV(TXEN0);