<commit_before>/* -*- Mode: C; c-basic-offset: 8; -*- */
//
// Copyright (c) 2011 <NAME>, All Rights Reserved
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//  o Redistributions of source code must retain the above copyright 
//    notice, this list of conditions and the following disclaimer.
//  o Redistributions in binary form must reproduce the above copyright 
//    notice, this list of conditions and the following disclaimer in 
//    the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//

/// @file	_start.c
///
/// Early startup code.
///
/// This file *must* be linked first for interrupt vector generation and main() to work.
///

#include <stdarg.h>

#include "radio.h"

// Interrupt vector prototypes
//
// Note that these *must* be placed in this file for SDCC to generate the
// interrupt vector table correctly.
//
extern void	uartIsr(void) __interrupt(INTERRUPT_UART0) __using(1);
extern void	Receiver_ISR(void) __interrupt(INTERRUPT_INT0);

// Local prototypes
static void hardware_init(void);

static uint8_t		rlen;
static __xdata uint8_t	rbuf[64];

void
main(void)
{
	PHY_STATUS	s;

	hardware_init();
	puts("SiK radio starting");

	// Init the radio driver
	s = rtPhyInit();
	if (s != PHY_STATUS_SUCCESS)
		panic("rtPhyInit failed: %u", s);

	// try to load parameters; set them to defaults if that fails
	// XXX default parameter selection should be based on strapping
	// options
	if (!param_load())
		param_default_434();

	// XXX this should almost certainly be replaced with the ppPhy code
	// plus some minor parameter tweaking.
	rtPhySet(TRX_FREQUENCY,		param_get32(PARAM_TRX_FREQUENCY));
	rtPhySet(TRX_CHANNEL_SPACING,	param_get32(PARAM_TRX_CHANNEL_SPACING));
	rtPhySet(TRX_DEVIATION,		param_get32(PARAM_TRX_DEVIATION));
	rtPhySet(TRX_DATA_RATE,		param_get32(PARAM_TRX_DATA_RATE));
	rtPhySet(RX_BAND_WIDTH,		param_get32(PARAM_RX_BAND_WIDTH));

	s = rtPhyInitRadio();
	if (s != PHY_STATUS_SUCCESS)
		panic("rtPhyInitRadio failed: %u", s);
	s = rtPhyRxOn();
	if (s != PHY_STATUS_SUCCESS)
		panic("rtPhyRxOn failed: %u", s);

	puts("radio config done");

	for (;;) {

		if (rtPhyGetRxPacket(&rlen, rbuf) == PHY_STATUS_SUCCESS) {
			rtPhyTx(rlen, rbuf);
		}
	}
}

/// Panic and stop the system
///
/// This is not terribly solid - printf is large and it uses putchar which
/// depends on interrupts.  Consider hacking printf_tiny into a panic handler.
///
void
panic(const char *reason, ...)
{
	va_list	arg;

	puts("**PANIC**");
	va_start(arg, reason);
	vprintf(reason, arg);
	va_end(arg);
	puts("");
	for(;;)
		;
}

/// Additional basic hardware intialisation beyond the basic operating conditions
/// set up by the bootloader.
///
static void
hardware_init(void)
{

	// SPI
	XBR1	|= 0x40;	// enable SPI in 3-wire mode
	P1MDOUT	|= 0x15;	// SCK1, MOSI1, MISO1 push-pull
	SFRPAGE	 = CONFIG_PAGE;
	P1DRV	|= 0x15;	// SPI signals use high-current mode
	SFRPAGE	 = LEGACY_PAGE;
	SPI1CFG	 = 0x40;	// master mode
	SPI1CN	 = 0x00;	// 3 wire master mode
	SPI1CKR	 = 0x01;	// initialize SPI prescaler to divide-by-4, might be able to use /2 if we're lucky
	SPI1CN	|= 0x01;	// enable SPI
	NSS1	 = 1;		// set NSS high

	// Clear the radio interrupt state
	IE0	 = 0;

	// uart - leave the baud rate alone
	uartInitUart(BAUD_RATE_NO_CHANGE);

	// global interrupt enable
	EA = 1;

	// Turn on the 'radio running' LED and turn off the bootloader LED
	LED_RADIO = LED_ON;
	LED_BOOTLOADER = LED_OFF;
}<commit_msg>Re-enable receiver after transmit, seems to be required to avoid getting phony rx events.<commit_after>/* -*- Mode: C; c-basic-offset: 8; -*- */
//
// Copyright (c) 2011 <NAME>, All Rights Reserved
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//  o Redistributions of source code must retain the above copyright 
//    notice, this list of conditions and the following disclaimer.
//  o Redistributions in binary form must reproduce the above copyright 
//    notice, this list of conditions and the following disclaimer in 
//    the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//

/// @file	_start.c
///
/// Early startup code.
///
/// This file *must* be linked first for interrupt vector generation and main() to work.
///

#include <stdarg.h>

#include "radio.h"

// Interrupt vector prototypes
//
// Note that these *must* be placed in this file for SDCC to generate the
// interrupt vector table correctly.
//
extern void	uartIsr(void) __interrupt(INTERRUPT_UART0) __using(1);
extern void	Receiver_ISR(void) __interrupt(INTERRUPT_INT0);
extern void	T0_ISR(void) __interrupt(INTERRUPT_TIMER0);


// Local prototypes
static void hardware_init(void);

static uint8_t		rlen;
static __xdata uint8_t	rbuf[64];

void
main(void)
{
	PHY_STATUS	s;

	hardware_init();
	puts("SiK radio starting");

	// Init the radio driver
	s = rtPhyInit();
	if (s != PHY_STATUS_SUCCESS)
		panic("rtPhyInit failed: %u", s);

	// try to load parameters; set them to defaults if that fails
	// XXX default parameter selection should be based on strapping
	// options
	if (!param_load())
		param_default_434();

	// XXX this should almost certainly be replaced with the ppPhy code
	// plus some minor parameter tweaking.
	rtPhySet(TRX_FREQUENCY,		param_get32(PARAM_TRX_FREQUENCY));
	rtPhySet(TRX_CHANNEL_SPACING,	param_get32(PARAM_TRX_CHANNEL_SPACING));
	rtPhySet(TRX_DEVIATION,		param_get32(PARAM_TRX_DEVIATION));
	rtPhySet(TRX_DATA_RATE,		param_get32(PARAM_TRX_DATA_RATE));
	rtPhySet(RX_BAND_WIDTH,		param_get32(PARAM_RX_BAND_WIDTH));

	s = rtPhyInitRadio();
	if (s != PHY_STATUS_SUCCESS)
		panic("rtPhyInitRadio failed: %u", s);
	s = rtPhyRxOn();
	if (s != PHY_STATUS_SUCCESS)
		panic("rtPhyRxOn failed: %u", s);

	puts("radio config done");

	for (;;) {

		if (rtPhyGetRxPacket(&rlen, rbuf) == PHY_STATUS_SUCCESS) {
			LED_ACTIVITY = LED_ON;
			rtPhyTx(rlen, rbuf);
			LED_ACTIVITY = LED_OFF;
			rtPhyRxOn();
			printf("pkt %d 0x%02x\n", rlen, rbuf[0]);
		}
	}
}

/// Panic and stop the system
///
/// This is not terribly solid - printf is large and it uses putchar which
/// depends on interrupts.  Consider hacking printf_tiny into a panic handler.
///
void
panic(const char *reason, ...)
{
	va_list	arg;

	puts("**PANIC**");
	va_start(arg, reason);
	vprintf(reason, arg);
	va_end(arg);
	puts("");
	for(;;)
		;
}

/// Additional basic hardware intialisation beyond the basic operating conditions
/// set up by the bootloader.
///
static void
hardware_init(void)
{

	// SPI
	XBR1	|= 0x40;	// enable SPI in 3-wire mode
	P1MDOUT	|= 0x15;	// SCK1, MOSI1, MISO1 push-pull
	SFRPAGE	 = CONFIG_PAGE;
	P1DRV	|= 0x15;	// SPI signals use high-current mode
	SFRPAGE	 = LEGACY_PAGE;
	SPI1CFG	 = 0x40;	// master mode
	SPI1CN	 = 0x00;	// 3 wire master mode
	SPI1CKR	 = 0x00;	// initialize SPI prescaler to divide-by-2 (12.25MHz, technically out of spec)
	SPI1CN	|= 0x01;	// enable SPI
	NSS1	 = 1;		// set NSS high

	// Clear the radio interrupt state
	IE0	 = 0;

	// uart - leave the baud rate alone
	uartInitUart(BAUD_RATE_NO_CHANGE);

	// global interrupt enable
	EA = 1;

	// Turn on the 'radio running' LED and turn off the bootloader LED
	LED_RADIO = LED_ON;
	LED_BOOTLOADER = LED_OFF;
}
{'fraction_non_alphanumeric': 0.10642817216321968, 'fraction_numerical': 0.015539407490218, 'mean_word_length': 3.7788461538461537, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '477900', 'n_tokens_mistral': 3200, 'n_tokens_neox': 2941, 'n_words': 1053}