<commit_before>/** @file
    @brief Main implementation

    @date 2015

    @author
    Sensics, Inc.
    <http://sensics.com/osvr>
*/

/*
// Copyright 2015 Sensics, Inc.
//
// All rights reserved.
*/

/* Internal Includes */
#include "MCUConfig.h"

#include "array_init.h"
#include "uart_protocol.h"

/* Library/third-party includes */
#include "stm8s.h"

/* Standard includes */
#include "intrinsics.h" //__enable_interrupt ().

#if 0
#define BIT_CRCEN (1 << 5)
#define BIT_CRCNEXT (1 << 4)
#endif

#define MCU_CLOCK 16000000

uint8_t index_16 = 15;

/// @name Timer 1 values
/// @brief Units are microseconds. Total time for a flash process, roughly
/// _flash_period + 5 * _flash_blank_period + 5 *
/// _flash_interval_period
/// @{
#define MAX_FLASH_PERIOD 2000

/// Offset taken (reducing timer duration) to account for computational overhead
/// when setting flash period, to achieve correct duration validated by logic analyzer
#define MAX_FLASH_PERIOD_ADJUSTMENT 13
/// Offset taken (reducing timer duration) to account for computational overhead
/// when setting blank period, to achieve correct duration validated by logic analyzer
#define MAX_BLANK_PERIOD_ADJUSTMENT 14
/// Offset taken (reducing timer duration) to account for computational overhead
/// when setting interval period, to achieve correct duration validated by logic analyzer
#define MAX_INTERVAL_PERIOD_ADJUSTMENT 14

/// Additional overhead of the startup delay timer measured to be roughly 13.78usec
#define SYNC_TIMER_DELAY_ADJUSTMENT 14

#if FLASH_BRIGHT_PERIOD <= MAX_FLASH_PERIOD_ADJUSTMENT || FLASH_BRIGHT_PERIOD >= MAX_FLASH_PERIOD
#error "FLASH_BRIGHT_PERIOD out of range!"
#endif
#if FLASH_INTERVAL_PERIOD <= MAX_INTERVAL_PERIOD_ADJUSTMENT || FLASH_INTERVAL_PERIOD >= MAX_FLASH_PERIOD
#error "FLASH_INTERVAL_PERIOD out of range!"
#endif
#if FLASH_DIM_PERIOD <= MAX_BLANK_PERIOD_ADJUSTMENT || FLASH_DIM_PERIOD >= MAX_FLASH_PERIOD
#error "FLASH_DIM_PERIOD out of range!"
#endif

#if defined(SYNC_DELAY_TOTAL_US) && defined(SYNC_DELAY_TIMER)
#if SYNC_DELAY_TOTAL_US > MAX_FLASH_PERIOD
#error "Total sync delay exceeds maximum possible for SYNC_DELAY_TIMER mode!"
#endif
#endif

uint16_t _flash_blank_period    = FLASH_DIM_PERIOD;      // max 2000
uint16_t _flash_interval_period = FLASH_INTERVAL_PERIOD; // max 2000
uint16_t _flash_period          = FLASH_BRIGHT_PERIOD;   // max 2000

/// @}

static void Delay(uint16_t n)
{
  while (n--)
    ;
}

/// USEC_TO_TICKS
/// Based on how long the following loop is for each compiler:
/// while (n--) {}
#if defined(__ICCSTM8__)
  // this loop is ldw, decw, tnzw, jrne on IAR - so 5 (5.5?) cycles.
#define USEC_TO_TICKS(USEC) (((MCU_CLOCK * USEC / 1000000UL) - 5) / 5)
#else<commit_msg>Fix integer math.<commit_after>/** @file
    @brief Main implementation

    @date 2015

    @author
    Sensics, Inc.
    <http://sensics.com/osvr>
*/

/*
// Copyright 2015 Sensics, Inc.
//
// All rights reserved.
*/

/* Internal Includes */
#include "MCUConfig.h"

#include "array_init.h"
#include "uart_protocol.h"

/* Library/third-party includes */
#include "stm8s.h"

/* Standard includes */
#include "intrinsics.h" //__enable_interrupt ().

#if 0
#define BIT_CRCEN (1 << 5)
#define BIT_CRCNEXT (1 << 4)
#endif

#define MCU_CLOCK 16000000

uint8_t index_16 = 15;

/// @name Timer 1 values
/// @brief Units are microseconds. Total time for a flash process, roughly
/// _flash_period + 5 * _flash_blank_period + 5 *
/// _flash_interval_period
/// @{
#define MAX_FLASH_PERIOD 2000

/// Offset taken (reducing timer duration) to account for computational overhead
/// when setting flash period, to achieve correct duration validated by logic analyzer
#define MAX_FLASH_PERIOD_ADJUSTMENT 13
/// Offset taken (reducing timer duration) to account for computational overhead
/// when setting blank period, to achieve correct duration validated by logic analyzer
#define MAX_BLANK_PERIOD_ADJUSTMENT 14
/// Offset taken (reducing timer duration) to account for computational overhead
/// when setting interval period, to achieve correct duration validated by logic analyzer
#define MAX_INTERVAL_PERIOD_ADJUSTMENT 14

/// Additional overhead of the startup delay timer measured to be roughly 13.78usec
#define SYNC_TIMER_DELAY_ADJUSTMENT 14

#if FLASH_BRIGHT_PERIOD <= MAX_FLASH_PERIOD_ADJUSTMENT || FLASH_BRIGHT_PERIOD >= MAX_FLASH_PERIOD
#error "FLASH_BRIGHT_PERIOD out of range!"
#endif
#if FLASH_INTERVAL_PERIOD <= MAX_INTERVAL_PERIOD_ADJUSTMENT || FLASH_INTERVAL_PERIOD >= MAX_FLASH_PERIOD
#error "FLASH_INTERVAL_PERIOD out of range!"
#endif
#if FLASH_DIM_PERIOD <= MAX_BLANK_PERIOD_ADJUSTMENT || FLASH_DIM_PERIOD >= MAX_FLASH_PERIOD
#error "FLASH_DIM_PERIOD out of range!"
#endif

#if defined(SYNC_DELAY_TOTAL_US) && defined(SYNC_DELAY_TIMER)
#if SYNC_DELAY_TOTAL_US > MAX_FLASH_PERIOD
#error "Total sync delay exceeds maximum possible for SYNC_DELAY_TIMER mode!"
#endif
#endif

uint16_t _flash_blank_period    = FLASH_DIM_PERIOD;      // max 2000
uint16_t _flash_interval_period = FLASH_INTERVAL_PERIOD; // max 2000
uint16_t _flash_period          = FLASH_BRIGHT_PERIOD;   // max 2000

/// @}

static void Delay(uint16_t n)
{
  while (n--)
    ;
}

/// USEC_TO_TICKS
/// Based on how long the following loop is for each compiler:
/// while (n--) {}
#if defined(__ICCSTM8__)
  // this loop is ldw, decw, tnzw, jrne on IAR - so 5 (5.5?) cycles.
#define USEC_TO_TICKS(USEC) (((unsigned short)((MCU_CLOCK * (unsigned long)USEC) / 1000000UL) - 5) / 5)
#else
{'fraction_non_alphanumeric': 0.09998172180588558, 'fraction_numerical': 0.028879546700785963, 'mean_word_length': 4.3858267716535435, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, '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': '3454218', 'n_tokens_mistral': 2077, 'n_tokens_neox': 1891, 'n_words': 551}