Thanks to visit codestin.com
Credit goes to github.com

Skip to content

renesas-ra: Support changing baudrate for UART. #11680

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

Merged
merged 1 commit into from
Jun 26, 2023

Conversation

TakeoTakahashi2020
Copy link
Contributor

@TakeoTakahashi2020 TakeoTakahashi2020 commented Jun 1, 2023

This PR enables changing baudrate for UART such as UART.init(baudrate) and available other than 115200 baudrate.

  • Link fsp/src/r_sci_uart/r_sci_uart.c and call R_SCI_UART_BaudCalculate() from ra/ra_sci.c to change baudrate.
  • Make UART.init(baudrate) work.
  • Tested on EK boards with baudrate 4800/9600/19200/38400/115200/230400/460800/576000/921600/1152000/2000000.
     (except EK-RA4M1 & EK-RA4W1 does not work with 2000000)

This request and issue was reported by @iabdalkader -san and @mbedNoobNinja -san in #10943.
This change affects #10943 and #11405.

@iabdalkader
Copy link
Contributor

When I tested this before I had to disable/re-enable uart before changing the baudrate, this makes it work if UART is already initialized.

    if (R_SCI_UART_BaudCalculate(baud, 1, 5000, &baud_setting) == FSP_SUCCESS) {
        // Disable transmitter and receiver.
       uint8_t scr = sci_reg->SCR;
       sci_reg->SCR = scr & ~(0xF0);
       
       // Change baudrate    
       //...

        // Restore SCR
       sci_reg->SCR = scr;
    }

@TakeoTakahashi2020
Copy link
Contributor Author

Thank you for your review, @iabdalkader -san.

When UART is already initialized, it is re-initialized in ra_sci_init_with_flow() before calling ra_sci_set_baud() which calls R_SCI_UART_BaudCalcurate().

