Author Topic: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?  (Read 117252 times)

0 Members and 1 Guest are viewing this topic.

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #150 on: May 19, 2016, 06:27:26 am »
Quote
It's pretty much "fill in a data structure with a bunch of obscure parameters that you'll have to study the datasheet to understand", and then we'll copy those into the appropriate registers."  All the cost of abstraction with none of the benefits.
Thak god now cube mx is multiplatform and you can get it to write for you the startup code, then you give it the middle finger and just write the registers as they are after the code
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #151 on: May 19, 2016, 07:04:19 am »
It is not rocket science to write simple API that works for 90% of the people 90% of the time (or whatever the percentages are).

Yes, that is how I approach it also (80% is good enough). If you make sure your library code can be replaced and/or extended in a simple way, this is the way to go, otherwise you'll spend 80% of the time on 20% of the exceptions.

Writing good libraries is hard. You'll have to find a pattern for each peripheral that makes sense for that subject and structure it in a way that allows the flexibility, extensibility and efficiency you need. My weapon of choice is to use a sub-set of C++ (templates) but not all chips support that (only have experience with AVR - see signature). The C language is sub-optimal for writing reusable code IMHO. Some may be remidied with naming and coding conventions though.
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #152 on: May 19, 2016, 07:16:21 am »
In my experience, templates would be too hard for general embedded users. Sure I am sure there are EEVBlogger who can write turing complete templates in their sleep, but a lot of embedded programmers are actually traned in... hardware and such. So getting them to write in C is already a challenge. One might argue whether a company should only hire certain people with certain background, but we just sell tools. We don't care who buy them, as long as they buy them :-)
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #153 on: May 19, 2016, 07:26:46 am »
Quote
then you give it the middle finger and just write the registers as they are after the code
I've been waiting for one of the GUI startup-code generators to produce a table of address/value pairs (or something like that) and some minimal code that loops through them, instead of generating "real code" that calls a bunch of bloated library functions...  Of course, a vendor would still need to provide the code libraries as an example of how the peripheral works.  Or improve the data-sheet documentation a lot.  (Is that what we're really talking about?  Peripherals so complex and datasheets so bad that most people can't figure out how to make anything work without example code?)

Quote
templates would be too hard for general embedded users. Sure I am sure there are EEVBlogger who can write turing complete templates in their sleep, but ...
Vendor-provided templates would only have to be "readable", which I don't think is TOO difficult - in some ways templates are more like assembler macros than C macros ever were.
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6460
  • Country: nl
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #154 on: May 19, 2016, 07:41:38 am »
Because doing that is SO common?  I don't think so!  (that's one of the big objections.) 
Average use case is that you set up the clock at startup and it stays at that rate, and/or stops completely in low-power sleep modes.
If I recall correctly (has been some time ago for me these uCs) in those stop conditions the MCU switches to the LSI to be able to auto wake up. So you do switch clocks.
But the point is that ST does not know if you and your company will use an external crystal / oscillator or use the internal etc. so they do have to deliver all the code examples, but I agree there could have been a better sw design made where you can configure it.

Quote
It's not a "50 byte" savings.   More like 1000 bytes.  For code that SHOULD be about 50 bytes. 
Ok i just checked on one of our products and they use ST's HAL libraries but a different one (for STM32F2) the module is called stm32f2xx_rcc and has seperate functions for each clock and configuration.
Compiled (after configuration for our product) it takes only 16 bytes in the end product (release mode optimized IAR compiler) which is good enough don't you think?   
.data             inited   0x20003820     0x10  stm32lib_rcc.o [98]

So not sure whats going on with your hal libs if they are screwed up by ST or you have looked at debug mode no optimization or whatever has went sour but the standard ST hal libraries can be pretty lean and mean. Not all off them though and it is not my point defending all the code ever written by them.
My mistake  |O see below.

