/* * RTC.c * * Created on: 15 janv. 2019 * Author: e_rcluze */ #include "RTC.h" /** Private Fonction*/ HAL_StatusTypeDef readRTC(uint8_t reg, uint8_t *tab, int len) { HAL_StatusTypeDef res = HAL_I2C_Master_Transmit(I2C_RTC, RTC_ADD, ®, 1, 10000); if(res != HAL_OK) return res; return HAL_I2C_Master_Receive(I2C_RTC, RTC_ADD, tab, len, 1000); } HAL_StatusTypeDef writeRTC(uint8_t reg, uint8_t *tab, int len) { uint8_t t[len + 1]; t[0] = reg; for (int i = 0; i < len; i++) t[i + 1] = tab[i]; return HAL_I2C_Master_Transmit(I2C_RTC, RTC_ADD, t, len + 1, 1000); } /** Public Fonction*/ void initRTC(I2C_HandleTypeDef *typ) { I2C_RTC = typ; } HAL_StatusTypeDef readHeure(Heure *heure) { uint8_t tab[2]; HAL_StatusTypeDef res = readRTC(MINUTES, tab, 2); heure->minute = (((tab[0] & 0x70) >> 4) * 10) + (tab[0] & 0X0F); heure->heure = (((tab[1] & 0X30) >> 4) * 10) + (tab[1] & 0X0F); return res; } /** Public Fonction*/ HAL_StatusTypeDef readDate(Date *date) { uint8_t tab[7]; HAL_StatusTypeDef res = readRTC(SECOND, tab, 7); date->second = (((tab[0] & 0X70) >> 4) * 10) + (tab[0] & 0X0F); date->minute = (((tab[1] & 0x70) >> 4) * 10) + (tab[1] & 0X0F); date->heure = (((tab[2] & 0X30) >> 4) * 10) + (tab[2] & 0X0F); date->jour = (((tab[4] & 0X30) >> 4) * 10) + (tab[4] & 0X0F); date->mois = (((tab[5] & 0X10) >> 4) * 10) + (tab[5] & 0X0F); date->annee = ((tab[6] >> 4) * 10) + (tab[6] & 0X0F); return res; } HAL_StatusTypeDef writeDate(Date *date) { uint8_t tab[7]; tab[0] = ((date->second / 10) << 4) + (date->second % 10); tab[1] = ((date->minute / 10) << 4) + (date->minute % 10); tab[2] = ((date->heure / 10) << 4) + (date->heure % 10); tab[3] = date->joursemaine; tab[4] = ((date->jour / 10) << 4) + (date->jour % 10); tab[5] = ((date->mois / 10) << 4) + (date->mois % 10); tab[6] = ((date->annee / 10) << 4) + (date->annee % 10); return writeRTC(SECOND, tab, 7); } HAL_StatusTypeDef redemareRTC() { uint8_t pt[1]; HAL_StatusTypeDef res = readRTC(ALARM_HOURS, pt, 1); if (res != HAL_OK) return res; pt[0] = pt[0] & !0x40; return writeRTC(ALARM_HOURS, pt, 1); } void testRTC() { Date d; d.annee = 2020; d.mois = 1; d.jour = 20; d.joursemaine = 1; d.heure = 14; d.minute = 0; d.second = 0; writeDate(&d); redemareRTC(); HAL_Delay(2000); readDate(&d); HAL_Delay(1); } uint16_t HeureEnMinute() { Heure h; readHeure(&h); return h.minute+h.heure*60; } void testBatteryRTC() { redemareRTC(); Date d; readDate(&d); HAL_Delay(1); }