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.

cc2530 and uart

Other Parts Discussed in Thread: CC2530

i am working on uart on cc2530 and i have been reading DN 112"http://www.ti.com/lit/an/swra222b/swra222b.pdf"

i wrote the code in iar embedded workbench and it does not work 

i trace the program and found that:

for (uartTxIndex = 0; uartTxIndex < uartTxBufLength; uartTxIndex++)

{

U0DBUF = uartTxBuf[uartTxIndex];

while( !UTX0IF );

UTX0IF = 0;

}

 

in that code the command  U0DBUF = uartTxBuf[uartTxIndex];   is not executed as the value of the register  U0DBUF  does not change during the for loop

 

thanks in advance

the complete code

 

#include <iocc2530.h>

// C language code: 

// Define and allocate a setup structure for the UART protocol: 

 

typedef struct { 

  unsigned char uartNum               : 1;  // UART peripheral number (0 or 1) 

  unsigned char START                 : 1;  // Start bit level (low/high) 

  unsigned char STOP                  : 1;  // Stop bit level (low/high) 

  unsigned char SPB                   : 1;  // Stop bits (0 => 1, 1 => 2) 

  unsigned char PARITY                : 1;  // Parity control (enable/disable) 

  unsigned char BIT9                  : 1;  // 9 bit enable (8bit / 9bit) 

  unsigned char D9                    : 1;  // 9th bit level or Parity type 

  unsigned char FLOW                  : 1;  // HW Flow Control (enable/disable) 

  unsigned char ORDER                 : 1;  // Data bit order(LSB/MSB first) 

} UART_PROT_CONFIG; 

 

UART_PROT_CONFIG __xdata uartProtConfig; 

 

// Define size of allocated UART RX/TX buffer (just an example) 

#define SIZE_OF_UART_RX_BUFFER   50 

#define SIZE_OF_UART_TX_BUFFER   SIZE_OF_UART_RX_BUFFER 

 

// Allocate buffer+ind

unsigned short __xdata uartRxBuffer[SIZE_OF_UART_RX_BUFFER]; 

unsigned short __xdata uartTxBuffer[SIZE_OF_UART_TX_BUFFER]; 

unsigned short __xdata uartRxIndex, uartTxIndex;

 

// C language code: 

 

// This function maps/connects the UART to the desired SoC I/O port. 

// The application should call this function with "uartPortAlt" = 1 or 2, 

// and "uartNum" = 0 or 1. 

 

void uartMapPort(unsigned char uartPortAlt, unsigned char uartNum) 

 

  // If UART Port Alternative 1 desired 

  if(uartPortAlt == 1)

  { 

    // If UART0 desired 

    if (uartNum == 0)

    { 

      // Configure UART0 for Alternative 1 => Port P0 (PERCFG.U0CFG = 0) 

     PERCFG &= ~0x01; 

      // 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 |= 0x3C; 

      // Configure relevant Port P1 pins back to GPIO function 

      P1SEL &= ~0x3C; 

    // Else (UART1 desired) 

    } 

    else

    { 

      // Configure UART1 for Alternative 1 => Port P0 (PERCFG.U1CFG = 0) 

      PERCFG &= ~0x02; 

      // Configure relevant Port P0 pins for peripheral function: 

      // P0SEL.SELP0_2/3/4/5 = 1 => CT = P0_2, RT = P0_3, TX = P0_4, RX = P0_5 

      P0SEL |= 0x3C; 

      // Configure relevant Port P1 pins back to GPIO function 

      P1SEL &= ~0xF0; 

    } 

  // Else (UART Port Alternative 2 desired) 

  } 

  else

  { 

    // If UART0 desired 

    if (uartNum == 0) { 

      // Configure UART0 for Alternative 2 => Port P1 (PERCFG.U0CFG = 1) 

      PERCFG |= 0x01; 

      // P1SEL.SELP1_2/3/4/5 = 1 => CT = P1_2, RT = P1_3, RX = P1_4, TX = P1_5 

      P1SEL |= 0x3C; 

      // Configure relevant Port P0 pins back to GPIO function 

      P0SEL &= ~0x3C; 

    // Else (UART1 desired) 

    }

    else

    { 

      // Configure UART1 for Alternative 2 => Port P1 (PERCFG.U1CFG = 1) 

      PERCFG |= 0x02; 

      // P1SEL.SELP1_4/5/6/7 = 1 => CT = P1_4, RT = P1_5, TX = P1_6, RX = P1_7 

      P1SEL |= 0xF0; 

      // Configure relevant Port P0 pins back to GPIO function 

      P0SEL &= ~0x3C; 

    } 

  } 

 

}

 