I also have had my share of problems with some of their stuff and HW but I just wanted to make a point that some people look at things from a perspective that might be not compatible with working with ARM32 microcontrollers, so to generalize and make the point these persons IMO better not start with the ARM32 stuff unless they want to change their WOW:
1) the new generation Arduino users : " if it does not work within 1 hour, it sucks! Where is my ready to go code?"
2) the dinosaurs (70s assembly generation): " 200 bytes for one function!  :wtf: we wrote an entire OS in 200 bytes in the good 'ol days"  (BTW I do admire those guys, real heroes but sometimes they forget they are in the 21th century and we have evolved in platforms).
3) the HW engineer that barely manages to write C code for an 8 bitter: "I want to directly peek and poke the registers, what is all this classes and abstraction nonsense doing here?"
 ;)

OpenSSL is open source. How long was the heartbleed vulnerability in that code before someone exploited it? Why didn't all of those hoards and hoards of eyes looking at it day and night find and fix it before it became a major headache?
Because it is a tough domain, there are (relatively) little experts and each code change can have severe impacts. Still you can argue that if it was closed source the bug would not have been found and still exist. So yes there are always vulnerabilities in large software codes, take for instance linux, it is swamped with bugs because they sometimes add a Mloc in 1 year!  :scared:
Noone can review that! The same for other OSs like MS Win which is closed source, so they depend on those bounty programs to help them find the vulnerabilities. In any case, open source is not holy but statistically the chances that bugs will be found and solved are larger than with company proprietary software. At least that is my opinion.
« Last Edit: May 24, 2016, 11:13:28 am by Kjelt »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #155 on: May 19, 2016, 07:49:22 am »
Quote
Compiled (after configuration for our product) it takes only 16 bytes in the end product which is good enough don't you think?   
.data             inited   0x20003820     0x10  stm32lib_rcc.o [98]
Well, that's in ".data" - it doesn't include code.   Where's the code?
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6460
  • Country: nl
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #156 on: May 19, 2016, 07:57:26 am »
Quote
Compiled (after configuration for our product) it takes only 16 bytes in the end product which is good enough don't you think?   
.data             inited   0x20003820     0x10  stm32lib_rcc.o [98]
Well, that's in ".data" - it doesn't include code.   Where's the code?
Damn what a mistake  |O  sorry about that, I do this in between on the job (shouldn't be doing this actually),
Indeed the codesize is about 400 bytes (394) which indeed is pretty big for something trivial like this.

 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6460
  • Country: nl
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #157 on: May 19, 2016, 08:06:11 am »
I was used to Keil, now we use IAR haven't worked with it actually since I switched jobs, but they also have this overview of the entire library and ok I can see that it can become large.
This is the entire library usage for our product:

Module                                       ro code  ro data  rw data  ro data
stm32lib.lib: [98]
Total:                                         5 546      398       28

But mind this is for one of our products that has a 512kB ROM size and is filled about 87%, so in this case the overhead for the st lib is about 1,5% if indeed as someone mentioned you use a 32kB version it will be a significant portion. Still you do need (at least part of) the code and as you states yourself rewriting it yourself entirely is not much of an option, and leaving things out is also not an option.
So tweaking with custom defines seem to be a way to go.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #158 on: May 19, 2016, 08:30:13 am »
Quote
1) the Arduino users
2) the dinosaurs
3) the HW engineer
4) The new breed: "it fits in the chip, so who cares if it's a lot bigger than it needs to be."
The sad/frustrating part is that I'm not sure that the "type 4" people aren't correct!  Just like I'm not sure I shouldn't send GHz RPi's at embedded problems where a 50MHz CM3 probably would have worked.   Sigh.
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6460
  • Country: nl
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #159 on: May 19, 2016, 09:12:00 am »
4) The new breed: "it fits in the chip, so who cares if it's a lot bigger than it needs to be."
The sad/frustrating part is that I'm not sure that the "type 4" people aren't correct!  Just like I'm not sure I shouldn't send GHz RPi's at embedded problems where a 50MHz CM3 probably would have worked.   Sigh.
All in perspective ofcourse but it is a balance between the BOM and the NRE in the end. And for a hobbieist unless you get a kick out of reducing the codesize which can be a legitimate goal an sich, I think they also rather spent their hours on the challenging puzzle then to rewrite low level libraries for each and every uC they have.
So yeah my colleagues sometimes still end up rewriting part of some code to fit a too small indesigned uC (which can be a curse also) but it is not as when I started and my first asignment was to add a small feature to an 8bit uC and I had 18 bytes to do it in, ending up rewriting the ADC module which was written in C to assembly in order to shave off another 10 bytes of code. That probably won't happen anymore in todays companies. And I think that is a good thing.

 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #160 on: May 19, 2016, 09:20:57 am »
In my experience, templates would be too hard for general embedded users.

To my eyes, this is very easy code to use and read back / maintain. (AVR)
Code: [Select]
void Globals::OpenMidiPort()
{
    UsartConfig config;
    config.InitAsync(MIDI_BAUDRATE);
   
    MidiInOutPort1.OpenAsync(config);
    MidiInOutPort1.Receive.setEnable();
    MidiInOutPort1.Transmit.setEnable();
}

ISR(USART0_RX_vect)
{
    Globals::MidiInOutPort1.Receive.OnIsCompleteInterrupt();
}

ISR(USART0_UDRE_vect)
{
    Globals::MidiInOutPort1.Transmit.OnAcceptDataInterrupt();
}

The flexibility and complexity is in the type definitions:
Code: [Select]
template<const UsartIds::type UsartId>
class MidiOutPort : public MidiWriter<UsartOutputStream<UsartTransmit<UsartId>, RingBufferFast<uint8_t, MidiTransmitBufferSize> > >
{ };

template<typename StateT>
class MidiInOutPort1T : public Usart<UsartIds::UsartId0, MidiOutPort<UsartIds::UsartId0>, MidiReader<MidiMessageHandler<StateT> > >
{ };

Without going into what everything does, every piece of code is pluggable and can be replaced if needed. But there are typical typedefs novice user can use to get things going fast.

If you complain about code structure and code (design) patterns and you really want a solution, you must be willing to look further outside your comfort zone...
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 
The following users thanked this post: Skashkash

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #161 on: May 19, 2016, 06:37:44 pm »
Quote
"I want to directly peek and poke the registers, what is all this classes and abstraction nonsense doing here?"

I'm actually quite fond of those classes and abstraction / layering as long as they make my life easier.

It is reasonable to think that in performance driven environment where the hardware is pushed to its limit and beyond this could be a problem. but for most of other applications, you rarely run into a case where you are so constrained as to having to deal with this manually.

My philosophy has always been: letting computers do those low value-add stuff and let humans do those high value-add stuff.
================================
https://dannyelectronics.wordpress.com/
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #162 on: May 20, 2016, 06:37:12 am »
Is that what we're really talking about?  Peripherals so complex and datasheets so bad that most people can't figure out how to make anything work without example code?
Yes, or that at least is what i would want, but for the moment i have to do whatever works best (= whatever makes my chip m3 arm chip have at least the same performance as a dspic)

Quote
the HW engineer that barely manages to write C code for an 8 bitter: "I want to directly peek and poke the registers, what is all this classes and abstraction nonsense doing here?"
Yes that's me :) I remember at the first programming course in uni, our teacher on the first day warned us "and if i see any of you microcontroller freaks declare a variable "char" instead of "int" because you want to spare three bytes of memory.." We are writing computer software.

As dannyf said, it's okay to have abstraction. it's useful for a number of situations for a number of reasons. It's nice sometimes to just select a different chip in the project configuration, compile and all is good... but when the generated code is that? on a cortex, which is not that complex? come on, that's just garbage.
« Last Edit: May 20, 2016, 06:52:54 am by JPortici »
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6460
  • Country: nl
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #163 on: May 20, 2016, 07:14:42 am »
but when the generated code is that? on a cortex, which is not that complex? come on, that's just garbage.
IMO it is only garbage if it does not work.
No-one is stopping you from using the working example code as a reference and code your own HAL.
If not tweaked to a single configuration it will still be too large.
Then you need another approach that ST took , the Cube, build a GUI so the user can configure the exact pinout and peripheral setup he wants and generate the end result setup code (at least I think that is what it should do since I have no experience with it).
That seems not too work to well as I read and heard and the major problem AFAICS is that you can not easily switch some settings to another setup without that GUI .
This means that you have to maintain the GUI in your project as well (version control, OS control etc.)

My point is not that the HAL is extremely well written, it is that it works but generates (as we seen) some overhead. So you end up with 5kB of configuration code which is the direct result of general software configuration options, the enormous amount of options and peripherals and that configuring these peripherals sometimes almost needs a statemachine to execute correctly.

Most people that make comments that ST is garbage will be better off with mbed kind of P&P environments but also those generate enormous amount of waste code so that comment is not justified then.
If you like to peek and poke the registers directly, fine do it, but it takes more (a lot of) time to do it that way and the end result is the same except you perhaps saved 2kB or so, that is a choice everyone has to make for himself.

 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6460
  • Country: nl
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #164 on: May 20, 2016, 07:17:59 am »
PS: not want to offend you but if this is what you are now already facing and having problems with:
https://www.eevblog.com/forum/beginners/problems-with-stm32-spi-and-chip-select/msg940835/#msg940835
then I would suggest make a project work first with the out of the box ST code and then start tweaking it.
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #165 on: May 20, 2016, 07:40:22 am »
My goal when writing a library is to make the API as self explanatory as possible. The Intellisense (code completion) should give the dev all the ques necessary to work with the peripheral successfully - with minimal effort. Ideally a similar 'way of doing things' would be used throughout the library to minimize the learning curve.

In big(ger) systems -with more code- abstraction does make a lot of sense. With abstractions comes overhead - optimize only where needed. If it's a small code base on hardware the barely fits, hand optimized assembly may be needed, but if you take that approach for a new project - you're doing it wrong IMO. So some devs may get away with just poking the registers and (re-)reading the datasheets for every project they do. For others this approach may come with a too high a price tag. Developing software takes a long time. The faster you can get it working the better. Over-specing the hardware a bit may make perfect sense in some cases.

If you prefer to poke at the registers directly, then this discussion is of no interest to you, you do not need anything to do that. If you're serious about building a stable code base and reusing your code across multiple projects, then you will need something that fits your bill. What that is, is dependent on the type of projects, the complexity of the code involved and the level of the devs. There is no silver bullet, no 'one shoe fits all'.

[2c]
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #166 on: May 20, 2016, 11:20:05 am »
PS: not want to offend you but if this is what you are now already facing and having problems with:
https://www.eevblog.com/forum/beginners/problems-with-stm32-spi-and-chip-select/msg940835/#msg940835
then I would suggest make a project work first with the out of the box ST code and then start tweaking it.

None taken, that problem came from misunderstanding the documentation.
What i'm doing now is writing software with the HAL and whenever it works cut all the shit out and just read/write the register (which is what is actually done, but in a convoluted way that could/should be done more efficiently as people have pointed out.. and at least between the various stm32 chips there is consistency between the registers names and bits)
Peripheral initialization, that's another mistery: a problem i had (which i wrote in the other topic) was that i could not properly set up the peripheral. Looking at the code that the hal generated i saw i had also to write values in the i2s configuration registers.. which was not written in the "how to set up the peripheral in spi master mode". How fool of me to think that all the info was in one place :) So maybe the probem comes even before the hal?
Yes there are many  people that in spite of this were able to put out functioning product but this is not the way of working i'm used to (and probably not the one for many people in this thread) so it can be very frustrating at times
 

Offline autobot

  • Regular Contributor
  • *
  • Posts: 66
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #167 on: May 20, 2016, 12:56:09 pm »
In my experience, templates would be too hard for general embedded users...So getting them to write in C is already a challenge
but we just sell tools. We don't care who buy them, as long as they buy them :-)

:)

But didn't the Arduino proved that a subset of c++ is usable by beginners, while still letting the grown ups write powerful and reusable libraries(together with a lot of crap, of course) ?
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #168 on: May 20, 2016, 02:38:20 pm »
Quote
I would suggest make a project work first with the out of the box ST code and then start tweaking it.

That's the general approach I take as well. Top-down, outside-in. I typically dice up my projects into small pieces and write those empty pieces with known behaviors: many times those pieces return a pre-determined value / values so that I can tweak the structure the projects. I also don't link in the actual hardware until i'm happy with the overall architecture of the project. Lots of software i2c/spi/uart for example.

Then hardware modules are dropped in gradually to replace the software modules, starting with hardware modules I have used before that are proven. If I have to use a new hardware peripheral, I would start / debug it in a simple environment and not integrated it until it is reasonably reliable.

This approach allows multiple individuals to work on the same project at the same time and generally gives a smooth integration phase. It is slow to get going but it gets to the end typically faster.
================================
https://dannyelectronics.wordpress.com/
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #169 on: May 21, 2016, 01:44:35 am »
Over in another thread: https://www.eevblog.com/forum/microcontrollers/uart-echo-code-outputs-alternate-characters/ , we seem to be discovering that the HAL UART library does a lot of things, but ... not the most common use case for a UART in an embedded system (ie a ring buffer.)

Also, it sure looks like 8bits with parity is broken, and 9bits is either broken or really weird.  And (as I've mentioned previously) the baud rate divisor generation is much more complex than it needs to be (it is in the reference manual too...)
 

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #170 on: May 21, 2016, 04:14:35 am »
That's because ST's philosophy seems to be "thin (obfuscating) layer on top of the hardware registers". e.g. all the functions are no more than "things to poke or peek the I/O registers". It's not a functional API.

There is no ring buffer because the hardware has no such things. The Baud rate code is broken because the hardware guy documents how the state machine works, and forgot to comment that, "Oh, BTW, FUNCTIONALLY speaking, you just do BRR = baudrate. Yes, seriously"
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6460
  • Country: nl
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #171 on: May 21, 2016, 11:31:55 am »
have not read the thread but a ring (circular) buffer is something (if not available in HW) that should be implemented by your own utility layer.
This software module could simply be configured for uint8_t till uint32_t and have any size (restricted by available RAM ofcourse).
HW can be nice but is always limited, if in software you can make it whatever you need.

Some peripherals like the Ethernet MAC interface has dedicated DMA circular buffers, and if I am not mistaken if you use the DMA controller to handle the USART (not sure if this is possible) on the APBx bus, the DMA controller does have some circular buffer capacity:
Quote
The devices feature two general-purpose dual-port DMAs (DMA1 and DMA2) with 8
streams each. They are able to manage memory-to-memory, peripheral-to-memory and
memory-to-peripheral transfers. They share some centralized FIFOs for APB/AHB
peripherals, support burst transfer and are designed to provide the maximum peripheral
bandwidth (AHB/APB).
The two DMA controllers support circular buffer management, so that no specific code is
needed when the controller reaches the end of the buffer. The two DMA controllers also
have a double buffering feature, which automates the use and switching of two memory
buffers without requiring any special code.
« Last Edit: May 21, 2016, 11:34:14 am by Kjelt »
 

Offline Sal Ammoniac

  • Super Contributor
  • ***
  • Posts: 1668
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #172 on: May 23, 2016, 04:12:23 pm »
have not read the thread but a ring (circular) buffer is something (if not available in HW) that should be implemented by your own utility layer.

IMO the ring buffer should be implemented as close to the hardware as possible.
Complexity is the number-one enemy of high-quality code.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #173 on: May 23, 2016, 05:22:38 pm »
Quote
a ring (circular) buffer is something (if not available in HW) that should be implemented by your own utility layer.
I don't disagree, but I think it is a good example of the original subject "STM32Cube Sucks" that the HAL library for the UART peripheral provides three separate APIs for accessing the UART (Polled, Interrupt, and DMA), and as far as I can tell, all three are essentially USELESS for implementing a ring buffer (nor do they use a ringbuffer internally to provide those APIs.)   So we're right back at "ignore the vendor library and write it yourself."
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ST's (STM32Cube) software ecosystem is terrible - how can we fix it?
« Reply #174 on: May 23, 2016, 06:09:13 pm »
Code review!  This is ONE function from stm32f1xx_hal_uart.c (v1.0.1, July 2015)
My comments preceded by the triple-semicolons: ";;;"
Code: [Select]
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
  uint16_t* tmp;
  uint32_t  tmp_state = 0;

  tmp_state = huart->State;
  if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_TX))
