<?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>nRF52832 SPI tagging for multi-channel ADC?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/22690/nrf52832-spi-tagging-for-multi-channel-adc</link><description>Hi, 
 I have successfully connected an MCP3004 to the nRF52832 and can assign the ADC channel 0 output to a characteristic value. Works beautifully. 
 However, there are three other channels I want to use on the ADC for other sensors and the method</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 21 Sep 2017 16:02:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/22690/nrf52832-spi-tagging-for-multi-channel-adc" /><item><title>RE: nRF52832 SPI tagging for multi-channel ADC?</title><link>https://devzone.nordicsemi.com/thread/89209?ContentTypeID=1</link><pubDate>Thu, 21 Sep 2017 16:02:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7d121044-21a2-4105-992f-9dcb7fbc9a46</guid><dc:creator>Tim</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Is it possible to put a link to the entire code. I am trying to communicate with the MCP3008 in a similar way but it still doesn&amp;#39;t seem to work.&lt;/p&gt;
&lt;p&gt;Kind regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52832 SPI tagging for multi-channel ADC?</title><link>https://devzone.nordicsemi.com/thread/89208?ContentTypeID=1</link><pubDate>Mon, 19 Jun 2017 07:59:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bef487cd-9678-4089-a47a-ac31187ae914</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Thank you for posting your solution!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52832 SPI tagging for multi-channel ADC?</title><link>https://devzone.nordicsemi.com/thread/89207?ContentTypeID=1</link><pubDate>Sat, 17 Jun 2017 05:36:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e708ef2a-8d4e-472d-a1ac-4f2914e8adb9</guid><dc:creator>cswanson420</dc:creator><description>&lt;p&gt;Thanks for the response. I was able to get a solution that I will put below for others to maybe use if they have a similar problem.&lt;/p&gt;
&lt;p&gt;I am using SPIM and Easy-DMA. I have some instability in the read ADC values and I am not sure if that&amp;#39;s my poor electronics or something else. I was, however, able to use an oscilloscope on the four SPI channels and witness proper SPI correspondence between master and slave. Thus, this is fabulous.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/DS1Z_5F00_QuickPrint5.png" alt="image description" /&gt;&lt;/p&gt;
&lt;p&gt;I am using SDK 11, not sure why actually, but it&amp;#39;s working for me, so I&amp;#39;ll stick to it.&lt;/p&gt;
&lt;p&gt;The design I am using makes an array of uint8_t elements that is the (number of channels being used * length of channels). I have two functions (rx_adc_buffer_channel and rx_adc_buffer_address) that can give the array offset given a channel number and its inverse function: given an address return the channel number. This allows me to pass the proper channel rx_buffer to the timer event for the SPI MOSI event and then get the channel after the SPI transfer event or MOSI data.&lt;/p&gt;
&lt;p&gt;I found I couldn&amp;#39;t make two writes back-to-back in one timer interval, but this could just be confusion on my part and I should revisit it. However, I don&amp;#39;t require an aggressive sample rate so I interleave the channels during ADC read events and this is working.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m just going to paste the code fragments here:
(Formatting is terrible; sorry)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#define ADC_CHANNEL_COUNT               2
#define ADC_RX_BUFFER_LENGTH            3

static uint8_t*                         rx_adc_channels;
static uint8_t                          m_channel_toggle;
static uint16_t                         adc_char_handle_arr[ADC_CHANNEL_COUNT];


static uint8_t* rx_adc_buffer_address(uint8_t channel_id) {
    int offset;
    uint8_t* return_addr;

    offset = sizeof(uint8_t);
    offset *= ADC_RX_BUFFER_LENGTH;
    offset *= channel_id;

    return_addr = rx_adc_channels + offset;

    return return_addr;
}


static uint8_t rx_adc_buffer_channel(uint8_t* address) {
    uint8_t channel;

    channel = (address - rx_adc_channels) / ADC_RX_BUFFER_LENGTH;

    return channel;
}


/* ADC MCP3004 */

static void read_adc(uint8_t* counter) {
    uint8_t tx_length = 3;
    uint8_t rx_length = ADC_RX_BUFFER_LENGTH;
    uint8_t channel;
    uint8_t channel_bitmap;

    m_channel_toggle++;

    channel = m_channel_toggle % ADC_CHANNEL_COUNT;
   /* Sorry I don&amp;#39;t know the escape characters for less than so bitshift left is two lt signs.*/
    channel_bitmap = 128 | (channel (bitshift left) 4);

    /* Set SPI Channel */
    m_tx_buf[0] = 1;
    m_tx_buf[1] = channel_bitmap;
    m_tx_buf[2] = 0;

    APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, m_tx_buf, tx_length,
        rx_adc_buffer_address(channel), rx_length));
}


void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
{
    int _size;
    uint8_t _channel;
    uint16_t _handle;

    if (p_event-&amp;gt;data.done.rx_length &amp;gt;= 3 ) {

        _channel = rx_adc_buffer_channel(p_event-&amp;gt;data.done.p_rx_buffer);
        _handle = adc_char_handle_arr[_channel];

        rx_adc_buffer_channel(p_event-&amp;gt;data.done.p_rx_buffer);
        m_adc_channel0 = (*(p_event-&amp;gt;data.done.p_rx_buffer + 2) * 256 ) +
            *(p_event-&amp;gt;data.done.p_rx_buffer + 1);

        if (m_conn_handle != BLE_CONN_HANDLE_INVALID) {
            counter_attr_value.len = 0x02;
            counter_attr_value.offset = 0x00;
            counter_attr_value.p_value = (uint8_t*)&amp;amp;m_adc_channel0;
            sd_ble_gatts_value_set(m_conn_handle, _handle, &amp;amp;counter_attr_value);
         }
    }
}


static void rx_adc_channels_init(void) {
    int size;

    size = sizeof(uint8_t) * ADC_CHANNEL_COUNT * ADC_RX_BUFFER_LENGTH;
    rx_adc_channels = (uint8_t*)malloc(size);
    memset(rx_adc_channels, 0, size);
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52832 SPI tagging for multi-channel ADC?</title><link>https://devzone.nordicsemi.com/thread/89206?ContentTypeID=1</link><pubDate>Mon, 12 Jun 2017 12:34:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45f053f0-7b86-417a-8b35-71989f31e15b</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Are you using the SPI master driver in the SDK? Your solution of using multiple rx buffers might work, but you will need some way to know which buffer contain valid data. If you are OK with reading &amp;quot;old data&amp;quot;, you can setup a rx buffer for each channel and update the characteristics from all channels on each SPI callback, but this will generate additional work for the CPU.&lt;/p&gt;
&lt;p&gt;An alternative is to use the &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v13.0.0/group__nrf__drv__spi.html#ga2319de449c320ce104d8f3b40759c64c"&gt;&lt;code&gt;p_context&lt;/code&gt; pointer&lt;/a&gt; to signalize to the event handler which channel is in use. Unfortunately this pointer is set on the interface initialization, and not on the execution of a transfer. You will then have to uninit and initialize the SPI interface for each transfer. In SDK 13 you can also use the SPI transaction manager, which can take a pointer, &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v13.0.0/structnrf__spi__mngr__transaction__t.html"&gt;p_user_data&lt;/a&gt; as argument, and pass this to the event handler. This takes advantage of the p_context pointer in the SPI master driver, and will automatically uninit and init the SPI interface if the parameters change.&lt;/p&gt;
&lt;p&gt;Are you starting multiple transaction simultaneously, or use EasyDMA with the SPI peripheral? If not, you can simply set a flag indicating which channel is read and check this in the SPI event handler.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>