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

driver: uart-stm32: add line_ctrl_set/get support #8306

Closed

Conversation

qianfan-Zhao
Copy link
Collaborator

support change bandrate and hardware flow control at runtime

Signed-off-by: qianfan Zhao [email protected]

@codecov-io
Copy link

codecov-io commented Jun 11, 2018

Codecov Report

Merging #8306 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #8306   +/-   ##
======================================
  Coverage    53.1%   53.1%           
======================================
  Files         218     218           
  Lines       26883   26883           
  Branches     5952    5952           
======================================
  Hits        14277   14277           
  Misses      10172   10172           
  Partials     2434    2434

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7aa58ff...b6ecfc7. Read the comment docs.

@qianfan-Zhao qianfan-Zhao force-pushed the stm32-line-ctrl branch 2 times, most recently from 45ffb37 to d27d8c8 Compare June 11, 2018 06:55
@@ -19,6 +19,8 @@ struct uart_stm32_config {
struct stm32_pclken pclken;
/* Baud rate */
u32_t baud_rate;
/* HW flow control */
u32_t rts_cts:1;
Copy link
Member

Choose a reason for hiding this comment

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

"config" struct is device startup configuration.
You should not use it to store information that will be modified at run time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, it's my mistake and actually 'config' is const and can't changed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I want move baud_rate from uart_stm32_config to uart_stm32_data. Because uart_stm32_data can be changed at runtime. what's your opinion?

Copy link
Member

Choose a reason for hiding this comment

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

It should be ok to do that.
But what about setting/reading directly in registers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

change baud rate need disable usart first and then calc clock rate and then set registers. thats was almost did in stm32_init.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I had updated this pr setting directly in registers now. please review again

} while (0)

switch (ctrl) {
case LINE_CTRL_BAUD_RATE:
Copy link
Member

Choose a reason for hiding this comment

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

You should not store information in baud_rate which is used to hold start-up configuration.
I propose you update registers directly.

@@ -314,6 +293,15 @@ static int uart_stm32_init(struct device *dev)
LL_USART_PARITY_NONE,
LL_USART_STOPBITS_1);

/* HW flow control */
if (config->rts_cts) {
Copy link
Member

Choose a reason for hiding this comment

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

You should provide a start up configuration otherwise behavior is undefined here.

@erwango erwango added platform: STM32 ST Micro STM32 area: UART Universal Asynchronous Receiver-Transmitter labels Jun 11, 2018
@qianfan-Zhao qianfan-Zhao force-pushed the stm32-line-ctrl branch 5 times, most recently from 2493699 to e40efad Compare June 12, 2018 05:57

switch (ctrl) {
case LINE_CTRL_BAUD_RATE:
change_if_not_equal(baud_rate, val);
Copy link
Member

Choose a reason for hiding this comment

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

If we're able to update directly registers, why do you need to store information in driver data?
Using registers, you can save some memory.

Copy link
Member

@erwango erwango left a comment

Choose a reason for hiding this comment

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

Not sure we need to use data fields to store information, since we can use device registers.

@qianfan-Zhao
Copy link
Collaborator Author

qianfan-Zhao commented Jun 12, 2018

@erwango

Not sure we need to use data fields to store information, since we can use device registers.

No, you should not do that. We must using an variable to store the baud_rate setted before.
the baud_rate we read using LL_USART_GetBaudRate maybe not equal the baud_rate setted using LL_USART_SetBaudRate because the BRR is inaccurate for some specify baud rate.

eg: I had set uart's baud rate to 115200, but the baudrate readed is 115107.

332             baud_rate = LL_USART_GetBaudRate(UartInstance, clock_rate, LL_USART_OVERSAMPLING_16);

(gdb) p baud_rate 
$1 = 115107

return -ENOTSUP;
}

return 0;
Copy link
Member

Choose a reason for hiding this comment

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

This line could not be accessed, code coverage tools will complain.
I propose to remove default and return -ENOSUP here.

}; \
\
static struct uart_stm32_data uart_stm32_data_##name = { \
.baud_rate = CONFIG_UART_STM32_##name##_BAUD_RATE, \
.rts_cts = 0, \
Copy link
Member

Choose a reason for hiding this comment

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

Please provide a constant here so one can known what default value is actually.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sorry can you explain more clear?

Copy link
Member

Choose a reason for hiding this comment

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

Replace 0 with something like RTS_HW_FC_DISABLED

