<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>UART configuration and manipulation</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/56876/uart-configuration-and-manipulation</link><description>In order to interact with the UART, you need to first declare a device struct and then get a binding to the device: 
 
 
 
 Then in order to switch the UART on and off you need to do: 
 
 But I wonder if there is a way to write a function that will take</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 23 Jan 2020 10:28:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/56876/uart-configuration-and-manipulation" /><item><title>RE: UART configuration and manipulation</title><link>https://devzone.nordicsemi.com/thread/230547?ContentTypeID=1</link><pubDate>Thu, 23 Jan 2020 10:28:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9ae86105-5200-41a7-a6db-a05618de5b85</guid><dc:creator>MJD093</dc:creator><description>[quote userid="80274" url="~/f/nordic-q-a/56876/uart-configuration-and-manipulation/230525"]Ok, so what I am trying to do is read data from UARTx, using a callback. In order to do that, I need a struct device *uart.[/quote]
&lt;p&gt;This can be handled in the struct that I mentioned previously, this struct can be given all &amp;#39;struct device&amp;#39;s you need and the function can handle what one it wants.&lt;/p&gt;
[quote userid="80274" url="~/f/nordic-q-a/56876/uart-configuration-and-manipulation/230525"]Then I need to initialize that device, using the device_get_binding(&amp;quot;UART_X&amp;quot;).[/quote]
&lt;p&gt;Init your UARTs seperately, the init stage is independant of the actual manipulation of the UART. No need to have a function that inits, reads and turns them ON/OFF.&lt;/p&gt;
[quote userid="80274" url="~/f/nordic-q-a/56876/uart-configuration-and-manipulation/230525"]Then I need to attach a callback to that device to treat incoming data: uart_irq_callback_set(uart, callback)[/quote]
&lt;p&gt;ISRs set-ups should be done in your init function for the UART, this is another thing that only ever is one once and does not need to be in a recallable function. The custom struct I mentioned can also hold the &amp;#39;struct gpio_callback&amp;#39; variables.&lt;/p&gt;
[quote userid="80274" url="~/f/nordic-q-a/56876/uart-configuration-and-manipulation/230525"]Then I need to be able to switch on and off this uart. As you have suggested in your answer, you need to do this:[/quote]
&lt;p&gt;Set-up a single method to decide what UART you are looking to control, as mentioned previously a typedef such as:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;typedef enum
{
	UART0,
	UART1,
	UART2
}UARTS;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Would allow you to call a function in this manner: uart_power_on(UART0).&lt;/p&gt;
&lt;p&gt;All together you would call a function with your struct and enum as uart_power_on(&amp;amp;MyStruct, UART0).&lt;/p&gt;
&lt;p&gt;From here, your functions should be able to decypher your input, pull all the variables and information it needs from your struct and global defines and then perform the operation required.&lt;/p&gt;
&lt;p&gt;If you really really want to condense the function, you could look at then calling void uart_power_on(NRF_UARTE_Type* dev) inside this function which can call the specific version of NRF_UARTE#_NS but that&amp;#39;s a bit overkill unless you are really stuck for application memory size.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART configuration and manipulation</title><link>https://devzone.nordicsemi.com/thread/230533?ContentTypeID=1</link><pubDate>Thu, 23 Jan 2020 10:01:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b4e1d92d-d7e3-470c-9254-8cb3334889cb</guid><dc:creator>Giannis Anastasopoulos</dc:creator><description>&lt;p&gt;This is definitely possible to use and it is my last resort, but there is definitely a matching mechanism between the string and the NRF_UARTE_Type*&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART configuration and manipulation</title><link>https://devzone.nordicsemi.com/thread/230532?ContentTypeID=1</link><pubDate>Thu, 23 Jan 2020 09:57:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:07730e79-bc87-407e-8dda-f0e9243cfd8b</guid><dc:creator>MJD093</dc:creator><description>&lt;p&gt;I mean there would be other ways of letting the function know what UART you want to control. A bool flag for example or a typedef enum that allows you to write a text UART1 as an argument but use a switch case inside the function based of the enum. That way you avoid passing the NRF_UARTE_Type*&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART configuration and manipulation</title><link>https://devzone.nordicsemi.com/thread/230525?ContentTypeID=1</link><pubDate>Thu, 23 Jan 2020 09:47:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:36be5db2-49fb-419b-be84-67f6137a33a5</guid><dc:creator>Giannis Anastasopoulos</dc:creator><description>&lt;p&gt;Ok, so what I am trying to do is read data from UARTx, using a callback. In order to do that, I need a struct device *uart.&lt;br /&gt;&lt;br /&gt;Then I need to initialize that device, using the device_get_binding(&amp;quot;UART_X&amp;quot;).&lt;br /&gt;&lt;br /&gt;Then I need to attach a callback to that device to treat incoming data: uart_irq_callback_set(uart, callback)&lt;br /&gt;&lt;br /&gt;Then I need to enable the callback: uart_irq_rx_enable(uart)&lt;br /&gt;&lt;br /&gt;Then I need to be able to switch on and off this uart. As you have suggested in your answer, you need to do this:&lt;br /&gt;&lt;br /&gt;uart_on()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; NRF_UARTE1_NS-&amp;gt;ENABLE = 8;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; NRF_UARTE1_NS-&amp;gt;TASKS_STARTRX = 1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; NRF_UARTE1_NS-&amp;gt;TASKS_STARTTX = 1;&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;This function will explicitly power onUART_1. What I want is to write a function that will power off any UART(0,1,2,3).&lt;br /&gt;&lt;br /&gt;This is done by this function:&lt;br /&gt;void uart_power_on(NRF_UARTE_Type* dev)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dev-&amp;gt;ENABLE = 8;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dev-&amp;gt;TASKS_STARTRX = 1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dev-&amp;gt;TASKS_STARTTX = 1;&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;In my understanding, passing the string argument &amp;quot;UART_1&amp;quot; to device_get_binding(), somehow matches the string to NRF_UARTE1_NS. So what I am looking for is a way to get the NRF_UARTE1_NS, by using the string &amp;quot;UART_1&amp;quot; or vice versa.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART configuration and manipulation</title><link>https://devzone.nordicsemi.com/thread/230522?ContentTypeID=1</link><pubDate>Thu, 23 Jan 2020 09:36:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d8ce7b7a-4a5c-4eb8-8db6-83883a54971f</guid><dc:creator>MJD093</dc:creator><description>&lt;p&gt;I don&amp;#39;t think I understand, why exactly do you think you need to pass a type NRF_UARTE_Type*, you already showed that calling NRF_UARTE1_NS, which requires no function argument, works (and I know it does because I do it also for SPI, UART and PWM). So why are you trying to make something simple more complicated?&lt;/p&gt;
&lt;p&gt;Secondly, you do not need to pass &amp;quot;UART_1&amp;quot; to a function. You can define it as #define UART1 &amp;quot;UART_1&amp;quot; and then call UART1 in your function. This is how I do it and how you should be doing it as you should be defining your own define from the Zephyr generated list of peripheral names.&lt;/p&gt;
&lt;p&gt;For reference, pleae see /path/to/ncs/zephyr/samples/basic/blinky/main.c and refer to how they define the LED GPIO Port Controller and Pins. These can be defined in your code and called by any function.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART configuration and manipulation</title><link>https://devzone.nordicsemi.com/thread/230508?ContentTypeID=1</link><pubDate>Thu, 23 Jan 2020 09:02:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05274a7a-c3b5-4455-b3b4-4aef70554f97</guid><dc:creator>Giannis Anastasopoulos</dc:creator><description>&lt;p&gt;I don&amp;#39;t see how that will work though.&lt;br /&gt;&lt;br /&gt;I mean, in order to initialize and enable UART1, I need to pass the argument &amp;quot;UART_1&amp;quot; to the device_get_binding&lt;br /&gt;&lt;br /&gt;and I need to pass an argument of type NRF_UARTE_Type* in order to switch on and off the UART.&lt;br /&gt;&lt;br /&gt;So, I assume that there has to be a way that all those different ways of addressing the same device can be somehow unified and, for example be able to get the NRF_UARTE_Type* from a function by passing it the argument &amp;quot;UART_1&amp;quot;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART configuration and manipulation</title><link>https://devzone.nordicsemi.com/thread/230417?ContentTypeID=1</link><pubDate>Wed, 22 Jan 2020 16:24:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3c12b6d7-e588-4caf-a3f7-d060115498dd</guid><dc:creator>MJD093</dc:creator><description>&lt;p&gt;So to clarify, your just looking to pass a single arguement in which you can do things like turn off the UART?&lt;/p&gt;
&lt;p&gt;If that is the case, I would recommend looking at typedef&amp;#39;ing your own struct.&lt;/p&gt;
&lt;p&gt;Then you can pass that struct into your functions and access all the information stored in that struct. You can add bools to the struct that you can then check to see what you last set your UART to be (ON/OFF) and then preform the opposite operation on the UART (i.e if ON, switch off).&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#first define it
typedef struct My_Struct
{
	struct device 	*UART;
}MY_STRUCT;

#init it in main
MY_STRUCT MySystem;

#pass it to your functions
uart_power_on(&amp;amp;MySystem);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>