I'm trying to read the values from the DS1307 RTC through TWI. I've established a connection with the device but am not sure I'm using the nrf_drv_twi_rx() function correctly. When the log is viewed it prints that there was a device successfully detected at the given address and that 7 bytes are received over TWI to the rx_data array which is an array with 7 uint8_t.
From what I read about the nrf_drv_twi_rx() function I thought that passing it an initialized twi instance, device address, buffer to hold data, and amount of data to receive that it would automatically read from the correct device registry locations. The problem is that the outputs I get are not related to the clock time and vary from 0 to 80. I'm not sure why these values are not the clock values. From the DS1307 datasheet the memory addresses are listed as follows:
I'm not sure what the 00H-3FH address notion means as I would expect a binary or hex address for a memory location, so any help in understanding what these addresses mean would be helpful as well. The following paragraph from the datasheet describes how the data is to be accessed:
"The time and calendar information is obtained by reading the appropriate register bytes. The contents of the time and calendar registers are in the BCD format. Bit 7 of register 0 is the clock halt (CH) bit. When this bit is set to a 1, the oscillator is disabled. When cleared to a 0, the oscillator is enabled."
I attached the full datasheet if you would like any extra information.
Another thing I noticed while debugging is that the values received to rx_data change every time the device is reset and occasionally hold the expected correct month/day/year etc. data but usually do not. This makes me think there is an inconsistency with how the data is being accessed from the DS1307 addresses and that different information from the registry is being accessed each time the program runs for some reason, but I don't know why.
Here is my code that I've developed so far based on the twi_scanner_pca_10040 example in SES.
#include <stdio.h> #include "boards.h" #include "app_util_platform.h" #include "app_error.h" #include "nrf_drv_twi.h" #include "nrf_log.h" #include "nrf_log_ctrl.h" #include "nrf_log_default_backends.h" /* TWI instance ID. */ #define TWI_INSTANCE_ID 0 static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID); void twi_init(void) //configure twi connection { ret_code_t err_code; const nrf_drv_twi_config_t twi_config = { .scl = 22, //configure pin 22 to scl .sda = 23, //configure pin 23 to sda .frequency = NRF_DRV_TWI_FREQ_100K, //nrf freq 100k 250k or 400k .interrupt_priority = APP_IRQ_PRIORITY_HIGH, //if using a soft device this has to be changed accordingly .clear_bus_init = false }; err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL); //can pass this an interrupt handler APP_ERROR_CHECK(err_code); nrf_drv_twi_enable(&m_twi); } int main(void) { ret_code_t err_code; uint8_t address = 0x68; //address of DS1307 uint8_t rx_data[7] = {0}; //buffer for rx twi info. array of 7 uint8_t to hold 7 bytes received over twi APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); NRF_LOG_DEFAULT_BACKENDS_INIT(); NRF_LOG_INFO("Application Started"); NRF_LOG_FLUSH(); twi_init(); //read data from the slave register to show successful communication err_code = nrf_drv_twi_rx(&m_twi, address, &rx_data, sizeof(rx_data)); //receive data from the device registry and send to rx_data if(err_code == NRF_SUCCESS) { NRF_LOG_INFO("Successfully detected a device at address: 0x%x", address); for(int i = 0; i < 8; ++i) //display the data obtained from the rx { NRF_LOG_INFO("Data %d obtained from registry: %x", (i+1), rx_data[i]); } } else { NRF_LOG_INFO("No device detected at address 0x%x", address); } NRF_LOG_FLUSH(); while (true) { /* Empty loop. */ } }
Datasheet for DS1307 RTC: /cfs-file/__key/communityserver-discussions-components-files/4/DS1307_5F00_Datasheet.pdf