I am having issue with interfacing the ds1307 RTC with S32K144. I will post the code, the first code is main.c and second is the driver. when i call setdate everything works fine but after thsi ebery function call goes to status busy that is I2B bus does not return to idle state:
/********** Includes **********/
#include "sdk_project_config.h"
#include <stdio.h>
#include <string.h>
#include <rtc.h>
#include <uart_bms.h>
#include <ds1307_rtc.h>
char timeDate[] = "29-02-2024 5:39:00";
int main(void)
{
cmd_error State = {0};
// Clock initialization
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
CLOCK_DRV_Init(&clockMan1_InitConfig0);
// Pin initialization
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS0, g_pin_mux_InitConfigArr0);
// UART initialization
UARTInit();
// RTC initialization
DS1307_Init();
DS1307_SetDate(3);
DS1307_SetMonth(4);
DS1307_SetYear(2024);
DS1307_SetHour(11);
DS1307_SetMinute(22);
DS1307_SetSecond(10);
uint8_t date = DS1307_GetDate();
uint8_t month = DS1307_GetMonth();
uint16_t year = DS1307_GetYear();
uint8_t hour = DS1307_GetHour();
uint8_t minute = DS1307_GetMinute();
uint8_t second = DS1307_GetSecond();
// char dateStr[11]; // Date format DD/MM/YYYY
// sprintf(dateStr, "%02d/%02d/%04d", date, month, year);
// LPUART_DRV_SendDataBlocking(0, (uint8_t*)dateStr, sizeof(dateStr), 1000);
char timeStr[9]; // Time format HH:MM:SS
sprintf(timeStr, "%02d:%02d:%02d", hour, minute, second);
LPUART_DRV_SendDataBlocking(0, (uint8_t*)timeStr, sizeof(timeStr), 1000);
while (1)
{
// Receive from the UART
UARTReceive();
// Send the command from the UART
SendCmd(&State);
memset(buffer, 0, BUFFER_SIZE);
bufferIdx = 0;
}
return 0;
}
/*
* ds1307_rtc.c
*
* Created on: Mar 1, 2024
* Author: kaush
*/
#include "sdk_project_config.h"
#include <stdio.h>
#include <string.h>
#include <rtc.h>
#include <ds1307_rtc.h>
#include <uart_bms.h>
lpi2c_master_state_t ds1307Lpi2cMasterState;
lpi2c_slave_state_t ds1307Lpi2cSlaveState;
void DS1307_Init() {
LPI2C_DRV_MasterInit(LPI2C_INSTANCE, &lpi2c0_MasterConfig0, &ds1307Lpi2cMasterState);
// DS1307_SetClockHalt(0);
}
void DS1307_SetClockHalt(uint8_t halt) {
uint8_t ch = (halt ? 1 << 7 : 0);
uint8_t secondsReg = DS1307_GetRegByte(DS1307_REG_SECOND) & 0x7F;
DS1307_SetRegByte(DS1307_REG_SECOND, ch | secondsReg);
}
uint8_t DS1307_GetClockHalt(void) {
return (DS1307_GetRegByte(DS1307_REG_SECOND) & 0x80) >> 7;
}
void DS1307_SetRegByte(uint8_t regAddr, uint8_t val) {
uint8_t bytes[2] = { regAddr, val };
LPI2C_DRV_MasterSendDataBlocking(LPI2C_INSTANCE, bytes, 2, true, 1000);
}
uint8_t DS1307_GetRegByte(uint8_t regAddr) {
uint8_t val = 0;
LPI2C_DRV_MasterSendDataBlocking(LPI2C_INSTANCE, ®Addr, 1, true, 1000);
LPI2C_DRV_MasterReceiveDataBlocking(LPI2C_INSTANCE, &val, 1, true, 1000);
return val;
}
uint8_t DS1307_GetDayOfWeek(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_DOW));
}
uint8_t DS1307_GetDate(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_DATE));
}
uint8_t DS1307_GetMonth(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_MONTH));
}
uint16_t DS1307_GetYear(void) {
uint16_t cen = DS1307_GetRegByte(DS1307_REG_CENT) * 100;
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_YEAR)) + cen;
}
uint8_t DS1307_GetHour(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_HOUR) & 0x3f);
}
uint8_t DS1307_GetMinute(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_MINUTE));
}
uint8_t DS1307_GetSecond(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_SECOND) & 0x7f);
}
void DS1307_SetDayOfWeek(uint8_t dayOfWeek) {
DS1307_SetRegByte(DS1307_REG_DOW, DS1307_EncodeBCD(dayOfWeek));
}
void DS1307_SetDate(uint8_t date) {
DS1307_SetRegByte(DS1307_REG_DATE, DS1307_EncodeBCD(date));
}
void DS1307_SetMonth(uint8_t month) {
DS1307_SetRegByte(DS1307_REG_MONTH, DS1307_EncodeBCD(month));
}
void DS1307_SetYear(uint16_t year) {
DS1307_SetRegByte(DS1307_REG_CENT, year / 100);
DS1307_SetRegByte(DS1307_REG_YEAR, DS1307_EncodeBCD(year % 100));
}
void DS1307_SetHour(uint8_t hour_24mode) {
DS1307_SetRegByte(DS1307_REG_HOUR, DS1307_EncodeBCD(hour_24mode & 0x3f));
}
void DS1307_SetMinute(uint8_t minute) {
DS1307_SetRegByte(DS1307_REG_MINUTE, DS1307_EncodeBCD(minute));
}
void DS1307_SetSecond(uint8_t second) {
uint8_t ch = DS1307_GetClockHalt();
DS1307_SetRegByte(DS1307_REG_SECOND, DS1307_EncodeBCD(second | ch));
}
uint8_t DS1307_DecodeBCD(uint8_t bin) {
return (((bin & 0xf0) >> 4) * 10) + (bin & 0x0f);
}
uint8_t DS1307_EncodeBCD(uint8_t dec) {
return (dec % 10 + ((dec / 10) << 4));
}
void Display_DateTime()
{
uint8_t date = DS1307_GetDate();
uint8_t month = DS1307_GetMonth();
uint16_t year = DS1307_GetYear();
uint8_t hour = DS1307_GetHour();
uint8_t minute = DS1307_GetMinute();
uint8_t second = DS1307_GetSecond();
char dateStr[11]; // Date format DD/MM/YYYY
sprintf(dateStr, "%02d/%02d/%04d", date, month, year);
LPUART_DRV_SendDataBlocking(0, (uint8_t *)dateStr, sizeof(dateStr), 1000);
char timeStr[9]; // Time format HH:MM:SS
sprintf(timeStr, "%02d:%02d:%02d", hour, minute, second);
LPUART_DRV_SendDataBlocking(0, (uint8_t *)timeStr, sizeof(timeStr), 1000);
}
Hi @KKB
According to the information provided, the communication from the S32K1 looks correct. So, I suggest you contact your device supplier to verify the required process and steps to work with the RTC.
If my understanding is wrong, please share the output signals of the S32K1 to verify the driver's behavior.
B.R.
VaneB