<commit_before>#include <unistd.h>
#include <bcm2835.h>
#include <sys/time.h>

#include "hardware.h"
#include "pins.h"

unsigned int hw_maxtracks = HW_MAXTRACKS;
unsigned int hw_currenttrack = 0;
unsigned int hw_currenthead = 0;
unsigned long hw_samplerate = 0;
float hw_rpm = HW_DEFAULTRPM;

int hw_stepping = HW_NORMALSTEPPING;

// Initialise GPIO and SPI
int hw_init(const int spiclockdivider)
{
  if (!bcm2835_init()) return 0;

  bcm2835_gpio_fsel(DS0_OUT, GPIO_OUT);
  bcm2835_gpio_clr(DS0_OUT);

  bcm2835_gpio_fsel(MOTOR_ON, GPIO_OUT);
  bcm2835_gpio_clr(MOTOR_ON);

  bcm2835_gpio_fsel(DIR_SEL, GPIO_OUT);
  bcm2835_gpio_clr(DIR_SEL);

  bcm2835_gpio_fsel(DIR_STEP, GPIO_OUT);
  bcm2835_gpio_clr(DIR_STEP);

  bcm2835_gpio_fsel(WRITE_GATE, GPIO_OUT);
  bcm2835_gpio_clr(WRITE_GATE);

  bcm2835_gpio_fsel(SIDE_SELECT, GPIO_OUT);
  bcm2835_gpio_clr(SIDE_SELECT);

  bcm2835_gpio_fsel(WRITE_PROTECT, GPIO_IN);
  bcm2835_gpio_set_pud(WRITE_PROTECT, PULL_UP);

  bcm2835_gpio_fsel(TRACK_0, GPIO_IN);
  bcm2835_gpio_set_pud(TRACK_0, PULL_UP);

  bcm2835_gpio_fsel(INDEX_PULSE, GPIO_IN);
  bcm2835_gpio_set_pud(INDEX_PULSE, PULL_UP);

  //bcm2835_gpio_fsel(READ_DATA, GPIO_IN);
  //bcm2835_gpio_set_pud(READ_DATA, PULL_UP);

  switch (spiclockdivider)
  {
    case HW_SPIDIV1024:
      // 244.140kHz on RPI1/2, 390.625kHz on RPI3
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024);
      break;

    case HW_SPIDIV512:
      // 488.281kHz on RPI1/2, 781.25kHz on RPI3
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_512);
      break;

    case HW_SPIDIV256:
      // 976.562kHz on RPI1/2, 1.562MHz on RPI3
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256);
      break;

    case HW_SPIDIV128:
      // 1.953MHz on RPI1/2, 3.125MHz on RPI3
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128);
      break;

    case HW_SPIDIV64:
      // 3.906MHz on RPI1/2, 6.250MHz on RPI3
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64);
      break;

    case HW_SPIDIV32:
      // 7.812MHz on RPI1/2, 12.5MHz on RPI3 - ** WORKS **
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32);
      break;

    case HW_SPIDIV16:
      // 15.625MHz on RPI1/2, 25MHz on RPI3 - ** WORKS **
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16);
      break;

    case HW_SPIDIV8:
      // 31.25MHz on RPI1/2, 50MHz on RPI3 - ** LESS RELIABLE **
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_8);
      break;

    case HW_SPIDIV4:
      // 62.5MHz on RPI1/2, 100MHz on RPI3 - ** UNRELIABLE, GIVES SPURIOUS VALUES  **
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_4);
      break;

    default:
      return 0;
      break;
  }

#if defined(HAS_BCM2837)
  hw_samplerate=HW_400MHZ/spiclockdivider; // 400MHz core
#elif defined(HAS_BCM2711)
  hw_samplerate=HW_500MHZ/spiclockdivider; // 500MHz core
#else
  hw_samplerate=HW_250MHZ/spiclockdivider; // 250MHz core
#endif

  bcm2835_spi_setDataMode(BCM2835_SPI_MODE2); // CPOL (Clock Polarity) = 1, CPHA (Clock Phase) = 0 

  bcm2835_spi_begin(); // sets all correct pin modes<commit_msg>Added RPI4 sampling frequencies<commit_after>#include <unistd.h>
#include <bcm2835.h>
#include <sys/time.h>

#include "hardware.h"
#include "pins.h"