static inline void uart_stm32_set_baud_rate(struct device *dev,
u32_t clock_rate, u32_t baud_rate)
{
#if defined(CONFIG_SOC_SERIES_STM32L0X) || defined(CONFIG_SOC_SERIES_STM32L4X)
Copy link
Member

Choose a reason for hiding this comment

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

Please take care to rework this if #8359 is merged first

@erwango
Copy link
Member

erwango commented Jun 18, 2018

No, you should not do that. We must using an variable to store the baud_rate setted before.

Ok, let's keep it that way then

@qianfan-Zhao
Copy link
Collaborator Author

@erwango Done

@erwango
Copy link
Member

erwango commented Jun 29, 2018

recheck

@erwango
Copy link
Member

erwango commented Jun 29, 2018

@qianfan-Zhao , can you fix CI issue, so we can continue the review?

-:41: ERROR:SPACING: need consistent spacing around '*' (ctx:WxV)
#41: FILE: drivers/serial/uart_stm32.c:264:
+	USART_TypeDef *UartInstance = UART_STRUCT(dev);

@qianfan-Zhao
Copy link
Collaborator Author

@erwango But I think the best way is no space, there shouldn't add a space between '*' and point. eg:

int p; / good /
int * p; /
bad */

Can we ignore this error?

@erwango
Copy link
Member

erwango commented Jun 29, 2018

@qianfan-Zhao

Can we ignore this error?

I don't think so. It is strange that this "error" has not been detected before.
Was there any recent change in checkpatch.pl?

@qianfan-Zhao
Copy link
Collaborator Author

@erwango

This is the lastly commit for checkpatch.pl, seems that scripts haven't changed this days.

commit 65f0c67906941050171511c2e4c67a1e939ddd2d
Author: Anas Nashif <[email protected]>
Date:   Tue May 8 10:19:30 2018 -0500

    checkpatch: downgrade COMPLEX_MACRO to a warning
    
    This is generating lots of false positives and we keep overriding them
    manually, so downgrade and mark it as a warning.
    
    Signed-off-by: Anas Nashif <[email protected]>

@erwango
Copy link
Member

erwango commented Jun 29, 2018

This is really strange since CAN driver introduction also introduced some CAN_TypeDef *can = cfg->can; recently and no one complained

Copy link
Member

@erwango erwango left a comment

Choose a reason for hiding this comment

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

@qianfan-Zhao

Is that mandatory to store RTS_CTS in uart_stm32_data_XXX?
Can't we set and get info directly in registers?

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_callback_t user_cb;
#endif

Copy link
Member

Choose a reason for hiding this comment

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

Remove this extra line

@qianfan-Zhao
Copy link
Collaborator Author

@erwango I had updated this pr based on your requested. Get RTS_CTS informations direct from register.

And has another issue fix:
I had noticed that not all serials(UART/USART) are supported hard flow control feature, and stm32cube provide a function to detect that, so I had update uart_stm32_line_ctrl_set in this way:

static int uart_stm32_line_ctrl_set(struct device *dev,
				    u32_t ctrl, u32_t val)
{
	USART_TypeDef *UartInstance = UART_STRUCT(dev);
	struct uart_stm32_data *data = DEV_DATA(dev);
	int result = 0;

	LL_USART_Disable(UartInstance);

	switch (ctrl) {
	case LINE_CTRL_BAUD_RATE:
		uart_stm32_set_baud_rate(dev, data->clock_rate, val);
		data->baud_rate = val;
		break;
	case LINE_CTRL_RTS:
		if (!IS_UART_HWFLOW_INSTANCE(UartInstance)) {
			result = -EINVAL;
			goto done;
		} else if (val) {
			LL_USART_SetHWFlowCtrl(UartInstance,
					       LL_USART_HWCONTROL_RTS_CTS);
		} else {
			LL_USART_SetHWFlowCtrl(UartInstance,
					       LL_USART_HWCONTROL_NONE);
		}
		break;
	default:
		result = -ENOTSUP;
		break;
	}

done:
	LL_USART_Enable(UartInstance);
	return result;
}

Please review this section carefully. I don't know if return -EINVAL if an uart instance doesn't support hardware flow control is OK.

@qianfan-Zhao
Copy link
Collaborator Author

@erwango Could you please also review PR #8307 ?

Copy link
Member

@erwango erwango left a comment

Choose a reason for hiding this comment

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

Minor comments, otherwise looks good

#else
uart_stm32_usart_set_baud_rate(dev, clock_rate, baud_rate);
#endif
uart_stm32_set_baud_rate(dev, data->clock_rate, baud_rate);
Copy link
Member

Choose a reason for hiding this comment

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

Why not using "data->baud_rate" directly there as well?

break;
case LINE_CTRL_RTS:
if (!IS_UART_HWFLOW_INSTANCE(UartInstance)) {
result = -EINVAL;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe ENOTSUP would fit better here

}
break;
default:
result = -ENOTSUP;
Copy link
Member

Choose a reason for hiding this comment

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

EINVAL at this point?

@qianfan-Zhao
Copy link
Collaborator Author

@erwango recheck

@erwango
Copy link
Member

erwango commented Nov 8, 2018

@qianfan-Zhao ,

Can you fix CI and we're good:

-:41: ERROR:SPACING: need consistent spacing around '*' (ctx:WxV)
#41: FILE: drivers/serial/uart_stm32.c:267:

  • USART_TypeDef *UartInstance = UART_STRUCT(dev);

@erwango
Copy link
Member

erwango commented Nov 9, 2018

Well, I'm unsure what is the issue here.
Checkpatch doesn't complain on my side and seems good to uncrustify as well.

@qianfan-Zhao qianfan-Zhao reopened this Nov 9, 2018
@qianfan-Zhao
Copy link
Collaborator Author

@erwango Or just fix this issue to let Shippable happy?

@erwango
Copy link
Member

erwango commented Nov 9, 2018

@qianfan-Zhao:

@erwango Or just fix this issue to let Shippable happy?

Sure, if you manage to

support change bandrate and hardware flow control at runtime

Signed-off-by: qianfan Zhao <[email protected]>
@erwango
Copy link
Member

erwango commented Nov 12, 2018

@qianfan-Zhao , this PR will now conflicts with #11185

Do you plan to rebase this PR on top of #11185 when merged?
Then, we'll try to get it merged much faster

@qianfan-Zhao
Copy link
Collaborator Author

@erwango sure, i will

@erwango
Copy link
Member

erwango commented Jan 2, 2019

#11185 merged

@erwango
Copy link
Member

erwango commented Feb 6, 2019

@qianfan-Zhao , closing this as #11185 merged.

@erwango erwango closed this Feb 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: UART Universal Asynchronous Receiver-Transmitter platform: STM32 ST Micro STM32
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants