diff --git a/arch/arm/src/lpc43xx/lpc43_i2c.c b/arch/arm/src/lpc43xx/lpc43_i2c.c index 8dd3175e70..0b9efffa7d 100644 --- a/arch/arm/src/lpc43xx/lpc43_i2c.c +++ b/arch/arm/src/lpc43xx/lpc43_i2c.c @@ -1,7 +1,7 @@ /**************************************************************************** * arch/arm/src/lpc43xx/lpc43_i2c.c * - * Copyright (C) 2012, 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2012, 2014-2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Ported from from the LPC17 version: @@ -234,8 +234,8 @@ static int i2c_write(FAR struct i2c_dev_s *dev, const uint8_t *buffer, priv->msg.buffer = (uint8_t *)buffer; priv->msg.length = buflen; - priv->nmsg = 1; - priv->msgs = &(priv->msg); + priv->nmsg = 1; + priv->msgs = &(priv->msg); if (buflen > 0) { @@ -261,14 +261,14 @@ static int i2c_read(FAR struct i2c_dev_s *dev, uint8_t *buffer, int buflen) DEBUGASSERT(dev != NULL); - priv->wrcnt = 0; - priv->rdcnt = 0; + priv->wrcnt = 0; + priv->rdcnt = 0; priv->msg.flags = I2C_M_READ; priv->msg.buffer = buffer; priv->msg.length = buflen; - priv->nmsg = 1; - priv->msgs = &(priv->msg); + priv->nmsg = 1; + priv->msgs = &(priv->msg); if (buflen > 0) { @@ -427,8 +427,15 @@ static int i2c_interrupt(int irq, FAR void *context) case 0x08: /* A START condition has been transmitted. */ case 0x10: /* A Repeated START condition has been transmitted. */ - putreg32(((I2C_M_READ & msg->flags) == I2C_M_READ)?I2C_READADDR8(msg->addr):I2C_WRITEADDR8(msg->addr), priv->base + LPC43_I2C_DAT_OFFSET); /* set address */ - putreg32(I2C_CONCLR_STAC, priv->base + LPC43_I2C_CONCLR_OFFSET); /* clear start bit */ + /* Set address */ + + putreg32(((I2C_M_READ & msg->flags) == I2C_M_READ) ? + I2C_READADDR8(msg->addr) : + I2C_WRITEADDR8(msg->addr), priv->base + LPC43_I2C_DAT_OFFSET); + + /* Clear start bit */ + + putreg32(I2C_CONCLR_STAC, priv->base + LPC43_I2C_CONCLR_OFFSET); break; /* Write cases */ diff --git a/arch/arm/src/lpc43xx/lpc43_spi.c b/arch/arm/src/lpc43xx/lpc43_spi.c index e56367e4ef..05859e39c0 100644 --- a/arch/arm/src/lpc43xx/lpc43_spi.c +++ b/arch/arm/src/lpc43xx/lpc43_spi.c @@ -1,7 +1,7 @@ /**************************************************************************** * arch/arm/src/lpc43xx/lpc43_spi.c * - * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -603,20 +603,20 @@ FAR struct spi_dev_s *lpc43_spiinitialize(int port) FAR struct spi_dev_s *up_spiinitialize(int port) { - if (port) { -#if ( defined(CONFIG_LPC43_SSP0) || defined(CONFIG_LPC43_SSP1) ) - return lpc43_sspinitialize(port-1); + if (port) + { +#if defined(CONFIG_LPC43_SSP0) || defined(CONFIG_LPC43_SSP1) + return lpc43_sspinitialize(port - 1); #else - return NULL; + return NULL; #endif - } else { + } + else + { #if defined(CONFIG_LPC43_SPI) - return lpc43_spiinitialize(port); + return lpc43_spiinitialize(port); #else - return NULL; + return NULL; #endif - } + } } - - - diff --git a/arch/arm/src/lpc43xx/lpc43_ssp.c b/arch/arm/src/lpc43xx/lpc43_ssp.c index 38c1d0bde2..ed29d8932e 100644 --- a/arch/arm/src/lpc43xx/lpc43_ssp.c +++ b/arch/arm/src/lpc43xx/lpc43_ssp.c @@ -137,6 +137,7 @@ static void ssp_exchange(FAR struct spi_dev_s *dev, FAR const void *txbuffer static void ssp_sndblock(FAR struct spi_dev_s *dev, FAR const void *buffer, size_t nwords); static void ssp_recvblock(FAR struct spi_dev_s *dev, FAR void *buffer, size_t nwords); #endif + /* Initialization */ #ifdef CONFIG_LPC43_SSP0 @@ -537,8 +538,30 @@ static uint16_t ssp_send(FAR struct spi_dev_s *dev, uint16_t wd) return (uint16_t)regval; } +/**************************************************************************** + * Name: ssp_exchange + * + * Description: + * Exahange a block of data from SPI. Required. + * + * Input Parameters: + * dev - Device-specific state data + * txbuffer - A pointer to the buffer of data to be sent + * rxbuffer - A pointer to the buffer in which to receive data + * nwords - the length of data that to be exchanged in units of words. + * The wordsize is determined by the number of bits-per-word + * selected for the SPI interface. If nbits <= 8, the data is + * packed into uint8_t's; if nbits >8, the data is packed into + * uint16_t's + * + * Returned Value: + * None + * + ****************************************************************************/ + static void ssp_exchange(FAR struct spi_dev_s *dev, FAR const void *txbuffer, - FAR void *rxbuffer, size_t nwords) { + FAR void *rxbuffer, size_t nwords) +{ FAR struct lpc43_sspdev_s *priv = (FAR struct lpc43_sspdev_s *)dev; union { @@ -553,7 +576,7 @@ static void ssp_exchange(FAR struct spi_dev_s *dev, FAR const void *txbuffer, FAR void *pv; } rx; uint32_t data; - uint32_t datadummy = (priv->nbits > 8)?0xffff:0xff; + uint32_t datadummy = (priv->nbits > 8) ? 0xffff : 0xff; uint32_t rxpending = 0; /* While there is remaining to be sent (and no synchronization error has occurred) */ @@ -572,38 +595,39 @@ static void ssp_exchange(FAR struct spi_dev_s *dev, FAR const void *txbuffer, spivdbg("TX: rxpending: %d nwords: %d\n", rxpending, nwords); while ((ssp_getreg(priv, LPC43_SSP_SR_OFFSET) & SSP_SR_TNF) && - (rxpending < LPC43_SSP_FIFOSZ) && nwords) - { - if (txbuffer && priv->nbits > 8) - { - data = (uint32_t)*tx.p16++; - } - else - { - data = (uint32_t)*tx.p8++; - } + (rxpending < LPC43_SSP_FIFOSZ) && nwords) + { + if (txbuffer && priv->nbits > 8) + { + data = (uint32_t)*tx.p16++; + } + else + { + data = (uint32_t)*tx.p8++; + } - ssp_putreg(priv, LPC43_SSP_DR_OFFSET, txbuffer?data:datadummy); - nwords--; - rxpending++; - } + ssp_putreg(priv, LPC43_SSP_DR_OFFSET, txbuffer?data:datadummy); + nwords--; + rxpending++; + } /* Now, read the RX data from the RX FIFO while the RX FIFO is not empty */ spivdbg("RX: rxpending: %d\n", rxpending); while (ssp_getreg(priv, LPC43_SSP_SR_OFFSET) & SSP_SR_RNE) - { - data = ssp_getreg(priv, LPC43_SSP_DR_OFFSET); - if (rxbuffer && priv->nbits > 8) - { - *rx.p16++ = (uint16_t)data; - } - else - { - *rx.p8++ = (uint8_t)data; - } - rxpending--; - } + { + data = ssp_getreg(priv, LPC43_SSP_DR_OFFSET); + if (rxbuffer && priv->nbits > 8) + { + *rx.p16++ = (uint16_t)data; + } + else + { + *rx.p8++ = (uint8_t)data; + } + + rxpending--; + } } } @@ -619,12 +643,14 @@ static void ssp_exchange(FAR struct spi_dev_s *dev, FAR const void *txbuffer, * nwords - the length of data to send from the buffer in number of words. * The wordsize is determined by the number of bits-per-word * selected for the SPI interface. If nbits <= 8, the data is - * packed into uint8_t's; if nbits >8, the data is packed into uint16_t's + * packed into uint8_t's; if nbits >8, the data is packed into + * uint16_t's * * Returned Value: * None * ****************************************************************************/ + #ifndef CONFIG_SPI_EXCHANGE static void ssp_sndblock(FAR struct spi_dev_s *dev, FAR const void *buffer, size_t nwords) { @@ -643,7 +669,8 @@ static void ssp_sndblock(FAR struct spi_dev_s *dev, FAR const void *buffer, size * nwords - the length of data that can be received in the buffer in number * of words. The wordsize is determined by the number of bits-per-word * selected for the SPI interface. If nbits <= 8, the data is - * packed into uint8_t's; if nbits >8, the data is packed into uint16_t's + * packed into uint8_t's; if nbits >8, the data is packed into + * uint16_t's * * Returned Value: * None @@ -654,10 +681,8 @@ static void ssp_recvblock(FAR struct spi_dev_s *dev, FAR void *buffer, size_t nw { return ssp_exchange(dev, NULL, buffer, nwords); } +#endif /* !CONFIG_SPI_EXCHANGE */ -#endif - -#ifdef CONFIG_LPC43_SSP0 /**************************************************************************** * Name: lpc43_ssp0initialize * @@ -672,6 +697,7 @@ static void ssp_recvblock(FAR struct spi_dev_s *dev, FAR void *buffer, size_t nw * ****************************************************************************/ +#ifdef CONFIG_LPC43_SSP0 static inline FAR struct lpc43_sspdev_s *lpc43_ssp0initialize(void) { irqstate_t flags; diff --git a/arch/arm/src/lpc43xx/lpc43_tickless_rit.c b/arch/arm/src/lpc43xx/lpc43_tickless_rit.c index 531755cfe4..ef63c48ea0 100644 --- a/arch/arm/src/lpc43xx/lpc43_tickless_rit.c +++ b/arch/arm/src/lpc43xx/lpc43_tickless_rit.c @@ -1,7 +1,7 @@ /**************************************************************************** * arch/arm/src/lpc43/lpc43_rit.c * - * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2015 Gregory Nutt. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -40,7 +40,6 @@ * ****************************************************************************/ - /**************************************************************************** * Included Files ****************************************************************************/ @@ -80,18 +79,18 @@ static uint32_t COMMON_DEV; static uint32_t MIN_TICKS; static uint32_t MIN_NSEC; -static uint32_t RESET_TICKS = 1000; /* ticks to add to force a reset */ +static uint32_t RESET_TICKS = 1000; /* Ticks to add to force a reset */ -static struct timespec base_ts; /* time base */ -static uint32_t base_rest; /* rest of ticks that is < MIN_TICKS*/ +static struct timespec base_ts; /* Time base */ +static uint32_t base_rest; /* Rest of ticks that is < MIN_TICKS*/ static struct timespec alarm_time_ts; /* alarmTime to set on next interrupt, used if not already armed */ -static bool alarm_time_set = false; /* true if alarm_time set and need to be processed */ -static bool call = false; /* true if callback should be called on next interrupt */ -static bool forced_int = false; /* true if interrupt was forced with mask, no reset */ -static bool armed = false; /* true if alarm is armed for next match */ -static uint32_t synch = 0; /* synch all calls, recursion is possible */ +static bool alarm_time_set = false; /* true if alarm_time set and need to be processed */ +static bool call = false; /* true if callback should be called on next interrupt */ +static bool forced_int = false; /* true if interrupt was forced with mask, no reset */ +static bool armed = false; /* true if alarm is armed for next match */ +static uint32_t synch = 0; /* Synch all calls, recursion is possible */ static irqstate_t g_flags; static uint32_t ctrl_cache; @@ -102,115 +101,115 @@ static uint32_t compare_cache; * Private Functions ****************************************************************************/ -/* some timer HW functions */ +/* Some timer HW functions */ - static inline void lpc43_tl_set_counter (uint32_t value) +static inline void lpc43_tl_set_counter(uint32_t value) { putreg32(value, LPC43_RIT_COUNTER); } - static inline uint32_t lpc43_tl_get_counter (void) +static inline uint32_t lpc43_tl_get_counter(void) { return getreg32(LPC43_RIT_COUNTER); } - static inline void lpc43_tl_set_compare (uint32_t value) +static inline void lpc43_tl_set_compare(uint32_t value) { - if ( value != compare_cache) + if (value != compare_cache) { compare_cache = value; putreg32(value, LPC43_RIT_COMPVAL); } } -static inline uint32_t lpc43_tl_get_compare (void) +static inline uint32_t lpc43_tl_get_compare(void) { return compare_cache; } - static inline void lpc43_tl_set_mask (uint32_t value) + static inline void lpc43_tl_set_mask(uint32_t value) { - if ( value != mask_cache) + if (value != mask_cache) { mask_cache = value; putreg32(value, LPC43_RIT_MASK); } } -static inline uint32_t lpc43_tl_get_mask (void) +static inline uint32_t lpc43_tl_get_mask(void) { return mask_cache; } -static inline bool lpc43_tl_get_ctrl_bit (uint32_t bit) +static inline bool lpc43_tl_get_ctrl_bit(uint32_t bit) { return ((ctrl_cache & bit)?true:false); } - static inline void lpc43_tl_set_ctrl_bit (uint32_t bit, bool value) +static inline void lpc43_tl_set_ctrl_bit(uint32_t bit, bool value) { - - if ( lpc43_tl_get_ctrl_bit(bit) != value ) { - - if (value) - { - ctrl_cache |= bit; - } - else - { - ctrl_cache &= ~bit; - } + if (lpc43_tl_get_ctrl_bit(bit) != value) + { + if (value) + { + ctrl_cache |= bit; + } + else + { + ctrl_cache &= ~bit; + } putreg32(ctrl_cache, LPC43_RIT_CTRL); - } + } } -static inline void lpc43_tl_set_reset_on_match (bool value) +static inline void lpc43_tl_set_reset_on_match(bool value) { - lpc43_tl_set_ctrl_bit (RIT_CTRL_ENCLR, value); + lpc43_tl_set_ctrl_bit(RIT_CTRL_ENCLR, value); } -static inline bool lpc43_tl_get_reset_on_match (void) +static inline bool lpc43_tl_get_reset_on_match(void) { - return lpc43_tl_get_ctrl_bit (RIT_CTRL_ENCLR); + return lpc43_tl_get_ctrl_bit(RIT_CTRL_ENCLR); } -static inline void lpc43_tl_set_enable (bool value) +static inline void lpc43_tl_set_enable(bool value) { - lpc43_tl_set_ctrl_bit (RIT_CTRL_EN, value); + lpc43_tl_set_ctrl_bit(RIT_CTRL_EN, value); } -static inline bool lpc43_tl_get_enable (void) +static inline bool lpc43_tl_get_enable(void) { - return lpc43_tl_get_ctrl_bit (RIT_CTRL_EN); + return lpc43_tl_get_ctrl_bit(RIT_CTRL_EN); } - static inline void lpc43_tl_clear_interrupt (void) +static inline void lpc43_tl_clear_interrupt(void) { putreg32(ctrl_cache | RIT_CTRL_INT, LPC43_RIT_CTRL); } - static inline bool lpc43_tl_get_interrupt (void) +static inline bool lpc43_tl_get_interrupt(void) { - return (( getreg32(LPC43_RIT_CTRL) & RIT_CTRL_INT )?true:false); + return ((getreg32(LPC43_RIT_CTRL) & RIT_CTRL_INT)?true:false); } -/* converters */ +/* Converters */ + static uint32_t commonDev(uint32_t a, uint32_t b) { - while(b !=0) + while (b != 0) { - int h = a%b; - a = b; - b = h; + int h = a%b; + a = b; + b = h; } return a; } - static void lpc43_tl_add(FAR const struct timespec *ts1, - FAR const struct timespec *ts2, - FAR struct timespec *ts3) +static void lpc43_tl_add(FAR const struct timespec *ts1, + FAR const struct timespec *ts2, + FAR struct timespec *ts3) { time_t sec = ts1->tv_sec + ts2->tv_sec; long nsec = ts1->tv_nsec + ts2->tv_nsec; @@ -225,9 +224,9 @@ static uint32_t commonDev(uint32_t a, uint32_t b) ts3->tv_nsec = nsec; } - static void lpc43_tl_sub(FAR const struct timespec *ts1, - FAR const struct timespec *ts2, - FAR struct timespec *ts3) +static void lpc43_tl_sub(FAR const struct timespec *ts1, + FAR const struct timespec *ts2, + FAR struct timespec *ts3) { time_t sec; long nsec; @@ -260,157 +259,159 @@ static uint32_t commonDev(uint32_t a, uint32_t b) ts3->tv_nsec = nsec; } -static inline uint32_t lpc43_tl_ts2tick ( FAR const struct timespec *ts) - { - return ( ts->tv_sec*LPC43_CCLK + ( ts->tv_nsec/MIN_NSEC*MIN_TICKS ) ); - } +static inline uint32_t lpc43_tl_ts2tick(FAR const struct timespec *ts) +{ + return (ts->tv_sec*LPC43_CCLK + (ts->tv_nsec/MIN_NSEC*MIN_TICKS)); +} - static uint32_t lpc43_tl_tick2ts (uint32_t ticks, FAR struct timespec *ts, bool with_rest) - { +static uint32_t lpc43_tl_tick2ts(uint32_t ticks, FAR struct timespec *ts, + bool with_rest) +{ + uint32_t ticks_whole; + uint32_t ticks_rest = 0; - uint32_t ticks_whole; - uint32_t ticks_rest = 0; + if (with_rest) + { + uint32_t ticks_mult = ticks/MIN_TICKS; + ticks_whole = ticks_mult*MIN_TICKS; + ticks_rest = ticks - ticks_whole; + } + else + { + ticks_whole = ticks; + } - if (with_rest) - { - uint32_t ticks_mult = ticks/MIN_TICKS; - ticks_whole = ticks_mult*MIN_TICKS; - ticks_rest = ticks - ticks_whole; + ts->tv_sec = ticks_whole/LPC43_CCLK; + ts->tv_nsec = ((ticks_whole%LPC43_CCLK)/MIN_TICKS)*MIN_NSEC; - } - else - { - ticks_whole = ticks; - } + return ticks_rest; +} - ts->tv_sec = ticks_whole/LPC43_CCLK; - ts->tv_nsec = ((ticks_whole%LPC43_CCLK)/MIN_TICKS)*MIN_NSEC; +/* Logic functions */ - return ticks_rest; - } - -/* logic functions */ - -static inline void lpc43_tl_sync_up (void) { +static inline void lpc43_tl_sync_up(void) +{ irqstate_t flags; - flags = irqsave (); + flags = irqsave(); - if ( synch == 0 ) + if (synch == 0) { g_flags = flags; - } + synch++; } -static inline void lpc43_tl_sync_down (void) { +static inline void lpc43_tl_sync_down(void) +{ synch--; - if ( synch == 0 ) + if (synch == 0) { - irqrestore (g_flags); + irqrestore(g_flags); } } -/* assuming safe timer state, force interrupt, no reset possible */ +/* Assuming safe timer state, force interrupt, no reset possible */ -static inline void lpc43_tl_force_int (void) +static inline void lpc43_tl_force_int(void) { forced_int = true; - lpc43_tl_set_reset_on_match (false); - lpc43_tl_set_mask (UINT32_MAX); + lpc43_tl_set_reset_on_match(false); + lpc43_tl_set_mask(UINT32_MAX); lpc43_tl_set_compare(UINT32_MAX); } -/* init all vars, forced_int should not be cleared */ +/* Init all vars, forced_int should not be cleared */ -static inline void lpc43_tl_init_timer_vars (void) +static inline void lpc43_tl_init_timer_vars(void) { alarm_time_set = false; call = false; armed = false; - } -/* calc RESET_TICKS and set compare to TO_RESET */ +/* Calc RESET_TICKS and set compare to TO_RESET */ -static void lpc43_tl_calibrate_init (void) +static void lpc43_tl_calibrate_init(void) { - uint32_t counter = lpc43_tl_get_counter (); + uint32_t counter = lpc43_tl_get_counter(); - uint32_t counter_after = lpc43_tl_get_counter (); + uint32_t counter_after = lpc43_tl_get_counter(); counter_after = TO_RESET + counter; counter_after = counter_after - counter; - /*shift to toReset*/ + /* Shift to to Reset */ - lpc43_tl_set_compare (counter_after); + lpc43_tl_set_compare(counter_after); - counter_after = lpc43_tl_get_counter (); + counter_after = lpc43_tl_get_counter(); RESET_TICKS = (counter_after - counter) * 2; - } -/* process current and set timer in default safe state */ +/* Process current and set timer in default safe state */ -static void lpc43_tl_save_timer (bool from_isr) +static void lpc43_tl_save_timer(bool from_isr) { if (forced_int) /* special case of forced interrupt by mask*/ { - forced_int = false; - lpc43_tl_set_compare (UINT32_MAX); - lpc43_tl_set_mask (0); - lpc43_tl_clear_interrupt (); + forced_int = false; + lpc43_tl_set_compare(UINT32_MAX); + lpc43_tl_set_mask(0); + lpc43_tl_clear_interrupt(); } else { + /* Process reset if any */ - /*process reset if any*/ + uint32_t match = lpc43_tl_get_compare(); - uint32_t match = lpc43_tl_get_compare (); + /* Move to end, no resets during processing */ - /*move to end, no resets during processing*/ + lpc43_tl_set_compare(UINT32_MAX); + lpc43_tl_set_mask(0); - lpc43_tl_set_compare (UINT32_MAX); - lpc43_tl_set_mask (0); + if (from_isr || lpc43_tl_get_interrupt()) + { + if (lpc43_tl_get_reset_on_match()) /*was reset ?*/ + { + struct timespec match_ts; + base_rest = lpc43_tl_tick2ts(match + base_rest, + &match_ts,true); + lpc43_tl_add(&base_ts, &match_ts,&base_ts); + } - if (from_isr || lpc43_tl_get_interrupt ()) - { - if (lpc43_tl_get_reset_on_match ()) /*was reset ?*/ - { - struct timespec match_ts; - base_rest = lpc43_tl_tick2ts(match + base_rest, &match_ts,true); - lpc43_tl_add(&base_ts, &match_ts,&base_ts); - } - lpc43_tl_clear_interrupt (); - } - } + lpc43_tl_clear_interrupt(); + } + } } -/* assuming safe timer state, true if set, false - time is in the past */ +/* Assuming safe timer state, true if set, false - time is in the past */ -static bool lpc43_tl_set_safe_compare (uint32_t compare_to_set) +static bool lpc43_tl_set_safe_compare(uint32_t compare_to_set) { if (compare_to_set < TO_RESET) { - lpc43_tl_set_reset_on_match (false); + lpc43_tl_set_reset_on_match(false); } else { - lpc43_tl_set_reset_on_match (true); + lpc43_tl_set_reset_on_match(true); } - lpc43_tl_set_compare (compare_to_set); + lpc43_tl_set_compare(compare_to_set); - //check if ok - bool reset = lpc43_tl_get_interrupt (); - uint32_t counter = lpc43_tl_get_counter (); - bool reset_after = lpc43_tl_get_interrupt (); + /* Check if ok */ + + bool reset = lpc43_tl_get_interrupt(); + uint32_t counter = lpc43_tl_get_counter(); + bool reset_after = lpc43_tl_get_interrupt(); if (reset != reset_after) { - //was a reset get new counter - counter = lpc43_tl_get_counter (); + /* Was a reset get new counter */ + + counter = lpc43_tl_get_counter(); } if (reset_after || (!reset_after && compare_to_set > counter)) @@ -419,31 +420,31 @@ static bool lpc43_tl_set_safe_compare (uint32_t compare_to_set) } else { - lpc43_tl_set_compare (UINT32_MAX); + lpc43_tl_set_compare(UINT32_MAX); return false; } - } -/* assuming safe timer state, set_safe_compare in loop */ +/* Assuming safe timer state, set_safe_compare in loop */ -static void lpc43_tl_looped_forced_set_compare (void) +static void lpc43_tl_looped_forced_set_compare(void) { uint32_t i = 1; - bool result = lpc43_tl_set_safe_compare ( - lpc43_tl_get_counter () + RESET_TICKS); /* like in calibrateInit */ + bool result = lpc43_tl_set_safe_compare( + lpc43_tl_get_counter() + RESET_TICKS); /* like in calibrateInit */ + while (!result) { i++; - result = lpc43_tl_set_safe_compare ( - lpc43_tl_get_counter () + RESET_TICKS * i); - }; + result = lpc43_tl_set_safe_compare( + lpc43_tl_get_counter() + RESET_TICKS * i); + } } -/* assuming safe timer state, true if set, false - time is in the past */ +/* Assuming safe timer state, true if set, false - time is in the past */ -static bool lpc43_tl_set_calc_arm (uint32_t curr, uint32_t to_set, bool arm) +static bool lpc43_tl_set_calc_arm(uint32_t curr, uint32_t to_set, bool arm) { uint32_t calcTime; @@ -455,17 +456,17 @@ static bool lpc43_tl_set_calc_arm (uint32_t curr, uint32_t to_set, bool arm) else { if (curr < TO_END) - { - calcTime = min(curr + RESET_TICKS, to_set); - } + { + calcTime = min(curr + RESET_TICKS, to_set); + } else - { - lpc43_tl_looped_forced_set_compare (); - return true; - } + { + lpc43_tl_looped_forced_set_compare(); + return true; + } } - bool set = lpc43_tl_set_safe_compare (calcTime); + bool set = lpc43_tl_set_safe_compare(calcTime); if (arm && set && (calcTime == to_set)) { @@ -475,26 +476,27 @@ static bool lpc43_tl_set_calc_arm (uint32_t curr, uint32_t to_set, bool arm) return set; } -/* assuming safe timer state, try to set compare for normal operation */ +/* Assuming safe timer state, try to set compare for normal operation */ -static void lpc43_tl_set_default_compare (uint32_t curr) +static void lpc43_tl_set_default_compare(uint32_t curr) { - bool result = lpc43_tl_set_calc_arm (curr, UINT32_MAX, - false); + bool result = lpc43_tl_set_calc_arm(curr, UINT32_MAX, false); if (!result) { - result = lpc43_tl_set_calc_arm (lpc43_tl_get_counter (), UINT32_MAX, - false); + result = lpc43_tl_set_calc_arm(lpc43_tl_get_counter(), UINT32_MAX, + false); if (!result) - { - lpc43_tl_looped_forced_set_compare (); - } + { + lpc43_tl_looped_forced_set_compare(); + } } - } -/* calculates ticks to set from alarm_time_ts and base_ts/base_rest, UINT32_MAX if overflow */ -static inline uint32_t lpc43_tl_calc_to_set (void) +/* Calculates ticks to set from alarm_time_ts and base_ts/base_rest, + * UINT32_MAX if overflow. + */ + +static inline uint32_t lpc43_tl_calc_to_set(void) { struct timespec diff_ts; struct timespec ovf_ts; @@ -502,99 +504,99 @@ static inline uint32_t lpc43_tl_calc_to_set (void) lpc43_tl_sub(&alarm_time_ts,&base_ts,&diff_ts); lpc43_tl_sub(&diff_ts,&MAX_TS,&ovf_ts); - if ( ovf_ts.tv_sec == 0 && ovf_ts.tv_nsec == 0 ) /* check overflow */ + if (ovf_ts.tv_sec == 0 && ovf_ts.tv_nsec == 0) /* check overflow */ { - return ( lpc43_tl_ts2tick(&diff_ts) - base_rest ); + return (lpc43_tl_ts2tick(&diff_ts) - base_rest); } else { return UINT32_MAX; } - } -/* assuming safe timer state, used by isr: sets default compare , calls alarm */ -static inline void lpc43_tl_alarm (uint32_t curr) -{ - lpc43_tl_init_timer_vars (); - lpc43_tl_set_default_compare (curr); +/* Assuming safe timer state, used by isr: sets default compare, + * calls alarm. + */ +static inline void lpc43_tl_alarm(uint32_t curr) +{ + lpc43_tl_init_timer_vars(); + lpc43_tl_set_default_compare(curr); #ifdef CONFIG_SCHED_TICKLESS_ALARM struct timespec ts; up_timer_gettime(&ts); - sched_alarm_expiration (&ts); + sched_alarm_expiration(&ts); #else sched_timer_expiration(); #endif } -/* interrupt handler */ +/* Interrupt handler */ -static int lpc43_tl_isr (int irq, FAR void *context) - { - lpc43_tl_sync_up(); +static int lpc43_tl_isr(int irq, FAR void *context) +{ + lpc43_tl_sync_up(); - lpc43_tl_save_timer(true); + lpc43_tl_save_timer(true); - uint32_t curr = lpc43_tl_get_counter (); - if (call) - { - lpc43_tl_alarm(curr); - } - else - { - if (armed) - { - lpc43_tl_alarm(curr); /* armed - call alarm */ - } - else - { - if (alarm_time_set) /* need to set alarm time */ - { - uint32_t toSet = lpc43_tl_calc_to_set(); + uint32_t curr = lpc43_tl_get_counter(); + if (call) + { + lpc43_tl_alarm(curr); + } + else + { + if (armed) + { + lpc43_tl_alarm(curr); /* armed - call alarm */ + } + else + { + if (alarm_time_set) /* need to set alarm time */ + { + uint32_t toSet = lpc43_tl_calc_to_set(); - if (toSet > curr) - { - if (toSet > TO_END) - { - lpc43_tl_set_default_compare (curr); - } - else - { - bool set = lpc43_tl_set_calc_arm (curr, toSet, true); - if (!set) - { - lpc43_tl_alarm(curr); - } - } - } - else - { - lpc43_tl_alarm(curr); - } - } - else - { - lpc43_tl_set_default_compare (curr); - } - } - } + if (toSet > curr) + { + if (toSet > TO_END) + { + lpc43_tl_set_default_compare(curr); + } + else + { + bool set = lpc43_tl_set_calc_arm(curr, toSet, true); + if (!set) + { + lpc43_tl_alarm(curr); + } + } + } + else + { + lpc43_tl_alarm(curr); + } + } + else + { + lpc43_tl_set_default_compare(curr); + } + } + } - lpc43_tl_sync_down(); - - return OK; - } + lpc43_tl_sync_down(); + return OK; +} /**************************************************************************** * Public Functions ****************************************************************************/ -void up_timer_initialize (void) +void up_timer_initialize(void) { irqstate_t flags; - flags = irqsave (); + flags = irqsave(); ctrl_cache = getreg32(LPC43_RIT_CTRL); ctrl_cache &= ~RIT_CTRL_INT; /* set interrupt to 0*/ @@ -611,163 +613,163 @@ void up_timer_initialize (void) lpc43_tl_tick2ts(TO_END,&MAX_TS,false); - lpc43_tl_set_enable (false); + lpc43_tl_set_enable(false); - lpc43_tl_set_compare (UINT32_MAX); - lpc43_tl_set_counter (0); - lpc43_tl_set_mask (0); + lpc43_tl_set_compare(UINT32_MAX); + lpc43_tl_set_counter(0); + lpc43_tl_set_mask(0); - lpc43_tl_set_reset_on_match (false); - lpc43_tl_clear_interrupt (); + lpc43_tl_set_reset_on_match(false); + lpc43_tl_clear_interrupt(); - irq_attach (LPC43M4_IRQ_RITIMER, lpc43_tl_isr); - up_enable_irq (LPC43M4_IRQ_RITIMER); + irq_attach(LPC43M4_IRQ_RITIMER, lpc43_tl_isr); + up_enable_irq(LPC43M4_IRQ_RITIMER); - lpc43_tl_init_timer_vars (); + lpc43_tl_init_timer_vars(); - lpc43_tl_set_enable (true); + lpc43_tl_set_enable(true); - lpc43_tl_calibrate_init (); - - irqrestore (flags); + lpc43_tl_calibrate_init(); + irqrestore(flags); } +/* No reg changes, only processing */ -/* no reg changes, only processing */ -int up_timer_gettime (FAR struct timespec *ts) - { - lpc43_tl_sync_up(); +int up_timer_gettime(FAR struct timespec *ts) +{ + lpc43_tl_sync_up(); - /* order of calls is important, reset can come during processing */ + /* Order of calls is important, reset can come during processing */ - bool reset = lpc43_tl_get_interrupt (); - uint32_t count = lpc43_tl_get_counter (); + bool reset = lpc43_tl_get_interrupt(); + uint32_t count = lpc43_tl_get_counter(); - /* not processed reset can exist */ - if (lpc43_tl_get_reset_on_match ()) - { - bool resetAfter = lpc43_tl_get_interrupt (); + /* Not processed reset can exist */ - if (reset != resetAfter) /* was a reset during processing? get new counter */ - { - count = lpc43_tl_get_counter (); - } + if (lpc43_tl_get_reset_on_match()) + { + bool resetAfter = lpc43_tl_get_interrupt(); + + /* Was a reset during processing? get new counter */ + + if (reset != resetAfter) + { + count = lpc43_tl_get_counter(); + } if (resetAfter) - { - count += lpc43_tl_get_compare (); /* count should be smaller then UINT32_MAX-TO_END -> no overflow */ - } - } + { + /* Count should be smaller then UINT32_MAX-TO_END -> no overflow */ - struct timespec count_ts; - - - lpc43_tl_tick2ts(count + base_rest,&count_ts,false ); - - lpc43_tl_add(&base_ts,&count_ts,ts); - - lpc43_tl_sync_down(); - - return OK; - } - - -int up_alarm_cancel (FAR struct timespec *ts) - { - lpc43_tl_sync_up(); - - /*no reg changes, only variables logic*/ - - if ( ts != NULL ) { - up_timer_gettime (ts); + count += lpc43_tl_get_compare(); + } } - /* let default setup will be done in interrupt handler or up_alarm_start */ - lpc43_tl_init_timer_vars (); + struct timespec count_ts; - lpc43_tl_sync_down(); - return OK; - } + lpc43_tl_tick2ts(count + base_rest,&count_ts,false); -int up_alarm_start (FAR const struct timespec *ts) - { - lpc43_tl_sync_up(); + lpc43_tl_add(&base_ts,&count_ts,ts); - lpc43_tl_save_timer (false); + lpc43_tl_sync_down(); - lpc43_tl_init_timer_vars (); + return OK; +} - alarm_time_set = true; - alarm_time_ts.tv_sec = ts->tv_sec; - alarm_time_ts.tv_nsec = ts->tv_nsec; +int up_alarm_cancel(FAR struct timespec *ts) +{ + lpc43_tl_sync_up(); - uint32_t toSet = lpc43_tl_calc_to_set(); + /* No reg changes, only variables logic */ - uint32_t curr = lpc43_tl_get_counter (); + if (ts != NULL) + { + up_timer_gettime(ts); + } - if (toSet > curr) - { - if (toSet > TO_END) /* future set */ - { - lpc43_tl_set_default_compare (curr); - } - else - { - bool set = lpc43_tl_set_calc_arm (curr, toSet, true); - if (!set) /* signal call, force interrupt handler */ - { - call = true; - lpc43_tl_force_int (); - } + /* Let default setup will be done in interrupt handler or up_alarm_start */ - } - } - else /* signal call, force interrupt handler */ - { - call = true; - lpc43_tl_force_int (); - } + lpc43_tl_init_timer_vars(); - lpc43_tl_sync_down(); + lpc43_tl_sync_down(); + return OK; +} - return OK; - } +int up_alarm_start(FAR const struct timespec *ts) +{ + lpc43_tl_sync_up(); + + lpc43_tl_save_timer(false); + + lpc43_tl_init_timer_vars(); + + alarm_time_set = true; + alarm_time_ts.tv_sec = ts->tv_sec; + alarm_time_ts.tv_nsec = ts->tv_nsec; + + uint32_t toSet = lpc43_tl_calc_to_set(); + + uint32_t curr = lpc43_tl_get_counter(); + + if (toSet > curr) + { + if (toSet > TO_END) /* Future set */ + { + lpc43_tl_set_default_compare(curr); + } + else + { + bool set = lpc43_tl_set_calc_arm(curr, toSet, true); + if (!set) /* Signal call, force interrupt handler */ + { + call = true; + lpc43_tl_force_int(); + } + } + } + else /* Signal call, force interrupt handler */ + { + call = true; + lpc43_tl_force_int(); + } + + lpc43_tl_sync_down(); + + return OK; +} #ifndef CONFIG_SCHED_TICKLESS_ALARM - int up_timer_cancel(FAR struct timespec *ts) - { - lpc43_tl_sync_up(); +{ + lpc43_tl_sync_up(); - if (ts != NULL) - { - struct timespec abs_ts; - up_timer_gettime(&abs_ts); - lpc43_tl_sub( &alarm_time_ts,&abs_ts,ts ); - } + if (ts != NULL) + { + struct timespec abs_ts; + up_timer_gettime(&abs_ts); + lpc43_tl_sub(&alarm_time_ts,&abs_ts,ts); + } - lpc43_tl_init_timer_vars (); + lpc43_tl_init_timer_vars(); - lpc43_tl_sync_down(); - return OK; - } + lpc43_tl_sync_down(); + return OK; +} int up_timer_start(FAR const struct timespec *ts) - { +{ + lpc43_tl_sync_up(); - lpc43_tl_sync_up(); + struct timespec abs_ts; + up_timer_gettime(&abs_ts); + lpc43_tl_add(&abs_ts,ts,&abs_ts); - struct timespec abs_ts; - up_timer_gettime(&abs_ts); - lpc43_tl_add(&abs_ts,ts,&abs_ts); + up_alarm_start(&abs_ts); - up_alarm_start(&abs_ts); + lpc43_tl_sync_down(); + return OK; +} - lpc43_tl_sync_down(); - return OK; - } - -# endif /* CONFIG_SCHED_TICKLESS_ALARM */ - -#endif /* CONFIG_SCHED_TICKLESS */ +#endif /* CONFIG_SCHED_TICKLESS_ALARM */ +#endif /* CONFIG_SCHED_TICKLESS */