I am trying to follow Microchip application note TB3216
I am successful with the ‘Hello World’ example (Chapter3) of printing a string but am struggling with the use of the printf (Chapter 4). I just want to have some simple debug output to hand. I have ‘mashed’ a print integer routine together but it seems inelegant and using printf
seems to be a better way.
The example tried to demonstrate changing the output stream of the printf
function but I get stuck on the FDEV_SETUP_STREAM
definition.
I have tried moving the location of this definition around with no success.
Please see source code, platformio.ini and output from compiler. Do you have any suggestions?
// uses demo code from Microchip TB3216 <https://ww1.microchip.com/downloads/en/Appnotes/TB3216-Getting-Started-with-USART-DS90003216.pdf>
#include <Arduino.h>
#define F_CPU 20000000
#define USART0_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5)
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
//FILE USART_stream = FDEV_SETUP_STREAM(USART0_printChar, NULL, _FDEV_SETUP_WRITE);
void USART0_init(void);
void USART0_sendChar(char c);
void USART0_sendString(char *str);
int USART0_printChar(char character, FILE *stream);
FILE USART_stream = FDEV_SETUP_STREAM(USART0_printChar, NULL, _FDEV_SETUP_WRITE);
void setup() {
// put your setup code here, to run once://
USART0_init();
stdout = &USART_stream;
USART0_sendString("\r\nStarting\r\n");
}
void loop() {
// put your main code here, to run repeatedly:
//USART0_sendString("Basic\r\n");
static int val = 10;
printf("Counter value is: %d\r\n", val--);
_delay_ms(500);
}
void USART0_init(void){
/* pin information
ATtiny402
----u---
VDD| |GND
TXD BUZ PA6| |PA3 SIG
RXD SW_M PA7| |PA0 UDIP
PWR PA1| |PA2 SW_P
--------
*/
PORTA.DIR &= ~PIN7_bm; // input
PORTA.DIR |= PIN6_bm; // output
USART0.BAUD = (uint16_t)USART0_BAUD_RATE(115200);
USART0.CTRLB |= USART_TXEN_bm;
}
void USART0_sendChar(char c){
while (!(USART0.STATUS & USART_DREIF_bm));
USART0.TXDATAL = c;
}
void USART0_sendString(char *str){
for(size_t i = 0; i < strlen(str); i++){
USART0_sendChar(str[i]);
}
}
int USART0_printChar(char character, FILE *stream){
while (!(USART0.STATUS & USART_DREIF_bm));
return 0;
}
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:ATtiny402]
platform = atmelmegaavr
board = ATtiny402
framework = arduino
board_build.f_cpu = 20000000L
upload_speed = 115200
upload_port = COM7
upload_flags =
--tool
uart
--device
attiny402
--uart
$UPLOAD_PORT
--clk
$UPLOAD_SPEED
upload_command = pymcuprog write --erase $UPLOAD_FLAGS --filename $SOURCE
Processing ATtiny402 (platform: atmelmegaavr; board: ATtiny402; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelmegaavr/ATtiny402.html
PLATFORM: Atmel megaAVR (1.6.0) > ATtiny402
HARDWARE: ATTINY402 20MHz, 256B RAM, 4KB Flash
PACKAGES:
- framework-arduino-megaavr-megatinycore @ 2.5.11
- toolchain-atmelavr @ 3.70300.220127 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 15 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\ATtiny402\src\main.cpp.o
Compiling .pio\build\ATtiny402\FrameworkArduino\WInterrupts_PC.c.o
Compiling .pio\build\ATtiny402\FrameworkArduino\WMath.cpp.o
Compiling .pio\build\ATtiny402\FrameworkArduino\abi.cpp.o
Compiling .pio\build\ATtiny402\FrameworkArduino\api\Common.cpp.o
Compiling .pio\build\ATtiny402\FrameworkArduino\api\IPAddress.cpp.o
Compiling .pio\build\ATtiny402\FrameworkArduino\api\PluggableUSB.cpp.o
Compiling .pio\build\ATtiny402\FrameworkArduino\api\Print.cpp.o
In file included from C:\Users\Peter\.platformio\packages\framework-arduino-megaavr-megatinycore\cores\megatinycore/api/Print.h:22:0,
from C:\Users\Peter\.platformio\packages\framework-arduino-megaavr-megatinycore\cores\megatinycore/api/Stream.h:25,
from C:\Users\Peter\.platformio\packages\framework-arduino-megaavr-megatinycore\cores\megatinycore/api/Client.h:22,
from C:\Users\Peter\.platformio\packages\framework-arduino-megaavr-megatinycore\cores\megatinycore/api/ArduinoAPI.h:29,
from C:\Users\Peter\.platformio\packages\framework-arduino-megaavr-megatinycore\cores\megatinycore/Arduino.h:23,
from src\main.cpp:4:
src\main.cpp:21:21: sorry, unimplemented: non-trivial designated initializers not supported
FILE USART_stream = FDEV_SETUP_STREAM(USART0_printChar, NULL, _FDEV_SETUP_WRITE);
^
src\main.cpp:21:21: sorry, unimplemented: non-trivial designated initializers not supported
src\main.cpp:21:21: sorry, unimplemented: non-trivial designated initializers not supported
*** [.pio\build\ATtiny402\src\main.cpp.o] Error 1
================================================= [FAILED] Took 7.23 seconds =================================================
* The terminal process "C:\Users\Peter\.platformio\penv\Scripts\platformio.exe 'run', '--environment', 'ATtiny402'" terminated with exit code: 1.
* Terminal will be reused by tasks, press any key to close it.