Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "nrf_drv_spi.h"
- #include "app_util_platform.h"
- #include "nrf_gpio.h"
- #include "nrf_delay.h"
- #include "boards.h"
- #include "app_error.h"
- #include <string.h>
- #include "nrf_log.h"
- #include "nrf_log_ctrl.h"
- #include "nrf_log_default_backends.h"
- #define SPI_INSTANCE 0 /**< SPI instance index. */
- static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
- static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
- uint8_t m_rx_buf[2]; // Receiving buffer; cleared and read upon each command
- /**
- * @brief SPI user event handler.
- * @param event
- */
- void spi_event_handler(nrf_drv_spi_evt_t const *p_event, void *p_context){
- spi_xfer_done = true;
- //NRF_LOG_INFO("Transfer completed."); //Our RX_BUF is not loaded until after __WFE();
- }
- uint8_t intanRead(uint8_t addr )
- {
- //reads register addr
- //result is returned in 2 commands
- //use addr = 99 to send dummy command (i.e. tx_buf = {0, 0})
- memset(m_rx_buf, 0, 2); //Clear RX BUF
- spi_xfer_done = false; //Clear xfer flag
- uint8_t m_tx_buf[2]; //Building xfer command
- m_tx_buf[0] =((0xc0) | addr)*(addr!=99); //Use addr == 99 for dummy command (i.e. tx_buf = {0, 0})
- m_tx_buf[1] = 0;
- APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2)); //Execute xfer command
- while (!spi_xfer_done)
- {
- __WFE(); //Our RX_BUF is not loaded until after __WFE();
- }
- //NRF_LOG_INFO("m_tx_buf:");
- //NRF_LOG_HEXDUMP_INFO(m_tx_buf,2); //debug tx buf
- //NRF_LOG_INFO("m_rx_buf:");
- //NRF_LOG_HEXDUMP_INFO(m_rx_buf,2); //debug rx buf
- //NRF_LOG_FLUSH();
- //nrf_delay_ms(20);
- return m_rx_buf[1]; //Load our receiving buffer to result
- }
- uint8_t intanWrite(uint8_t addr, uint8_t data)
- {
- //writes data to register addr
- //result is echoed in 2 commands
- //returns lower byte, depends on command sent 2 CS pulses ago
- memset(m_rx_buf, 0, 2); //Clear RX BUF
- spi_xfer_done = false; //Clear xfer flag
- uint8_t m_tx_buf[2]; //Building xfer command
- m_tx_buf[0] =((0x80) | addr);
- m_tx_buf[1] = data;
- APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2)); //Execute xfer command
- while (!spi_xfer_done)
- {
- __WFE(); //Our RX_BUF is not loaded until after __WFE();
- }
- //NRF_LOG_INFO("m_tx_buf:");
- //NRF_LOG_HEXDUMP_INFO(m_tx_buf,2); //debug tx buf
- //NRF_LOG_INFO("m_rx_buf:");
- //NRF_LOG_HEXDUMP_INFO(m_rx_buf,2); //debug rx buf
- //NRF_LOG_FLUSH();
- //nrf_delay_ms(20);
- return m_rx_buf[1]; //Load our receiving buffer to result
- }
- void intanSetupRegisters()
- {
- //uint8_t echoResult[21]; //18 registers, 1 duplicate, delay 2
- uint8_t i; //index/addr
- uint8_t maxAddr = 17; //Highest address to write to
- uint8_t fastSettle = 0xFE; //D[5] must be set 1, then set again to 0
- uint8_t data[] = {
- //Register 0: ADC config and amplifier fast settle
- //D[5] must be set 1, then set again to 0
- //D[4] must wait 100 mu s after setting for valid ADC
- 0xDE,
- //Register 1: Supply sensor (off) and ADC buffer bias current ( = 32)
- 0x20,
- //Register 2: MUX Bias current (= 40)
- 0x28,
- //Register 3: MUX load, temp sensor, aux dig output (ALL zero)
- 0x00,
- //Register 4: ADC output format and DSP offset removal (ALL zero)
- 0x00,
- //Register 5: Impedance check control (zero)
- 0x00,
- //Register 6: Impedance check DAC (Zero)
- 0x00,
- //Register 7: Impedance check amp select (zero)
- 0x00,
- //Registers 8 - 13: amplifier bandidth select
- //00001000
- //00000000
- //00000100
- //00000000
- //00010000
- //01111100
- 0x08, 0x00, 0x04, 0x00, 0x10, 0x7C,
- //Register 14 - 17: Amplifier individual power
- //11111111
- //11111111
- //11111111
- //11111111
- 0xFF, 0xFF, 0xFF, 0xFF
- };
- /**echoResult[0] = **/intanWrite(0, fastSettle);
- for(i=0; i<= maxAddr; i++){
- /*echoResult[i+1] =*/ intanWrite(i, data[i]);
- }
- // echoResult[19] = intanRead(99);
- // echoResult[20] = intanRead(99);
- //
- //NRF_LOG_HEXDUMP_INFO(echoResult,sizeof(echoResult));
- }
- void intanCalibrate()
- {
- //reads register addr
- //result is returned in 2 commands
- //use addr = 99 to send dummy command (i.e. tx_buf = {0, 0})
- memset(m_rx_buf, 0, 2); //Clear RX BUF
- spi_xfer_done = false; //Clear xfer flag
- uint8_t m_tx_buf[2]; //Building xfer command
- m_tx_buf[0] = 0x55; //Calbrate command 0b01010101 0b00000000
- m_tx_buf[1] = 0;
- APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2)); //Execute xfer command
- while (!spi_xfer_done)
- {
- __WFE(); //Our RX_BUF is not loaded until after __WFE();
- }
- //NRF_LOG_INFO("m_tx_buf:");
- //NRF_LOG_HEXDUMP_INFO(m_tx_buf,2); //debug tx buf
- //NRF_LOG_INFO("m_rx_buf:");
- //NRF_LOG_HEXDUMP_INFO(m_rx_buf,2); //debug rx buf
- //NRF_LOG_FLUSH();
- //nrf_delay_ms(20);
- uint8_t i;
- for (i=0;i<9;i++){
- intanRead(99); //send 9 dummy commands
- }
- }
- void intanInit()
- {
- intanSetupRegisters();
- intanCalibrate();
- }
- uint16_t intanConvert(uint8_t chan)
- {
- //converts info at channel "chan" from ADC
- //result is returned in 2 commands
- //returns 16 bit unsigned integer (check 2s complement settings for format)
- //can use chan = 63 for successive channels
- memset(m_rx_buf, 0, 2); //Clear RX BUF
- spi_xfer_done = false; //Clear xfer flag
- uint8_t m_tx_buf[2]; //Building xfer command
- m_tx_buf[0] =((0x00) | chan) * (chan != 99); //Use addr == 99 for dummy command (i.e. tx_buf = {0, 0})
- m_tx_buf[1] = 0;
- APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2)); //Execute xfer command
- while (!spi_xfer_done)
- {
- __WFE(); //Our RX_BUF is not loaded until after __WFE();
- }
- //NRF_LOG_INFO("m_tx_buf:");
- //NRF_LOG_HEXDUMP_INFO(m_tx_buf,2); //debug tx buf
- //NRF_LOG_INFO("m_rx_buf:");
- //NRF_LOG_HEXDUMP_INFO(m_rx_buf,2); //debug rx buf
- //NRF_LOG_FLUSH();
- //nrf_delay_ms(20);
- return (m_rx_buf[0] << 8) | m_rx_buf[1]; //Load our receiving buffer to result
- }
- int main(void){
- bsp_board_init(BSP_INIT_LEDS);
- APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
- NRF_LOG_DEFAULT_BACKENDS_INIT();
- nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
- spi_config.ss_pin = SPI_SS_PIN;
- spi_config.miso_pin = SPI_MISO_PIN;
- spi_config.mosi_pin = SPI_MOSI_PIN;
- spi_config.sck_pin = SPI_SCK_PIN;
- spi_config.frequency = NRF_DRV_SPI_FREQ_125K; //Low frequency for clean OSC signals
- APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
- NRF_LOG_INFO("SPI example started.");
- intanInit();
- // uint8_t ourResult[4]; //For reading INTAN, we need 7 commands (5 read + 2 dummy)
- uint16_t ourData[18]; //16 channels + 2 delay
- uint8_t initialChan = 8; //initial channel to sample
- int j; //index
- uint8_t chan; //channel to sample
- // uint16_t temp;
- while (1)
- {
- // ourResult[0] = intanRead(40); //read registers 40-44, send 2 dummy commands, store in ourResult
- // ourResult[1] = intanRead(41);
- // ourResult[2] = intanRead(42);
- // ourResult[3] = intanRead(43);
- // ourResult[4] = intanRead(44);
- // ourResult[5] = intanRead(99);
- // ourResult[6] = intanRead(99);
- // //write register 1: ADC buffer bias = 32
- // ourResult[0] = intanWrite(1, 32);
- // //write register 2: MUX bias = 40
- // ourResult[1] = intanWrite(2, 40);
- // //send dummy command 1
- // ourResult[2] = intanRead(99);
- // //send dummy command 2
- // ourResult[3] = intanRead(99);
- memset(ourData, 0, sizeof(ourData)/2);
- for (j=0; j<sizeof(ourData)/2;j++)
- {
- if (j==0){ //if we are on first sample we point to first channel
- chan = initialChan;
- } else {
- chan = 63;
- }
- if ((j==sizeof(ourData)/2 - 1)|(j==sizeof(ourData)/2 - 2)) {
- //if we are on last 2 we will send dummy command
- //temp = intanRead(99);
- //ourData[j] = temp;
- ourData[j] = intanConvert(99);
- } else {
- ourData[j] = intanConvert(chan);
- }
- }
- bsp_board_led_invert(BSP_BOARD_LED_0);
- // NRF_LOG_INFO("ourResult:");
- // NRF_LOG_HEXDUMP_INFO(ourResult,sizeof(ourResult)); //Debug our array
- //NRF_LOG_INFO("ourData:");
- //NRF_LOG_HEXDUMP_INFO(ourData,sizeof(ourData)/2);
- //NRF_LOG_INFO("Decimal:");
- // for (j=0; j<sizeof(ourData)/2;j++)
- // {
- // NRF_LOG_INFO("%d: %d",j+initialChan, ourData[j]);
- // }
- NRF_LOG_INFO("%d: %d",10+initialChan, ourData[10])
- //NRF_LOG_INFO("//////////////////////////");
- NRF_LOG_FLUSH();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement