This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430F5438A: MSP430F5438A

Part Number: MSP430F5438A
Other Parts Discussed in Thread: CC1200,

Hello World!

So I'm using sniff example code from the TI website to send data through the RF system. I modified it a little bit, so that it is possible to send data, obtained by a pulsed output. It’s working very well.

I am using two MSP430F5438a modules with the CC1200. The first one works as the remote unit to collect information from the pulsed output and the second one is local unit to receive.

But I’m not able to do the communication of the second module with the PC to verify all receive data on a remote terminal, what I mean is, I need to send data between two points and see the data in a terminal.

I’ve problems with UART communication.

I used bspUartOpen function to defined baud rate, bspUartDataPut to put data in Uart to prepare to send but what I need to do now? Use some instruction like printf? I've tried but didn't work.

I am working with the IAR and if anybody can help me, the code follows below.

Thanks!

//******************************************************************************

//! @file       cc1200_rx_sniff_mode_rx.c

//! @brief     This program demonstrates Rx Sniff Mode for the 38.4 kbps

//             setting available in SmartRf Studio.

//             The transmitter sends a packet every time a button is pushed and

//             the receiver implements RX Sniff Mode to reduce the current

//             consumption.

//             DN511 (http://www.ti.com/lit/swra428) explains how the register

//             settings are found.

//

// Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/

//

// Redistribution and use in source and binary forms, with or without

// modification, are permitted provided that the following conditions

// are met:

//

//     Redistributions of source code must retain the above copyright

//     notice, this list of conditions and the following disclaimer.

//

//     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.

//

//     Neither the name of Texas Instruments Incorporated nor the names of

//     its contributors may be used to endorse or promote products derived

//     from this software without specific prior written permission.

//

// 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

// OWNER 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.

//*****************************************************************************/

/*******************************************************************************

* INCLUDES

*/

#include "msp430.h"

#include "lcd_dogm128_6.h"

#include "hal_spi_rf_trxeb.h"

#include "cc120x_spi.h"

#include "stdlib.h"

#include "bsp.h"

//#include "bsp_key.h"

#include "io_pin_int.h"

#include "bsp_led.h"

#include "projeto_vazao_reg_config.h"

#include "stdio.h"

#include "string.h"

#include "bsp_uart.h"

/*******************************************************************************

* DEFINES

*/

#define ISR_ACTION_REQUIRED     1

#define ISR_IDLE               0

#define PKTLEN                 30 // 1 < PKTLEN < 126

#define GPIO3                   0x04

#define GPIO2                   0x08

#define GPIO0                   0x80

/*******************************************************************************

* LOCAL VARIABLES

*/

static uint8 packetSemaphoreTX, packetSemaphoreRX;

static uint32 packetCounter = 0, x;

unsigned char dadox[200];

uint16 dado = 0;

/*******************************************************************************

* STATIC FUNCTIONS

*/

static void initMCU(void);

static void registerConfig(void);

static void runRX(void);

static void radioRxISR(void);

static void updateLcd(uint16 dado1, uint16 dado2);

static void calibrateRCOsc(void);

static void initRX(void);

static void initTX(void);

static void runTX(void);

static void radioTxISR(void);

static void createPacket(uint8 txBuffer[], uint16 dado);

uint16 enviado = 0, recebido = 0;

 

#define set_bit(y,bit) (y|=(1<<bit))   // set bit x from byte Y

#define clr_bit(y,bit) (y&=~(1<<bit)) // clear bit x from byte Y

#define cpl_bit(y,bit) (y^=(1<<bit))   // toggle bit x from byte Y

#define tst_bit(y,bit) (y&(1<<bit))   // test bit

 

void init()

{

   // Initialize MCU and peripherals

   initMCU();

 

   // Write radio registers

   registerConfig();

   //initTX();

   //initRX();  

   updateLcd(enviado, recebido);  

} 

/****************************************************************************

* uint16_t

* bspUartDataPut(uint8_t *pui8Data, uint16_t ui16Length)

*

* @brief   This function puts up to \e ui16Length bytes into the BSP UART TX

*           buffer and starts to transfer data over UART.

*

*           If \b BSP_UART_ALL_OR_NOTHING is defined, data is put into the

*           TX buffer only if there is room for all \e ui16Length bytes.

*

* @param   pui8Data       is a pointer to the source buffer.

* @param   ui16Length     is the number of bytes to transfer.

*

* @return   Returns the number of bytes actually copied to the TX buffer.

******************************************************************************/

