<?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>How to combine peripheral_uart example and an analog port code on my nRF5340DK?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76119/how-to-combine-peripheral_uart-example-and-an-analog-port-code-on-my-nrf5340dk</link><description>Hi everyone, I&amp;#39;m an intermediate level nRF Connect SDK IDE programmer, and I describe my issue below: 
 1.- In the link below I found an example of code for the analog port with the nRF5340DK board: 
 https://devzone.nordicsemi.com/nordic/nrf-connect</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 10 Jun 2021 13:39:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76119/how-to-combine-peripheral_uart-example-and-an-analog-port-code-on-my-nrf5340dk" /><item><title>RE: How to combine peripheral_uart example and an analog port code on my nRF5340DK?</title><link>https://devzone.nordicsemi.com/thread/314740?ContentTypeID=1</link><pubDate>Thu, 10 Jun 2021 13:39:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a2db4c2c-cf2e-4177-aa85-77e21487f83a</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Guillermo&lt;/p&gt;
&lt;p&gt;Good to hear you got the example working &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The Nordic UART service is intended for sending text strings back and forth (ASCII), and when you send raw data from the ADC it will be interpreted as ASCII characters in the Android app.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In order to display the numbers properly you have to convert it to an ASCII string, either before you pass it to the NUS service in the nRF5 device or after you receive it in the Android app.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The advantage of converting it on the nRF5 side is that you don&amp;#39;t need to change the app, but the drawback is that you will increase the amount of data that you need to send over the Bluetooth connection, which will effectively limit the amount of ADC data that you can send.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The reason the numbers are readable in the serial terminal is that the printk(..) function does the ASCII conversion for you when you use the %d parameter:&lt;br /&gt;&lt;a href="https://github.com/too1/ncs-peripheral-uart-adc/blob/master/src/main.c#L551"&gt;https://github.com/too1/ncs-peripheral-uart-adc/blob/master/src/main.c#L551&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to combine peripheral_uart example and an analog port code on my nRF5340DK?</title><link>https://devzone.nordicsemi.com/thread/314570?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2021 23:47:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:19bce701-9dcf-44ce-8be2-b9a684841c24</guid><dc:creator>guillengap</dc:creator><description>&lt;p&gt;Congratulations, your solution is elegant and worked ... before closing this discussion, just a small observation.&lt;/p&gt;
&lt;p&gt;This is what&amp;nbsp;I saw&amp;nbsp;in the serial port:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/673x240/__key/communityserver-discussions-components-files/4/0647.adc_5F00_test.png" /&gt;&lt;/p&gt;
&lt;p&gt;This is what I saw in the &amp;quot;nRF Connect&amp;quot; App: (Sometimes special characters)&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/1040x240/__key/communityserver-discussions-components-files/4/test_5F00_1.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;Sometimes number train&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/1040x240/__key/communityserver-discussions-components-files/4/test_5F00_2.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;I assume this is an Android app issue.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to combine peripheral_uart example and an analog port code on my nRF5340DK?</title><link>https://devzone.nordicsemi.com/thread/314394?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2021 08:51:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:94d156d9-61d5-4019-aff2-27e706ca394c</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Guillermo&lt;/p&gt;
&lt;p&gt;I took a shot at trying to combine the peripheral_uart and ADC examples myself, and discovered a&amp;nbsp;problem with the original ADC code:&amp;nbsp;The example in the blog has a weakness, in that it tries to read ADC values from and ISR (the k_timer callback). Because the ADC driver uses various Zephyr OS structures this is not allowed, and can lead to crashes.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To solve this a work queue can be used, which is a way to schedule a callback from an ISR that will be run in thread (main) context.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Secondly I took a crack at tying together the ADC samples with the UART service as I described in my last reply, in order to pass the ADC data over the BLE connection.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I made the code available &lt;a href="https://github.com/too1/ncs-peripheral-uart-adc"&gt;here&lt;/a&gt; if you want to have a look at it. It was developed in NCS v1.5.0, but might work in other versions as well.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Ideally I would have liked to modify the example further to remove the reliance on dynamic memory allocation, which I think would simplify the code a bit, but I will have to come back to that later.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user="guillengap"]2) the second option is try: k_fifo_put(&amp;amp;m_sample_buffer, buf);[/quote]
&lt;p&gt;In this case the first argument to the function is definitely wrong. It should point to the&amp;nbsp;fifo_uart_rx_data struct.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to combine peripheral_uart example and an analog port code on my nRF5340DK?</title><link>https://devzone.nordicsemi.com/thread/314334?ContentTypeID=1</link><pubDate>Wed, 09 Jun 2021 06:23:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c2837613-7b3c-4e68-bc18-f723e07cbac9</guid><dc:creator>guillengap</dc:creator><description>&lt;p&gt;Hi Torbjorn&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for your reply, your explanation is didactic... Yesterday I inserted the &amp;quot;main(void)&amp;quot; ADC code here and there&amp;#39;re no errors, I just need to try any of the two changes that you suggested me.&lt;/p&gt;
&lt;p&gt;In my case the ADC buffer is &amp;quot;m_sample_buffer&amp;quot;.&lt;/p&gt;
&lt;p&gt;1) If I understood you, the first option is to change this buffer instead &amp;quot;data.rx.buf&amp;quot;&lt;/p&gt;
&lt;p&gt;2) the second option is try: k_fifo_put(&amp;amp;m_sample_buffer, buf);&lt;/p&gt;
&lt;p&gt;I want to avoid this error: &amp;#39;struct uart_event&amp;#39; has no member named &amp;#39;m_sample_buffer&amp;#39;.&lt;/p&gt;
&lt;p&gt;How to solve this issue without so many lines? would try a return of &amp;quot;m_sample_buffer&amp;quot; from &amp;quot;static int16_t m_sample_buffer[BUFFER_SIZE]&amp;quot;, or declare again the member &amp;quot;m_sample_buffer&amp;quot;.&lt;/p&gt;
&lt;p&gt;Could you give me a very simple example?&lt;/p&gt;
&lt;p&gt;Kind regards&lt;/p&gt;
&lt;p&gt;Guillermo&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to combine peripheral_uart example and an analog port code on my nRF5340DK?</title><link>https://devzone.nordicsemi.com/thread/314251?ContentTypeID=1</link><pubDate>Tue, 08 Jun 2021 14:16:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:91d8bdcc-8f5a-45a7-b641-8ec435c9df39</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Guillermo&lt;/p&gt;
&lt;p&gt;The peripheral_uart example is not the easiest one to get started with I would say, but it should be possible to modify it to do what you want without too many changes (not considering the added code to run the ADC).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can start by looking at how the existing code writes to the fifo_uart_rx_data FIFO every time a byte is received over the UART, in the uart_cb(..) callback:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;case UART_RX_RDY:
	buf = CONTAINER_OF(evt-&amp;gt;data.rx.buf, struct uart_data_t, data);
	buf-&amp;gt;len += evt-&amp;gt;data.rx.len;
	buf_release = false;

	if (buf-&amp;gt;len == UART_BUF_SIZE) {
		k_fifo_put(&amp;amp;fifo_uart_rx_data, buf);
	} else if ((evt-&amp;gt;data.rx.buf[buf-&amp;gt;len - 1] == &amp;#39;\n&amp;#39;) ||
		  (evt-&amp;gt;data.rx.buf[buf-&amp;gt;len - 1] == &amp;#39;\r&amp;#39;)) {
		k_fifo_put(&amp;amp;fifo_uart_rx_data, buf);
		current_buf = evt-&amp;gt;data.rx.buf;
		buf_release = true;
		uart_rx_disable(uart);
	}

	break;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Essentially the buffer &amp;#39;buf&amp;#39; is filled up by bytes from the UART until either the newline character is received, or buf is full. Once that happens the buffer will be passed to the fifo_uart_rx_data FIFO, which will cause the ble_write_thread to continue and send the bytes stored in fifo_uart_rx_data.&lt;/p&gt;
&lt;p&gt;It should be possible to do the same in the ADC handler: Store every sampled byte in buf, and send buf to the FIFO once&amp;nbsp;it is full.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you scale the ADC buffer to the size of buf you simplify things further, since you don&amp;#39;t need to manually count the bytes before the buffer is filled (then you can probably skip buf altogether, and simply pass the ADC buffer directly to the FIFO).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I thought I would make a small example to illustrate this, but it seems I am running out of time for the day. If you have problems with this just let me know, and I will try to provide an example tomorrow.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>