/*********************************************************************************** * @fn appRfReceiverTask * * @brief Check if a new packet has been received. If a new packet * is received the payload is sent to the UART. * * @param none * * @return none */ static void appRfReceiverTask(void) { if (mrfiLinkDataRdy()) { uint8 nToSend; uint8 fSuccess; // Tell the PC not to send data halUartEnableRxFlow(FALSE); // Wait for the PC to respond halMcuWaitUs(1000); // Receive RF data nToSend = mrfiLinkRecv(pRxData); // If reception successful, send packet to UART fSuccess= FALSE; if(nToSend>0) { if (halUartWrite(pRxData,nToSend)==nToSend) fSuccess= TRUE; } if (!fSuccess) { nRxErr++; appUpdateDisplay(); } // Signal RX flow on, the PC may send data again halUartEnableRxFlow(TRUE); } }
/*********************************************************************************** * @fn appRfSenderTask * * @brief Checks if new bytes have arrived from the UART. If there * are enough bytes to fill a maximal sized packet, or if the UART * is idle, the bytes are transmitted on the air. * * @param none * * @return none */ static void appRfSenderTask(void) { uint8 nBytes; uint8 payloadLength; uint8 bytesToRead; nBytes = halUartGetNumRxBytes(); payloadLength= 0; bytesToRead= 0; if(nBytes >= APP_PAYLOAD_LENGTH || (appUartRxIdle && nBytes>0) ) { // Signal PC not to send on UART, while sending on air. halUartEnableRxFlow(FALSE); // Wait for PC to respond halMcuWaitUs(1000); bytesToRead = MIN(nBytes, APP_PAYLOAD_LENGTH); halUartRead(pTxData,bytesToRead); payloadLength+= bytesToRead; halLedToggle(3); if( (mrfiLinkSend(pTxData, payloadLength,N_RETRIES)) != MRFI_TX_RESULT_SUCCESS) { nTxErr++; appUpdateDisplay(); } // Signal RX flow on halUartEnableRxFlow(TRUE); // Restart idle timer halTimer32kRestart(); halTimer32kIntEnable(); // Reset idle fimer flag appUartRxIdle = FALSE; } }
/*********************************************************************************** * @fn halUartInit * * @brief Initalise UART. Supported baudrates are: 19220, 38400, 57600 and 115200 * * @param uint8 baudrate * uint8 options - this parameter is ignored * * @return none */ void halUartInit(uint8 baudrate, uint8 options) { // For the moment, this UART implementation only // supports communication 8N1 // i.e. ignore options arguments. UCA0CTL1 |= UCSWRST; // Keep USART1 in reset state UCA0CTL1 |= UCSSEL1; // Set clock source SMCLK UCA0CTL1 &= ~UCSSEL0; P3SEL |= BIT4; // P3.4 = USART1 TXD P3SEL |= BIT5; // P3.5 = USART1 RXD switch (baudrate) { case HAL_UART_BAUDRATE_9600: UCA0BR0 = 0x41; // 8MHz 9600 UCA0BR1 = 0x03; // 8MHz 9600 break; case HAL_UART_BAUDRATE_19200: UCA0BR0 = 0xA0; // 8MHz 19200 UCA0BR1 = 0x01; // 8MHz 19200 break; case HAL_UART_BAUDRATE_38400: UCA0BR0 = 0xD0; // 8MHz 38400 UCA0BR1 = 0x00; // 8MHz 38400 break; case HAL_UART_BAUDRATE_57600: UCA0BR0 = 0x8A; // 8MHz 57600 UCA0BR1 = 0x00; // 8MHz 57600 break; case HAL_UART_BAUDRATE_115200: UCA0BR0 = 0x45; // 8MHz 115200 UCA0BR1 = 0x00; // 8MHz 115200 break; default: break; } UCA0CTL0 &= ~UCPEN; // No parity UCA0CTL0 &= ~UCSPB; // 1 stop bit UCA0CTL0 &= ~UC7BIT; // 8 data bits UCA0CTL1 &= ~UCSWRST; // Initialize USART1 state machine //bufInit(&rbRxBuf); // Enable RX interrupt halUartRxIntEnable(); // Set RTS pin to output //HAL_RTS_DIR_OUT(); // Enable RX Flow halUartEnableRxFlow(TRUE); rx_in_progress = 0; transmitting = 0; }