unsigned int hw_maxtracks = HW_MAXTRACKS;
unsigned int hw_currenttrack = 0;
unsigned int hw_currenthead = 0;
unsigned long hw_samplerate = 0;
float hw_rpm = HW_DEFAULTRPM;

int hw_stepping = HW_NORMALSTEPPING;

// Initialise GPIO and SPI
int hw_init(const int spiclockdivider)
{
  if (!bcm2835_init()) return 0;

  bcm2835_gpio_fsel(DS0_OUT, GPIO_OUT);
  bcm2835_gpio_clr(DS0_OUT);

  bcm2835_gpio_fsel(MOTOR_ON, GPIO_OUT);
  bcm2835_gpio_clr(MOTOR_ON);

  bcm2835_gpio_fsel(DIR_SEL, GPIO_OUT);
  bcm2835_gpio_clr(DIR_SEL);

  bcm2835_gpio_fsel(DIR_STEP, GPIO_OUT);
  bcm2835_gpio_clr(DIR_STEP);

  bcm2835_gpio_fsel(WRITE_GATE, GPIO_OUT);
  bcm2835_gpio_clr(WRITE_GATE);

  bcm2835_gpio_fsel(SIDE_SELECT, GPIO_OUT);
  bcm2835_gpio_clr(SIDE_SELECT);

  bcm2835_gpio_fsel(WRITE_PROTECT, GPIO_IN);
  bcm2835_gpio_set_pud(WRITE_PROTECT, PULL_UP);

  bcm2835_gpio_fsel(TRACK_0, GPIO_IN);
  bcm2835_gpio_set_pud(TRACK_0, PULL_UP);

  bcm2835_gpio_fsel(INDEX_PULSE, GPIO_IN);
  bcm2835_gpio_set_pud(INDEX_PULSE, PULL_UP);

  //bcm2835_gpio_fsel(READ_DATA, GPIO_IN);
  //bcm2835_gpio_set_pud(READ_DATA, PULL_UP);

  switch (spiclockdivider)
  {
    case HW_SPIDIV1024:
      // 244.140kHz on RPI1/2, 390.625kHz on RPI3, 488.28125MHz on RPI4
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024);
      break;

    case HW_SPIDIV512:
      // 488.281kHz on RPI1/2, 781.25kHz on RPI3, 976.5625MHz on RPI4
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_512);
      break;

    case HW_SPIDIV256:
      // 976.562kHz on RPI1/2, 1.562MHz on RPI3, 1.953125MHz on RPI4
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256);
      break;

    case HW_SPIDIV128:
      // 1.953MHz on RPI1/2, 3.125MHz on RPI3, 3.90625MHz on RPI4
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128);
      break;

    case HW_SPIDIV64:
      // 3.906MHz on RPI1/2, 6.250MHz on RPI3, 7.8125MHz on RPI4
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64);
      break;

    case HW_SPIDIV32:
      // 7.812MHz on RPI1/2, 12.5MHz on RPI3, 15.625MHz on RPI4 - ** WORKS **
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32);
      break;

    case HW_SPIDIV16:
      // 15.625MHz on RPI1/2, 25MHz on RPI3, 31.25MHz on RPI4 - ** WORKS **
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16);
      break;

    case HW_SPIDIV8:
      // 31.25MHz on RPI1/2, 50MHz on RPI3, 62.5MHz on RPI4 - ** LESS RELIABLE **
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_8);
      break;

    case HW_SPIDIV4:
      // 62.5MHz on RPI1/2, 100MHz on RPI3, 125MHz on RPI4 - ** UNRELIABLE, GIVES SPURIOUS VALUES  **
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_4);
      break;

    default:
      return 0;
      break;
  }

#if defined(HAS_BCM2837)
  hw_samplerate=HW_400MHZ/spiclockdivider; // 400MHz core
#elif defined(HAS_BCM2711)
  hw_samplerate=HW_500MHZ/spiclockdivider; // 500MHz core
#else
  hw_samplerate=HW_250MHZ/spiclockdivider; // 250MHz core
#endif

  bcm2835_spi_setDataMode(BCM2835_SPI_MODE2); // CPOL (Clock Polarity) = 1, CPHA (Clock Phase) = 0 

  bcm2835_spi_begin(); // sets all correct pin modes
{'fraction_non_alphanumeric': 0.09217660728117738, 'fraction_numerical': 0.11866769945778466, 'mean_word_length': 4.039812646370024, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '607912', 'n_tokens_mistral': 3454, 'n_tokens_neox': 2878, 'n_words': 453}