/*******************************************************************************

*   @fn         main

*

*   @brief     Runs the main routine

*

*   @param     none

*

*   @return     none

*/

void main(void) {

  

   init();

   // Enter runRX, never coming back

   initRX();

   while(1)

   { 

     //runRX();

     updateLcd(enviado, recebido);

     if(packetSemaphoreRX == ISR_ACTION_REQUIRED)

     {

       for(x = 0; x < 1000; x++);

     initTX();

       runTX();

       initRX();

       updateLcd(enviado, recebido);

 

/******************************************************************************************

******************* Communication test with remote terminal *************************

*/

 

       bspUartOpen(BSP_UART_BR_115200);

       bspUartDataPut((uint8_t *)recebido, 1);

 

/*

*************************** end test *****************************************

*******************************************************************************/

       // Clear semaphore flag

       packetSemaphoreRX = ISR_IDLE;

     }

   }

}

 

/*******************************************************************************

*   @fn         initTX

*

*   @brief     Configura o modo TX para enviar dados.

*

*   @param     none

*

*   @return     none

*/

static void initTX(void)

{

   static uint8 marcState;

 

   // Connect ISR function to GPIO0

   ioPinIntRegister(IO_PIN_PORT_1, GPIO0, &radioTxISR);

 

   // Interrupt on falling edge

   ioPinIntTypeSet(IO_PIN_PORT_1, GPIO0, IO_PIN_FALLING_EDGE);

 

   // Clear ISR flag

   ioPinIntClear(IO_PIN_PORT_1, GPIO0);

 

   // Enable interrupt

   ioPinIntEnable(IO_PIN_PORT_1, GPIO0);

 

   // Update LCD

   //updateLcd();

 

   // Calibrate radio

   trxSpiCmdStrobe(CC120X_SCAL);

 

   // Wait for calibration to be done (radio back in IDLE state)

   do {

       cc120xSpiReadReg(CC120X_MARCSTATE, &marcState, 1);

   } while (marcState != 0x41);

  

}

 

/*******************************************************************************

*   @fn         runTX

*

*   @brief     Transmits a packet.

*

*   @param     none

*

*   @return     none

*/

static void runTX(void) {

 

  

   // Initialize packet buffer of size PKTLEN + 1

   uint8 txBuffer[PKTLEN + 1];

  

   // Infinite loop

   //while(TRUE) {

 

       // Wait for button push

       //while(!bspKeyPushed(BSP_KEY_ALL));

 

       // Create a random packet with PKTLEN + 2 byte packet counter + n x random bytes

      

       enviado++;

      

       createPacket(txBuffer, enviado);

 

       // Write packet to TX FIFO

       cc120xSpiWriteTxFifo(txBuffer, sizeof(txBuffer));

 

       // Strobe TX to send packet

      trxSpiCmdStrobe(CC120X_STX);

 

       // Wait for packet to be sent

       while(packetSemaphoreTX != ISR_ACTION_REQUIRED);

 

       // Clear semaphore flag

       packetSemaphoreTX = ISR_IDLE;

 

       // Update LCD

       //updateLcd();

   //}

}

 

/*******************************************************************************

* @fn         radioTxISR

*

* @brief       ISR for packet handling in TX. Sets packet semaphore

*             and clears interrupt flag

*

* @param       none

*

* @return     none

*/

static void radioTxISR(void) {

 

   // Set packet semaphore

   packetSemaphoreTX = ISR_ACTION_REQUIRED;

   cpl_bit(P4OUT,2);                       // Alterna bit

 

 

   // Clear ISR flag

   ioPinIntClear(IO_PIN_PORT_1, GPIO0);

}

 

/*******************************************************************************

*   @fn         createPacket

*

*   @brief     This function is called before a packet is transmitted. It fills

*               the txBuffer with a packet consisting of a length byte, two

*               bytes packet counter and n random bytes

*

*               The packet format is as follows:

*               |--------------------------------------------------------------|

*               |           |           |           |         |      |       |

*               | pktLength | pktCount1 | pktCount0 | rndData |.......| rndData|

*               |           |           |           |         |       |       |

*               |--------------------------------------------------------------|

*               txBuffer[0] txBuffer[1] txBuffer[2] ......... txBuffer[PKTLEN]

*

*   @param       Pointer to start of txBuffer

*

*   @return     none

*/

static void createPacket(uint8 txBuffer[], uint16 dado) {

 

   txBuffer[0] = PKTLEN;                        // Length byte

   txBuffer[1] = (uint8) (packetCounter >> 8);   // MSB of packetCounter

   txBuffer[2] = (uint8) packetCounter;         // LSB of packetCounter

   txBuffer[3] = (uint8) (dado >> 8);

   txBuffer[4] = (uint8) dado;

  

   for(uint8 i = 5; i < (PKTLEN + 1); i++) txBuffer[i] = 0;

   // Fill rest of buffer with random bytes

   //for(uint8 i = 3; i < (PKTLEN + 1); i++) {

   //   txBuffer[i] = (uint8)rand();

   //}

}

 

 

/*******************************************************************************

*   @fn         initRX

*

*   @brief     Puts radio in RX Sniff Mode and waits for packets.

*

*   @param     none

*

*   @return     none

*/

static void initRX(void)

{

   uint8 marcState;

  

   // Connect ISR function to GPIO2

   ioPinIntRegister(IO_PIN_PORT_1, GPIO2, &radioRxISR);

 

   // Interrupt on falling edge

   ioPinIntTypeSet(IO_PIN_PORT_1, GPIO2, IO_PIN_RISING_EDGE);

 

   // Clear ISR flag

   ioPinIntClear(IO_PIN_PORT_1, GPIO2);

 

   // Enable interrupt

   ioPinIntEnable(IO_PIN_PORT_1, GPIO2);

 

   // Update LCD

 

   updateLcd(enviado, recebido);

 

   // Calibrate radio

   trxSpiCmdStrobe(CC120X_SCAL);

 

   // Wait for calibration to be done (radio back in IDLE state)

   do {

       cc120xSpiReadReg(CC120X_MARCSTATE, &marcState, 1);

   } while (marcState != 0x41);

 

   // Calibrate the RCOSC

   calibrateRCOsc();

  

   // Set radio in RX Sniff Mode

   trxSpiCmdStrobe(CC120X_SWOR);

}

 

/*******************************************************************************

*   @fn         runRX

*

*   @brief     Puts radio in RX Sniff Mode and waits for packets. A packet

*               counter is incremented for each packet received and the LCD is

*               updated

*

*   @param     none

*

*   @return     none

*/

static void runRX(void) {

 

   uint8 rxBuffer[128] = {0};

   uint8 rxBytes;

  

   // Infinite loop

   //while(TRUE) {

 

       // Set radio in RX Sniff Mode

       //trxSpiCmdStrobe(CC120X_SWOR);

 

       // Wait for packet to be received

       while(packetSemaphoreRX != ISR_ACTION_REQUIRED);

 

       // Clear semaphore flag

       //packetSemaphoreRX = ISR_IDLE;

 

      // Read number of bytes in RX FIFO

       cc120xSpiReadReg(CC120X_NUM_RXBYTES, &rxBytes, 1);

 

       // Read all the bytes in the RX FIFO

       cc120xSpiReadRxFifo(rxBuffer, rxBytes);

      

       recebido = (rxBuffer[3]<<8) + rxBuffer[4];

 

        // Update LCD

       updateLcd(enviado, recebido);

//       cpl_bit(P4OUT,0);                       // Alterna bit

 

   //}

}

 

/*******************************************************************************

*   @fn         calibrateRcOsc

*

*   @brief     Calibrates the RC oscillator used for the eWOR timer. When this

*               function is called, WOR_CFG0.RC_PD must be 0

*

*   @param     none

*

*   @return     none

*/

static void calibrateRCOsc(void) {

 

   uint8 temp;

 

   // Read current register value

   cc120xSpiReadReg(CC120X_WOR_CFG0, &temp,1);

 

   // Mask register bit fields and write new values

   temp = (temp & 0xF9) | (0x02 << 1);

 

   // Write new register value

   cc120xSpiWriteReg(CC120X_WOR_CFG0, &temp,1);

 

   // Strobe IDLE to calibrate the RCOSC

   trxSpiCmdStrobe(CC120X_SIDLE);

 

   // Disable RC calibration

   temp = (temp & 0xF9) | (0x00 << 1);

   cc120xSpiWriteReg(CC120X_WOR_CFG0, &temp, 1);

}

 

/*******************************************************************************

*   @fn         radioRxISR

*

*   @brief     ISR for packet handling in RX. Sets packet semaphore

*               and clears interrupt flag

*

*   @param     none

*

*   @return     none

*/

static void radioRxISR(void) {

 

   // Set packet semaphore

   packetSemaphoreRX = ISR_ACTION_REQUIRED;

  

   runRX();

  

   // Clear ISR flag

   ioPinIntClear(IO_PIN_PORT_1, GPIO2);

}

 

/*******************************************************************************

*   @fn         initMCU

*

*   @brief     Initialize MCU and board peripherals

*

*   @param     none

*

*   @return     none

*/

static void initMCU(void) {

 

   // Init clocks and I/O

   bspInit(BSP_SYS_CLK_8MHZ);

 

   // Init LEDs

   bspLedInit();

 

   // Init buttons

   //bspKeyInit(BSP_KEY_MODE_POLL);

 

   // Initialize SPI interface to LCD (shared with SPI flash)

   bspIoSpiInit(BSP_FLASH_LCD_SPI, BSP_FLASH_LCD_SPI_SPD);

 

   // Init LCD

   lcdInit();

 

   // Instantiate transceiver RF SPI interface to SCLK ~ 4 MHz

   // Input parameter is clockDivider

   // SCLK frequency = SMCLK/clockDivider

   trxRfSpiInterfaceInit(2);

 

   // Enable global interrupt

   _BIS_SR(GIE);

}

 

/*******************************************************************************

*   @fn         registerConfig

*

*   @brief     Write register settings as given by SmartRF Studio found in

*               cc1200_rx_sniff_mode_reg_config.h

*

*   @param     none

*

*   @return     none

*/

static void registerConfig(void) {

 

   uint8 writeByte;

 

   // Reset radio

   trxSpiCmdStrobe(CC120X_SRES);

 

   // Write registers to radio

   for(uint16 i = 0;

       i < (sizeof(preferredSettings)/sizeof(registerSetting_t)); i++) {

       writeByte = preferredSettings[i].data;

       cc120xSpiWriteReg(preferredSettings[i].addr, &writeByte, 1);

   }

}

 

/*******************************************************************************

*   @fn         updateLcd

*

*   @brief     Updates LCD buffer and sends buffer to LCD module

*

*   @param     none

*

*   @return     none

*/

static void updateLcd(uint16 dado1, uint16 dado2) {

  

   // Update LDC buffer and send to screen

   lcdBufferClear(0);

   lcdBufferPrintString(0, "Concentrador", 0, eLcdPage0);

   lcdBufferSetHLine(0, 0, LCD_COLS - 1, 7);

   lcdBufferPrintString(0, "Recebido:", 0, eLcdPage3);

   lcdBufferPrintInt(0, dado2, 80, eLcdPage3);

  

   lcdBufferPrintString(0, "Enviado:", 0, eLcdPage4);

   lcdBufferPrintInt(0, dado1, 80, eLcdPage4);

   //lcdBufferPrintInt(0, packetCounter++, 70, eLcdPage4);

   lcdBufferPrintString(0, "RX", 0, eLcdPage7);

   lcdBufferSetHLine(0, 0, LCD_COLS - 1, 55);

   lcdBufferInvertPage(0, 0, LCD_COLS, eLcdPage7);

   lcdSendBuffer(0);

}

  • Fabio,

    That example project has a UART driver callled bsp_uart.c and it implements basic UART handling code. The main one you need after having opened the UART is this one:

    • uint16_t bspUartDataPut(uint8_t *pui8Data, uint16_t ui16Length)

    This one can be used something like this:

    • bspUartDataPut("hello world\n", 12);

    Note that you have to supply the length of the array to be written to the UART, here I counted the number of bytes including special characters and spaces.

    Regards,
    /TA

  • Hello TA12012,

    I do not know why, but it didn't work with bspUartDatPut. I tried it another way, but it did not work.

    I found a tutorial on www.glitovsky.com which shows a UART configuration and I implemented it and worked.

    Below is the code, thanks for the reply.

    //******************************************************************************
    //! @file cc1200_rx_sniff_mode_rx.c
    //! @brief This program demonstrates Rx Sniff Mode for the 38.4 kbps
    // setting available in SmartRf Studio.
    // The transmitter sends a packet every time a button is pushed and
    // the receiver implements RX Sniff Mode to reduce the current
    // consumption.
    // DN511 (http://www.ti.com/lit/swra428) explains how the register
    // settings are found.
    //
    //
    // Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
    //
    // Redistribution and use in source and binary forms, with or without
    // modification, are permitted provided that the following conditions
    // are met:
    //
    // Redistributions of source code must retain the above copyright
    // notice, this list of conditions and the following disclaimer.
    //
    // 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.
    //
    // Neither the name of Texas Instruments Incorporated nor the names of
    // its contributors may be used to endorse or promote products derived
    // from this software without specific prior written permission.
    //
    // 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
    // OWNER 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.
    //*****************************************************************************/


    /*******************************************************************************
    * INCLUDES
    */
    #include <string.h>
    #include <stdio.h>
    #include "msp430.h"
    #include "lcd_dogm128_6.h"
    #include "hal_spi_rf_trxeb.h"
    #include "cc120x_spi.h"
    #include "stdlib.h"
    #include "bsp.h"
    //#include "bsp_key.h"
    #include "io_pin_int.h"
    #include "bsp_led.h"
    #include "projeto_vazao_reg_config.h"
    #include "uart.h"


    /*******************************************************************************
    * DEFINES
    */
    #define ISR_ACTION_REQUIRED 1
    #define ISR_IDLE 0
    #define PKTLEN 30 // 1 < PKTLEN < 126

    #define GPIO3 0x04
    #define GPIO2 0x08
    #define GPIO0 0x80


    /*******************************************************************************
    * LOCAL VARIABLES
    */
    static uint8 packetSemaphoreTX, packetSemaphoreRX;
    static uint32 packetCounter = 0, x;

    uint16 dado = 0;

    /*******************************************************************************
    * STATIC FUNCTIONS
    */
    static void initMCU(void);
    static void registerConfig(void);
    static void runTXbackRX(void);
    static void radioRxISR(void);
    static void updateLcd(uint16 dado1, uint16 dado2);
    static void calibrateRCOsc(void);

    static void initRX(void);
    static void initTX(void);

    static void runTX(void);
    static void radioTxISR(void);
    static void createPacket(uint8 txBuffer[], uint16 dado);

    void init(void);
    void initUARTConfigs(void);
    void setSMCLK8MHz(void);



    uint16 enviado = 0, recebido = 0;

    /***************** USADO PELA UART ********************************************/
    void setSMCLK8MHz();

    // UART Port Configuration parameters and registers
    UARTConfig cnf;
    USCIUARTRegs uartUsciRegs;
    USARTUARTRegs uartUsartRegs;

    // Buffers to be used by UART Driver
    unsigned char uartTxBuf[200];
    unsigned char uartRxBuf[200];


    /*******************************************************************************
    * @fn main
    *
    * @brief Runs the main routine
    *
    * @param none
    *
    * @return none
    */
    void main(void) {

    init();

    // Enter runRX, never coming back
    while(1)
    {

    //runRX();
    updateLcd(enviado, recebido);
    if(packetSemaphoreRX == ISR_ACTION_REQUIRED)
    {
    for(x = 0; x < 1000; x++);

    runTXbackRX();

    updateLcd(enviado, recebido);
    // Clear semaphore flag
    packetSemaphoreRX = ISR_IDLE;
    }
    }
    }

    /*******************************************************************************
    * @fn init
    *
    * @brief Inicializa a configuracoes.
    *
    * @param none
    *
    * @return none
    */
    void init(void)
    {
    // Initialize MCU and peripherals
    initMCU();

    // Write radio registers
    registerConfig();

    initTX();
    initRX();

    initUARTConfigs();

    updateLcd(enviado, recebido);

    }

    /*******************************************************************************
    * @fn initUARTConfigs
    *
    * @brief Inicializa a UART.
    *
    * @param none
    *
    * @return none
    */

    void initUARTConfigs(void)
    {
    // Disable MSP430 Watchdog Timer
    WDTCTL = WDTPW | WDTHOLD;


    // Configure SMCLK to 8MHz
    setSMCLK8MHz();


    /********************************
    *
    * UART Specific Configuration
    *
    ********************************/
    initUartDriver();

    // Configure UART Module on USCIA1
    cnf.moduleName = USCI_A1;

    // Use UART Pins P5.7 and P5.6
    cnf.portNum = PORT_5;
    cnf.RxPinNum = PIN7;
    cnf.TxPinNum = PIN6;

    // 115200 Baud from 8MHz SMCLK
    cnf.clkRate = 8000000L;
    cnf.baudRate = 115200L;
    cnf.clkSrc = UART_CLK_SRC_SMCLK;

    // 8N1
    cnf.databits = 8;
    cnf.parity = UART_PARITY_NONE;
    cnf.stopbits = 1;

    int res = configUSCIUart(&cnf,&uartUsciRegs);
    if(res != UART_SUCCESS)
    {
    // Failed to initialize UART for some reason
    __no_operation();
    }

    // Configure the buffers that will be used by the UART Driver.
    // These buffers are exclusively for the UART driver's use and should not be touched
    // by the application itself. Note that they may affect performance if they're too
    // small.
    setUartTxBuffer(&cnf, uartTxBuf, 200);
    setUartRxBuffer(&cnf, uartRxBuf, 200);

    enableUartRx(&cnf);

    /*********************************/

    __enable_interrupt(); // Enable Global Interrupts

    }

    /*******************************************************************************
    * \brief Initializes SMCLK to 8MHz
    *
    *
    * @param None
    * \return None
    *
    */
    void setSMCLK8MHz(void)
    {
    // Set clock to 8MHz
    UCSCTL3 |= SELREF_2; // Set DCO FLL reference = REFO
    UCSCTL4 |= SELA_2; // Set ACLK = REFO

    __bis_SR_register(SCG0); // Disable the FLL control loop
    UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
    UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation
    UCSCTL2 = FLLD_1 + 244; // Set DCO Multiplier for 8MHz
    // (N + 1) * FLLRef = Fdco
    // (244 + 1) * 32768 = 8MHz
    // Set FLL Div = fDCOCLK/2
    __bic_SR_register(SCG0); // Enable the FLL control loop

    // Worst-case settling time for the DCO when the DCO range bits have been
    // changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
    // UG for optimization.
    // 32 x 32 x 8 MHz / 32,768 Hz = 250000 = MCLK cycles for DCO to settle
    __delay_cycles(250000);

    // Loop until XT1,XT2 & DCO fault flag is cleared
    do
    {
    UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG);
    // Clear XT2,XT1,DCO fault flags
    SFRIFG1 &= ~OFIFG; // Clear fault flags
    }while (SFRIFG1&OFIFG); // Test oscillator fault flag
    }


    /*******************************************************************************
    * @fn initTX
    *
    * @brief Configura o modo TX para enviar dados.
    *
    * @param none
    *
    * @return none
    */
    static void initTX(void)
    {
    static uint8 marcState;

    // Connect ISR function to GPIO0
    ioPinIntRegister(IO_PIN_PORT_1, GPIO0, &radioTxISR);

    // Interrupt on falling edge
    ioPinIntTypeSet(IO_PIN_PORT_1, GPIO0, IO_PIN_FALLING_EDGE);

    // Clear ISR flag
    ioPinIntClear(IO_PIN_PORT_1, GPIO0);

    // Enable interrupt
    ioPinIntEnable(IO_PIN_PORT_1, GPIO0);

    // Update LCD
    //updateLcd();

    // Calibrate radio
    trxSpiCmdStrobe(CC120X_SCAL);

    // Wait for calibration to be done (radio back in IDLE state)
    do {
    cc120xSpiReadReg(CC120X_MARCSTATE, &marcState, 1);
    } while (marcState != 0x41);

    }



    /*******************************************************************************
    * @fn runTX
    *
    * @brief Transmits a packet.
    *
    * @param none
    *
    * @return none
    */

    static void runTXbackRX(void) {


    // Initialize packet buffer of size PKTLEN + 1
    uint8 txBuffer[PKTLEN + 1];

    // Infinite loop
    //while(TRUE) {

    // Wait for button push
    //while(!bspKeyPushed(BSP_KEY_ALL));

    // Create a random packet with PKTLEN + 2 byte packet counter + n x random bytes
    enviado++;
    createPacket(txBuffer, enviado);

    // Write packet to TX FIFO
    cc120xSpiWriteTxFifo(txBuffer, sizeof(txBuffer));

    // Strobe TX to send packet
    trxSpiCmdStrobe(CC120X_STX);

    // Wait for packet to be sent
    while(packetSemaphoreTX != ISR_ACTION_REQUIRED);

    // Clear semaphore flag
    packetSemaphoreTX = ISR_IDLE;

    // Update LCD
    //updateLcd();

    // Set radio in RX Sniff Mode
    trxSpiCmdStrobe(CC120X_SWOR);
    //}
    }

    /*******************************************************************************
    * @fn radioTxISR
    *
    * @brief ISR for packet handling in TX. Sets packet semaphore
    * and clears interrupt flag
    *
    * @param none
    *
    * @return none
    */
    static void radioTxISR(void) {

    // Set packet semaphore
    packetSemaphoreTX = ISR_ACTION_REQUIRED;

    // Clear ISR flag
    ioPinIntClear(IO_PIN_PORT_1, GPIO0);
    }

    /*******************************************************************************
    * @fn createPacket
    *
    * @brief This function is called before a packet is transmitted. It fills
    * the txBuffer with a packet consisting of a length byte, two
    * bytes packet counter and n random bytes
    *
    * The packet format is as follows:
    * |--------------------------------------------------------------|
    * | | | | | | |
    * | pktLength | pktCount1 | pktCount0 | rndData |.......| rndData|
    * | | | | | | |
    * |--------------------------------------------------------------|
    * txBuffer[0] txBuffer[1] txBuffer[2] ......... txBuffer[PKTLEN]
    *
    * @param Pointer to start of txBuffer
    *
    * @return none
    */
    static void createPacket(uint8 txBuffer[], uint16 dado) {

    txBuffer[0] = PKTLEN; // Length byte
    txBuffer[1] = (uint8) (packetCounter >> 8); // MSB of packetCounter
    txBuffer[2] = (uint8) packetCounter; // LSB of packetCounter
    txBuffer[3] = (uint8) (dado >> 8);
    txBuffer[4] = (uint8) dado;

    for(uint8 i = 5; i < (PKTLEN + 1); i++) txBuffer[i] = 0;
    // Fill rest of buffer with random bytes
    //for(uint8 i = 3; i < (PKTLEN + 1); i++) {
    // txBuffer[i] = (uint8)rand();
    //}
    }


    /*******************************************************************************
    * @fn initRX
    *
    * @brief Puts radio in RX Sniff Mode and waits for packets.
    *
    * @param none
    *
    * @return none
    */
    static void initRX(void)
    {
    uint8 marcState;

    // Connect ISR function to GPIO2
    ioPinIntRegister(IO_PIN_PORT_1, GPIO2, &radioRxISR);

    // Interrupt on falling edge
    ioPinIntTypeSet(IO_PIN_PORT_1, GPIO2, IO_PIN_RISING_EDGE);

    // Clear ISR flag
    ioPinIntClear(IO_PIN_PORT_1, GPIO2);

    // Enable interrupt
    ioPinIntEnable(IO_PIN_PORT_1, GPIO2);

    // Update LCD
    updateLcd(enviado, recebido);

    // Calibrate radio
    trxSpiCmdStrobe(CC120X_SCAL);

    // Wait for calibration to be done (radio back in IDLE state)
    do {
    cc120xSpiReadReg(CC120X_MARCSTATE, &marcState, 1);
    } while (marcState != 0x41);

    // Calibrate the RCOSC
    calibrateRCOsc();

    // Set radio in RX Sniff Mode
    trxSpiCmdStrobe(CC120X_SWOR);
    }

    /*******************************************************************************
    * @fn runRX
    *
    * @brief Puts radio in RX Sniff Mode and waits for packets. A packet
    * counter is incremented for each packet received and the LCD is
    * updated
    *
    * @param none
    *
    * @return none
    */
    static void runRX(void) {

    uint8 rxBuffer[128] = {0};
    uint8 rxBytes;
    unsigned char str[20];

    // Infinite loop
    //while(TRUE) {

    // Set radio in RX Sniff Mode
    //trxSpiCmdStrobe(CC120X_SWOR);

    // Wait for packet to be received
    while(packetSemaphoreRX != ISR_ACTION_REQUIRED);

    // Clear semaphore flag
    //packetSemaphoreRX = ISR_IDLE;

    // Read number of bytes in RX FIFO
    cc120xSpiReadReg(CC120X_NUM_RXBYTES, &rxBytes, 1);

    // Read all the bytes in the RX FIFO
    cc120xSpiReadRxFifo(rxBuffer, rxBytes);


    recebido = (rxBuffer[3]<<8) + rxBuffer[4];

    sprintf((char *) str, "Consumo tempo %d %d litros\r\n", enviado, recebido);
    uartSendDataInt(&cnf, str, strlen((char *) str));

    // Update LCD
    updateLcd(enviado, recebido);

    //}
    }



    /*******************************************************************************
    * @fn calibrateRcOsc
    *
    * @brief Calibrates the RC oscillator used for the eWOR timer. When this
    * function is called, WOR_CFG0.RC_PD must be 0
    *
    * @param none
    *
    * @return none
    */
    static void calibrateRCOsc(void) {

    uint8 temp;

    // Read current register value
    cc120xSpiReadReg(CC120X_WOR_CFG0, &temp,1);

    // Mask register bit fields and write new values
    temp = (temp & 0xF9) | (0x02 << 1);

    // Write new register value
    cc120xSpiWriteReg(CC120X_WOR_CFG0, &temp,1);

    // Strobe IDLE to calibrate the RCOSC
    trxSpiCmdStrobe(CC120X_SIDLE);

    // Disable RC calibration
    temp = (temp & 0xF9) | (0x00 << 1);
    cc120xSpiWriteReg(CC120X_WOR_CFG0, &temp, 1);
    }


    /*******************************************************************************
    * @fn radioRxISR
    *
    * @brief ISR for packet handling in RX. Sets packet semaphore
    * and clears interrupt flag
    *
    * @param none
    *
    * @return none
    */
    static void radioRxISR(void) {

    // Set packet semaphore
    packetSemaphoreRX = ISR_ACTION_REQUIRED;

    runRX();



    // Clear ISR flag
    ioPinIntClear(IO_PIN_PORT_1, GPIO2);
    }


    /*******************************************************************************
    * @fn initMCU
    *
    * @brief Initialize MCU and board peripherals
    *
    * @param none
    *
    * @return none
    */
    static void initMCU(void) {

    // Init clocks and I/O
    bspInit(BSP_SYS_CLK_8MHZ);

    // Init LEDs
    bspLedInit();

    // Init buttons
    //bspKeyInit(BSP_KEY_MODE_POLL);

    // Initialize SPI interface to LCD (shared with SPI flash)
    bspIoSpiInit(BSP_FLASH_LCD_SPI, BSP_FLASH_LCD_SPI_SPD);

    // Init LCD
    lcdInit();

    // Instantiate transceiver RF SPI interface to SCLK ~ 4 MHz
    // Input parameter is clockDivider
    // SCLK frequency = SMCLK/clockDivider
    trxRfSpiInterfaceInit(2);

    // Enable global interrupt
    _BIS_SR(GIE);
    }


    /*******************************************************************************
    * @fn registerConfig
    *
    * @brief Write register settings as given by SmartRF Studio found in
    * cc1200_rx_sniff_mode_reg_config.h
    *
    * @param none
    *
    * @return none
    */
    static void registerConfig(void) {

    uint8 writeByte;

    // Reset radio
    trxSpiCmdStrobe(CC120X_SRES);

    // Write registers to radio
    for(uint16 i = 0;
    i < (sizeof(preferredSettings)/sizeof(registerSetting_t)); i++) {
    writeByte = preferredSettings[i].data;
    cc120xSpiWriteReg(preferredSettings[i].addr, &writeByte, 1);
    }
    }


    /*******************************************************************************
    * @fn updateLcd
    *
    * @brief Updates LCD buffer and sends buffer to LCD module
    *
    * @param none
    *
    * @return none
    */
    static void updateLcd(uint16 dado1, uint16 dado2) {



    // Update LDC buffer and send to screen
    lcdBufferClear(0);
    lcdBufferPrintString(0, "Concentrador2", 0, eLcdPage0);
    lcdBufferSetHLine(0, 0, LCD_COLS - 1, 7);
    lcdBufferPrintString(0, "Recebido:", 0, eLcdPage3);
    lcdBufferPrintInt(0, dado2, 80, eLcdPage3);

    lcdBufferPrintString(0, "Enviado:", 0, eLcdPage4);
    lcdBufferPrintInt(0, dado1, 80, eLcdPage4);
    //lcdBufferPrintInt(0, packetCounter++, 70, eLcdPage4);
    lcdBufferPrintString(0, "RX", 0, eLcdPage7);
    lcdBufferSetHLine(0, 0, LCD_COLS - 1, 55);
    lcdBufferInvertPage(0, 0, LCD_COLS, eLcdPage7);
    lcdSendBuffer(0);
    }
  • Great,

    I am happy to hear you got it to work. Gustavo is a personal friend, I a have read his post on the MSP430 peripherals and it is very good.

    Regards,
    /TA

**Attention** This is a public forum