<commit_before>/*
 * Copyright (c) 2013 <NAME>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef AMBROLIB_AVR_CLOCK_H
#define AMBROLIB_AVR_CLOCK_H

#include <stdint.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sfr_defs.h>

#include <aprinter/base/DebugObject.h>
#include <aprinter/base/Assert.h>
#include <aprinter/base/Lock.h>
#include <aprinter/base/Likely.h>
#include <aprinter/system/AvrLock.h>
#include <aprinter/system/AvrIo.h>

#include <aprinter/BeginNamespace.h>

template <typename Context, int Prescale>
class AvrClock
: private DebugObject<Context, AvrClock<Context, Prescale>>
{
    static_assert(Prescale >= 1, "Prescale must be >=1");
    static_assert(Prescale <= 5, "Prescale must be <=5");
    
public:
    typedef uint32_t TimeType;
    
public:
    static constexpr TimeType prescale_divide =
        (Prescale == 1) ? 1 :
        (Prescale == 2) ? 8 :
        (Prescale == 3) ? 64 :
        (Prescale == 4) ? 256 :
        (Prescale == 5) ? 1024 : 0;
    
    static constexpr double time_unit = (double)prescale_divide / F_CPU;
    static constexpr double time_freq = 1.0 / time_unit;
    
    void init (Context c)
    {
        m_offset = 0;
        TIMSK1 = 0;
        TCCR1A = 0;
        TCCR1B = (uint16_t)Prescale;
        TIMSK1 = (1 << TOIE1);
        this->debugInit(c);
    }
    
    void deinit (Context c)
    {
        this->debugDeinit(c);
        TIMSK1 = 0;
        TCCR1B = 0;
    }
    
    template <typename ThisContext>
    TimeType getTime (ThisContext c)
    {
        this->debugAccess(c);
        
        uint16_t now_high;
        uint16_t now_low;
        
        AMBRO_LOCK_T(AvrTempLock(), c, lock_c, {
            now_high = m_offset;
            uint8_t tmp;
            asm volatile (
                "    lds %A[now_low],%[tcnt1]+0\n"
                "    lds %B[now_low],%[tcnt1]+1\n"
                "    in %[tmp],%[tifr1]\n"
                "    andi %[tmp],1<<%[tov1]\n"
                "    breq no_overflow_%=\n"
                "    subi %A[now_high],-1\n"
                "    sbci %B[now_high],-1\n"
                "    lds %A[now_low],%[tcnt1]+0\n"
                "    lds %B[now_low],%[tcnt1]+1\n"
                "no_overflow_%=:\n"
            : [now_low] "=&r" (now_low),
            [now_high] "=&d" (now_high),
            [tmp] "=&d" (tmp)
            : "[now_high]" (now_high),
            [tcnt1] "n" (_SFR_MEM_ADDR(TCNT1)),
            [tifr1] "I" (_SFR_IO_ADDR(TIFR1)),
            [tov1] "n" (TOV1)
            );
        });
        
        return ((uint32_t)now_high << 16) | now_low;
    }
    
#ifdef TCNT3
    void initTC3 (Context c)
    {
        this->debugAccess(c);
        
        TIMSK3 = 0;
        TCCR3A = 0;
        TCCR3B = (uint16_t)Prescale;
        TCNT3 = TCNT1 - 1;
    }
    
    void deinitTC3 (Context c)
    {
        this->debugAccess(c);
        
        TIMSK3 = 0;
        TCCR3B = 0;
    }
#endif

#ifdef TCNT0
    static const int TC0Prescale =
        (Prescale == 1) ? 1 :
        (Prescale == 2) ? 2 :
        (Prescale == 3) ? 4 :
        (Prescale == 4) ? 6 :
        (Prescale == 5) ? 7 : 0;
    
    void initTC0 (Context c)
    {
        this->debugAccess(c);
        
        TIMSK0 = 0;
        TCCR0A = 0;
        TCCR0B = TC0Prescale;
        TCNT0 = TCNT1 - 1;
    }
    
    void deinitTC0 (Context c)
    {
        this->debugAccess(c);
        
        TIMSK0 = 0;
        TCCR0B = 0;
    }
#endif
    
#ifdef TCNT2
    static const int TC2Prescale =<commit_msg>AvrClock: fix T/C0 prescaler. The datasheet is a little confusing here, referring you to T/C2 for prescaler information even though the T/C2 prescaler table does not apply to T/C0.<commit_after>/*
 * Copyright (c) 2013 <NAME>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef AMBROLIB_AVR_CLOCK_H
#define AMBROLIB_AVR_CLOCK_H

#include <stdint.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sfr_defs.h>

#include <aprinter/base/DebugObject.h>
#include <aprinter/base/Assert.h>
#include <aprinter/base/Lock.h>
#include <aprinter/base/Likely.h>
#include <aprinter/system/AvrLock.h>
#include <aprinter/system/AvrIo.h>

#include <aprinter/BeginNamespace.h>

template <typename Context, int Prescale>
class AvrClock
: private DebugObject<Context, AvrClock<Context, Prescale>>
{
    static_assert(Prescale >= 1, "Prescale must be >=1");
    static_assert(Prescale <= 5, "Prescale must be <=5");
    
public:
    typedef uint32_t TimeType;
    
public:
    static constexpr TimeType prescale_divide =
        (Prescale == 1) ? 1 :
        (Prescale == 2) ? 8 :
        (Prescale == 3) ? 64 :
        (Prescale == 4) ? 256 :
        (Prescale == 5) ? 1024 : 0;
    
    static constexpr double time_unit = (double)prescale_divide / F_CPU;
    static constexpr double time_freq = 1.0 / time_unit;
    
    void init (Context c)
    {
        m_offset = 0;
        TIMSK1 = 0;
        TCCR1A = 0;
        TCCR1B = (uint16_t)Prescale;
        TIMSK1 = (1 << TOIE1);
        this->debugInit(c);
    }
    
    void deinit (Context c)
    {
        this->debugDeinit(c);
        TIMSK1 = 0;
        TCCR1B = 0;
    }
    
    template <typename ThisContext>
    TimeType getTime (ThisContext c)
    {
        this->debugAccess(c);
        
        uint16_t now_high;
        uint16_t now_low;
        
        AMBRO_LOCK_T(AvrTempLock(), c, lock_c, {
            now_high = m_offset;
            uint8_t tmp;
            asm volatile (
                "    lds %A[now_low],%[tcnt1]+0\n"
                "    lds %B[now_low],%[tcnt1]+1\n"
                "    in %[tmp],%[tifr1]\n"
                "    andi %[tmp],1<<%[tov1]\n"
                "    breq no_overflow_%=\n"
                "    subi %A[now_high],-1\n"
                "    sbci %B[now_high],-1\n"
                "    lds %A[now_low],%[tcnt1]+0\n"
                "    lds %B[now_low],%[tcnt1]+1\n"
                "no_overflow_%=:\n"
            : [now_low] "=&r" (now_low),
            [now_high] "=&d" (now_high),
            [tmp] "=&d" (tmp)
            : "[now_high]" (now_high),
            [tcnt1] "n" (_SFR_MEM_ADDR(TCNT1)),
            [tifr1] "I" (_SFR_IO_ADDR(TIFR1)),
            [tov1] "n" (TOV1)
            );
        });
        
        return ((uint32_t)now_high << 16) | now_low;
    }
    
#ifdef TCNT3
    void initTC3 (Context c)
    {
        this->debugAccess(c);
        
        TIMSK3 = 0;
        TCCR3A = 0;
        TCCR3B = (uint16_t)Prescale;
        TCNT3 = TCNT1 - 1;
    }
    
    void deinitTC3 (Context c)
    {
        this->debugAccess(c);
        
        TIMSK3 = 0;
        TCCR3B = 0;
    }
#endif

#ifdef TCNT0
    static const int TC0Prescale = Prescale;
    
    void initTC0 (Context c)
    {
        this->debugAccess(c);
        
        TIMSK0 = 0;
        TCCR0A = 0;
        TCCR0B = TC0Prescale;
        TCNT0 = TCNT1 - 1;
    }
    
    void deinitTC0 (Context c)
    {
        this->debugAccess(c);
        
        TIMSK0 = 0;
        TCCR0B = 0;
    }
#endif
    
#ifdef TCNT2
    static const int TC2Prescale =
{'fraction_non_alphanumeric': 0.10941741490916684, 'fraction_numerical': 0.02453539361035707, 'mean_word_length': 2.135515548281506, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6313168', 'n_tokens_mistral': 3537, 'n_tokens_neox': 3178, 'n_words': 899}