<commit_before>/**
 * @cond internal
 * @file
 */
#include <stdint.h>
#include <math.h>
#include <avr/cpufunc.h>
#include <util/atomic.h>
#include <util/parity.h>
#include <clock/defs.h>
#include <util/delay.h>
#include "adxrs.h"
#include "adxrs_config.h"

#define T int16_t
#include "fifo.h"
#undef T

#if (CLOCK_PER_FREQ / ADXRS_SPI_PRESCALER) > 8080000
# error ADXRS_SPI_PRESCALER is too low, max ADXRS SPI frequency is 8.08MHz
#endif

// Get SPI to use and the interrupt vector

// Command to duplicate SPIC code for each SPI.
// Vimmers will use it with " :*! ".
//   python -c 'import sys; s=sys.stdin.read(); print "\n".join(s.replace("C",x) for x in "CDEF")'

#ifdef ADXRS_SPIC_ENABLE
# ifdef ADXRS_SPI
#  error Multiple ADXRS_SPIx_ENABLE defined can be defined
# endif
# define ADXRS_SPI  SPIC
# define ADXRS_SPI_INT_vect  SPIC_INT_vect
#endif

#ifdef ADXRS_SPID_ENABLE
# ifdef ADXRS_SPI
#  error Multiple ADXRS_SPIx_ENABLE defined can be defined
# endif
# define ADXRS_SPI  SPID
# define ADXRS_SPI_INT_vect  SPID_INT_vect
#endif

#ifdef ADXRS_SPIE_ENABLE
# ifdef ADXRS_SPI
#  error Multiple ADXRS_SPIx_ENABLE defined can be defined
# endif
# define ADXRS_SPI  SPIE
# define ADXRS_SPI_INT_vect  SPIE_INT_vect
#endif

#ifdef ADXRS_SPIF_ENABLE
# ifdef ADXRS_SPI
#  error Multiple ADXRS_SPIx_ENABLE defined can be defined
# endif
# define ADXRS_SPI  SPIF
# define ADXRS_SPI_INT_vect  SPIF_INT_vect
#endif

#ifndef ADXRS_SPI_INT_vect
# error No defined ADXRS_SPIx_ENABLE
#endif


#define CALIBRATION_SAMPLES_LENGTH 101

/** @brief ADXRS gyro data
 *
 * The \e response field contains data of the last received response, i.e. the
 * response to the penultimate command.
 * It is updated by \ref adxrs_parse_response().
 */
typedef struct {
  portpin_t cspp;  ///< port pin of the inversed CS pin
  adxrs_response_t response;  ///< response to the penultimate command
  float angle;  ///< current angle for capture mode
  float capture_scale;  ///< scaling coefficient for captured values
  uint8_t capture_index;  ///< index of next captured byte
  int16_t capture_speed;  ///< last (valid) captured angle speed

  struct {
    bool mode;  ///< calibration mode
    int16_t offset;  ///< calibration offset
    float offset_sqsd; ///< calibration squared sd

    float sum;
    float sqsum;
    fifo_t samples;
    int16_t samples_buffer[CALIBRATION_SAMPLES_LENGTH];
  } calibration;
} adxrs_t;


/// The gyro
static adxrs_t gyro;


