This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Interfacing with MCP4725 with TWI registers

I just began working with the NRF52, using the SparkFun breakout board. I'm developing in Segger Embedded Studio. I am aware that there are drivers for this chip in the SDK, but I'm trying to use the registers directly, it seems more simple.

My code is below, and I'm expecting to see ~5V on my output, and I'm not. I have a few questions to help me troubleshoot.

1) The TWI documentation says that to write to a slave, the program must first send the start condition (NRF_TWIM0->TASKS_STARTTX = 1; in my code), and then send out data. I'm trying to follow the instructions on page 19 of the MCP4725 data sheet (https://www.sparkfun.com/datasheets/BreakoutBoards/MCP4725.pdf), which instructs the user to send a start condition, the device address, a command bye, and then 2 bytes containing the 12 bit desired output (upon receiving acknowledgments between each byte). What should I load into my TX Buffer to accomplish this? (Currently, I have the address, the control byte 0x60, and 2 bytes for the output (output & 0xF0). Do I need to load the address twice, since the NRF and the MCP each expect it? 

2) Followup, is there any special protocol for trying to transmit several consecutive bytes between a start and stop condition? What do I need to do to "wait" for each bit to be sent and the ACK to be received before sending the next byte?

2) How can I check for the ACK/NACK bit, where are they stored? Ideally, I'd like to flash an LED incase of an error (NACK).

I'm fairly lost right now, I'm sorry for not having more specific questions. I wrote this code based on Andenore's NRF Snippets (https://github.com/andenore/NordicSnippets)

I have found that I am stuck in the "while (NRF_TWIM0->EVENTS_STOPPED == 0)" loop. I put a printf right after it that never prints.

#include "nrf.h"
#include "MCP4725.h"

#define SCL_pin 10
#define SDA_pin 11
#define DEVICE_ADDRESS 0xC0

void MCP4725_setup(int scl, int sda, int address){

  NRF_TWIM0->ENABLE = TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos;

  NRF_TWIM0->PSEL.SCL = scl;
  NRF_TWIM0->PSEL.SDA = sda;

  NRF_TWIM0->ADDRESS = address;
  NRF_TWIM0->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400 << TWIM_FREQUENCY_FREQUENCY_Pos;
  NRF_TWIM0->SHORTS = 0;

  }

void MCP4725_write(int address, int output){
  
  uint8_t tx_buf[4];
  NRF_TWIM0->SHORTS = TWIM_SHORTS_LASTTX_STOP_Msk;

  tx_buf[0] = address;
  tx_buf[1] = 0x60;
  tx_buf[2] = output;
  tx_buf[3] = 0xF0;
  NRF_TWIM0->TXD.MAXCNT = sizeof(tx_buf);
  NRF_TWIM0->TXD.PTR = (uint32_t)&tx_buf[0];

  NRF_TWIM0->EVENTS_STOPPED = 0;
  NRF_TWIM0->TASKS_STARTTX = 1;
  while (NRF_TWIM0->EVENTS_STOPPED == 0);
  NRF_TWIM0->TASKS_STOP;

  }

void main(void) {
  
  MCP4725_setup(SCL_pin, SDA_pin, DEVICE_ADDRESS);

  MCP4725_write(DEVICE_ADDRESS, 255);

  while(1){
    __WFE();
  }
}

/*************************** End of file ****************************/
Related