<commit_before>#include <bc_spirit1.h>
#include <bc_scheduler.h>
#include <bc_exti.h>
#include <bc_system.h>
#include <bc_timer.h>
#include <stm32l0xx.h>
#include "SPIRIT_Config.h"
#include "SDK_Configuration_Common.h"
#include "MCU_Interface.h"

typedef enum
{
    BC_SPIRIT1_STATE_SLEEP = 0,
    BC_SPIRIT1_STATE_TX = 1,
    BC_SPIRIT1_STATE_RX = 2

} bc_spirit1_state_t;

typedef struct
{
    void (*event_handler)(bc_spirit1_event_t, void *);
    void *event_param;
    bc_scheduler_task_id_t task_id;
    bc_spirit1_state_t desired_state;
    bc_spirit1_state_t current_state;
    uint8_t tx_buffer[BC_SPIRIT1_MAX_PACKET_SIZE];
    size_t tx_length;
    uint8_t  rx_buffer[BC_SPIRIT1_MAX_PACKET_SIZE];
    size_t rx_length;
    bc_tick_t rx_timeout;
    bc_tick_t rx_tick_timeout;

} bc_spirit1_t;

static bc_spirit1_t _bc_spirit1;

#define XTAL_FREQUENCY 50000000

SRadioInit xRadioInit = {
  XTAL_OFFSET_PPM,
  BASE_FREQUENCY,
  CHANNEL_SPACE,
  CHANNEL_NUMBER,
  MODULATION_SELECT,
  DATARATE,
  FREQ_DEVIATION,
  BANDWIDTH
};

PktBasicInit xBasicInit={
  PREAMBLE_LENGTH,
  SYNC_LENGTH,
  SYNC_WORD,
  LENGTH_TYPE,
  LENGTH_WIDTH,
  CRC_MODE,
  CONTROL_LENGTH,
  EN_ADDRESS,
  EN_FEC,
  EN_WHITENING
};

PktBasicAddressesInit xAddressInit={
  EN_FILT_MY_ADDRESS,
  MY_ADDRESS,
  EN_FILT_MULTICAST_ADDRESS,
  MULTICAST_ADDRESS,
  EN_FILT_BROADCAST_ADDRESS,
  BROADCAST_ADDRESS
};

SGpioInit xGpioIRQ={
  SPIRIT_GPIO_0,
  SPIRIT_GPIO_MODE_DIGITAL_OUTPUT_LP,
  SPIRIT_GPIO_DIG_OUT_IRQ
};

static void _bc_spirit1_enter_state_tx(void);
static void _bc_spirit1_check_state_tx(void);
static void _bc_spirit1_enter_state_rx(void);
static void _bc_spirit1_check_state_rx(void);
static void _bc_spirit1_enter_state_sleep(void);

void bc_spirit1_hal_chip_select_low(void);
void bc_spirit1_hal_chip_select_high(void);
uint8_t bc_spirit1_hal_transfer_byte(uint8_t value);
static void bc_spirit1_hal_init_gpio(void);
static void bc_spirit1_hal_init_spi(void);

static void _bc_spirit1_task(void *param);
static void _bc_spirit1_interrupt(bc_exti_line_t line, void *param);

void bc_spirit1_init(void)
{
    memset(&_bc_spirit1, 0, sizeof(_bc_spirit1));

    SpiritRadioSetXtalFrequency(XTAL_FREQUENCY);
    SpiritSpiInit();

    /* Spirit ON */
    SpiritEnterShutdown();
    SpiritExitShutdown();
    SpiritManagementWaExtraCurrent();

    /* Spirit IRQ config */
    SpiritGpioInit(&xGpioIRQ);

    /* Spirit Radio config */
    SpiritRadioInit(&xRadioInit);

    /* Spirit Packet config */
    SpiritPktBasicInit(&xBasicInit);
    SpiritPktBasicAddressesInit(&xAddressInit);

    _bc_spirit1.task_id = bc_scheduler_register(_bc_spirit1_task, NULL, BC_TICK_INFINITY);

    _bc_spirit1_enter_state_sleep();
}

void bc_spirit1_set_event_handler(void (*event_handler)(bc_spirit1_event_t, void *), void *event_param)
{
    _bc_spirit1.event_handler = event_handler;
    _bc_spirit1.event_param = event_param;
}

void *bc_spirit1_get_tx_buffer(void)
{
    return _bc_spirit1.tx_buffer;
}

void bc_spirit1_set_tx_length(size_t length)
{
    _bc_spirit1.tx_length = length;<commit_msg>Fix spirit1 sleep after init<commit_after>#include <bc_spirit1.h>
#include <bc_scheduler.h>
#include <bc_exti.h>
#include <bc_system.h>
#include <bc_timer.h>
#include <stm32l0xx.h>
#include "SPIRIT_Config.h"
#include "SDK_Configuration_Common.h"
#include "MCU_Interface.h"

typedef enum
{
    BC_SPIRIT1_STATE_INIT = 0,
    BC_SPIRIT1_STATE_SLEEP = 1,
    BC_SPIRIT1_STATE_TX = 2,
    BC_SPIRIT1_STATE_RX = 3

} bc_spirit1_state_t;

typedef struct
{
    void (*event_handler)(bc_spirit1_event_t, void *);
    void *event_param;
    bc_scheduler_task_id_t task_id;
    bc_spirit1_state_t desired_state;
    bc_spirit1_state_t current_state;
    uint8_t tx_buffer[BC_SPIRIT1_MAX_PACKET_SIZE];
    size_t tx_length;
    uint8_t  rx_buffer[BC_SPIRIT1_MAX_PACKET_SIZE];
    size_t rx_length;
    bc_tick_t rx_timeout;
    bc_tick_t rx_tick_timeout;

} bc_spirit1_t;

static bc_spirit1_t _bc_spirit1;

#define XTAL_FREQUENCY 50000000

SRadioInit xRadioInit = {
  XTAL_OFFSET_PPM,
  BASE_FREQUENCY,
  CHANNEL_SPACE,
  CHANNEL_NUMBER,
  MODULATION_SELECT,
  DATARATE,
  FREQ_DEVIATION,
  BANDWIDTH
};

PktBasicInit xBasicInit={
  PREAMBLE_LENGTH,
  SYNC_LENGTH,
  SYNC_WORD,
  LENGTH_TYPE,
  LENGTH_WIDTH,
  CRC_MODE,
  CONTROL_LENGTH,
  EN_ADDRESS,
  EN_FEC,
  EN_WHITENING
};

PktBasicAddressesInit xAddressInit={
  EN_FILT_MY_ADDRESS,
  MY_ADDRESS,
  EN_FILT_MULTICAST_ADDRESS,
  MULTICAST_ADDRESS,
  EN_FILT_BROADCAST_ADDRESS,
  BROADCAST_ADDRESS
};

SGpioInit xGpioIRQ={
  SPIRIT_GPIO_0,
  SPIRIT_GPIO_MODE_DIGITAL_OUTPUT_LP,
  SPIRIT_GPIO_DIG_OUT_IRQ
};

static void _bc_spirit1_enter_state_tx(void);
static void _bc_spirit1_check_state_tx(void);
static void _bc_spirit1_enter_state_rx(void);
static void _bc_spirit1_check_state_rx(void);
static void _bc_spirit1_enter_state_sleep(void);

void bc_spirit1_hal_chip_select_low(void);
void bc_spirit1_hal_chip_select_high(void);
uint8_t bc_spirit1_hal_transfer_byte(uint8_t value);
static void bc_spirit1_hal_init_gpio(void);
static void bc_spirit1_hal_init_spi(void);

static void _bc_spirit1_task(void *param);
static void _bc_spirit1_interrupt(bc_exti_line_t line, void *param);

void bc_spirit1_init(void)
{
    memset(&_bc_spirit1, 0, sizeof(_bc_spirit1));

    SpiritRadioSetXtalFrequency(XTAL_FREQUENCY);
    SpiritSpiInit();

    /* Spirit ON */
    SpiritEnterShutdown();
    SpiritExitShutdown();
    SpiritManagementWaExtraCurrent();

    /* Spirit IRQ config */
    SpiritGpioInit(&xGpioIRQ);

    /* Spirit Radio config */
    SpiritRadioInit(&xRadioInit);

    /* Spirit Packet config */
    SpiritPktBasicInit(&xBasicInit);
    SpiritPktBasicAddressesInit(&xAddressInit);

    _bc_spirit1.desired_state = BC_SPIRIT1_STATE_SLEEP;

    _bc_spirit1.task_id = bc_scheduler_register(_bc_spirit1_task, NULL, 0);
}

void bc_spirit1_set_event_handler(void (*event_handler)(bc_spirit1_event_t, void *), void *event_param)
{
    _bc_spirit1.event_handler = event_handler;
    _bc_spirit1.event_param = event_param;
}

void *bc_spirit1_get_tx_buffer(void)
{
    return _bc_spirit1.tx_buffer;
}

void bc_spirit1_set_tx_length(size_t length)
{
    _bc_spirit1.tx_length = length;
{'fraction_non_alphanumeric': 0.08223153821347952, 'fraction_numerical': 0.01983231215736859, 'mean_word_length': 5.764449291166849, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '192901', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2506, 'n_words': 368}