Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

You can't have everything... where would you put it? -- Steven Wright


devel / comp.arch.embedded / Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

SubjectAuthor
* How to write a simple driver in bare metal systems: volatile, memorypozz
+- Re: How to write a simple driver in bare metal systems: volatile, memory barrierClifford Heath
+* Re: How to write a simple driver in bare metal systems: volatile,Don Y
|`* Re: How to write a simple driver in bare metal systems: volatile,pozz
| `- Re: How to write a simple driver in bare metal systems: volatile,Don Y
+* Re: How to write a simple driver in bare metal systems: volatile,David Brown
|+* Re: How to write a simple driver in bare metal systems: volatile,pozz
||`* Re: How to write a simple driver in bare metal systems: volatile,David Brown
|| `* Re: How to write a simple driver in bare metal systems: volatile,pozz
||  `- Re: How to write a simple driver in bare metal systems: volatile,David Brown
|`* Re: How to write a simple driver in bare metal systems: volatile,pozz
| +- Re: How to write a simple driver in bare metal systems: volatile,David Brown
| `- Re: How to write a simple driver in bare metal systems: volatile,Richard Damon
`* Re: How to write a simple driver in bare metal systems: volatile,Johann Klammer
 `* Re: How to write a simple driver in bare metal systems: volatile,Dimiter_Popoff
  +* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |+* Re: How to write a simple driver in bare metal systems: volatile,Dimiter_Popoff
  ||+* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |||+* Re: How to write a simple driver in bare metal systems: volatile,Dimiter_Popoff
  ||||`* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |||| `* Re: How to write a simple driver in bare metal systems: volatile,Dimiter_Popoff
  ||||  `- Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |||+- Re: How to write a simple driver in bare metal systems: volatile,David Brown
  |||`* Re: How to write a simple driver in bare metal systems: volatile,Niklas Holsti
  ||| `* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |||  `* Re: How to write a simple driver in bare metal systems: volatile,Niklas Holsti
  |||   +- Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |||   `* Re: How to write a simple driver in bare metal systems: volatile,David Brown
  |||    `- Re: How to write a simple driver in bare metal systems: volatile,Niklas Holsti
  ||`* Re: How to write a simple driver in bare metal systems: volatile,Niklas Holsti
  || +* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  || |`* Re: How to write a simple driver in bare metal systems: volatile,Niklas Holsti
  || | `- Re: How to write a simple driver in bare metal systems: volatile,Don Y
  || +* Re: How to write a simple driver in bare metal systems: volatile,Dimiter_Popoff
  || |`* Re: How to write a simple driver in bare metal systems: volatile,Niklas Holsti
  || | +* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  || | |`* Re: How to write a simple driver in bare metal systems: volatile,Dimiter_Popoff
  || | | `- Re: How to write a simple driver in bare metal systems: volatile,Don Y
  || | +* Re: How to write a simple driver in bare metal systems: volatile,pozz
  || | |+- Re: How to write a simple driver in bare metal systems: volatile,Don Y
  || | |`* Re: How to write a simple driver in bare metal systems: volatile,Niklas Holsti
  || | | +* Re: How to write a simple driver in bare metal systems: volatile,Dimiter_Popoff
  || | | |`* Re: How to write a simple driver in bare metal systems: volatile,Niklas Holsti
  || | | | `* Re: How to write a simple driver in bare metal systems: volatile,Dimiter_Popoff
  || | | |  `- Re: How to write a simple driver in bare metal systems: volatile,David Brown
  || | | `- Re: How to write a simple driver in bare metal systems: volatile,pozz
  || | `- Re: How to write a simple driver in bare metal systems: volatile,David Brown
  || `- Re: How to write a simple driver in bare metal systems: volatile, memory barrierClifford Heath
  |`* Re: How to write a simple driver in bare metal systems: volatile, memory barrierantispam
  | `* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |  `* Re: How to write a simple driver in bare metal systems: volatile, memory barrierantispam
  |   `* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |    `* Re: How to write a simple driver in bare metal systems: volatile, memory barrierantispam
  |     `* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |      `* Re: How to write a simple driver in bare metal systems: volatile, memory barrierantispam
  |       `* Re: How to write a simple driver in bare metal systems: volatile,Don Y
  |        `* Re: How to write a simple driver in bare metal systems: volatile, memory barrierantispam
  |         `- Re: How to write a simple driver in bare metal systems: volatile,Don Y
  `- Re: How to write a simple driver in bare metal systems: volatile,David Brown

Pages:123
Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<itn9hjFq77qU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=668&group=comp.arch.embedded#668

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.h...@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 11:52:02 +0300
Organization: Tidorum Ltd
Lines: 49
Message-ID: <itn9hjFq77qU1@mid.individual.net>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <sl4i03$784$1@dont-email.me>
<itn68pFpjgeU1@mid.individual.net> <sl5pbe$dv8$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net cEb/ZA5Td/oLZ4Q2wbRLHQQVnJWfcoiKaymuGnqc45rNu1lT83
Cancel-Lock: sha1:vQoGacyOWcTnmsvsTMRsZpbShQc=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sl5pbe$dv8$1@dont-email.me>
Content-Language: en-US
 by: Niklas Holsti - Mon, 25 Oct 2021 08:52 UTC

On 2021-10-25 11:19, Don Y wrote:
> On 10/25/2021 12:56 AM, Niklas Holsti wrote:
>> On 2021-10-25 0:08, Don Y wrote:
>>
>>> There are (and have been) many "safer" languages.  Many that are more
>>> descriptive (for certain classes of problem).  But, C has survived to
>>> handle all-of-the-above... perhaps in a suboptimal way but at least
>>> a manner that can get to the desired solution.
>>>
>>> Look at how few applications SNOBOL handles.  Write an OS in COBOL?
>>> Ada?
>>
>> I don't know about COBOL, but typically the real-time kernels
>> ("run-time systems") associated with Ada compilers for bare-board
>> embedded systems are written in Ada, with a minor amount of assembly
>> language for the most HW-related bits like HW context saving and
>> restoring. I'm pretty sure that C-language OS kernels also use
>> assembly for those things.
>
> Of course you *can* do these things.

Then I misunderstood your (rhetorical?) question.

> The question is how often
> they are ACTUALLY done with these other languages.

I don't find that question very interesting.

It is a typical chicken-and-egg, first-to-market conundrum. There is an
enormous amount of status-quo-favouring friction in awareness,
education, tool availability, and legacy code.

> [By the same token, expecting the past to mirror the present is equally
> naive.  People forget that tools and processes have evolved (in the 40+
> years that I've been designing embedded products).  And, that the isssues
> folks now face often weren't issues when tools were "stupider" (I've
> probably got $60K of obsolete compilers to prove this -- anyone written
> any C on an 1802 recently?  Or, a 2A03?  65816?  Z180?  6809?)  Don't
> even *think* about finding an Ada compiler for them -- in the past!]

Well, the Janus/Ada compiler was available for Z80 in its day. There are
also Ada compilers that use C as an intermediate language, with
applications for example on TI MSP430's, but those were probably not
available in the past ages you refer to.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<itnabsFqas4U1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=669&group=comp.arch.embedded#669

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.h...@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 12:06:04 +0300
Organization: Tidorum Ltd
Lines: 65
Message-ID: <itnabsFqas4U1@mid.individual.net>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl5pqv$gu1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net lX0ana9lN3KiT826/lR06wvxWPapK4g8rBczeGopfo1jhSZvCb
Cancel-Lock: sha1:aTBDpXeLtt6dSH/f+4ud3wvARsc=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sl5pqv$gu1$1@dont-email.me>
Content-Language: en-US
 by: Niklas Holsti - Mon, 25 Oct 2021 09:06 UTC

On 2021-10-25 11:28, Don Y wrote:
> On 10/25/2021 1:09 AM, Niklas Holsti wrote:
>> On 2021-10-24 23:27, Dimiter_Popoff wrote:
>>> On 10/24/2021 22:54, Don Y wrote:
>>>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>>>> Disable interrupts while accessing the fifo. you really have to.
>>>>>> alternatively you'll often get away not using a fifo at all,
>>>>>> unless you're blocking for a long while in some part of the code.
>>>>>
>>>>> Why would you do that. The fifo write pointer is only modified by
>>>>> the interrupt handler, the read pointer is only modified by the
>>>>> interrupted code. Has been done so for times immemorial.
>>>>
>>>> The OPs code doesn't differentiate between FIFO full and empty.
>>>
>>> So he should fix that first, there is no sane reason why not.
>>> Few things are simpler to do than that.
>>
>>
>>     [snip]
>>
>>
>>> Whatever handshakes he makes there is no problem knowing whether
>>> the fifo is full - just check if the position the write pointer
>>> will have after putting the next byte matches the read pointer
>>> at the moment.  Like I said before, few things are simpler than
>>> that, can't imagine someone working as a programmer being
>>> stuck at *that*.
>>
>> That simple check would require keeping a maximum of only N-1 entries
>> in the N-position FIFO buffer, and the OP explicitly said they did not
>> want to allocate an unused place in the buffer (which I think is
>> unreasonable of the OP, but that is only IMO).
>>
>> The simple explanation for the N-1 limit is that the difference
>> between two wrap-around pointers into an N-place buffer has at most N
>> different values, while there are N+1 possible filling states of the
>> buffer, from empty (zero items) to full (N items).
>
> But, again, that just deals with the "full check".  The easiest way to do
> this is just to check ".in" *after* advancement and inhibit the store if
> it coincides with the ".out" value.
>
> Checking for a "high water mark" to enable flow control requires more
> computation (albeit simple) as you have to accommodate the delays in
> that notification reaching the remote sender (lest he continue
> sending and overrun your buffer).
>
> And, later noting when you've consumed enough of the FIFO's contents
> to reach a "low water mark" and reenable the remote's transmissions.
>
> [And, if you ever have to deal with more "established" protocols
> that require the sequencing of specific control signals DURING
> a transfer, the ISR quickly becomes very complex!]

Of course. Perhaps you (Don) did not see that I was agreeing with your
position and objecting to the "it is very simple" stance of Dimiter
(considering the OP's expressed constraints).

Personally I would use critical sections to avoid relying on delicate
reasoning about interleaved executions. And to allow for easy future
complexification of the concurrent activities. The overhead of interrupt
disabling and enabling is seldom significant when that can be done
directly without kernel calls.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl5tot$dig$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=670&group=comp.arch.embedded#670

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: blockedo...@foo.invalid (Don Y)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 02:35:17 -0700
Organization: A noiseless patient Spider
Lines: 107
Message-ID: <sl5tot$dig$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl5pqv$gu1$1@dont-email.me> <itnabsFqas4U1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 09:35:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0a41b6a329df076c2a27c48b9f62cde0";
logging-data="13904"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PyjLFxGJsU3Gsc6vZUCCQ"
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101
Thunderbird/52.1.1
Cancel-Lock: sha1:Avfm5swhdyOS1JZC25AqTQ1FWmA=
In-Reply-To: <itnabsFqas4U1@mid.individual.net>
Content-Language: en-US
 by: Don Y - Mon, 25 Oct 2021 09:35 UTC

On 10/25/2021 2:06 AM, Niklas Holsti wrote:
> On 2021-10-25 11:28, Don Y wrote:
>> On 10/25/2021 1:09 AM, Niklas Holsti wrote:
>>> On 2021-10-24 23:27, Dimiter_Popoff wrote:
>>>> On 10/24/2021 22:54, Don Y wrote:
>>>>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>>>>> Disable interrupts while accessing the fifo. you really have to.
>>>>>>> alternatively you'll often get away not using a fifo at all,
>>>>>>> unless you're blocking for a long while in some part of the code.
>>>>>>
>>>>>> Why would you do that. The fifo write pointer is only modified by
>>>>>> the interrupt handler, the read pointer is only modified by the
>>>>>> interrupted code. Has been done so for times immemorial.
>>>>>
>>>>> The OPs code doesn't differentiate between FIFO full and empty.
>>>>
>>>> So he should fix that first, there is no sane reason why not.
>>>> Few things are simpler to do than that.
>>>
>>>
>>> [snip]
>>>
>>>
>>>> Whatever handshakes he makes there is no problem knowing whether
>>>> the fifo is full - just check if the position the write pointer
>>>> will have after putting the next byte matches the read pointer
>>>> at the moment. Like I said before, few things are simpler than
>>>> that, can't imagine someone working as a programmer being
>>>> stuck at *that*.
>>>
>>> That simple check would require keeping a maximum of only N-1 entries in the
>>> N-position FIFO buffer, and the OP explicitly said they did not want to
>>> allocate an unused place in the buffer (which I think is unreasonable of the
>>> OP, but that is only IMO).
>>>
>>> The simple explanation for the N-1 limit is that the difference between two
>>> wrap-around pointers into an N-place buffer has at most N different values,
>>> while there are N+1 possible filling states of the buffer, from empty (zero
>>> items) to full (N items).
>>
>> But, again, that just deals with the "full check". The easiest way to do
>> this is just to check ".in" *after* advancement and inhibit the store if
>> it coincides with the ".out" value.
>>
>> Checking for a "high water mark" to enable flow control requires more
>> computation (albeit simple) as you have to accommodate the delays in
>> that notification reaching the remote sender (lest he continue
>> sending and overrun your buffer).
>>
>> And, later noting when you've consumed enough of the FIFO's contents
>> to reach a "low water mark" and reenable the remote's transmissions.
>>
>> [And, if you ever have to deal with more "established" protocols
>> that require the sequencing of specific control signals DURING
>> a transfer, the ISR quickly becomes very complex!]
>
> Of course. Perhaps you (Don) did not see that I was agreeing with your position
> and objecting to the "it is very simple" stance of Dimiter (considering the
> OP's expressed constraints).

Yes, but I was afraid the emphasis would shift away from the "more involved"
case (by trivializing the "full" case).

> Personally I would use critical sections to avoid relying on delicate reasoning
> about interleaved executions. And to allow for easy future complexification of
> the concurrent activities. The overhead of interrupt disabling and enabling is
> seldom significant when that can be done directly without kernel calls.

We (developers, in general) tend to forget how often we cobble
together solutions from past implementations. And, as those past
implementations tend to be lax when it comes to enumerating the
assumptions under which they were created, we end up propagating
a bunch of dubious qualifiers that ultimately affect the code's
performance and "correctness".

Someone (including ourselves) trying to pilfer code from THIS
project might incorrectly expect the ISR to protect against buffer
wrap. Or, implement flow control. Or, be designed for a higher
data rate than it actually sees (saw!) -- will the buffer size -- and
task() timing -- be adequate to handle burst transmissions at 115Kbaud?
If not, where is the upper bound? What if the CPU clock is changed?
Or, the processor load? ...

"Steal" several bits of code -- possibly from different projects -- and
you've got an assortment of such hidden assumptions, all willing to eat
your lunch! While you remain convinced that none of those things
can happen!

My first UART driver had to manage about a dozen control signals as
the standard had a different intent and interpretation in the mid 70's,
early 80's (anyone remember TWX? TELEX? DB25s?). Porting it forward
ended up with a bunch of "issues" that no longer applied. (e.g.,
RTS/CTS weren't originally used as flow control/handshaking signals as
they are commonly used, now). *Assuming* a letter revision of the standard
was benign wrt the timing of signal transitions was folly.

You only see these things when you lay out all of your assumptions
in the codebase. And, hope the next guy actually READS what you took
the time to WRITE!

[When I wrote my 9 track tape driver, I had ~200 lines of commentary
just explaining the role of the interface wrt the formatter, transports,
etc. E.g., when you can read reverse, seek forward, rewind, etc.
with multiple transports hung off that same interface. Otherwise,
an observant developer would falsely conclude that the driver was
riddled with bugs -- as it *facilitated* a multiplicity of
concurrent operations]

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl5ukh$jcv$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=671&group=comp.arch.embedded#671

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: blockedo...@foo.invalid (Don Y)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 02:50:04 -0700
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <sl5ukh$jcv$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <sl4i03$784$1@dont-email.me>
<itn68pFpjgeU1@mid.individual.net> <sl5pbe$dv8$1@dont-email.me>
<itn9hjFq77qU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 09:50:10 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0a41b6a329df076c2a27c48b9f62cde0";
logging-data="19871"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+F/R3eDMxH+hgXyg8uaXYJ"
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101
Thunderbird/52.1.1
Cancel-Lock: sha1:gmB5oF1M9iwuBo/z49i+EkubQRM=
In-Reply-To: <itn9hjFq77qU1@mid.individual.net>
Content-Language: en-US
 by: Don Y - Mon, 25 Oct 2021 09:50 UTC

On 10/25/2021 1:52 AM, Niklas Holsti wrote:
> On 2021-10-25 11:19, Don Y wrote:
>> On 10/25/2021 12:56 AM, Niklas Holsti wrote:
>>> On 2021-10-25 0:08, Don Y wrote:
>>>
>>>> There are (and have been) many "safer" languages. Many that are more
>>>> descriptive (for certain classes of problem). But, C has survived to
>>>> handle all-of-the-above... perhaps in a suboptimal way but at least
>>>> a manner that can get to the desired solution.
>>>>
>>>> Look at how few applications SNOBOL handles. Write an OS in COBOL? Ada?
>>>
>>> I don't know about COBOL, but typically the real-time kernels ("run-time
>>> systems") associated with Ada compilers for bare-board embedded systems are
>>> written in Ada, with a minor amount of assembly language for the most
>>> HW-related bits like HW context saving and restoring. I'm pretty sure that
>>> C-language OS kernels also use assembly for those things.
>>
>> Of course you *can* do these things.
>
> Then I misunderstood your (rhetorical?) question.
>
>> The question is how often
>> they are ACTUALLY done with these other languages.
>
> I don't find that question very interesting.

Why not? If a tool isn't used for a purpose for which it *should*
be "ideal", you have to start wondering "why not?" Was it NOT
suited to the task? Was it too costly (money and/or experience)?
How do we not repeat that problem, going forward? I.e., is it
better to EVOLVE a language to acquire the characteristics of the
"better" one -- rather than trying to encourage people to
"jump ship"?

> It is a typical chicken-and-egg, first-to-market conundrum. There is an
> enormous amount of status-quo-favouring friction in awareness, education, tool
> availability, and legacy code.

Of course!

And, there is also the pressure of the market. Do *you* want to be The Guy
who tries something new and sinks a product's development or market release?
If your approach proves to be a big hit, will you benefit as much as you'd
LOSE if it was a flop?

>> [By the same token, expecting the past to mirror the present is equally
>> naive. People forget that tools and processes have evolved (in the 40+
>> years that I've been designing embedded products). And, that the isssues
>> folks now face often weren't issues when tools were "stupider" (I've
>> probably got $60K of obsolete compilers to prove this -- anyone written
>> any C on an 1802 recently? Or, a 2A03? 65816? Z180? 6809?) Don't
>> even *think* about finding an Ada compiler for them -- in the past!]
>
> Well, the Janus/Ada compiler was available for Z80 in its day. There are also
> Ada compilers that use C as an intermediate language, with applications for
> example on TI MSP430's, but those were probably not available in the past ages
> you refer to.

I recall JRT Pascal and PL/M as the "high level" languages, back then.
C compilers were notoriously bad. You could literally predict the
code that would be generated for any statement. The whole idea of
"peephole optimizers" looking for:
STORE A
LOAD A
sequences to elide is testament to how little global knowledge they
had of the code they were processing.

Performance? A skilled ASM coder could beat the generated code
(in time AND space) without breaking into a sweat.

And, you bought a compiler/assembler/linker/debugger for *each*
processor -- not just a simple command line switch to alter the
code generation, etc. Vendors might have a common codebase
for the tools but built each variant conditionally.

The limits of the language were largely influenced by the targeted
hardware -- "helper routines" to support longs, floats, etc.
("Oh, did you want that support to be *reentrant*? We assumed
there would be a single floating point accumulator used throughout
the code, not one per thread!") Different sizes of addresses
(e.g., for the Z180, you could have 16b "logical" addresses
and 24b physical addresses -- mapped into that logical space
by the compiler's runtime support and linkage editor.)

Portable code? Maybe -- with quite a bit of work!

Fast/small? Meh...

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl65je$4f2$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=673&group=comp.arch.embedded#673

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 13:49:02 +0200
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <sl65je$4f2$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <sl4i03$784$1@dont-email.me>
<itn68pFpjgeU1@mid.individual.net> <sl5pbe$dv8$1@dont-email.me>
<itn9hjFq77qU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 11:49:02 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="bc28c0b8ce32574cb05447f28b439cdd";
logging-data="4578"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GtIE0d7+u9LsGI9u0T567k8KEap1Qxfg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:FJk4UWAyMzx+3DZcrmxQzKlxKIs=
In-Reply-To: <itn9hjFq77qU1@mid.individual.net>
Content-Language: en-GB
 by: David Brown - Mon, 25 Oct 2021 11:49 UTC

On 25/10/2021 10:52, Niklas Holsti wrote:

>
> Well, the Janus/Ada compiler was available for Z80 in its day. There are
> also Ada compilers that use C as an intermediate language, with
> applications for example on TI MSP430's, but those were probably not
> available in the past ages you refer to.

Presumably there is gcc-based Ada for the msp430 (as there is for the
8-bit AVR)? There might not be a full library available, or possibly
some missing features in the language.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<itnlgaFsem9U1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=674&group=comp.arch.embedded#674

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.h...@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 15:16:10 +0300
Organization: Tidorum Ltd
Lines: 24
Message-ID: <itnlgaFsem9U1@mid.individual.net>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <sl4i03$784$1@dont-email.me>
<itn68pFpjgeU1@mid.individual.net> <sl5pbe$dv8$1@dont-email.me>
<itn9hjFq77qU1@mid.individual.net> <sl65je$4f2$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net wCXF8UNDKAYqI4CrcrcuSgKv1X7uersxlmMAur+ewMfo54vhLw
Cancel-Lock: sha1:JIcuBXss6FGkLw87cFECWZMZ41E=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sl65je$4f2$1@dont-email.me>
Content-Language: en-US
 by: Niklas Holsti - Mon, 25 Oct 2021 12:16 UTC

On 2021-10-25 14:49, David Brown wrote:
> On 25/10/2021 10:52, Niklas Holsti wrote:
>
>>
>> Well, the Janus/Ada compiler was available for Z80 in its day. There are
>> also Ada compilers that use C as an intermediate language, with
>> applications for example on TI MSP430's, but those were probably not
>> available in the past ages you refer to.
>
> Presumably there is gcc-based Ada for the msp430 (as there is for the
> 8-bit AVR)?

Indeed there seems to be one, or at least work towards one:
https://sourceforge.net/p/msp430ada/wiki/Home/.

> There might not be a full library available, or possibly
> some missing features in the language.

Certainly. I think that Janus/Ada for the Z80 was limited to the
original Ada (Ada 83), and may well have also had some significant
missing features. But I believe it was self-hosted on CP/M, quite a feat.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6a1d$4ci$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=675&group=comp.arch.embedded#675

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dp...@tgi-sci.com (Dimiter_Popoff)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 16:04:44 +0300
Organization: TGI
Lines: 35
Message-ID: <sl6a1d$4ci$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
Reply-To: dp@tgi-sci.com
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 25 Oct 2021 13:04:45 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="328f47122045252f51317f11fee19557";
logging-data="4498"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19dtMBJoy3jO6+ntL9wQGYV6AE6u27Ho3c="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:ExCY/O1AlmLyfztIuuyf4iBa9kU=
In-Reply-To: <itn713FpntqU1@mid.individual.net>
Content-Language: en-US
 by: Dimiter_Popoff - Mon, 25 Oct 2021 13:04 UTC

On 10/25/2021 11:09, Niklas Holsti wrote:
> On 2021-10-24 23:27, Dimiter_Popoff wrote:
>> On 10/24/2021 22:54, Don Y wrote:
>>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>>> Disable interrupts while accessing the fifo. you really have to.
>>>>> alternatively you'll often get away not using a fifo at all,
>>>>> unless you're blocking for a long while in some part of the code.
>>>>
>>>> Why would you do that. The fifo write pointer is only modified by
>>>> the interrupt handler, the read pointer is only modified by the
>>>> interrupted code. Has been done so for times immemorial.
>>>
>>> The OPs code doesn't differentiate between FIFO full and empty.
>>
>> So he should fix that first, there is no sane reason why not.
>> Few things are simpler to do than that.
>
>
>    [snip]
>
>
>> Whatever handshakes he makes there is no problem knowing whether
>> the fifo is full - just check if the position the write pointer
>> will have after putting the next byte matches the read pointer
>> at the moment.  Like I said before, few things are simpler than
>> that, can't imagine someone working as a programmer being
>> stuck at *that*.
>
> That simple check would require keeping a maximum of only N-1 entries in
> the N-position FIFO buffer, and the OP explicitly said they did not want
> to allocate an unused place in the buffer (which I think is unreasonable
> of the OP, but that is only IMO).

Well it might be reasonable if the fifo has a size of two, you know :-).

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<ito14pF55oU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=676&group=comp.arch.embedded#676

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.h...@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 18:34:48 +0300
Organization: Tidorum Ltd
Lines: 40
Message-ID: <ito14pF55oU1@mid.individual.net>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net QmPGszIPZE/IPhE8VXEHdgbKfbnXcK3IiJDD1AGhT39gjPId3u
Cancel-Lock: sha1:pQr8LXvRM+d4Jq5NMK0uDAmMRuI=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sl6a1d$4ci$1@dont-email.me>
Content-Language: en-US
 by: Niklas Holsti - Mon, 25 Oct 2021 15:34 UTC

On 2021-10-25 16:04, Dimiter_Popoff wrote:
> On 10/25/2021 11:09, Niklas Holsti wrote:
>> On 2021-10-24 23:27, Dimiter_Popoff wrote:
>>> On 10/24/2021 22:54, Don Y wrote:
>>>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>>>> Disable interrupts while accessing the fifo. you really have to.
>>>>>> alternatively you'll often get away not using a fifo at all,
>>>>>> unless you're blocking for a long while in some part of the code.
>>>>>
>>>>> Why would you do that. The fifo write pointer is only modified by
>>>>> the interrupt handler, the read pointer is only modified by the
>>>>> interrupted code. Has been done so for times immemorial.
>>>>
>>>> The OPs code doesn't differentiate between FIFO full and empty.
>>>
>>> So he should fix that first, there is no sane reason why not.
>>> Few things are simpler to do than that.
>>
>>
>>     [snip]
>>
>>
>>> Whatever handshakes he makes there is no problem knowing whether
>>> the fifo is full - just check if the position the write pointer
>>> will have after putting the next byte matches the read pointer
>>> at the moment.  Like I said before, few things are simpler than
>>> that, can't imagine someone working as a programmer being
>>> stuck at *that*.
>>
>> That simple check would require keeping a maximum of only N-1 entries
>> in the N-position FIFO buffer, and the OP explicitly said they did not
>> want to allocate an unused place in the buffer (which I think is
>> unreasonable of the OP, but that is only IMO).
>
> Well it might be reasonable if the fifo has a size of two, you know :-).

And if each of those two items is large, yes. But here we have a FIFO of
8-bit characters... few programs are so tight on memory that they cannot
stand one unused octet.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6qcc$4fe$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=677&group=comp.arch.embedded#677

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: blockedo...@foo.invalid (Don Y)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 10:43:38 -0700
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <sl6qcc$4fe$2@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 17:43:40 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0a41b6a329df076c2a27c48b9f62cde0";
logging-data="4590"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MAb7FPO3E+aQeg7EpV3eN"
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101
Thunderbird/52.1.1
Cancel-Lock: sha1:jlCeUlX5CT02IJuG8GIHRZRENIE=
In-Reply-To: <ito14pF55oU1@mid.individual.net>
Content-Language: en-US
 by: Don Y - Mon, 25 Oct 2021 17:43 UTC

On 10/25/2021 8:34 AM, Niklas Holsti wrote:
> And if each of those two items is large, yes. But here we have a FIFO of 8-bit
> characters... few programs are so tight on memory that they cannot stand one
> unused octet.

It's not "unused". Rather, it's roll is that of indicating "full/overrun".
The OP seems to have decided that this is of no concern -- in *one* app?

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6qtm$9oh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=678&group=comp.arch.embedded#678

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 19:52:52 +0200
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <sl6qtm$9oh$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 25 Oct 2021 17:52:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cbaea9215393f46884ecaccb923a9697";
logging-data="10001"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19jK4ER+y75z8avrbkSJznlP3XuwUhHtQg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:yAwl8/r+HFk5axukieG0cgKAtvU=
In-Reply-To: <ito14pF55oU1@mid.individual.net>
Content-Language: it
 by: pozz - Mon, 25 Oct 2021 17:52 UTC

Il 25/10/2021 17:34, Niklas Holsti ha scritto:
> On 2021-10-25 16:04, Dimiter_Popoff wrote:
>> On 10/25/2021 11:09, Niklas Holsti wrote:
>>> On 2021-10-24 23:27, Dimiter_Popoff wrote:
>>>> On 10/24/2021 22:54, Don Y wrote:
>>>>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>>>>> Disable interrupts while accessing the fifo. you really have to.
>>>>>>> alternatively you'll often get away not using a fifo at all,
>>>>>>> unless you're blocking for a long while in some part of the code.
>>>>>>
>>>>>> Why would you do that. The fifo write pointer is only modified by
>>>>>> the interrupt handler, the read pointer is only modified by the
>>>>>> interrupted code. Has been done so for times immemorial.
>>>>>
>>>>> The OPs code doesn't differentiate between FIFO full and empty.
>>>>
>>>> So he should fix that first, there is no sane reason why not.
>>>> Few things are simpler to do than that.
>>>
>>>
>>>     [snip]
>>>
>>>
>>>> Whatever handshakes he makes there is no problem knowing whether
>>>> the fifo is full - just check if the position the write pointer
>>>> will have after putting the next byte matches the read pointer
>>>> at the moment.  Like I said before, few things are simpler than
>>>> that, can't imagine someone working as a programmer being
>>>> stuck at *that*.
>>>
>>> That simple check would require keeping a maximum of only N-1 entries
>>> in the N-position FIFO buffer, and the OP explicitly said they did
>>> not want to allocate an unused place in the buffer (which I think is
>>> unreasonable of the OP, but that is only IMO).
>>
>> Well it might be reasonable if the fifo has a size of two, you know :-).
>
>
> And if each of those two items is large, yes. But here we have a FIFO of
> 8-bit characters... few programs are so tight on memory that they cannot
> stand one unused octet.

When I have a small (<256) power-of-two (16, 32, 64, 128) buffer (and
this is the case for a UART receiving ring-buffer), I like to use this
implementation that works and doesn't waste any element.

However I know this isn't the best implementation ever and it's a pity
the thread emphasis has been against this implementation (that was used
as *one* implementation just to have an example to discuss on).

The main point was the use of volatile (and other techniques) to
guarantee a correct compiler output, whatever legal (respect the C
standard) optimizations the compiler thinks to do.

It seems to me the arguments againts or for volatile are completely
indipendent from the implementation of ring-buffer.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6que$937$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=679&group=comp.arch.embedded#679

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dp...@tgi-sci.com (Dimiter_Popoff)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 20:53:18 +0300
Organization: TGI
Lines: 12
Message-ID: <sl6que$937$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qcc$4fe$2@dont-email.me>
Reply-To: dp@tgi-sci.com
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 25 Oct 2021 17:53:18 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="328f47122045252f51317f11fee19557";
logging-data="9319"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ka2hwJRcnn/e5Y+Qrz3TryzkKh8aTpDU="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:ExKzyX6xdTi5ICTQ2gQFe+RRDyM=
In-Reply-To: <sl6qcc$4fe$2@dont-email.me>
Content-Language: en-US
 by: Dimiter_Popoff - Mon, 25 Oct 2021 17:53 UTC

On 10/25/2021 20:43, Don Y wrote:
> On 10/25/2021 8:34 AM, Niklas Holsti wrote:
>> And if each of those two items is large, yes. But here we have a FIFO
>> of 8-bit characters... few programs are so tight on memory that they
>> cannot stand one unused octet.
>
> It's not "unused".  Rather, it's roll is that of indicating "full/overrun".
> The OP seems to have decided that this is of no concern -- in *one* app?

Oh come on, I joked about the fifo of two bytes only because this whole
thread is a joke - pages and pages of C to maintain a fifo, what can be
more of a joke than this.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6ret$cmt$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=680&group=comp.arch.embedded#680

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: blockedo...@foo.invalid (Don Y)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 11:02:03 -0700
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <sl6ret$cmt$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qcc$4fe$2@dont-email.me> <sl6que$937$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 18:02:05 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0a41b6a329df076c2a27c48b9f62cde0";
logging-data="13021"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FdFN7oZusi8Hu79B/UiJG"
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101
Thunderbird/52.1.1
Cancel-Lock: sha1:igJalE7u+Qx3u8LdgZj5hSOIRkA=
In-Reply-To: <sl6que$937$1@dont-email.me>
Content-Language: en-US
 by: Don Y - Mon, 25 Oct 2021 18:02 UTC

On 10/25/2021 10:53 AM, Dimiter_Popoff wrote:
> On 10/25/2021 20:43, Don Y wrote:
>> On 10/25/2021 8:34 AM, Niklas Holsti wrote:
>>> And if each of those two items is large, yes. But here we have a FIFO of
>>> 8-bit characters... few programs are so tight on memory that they cannot
>>> stand one unused octet.
>>
>> It's not "unused". Rather, it's roll is that of indicating "full/overrun".
>> The OP seems to have decided that this is of no concern -- in *one* app?
>
> Oh come on, I joked about the fifo of two bytes only because this whole
> thread is a joke

My comment applies regardless of the size of the FIFO.

> - pages and pages of C to maintain a fifo, what can be
> more of a joke than this.

Where do you see "pages and pages of C to maintain a FIFO"?

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6rv0$i1u$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=681&group=comp.arch.embedded#681

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: blockedo...@foo.invalid (Don Y)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 11:10:33 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <sl6rv0$i1u$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qtm$9oh$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 18:10:41 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0a41b6a329df076c2a27c48b9f62cde0";
logging-data="18494"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+MxaNRHUR4qoiuon95NgM7"
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101
Thunderbird/52.1.1
Cancel-Lock: sha1:nmZS/wZ0kJnf+n8MvxzaUYTaWpM=
In-Reply-To: <sl6qtm$9oh$1@dont-email.me>
Content-Language: en-US
 by: Don Y - Mon, 25 Oct 2021 18:10 UTC

On 10/25/2021 10:52 AM, pozz wrote:
> However I know this isn't the best implementation ever and it's a pity the
> thread emphasis has been against this implementation (that was used as *one*
> implementation just to have an example to discuss on).

The point is that you need a COMPLETE implementation before you start
thinking about the amount of "license" the compiler can take with your code.

Here's *part* of an implementation:

a = 37;

Now, should I declare A as volatile? Use the register qualifier?
What size should the integer A be? Can the optimizer elide this
statement from my code?

All sorts of questions whose answers depend on the REST of the
implementation -- not shown!

> The main point was the use of volatile (and other techniques) to guarantee a
> correct compiler output, whatever legal (respect the C standard) optimizations
> the compiler thinks to do.
>
> It seems to me the arguments againts or for volatile are completely indipendent
> from the implementation of ring-buffer.

It has to do with indicating how YOU (the developer) see the object
being used (accessed). You, in theory, know more about the role of
the object than the compiler (because it may be accessed in other modules,
or, have "stuff" tied to it -- like special hardware, etc.) You need a way
to tell the compiler that "you know what you are doing" in your use
of the object and that it should restrain itself from making assumptions
that might not be true.

If your example doesn't bring to light those various issues, then
the decision as to its applicability is moot.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6s79$jt1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=682&group=comp.arch.embedded#682

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 20:15:03 +0200
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <sl6s79$jt1$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl1c3a$dfr$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 18:15:05 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cbaea9215393f46884ecaccb923a9697";
logging-data="20385"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GLqJuiqKpnSxp0nF55fyXZNTYXiba5Tk="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:mOuFxBNeUUKD8D06ifn3zCL3mlU=
In-Reply-To: <sl1c3a$dfr$1@dont-email.me>
Content-Language: it
 by: pozz - Mon, 25 Oct 2021 18:15 UTC

Il 23/10/2021 18:09, David Brown ha scritto:
[...]
> Marking "in" and "buf" as volatile is /far/ better than using a critical
> section, and likely to be more efficient than a memory barrier. You can
> also use volatileAccess rather than making buf volatile, and it is often
> slightly more efficient to cache volatile variables in a local variable
> while working with them.

I think I got your point, but I'm wondering why there are plenty of
examples of ring-buffer implementations that don't use volatile at all,
even if the author explicitly refers to interrupts and multithreading.

Just an example[1] by Quantum Leaps. It promises to be a *lock-free* (I
think thread-safe) ring-buffer implementation in the scenario of single
producer/single consumer (that is my scenario too).

In the source code there's no use of volatile. I could call
RingBuf_put() in my rx uart ISR and call RingBuf_get() in my mainloop code.

From what I learned from you, this code usually works, but the standard
doesn't guarantee it will work with every old, current and future compilers.

[1] https://github.com/QuantumLeaps/lock-free-ring-buffer

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<itobkaF258sU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=683&group=comp.arch.embedded#683

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.h...@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 21:33:44 +0300
Organization: Tidorum Ltd
Lines: 62
Message-ID: <itobkaF258sU1@mid.individual.net>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qtm$9oh$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net i5cva3ddKlQmuL3JwhV9MQbxZp5khCkaeeaL9iLlmQo19ckWNo
Cancel-Lock: sha1:SLJIku5NBREkzVnLFloyUHF5nYE=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sl6qtm$9oh$1@dont-email.me>
Content-Language: en-US
 by: Niklas Holsti - Mon, 25 Oct 2021 18:33 UTC

On 2021-10-25 20:52, pozz wrote:
> Il 25/10/2021 17:34, Niklas Holsti ha scritto:
>> On 2021-10-25 16:04, Dimiter_Popoff wrote:
>>> On 10/25/2021 11:09, Niklas Holsti wrote:
>>>> On 2021-10-24 23:27, Dimiter_Popoff wrote:
>>>>> On 10/24/2021 22:54, Don Y wrote:
>>>>>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>>>>>> Disable interrupts while accessing the fifo. you really have to.
>>>>>>>> alternatively you'll often get away not using a fifo at all,
>>>>>>>> unless you're blocking for a long while in some part of the code.
>>>>>>>
>>>>>>> Why would you do that. The fifo write pointer is only modified by
>>>>>>> the interrupt handler, the read pointer is only modified by the
>>>>>>> interrupted code. Has been done so for times immemorial.
>>>>>>
>>>>>> The OPs code doesn't differentiate between FIFO full and empty.

(I suspect something is not quite right with the attributions of the
quotations above -- Dimiter probably did not suggest disabling
interrupts -- but no matter.)

[snip]

> When I have a small (<256) power-of-two (16, 32, 64, 128) buffer (and
> this is the case for a UART receiving ring-buffer), I like to use this
> implementation that works and doesn't waste any element.
>
> However I know this isn't the best implementation ever and it's a pity
> the thread emphasis has been against this implementation (that was used
> as *one* implementation just to have an example to discuss on).
>
> The main point was the use of volatile (and other techniques) to
> guarantee a correct compiler output, whatever legal (respect the C
> standard) optimizations the compiler thinks to do.
>
> It seems to me the arguments againts or for volatile are completely
> indipendent from the implementation of ring-buffer.

Of course "volatile" is needed, in general, whenever anything is written
in one thread and read in another. The issue, I think, is when
"volatile" is _enough_.

I feel that detection of a full buffer (FIFO overflow) is required for a
proper ring buffer implementation, and that has implications for the
data structure needed, and that has implications for whether critical
sections are needed.

If the FIFO implementation is based on just two pointers (read and
write), and each pointer is modified by just one of the two threads
(main thread = reader, and interrupt handler = writer), and those
modifications are both "volatile" AND atomic (which has not been
discussed so far, IIRC...), then one can do without a critical region.
But then detection of a full buffer needs one "wasted" element in the
buffer.

To avoid the wasted element, one could add a "full"/"not full" Boolean
flag. But that flag would be modified by both threads, and should be
modified atomically together with the pointer modifications, which (I
think) means that a critical section is needed.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6uh2$5gr$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=684&group=comp.arch.embedded#684

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 20:54:26 +0200
Organization: A noiseless patient Spider
Lines: 85
Message-ID: <sl6uh2$5gr$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl1c3a$dfr$1@dont-email.me>
<sl6s79$jt1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 25 Oct 2021 18:54:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="a5438c96186927b7a6a5f2a6654af5ec";
logging-data="5659"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19q6kDGf9W6SBWqjPAo55n8zJ7R0Hbb1zI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:zltAH7yeybu5RUqLvhHUzYl+uAs=
In-Reply-To: <sl6s79$jt1$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Mon, 25 Oct 2021 18:54 UTC

On 25/10/2021 20:15, pozz wrote:
> Il 23/10/2021 18:09, David Brown ha scritto:
> [...]
>> Marking "in" and "buf" as volatile is /far/ better than using a critical
>> section, and likely to be more efficient than a memory barrier.  You can
>> also use volatileAccess rather than making buf volatile, and it is often
>> slightly more efficient to cache volatile variables in a local variable
>> while working with them.
>
> I think I got your point, but I'm wondering why there are plenty of
> examples of ring-buffer implementations that don't use volatile at all,
> even if the author explicitly refers to interrupts and multithreading.

You don't have to use "volatile". You can make correct code here using
critical sections - it's just a lot less efficient. (If you have a
queue where more than one context can be reading it or writing it, then
you /do/ need some kind of locking mechanism.)

You can also use memory barriers instead of volatile, but it is likely
to be slightly less efficient.

You can also use atomics instead of volatiles, but it is also quite
likely to be slightly less efficient. If you have an SMP system, on the
other hand, then you need something more than volatile and compiler
memory barriers - atomics are quite possibly the most efficient solution
in that case.

And sometimes you can make code that doesn't need any special treatment
at all, because you know the way it is being called. If the two ends of
your buffer are handled by tasks in a cooperative multi-tasking
scenario, then there is no problem - you don't need to worry about
volatile or any alternatives. If you know your interrupt can't occur
while the other end of the buffer is being handled, that can reduce your
need for volatile. (In particular, that can also avoid complications if
you have counter variables that are bigger than the processor can handle
atomically - usually not a problem for a 32-bit Cortex-M, but often
important on an 8-bit AVR.)

If you know, for a fact, that the code will be compiled by a weak
compiler or with weak optimisation, or that the "get" and "put"
implementations will always be in a separately compiled unit from code
calling these functions and you'll never use any kind of cross-unit
optimisations, then you can get often away without using volatile.

>
> Just an example[1] by Quantum Leaps. It promises to be a *lock-free* (I
> think thread-safe) ring-buffer implementation in the scenario of single
> producer/single consumer (that is my scenario too).

It's lock-free, but not safe in the face of modern optimisation (gcc has
had LTO for many years, and a lot of high-end commercial embedded
compilers have used such techniques for decades). And I'd want to study
it in detail and think a lot before accepting that it is safe to use its
16-bit counters on an 8-bit AVR. That could be fixed by just changing
the definition of the RingBufCtr type, which is a nice feature in the code.

>
> In the source code there's no use of volatile. I could call
> RingBuf_put() in my rx uart ISR and call RingBuf_get() in my mainloop code.
>

You don't want to call functions from an ISR if you can avoid it, unless
the functions are defined in the same unit and can be inlined. On many
processors (less so on the Cortex-M) calling an external function from
an ISR means a lot of overhead to save and restore the so-called
"volatile" registers (no relation to the C keyword "volatile"), usually
completely unnecessarily.

> From what I learned from you, this code usually works, but the standard
> doesn't guarantee it will work with every old, current and future
> compilers.
>

Yes, that's a fair summary.

It might be good enough for some purposes. But since "volatile" will
cost nothing in code efficiency but greatly increase the portability and
safety of the code, I'd recommend using it. And I am certainly in
favour of thinking carefully about these things - as you did in the
first place, which is why we have this thread.

>
>
> [1] https://github.com/QuantumLeaps/lock-free-ring-buffer

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6uoh$77d$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=685&group=comp.arch.embedded#685

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 20:58:25 +0200
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <sl6uoh$77d$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 18:58:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="a5438c96186927b7a6a5f2a6654af5ec";
logging-data="7405"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/NwaM2GiM7eOL7x/fyB93E84LZzPaFv0Y="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:NK6Nxoh7u1HhROq7Tjy0/MXP+eA=
In-Reply-To: <ito14pF55oU1@mid.individual.net>
Content-Language: en-GB
 by: David Brown - Mon, 25 Oct 2021 18:58 UTC

On 25/10/2021 17:34, Niklas Holsti wrote:

> And if each of those two items is large, yes. But here we have a FIFO of
> 8-bit characters... few programs are so tight on memory that they cannot
> stand one unused octet.

I remember a program I worked with where the main challenge for the
final features was not figuring out the implementation, but finding a
few spare bytes of code space and a couple of spare bits of ram to use.
And that was with 32 KB ROM and 512 bytes RAM (plus some bits in the
registers of peripherals that weren't used). That was probably the last
big assembly program I wrote - non-portability was a killer.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl6vcu$cca$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=686&group=comp.arch.embedded#686

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dp...@tgi-sci.com (Dimiter_Popoff)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 22:09:17 +0300
Organization: TGI
Lines: 36
Message-ID: <sl6vcu$cca$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qtm$9oh$1@dont-email.me> <itobkaF258sU1@mid.individual.net>
Reply-To: dp@tgi-sci.com
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 19:09:18 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="328f47122045252f51317f11fee19557";
logging-data="12682"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+BFVFzEsY7Sk0ptnAToNoRdo42kXC8NMU="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:C1uIxgq29tMSUF0N1pEC+fXrKHc=
In-Reply-To: <itobkaF258sU1@mid.individual.net>
Content-Language: en-US
 by: Dimiter_Popoff - Mon, 25 Oct 2021 19:09 UTC

On 10/25/2021 21:33, Niklas Holsti wrote:
> ....
>
> If the FIFO implementation is based on just two pointers (read and
> write), and each pointer is modified by just one of the two threads
> (main thread = reader, and interrupt handler = writer), and those
> modifications are both "volatile" AND atomic (which has not been
> discussed so far, IIRC...), then one can do without a critical region.
> But then detection of a full buffer needs one "wasted" element in the
> buffer.

Why atomic? No need for that unless more than one interrupted task would
want to read from the fifo at the same time, which is nonsense. [I once
wasted a day looking at a garbled input from an auxiliary HV to a netMCA
only to discover I had left a shell to start via the same UART during
boot, through an outdated (but available) driver accessing the same
UART the standard driver through which the HV was used....
This across half the planet, customer was in South Africa. Not
an experience anybody would ask for, I can tell you :)].
Just like there is no need to mask interrupts, as you mentioned I
had said before.

> To avoid the wasted element, one could add a "full"/"not full" Boolean
> flag. But that flag would be modified by both threads, and should be
> modified atomically together with the pointer modifications, which (I
> think) means that a critical section is needed.

Now this is where atomic access is necessary - for no good reason in
this case, as mentioned before, but if one wants to bang their head
in the wall this is the proper way to do it.
As for "volatile" I can't say much, but if this is the way to make
the compiler access every time the address declared such instead of
using some stale data it has then it would be needed of
course.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<itog94F3126U1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=687&group=comp.arch.embedded#687

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.h...@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 22:53:08 +0300
Organization: Tidorum Ltd
Lines: 25
Message-ID: <itog94F3126U1@mid.individual.net>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qtm$9oh$1@dont-email.me> <itobkaF258sU1@mid.individual.net>
<sl6vcu$cca$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net 1rKuFvsjiHTFucb0n4vhvAxebb8QtOi90rDv8/5ywJJEh8cMeS
Cancel-Lock: sha1:/aqmJubh+I/wMH4WvJDCm3J+dgc=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
In-Reply-To: <sl6vcu$cca$1@dont-email.me>
Content-Language: en-US
 by: Niklas Holsti - Mon, 25 Oct 2021 19:53 UTC

On 2021-10-25 22:09, Dimiter_Popoff wrote:
> On 10/25/2021 21:33, Niklas Holsti wrote:
>> ....
>>
>> If the FIFO implementation is based on just two pointers (read and
>> write), and each pointer is modified by just one of the two threads
>> (main thread = reader, and interrupt handler = writer), and those
>> modifications are both "volatile" AND atomic (which has not been
>> discussed so far, IIRC...), then one can do without a critical region.
>> But then detection of a full buffer needs one "wasted" element in the
>> buffer.
>
> Why atomic?

If the read/write pointers/indices are, say, 16 bits, but the processor
has only 8-bit store/load instructions, updating a pointer/index happens
non-atomically, 8 bits at a time, and the interrupt handler can read a
half-updated value if the interrupt happens in the middle of an update.
That would certainly mess up the comparison between the read and write
points in the interrupt handler.

In the OP's code, I suppose (but I don't recall) that the indices are 8
bits, so probably atomically readable and writable.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl72gr$3js$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=688&group=comp.arch.embedded#688

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dp...@tgi-sci.com (Dimiter_Popoff)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 23:02:34 +0300
Organization: TGI
Lines: 29
Message-ID: <sl72gr$3js$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qtm$9oh$1@dont-email.me> <itobkaF258sU1@mid.individual.net>
<sl6vcu$cca$1@dont-email.me> <itog94F3126U1@mid.individual.net>
Reply-To: dp@tgi-sci.com
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 20:02:35 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="328f47122045252f51317f11fee19557";
logging-data="3708"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Ql/GyIsJzsYgVGP6EemhJuKCCSXKSFik="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:Ijl2dcFl1fXazY5dzH+OQbHoVtI=
In-Reply-To: <itog94F3126U1@mid.individual.net>
Content-Language: en-US
 by: Dimiter_Popoff - Mon, 25 Oct 2021 20:02 UTC

On 10/25/2021 22:53, Niklas Holsti wrote:
> On 2021-10-25 22:09, Dimiter_Popoff wrote:
>> On 10/25/2021 21:33, Niklas Holsti wrote:
>>> ....
>>>
>>> If the FIFO implementation is based on just two pointers (read and
>>> write), and each pointer is modified by just one of the two threads
>>> (main thread = reader, and interrupt handler = writer), and those
>>> modifications are both "volatile" AND atomic (which has not been
>>> discussed so far, IIRC...), then one can do without a critical
>>> region. But then detection of a full buffer needs one "wasted"
>>> element in the buffer.
>>
>> Why atomic?
>
>
> If the read/write pointers/indices are, say, 16 bits, but the processor
> has only 8-bit store/load instructions, updating a pointer/index happens
> non-atomically, 8 bits at a time, and the interrupt handler can read a
> half-updated value if the interrupt happens in the middle of an update.
> That would certainly mess up the comparison between the read and write
> points in the interrupt handler.
>
> In the OP's code, I suppose (but I don't recall) that the indices are 8
> bits, so probably atomically readable and writable.
>

Ah, well, this is a possible scenario in a multicore system (or single
core if the two bytes are written by separate opcodes).

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl77p9$6gt$1@z-news.wcss.wroc.pl>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=689&group=comp.arch.embedded#689

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!newsfeed.neostrada.pl!unt-exc-02.news.neostrada.pl!newsfeed.pionier.net.pl!pwr.wroc.pl!news.wcss.wroc.pl!not-for-mail
From: antis...@math.uni.wroc.pl
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 21:32:25 +0000 (UTC)
Organization: Politechnika Wroclawska
Lines: 21
Message-ID: <sl77p9$6gt$1@z-news.wcss.wroc.pl>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org> <sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
NNTP-Posting-Host: hera.math.uni.wroc.pl
X-Trace: z-news.wcss.wroc.pl 1635197545 6685 156.17.86.1 (25 Oct 2021 21:32:25 GMT)
X-Complaints-To: abuse@news.pwr.wroc.pl
NNTP-Posting-Date: Mon, 25 Oct 2021 21:32:25 +0000 (UTC)
Cancel-Lock: sha1:CDNPs7qJgmBZ1E9IRTS6Y18BVDQ=
User-Agent: tin/2.4.3-20181224 ("Glen Mhor") (UNIX) (Linux/4.19.0-10-amd64 (x86_64))
X-Received-Bytes: 2008
 by: antis...@math.uni.wroc.pl - Mon, 25 Oct 2021 21:32 UTC

Don Y <blockedofcourse@foo.invalid> wrote:
> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
> >> Disable interrupts while accessing the fifo. you really have to.
> >> alternatively you'll often get away not using a fifo at all,
> >> unless you're blocking for a long while in some part of the code.
> >
> > Why would you do that. The fifo write pointer is only modified by
> > the interrupt handler, the read pointer is only modified by the
> > interrupted code. Has been done so for times immemorial.
>
> The OPs code doesn't differentiate between FIFO full and empty.

If you read carefuly what he wrote you would know that he does.
The trick he uses is that his indices may point outside buffer:
empty is equal indices, full is difference equal to buffer
size. Of course his approach has its own limitations, like
buffer size being power of 2 and with 8 bit indices maximal
buffer size is 128.

--
Waldek Hebisch

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<16b16417ac5d7877$1$3155485$e8dde262@news.thecubenet.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=690&group=comp.arch.embedded#690

  copy link   Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on
Newsgroups: comp.arch.embedded
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org> <sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me> <sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
From: no.s...@please.net (Clifford Heath)
Date: Tue, 26 Oct 2021 08:43:12 +1100
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <itn713FpntqU1@mid.individual.net>
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Message-ID: <16b16417ac5d7877$1$3155485$e8dde262@news.thecubenet.com>
Lines: 36
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!2a07:8080:119:fe:2ddf:7d15:5c99:67fa.MISMATCH!news.thecubenet.com!not-for-mail
NNTP-Posting-Date: Mon, 25 Oct 2021 21:43:12 +0000
X-Received-Bytes: 2292
Organization: theCubeNet - www.thecubenet.com
X-Complaints-To: abuse@thecubenet.com
 by: Clifford Heath - Mon, 25 Oct 2021 21:43 UTC

On 25/10/21 7:09 pm, Niklas Holsti wrote:
> On 2021-10-24 23:27, Dimiter_Popoff wrote:
>> On 10/24/2021 22:54, Don Y wrote:
>>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>>> Disable interrupts while accessing the fifo. you really have to.
>>>>> alternatively you'll often get away not using a fifo at all,
>>>>> unless you're blocking for a long while in some part of the code.
>>>>
>>>> Why would you do that. The fifo write pointer is only modified by
>>>> the interrupt handler, the read pointer is only modified by the
>>>> interrupted code. Has been done so for times immemorial.
>>>
>>> The OPs code doesn't differentiate between FIFO full and empty.
>>
>> So he should fix that first, there is no sane reason why not.
>> Few things are simpler to do than that.
>
>
>    [snip]
>> Whatever handshakes he makes there is no problem knowing whether
>> the fifo is full - just check if the position the write pointer
>> will have after putting the next byte matches the read pointer
>> at the moment.  Like I said before, few things are simpler than
>> that, can't imagine someone working as a programmer being
>> stuck at *that*.
>
> That simple check would require keeping a maximum of only N-1 entries in
> the N-position FIFO buffer, and the OP explicitly said they did not want
> to allocate an unused place in the buffer (which I think is unreasonable
> of the OP, but that is only IMO).

In my opinion too. If you're going to waste a memory cell, why not use
it for a count variable instead of an unused element?

CH

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl78jh$f3a$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=691&group=comp.arch.embedded#691

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 23:46:23 +0200
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <sl78jh$f3a$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qtm$9oh$1@dont-email.me> <itobkaF258sU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 25 Oct 2021 21:46:25 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cbaea9215393f46884ecaccb923a9697";
logging-data="15466"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19txgLSBjFB8JAZklg1mm2+Rgzv4mGEbJY="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:QdxVfw8JtM2gmkhJE8Zj847oCoA=
In-Reply-To: <itobkaF258sU1@mid.individual.net>
Content-Language: it
 by: pozz - Mon, 25 Oct 2021 21:46 UTC

Il 25/10/2021 20:33, Niklas Holsti ha scritto:
> On 2021-10-25 20:52, pozz wrote:
>> Il 25/10/2021 17:34, Niklas Holsti ha scritto:
>>> On 2021-10-25 16:04, Dimiter_Popoff wrote:
>>>> On 10/25/2021 11:09, Niklas Holsti wrote:
>>>>> On 2021-10-24 23:27, Dimiter_Popoff wrote:
>>>>>> On 10/24/2021 22:54, Don Y wrote:
>>>>>>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>>>>>>> Disable interrupts while accessing the fifo. you really have to.
>>>>>>>>> alternatively you'll often get away not using a fifo at all,
>>>>>>>>> unless you're blocking for a long while in some part of the code.
>>>>>>>>
>>>>>>>> Why would you do that. The fifo write pointer is only modified by
>>>>>>>> the interrupt handler, the read pointer is only modified by the
>>>>>>>> interrupted code. Has been done so for times immemorial.
>>>>>>>
>>>>>>> The OPs code doesn't differentiate between FIFO full and empty.
>
>
> (I suspect something is not quite right with the attributions of the
> quotations above -- Dimiter probably did not suggest disabling
> interrupts -- but no matter.)
>
>    [snip]
>
>
>> When I have a small (<256) power-of-two (16, 32, 64, 128) buffer (and
>> this is the case for a UART receiving ring-buffer), I like to use this
>> implementation that works and doesn't waste any element.
>>
>> However I know this isn't the best implementation ever and it's a pity
>> the thread emphasis has been against this implementation (that was
>> used as *one* implementation just to have an example to discuss on).
>>
>> The main point was the use of volatile (and other techniques) to
>> guarantee a correct compiler output, whatever legal (respect the C
>> standard) optimizations the compiler thinks to do.
>>
>> It seems to me the arguments againts or for volatile are completely
>> indipendent from the implementation of ring-buffer.
>
>
> Of course "volatile" is needed, in general, whenever anything is written
> in one thread and read in another. The issue, I think, is when
> "volatile" is _enough_.
>
> I feel that detection of a full buffer (FIFO overflow) is required for a
> proper ring buffer implementation, and that has implications for the
> data structure needed, and that has implications for whether critical
> sections are needed.
>
> If the FIFO implementation is based on just two pointers (read and
> write), and each pointer is modified by just one of the two threads
> (main thread = reader, and interrupt handler = writer), and those
> modifications are both "volatile" AND atomic (which has not been
> discussed so far, IIRC...), then one can do without a critical region.
> But then detection of a full buffer needs one "wasted" element in the
> buffer.

Yeah, this is exactly the topic of my original post. Anyway it seems
what you say isn't always correct. As per C standard, the compiler could
reorder instructions that involve non-volatile data. So, even in your
simplified scenario (atomic access for indexes), volatile for the head
only (that ISR changes) is not sufficient.

The function called in the mainloop and that get data from the buffer
access three variables: head (changed in ISR), tail (not changed in ISR)
and buf[] (written in ISR ad read in mainloop).

The get function firstly check if some data is available in the FIFO and
*next* read from buf[]. However compiler could rearrange instructions so
reading from buf[] at first and then checking FIFO empty condition.
If the compiler goes this way, errors could occur during execution.

My original question was exactly if this could happen (without breaking
C specifications) and, if yes, how to avoid this: volatile? critical
section? memory barrier?

David Brown said this is possible and suggested to access both head and
buf[] as volatile in get() function, forcing the compiler to respect the
order of instructions.

> To avoid the wasted element, one could add a "full"/"not full" Boolean
> flag. But that flag would be modified by both threads, and should be
> modified atomically together with the pointer modifications, which (I
> think) means that a critical section is needed.

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl79no$ofp$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=692&group=comp.arch.embedded#692

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Tue, 26 Oct 2021 00:05:43 +0200
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <sl79no$ofp$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl4fk0$2si$1@dont-email.me> <itn713FpntqU1@mid.individual.net>
<sl6a1d$4ci$1@dont-email.me> <ito14pF55oU1@mid.individual.net>
<sl6qtm$9oh$1@dont-email.me> <itobkaF258sU1@mid.individual.net>
<sl6vcu$cca$1@dont-email.me> <itog94F3126U1@mid.individual.net>
<sl72gr$3js$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 22:05:44 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5e00e34e3a6357408d29d2fb7e0a5829";
logging-data="25081"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dxrq59ufQeBlTmqebonHk7APXwln7734="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:6QfyQhNw8PYneBVsEdMsjHs8nmw=
In-Reply-To: <sl72gr$3js$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Mon, 25 Oct 2021 22:05 UTC

On 25/10/2021 22:02, Dimiter_Popoff wrote:
> On 10/25/2021 22:53, Niklas Holsti wrote:
>> On 2021-10-25 22:09, Dimiter_Popoff wrote:
>>> On 10/25/2021 21:33, Niklas Holsti wrote:
>>>> ....
>>>>
>>>> If the FIFO implementation is based on just two pointers (read and
>>>> write), and each pointer is modified by just one of the two threads
>>>> (main thread = reader, and interrupt handler = writer), and those
>>>> modifications are both "volatile" AND atomic (which has not been
>>>> discussed so far, IIRC...), then one can do without a critical
>>>> region. But then detection of a full buffer needs one "wasted"
>>>> element in the buffer.
>>>
>>> Why atomic?
>>
>>
>> If the read/write pointers/indices are, say, 16 bits, but the
>> processor has only 8-bit store/load instructions, updating a
>> pointer/index happens non-atomically, 8 bits at a time, and the
>> interrupt handler can read a half-updated value if the interrupt
>> happens in the middle of an update. That would certainly mess up the
>> comparison between the read and write points in the interrupt handler.
>>
>> In the OP's code, I suppose (but I don't recall) that the indices are
>> 8 bits, so probably atomically readable and writable.
>>
>
> Ah, well, this is a possible scenario in a multicore system (or single
> core if the two bytes are written by separate opcodes).

For the AVR, writing 16-bit values is not atomic - but the OP used 8-bit
counters (which are more appropriate for the AVR anyway, as they don't
have enough memory to spend on big buffers).

For multi-core systems you can have added complications. Memory
accesses are always seen in assembly-code order on one core, regardless
of how they may be re-ordered by buffers, caches, out-of-order
execution, speculative execution, etc. (CPU designers sometimes have to
work quite hard to achieve this, but anything else would be impossible
to work with.) However, the order seen by other cores could be
different. So "volatile" accesses are no longer enough - you need to
use C11/C++11 atomics, or the equivalent.

(Don't use C11/C++11 atomics on gcc for the Cortex-M or AVR, at least
not with anything that can't be done with a single read or write
instruction - the library that comes with gcc is deeply flawed.)

Re: How to write a simple driver in bare metal systems: volatile, memory barrier, critical sections and so on

<sl7arc$v0i$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=693&group=comp.arch.embedded#693

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: blockedo...@foo.invalid (Don Y)
Newsgroups: comp.arch.embedded
Subject: Re: How to write a simple driver in bare metal systems: volatile,
memory barrier, critical sections and so on
Date: Mon, 25 Oct 2021 15:24:38 -0700
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <sl7arc$v0i$1@dont-email.me>
References: <skvcnd$5dv$1@dont-email.me> <sl3d4h$17d3$1@gioia.aioe.org>
<sl3f65$jdl$1@dont-email.me> <sl4dm3$5ut$1@dont-email.me>
<sl77p9$6gt$1@z-news.wcss.wroc.pl>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Oct 2021 22:24:44 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="aad27a47c67af399cd185ebd20909dc7";
logging-data="31762"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5wgCHB4fBYdJqPs3BeGXv"
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101
Thunderbird/52.1.1
Cancel-Lock: sha1:iKBI9BJgfTvlWD0a4GNLqC2WwEM=
In-Reply-To: <sl77p9$6gt$1@z-news.wcss.wroc.pl>
Content-Language: en-US
 by: Don Y - Mon, 25 Oct 2021 22:24 UTC

On 10/25/2021 2:32 PM, antispam@math.uni.wroc.pl wrote:
> Don Y <blockedofcourse@foo.invalid> wrote:
>> On 10/24/2021 4:14 AM, Dimiter_Popoff wrote:
>>>> Disable interrupts while accessing the fifo. you really have to.
>>>> alternatively you'll often get away not using a fifo at all,
>>>> unless you're blocking for a long while in some part of the code.
>>>
>>> Why would you do that. The fifo write pointer is only modified by
>>> the interrupt handler, the read pointer is only modified by the
>>> interrupted code. Has been done so for times immemorial.
>>
>> The OPs code doesn't differentiate between FIFO full and empty.
>
> If you read carefuly what he wrote you would know that he does.
> The trick he uses is that his indices may point outside buffer:
> empty is equal indices, full is difference equal to buffer

Doesn't matter as any index can increase by any amount and
invalidate the "reality" of the buffer's contents (i.e.
actual number of characters that have been tranfered to
that region of memory).

Buffer size is 128, for example. in is 127, out is 127.
What's that mean? Can you tell me what has happened prior
to this point in time? Have 127 characters been received?
Or, 383? Or, 1151?

How many characters have been removed from the buffer?
(same numeric examples).

Repeat for .in being 129, 227, 255, etc.

Remember, there is nothing that GUARANTEES that the uart
task is keeping up with the input data rate. So, the buffer
could wrap 50 times and it's instantaneous state (visible
via .in and .out) would appear unchanged to that task!

If you wanted to rely on .in != .out to indicate the
presence of data REGARDLESS OF WRAPS, then you'd need the
size of each to be "significantly larger" than the maximum
fill rate of the buffer to ensure THEY can't wrap.

> size. Of course his approach has its own limitations, like
> buffer size being power of 2 and with 8 bit indices maximal
> buffer size is 128.

The biggest practical limitation is that of expectations of
other developers who may inherit (or copy) his code expecting
the FIFO to be "well behaved".

Pages:123
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor