View previous topic :: View next topic |
Author |
Message |
kasny.ondrej
Joined: 03 Feb 2024 Posts: 52

|
Posted: Wed Jul 17, 2024 3:44 pm Post subject: ATXtiny3217 USART RX buffer bug? |
|
|
Hello Mark me again,
I have some troubles with USART. I spent two days until I figured that Input statement does not clear flags or dont read whole buffer thus MCU goes wild. Now facing different problem. Messages between two xtinys are not 100% realible. Not every message is complete.
Changing baudrate won't solve anything.
Recieve side code:
Code: |
$regfile = "atxtiny3217.dat"
$crystal = 8000000
$hwstack = 50
$swstack = 50
$framesize = 60
'$sim
'$dbg
Config Sysclock = 16_20mhz , Prescale = 2
'-----------------------UART CONFIG------------------------------------------------
Config Com1 = 38400 , Mode = Asynchroneous , Parity = None , Databits = 8 , Stopbits = 1 , Baud_offset = Osc16_3v3
Config Serialin = Buffered , Size = 40
'---------------------------SPI CONFIG------------------------------------------------
Config Spi0 = Hard , Clockdiv = Clk16 , Data_order = Msb , Mode = 0 , Master = Yes , Ss = Auto
do
If Ischarwaiting() = 1 Then
Do
Uart_input_byte = Inkey()
Uart_income_data = Uart_income_data + Chr(uart_input_byte)
Loop Until Ischarwaiting() = 0
print Uart_income_data
end if
waitms 15
loop
|
I found that problem is in reciving side. When I send messages into xtiny via USART (USB 232 converter) and then print them back, they are often separated, but when I am only printing anything that is not from input buffer it is fine:
Also in loop I need to have wait statement or Ischarwaiting would not work.
Tested on two xtinys and it does the same. Also if messsage is longer it makes much more often
 |
|
Back to top |
|
 |
EDC
Joined: 26 Mar 2014 Posts: 1126

|
Posted: Wed Jul 17, 2024 3:57 pm Post subject: |
|
|
Your receiving method is poor.
You should wait for some string termination sign like Carriage Return or Line Feed.
This is my frequently used method:
Code: | Do
'--[ USART0 COM1 USB ]------------------------
If 0 < Ischarwaiting(#usb) Then
Char = Inkey(#usb)
Select Case Char
Case 10 'LF sign -> simply swallow and ignore
Case 13 : Got_str1 = 1 'CR sign -> parse msg
Case Else
Command1 = Command1 + Chr(char)
End Select
If Len(command1) > 49 Then Got_str1 = 1 '
If Got_str1 = 0 Then Cmd_timeout1 = 50 '50x10ms
'end if Ischarwaiting
End If
If Got_str = 1 Then
Got_str = 0
Cmd_timeout1 = 0
'parse new message
Command1 = ""
End If
'--[ TIMER2 10ms ]----------------------------
If Tifr2.ocf2a = 1 Then 'valid for Mega2560
Tifr2.ocf2a = 1
'COM1 USB
If Cmd_timeout1 > 0 Then
Decr Cmd_timeout1
If Cmd_timeout1 = 0 Then
'Got_str1 = 1
Command1 = ""
End If
End If
End If
Loop
End |
Eventually, in your method, after the first Ischarwaiting() you should wait some time in the Do Loop Until Ischarwaiting = 0 for receive next character. Baud 38400 means ~250us for one byte i think. _________________ Check B-Flash -my MCS bootloader app for Android (updated)
Last edited by EDC on Wed Jul 17, 2024 4:50 pm; edited 1 time in total |
|
Back to top |
|
 |
kasny.ondrej
Joined: 03 Feb 2024 Posts: 52

|
Posted: Wed Jul 17, 2024 4:25 pm Post subject: |
|
|
Ok, I will process it into code a give it a try again. |
|
Back to top |
|
 |
kasny.ondrej
Joined: 03 Feb 2024 Posts: 52

|
Posted: Wed Jul 17, 2024 4:42 pm Post subject: |
|
|
Works!!!! Thank you EDC, I thought that every command take its own time to process it without errors. Now I see that it is not true and any type of wait statement must be in code included. I apologize to all for my unconscious. |
|
Back to top |
|
 |
i.dobson
Joined: 05 Jan 2006 Posts: 1571 Location: Basel, Switzerland

|
Posted: Wed Jul 17, 2024 5:11 pm Post subject: |
|
|
Hi,
I don't think your actually using serial buffering. Your missing the:
Open "COM1:" For Binary As #1
enabling interrupts
reading the buffered serial data over #1 ( Inkey(#1))
It works for you as your reading directly from the serial input without any buffering, and your CPU is doing nothing more than waiting for data from the serial port.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
 |
kasny.ondrej
Joined: 03 Feb 2024 Posts: 52

|
Posted: Wed Jul 17, 2024 5:20 pm Post subject: |
|
|
Forgot to include here on web into sample code, of course Enable Interrupts is in code. |
|
Back to top |
|
 |
kasny.ondrej
Joined: 03 Feb 2024 Posts: 52

|
Posted: Fri Mar 21, 2025 2:13 pm Post subject: |
|
|
Hello guys. I have encoutered another buffer bug seems to me at Atxtiny3217. From some reason exceeding buffer size start causing random executing errors those errors are perfectly repeatable. The thing is: Let say we have Config serialin = 50. If come more bytes than this, let say 80+ bytes, some variables gets corrupted. It look like buffer continues to write in SRAM where other variables are already stored and thus overwriting them. Code then starts act like crazy. Somethin starts executing when it shouldn't, primarily I would say thanks to corrupted variables. Where should be zero is one and magic starts to happening then. |
|
Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6173 Location: Holland

|
Posted: Fri Mar 21, 2025 3:08 pm Post subject: |
|
|
you should post a sample that will demo this.
serial buffered input is used a lot and the only difference between xtiny/normal AVR is that other registers need to be accessed.
i also do not recall that 3217 has some specific difference.
for your info : when the buffer is full it will not store more data.
so make a small as possible example and test it. when it demos this bug post it. you should also give info about the data you send.
that said : when you read data and add it to a string and do not check the string length it can overwrite the string. bur that is USER code not a built in function/code.
and finally when you made your own hardware post the schematic too. _________________ Mark |
|
Back to top |
|
 |
kasny.ondrej
Joined: 03 Feb 2024 Posts: 52

|
Posted: Mon Mar 24, 2025 7:22 am Post subject: |
|
|
My apologies Mark I am idiot as usual. You are right. I didn't realized when string is bigger than dimensioned it starts to overwrite another data behind him. Another valuable experience. Always check length of string. There is a saying: He can't see the forest for the trees. |
|
Back to top |
|
 |
|