Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent default serial baud #2398

Closed
wants to merge 1 commit into from
Closed

Consistent default serial baud #2398

wants to merge 1 commit into from

Conversation

bogdanm
Copy link
Contributor

@bogdanm bogdanm commented Aug 9, 2016

This commit changes the default baud of a new Serial or SerialBase
instance to the one defined by the core configuration
(MBED_CONF_CORE_STDIO_BAUD_RATE). This should ensure a consistent
behaviour of newly created Serial objects across all platforms.

Related to #2396.

This commit changes the default baud of a new `Serial` or `SerialBase`
instance to the one defined by the core configuration
(`MBED_CONF_CORE_STDIO_BAUD_RATE`). This should ensure a consistent
behaviour of newly created `Serial` objects across all platforms.

Related to #2396.
@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 9, 2016

/morph test

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 9, 2016

Before this PR, the default baud rate of a serial object was defined in its HAL implementation. The PR might break some existing code, but I personally don't think it's worse than (say) this:

$ ack-grep -w STDIO_BAUD .
TARGET_NXP/TARGET_LPC43XX/TARGET_LPC4337/PeripheralNames.h
92:#define STDIO_BAUD        9600

TARGET_NXP/TARGET_LPC43XX/TARGET_LPC4330/PeripheralNames.h
92:#define STDIO_BAUD        115200

TARGET_NXP/TARGET_LPC43XX/serial_api.c
166:        serial_baud (obj, STDIO_BAUD);

I think we should bite the bullet and go ahead with it.
@0xc0170 @sg-

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 9, 2016

Interesting. This seems to have failed because somehow build.py doesn't get along with the configuration system. I'll investigate. Please don't merge for now (but feel free to comment anyway).