ra_sci_init_with_flow() {
    ...
    if (ra_sci_init_flag[idx] == 0) {
        ra_sci_fifo_init(ch);
        sci_cb[idx] = 0;
        ra_sci_init_flag[idx]++;
    } else {
        ra_sci_irq_disable(ch);
        ra_sci_module_stop(ch);
    }
    ra_sci_module_start(ch);
    ra_sci_tx_set_pin(tx_pin);
    ra_sci_rx_set_pin(rx_pin);
    ...
    ra_sci_set_baud(ch, baud);
    ...

@iabdalkader
Copy link
Contributor

When UART is already initialized, it is re-initialized in ra_sci_init_with_flow() before calling

But you don't need to re-initialize it just to change the baudrate. For example, I initialize uart, then later want to switch to a higher baudrate, I can just call this:

ra_sci_set_baud(uart_id, baudrate);

But this only works if you disable/restore the SCR register.

@TakeoTakahashi2020
Copy link
Contributor Author

But you don't need to re-initialize it just to change the baudrate.

Yes, right.

In case of when UART(id, baudrate) and UART.init(baudrate, ....) are executed, ra_sci_init_with_flow() is called internally. Therefore it is re-initialized currently.

@TakeoTakahashi2020
Copy link
Contributor Author

TakeoTakahashi2020 commented Jun 1, 2023

If you need to use ra_sci_set_baud(uart_id, baudrate) internally, disable/restore the SCR register might be needed in ra_sci_set_baud() as you mentioned. If you need it, I will try it.

@iabdalkader
Copy link
Contributor

If you need to use ra_sci_set_baud(uart_id, baudrate) internally

Yes I use machine_uart and ra_sci.c internally for bluetooth, at some point the baudrate is switched to higher one, so I call ra_sci_set_baud instead of re-initializing the uart.

I understand that in this change you made it possible to re-initialize the uart, which would allow changing the baudrate, but I still think it's more efficient to just switch the baudrate when needed if uart is already initialized.

@TakeoTakahashi2020
Copy link
Contributor Author

Understood. Your code below would be able to support both use-cases. I will try it and test tomorrow.

    if (R_SCI_UART_BaudCalculate(baud, 1, 5000, &baud_setting) == FSP_SUCCESS) {
        // Disable transmitter and receiver.
       uint8_t scr = sci_reg->SCR;
       sci_reg->SCR = scr & ~(0xF0);
       
       // Change baudrate    
       //...

        // Restore SCR
       sci_reg->SCR = scr;
    }

@TakeoTakahashi2020
Copy link
Contributor Author

Updated and tested so that ra_sci_set_baud() can be used internally.

@iabdalkader
Copy link
Contributor

iabdalkader commented Jun 2, 2023

@TakeoTakahashi2020 I tested this and it seems to be working fine, thank you! However, the flags you disable in the r_sci_uart_cfg.h header files seem to be global, correct ? Meaning MicroPython code will also ignore unused variables (which is not good) or maybe they just affect the included files. Note you don't actually need to change any headers you can just specify all those flags for a specific file, for example I tested this and it builds:

diff --git a/ports/renesas-ra/Makefile b/ports/renesas-ra/Makefile
index 51fdfb12d..b554d8bd7 100644
--- a/ports/renesas-ra/Makefile
+++ b/ports/renesas-ra/Makefile
@@ -375,6 +375,9 @@ HAL_SRC_C += $(addprefix $(HAL_DIR)/ra/fsp/src/,\
        r_sci_uart/r_sci_uart.c \
        )
 
+CFLAGS_FSP = -DSCI_UART_CFG_TX_ENABLE=0 -DSCI_UART_CFG_RX_ENABLE=0 -Wno-unused-variable -Wno-unused-function
+$(BUILD)/lib/fsp/ra/fsp/src/r_sci_uart/r_sci_uart.o: CFLAGS += $(CFLAGS_FSP)

@TakeoTakahashi2020 TakeoTakahashi2020 force-pushed the ra-development-fix-uart branch from eb0a738 to e2e2ac3 Compare June 5, 2023 08:36
@TakeoTakahashi2020
Copy link
Contributor Author

@iabdalkader -san, Thank you very much for testing this and proposal of Makefile. I agree that your proposal is better way. I have done applying it and test.

@iabdalkader
Copy link
Contributor

Looks great, thank you!

@iabdalkader
Copy link
Contributor

@TakeoTakahashi2020 Unrelated, but would you happen to know if there's a simple way to just read the TRNG ? Any example code, documentation etc.. ?

@TakeoTakahashi2020
Copy link
Contributor Author

Sorry for the delay in responding. I am not familier but could you refer to the following document?
https://renesas.github.io/fsp/group___s_c_e___p_r_o_t_e_c_t_e_d.html#ga68cdff156fa01f936f2d24db53db4869
Following docuemnt also might be helpful:
https://www.renesas.com/us/en/document/apn/secure-crypto-engine-operational-modes-application-note

@iabdalkader
Copy link
Contributor

@TakeoTakahashi2020 Thank you, I did see those before and tried to enable and use the SCE, through that function, but it didn't work, something about missing definition of TRNG, even though the series should have one. I will try again this week.

@TakeoTakahashi2020
Copy link
Contributor Author

@iabdalkader-san, If you could use e2 studio, you would be able to confirm the function works as the first step. You need to add the component and need to call Open function before calling it.

@iabdalkader
Copy link
Contributor

It's now working thank you, but there's no simple way to just get a random number from micropython, I have to link at least the following files

HAL_SRC_C += $(addprefix $(HAL_DIR)/$(CRYPTO_DIR)/,\
	public/r_sce.c \
	public/r_sce_ecc.c \
	public/r_sce_sha.c \
	public/r_sce_aes.c \
	private/r_sce_private.c \
	primitive/r_sce_p00.c \
	primitive/r_sce_p20.c \
	primitive/r_sce_p26.c \
	primitive/r_sce_p81.c \
	primitive/r_sce_p82.c \
	primitive/r_sce_p92.c \
	primitive/r_sce_p40.c \
	primitive/r_sce_func050.c \
	primitive/r_sce_func051.c \
	primitive/r_sce_func052.c \
	primitive/r_sce_func053.c \
	primitive/r_sce_func054.c \
	primitive/r_sce_func100.c \
	primitive/r_sce_func101.c \
	primitive/r_sce_func040.c \
	primitive/r_sce_func048.c \
	primitive/r_sce_func102.c \
	primitive/r_sce_func103.c \
	primitive/r_sce_subprc01.c\
	)

@TakeoTakahashi2020
Copy link
Contributor Author

Thank you @iabdalkader-san . Good to hear that it's working. It's only way because the spec is not disclosed. Hope removing unused .text by compiler & linker optimization.

)

