<commit_before>/*
 * Copyright (C) 2014 INRIA
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup cpu
 * @{
 */

/**
 * @file
 * @brief       lpc iap  driver
 * @note        based on iap driver for LPC2148 Controller made by <NAME>, 2008
 *
 * @author      <NAME> <<EMAIL>>
 *
 */

#include <stddef.h>
/* iap driver
 *
 *
 */

#include "irq.h"
#include "flashrom.h"
#include "iap.h"
/* TODO: replace by a lpc generic header */
#include "lpc2387.h"

#define ENABLE_DEBUG    (0)
#include "debug.h"

/* pointer to reserved flash rom section for configuration data */
__attribute((aligned(256))) char configmem[256] __attribute__((section(".configmem")));

static unsigned int iap_command[5];         // contains parameters for IAP command
static unsigned int iap_result[2];          // contains results
typedef void (*IAP)(unsigned int[], unsigned int[]);    // typedefinition for IAP entry function
IAP IAP_Entry;

/* some function prototypes */
static uint32_t blank_check_sector(uint32_t tmp_sect1, uint32_t tmp_sect2);
static uint32_t prepare_sectors(uint32_t tmp_sect1, uint32_t tmp_sect2);
static uint32_t erase_sectors(uint32_t tmp_sect1, uint32_t tmp_sect2);
static uint32_t copy_ram_to_flash(uint32_t tmp_adr_dst, uint32_t tmp_adr_src, uint32_t tmp_size);
static uint32_t compare(uint32_t tmp_adr_dst, uint32_t tmp_adr_src, uint32_t tmp_size);
static uint32_t iap(uint32_t code, uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);

/******************************************************************************
 * P U B L I C   F U N C T I O N S
 *****************************************************************************/
uint8_t flashrom_write(uint8_t *dst, const uint8_t *src, size_t size)
{
    (void) size; /* unused */

    char err;
    unsigned intstate;
    uint8_t sec;

    sec = iap_get_sector((uint32_t) dst);

    if (sec == INVALID_ADDRESS) {
        DEBUG("Invalid address\n");
        return 0;
    }

    /* check sector */
    if (blank_check_sector(sec, sec) == SECTOR_NOT_BLANK) {
        DEBUG("Warning: Sector %i not blank\n", sec);
    }

    /* prepare sector */
    err = prepare_sectors(sec, sec);

    if (err) {
        DEBUG("\n-- ERROR: PREPARE_SECTOR_FOR_WRITE_OPERATION: %u\n", err);
        return 0;
    }
    /* write flash */
    else {
        intstate = disableIRQ();
        err = copy_ram_to_flash((uint32_t) dst, (uint32_t) src, 256);
        restoreIRQ(intstate);

        if (err) {
            DEBUG("ERROR: COPY_RAM_TO_FLASH: %u\n", err);
            /* set interrupts back and return */
            restoreIRQ(intstate);
            return 0;
        }
        /* check result */
        else {
            err = compare((uint32_t) dst, (uint32_t) src, 256);

            if (err) {
                DEBUG("ERROR: COMPARE: %i (at position %u)\n", err, iap_result[1]);
                return 0;
            }
            else {
                DEBUG("Data successfully written!\n");
                return 1;
            }
        }
    }
}
<commit_msg>cpu: lpc_common: reduce indentation level in iap.c<commit_after>/*
 * Copyright (C) 2014 INRIA
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup cpu
 * @{
 */

/**
 * @file
 * @brief       lpc iap  driver
 * @note        based on iap driver for LPC2148 Controller made by <NAME>, 2008
 *
 * @author      <NAME> <<EMAIL>>
 *
 */

#include <stddef.h>
/* iap driver
 *
 *
 */

#include "irq.h"
#include "flashrom.h"
#include "iap.h"
/* TODO: replace by a lpc generic header */
#include "lpc2387.h"

#define ENABLE_DEBUG    (0)
#include "debug.h"

/* pointer to reserved flash rom section for configuration data */
__attribute((aligned(256))) char configmem[256] __attribute__((section(".configmem")));

static unsigned int iap_command[5];         // contains parameters for IAP command
static unsigned int iap_result[2];          // contains results
typedef void (*IAP)(unsigned int[], unsigned int[]);    // typedefinition for IAP entry function
IAP IAP_Entry;

/* some function prototypes */
static uint32_t blank_check_sector(uint32_t tmp_sect1, uint32_t tmp_sect2);
static uint32_t prepare_sectors(uint32_t tmp_sect1, uint32_t tmp_sect2);
static uint32_t erase_sectors(uint32_t tmp_sect1, uint32_t tmp_sect2);
static uint32_t copy_ram_to_flash(uint32_t tmp_adr_dst, uint32_t tmp_adr_src, uint32_t tmp_size);
static uint32_t compare(uint32_t tmp_adr_dst, uint32_t tmp_adr_src, uint32_t tmp_size);
static uint32_t iap(uint32_t code, uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);

/******************************************************************************
 * P U B L I C   F U N C T I O N S
 *****************************************************************************/
uint8_t flashrom_write(uint8_t *dst, const uint8_t *src, size_t size)
{
    (void) size; /* unused */

    char err;
    uint8_t sec;

    sec = iap_get_sector((uint32_t) dst);

    if (sec == INVALID_ADDRESS) {
        DEBUG("Invalid address\n");
        return 0;
    }

    /* check sector */
    if (blank_check_sector(sec, sec) == SECTOR_NOT_BLANK) {
        DEBUG("Warning: Sector %i not blank\n", sec);
    }

    /* prepare sector */
    err = prepare_sectors(sec, sec);
    if (err) {
        DEBUG("\n-- ERROR: PREPARE_SECTOR_FOR_WRITE_OPERATION: %u\n", err);
        return 0;
    }

    /*  write flash */
    unsigned intstate = disableIRQ();
    err = copy_ram_to_flash((uint32_t) dst, (uint32_t) src, 256);
    restoreIRQ(intstate);

    if (err) {
        DEBUG("ERROR: COPY_RAM_TO_FLASH: %u\n", err);
        /* set interrupts back and return */
        restoreIRQ(intstate);
        return 0;
    }
    /* check result */
    err = compare((uint32_t) dst, (uint32_t) src, 256);

    if (err) {
        DEBUG("ERROR: COMPARE: %i (at position %u)\n", err, iap_result[1]);
        return 0;
    }

    DEBUG("Data successfully written!\n");
    return 1;
}

{'fraction_non_alphanumeric': 0.1632520325203252, 'fraction_numerical': 0.035447154471544715, 'mean_word_length': 3.025523560209424, 'pattern_counts': {'":': 0, '<': 13, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6232862', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2018, 'n_words': 584}