Other Parts Discussed in Thread: CC1111EMK868-915
Dear All,
I've been having a lot of trouble setting up the USART1 to be used in alternative location 1 for UART. I've tested USART0 with much success, however, I am unable to repeat this with USART1. If anyone can have a quick look over this code, and let me know if there are any problems that would be great, else I'm at a loss as to why it isn't working.
//-----------Author: Siôn Nicholas Abraham, 07/01/2018 //-----------Ground Side C Code for AstonSAT communications at 838Mhz //-----------Designed for the CC111EM USB Dongle (Make note of XDATA Registers) //-----------Aston University, MEng Electrical and Electronic Engineering /*********************************************************************************** * INCLUDES */ #include "hal_types.h" #include "ioCCxx11_bitdef.h" #include "hal_mcu.h" #include "hal_board.h" #include "stdlib.h" /*********************************************************************************** * CONSTANTS */ #define N 100 #define SFSTXON 0x00 // Enable and calibrate frequency synthesizer #define SCAL 0x01 // Calibrate frequency synthesizer and turn it off #define SRX 0x02 // Enable RX #define STX 0x03 // Enable TX #define SIDLE 0x04 // Enter IDLE state (frequency synthesizer turned off) // Baudrate = 9600 #define UART_BAUD_M 163 #define UART_BAUD_E 8 #define SIZE_OF_UART_TX_BUFFER N /*********************************************************************************** * LOCAL VARIABLES */ uint8 txBuffer[N + 1]; uint8 rxBuffer[N + 3]; char asciiString[11]; uint32 packetsSent; uint32 packetsReceived; static uint16 __xdata uartTxBuffer[SIZE_OF_UART_TX_BUFFER]; static uint16 __xdata uartTxIndex; /*********************************************************************************** * LOCAL FUNCTIONS */ void uart1StartTxForIsr(void); void intToAscii(uint32 value); void delay(void); /*********************************************************************************** * Main Loop */ int main(void) { uint8 length; // Enable HS XOSC and set maximum clock speed halMcuInit(); // Initialise the UART PERCFG &= ~PERCFG_U1CFG; // Configure USART1 for Alternative 1 => Port P0 P0SEL |= BIT4 | BIT5; // P0SEL.SELP0_4/5 = 1 => RX = P0_5, TX = P0_4 U1BAUD = UART_BAUD_M; U1GCR = (U1GCR&~U1GCR_BAUD_E) | UART_BAUD_E; // Initialise bitrate = 9600 U1CSR |= U1CSR_MODE; //Sets USART0 to UART Mode U1UCR &= ~U1UCR_START; // Start bit level = low => Idle level = high U1UCR |= U1UCR_STOP; // Stop bit level = high U1UCR &= ~U1UCR_SPB; // Number of stop bits = 1 U1UCR &= ~U1UCR_PARITY; // Parity = disabled U1UCR &= ~U1UCR_BIT9; // 9-bit data enable = 8 bits transfer U1UCR &= ~U1UCR_D9; U1UCR &= ~U1UCR_FLOW; // Flow control = disabled U1GCR &= ~U1GCR_ORDER; // Bit order = LSB first // Turn off LED P0DIR |= 0x0F; P0 &= ~0x0F; // Configure the radio /* Address Config = No address check */ /* Base Frequency = 868.299683 */ /* CRC Enable = true */ /* Carrier Frequency = 868.299683 */ /* Channel Number = 0 */ /* Channel Spacing = 199.951172 */ /* Data Rate = 38.3606 */ /* Deviation = 20.507813 */ /* Device Address = 0 */ /* Manchester Enable = false */ /* Modulated = true */ /* Modulation Format = GFSK */ /* PA Ramping = false */ /* Packet Length = 255 */ /* Packet Length Mode = Variable packet length mode. Packet length configured by the first byte after sync word */ /* Preamble Count = 4 */ /* RX Filter BW = 93.750000 */ /* Sync Word Qualifier Mode = 30/32 sync word bits detected */ /* TX Power = 0 */ /* Whitening = false */ PKTCTRL0 = 0x05; FSCTRL1 = 0x06; FREQ2 = 0x24; FREQ1 = 0x2D; FREQ0 = 0xDD; MDMCFG4 = 0xCA; MDMCFG3 = 0xA3; MDMCFG2 = 0x13; MDMCFG1 = 0x23; MDMCFG0 = 0x11; DEVIATN = 0x36; MCSM0 = 0x18; FOCCFG = 0x16; AGCCTRL2 = 0x43; FSCAL3 = 0xE9; FSCAL2 = 0x2A; FSCAL1 = 0x00; FSCAL0 = 0x1F; TEST1 = 0x31; TEST0 = 0x09; PA_TABLE0 = 0x50; IOCFG0 = 0x06; PKTLEN = N; RFIF = 0; RFTXRXIF = 0; P0 |= 0x01; halMcuWaitMs(1000); P0 |= 0x02; halMcuWaitMs(1000); P0 |= 0x04; halMcuWaitMs(1000); P0 |= 0x08; halMcuWaitMs(1000); P0 &= ~0x0F; while (TRUE) { // Wait for button while ((P1 & 0x01)); halMcuWaitMs(200); // Turn on LED P0 |= 0x0F; // Create TX Packet txBuffer[0] = N; for (uint8 i = 1; i <= N; i++) { txBuffer[i] = i; } uart1StartTxForIsr(); //Sends all the TxBuffer data down the UART RFST = STX; while (!RFTXRXIF); RFTXRXIF = 0; RFD = txBuffer[0]; for (uint8 m = 1; m <= N; m++) { while (!RFTXRXIF); RFTXRXIF = 0; RFD = txBuffer[m]; } while ((RFIF & 0x10) == 0); // Wait for IRQ_DONE RFIF &= ~0x10; RFTXRXIF = 0; halMcuWaitMs(100); RFST = SRX; while (!RFTXRXIF); RFTXRXIF = 0; length = RFD; rxBuffer[0] = length; for (uint8 j = 1; j <= length + 2; j++) { while (!RFTXRXIF); RFTXRXIF = 0; rxBuffer[j] = RFD; } if(rxBuffer[0] == length) { P0 ^= 0x0F; } } //-------------------------------------------------------------------------- } void uart1StartTxForIsr(void) { for (int i = 0; i < N; i++) { uartTxBuffer[i] = txBuffer[i]; } // Initialize the UART TX buffer indexes. uartTxIndex = 0; // Clear any pending UART TX Interrupt Flag (IRCON2.UTXxIF = 0, UxCSR.TX_BYTE = 0) UTX1IF = 0; U1CSR &= ~U1CSR_TX_BYTE; // Send very first UART byte U1DBUF = uartTxBuffer[uartTxIndex++]; // Enable global interrupt (IEN0.EA = 1) and UART TX Interrupt (IEN2.UTXxIE = 1) EA = 1; IEN2 |= IEN2_UTX1IE; } #pragma vector = UTX1_VECTOR __interrupt void UART1_TX_ISR(void) { // Clear UART0 TX Interrupt Flag (IRCON2.UTX0IF = 0) UTX1IF = 0; // If no UART byte left to transmit, stop this UART TX session 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_UTX1IE; return; } // Send next UART byte U1DBUF = uartTxBuffer[uartTxIndex++]; }
Kindest Regards,
Siôn Abraham