From: Nathael Pajani Date: Tue, 5 Mar 2019 14:35:58 +0000 (+0100) Subject: Sample RTC code (PCF85363 from NXP) X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=cdb78a6ff638956165cefe811093d0eb6b51cb23;p=soft%2Flpc122x%2Fexamples Sample RTC code (PCF85363 from NXP) --- diff --git a/rtc_pcf85363/Makefile b/rtc_pcf85363/Makefile new file mode 100644 index 0000000..41ae555 --- /dev/null +++ b/rtc_pcf85363/Makefile @@ -0,0 +1,21 @@ +# Makefile for apps + +MODULE = $(shell basename $(shell cd .. && pwd && cd -)) +NAME = $(shell basename $(CURDIR)) + +# Add this to your ~/.vimrc in order to get proper function of :make in vim : +# let $COMPILE_FROM_IDE = 1 +ifeq ($(strip $(COMPILE_FROM_IDE)),) + PRINT_DIRECTORY = --no-print-directory +else + PRINT_DIRECTORY = + LANG = C +endif + +.PHONY: $(NAME).bin +$(NAME).bin: + @make -C ../../.. ${PRINT_DIRECTORY} NAME=$(NAME) MODULE=$(MODULE) apps/$(MODULE)/$(NAME)/$@ + +clean mrproper: + @make -C ../../.. ${PRINT_DIRECTORY} $@ + diff --git a/rtc_pcf85363/README b/rtc_pcf85363/README new file mode 100644 index 0000000..26835ca --- /dev/null +++ b/rtc_pcf85363/README @@ -0,0 +1,25 @@ +Example code for PCF85363 RTC support + +Copyright 2019 Nathael Pajani + + +/* **************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +This simple example reads the date found in the RTC, possibly updates it if older +than the date stored in the "oldest" rtc_time structure (hard-coded in source code) +and then periodically reads and displays the time on the first UART. + diff --git a/rtc_pcf85363/main.c b/rtc_pcf85363/main.c new file mode 100644 index 0000000..cd86240 --- /dev/null +++ b/rtc_pcf85363/main.c @@ -0,0 +1,139 @@ +/**************************************************************************** + * apps/base/rtc_pcf85363/main.c + * + * Sample support of NXP PCF85363 RTC + * + * Copyright 2016 Nathael Pajani + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + + + +#include "core/system.h" +#include "core/systick.h" +#include "core/pio.h" +#include "lib/stdio.h" +#include "lib/errno.h" +#include "drivers/serial.h" +#include "drivers/gpio.h" +#include "drivers/i2c.h" + +#include "extdrv/rtc_pcf85363a.h" +#include "extdrv/status_led.h" + +#define MODULE_VERSION 0x04 +#define MODULE_NAME "Mod GPIO Demo" + + +#define SELECTED_FREQ FREQ_SEL_48MHz + +/***************************************************************************** */ +/* Pins configuration */ +/* pins blocks are passed to set_pins() for pins configuration. + * Unused pin blocks can be removed safely with the corresponding set_pins() call + * All pins blocks may be safelly merged in a single block for single set_pins() call.. + */ +const struct pio_config common_pins[] = { + /* UART 0 : Config / Debug / USB */ + { LPC_UART0_RX_PIO_0_1, LPC_IO_DIGITAL }, + { LPC_UART0_TX_PIO_0_2, LPC_IO_DIGITAL }, + /* I2C : RTC, Display, UEXT */ + { LPC_I2C0_SCL_PIO_0_10, (LPC_IO_DIGITAL | LPC_IO_OPEN_DRAIN_ENABLE) }, + { LPC_I2C0_SDA_PIO_0_11, (LPC_IO_DIGITAL | LPC_IO_OPEN_DRAIN_ENABLE) }, + /* GPIO */ + ARRAY_LAST_PIO, +}; + + +const struct pio status_led_green = LPC_GPIO_1_4; +const struct pio status_led_red = LPC_GPIO_1_5; + + +/***************************************************************************** */ +/* RTC and time */ +#define RTC_ADDR 0xA2 +struct rtc_pcf85363a_config rtc_conf = { + .bus_num = I2C0, + .addr = RTC_ADDR, + .mode = PCF85363A_MODE_RTC, + .config_marker = PCF85363A_CONFIGURED_1, + .batt_ctrl = PCF85363A_CONF_BATT_TH_2_8V, +}; +/* Oldest acceptable time in RTC. BCD coded. */ +const struct rtc_time oldest = { + .year = 0x19, + .month = 0x01, + .day = 0x11, + .hour = 0x09, + .min = 0x25, +}; +static struct rtc_time now; + + +void system_init() +{ + /* Configure the Watchdog */ + startup_watchdog_disable(); /* Do it right now, before it gets a chance to break in */ + system_set_default_power_state(); + clock_config(SELECTED_FREQ); + set_pins(common_pins); + gpio_on(); + status_led_config(&status_led_green, &status_led_red); + /* System tick timer MUST be configured and running in order to use the sleeping + * functions */ + systick_timer_on(1); /* 1ms */ + systick_start(); +} + + +/***************************************************************************** */ +int main(void) +{ + int ret = 0; + char buff[30]; + + system_init(); + status_led(red_only); + uart_on(UART0, 115200, NULL); + i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER); + + /* RTC init */ + ret = rtc_pcf85363a_config(&rtc_conf); + ret = rtc_pcf85363a_is_up(&rtc_conf, &oldest); + if (ret == 1) { + rtc_pcf85363_time_read(&rtc_conf, &now); + rtc_pcf85363_time_to_str(&now, buff, 30); + uprintf(UART0, buff); + } else if (ret == -EFAULT) { + memcpy(&now, &oldest, sizeof(struct rtc_time)); + rtc_pcf85363_time_write(&rtc_conf, &now); + rtc_pcf85363_time_to_str(&now, buff, 30); + uprintf(UART0, "Unconfigured RTC, updating time to %s.\n", buff); + } + + while (1) { + /* Update time */ + chenillard(1000); + rtc_pcf85363_time_read(&rtc_conf, &now); + rtc_pcf85363_time_to_str(&now, buff, 30); + uprintf(UART0, buff); + } + return 0; +} + + +