<commit_before>#include "radio.h"

#include <assert.h>
#include <stdint.h>
#include <string.h>

#include "error.h"
#include "packet_queue.h"

#include "nrf51.h"
#include "nrf51_bitfields.h"

typedef enum
{
    IDLE,
    RX_PACKET_RECEIVE,
    RX_ACK_SEND,
    TX_PACKET_SEND,
    TX_ACK_RECEIVE,
} radio_state_t;

volatile static radio_state_t m_state;

static radio_evt_handler_t * m_evt_handler;

static packet_queue_t m_tx_queue;
static packet_queue_t m_rx_queue;
static radio_packet_t m_rx_packet;

void RADIO_IRQHandler(void)
{
    uint32_t err_code;

    if((NRF_RADIO->EVENTS_END == 1) && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk))
    {
        NRF_RADIO->EVENTS_END = 0;

        switch (m_state)
        {
            case TX_PACKET_SEND:
                m_state = TX_ACK_RECEIVE;
                break;

            case TX_ACK_RECEIVE:
                m_state = IDLE;
                break;

            case RX_PACKET_RECEIVE:
                m_state = RX_ACK_SEND;
                break;

            case RX_ACK_SEND:
                m_state = RX_PACKET_RECEIVE;
                break;

            case IDLE:
                break;
        }

        if (m_state == RX_PACKET_RECEIVE)
        {
            radio_packet_t * tx_packet;
            err_code = packet_queue_get(&m_tx_queue, &tx_packet);
            assert(err_code == SUCCESS);

            tx_packet->flags.ack = 1;

            NRF_RADIO->PACKETPTR = (uint32_t) tx_packet;

            err_code = packet_queue_add(&m_rx_queue, &m_rx_packet);
            assert(err_code == SUCCESS);

            radio_evt_t evt;
            evt.type = PACKET_RECEIVED;
            err_code = packet_queue_get(&m_rx_queue, &evt.packet);
            assert(err_code == SUCCESS);

            (*m_evt_handler)(&evt);
        }
        else
        {
            NRF_RADIO->PACKETPTR = (uint32_t) &m_rx_packet;
        }
    }
    if ((NRF_RADIO->EVENTS_DISABLED == 1) && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk))
    {
        NRF_RADIO->EVENTS_DISABLED = 0; 

        switch (m_state)
        {
            case TX_PACKET_SEND:
            case RX_ACK_SEND:
                NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos |
                    RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos |
                    RADIO_SHORTS_DISABLED_RXEN_Enabled << RADIO_SHORTS_DISABLED_RXEN_Pos;
                break;

            case RX_PACKET_RECEIVE:
                NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos |
                    RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos |
                    RADIO_SHORTS_DISABLED_TXEN_Enabled << RADIO_SHORTS_DISABLED_TXEN_Pos;
                break;

            case TX_ACK_RECEIVE:
                NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos |<commit_msg>Use error handling framework in radio.c<commit_after>#include "radio.h"

#include <stdint.h>
#include <string.h>

#include "error.h"
#include "packet_queue.h"

#include "nrf51.h"
#include "nrf51_bitfields.h"

typedef enum
{
    IDLE,
    RX_PACKET_RECEIVE,
    RX_ACK_SEND,
    TX_PACKET_SEND,
    TX_ACK_RECEIVE,
} radio_state_t;

volatile static radio_state_t m_state;

static radio_evt_handler_t * m_evt_handler;

static packet_queue_t m_tx_queue;
static packet_queue_t m_rx_queue;
static radio_packet_t m_rx_packet;

void RADIO_IRQHandler(void)
{
    uint32_t err_code;

    if((NRF_RADIO->EVENTS_END == 1) && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk))
    {
        NRF_RADIO->EVENTS_END = 0;

        switch (m_state)
        {
            case TX_PACKET_SEND:
                m_state = TX_ACK_RECEIVE;
                break;

            case TX_ACK_RECEIVE:
                m_state = IDLE;
                break;

            case RX_PACKET_RECEIVE:
                m_state = RX_ACK_SEND;
                break;

            case RX_ACK_SEND:
                m_state = RX_PACKET_RECEIVE;
                break;

            case IDLE:
                break;
        }

        if (m_state == RX_PACKET_RECEIVE)
        {
            radio_packet_t * tx_packet;
            err_code = packet_queue_get(&m_tx_queue, &tx_packet);
            ASSUME_SUCCESS(err_code);

            tx_packet->flags.ack = 1;

            NRF_RADIO->PACKETPTR = (uint32_t) tx_packet;

            err_code = packet_queue_add(&m_rx_queue, &m_rx_packet);
            ASSUME_SUCCESS(err_code);

            radio_evt_t evt;
            evt.type = PACKET_RECEIVED;
            err_code = packet_queue_get(&m_rx_queue, &evt.packet);
            ASSUME_SUCCESS(err_code);

            (*m_evt_handler)(&evt);
        }
        else
        {
            NRF_RADIO->PACKETPTR = (uint32_t) &m_rx_packet;
        }
    }
    if ((NRF_RADIO->EVENTS_DISABLED == 1) && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk))
    {
        NRF_RADIO->EVENTS_DISABLED = 0; 

        switch (m_state)
        {
            case TX_PACKET_SEND:
            case RX_ACK_SEND:
                NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos |
                    RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos |
                    RADIO_SHORTS_DISABLED_RXEN_Enabled << RADIO_SHORTS_DISABLED_RXEN_Pos;
                break;

            case RX_PACKET_RECEIVE:
                NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos |
                    RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos |
                    RADIO_SHORTS_DISABLED_TXEN_Enabled << RADIO_SHORTS_DISABLED_TXEN_Pos;
                break;

            case TX_ACK_RECEIVE:
                NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos |
{'fraction_non_alphanumeric': 0.07736976942783945, 'fraction_numerical': 0.005123825789923143, 'mean_word_length': 2.0531803962460895, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '45305', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2120, 'n_words': 290}