CFLAGS_FSP = -DSCI_UART_CFG_TX_ENABLE=0 -DSCI_UART_CFG_RX_ENABLE=0 -Wno-unused-variable -Wno-unused-function
Copy link
Member

Choose a reason for hiding this comment

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

I did not see the previous version of this PR, so I'm not certain of the discussion around these settings, but why can't they go in r_sci_uart_cfg.h? That config file is included by r_sci_uart.c which is the only place that uses these config variables.

Eg at the top of r_sci_uart_cfg.h add:

#define SCI_UART_CFG_TX_ENABLE 0
#define SCI_UART_CFG_RX_ENABLE 0

@iabdalkader is there a problem doing it that way, putting the configs in the header file?

Copy link
Contributor

Choose a reason for hiding this comment

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

There were unconditional #pragmas before to disable warnings, if the file is included anywhere else it will disable warnings there, but even if it's only included in one place (for now) this patch has to be re-added by hand if the config is regenerated (it's not autogenerated by e2studio).

Copy link
Member

Choose a reason for hiding this comment

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

The -Wno...'s can be kept here in the Makefile as they are, but the two CFG options would fit better in r_scr_uart_cfg.h (and there only needs to be one of those for the whole port, not one per board).

Copy link
Contributor

Choose a reason for hiding this comment

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

(and there only needs to be one of those for the whole port, not one per board).

If the headers can be shared, then yes those defines can be moved.

@@ -0,0 +1,16 @@
#ifndef R_SCI_UART_CFG_H_
Copy link
Member

Choose a reason for hiding this comment

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

These config files look exactly the same for all boards. In the interest of trying to reduce the number of files, and to make it easier to add new boards, can this config file be removed from each board and added either in ports/renesas-ra, or ports/renesas-ra/ra, or maybe in a new subdirectory called ports/renesas-ra/fsp_cfg?

Copy link
Member

Choose a reason for hiding this comment

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

See #11846 for my proposal to simplify this configuration.

@dpgeorge
Copy link
Member

@TakeoTakahashi2020 -san, if you could please rebase this PR on the latest master commit, have just a single fsp_cfg header file for all boards, and move the SCI_UART_CFG_TX_ENABLE and SCI_UART_CFG_RX_ENABLE settings to the header file, that would be great, thank you!

@TakeoTakahashi2020
Copy link
Contributor Author

@dpgeorge -san, Okay I will update.

@TakeoTakahashi2020 TakeoTakahashi2020 force-pushed the ra-development-fix-uart branch from e2e2ac3 to a856220 Compare June 26, 2023 08:48
@TakeoTakahashi2020
Copy link
Contributor Author

@dpgeorge -san, Rebase and test has been done. I would appreciate it if you could check it.

@@ -1097,23 +1098,28 @@ static void ra_sci_fifo_init(uint32_t ch) {
ra_sci_rxfifo_set(ch, (uint8_t *)&rx_buf[idx][0], SCI_RX_BUF_SIZE);
}


Copy link
Member

Choose a reason for hiding this comment

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

Please remove this extra blank line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, sorry. Removed it.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you.

* Use R_SCI_UART_BaudCalculate() of fsp/src/r_sci_uart/r_sci_uart.c
* Support UART.init(baudrate)

Signed-off-by: Takeo Takahashi <[email protected]>
@TakeoTakahashi2020 TakeoTakahashi2020 force-pushed the ra-development-fix-uart branch from a856220 to ab8c080 Compare June 26, 2023 09:30
@dpgeorge dpgeorge merged commit 92c7532 into micropython:master Jun 26, 2023
@TakeoTakahashi2020 TakeoTakahashi2020 deleted the ra-development-fix-uart branch July 21, 2023 01:11
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.

3 participants