;;; Why are there states?  There is no documentation for what the states
;;; are supposed to accomplish...
;;; I suppose we're using tmp_state because huart isn't "locked" yet?
;;; Does this allow polled, DMA, and ISR based uart IO to occur
;;; "simultaneously" (or "not occur" as the case may be)?
;;; How are those supposed to interact?
  {
    if((pData == NULL ) || (Size == 0))
    {
      return  HAL_ERROR;
    }

    /* Process Locked */
    __HAL_LOCK(huart);
;;; locked against what?

    huart->ErrorCode = HAL_UART_ERROR_NONE;
    /* Check if a non-blocking transmit process is ongoing or not */
    if(huart->State == HAL_UART_STATE_BUSY_TX)
    {
      huart->State = HAL_UART_STATE_BUSY_TX_RX;
    }
    else
    {
      huart->State = HAL_UART_STATE_BUSY_RX;
    }
;;; Now we've changed state.  Perhaps this does "real" half-duplex?

    huart->RxXferSize = Size;
    huart->RxXferCount = Size;
;;; Maybe we copy these so that we can do polled and interrupt driven
;;; IO at the same time?  Maybe the states help with that?  Oh well,
;;; a few extra copies won't hurt anything, right?
    /* Check the remain data to be received */
    while(huart->RxXferCount > 0)
    {
      huart->RxXferCount--;
      if(huart->Init.WordLength == UART_WORDLENGTH_9B)
      {
        if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
        {
          return HAL_TIMEOUT;
;;; So where are we now? huart->RxXferSize and huart->RxXferCount
;;; are intermediate values, and we didn't change the state to reflect
;;; that we aborted the input.  Doesn't this mean that we'll fail all
;;; subsequent attempts at input?
;;; We also didn't unlock anything!
        }
        tmp = (uint16_t*) pData ;
;;; for 9bit data, we recast our pointer.  Ok, I guess.  Since we're
;;; so casual with ASSERT, shouldn't we ASSERT word alignment too?
        if(huart->Init.Parity == UART_PARITY_NONE)
        {
          *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
          pData +=2;
;;; OK, we recieved 9bit data and put it in our 16bit buffer.
;;; (uint16_t)0x01FF seems silly.  Doesn't it subsequenlty get promoted
;;; to uint32 to match DR?
        }
        else
        {
          *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
          pData +=1;
;;; But WTF did this do?  If parity is turned on, we'll just throw
;;; away that 9th bit and store 8bits instead?  Doesn't the uart do
;;; 9 bits PLUS parity (10bits total)?  If I put it in 9bit mode, don't
;;; I probably want to receive 9bits for each input?
        }
;;; Did I mention how great I feel that my program will be cluttered with
;;; (broken?) 9bit UART code even though I'm unlikely to ever use it?
      }
      else
      {
        if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
        {
          return HAL_TIMEOUT;
;;; more returing without updating State or lock
        }
        if(huart->Init.Parity == UART_PARITY_NONE)
        {
          *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
;;; OK. masking unnecessary, but probably optimized anyway.
;;; casting "0x00FF" to uint8_t seems particularly silly.
        }
        else
        {
          *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
;;; This is OK for 7 databits + parity, and in fact is necessary since the
;;; UART hardware is documented as leaving the parity bit in DR instead
;;; of stripping it as with many other UARTs.
;;; However, this seems like it's substantially broken for any word size
;;; OTHER than 7 bits.  8 databits+parity will only return 7bits to the user,
;;; databits less than 7 will fail to strip the parity bit.
        }

      }
    }

    /* Check if a non-blocking transmit process is ongoing or not */
    if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
    {
      huart->State = HAL_UART_STATE_BUSY_TX;
    }
    else
    {
      huart->State = HAL_UART_STATE_READY;
    }
    /* Process Unlocked */
    __HAL_UNLOCK(huart);
;;; more state stuff.

    return HAL_OK;
  }
  else
  {
    return HAL_BUSY;
  }
}
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf