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

drivers: uart: samd0: add missing .configure API functionality #23234

Merged
merged 1 commit into from
Apr 17, 2020

Conversation

KubaFYI
Copy link
Contributor

@KubaFYI KubaFYI commented Mar 3, 2020

The UART driver for samd0 was is missing the .configure
functionality expected from api for serial driver. This commit
fixes this by providing basic configuration.

Note, using the function will briefly switch the peripheral off
taking the data line low.

Signed-off-by: Kuba Sanak contact@kuba.fyi

@zephyrbot
Copy link
Collaborator

zephyrbot commented Mar 3, 2020

All checks are passing now.

checkpatch (informational only, not a failure)

-:40: WARNING:LINE_SPACING: Missing a blank line after declarations
#40: FILE: drivers/serial/uart_sam0.c:392:
+	struct uart_sam0_dev_data *const dev_data = DEV_DATA(dev);
+	SercomUsart * const usart = cfg->regs;

-:136: WARNING:LINE_SPACING: Missing a blank line after declarations
#136: FILE: drivers/serial/uart_sam0.c:488:
+	struct uart_sam0_dev_data *const dev_data = DEV_DATA(dev);
+	memcpy(out_cfg, &(dev_data->config_cache),

- total: 0 errors, 2 warnings, 168 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

Your patch has style problems, please review.

NOTE: Ignored message types: AVOID_EXTERNS BRACES CONFIG_EXPERIMENTAL CONST_STRUCT DATE_TIME FILE_PATH_CHANGES MINMAX NETWORKING_BLOCK_COMMENT_STYLE PRINTK_WITHOUT_KERN_LEVEL SPLIT_STRING VOLATILE

NOTE: If any of the errors are false positives, please report
      them to the maintainers.

Tip: The bot edits this comment instead of posting a new one, so you can check the comment's history to see earlier messages.

@KubaFYI KubaFYI force-pushed the fix_uart_sam0_configure branch 3 times, most recently from bf9192e to c25357a Compare March 5, 2020 11:03
drivers/serial/uart_sam0.c Outdated Show resolved Hide resolved
drivers/serial/uart_sam0.c Outdated Show resolved Hide resolved
drivers/serial/uart_sam0.c Show resolved Hide resolved
@stephanosio stephanosio added area: Drivers area: UART Universal Asynchronous Receiver-Transmitter platform: Microchip SAM Microchip SAM Platform (formerly Atmel SAM) labels Mar 6, 2020
drivers/serial/uart_sam0.c Outdated Show resolved Hide resolved
drivers/serial/uart_sam0.c Outdated Show resolved Hide resolved
drivers/serial/uart_sam0.c Outdated Show resolved Hide resolved
@KubaFYI KubaFYI force-pushed the fix_uart_sam0_configure branch 3 times, most recently from c4676d5 to 0dd4721 Compare March 9, 2020 11:18
@KubaFYI KubaFYI requested a review from stephanosio April 7, 2020 13:56
@KubaFYI KubaFYI force-pushed the fix_uart_sam0_configure branch 2 times, most recently from a248844 to 6ff59fc Compare April 7, 2020 14:20
Copy link
Member

@stephanosio stephanosio left a comment

Choose a reason for hiding this comment

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

This PR adds the .configure interface implementation without .config_get. Both should be implemented to pass the UART driver test.

p.s. As far as I can see, all the other UART drivers that support .configure implement .config_get as well.

@KubaFYI KubaFYI force-pushed the fix_uart_sam0_configure branch 2 times, most recently from afb01da to 0c47fbd Compare April 7, 2020 21:17
@nandojve
Copy link
Member

nandojve commented Apr 7, 2020

We've been converting to new DT macros all Atmel SAM variations. The #24110 is about SAM Serial Drivers. I can rebase on top of it and add changes. I waiting some feedback on #24110 anyway but is important know if this can be merged soon.

@nandojve nandojve mentioned this pull request Apr 7, 2020
61 tasks
@KubaFYI KubaFYI force-pushed the fix_uart_sam0_configure branch 2 times, most recently from d56d7f6 to 712fca1 Compare April 8, 2020 09:47
Copy link
Member

@stephanosio stephanosio left a comment

Choose a reason for hiding this comment

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

The conventional way of implementing config_get would be to cache configurations in struct uart_config or data variables in the driver context and simply return those values.

The assumption is, and should be, that an application will not make arbitrary direct configuration changes to the UART hardware.

(e.g. see uart_ns16550.c, uart_nrfx_uart.c and others)

p.s. I am neither +1 nor -1 on the current implementation; the API specification does not state exactly how it should be implemented. With that being the case, the current implementation would cause a problem if an application relies on the behaviour in which the configured and retrieved baudrates exactly match, or the retrieved baudrates are expected to match one of the preset values.

EDIT:

the API specification does not state exactly how it should be implemented

though the test (tests/drivers/uart/uart_basic_api) does.

Copy link
Member

@stephanosio stephanosio left a comment

Choose a reason for hiding this comment

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

As far as the UART driver test (tests/drivers/uart/uart_basic_api) goes, the configured and retrieved data MUST EXACTLY MATCH:

if (memcmp(&uart_cfg, &uart_cfg_check, sizeof(uart_cfg)) != 0) {

So the current implementation will not pass the test.

drivers/serial/uart_sam0.c Outdated Show resolved Hide resolved
@KubaFYI
Copy link
Contributor Author

KubaFYI commented Apr 9, 2020

@stephanosio Ah, this branch was actually created before the test_uart_config.c file even existed so I didn't know the uart_configs are expected to match exactly. Will fix that now.

@KubaFYI KubaFYI force-pushed the fix_uart_sam0_configure branch 3 times, most recently from 2ab0fe7 to 8f62f6c Compare April 9, 2020 13:35
drivers/serial/uart_sam0.c Outdated Show resolved Hide resolved
The UART driver for samd0 was is missing the .configure and
.config_get functionality expected from api for serial driver.
This commit fixes this by providing basic configuration

Signed-off-by: Kuba Sanak <contact@kuba.fyi>
Copy link
Member

@stephanosio stephanosio left a comment

Choose a reason for hiding this comment

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

Looks ok to me.

return 0;
}

static int uart_sam0_config_get(struct device *dev,
Copy link
Collaborator

Choose a reason for hiding this comment

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

jw - Is it worth considering a way to accomplish this task without using memcpy()? I ask because I am curious about it from a secure coding perspective atm. Can this use of memcpy introduce a vulnerability? Please see uart_nrfx_uart.c and uart_ns16550.c for example implementations.

Copy link
Contributor Author

@KubaFYI KubaFYI Apr 16, 2020

Choose a reason for hiding this comment

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

In choosing the memcpy() I was aiming for making absolutely sure that the implementation passes relevant tests which currently use memcmp() to establish whether the set and retrieved struct uart_config's match. Doing things the way uart_nrfx_uart.c or uart_ns16550.c do it can potentially introduce a corner case where the test won't pass because of potential content of padding in the struct which won't be carried across without memcpy().

I guess the proper thing to do would be to edit the tests to use comparison of individual struct member variables instead of memcmp(), but I wanted to avoid doing changes not related to the work I'm doing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah that makes sense, thanks for clarifying! I am learning more with your help.
I wonder if you might want to add the cast to void in anticipation of the MISRA-C coding guidelines currently in discussion, namely regarding memcpy --> (void) memcpy() (see #18344). Adding @ceolin in case he wants to share thoughts here

Copy link
Member

Choose a reason for hiding this comment

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

As long as the copy size is static (i.e. sizeof(dev_data->config_cache)), that is highly unlikely.

As for the out_cfg pointer checking, that is a whole another issue.

Copy link
Member

Choose a reason for hiding this comment

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

Enforcement of MISRA-C, especially arguably ridiculous ones like (void)func() is a no-go for now IIRC.

Copy link
Member

Choose a reason for hiding this comment

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

Hi @jenmwms, regarding MISRA, it is not sepcific to memcpy. The rule says that we should check all return values from non-void functions. It happens, that memcpy() return is quite useless, so we just explicitly ignore it casting to void.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stephanosio

As for the out_cfg pointer checking, that is a whole another issue.

Do you mean the checking in function or in the test. In short, is there any other changes I should make to this PR?

Copy link
Member

Choose a reason for hiding this comment

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

@KubaFYI As far as my comments are concerned, no changes necessary in this PR.

I was simply alluding that we can go very far with advanced security checks and functional safety rules; but, given that the current Zephyr codebase is really not ready for compliance, I see no reason to enforce them in this particular PR.

@nashif nashif merged commit bf2dccb into zephyrproject-rtos:master Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Drivers area: UART Universal Asynchronous Receiver-Transmitter platform: Microchip SAM Microchip SAM Platform (formerly Atmel SAM)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants