Merged in v01d/nuttx/kinetis_i2c (pull request #113)

I2C and RTC support for Kinetis
This commit is contained in:
Gregory Nutt
2016-08-13 16:54:14 -06:00
11 changed files with 1245 additions and 6 deletions
+8
View File
@@ -131,6 +131,14 @@ ifeq ($(CONFIG_PWM),y)
CHIP_CSRCS += kinetis_pwm.c
endif
ifeq ($(CONFIG_I2C),y)
CHIP_CSRCS += kinetis_i2c.c
endif
ifeq ($(CONFIG_RTC),y)
CHIP_CSRCS += kinetis_rtc.c
endif
ifeq ($(CONFIG_NET),y)
ifeq ($(CONFIG_KINETIS_ENET),y)
CHIP_CSRCS += kinetis_enet.c
+4 -4
View File
@@ -59,7 +59,7 @@
#define KINETIS_RTC_CR_OFFSET 0x0010 /* RTC Control Register */
#define KINETIS_RTC_SR_OFFSET 0x0014 /* RTC Status Register */
#define KINETIS_RTC_LR_OFFSET 0x0018 /* RTC Lock Register */
#if defined(KINETIS_K40) || defined(KINETIS_K64)
#if defined(KINETIS_K20) || defined(KINETIS_K40) || defined(KINETIS_K64)
# define KINETIS_RTC_IER_OFFSET 0x001c /* RTC Interrupt Enable Register (K40) */
#endif
#ifdef KINETIS_K60
@@ -77,7 +77,7 @@
#define KINETIS_RTC_CR (KINETIS_RTC_BASE+KINETIS_RTC_CR_OFFSET)
#define KINETIS_RTC_SR (KINETIS_RTC_BASE+KINETIS_RTC_SR_OFFSET)
#define KINETIS_RTC_LR (KINETIS_RTC_BASE+KINETIS_RTC_LR_OFFSET)
#if defined(KINETIS_K40) || defined(KINETIS_K64)
#if defined(KINETIS_K20) || defined(KINETIS_K40) || defined(KINETIS_K64)
# define KINETIS_RTC_IER (KINETIS_RTC_BASE+KINETIS_RTC_IER_OFFSET)
#endif
#ifdef KINETIS_K60
@@ -135,13 +135,13 @@
#define RTC_LR_TCL (1 << 3) /* Bit 3: Time Compensation Lock */
#define RTC_LR_CRL (1 << 4) /* Bit 4: Control Register Lock */
#define RTC_LR_SRL (1 << 5) /* Bit 5: Status Register Lock */
#ifdef KINETIS_K40
#if defined(KINETIS_K20) || defined(KINETIS_K40)
# define RTC_LR_LRL (1 << 6) /* Bit 6: Lock Register Lock (K40) */
#endif
/* Bits 7-31: Reserved */
/* RTC Interrupt Enable Register (32-bits, K40) */
#if defined(KINETIS_K40) || defined(KINETIS_K64)
#if defined(KINETIS_K20) || defined(KINETIS_K40) || defined(KINETIS_K64)
# define RTC_IER_TIIE (1 << 0) /* Bit 0: Time Invalid Interrupt Enable */
# define RTC_IER_TOIE (1 << 1) /* Bit 1: Time Overflow Interrupt Enable */
# define RTC_IER_TAIE (1 << 2) /* Bit 2: Time Alarm Interrupt Enable */
+78
View File
@@ -0,0 +1,78 @@
#ifndef __ARCH_ARM_SRC_KINETIS_ALARM_H
#define __ARCH_ARM_SRC_KINETIS_ALARM_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include "chip.h"
#ifdef CONFIG_RTC_ALARM
/****************************************************************************
* Public Types
****************************************************************************/
#ifndef __ASSEMBLY__
/* The form of an alarm callback */
typedef CODE void (*alarmcb_t)(void);
/****************************************************************************
* Public Functions
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: kinetis_rtc_setalarm
*
* Description:
* Set up an alarm.
*
* Input Parameters:
* tp - the time to set the alarm
* callback - the function to call when the alarm expires.
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
****************************************************************************/
struct timespec;
int kinetis_rtc_setalarm(FAR const struct timespec *tp, alarmcb_t callback);
/****************************************************************************
* Name: kinetis_rtc_cancelalarm
*
* Description:
* Cancel a pending alarm alarm
*
* Input Parameters:
* none
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
****************************************************************************/
int kinetis_rtc_cancelalarm(void);
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* CONFIG_RTC_ALARM */
#endif /* __ARCH_ARM_SRC_KINETIS_ALARM_H */
File diff suppressed because it is too large Load Diff
+52
View File
@@ -0,0 +1,52 @@
#ifndef __ARCH_ARM_SRC_KINETIS_KINETIS_I2C_H
#define __ARCH_ARM_SRC_KINETIS_KINETIS_I2C_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/i2c/i2c_master.h>
#include "chip/kinetis_i2c.h"
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: kinetis_i2cbus_initialize
*
* Description:
* Initialize the selected I2C port. And return a unique instance of struct
* struct i2c_master_s. This function may be called to obtain multiple
* instances of the interface, each of which may be set up with a
* different frequency and slave address.
*
* Input Parameter:
* Port number (for hardware that has multiple I2C interfaces)
*
* Returned Value:
* Valid I2C device structure reference on succcess; a NULL on failure
*
****************************************************************************/
FAR struct i2c_master_s *kinetis_i2cbus_initialize(int port);
/****************************************************************************
* Name: kinetis_i2cbus_uninitialize
*
* Description:
* De-initialize the selected I2C port, and power down the device.
*
* Input Parameter:
* Device structure as returned by the lpc43_i2cbus_initialize()
*
* Returned Value:
* OK on success, ERROR when internal reference count mismatch or dev
* points to invalid hardware device.
*
****************************************************************************/
int kinetis_i2cbus_uninitialize(FAR struct i2c_master_s *dev);
#endif /* __ARCH_ARM_SRC_KINETIS_KINETIS_I2C_H */
+323
View File
@@ -0,0 +1,323 @@
/************************************************************************************
* Included Files
************************************************************************************/
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/timers/rtc.h>
#include <arch/board/board.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include "up_arch.h"
#include "kinetis_config.h"
#include "chip.h"
#include "chip/kinetis_rtc.h"
#include "chip/kinetis_sim.h"
#include "kinetis.h"
#include "kinetis_alarm.h"
#if defined(CONFIG_RTC)
/************************************************************************************
* Pre-processor Definitions
************************************************************************************/
/************************************************************************************
* Private Data
************************************************************************************/
#ifdef CONFIG_RTC_ALARM
static alarmcb_t g_alarmcb;
#endif
/************************************************************************************
* Private Declarations
************************************************************************************/
static int kinetis_rtc_interrupt(int irq, void *context);
/************************************************************************************
* Public Data
************************************************************************************/
volatile bool g_rtc_enabled = false;
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: up_rtc_initialize
*
* Description:
* Initialize the hardware RTC per the selected configuration. This function is
* called once during the OS initialization sequence
*
* Input Parameters:
* None
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
************************************************************************************/
int up_rtc_initialize(void)
{
int regval;
/* enable RTC module */
regval = getreg32(KINETIS_SIM_SCGC6);
regval |= SIM_SCGC6_RTC;
putreg32(regval, KINETIS_SIM_SCGC6);
/* disable counters (just in case) */
putreg32(0, KINETIS_RTC_SR);
/* enable oscilator */
putreg32(RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE, KINETIS_RTC_CR); /* capacitance values from teensyduino */
/* TODO: delay some time (1024 cycles? would be 30ms) */
/* disable interrupts */
putreg32(0, KINETIS_RTC_IER);
/* reset flags requires writing the seconds register, the following line avoids altering any stored time value */
putreg32(getreg32(KINETIS_RTC_TSR), KINETIS_RTC_TSR);
#if defined(CONFIG_RTC_ALARM)
/* enable alarm interrupts */
irq_attach(KINETIS_IRQ_RTC, kinetis_rtc_interrupt);
up_enable_irq(KINETIS_IRQ_RTC);
#endif
/* enable counters */
putreg32(RTC_SR_TCE, KINETIS_RTC_SR);
/* mark RTC enabled */
g_rtc_enabled = true;
return OK;
}
/************************************************************************************
* Name: up_rtc_time
*
* Description:
* Get the current time in seconds. This is similar to the standard time()
* function. This interface is only required if the low-resolution RTC/counter
* hardware implementation selected. It is only used by the RTOS during
* initialization to set up the system time when CONFIG_RTC is set but neither
* CONFIG_RTC_HIRES nor CONFIG_RTC_DATETIME are set.
*
* Input Parameters:
* None
*
* Returned Value:
* The current time in seconds
*
************************************************************************************/
#ifndef CONFIG_RTC_HIRES
time_t up_rtc_time(void)
{
return getreg32(KINETIS_RTC_TSR);
}
#endif
/************************************************************************************
* Name: up_rtc_gettime
*
* Description:
* Get the current time from the high resolution RTC clock/counter. This interface
* is only supported by the high-resolution RTC/counter hardware implementation.
* It is used to replace the system timer.
*
* Input Parameters:
* tp - The location to return the high resolution time value.
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
************************************************************************************/
#ifdef CONFIG_RTC_HIRES
int up_rtc_gettime(FAR struct timespec *tp)
{
irqstate_t flags;
uint32_t seconds, prescaler, prescaler2;
/*
* get prescaler and seconds register. this is in a loop which
* ensures that registers will be re-read if during the reads the
* prescaler has wrapped-around
*/
flags = enter_critical_section();
do
{
prescaler = getreg32(KINETIS_RTC_TPR);
seconds = getreg32(KINETIS_RTC_TSR);
prescaler2 = getreg32(KINETIS_RTC_TPR);
}
while (prescaler > prescaler2);
leave_critical_section(flags);
/* build seconds + nanoseconds from seconds and prescaler register */
tp->tv_sec = seconds;
tp->tv_nsec = prescaler * (1000000000 / CONFIG_RTC_FREQUENCY);
return OK;
}
#endif
/************************************************************************************
* Name: up_rtc_settime
*
* Description:
* Set the RTC to the provided time. All RTC implementations must be able to
* set their time based on a standard timespec.
*
* Input Parameters:
* tp - the time to use
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
************************************************************************************/
int up_rtc_settime(FAR const struct timespec *tp)
{
irqstate_t flags;
uint32_t seconds, prescaler;
seconds = tp->tv_sec;
prescaler = tp->tv_nsec * (CONFIG_RTC_FREQUENCY / 1000000000);
flags = enter_critical_section();
putreg32(0, KINETIS_RTC_SR); /* disable counter */
putreg32(prescaler, KINETIS_RTC_TPR); /* always write prescaler first */
putreg32(seconds, KINETIS_RTC_TSR);
putreg32(RTC_SR_TCE, KINETIS_RTC_SR); /* re-enable counter */
leave_critical_section(flags);
return OK;
}
/************************************************************************************
* Private Functions
************************************************************************************/
/************************************************************************************
* Name: kinetis_rtc_setalarm
*
* Description:
* Set up an alarm.
*
* Input Parameters:
* tp - the time to set the alarm
* callback - the function to call when the alarm expires.
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
************************************************************************************/
#ifdef CONFIG_RTC_ALARM
int kinetis_rtc_setalarm(FAR const struct timespec *tp, alarmcb_t callback)
{
/* Is there already something waiting on the ALARM? */
if (g_alarmcb == NULL)
{
/* No.. Save the callback function pointer */
g_alarmcb = callback;
/* Enable and set RTC alarm */
putreg32(tp->tv_sec, KINETIS_RTC_TAR); /* set alarm (also resets flags) */
putreg32(RTC_IER_TAIE, KINETIS_RTC_IER); /* enable alarm interrupt */
return OK;
}
else
return -EBUSY;
}
#endif
/************************************************************************************
* Name: kinetis_rtc_cancelalarm
*
* Description:
* Cancel a pending alarm alarm
*
* Input Parameters:
* none
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
************************************************************************************/
#ifdef CONFIG_RTC_ALARM
int kinetis_rtc_cancelalarm(void)
{
if (g_alarmcb != NULL)
{
/* Cancel the global callback function */
g_alarmcb = NULL;
/* Unset the alarm */
putreg32(0, KINETIS_RTC_IER); /* disable alarm interrupt */
return OK;
}
else
return -ENODATA;
}
#endif
/************************************************************************************
* Name: kinetis_rtc_interrupt
*
* Description:
* RTC interrupt service routine
*
* Input Parameters:
* irq - The IRQ number that generated the interrupt
* context - Architecture specific register save information.
*
* Returned Value:
* Zero (OK) on success; A negated errno value on failure.
*
************************************************************************************/
#if defined(CONFIG_RTC_ALARM)
static int kinetis_rtc_interrupt(int irq, void *context)
{
if (g_alarmcb != NULL)
{
/* Alarm callback */
g_alarmcb();
g_alarmcb = NULL;
}
/* Clear pending flags, disable alarm */
putreg32(0, KINETIS_RTC_TAR); /* unset alarm (resets flags) */
putreg32(0, KINETIS_RTC_IER); /* disable alarm interrupt */
return 0;
}
#endif
#endif // KINETIS_RTC
+15 -1
View File
@@ -190,7 +190,7 @@
#define LED_STACKCREATED 1 /* STATUS LED=ON */
#define LED_INIRQ 2 /* STATUS LED=no change */
#define LED_SIGNAL 2 /* STATUS LED=no change */
#define LED_ASSERTION 2 /* STATUS LED=no change */
#define LED_ASSERTION 3 /* STATUS LED=no change */
#define LED_PANIC 3 /* STATUS LED=flashing */
/* Button definitions ***************************************************************/
@@ -232,6 +232,20 @@
# define PIN_UART0_TX PIN_UART1_TX_1
#endif
#ifdef CONFIG_KINETIS_I2C0
#ifdef CONFIG_TEENSY_3X_I2C_ALT_PINS
# define PIN_I2C0_SCL (PIN_I2C0_SCL_1 | PIN_ALT2_OPENDRAIN | PIN_ALT2_SLOW)
# define PIN_I2C0_SDA (PIN_I2C0_SDA_1 | PIN_ALT2_OPENDRAIN | PIN_ALT2_SLOW)
#else
# define PIN_I2C0_SCL (PIN_I2C0_SCL_2 | PIN_ALT2_OPENDRAIN | PIN_ALT2_SLOW)
# define PIN_I2C0_SDA (PIN_I2C0_SDA_2 | PIN_ALT2_OPENDRAIN | PIN_ALT2_SLOW)
#endif
#endif
#ifdef CONFIG_KINETIS_I2C1
#error I2C1 not currently supported
#endif
/************************************************************************************
* Public Data
************************************************************************************/
+1 -1
View File
@@ -36,7 +36,7 @@
-include $(TOPDIR)/Make.defs
ASRCS =
CSRCS = k20_boot.c k20_spi.c
CSRCS = k20_boot.c k20_spi.c k20_i2c.c
ifeq ($(CONFIG_ARCH_LEDS),y)
CSRCS += k20_autoleds.c
+10
View File
@@ -88,3 +88,13 @@ void kinetis_boardinitialize(void)
board_autoled_initialize();
#endif
}
#if defined(CONFIG_BOARD_INITIALIZE)
void board_initialize(void)
{
#if defined(CONFIG_KINETIS_I2C0) || defined(CONFIG_KINETIS_I2C1)
//if (kinetis_i2cdev_initialize)
kinetis_i2cdev_initialize();
#endif
}
#endif
+54
View File
@@ -0,0 +1,54 @@
/************************************************************************************
* Included Files
************************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <debug.h>
#include <nuttx/i2c/i2c_master.h>
#include <arch/board/board.h>
#include "up_arch.h"
#include "chip.h"
#include "kinetis.h"
#include "kinetis_i2c.h"
#include "teensy-3x.h"
#if defined(CONFIG_KINETIS_I2C0) || defined(CONFIG_KINETIS_I2C1)
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: kinetis_i2cdev_initialize
*
* Description:
* Called to configure I2C
*
************************************************************************************/
void kinetis_i2cdev_initialize(void)
{
i2c_dev = NULL;
#if defined(CONFIG_KINETIS_I2C0)
i2c_dev = kinetis_i2cbus_initialize(0);
#if defined(CONFIG_I2C_DRIVER)
i2c_register(i2c_dev, 0);
#endif
#endif
#if defined(CONFIG_KINETIS_I2C1)
#error Not yet supported in kinetis driver
i2c_dev = kinetis_i2cbus_initialize(1);
#if defined(CONFIG_I2C_DRIVER)
i2c_register(i2c_dev, 1);
#endif
#endif
}
#endif /* CONFIG_KINETIS_I2C0 || CONFIG_KINETIS_I2C1 */
+13
View File
@@ -44,6 +44,7 @@
#include <nuttx/compiler.h>
#include <stdint.h>
#include <arch/kinetis/chip.h>
#include <nuttx/i2c/i2c_master.h>
/************************************************************************************
* Pre-processor Definitions
@@ -76,6 +77,8 @@
* Public data
************************************************************************************/
FAR struct i2c_master_s* i2c_dev;
#ifndef __ASSEMBLY__
/************************************************************************************
@@ -92,6 +95,16 @@
extern void weak_function kinetis_spidev_initialize(void);
/************************************************************************************
* Name: kinetis_i2cdev_initialize
*
* Description:
* Called to configure I2C
*
************************************************************************************/
void kinetis_i2cdev_initialize(void);
/************************************************************************************
* Name: kinetis_usbinitialize
*