<commit_before>/******************************************************************************

    Application entry point.

    Contains the main loop and the hardware initialization.

******************************************************************************/
#include <stdio.h>
#include <stdbool.h>

#include <platform.h>
#include <uart0.h>
#include <spi.h>
#include <rc_receiver.h>
#include <preprocessor_output.h>

#include <LPC8xx_ROM_API.h>


#ifndef BAUDRATE
    #define BAUDRATE 115200
#endif


void SysTick_handler(void);
void PININT0_irq_handler(void);
void SCT_irq_handler(void);
void MRT_irq_handler(void);



// Global flag that is true for one mainloop every __SYSTICK_IN_MS
bool systick;

static volatile uint32_t systick_count;



// ****************************************************************************
static void feed_the_watchdog(void)
{
    LPC_WWDT->FEED = 0xaa;
    LPC_WWDT->FEED = 0x55;
}


// ****************************************************************************
static void service_systick(void)
{
    if (!systick_count) {
        systick = false;
        return;
    }

    systick = true;

    // Disable the SysTick interrupt. Use memory barriers to ensure that no
    // interrupt is pending in the pipeline.
    // More info:
    // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0321a/BIHHFHJD.html
    SysTick->CTRL &= ~(1 << 1);
    __DSB();
    __ISB();
    --systick_count;
    SysTick->CTRL |= (1 << 1);      // Re-enable the system tick interrupt
}


// ****************************************************************************
static void stack_check(void)
{
#ifndef NO_DEBUG
    #define CANARY 0xcafebabe

    static uint32_t *last_found = (uint32_t *)(0x10001000 - 48);
    uint32_t *now;

    if (last_found == (uint32_t *)0x10000000) {
        return;
    }

    now = last_found;
    while (*now != CANARY && now > (uint32_t *)0x10000000) {
        --now;
    }

    if (now != last_found) {
        last_found = now;
        uart0_send_cstring("Stack down to 0x");
        uart0_send_uint32_hex((uint32_t)now);
        uart0_send_linefeed();
    }
#endif
}


// ****************************************************************************
static void init_hardware(void)
{
    int i;

#if __SYSTEM_CLOCK != 12000000
#error Clock initialization code expexts __SYSTEM_CLOCK to be set to 1200000
#endif

#ifndef USE_IRC
    LPC_SYSCON->PDRUNCFG &= ~(1 << 7);          // Enable the system PLL

    LPC_SYSCON->SYSPLLCLKSEL = 0x1;             // PLL input is the crystal oscillator
    LPC_SYSCON->SYSPLLCLKUEN = 0;               // Toggle PLL-enable
    LPC_SYSCON->SYSPLLCLKUEN = 1;

    // Set the PLL to 16Mhz * 3 / 4 = 12 Mhz
    LPC_SYSCON->SYSPLLCTRL = (0x2 << 0) |       // M = 3
                             (0x2 << 5);        // P = 4

    while (!(LPC_SYSCON->SYSPLLSTAT & 1)) {     // Wait for PLL lock
        ;
    }

    LPC_SYSCON->MAINCLKSEL = 0x3;               // Use the PLL clock output as main clock
    LPC_SYSCON->MAINCLKUEN = 0;                 // Toggle CLK-enable
    LPC_SYSCON->MAINCLKUEN = 1;
#endif

    // Set flash wait-states to 1 system clock
    LPC_FLASHCTRL->FLASHCFG = 0;


    // ------------------------
    // Turn on peripheral clocks for SCTimer, IOCON, SPI0, MRT, WWDT
    // (GPIO, SWM alrady enabled after reset)
    LPC_SYSCON->SYSAHBCLKCTRL |=
        (1 << 18) | (1 << 17) | (1 << 11) | (1 << 10) | (1 << 8);


    // ------------------------
    // Turn on brown-out detection and reset
    LPC_SYSCON->BODCTRL = (1 << 4) | (1 << 2) | (2 << 0);


    // ------------------------
    // IO configuration

    // Enable reset, all other special functions disabled
    LPC_SWM->PINENABLE0 = 0xffffffbf;

    // Enable hardware inputs and outputs
    LPC_SWM->PINASSIGN0 = (0xff << 24) |
                          (0xff << 16) |
                          (0xff << 8) |                 // UART0_RX
                          (GPIO_BIT_UART_TX << 0);      // UART0_TX

    LPC_SWM->PINASSIGN3 = (GPIO_BIT_NRF_SCK << 24) |    // SPI0_SCK
                          (0xff << 16) |
                          (0xff << 8) |<commit_msg>Change brown-out reset level to 2.8V<commit_after>/******************************************************************************

    Application entry point.

    Contains the main loop and the hardware initialization.

******************************************************************************/
#include <stdio.h>
#include <stdbool.h>

#include <platform.h>
#include <uart0.h>
#include <spi.h>
#include <rc_receiver.h>
#include <preprocessor_output.h>

#include <LPC8xx_ROM_API.h>


#ifndef BAUDRATE
    #define BAUDRATE 115200
#endif


void SysTick_handler(void);
void PININT0_irq_handler(void);
void SCT_irq_handler(void);
void MRT_irq_handler(void);



// Global flag that is true for one mainloop every __SYSTICK_IN_MS
bool systick;

static volatile uint32_t systick_count;



// ****************************************************************************
static void feed_the_watchdog(void)
{
    LPC_WWDT->FEED = 0xaa;
    LPC_WWDT->FEED = 0x55;
}


// ****************************************************************************
static void service_systick(void)
{
    if (!systick_count) {
        systick = false;
        return;
    }

    systick = true;

    // Disable the SysTick interrupt. Use memory barriers to ensure that no
    // interrupt is pending in the pipeline.
    // More info:
    // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0321a/BIHHFHJD.html
    SysTick->CTRL &= ~(1 << 1);
    __DSB();
    __ISB();
    --systick_count;
    SysTick->CTRL |= (1 << 1);      // Re-enable the system tick interrupt
}


// ****************************************************************************
static void stack_check(void)
{
#ifndef NO_DEBUG
    #define CANARY 0xcafebabe

    static uint32_t *last_found = (uint32_t *)(0x10001000 - 48);
    uint32_t *now;

    if (last_found == (uint32_t *)0x10000000) {
        return;
    }

    now = last_found;
    while (*now != CANARY && now > (uint32_t *)0x10000000) {
        --now;
    }

    if (now != last_found) {
        last_found = now;
        uart0_send_cstring("Stack down to 0x");
        uart0_send_uint32_hex((uint32_t)now);
        uart0_send_linefeed();
    }
#endif
}


// ****************************************************************************
static void init_hardware(void)
{
    int i;

#if __SYSTEM_CLOCK != 12000000
#error Clock initialization code expexts __SYSTEM_CLOCK to be set to 1200000
#endif

#ifndef USE_IRC
    LPC_SYSCON->PDRUNCFG &= ~(1 << 7);          // Enable the system PLL

    LPC_SYSCON->SYSPLLCLKSEL = 0x1;             // PLL input is the crystal oscillator
    LPC_SYSCON->SYSPLLCLKUEN = 0;               // Toggle PLL-enable
    LPC_SYSCON->SYSPLLCLKUEN = 1;

    // Set the PLL to 16Mhz * 3 / 4 = 12 Mhz
    LPC_SYSCON->SYSPLLCTRL = (0x2 << 0) |       // M = 3
                             (0x2 << 5);        // P = 4

    while (!(LPC_SYSCON->SYSPLLSTAT & 1)) {     // Wait for PLL lock
        ;
    }

    LPC_SYSCON->MAINCLKSEL = 0x3;               // Use the PLL clock output as main clock
    LPC_SYSCON->MAINCLKUEN = 0;                 // Toggle CLK-enable
    LPC_SYSCON->MAINCLKUEN = 1;
#endif

    // Set flash wait-states to 1 system clock
    LPC_FLASHCTRL->FLASHCFG = 0;


    // ------------------------
    // Turn on peripheral clocks for SCTimer, IOCON, SPI0, MRT, WWDT
    // (GPIO, SWM alrady enabled after reset)
    LPC_SYSCON->SYSAHBCLKCTRL |=
        (1 << 18) | (1 << 17) | (1 << 11) | (1 << 10) | (1 << 8);


    // ------------------------
    // Turn on brown-out detection and reset
    LPC_SYSCON->BODCTRL = (1 << 4) | (1 << 2) | (3 << 0);


    // ------------------------
    // IO configuration

    // Enable reset, all other special functions disabled
    LPC_SWM->PINENABLE0 = 0xffffffbf;

    // Enable hardware inputs and outputs
    LPC_SWM->PINASSIGN0 = (0xff << 24) |
                          (0xff << 16) |
                          (0xff << 8) |                 // UART0_RX
                          (GPIO_BIT_UART_TX << 0);      // UART0_TX

    LPC_SWM->PINASSIGN3 = (GPIO_BIT_NRF_SCK << 24) |    // SPI0_SCK
                          (0xff << 16) |
                          (0xff << 8) |
{'fraction_non_alphanumeric': 0.234249369974799, 'fraction_numerical': 0.03792151686067443, 'mean_word_length': 2.315035799522673, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5094839', 'n_tokens_mistral': 2956, 'n_tokens_neox': 2630, 'n_words': 645}