@@ -26,10 +26,11 @@ SerialBase::SerialBase(PinName tx, PinName rx) :
_thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
_rx_usage(DMA_USAGE_NEVER),
#endif
_serial(), _baud(9600) {
_serial(), _baud(MBED_CONF_CORE_STDIO_BAUD_RATE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the default value for MBED_CONF_CORE_STDIO_BAUD_RATE ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever you want it to be! :) Right now it's 9600:

https://github.com/ARMmbed/mbed-os/blob/master/mbed_lib.json#L11

@geky
Copy link
Contributor

geky commented Aug 9, 2016

Sorry I'm late to the party.

Are we sure we want to bind stdio config and the Serial class together like this? It seems like these are two independent concepts (stdio could be over spi for all the application cares).

As a user, I would find it surprising if I changed my terminal baud-rate and suddenly the uart underlying a homemade driver started failing.

@bridadan
Copy link
Contributor

bridadan commented Aug 9, 2016

@geky That's a really good point. I'm inclined to agree on that. In the past we've only slightly differentiated between stdio and serial. But maybe we need to start clearly documenting where these two entities differ?

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 9, 2016

As a user, I would find it surprising if I changed my terminal baud-rate and suddenly the uart underlying a homemade driver started failing.

I get what you're saying, the problem is that we allow people to instantiate Serial objects without specifying a baud rate (which is wrong to begin with IMO), and by doing that they end up relying on the platform's HAL implementation to choose a baud rate. That might not be what they want; even if it is, the HAL's baud rate might change unexpectedly. #2396 makes a good case for this PR, I think. Plus, there's quite a bit of tradition in mbed to associate the terminal baud rate with your serial instance baud rate, simply because until recently the only good way to change the default terminal baud rate was to instantiate a Serial object on top of it and call baud on that.
I'm not saying this PR fixes everything, but it feels less wrong and maybe a bit more consistent. I'm of course open to better solutions.

@bridadan
Copy link
Contributor

bridadan commented Aug 9, 2016

I think in this case, exposing the Serial class's default baud rate as a config option would be confusing. The last thing I want to do is start adding *s to all of our docs saying "value x by default (unless overriden by config`)".

But since stdio is an instantiation of the Serial class that we do behind the scenes for the user, having a config option for this makes a lot more sense to me.

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 9, 2016

Not sure what kind of config option you're looking for at this point. Config for what? Default baud rate for all Serial instances? I don't really see how that's better than this PR. Both encourage bad behaviour in a sense (letting people think that declaring a Serial instance without specifying a baud rate is a good idea), but the current PR feels a bit more consistent for the reasons that I already explained. Maybe it's just me.

@geky
Copy link
Contributor

geky commented Aug 9, 2016

they end up relying on the platform's HAL implementation to choose a baud rate.

Yeah, no, this is bad, I think we all agree this needs to be fixed and you deserve a thanks for getting this pr going.


I'm just worried that having the stdio config effect unrelated serial object will unnecessarily confuse users. Before this, modifying the baud rate of your stdio printf had no effect on serial objects on different pins.

Be it good or bad practice, the default baud rate for UART has been conventionally 9600 for a while. The handbook page for Serial even notes this:

The default setting for a Serial connection on the mbed Microcontroller is 9600 baud.

Line 33 seems sufficient for removing the problematic overrides in the targets (though we should probably go remove those anyways).

I still hold that 9600 is a reasonable cross-platform default. However, if we want to require users to be explicit, we can add a constructor that takes the baud-rate and deprecate the non-baud-rate constructors. Maybe it's a bit too early to tell, but this strategy seems to be working for the Thread class.

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 10, 2016

Line 33 seems sufficient for removing the problematic overrides in the targets (though we should probably go remove those anyways).

+1, but that cleanup will be later, first get this in - explicit baud rate in the ctor.

However, if we want to require users to be explicit, we can add a constructor that takes the baud-rate and deprecate the non-baud-rate constructors.

Either that, or do something for our current ctor that might not look good but stdio serial is an exception of its own:

// set default baudrate, for stdio serial we use config, for regular one defaults to 9600 (set in the init list)
if (TX == STDIO_TX) || (RX == STDIO_RX) { //assuming we allow TX=NC or RX=NC
    _baud = MBED_CONF_CORE_STDIO_BAUD_RATE;
}

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 10, 2016

I'm just worried that having the stdio config effect unrelated serial object will unnecessarily confuse users.

You're right, that is confusing. Which is why I think @0xc0170's solution (use MBED_CONF_CORE_STDIO_BAUD_RATE only for Serial pins that use the same UART as the terminal UART) is a better one.

Be it good or bad practice, the default baud rate for UART has been conventionally 9600 for a while.

Or at least it's supposed to. It took me less than 30 seconds yesterday to find #2398 (comment). I'm guessing that's not the only exception.

However, if we want to require users to be explicit, we can add a constructor that takes the baud-rate and deprecate the non-baud-rate constructors.

Definitely, and this was what I initially wanted to do, until I remembered that the Serial constructor looks like this:

Serial(PinName tx, PinName rx, const char *name=NULL);

That name parameter looks quite a bit weird there (plus, I believe it's seldom used), but it still needs to stay there for backward compatibility, and that's exactly where I wanted to put my baud argument. Although I guess there's nothing preventing us from doing this:

Serial(PinName tx, PinName rx, const char *name=NULL, int baud=9600);
Serial(PinName tx, PinName rx, int baud);

(and maybe deprecate the first constructor in the process).

// No lock needed in the constructor

serial_init(&_serial, tx, rx);
serial_baud(&_serial, _baud);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogdanm originally said this:

Before this PR, the default baud rate of a serial object was defined in its HAL implementation. The PR might break some existing code

This actually maybe problematic for Silicon Labs targets since their interface chips are tied to 115200 I believe. If we make this change, you might have to set the default stdio for Silicon Labs targets to be 115200 instead of 9600. @0xc0170 might know more about this.

@sg-
Copy link
Contributor

sg- commented Aug 11, 2016

I don't think STDIO and Serial should share the same macro to define the default speed given that terminal / logging and say GPS or HCI interface etc are more typical use cases of Serial than just logging to terminal IO.

Would a default parameter addition to the constructor of the Serial classes not do the trick?

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 11, 2016

Would a default parameter addition to the constructor of the Serial classes not do the trick?.

It would, but I think we're talking about two different things now:

  1. adding a constructor to Serial that has a baud argument.
  2. keeping the baud rate consistent (as described in Please update mbed-os to have stdio baud rate configurable #2396).

Fixing 1 above (which I'll do) doesn't automatically solve 2, since I can't force a constructor that'll aways require a baud argument for backward compatibility reasons. I think @0xc0170's suggestion is the best to fix 2:

// set default baudrate, for stdio serial we use config, for regular one defaults to 9600 (set in the init list)
if (TX == STDIO_TX) || (RX == STDIO_RX) { //assuming we allow TX=NC or RX=NC
    _baud = MBED_CONF_CORE_STDIO_BAUD_RATE;
}

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 11, 2016

See #2422 for 1 above.

@sg-
Copy link
Contributor

sg- commented Aug 12, 2016

Isn't MBED_CONF_CORE_STDIO_BAUD_RATE used for stdio retarget baud? I don't think stdio retarget and Serial default baud should share the same default value ie: Serial default baud being 9600 should suffice.

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 16, 2016

This was replaced by #2422.

@bogdanm bogdanm closed this Aug 16, 2016
@bogdanm bogdanm deleted the serial_fixe branch October 18, 2016 13:53
artokin pushed a commit to artokin/mbed-os that referenced this pull request Aug 17, 2020
…..48609ae

48609ae Merge branch 'release_internal' into release_external
62d8586 Ignore ns_monitor when receiving Ack (ARMmbed#2417)
3323f36 Add support for Ethernet RA dns configuration
d8e7d40 Iotthd 4239 (ARMmbed#2414)
b46f3c6 add empty function for ws_stack_info_get
fc97980 Changed RADIUS shared secret length to 16-bit value
f827ffc Added information API to Wi-SUN and border router
8f1f9d5 EDFE error handling update
51bf94e Fix adaptation interface unit tests (ARMmbed#2409)
0860b57 FHSS_WS: Fixed reading unicast remaining slots (ARMmbed#2408)
4d8c03b Border Router RADIUS client basic authentication functionality (ARMmbed#2406)
fbfada9 Adaptation IF: Allocate fragmentation buffer only if needed (ARMmbed#2407)
66f1bff Added storing of PAN version to NVM on BR
89826ce Iotthd 4224 (ARMmbed#2403)
3fc1ae2 BR EUI-64 is now selected for 4WH using PMKID on 4WH Message 1
af8438e Timing tool traces (ARMmbed#2401)
7938795 Fixed new PD data request for check if EDFE exchange is active.
85ab8fd Extented Frame exchange support
86b1f27 Merge pull request ARMmbed#2399 from ARMmbed/IOTTHD-4220
fed69e0 Add missing test method to ws_empty_functions
6b58e26 Add EDFE mode to Socket API setsockopt
1283077 Test API to adjust 6LoWPAN fragmentation MTU size (ARMmbed#2398)
e787874 Init MAC MTU size based on driver MTU size (ARMmbed#2397)
bf8e89e Ignore neighbors using unsupported channel function (ARMmbed#2395)
1c263fd FHSS exclude channel usage from mask and Brazilian Domain support
841dcbe MAC: Configurable data whitening (ARMmbed#2393)
9a10d66 Fix global address detection (ARMmbed#2392)
f27fe86 Corrected network name and PAN ID change on auth start
bcce0ed Clarified border router routing table API description
e4630a4 Wi-SUN interface now informs address changes as interface events
2174374 Fix error found by coverity (ARMmbed#2389)
843254a MPL: traces for transmit and receive message (ARMmbed#2387)

git-subtree-dir: features/nanostack/sal-stack-nanostack
git-subtree-split: 48609ae
artokin pushed a commit to artokin/mbed-os that referenced this pull request Aug 18, 2020
…..48609ae

48609ae Merge branch 'release_internal' into release_external
62d8586 Ignore ns_monitor when receiving Ack (ARMmbed#2417)
3323f36 Add support for Ethernet RA dns configuration
d8e7d40 Iotthd 4239 (ARMmbed#2414)
b46f3c6 add empty function for ws_stack_info_get
fc97980 Changed RADIUS shared secret length to 16-bit value
f827ffc Added information API to Wi-SUN and border router
8f1f9d5 EDFE error handling update
51bf94e Fix adaptation interface unit tests (ARMmbed#2409)
0860b57 FHSS_WS: Fixed reading unicast remaining slots (ARMmbed#2408)
4d8c03b Border Router RADIUS client basic authentication functionality (ARMmbed#2406)
fbfada9 Adaptation IF: Allocate fragmentation buffer only if needed (ARMmbed#2407)
66f1bff Added storing of PAN version to NVM on BR
89826ce Iotthd 4224 (ARMmbed#2403)
3fc1ae2 BR EUI-64 is now selected for 4WH using PMKID on 4WH Message 1
af8438e Timing tool traces (ARMmbed#2401)
7938795 Fixed new PD data request for check if EDFE exchange is active.
85ab8fd Extented Frame exchange support
86b1f27 Merge pull request ARMmbed#2399 from ARMmbed/IOTTHD-4220
fed69e0 Add missing test method to ws_empty_functions
6b58e26 Add EDFE mode to Socket API setsockopt
1283077 Test API to adjust 6LoWPAN fragmentation MTU size (ARMmbed#2398)
e787874 Init MAC MTU size based on driver MTU size (ARMmbed#2397)
bf8e89e Ignore neighbors using unsupported channel function (ARMmbed#2395)
1c263fd FHSS exclude channel usage from mask and Brazilian Domain support
841dcbe MAC: Configurable data whitening (ARMmbed#2393)
9a10d66 Fix global address detection (ARMmbed#2392)
f27fe86 Corrected network name and PAN ID change on auth start
bcce0ed Clarified border router routing table API description
e4630a4 Wi-SUN interface now informs address changes as interface events
2174374 Fix error found by coverity (ARMmbed#2389)
843254a MPL: traces for transmit and receive message (ARMmbed#2387)

git-subtree-dir: features/nanostack/sal-stack-nanostack
git-subtree-split: 48609ae
artokin pushed a commit to artokin/mbed-os that referenced this pull request Aug 21, 2020
…3fe574..48609ae

48609ae Merge branch 'release_internal' into release_external
62d8586 Ignore ns_monitor when receiving Ack (ARMmbed#2417)
3323f36 Add support for Ethernet RA dns configuration
d8e7d40 Iotthd 4239 (ARMmbed#2414)
b46f3c6 add empty function for ws_stack_info_get
fc97980 Changed RADIUS shared secret length to 16-bit value
f827ffc Added information API to Wi-SUN and border router
8f1f9d5 EDFE error handling update
51bf94e Fix adaptation interface unit tests (ARMmbed#2409)
0860b57 FHSS_WS: Fixed reading unicast remaining slots (ARMmbed#2408)
4d8c03b Border Router RADIUS client basic authentication functionality (ARMmbed#2406)
fbfada9 Adaptation IF: Allocate fragmentation buffer only if needed (ARMmbed#2407)
66f1bff Added storing of PAN version to NVM on BR
89826ce Iotthd 4224 (ARMmbed#2403)
3fc1ae2 BR EUI-64 is now selected for 4WH using PMKID on 4WH Message 1
af8438e Timing tool traces (ARMmbed#2401)
7938795 Fixed new PD data request for check if EDFE exchange is active.
85ab8fd Extented Frame exchange support
86b1f27 Merge pull request ARMmbed#2399 from ARMmbed/IOTTHD-4220
fed69e0 Add missing test method to ws_empty_functions
6b58e26 Add EDFE mode to Socket API setsockopt
1283077 Test API to adjust 6LoWPAN fragmentation MTU size (ARMmbed#2398)
e787874 Init MAC MTU size based on driver MTU size (ARMmbed#2397)
bf8e89e Ignore neighbors using unsupported channel function (ARMmbed#2395)
1c263fd FHSS exclude channel usage from mask and Brazilian Domain support
841dcbe MAC: Configurable data whitening (ARMmbed#2393)
9a10d66 Fix global address detection (ARMmbed#2392)
f27fe86 Corrected network name and PAN ID change on auth start
bcce0ed Clarified border router routing table API description
e4630a4 Wi-SUN interface now informs address changes as interface events
2174374 Fix error found by coverity (ARMmbed#2389)
843254a MPL: traces for transmit and receive message (ARMmbed#2387)

git-subtree-dir: connectivity/nanostack/sal-stack-nanostack
git-subtree-split: 48609ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants