-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#169] STM32: make UART thread safe #27
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,15 +30,28 @@ public void WriteChar(byte value) | |
this.Log(LogLevel.Warning, "Received a character, but the receiver is not enabled, dropping."); | ||
return; | ||
} | ||
receiveFifo.Enqueue(value); | ||
readFifoNotEmpty.Value = true; | ||
Update(); | ||
lock (receiveFifo) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: The queue was used in two threads but never locked. This led to crashes. |
||
{ | ||
receiveFifo.Enqueue(value); | ||
readFifoNotEmpty = true; | ||
|
||
machine.ClockSource.ExecuteInLock(() => | ||
{ | ||
if (receiverNotEmptyInterruptEnabled.Value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: So there is the case, that the machine is not yet done with the last interrupt handling: receiverNotEmptyInterruptEnabled is not yet set to true but transmissionCompleteInterruptEnabled and transmissionComplete are so the Update() method would wrongly set IRQ to true again. If this happens, the IRQ hangs somehow. Overall I am not a fan of updating the IRQ from a different thread than the machine thread. |
||
{ | ||
Update(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public override void Reset() | ||
{ | ||
base.Reset(); | ||
receiveFifo.Clear(); | ||
lock (receiveFifo) | ||
{ | ||
receiveFifo.Clear(); | ||
} | ||
} | ||
|
||
public uint BaudRate | ||
|
@@ -81,6 +94,7 @@ public Bits StopBits | |
|
||
public GPIO IRQ { get; } = new GPIO(); | ||
|
||
[field: Transient] | ||
public event Action<byte> CharReceived; | ||
|
||
private void DefineRegisters() | ||
|
@@ -91,7 +105,10 @@ private void DefineRegisters() | |
.WithTaggedFlag("NF", 2) | ||
.WithFlag(3, FieldMode.Read, valueProviderCallback: _ => false, name: "ORE") // we assume no receive overruns | ||
.WithTaggedFlag("IDLE", 4) | ||
.WithFlag(5, out readFifoNotEmpty, FieldMode.Read | FieldMode.WriteZeroToClear, name: "RXNE") // as these two flags are WZTC, we cannot just calculate their results | ||
.WithFlag(5, FieldMode.Read | FieldMode.WriteZeroToClear, name: "RXNE", valueProviderCallback: _ => | ||
{ | ||
return readFifoNotEmpty; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: This workaround I don't understand my self but otherwise, it is not working. if readFifoNotEmpty is a IFlagRegisterField, it happens that the machine does not see the change and the application stucks. If we put inside the callback function, this is not happening anymore. |
||
}) // as these two flags are WZTC, we cannot just calculate their results | ||
.WithFlag(6, out transmissionComplete, FieldMode.Read | FieldMode.WriteZeroToClear, name: "TC") | ||
.WithFlag(7, FieldMode.Read, valueProviderCallback: _ => true, name: "TXE") // we always assume "transmit data register empty" | ||
.WithTaggedFlag("LBD", 8) | ||
|
@@ -102,13 +119,16 @@ private void DefineRegisters() | |
Register.Data.Define(this, name: "USART_DR") | ||
.WithValueField(0, 9, valueProviderCallback: _ => | ||
{ | ||
uint value = 0; | ||
if(receiveFifo.Count > 0) | ||
byte value = 0; | ||
lock (receiveFifo) | ||
{ | ||
value = receiveFifo.Dequeue(); | ||
if(receiveFifo.Count > 0) | ||
{ | ||
value = receiveFifo.Dequeue(); | ||
Update(); | ||
} | ||
readFifoNotEmpty = receiveFifo.Count > 0; | ||
} | ||
readFifoNotEmpty.Value = receiveFifo.Count > 0; | ||
Update(); | ||
return value; | ||
}, writeCallback: (_, value) => | ||
{ | ||
|
@@ -164,11 +184,14 @@ private void DefineRegisters() | |
|
||
private void Update() | ||
{ | ||
IRQ.Set( | ||
(receiverNotEmptyInterruptEnabled.Value && readFifoNotEmpty.Value) || | ||
(transmitDataRegisterEmptyInterruptEnabled.Value) || // TXE is assumed to be true | ||
(transmissionCompleteInterruptEnabled.Value && transmissionComplete.Value) | ||
); | ||
machine.ClockSource.ExecuteInLock(() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Viatorus Sorry, How do you generate this figure? Could you please tell me the name of the tool? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a debug tool from Rider: https://www.jetbrains.com/help/rider/Debugging_Multithreaded_Applications.html#parallel-stacks |
||
{ | ||
IRQ.Set( | ||
(receiverNotEmptyInterruptEnabled.Value && readFifoNotEmpty) || | ||
(transmitDataRegisterEmptyInterruptEnabled.Value) || // TXE is assumed to be true | ||
(transmissionCompleteInterruptEnabled.Value && transmissionComplete.Value) | ||
); | ||
}); | ||
} | ||
|
||
private readonly uint frequency; | ||
|
@@ -183,7 +206,7 @@ private void Update() | |
private IFlagRegisterField receiverNotEmptyInterruptEnabled; | ||
private IFlagRegisterField receiverEnabled; | ||
private IFlagRegisterField transmitterEnabled; | ||
private IFlagRegisterField readFifoNotEmpty; | ||
private bool readFifoNotEmpty = false; | ||
private IFlagRegisterField transmissionComplete; | ||
private IValueRegisterField dividerMantissa; | ||
private IValueRegisterField dividerFraction; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI:
I changed the order, otherwise the value -1 would be used as dataReceived!