void adxrs_init(portpin_t cspp)
{
  gyro.cspp = cspp;
  gyro.response.type = ADXRS_RESPONSE_NONE;
  gyro.angle = 0;
  gyro.calibration.mode = false;
  gyro.calibration.offset = 0;

  fifo_init(&gyro.calibration.samples,
    gyro.calibration.samples_buffer,
    CALIBRATION_SAMPLES_LENGTH);

  // initialize SPI
  portpin_dirset(&PORTPIN_SPI_SS(&ADXRS_SPI));
  ADXRS_SPI.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc |
#if ADXRS_SPI_PRESCALER == 2
      SPI_PRESCALER_DIV4_gc | SPI_CLK2X_bm
#elif ADXRS_SPI_PRESCALER == 4<commit_msg>ADXRS : fix to calibration process<commit_after>/**
 * @cond internal
 * @file
 */
#include <stdint.h>
#include <math.h>
#include <avr/cpufunc.h>
#include <util/atomic.h>
#include <util/parity.h>
#include <clock/defs.h>
#include <util/delay.h>
#include "adxrs.h"
#include "adxrs_config.h"

#define T int16_t
#include "fifo.h"
#undef T

#if (CLOCK_PER_FREQ / ADXRS_SPI_PRESCALER) > 8080000
# error ADXRS_SPI_PRESCALER is too low, max ADXRS SPI frequency is 8.08MHz
#endif

// Get SPI to use and the interrupt vector

// Command to duplicate SPIC code for each SPI.
// Vimmers will use it with " :*! ".
//   python -c 'import sys; s=sys.stdin.read(); print "\n".join(s.replace("C",x) for x in "CDEF")'

#ifdef ADXRS_SPIC_ENABLE
# ifdef ADXRS_SPI
#  error Multiple ADXRS_SPIx_ENABLE defined can be defined
# endif
# define ADXRS_SPI  SPIC
# define ADXRS_SPI_INT_vect  SPIC_INT_vect
#endif

#ifdef ADXRS_SPID_ENABLE
# ifdef ADXRS_SPI
#  error Multiple ADXRS_SPIx_ENABLE defined can be defined
# endif
# define ADXRS_SPI  SPID
# define ADXRS_SPI_INT_vect  SPID_INT_vect
#endif

#ifdef ADXRS_SPIE_ENABLE
# ifdef ADXRS_SPI
#  error Multiple ADXRS_SPIx_ENABLE defined can be defined
# endif
# define ADXRS_SPI  SPIE
# define ADXRS_SPI_INT_vect  SPIE_INT_vect
#endif

#ifdef ADXRS_SPIF_ENABLE
# ifdef ADXRS_SPI
#  error Multiple ADXRS_SPIx_ENABLE defined can be defined
# endif
# define ADXRS_SPI  SPIF
# define ADXRS_SPI_INT_vect  SPIF_INT_vect
#endif

#ifndef ADXRS_SPI_INT_vect
# error No defined ADXRS_SPIx_ENABLE
#endif


#define CALIBRATION_SAMPLES_LENGTH 101

/** @brief ADXRS gyro data
 *
 * The \e response field contains data of the last received response, i.e. the
 * response to the penultimate command.
 * It is updated by \ref adxrs_parse_response().
 */
typedef struct {
  portpin_t cspp;  ///< port pin of the inversed CS pin
  adxrs_response_t response;  ///< response to the penultimate command
  float angle;  ///< current angle for capture mode
  float capture_scale;  ///< scaling coefficient for captured values
  uint8_t capture_index;  ///< index of next captured byte
  int16_t capture_speed;  ///< last (valid) captured angle speed

  struct {
    bool mode;  ///< calibration mode
    int16_t offset;  ///< calibration offset
    float offset_sqsd; ///< calibration squared sd

    float sum;
    float sqsum;
    fifo_t samples;
    int16_t samples_buffer[CALIBRATION_SAMPLES_LENGTH];
  } calibration;
} adxrs_t;


/// The gyro
static adxrs_t gyro;


void adxrs_init(portpin_t cspp)
{
  gyro.cspp = cspp;
  gyro.response.type = ADXRS_RESPONSE_NONE;
  gyro.angle = 0;
  gyro.calibration.mode = false;
  gyro.calibration.offset = 0;
  gyro.calibration.offset_sqsd = +INFINITY;

  fifo_init(&gyro.calibration.samples,
    gyro.calibration.samples_buffer,
    CALIBRATION_SAMPLES_LENGTH);

  // initialize SPI
  portpin_dirset(&PORTPIN_SPI_SS(&ADXRS_SPI));
  ADXRS_SPI.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc |
#if ADXRS_SPI_PRESCALER == 2
      SPI_PRESCALER_DIV4_gc | SPI_CLK2X_bm
#elif ADXRS_SPI_PRESCALER == 4
{'fraction_non_alphanumeric': 0.09174006980222703, 'fraction_numerical': 0.009639355160378926, 'mean_word_length': 4.147989734816082, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 19, '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': '7209783', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2277, 'n_words': 599}