// C language code: 

 

// This function initializes the UART bit rate. 

 

void uartInitBitrate(unsigned char uartBaudM, unsigned char uartBaudE) { 

 

  /////////////////////////////////////////////////////////////// 

  // This initial code section ensures that the SoC system clock is driven 

  // by the HS XOSC: 

 

  // Clear CLKCON.OSC to make the SoC operate on the HS XOSC. 

  // Set CLKCON.TICKSPD/CLKSPD = 000 => system clock speed = HS RCOSC speed. 

  CLKCONCMD &= 0x80; 

 

  // Monitor CLKCON.OSC to ensure that the HS XOSC is stable and actually 

  // applied as system clock source before continuing code execution 

  while(CLKCONCMD & 0x40); 

 

  // Set SLEEP.OSC_PD to power down the HS RCOSC. 

  //SLEEP |= 0x04; 

  /////////////////////////////////////////////////////////////// 

 

  // Initialize bitrate (U0BAUD.BAUD_M, U0GCR.BAUD_E) 

  U0BAUD = uartBaudM; 

  U0GCR = (U0GCR&~0x1F) | uartBaudE; 

}

 

// C language code: 

// This function initializes the UART protocol (start/stop bit, data bits, 

// parity, etc.). The application must call this function with an initialized 

// data structure according to the code in Figure 12. 

 

void uartInitProtocol(UART_PROT_CONFIG* uartProtConfig) { 

 

  // Initialize UART protocol for desired UART (0 or 1) 

  if (uartProtConfig->uartNum == 0)

  { 

    // USART mode = UART (U0CSR.MODE = 1) 

    U0CSR |= 0x80; 

 

    // Start bit level = low => Idle level = high (U0UCR.START = 0) 

    // Start bit level = high => Idle level = low (U0UCR.START = 1) 

   U0UCR = (U0UCR&~0x01) | uartProtConfig->START; 

 

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

    // Stop bit level = low (U0UCR.STOP = 0) 

     U0UCR = (U0UCR&~0x02) | (uartProtConfig->STOP << 1); 

 

     // Number of stop bits = 1 (U0UCR.SPB = 0) 

    // Number of stop bits = 2 (U0UCR.SPB = 1) 

    U0UCR = (U0UCR&~0x04) | (uartProtConfig->SPB << 2); 

 

    // Parity = disabled (U0UCR.PARITY = 0) 

    // Parity = enabled (U0UCR.PARITY = 1) 

    U0UCR = (U0UCR&~0x08) | (uartProtConfig->PARITY << 3); 

 

    // 9-bit data disable = 8 bits transfer (U0UCR.BIT9 = 0) 

    // 9-bit data enable = 9 bits transfer (U0UCR.BIT9 = 1) 

    U0UCR = (U0UCR&~0x10) | (uartProtConfig->BIT9 << 4); 

 

    // 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&~0x20) | (uartProtConfig->D9 << 5); 

 

    // Flow control = disabled (U0UCR.FLOW = 0) 

    // Flow control = enabled (U0UCR.FLOW = 1) 

    U0UCR = (U0UCR&~0x40) | (uartProtConfig->FLOW << 6); 

 

    // Bit order = MSB first (U0GCR.ORDER = 1) 

    // Bit order = LSB first (U0GCR.ORDER = 0) => For PC/Hyperterminal 

    U0GCR = (U0GCR&~0x20) | (uartProtConfig->ORDER << 5); 

  } 

  else 

  { 

    // USART mode = UART (U1CSR.MODE = 1) 

    U1CSR |= 0x80; 

    // Start bit level = low => Idle level = high (U1UCR.START = 0) 

    // Start bit level = high => Idle level = low (U1UCR.START = 1) 

    U1UCR = (U1UCR&~0x01) | uartProtConfig->START; 

    // Stop bit level = high (U1UCR.STOP = 1) 

    // Stop bit level = low (U1UCR.STOP = 0) 

    U1UCR = (U1UCR&~0x02) | (uartProtConfig->STOP << 1); 

    // Number of stop bits = 1 (U1UCR.SPB = 0) 

    // Number of stop bits = 2 (U1UCR.SPB = 1) 

    U1UCR = (U1UCR&~0x04) | (uartProtConfig->SPB << 2); 

    // Parity = disabled (U1UCR.PARITY = 0) 

    // Parity = enabled (U1UCR.PARITY = 1) 

    U1UCR = (U1UCR&~0x08) | (uartProtConfig->PARITY << 3); 

    // 9-bit data enable = 8 bits transfer (U1UCR.BIT9 = 0) 

    // 9-bit data enable = 8 bits transfer (U1UCR.BIT9 = 1) 

    U1UCR = (U1UCR&~0x10) | (uartProtConfig->BIT9 << 4); 

    // Level of bit 9 = 0 (U1UCR.D9 = 0), used when U1UCR.BIT9 = 1 

    // Level of bit 9 = 1 (U1UCR.D9 = 1), used when U1UCR.BIT9 = 1 

    // Parity = Even (U1UCR.D9 = 0), used when U1UCR.PARITY = 1 

    // Parity = Odd (U1UCR.D9 = 1), used when U1UCR.PARITY = 1 

    U1UCR = (U1UCR&~0x20) | (uartProtConfig->D9 << 5); 

    // Flow control = disabled (U1UCR.FLOW = 0) 

    // Flow control = enabled (U1UCR.FLOW = 1) 

    U1UCR = (U1UCR&~0x40) | (uartProtConfig->FLOW << 6); 

    // Bit order = MSB first (U1GCR.ORDER = 1) 

    // Bit order = LSB first (U1GCR.ORDER = 0) => For PC/Hyperterminal 

    U1GCR = (U1GCR&~0x20) | (uartProtConfig->ORDER << 5); 

  } 

}

 

// C language code: 

// The two functions below send a range of bytes on the UARTx TX line. Note 

// that, before the relevant function is called the application must execute 

// the initialization code in Figure 3, Figure 11, Figure 12, and Figure 13. 

 

// The code implements the following steps: 

// 1. Clear TX interrupt request (UTXxIF = 0). 

// 2. Loop: send each UARTx source byte on the UARTx TX line. 

// 2a. Read byte from the allocated UART TX source buffer and write to UxDBUF. 

// 2b. Wait until UART byte has been sent (UTXxIF = 1). 

// 2c. Clear UTXxIF. 

 

void uart0Send(unsigned char* uartTxBuf, unsigned short uartTxBufLength) 

{

unsigned short uartTxIndex;

UTX0IF = 0;

for (uartTxIndex = 0; uartTxIndex < uartTxBufLength; uartTxIndex++)

{

U0DBUF = uartTxBuf[uartTxIndex];

while( !UTX0IF );

UTX0IF = 0;

}

}

 

void uart0Str(unsigned char uartTxStr[])

{

    uart0Send(uartTxStr,20);

}

void delay()

{

  for(int i=0;i<1000;i++)

    for(int j=0;j<100;j++)

      ;

}

unsigned char s[20] = "Helllloooo\n\r\0";

void main()

{

  P0DIR |= 0x40;

  P0DIR |= 0x01;

  P0_0 = 0;

 

  uartMapPort(1,0);

  uartInitBitrate(59,8);

  UART_PROT_CONFIG h = {0,0,1,0,0,0,0,0,0};

  uartInitProtocol(&h);

 

  while(1)

  {

    uart0Str(s);

    delay();

    P0_6 = !P0_6;

  }

}