Minikit source code

[sourcecode language=”css”]

/*
Based on TI sample code. See their license (SimpliciTI can only be run on TI devices).
My additions are public domain. Use as you wish, but it’s nice if you can give credit to me.
*/

#include

#include "bsp.h"
#include "mrfi.h"
#include "bsp_leds.h"
#include "bsp_buttons.h"
#include "nwk_types.h"
#include "nwk_api.h"
#include "nwk_frame.h"
#include "nwk.h"

#include "mrfi.h"

#include
#include
#include

#if (chip == 1110)
#include
#endif

#ifndef APP_AUTO_ACK
#error ERROR: Must define the macro APP_AUTO_ACK for this application.
#endif

void toggleLED(uint8_t);

/***********************************************************************************
* CONSTANTS
*/

// Size of allocated UART RX/TX buffer (just an example)
#define SIZE_OF_UART_RX_BUFFER 30
#define SIZE_OF_UART_TX_BUFFER SIZE_OF_UART_RX_BUFFER

#define UART_TST_CHAR_1 0xA5
#define UART_TST_CHAR_2 0xB5

// Test definitions
#define UART_TST_MODE_RX
//#define UART_TST_MODE_TX

// Baudrate = 9600 bps (U0BAUD.BAUD_M = 131, U0GCR.BAUD_E = 8)
#define UART_BAUD_M 131
#define UART_BAUD_E 8

/***********************************************************************************
* LOCAL VARIABLES
*/

static uint8_t rxd = 0;
static uint8_t msg[MAX_APP_PAYLOAD];
static uint8_t len;

// Buffer+index for UART RX/TX
static uint8_t __xdata uartRxBuffer[SIZE_OF_UART_RX_BUFFER];
static uint8 __xdata uartRxIndex;

//static char __xdata uartTxBuffer[SIZE_OF_UART_TX_BUFFER];
static uint8_t __xdata uartTxBuffer[SIZE_OF_UART_TX_BUFFER];
static uint8 uartTxIndex;

static uint8 pktcount;

// Variable for buffer indexing
static uint16 __xdata i;

// Variable for UART packet monitoring
static uint8 __xdata uartPktReceived = 1;

// Prototype for local functions
void uart0StartRxForIsr(void);
void uart0StartTxForIsr(void);

/************ THIS SOURCE FILE REPRESENTS THE AUTOMATIC NOTIFICATION SOLUTION ************/

/* reserve space for the maximum possible peer Link IDs */
static linkID_t sLID[NUM_CONNECTIONS] = {0};
static linkID_t sLid;
static uint8_t sNumCurrentPeers = 0;

// Maximum data length
#define SIMPLICITI_MAX_PAYLOAD_LENGTH (32u)

// Data to send / receive
extern unsigned char simpliciti_data[SIMPLICITI_MAX_PAYLOAD_LENGTH];
unsigned char simpliciti_data[SIMPLICITI_MAX_PAYLOAD_LENGTH];

/* callback handler */
static uint8_t sCB(linkID_t);

/* received message handler */
static void processMessage(linkID_t, uint8_t *, uint8_t);

/* work loop semaphores */
static volatile uint8_t sPeerFrameSem = 0;
static volatile uint8_t sJoinSem = 0;

#ifdef FREQUENCY_AGILITY
/* ************** BEGIN interference detection support */

#define INTERFERNCE_THRESHOLD_DBM (-70)
#define SSIZE 25
#define IN_A_ROW 3
static int8_t sSample[SSIZE];
static uint8_t sChannel = 0;
#endif /* FREQUENCY_AGILITY */

/* blink LEDs when channel changes… */
static volatile uint8_t sBlinky = 0;

/* ************** END interference detection support */

#define SPIN_ABOUT_A_QUARTER_SECOND NWK_DELAY(250)
#define SPIN_ABOUT_A_10MS NWK_DELAY(10)
#define SPIN_ABOUT_A_1MS NWK_DELAY(1)

int signalcounter = 0;

void main (void)
{

// Initialise the allocated UART buffers:
for (i = 0; i < SIZE_OF_UART_RX_BUFFER; i++) { uartRxBuffer[i] = 0; } /*************************************************************************** * Setup I/O ports * * Port and pins used by USART0 operating in UART-mode are * RX : P0_2 * TX : P0_3 * CT/CTS : P0_4 * RT/RTS : P0_5 * * These pins can be set to function as peripheral I/O to be be used by UART0. * The TX pin on the transmitter must be connected to the RX pin on the receiver. * If enabling hardware flow control (U0UCR.FLOW = 1) the CT/CTS (Clear-To-Send) * on the transmitter must be connected to the RS/RTS (Ready-To-Send) pin on the * receiver. */ // Configure USART0 for Alternative 1 => Port P0 (PERCFG.U0CFG = 0)
// To avoid potential I/O conflict with USART1:
// configure USART1 for Alternative 2 => Port P1 (PERCFG.U1CFG = 1)
PERCFG = (PERCFG & ~PERCFG_U0CFG) | PERCFG_U1CFG;

// Configure relevant Port P0 pins for peripheral function:
// P0SEL.SELP0_2/3/4/5 = 1 => RX = P0_2, TX = P0_3, CT = P0_4, RT = P0_5
P0SEL |= BIT5 | BIT4 | BIT3 | BIT2;

/***************************************************************************
* Configure UART
*
* The system clock source used is the HS XOSC at 26 MHz speed.
*/

// Set system clock source to 26 Mhz XSOSC to support maximum transfer speed,
// ref. [clk]=>[clk_xosc.c]
SLEEP &= ~SLEEP_OSC_PD;
while( !(SLEEP & SLEEP_XOSC_S) );
CLKCON = (CLKCON & ~(CLKCON_CLKSPD | CLKCON_OSC)) | CLKSPD_DIV_1;
while (CLKCON & CLKCON_OSC);
SLEEP |= SLEEP_OSC_PD;

SPIN_ABOUT_A_10MS;

// Initialise bitrate = 57.6 kbps (U0BAUD.BAUD_M = 34, U0GCR.BAUD_E = 11)
U0BAUD = UART_BAUD_M;
U0GCR = (U0GCR&~U0GCR_BAUD_E) | UART_BAUD_E;

SPIN_ABOUT_A_10MS;

// Initialise UART protocol (start/stop bit, data bits, parity, etc.):

// USART mode = UART (U0CSR.MODE = 1)
U0CSR |= U0CSR_MODE;

// Start bit level = low => Idle level = high (U0UCR.START = 0)
U0UCR &= ~U0UCR_START;

// Stop bit level = high (U0UCR.STOP = 1)
U0UCR |= U0UCR_STOP;

// Number of stop bits = 1 (U0UCR.SPB = 0)
U0UCR &= ~U0UCR_SPB;

// Parity = disabled (U0UCR.PARITY = 0)
U0UCR &= ~U0UCR_PARITY;

// 9-bit data enable = 8 bits transfer (U0UCR.BIT9 = 0)
U0UCR &= ~U0UCR_BIT9;

// Level of bit 9 = 0 (U0UCR.D9 = 0), used when U0UCR.BIT9 = 1
// Level of bit 9 = 1 (U0UCR.D9 = 1), used when U0UCR.BIT9 = 1
// Parity = Even (U0UCR.D9 = 0), used when U0UCR.PARITY = 1
// Parity = Odd (U0UCR.D9 = 1), used when U0UCR.PARITY = 1
U0UCR &= ~U0UCR_D9;

// Flow control = disabled (U0UCR.FLOW = 0)
U0UCR &= ~U0UCR_FLOW;

// Bit order = LSB first (U0GCR.ORDER = 0)
U0GCR &= ~U0GCR_ORDER;

bspIState_t intState;

BSP_Init();

SMPL_Init(sCB);

uart0StartRxForIsr();

strcpy(uartTxBuffer,"OK\r\n");
uart0StartTxForIsr();

SPIN_ABOUT_A_QUARTER_SECOND;

while (1)
{
//SPIN_ABOUT_A_10MS;

/* Check for overflow */
while (RFIF & 0x40)
{
toggleLED(2);
/* OK to start again… */
BSP_ENTER_CRITICAL_SECTION(intState);
MRFI_RxIdle();
RFIF &= ~RFIF_IRQ_DONE;
RFIF &= ~RFIF_IRQ_RXOVF;
RFIF = 0;
S1CON &= ~(S1CON_RFIF_1 | S1CON_RFIF_0);
MRFI_RxOn();
SPIN_ABOUT_A_10MS;
BSP_EXIT_CRITICAL_SECTION(intState);
}

/* Wait for the Join semaphore to be set by the receipt of a Join frame from a
* device that supports an End Device.
*
*/
if (sJoinSem && (sNumCurrentPeers < NUM_CONNECTIONS)) { /* listen for a new connection */ while (1) { if (SMPL_SUCCESS == SMPL_LinkListen(&sLID[sNumCurrentPeers])) { break; } /* Implement fail-to-link policy here. otherwise, listen again. */ } sNumCurrentPeers++; BSP_ENTER_CRITICAL_SECTION(intState); sJoinSem–; BSP_EXIT_CRITICAL_SECTION(intState); } if (rxd == 1) { processMessage(sLid, msg, len); rxd = 0; } //BSP_EXIT_CRITICAL_SECTION(intState); } } void toggleLED(uint8_t which) { if (1 == which) { BSP_TOGGLE_LED1(); } else if (2 == which) { BSP_TOGGLE_LED2(); } return; } /* Runs in ISR context. Reading the frame should be done in the */ /* application thread not in the ISR thread. */ static uint8_t sCB(linkID_t lid) { if (lid) { sPeerFrameSem++; sBlinky = 0; } else { sJoinSem++; } pktcount++; if (rxd == 0) { SMPL_SUCCESS == SMPL_Receive(lid, msg, &len); sLid = lid; if (len > 1) {
toggleLED(1);
rxd = 1;
}
}
else {
// Ignore this packet 🙁
uint8_t msgign[MAX_APP_PAYLOAD], lenignore;
SMPL_SUCCESS == SMPL_Receive(lid, msgign, &lenignore);
toggleLED(2);
}

//return 0;
return 1;
}

void insertNumber(uint8_t *msg, uint8_t number)
{
msg[0] = ‘0’+ (number / 100);
msg[1] = ‘0’+ ((number / 10) % 10);
msg[2] = ‘0’+ (number % 10);
}

static void processMessage(linkID_t lid, uint8_t *msg, uint8_t len)
{
uartTxBuffer[0] = len;
uartTxBuffer[1] = msg[0];
uartTxBuffer[2] = msg[1];
uartTxBuffer[3] = msg[2];
uartTxBuffer[4] = msg[3];
uartTxBuffer[5] = msg[4];
uartTxBuffer[6] = msg[5];
uartTxBuffer[7] = msg[6];
uartTxBuffer[8] = ‘\r’;
uartTxBuffer[9] = ‘\n’;
uartTxBuffer[10] = ‘\0’;

uart0StartTxForIsr();

toggleLED(1);

if (len == 2) {
simpliciti_data[0] = 0x07;
SMPL_Send(lid, simpliciti_data, 1);
}

return;
}

static void processMessageReadable(linkID_t lid, uint8_t *msg, uint8_t len)
{

int i = 0;

insertNumber(&uartTxBuffer[0], len);
uartTxBuffer[3] = ‘,’;
insertNumber(&uartTxBuffer[4], msg[0]);
uartTxBuffer[7] = ‘,’;
insertNumber(&uartTxBuffer[8], msg[1]);
uartTxBuffer[11] = ‘,’;
insertNumber(&uartTxBuffer[12], msg[2]);
uartTxBuffer[15] = ‘,’;
insertNumber(&uartTxBuffer[16], msg[3]);

uartTxBuffer[19] = ‘\r’;
uartTxBuffer[20] = ‘\n’;
uartTxBuffer[21] = ‘\0’;

//sprintf(uartTxBuffer,"RX (%i):%i,%i,%i", len, msg[0], msg[1], msg[2], msg[3]);

uart0StartTxForIsr();

toggleLED(1);

if (len == 2) {
simpliciti_data[0] = 0x07;
SMPL_Send(lid, simpliciti_data, 1);
}

return;
}

/***********************************************************************************
* UART FUNCTIONS
*/

/***********************************************************************************
* @fn uart0StartTxForIsr
*
* @brief Function which starts/initiates the transmission of byte sequence
* on UART0.
*
* @param none
*
* @return 0
*/
void uart0StartTxForIsr(void)
{

// Initialize the UART TX buffer indexes.
uartTxIndex = 0;

// Clear any pending UART TX Interrupt Flag (IRCON2.UTXxIF = 0, UxCSR.TX_BYTE = 0)
UTX0IF = 0; U0CSR &= ~U0CSR_TX_BYTE;

// Send very first UART byte
U0DBUF = uartTxBuffer[uartTxIndex++];

// Enable global interrupt (IEN0.EA = 1) and UART TX Interrupt (IEN2.UTXxIE = 1)
EA = 1; IEN2 |= IEN2_UTX0IE;
}

/***********************************************************************************
* @fn uart0StartRxForIsr
*
* @brief Function which starts/initiates the receive of a byte sequence
* on UART0.
*
* @param none
*
* @return 0
*/
void uart0StartRxForIsr(void)
{

// Initialize the UART RX buffer index
uartRxIndex = 0;

// Clear any pending UART RX Interrupt Flag (TCON.URXxIF = 0, UxCSR.RX_BYTE = 0)
URX0IF = 0; U0CSR &= ~U0CSR_RX_BYTE;

// Enable UART RX (UxCSR.RE = 1)
U0CSR |= U0CSR_RE;

// Enable global Interrupt (IEN0.EA = 1) and UART RX Interrupt (IEN0.URXxIE = 1)
EA = 1; URX0IE = 1;
}

/***********************************************************************************
* @fn UART0_TX_ISR
*
* @brief Function which completes the UART transmit session, that is it
* sends the rest of the UART0 packet.
*
* @param none
*
* @return 0
*/
_Pragma("vector=0x73") __near_func __interrupt void UART1_TX_ISR(void);
_Pragma("vector=0x73") __near_func __interrupt void UART1_TX_ISR(void)
{

// Clear UART0 TX Interrupt Flag (IRCON2.UTX0IF = 0)
UTX0IF = 0;

// If no UART byte left to transmit, stop this UART TX session
//if (uartTxIndex > SIZE_OF_UART_TX_BUFFER || uartTxBuffer[uartTxIndex] == ‘\0’ )
if (uartTxIndex > SIZE_OF_UART_TX_BUFFER )
{
// Note:
// In order to start another UART TX session the application just needs
// to prepare the source buffer, and simply send the very first byte.
uartTxIndex = 0; IEN2 &= ~IEN2_UTX0IE; return;
}

// Send next UART byte
U0DBUF = uartTxBuffer[uartTxIndex++];

if (uartTxBuffer[uartTxIndex-1] == ‘\n’)
{
uartTxIndex = 0; IEN2 &= ~IEN2_UTX0IE; return;
}
}

/***********************************************************************************
* @fn UART0_RX_ISR
*
* @brief Function which completes the UART receive session.
*
* @param none
*
* @return 0
*/

_Pragma("vector=0x13") __near_func __interrupt void UART0_RX_ISR(void);
_Pragma("vector=0x13") __near_func __interrupt void UART0_RX_ISR(void)
{

// Clear UART0 RX Interrupt Flag (TCON.URX0IF = 0)
URX0IF = 0;

// Read UART0 RX buffer
uartRxBuffer[uartRxIndex++] = U0DBUF;

if (uartRxBuffer[uartRxIndex-1] == ‘\n’) {
uartRxIndex = 0;
uartPktReceived = 1;
}

// If all UART data received, stop this UART RX session
if (uartRxIndex >= SIZE_OF_UART_RX_BUFFER) {
uartRxIndex = 0;
uartPktReceived = 1;
}

}

[/sourcecode]

5 thoughts on “Minikit source code”

  1. please sent to my email code arduino + chronos watch 915mhz,,
    i hava rfbee 915 mhz but i don’t know make it 🙁

  2. Dear Sir,
    the code is giving me a lot of error . could you please verify the code (or) provide me a complete source for this project.
    it will also be helpfull for me if you could provide me the sketch for arduino also

    Thanks in advance

  3. I had a hard-disk crash on my old computer where this project was stored (both the minikit and Arduino code), and I don’t think I have the files any more. As far as I remember, it was difficult to package the whole project because so much of it was about editing SimpliciTI to work with the minikit. There were some documents from TI on how to do this. Have you tried googling this and setting this up with the standard examples that follow that code?

    If I did the project now, I’d probably use the MSP430 Launchpad with the Launchpad RF boosterpack instead of the minikit.

    If you email me the error codes, I can have a look and see if I can see quickly what the problem is, but it’s been a year or so now since I worked with the 8051. My email is lars and then the domain of this site (without the lars.)

  4. I must say it was hard to find your website in search results.
    You write great posts but you should rank your page higher in search engines.

    If you don’t know how to do it search on youtube: how to rank a website Marcel’s way

Leave a Reply

Your email address will not be published. Required fields are marked *