#include "inc/hw_memmap.h" #include "driverlib/5xx_6xx/ucs.h" #include "driverlib/5xx_6xx/wdt.h" #include "driverlib/5xx_6xx/gpio.h" #include "driverlib/5xx_6xx/spi.h" #include "driverlib/5xx_6xx/uart.h" //Function Declarations void uartprint(unsigned char *word_uart); void spiprint(unsigned char *word_spi); //Definitions for UART #define BAUD_RATE 9600 unsigned char receivedData_UART = 0x00; unsigned char transmitData_UART = 0x00; unsigned char init_uart[] ="UART INITIATED\r\n"; //Definitions for SPI #define SPICLK 1000000 unsigned char receivedData_SPI = 0x00; unsigned char transmitData_SPI = 0x00; unsigned char init_spi[] = "SPITRANSFER"; //General Definitions unsigned int i; /*######################################################################################################*/ //Main Segment void main(void) { //Stop the Watch dog timer WDT_hold(__MSP430_BASEADDRESS_WDT_A__); //Enable the LED Gpios P1.0,P1.1 GPIO_setAsOutputPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN0); GPIO_setAsOutputPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN1); //Initialisation of UART (P5.6(UCA1TXD),P5.7(UCA1RXD)). //Both Pins are connected to TUSB3410 UART to USB Bridge IC through Jumper JP5. GPIO_setAsPeripheralModuleFunctionOutputPin(__MSP430_BASEADDRESS_PORT5_R__,GPIO_PORT_P5,GPIO_PIN6);//TXD GPIO_setAsPeripheralModuleFunctionInputPin(__MSP430_BASEADDRESS_PORT5_R__,GPIO_PORT_P5,GPIO_PIN7);//RXD if ( STATUS_FAIL == UART_init(__MSP430_BASEADDRESS_USCI_A1__, UART_CLOCKSOURCE_SMCLK, UCS_getSMCLK(__MSP430_BASEADDRESS_UCS__), BAUD_RATE, UART_NO_PARITY, UART_LSB_FIRST, UART_ONE_STOP_BIT, UART_MODE, UART_LOW_FREQUENCY_BAUDRATE_GENERATION )) { return; } // Initialisation of SPI (P3.4(MOSI or UCA0SIMO ),P3.5(MISO or UCA0SOMI),P3.0(SCLK or UCA0CLK)),P1.7(Chip Enable). GPIO_setAsPeripheralModuleFunctionOutputPin(__MSP430_BASEADDRESS_PORT3_R__,GPIO_PORT_P3,GPIO_PIN4);//MOSI GPIO_setAsPeripheralModuleFunctionInputPin(__MSP430_BASEADDRESS_PORT3_R__,GPIO_PORT_P3,GPIO_PIN5);//MISO GPIO_setAsPeripheralModuleFunctionOutputPin(__MSP430_BASEADDRESS_PORT3_R__,GPIO_PORT_P3,GPIO_PIN0);//SCLK GPIO_setAsOutputPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN7);//Chip enable. //SPI with Clock Polarity = 0 and Clock Phase = 0; if(STATUS_FAIL == SPI_masterInit(__MSP430_BASEADDRESS_USCI_A0__,SPI_CLOCKSOURCE_SMCLK, UCS_getSMCLK(__MSP430_BASEADDRESS_UCS__), SPICLK, SPI_MSB_FIRST, SPI_PHASE_DATA_CAPTURED_ONFIRST_CHANGED_ON_NEXT, SPI_CLOCKPOLARITY_INACTIVITY_LOW))//Refer memap.h for STATUS_FAIL. { return; } //Enable Both UART and SPI .Indicate them with LEDs. UART_enable(__MSP430_BASEADDRESS_USCI_A1__); GPIO_setOutputHighOnPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN0); // SPI_enable(__MSP430_BASEADDRESS_USCI_A0__); // GPIO_setOutputHighOnPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN1); /*Enabling the Receive Data Interrupts for UART and SPI* Note: We have not enabled the Transmit data Interrupts. Here we are transmitting data through Polling. */ UART_enableInterrupt(__MSP430_BASEADDRESS_USCI_A1__,UART_RECEIVE_INTERRUPT);//Enabling Receive interrupt only. SPI_enableInterrupt(__MSP430_BASEADDRESS_USCI_A0__, SPI_RECEIVE_INTERRUPT);//Enabling Receive interrupt only. uartprint(init_uart); __bis_SR_register(GIE); //For Sending a Character in SPI GPIO_setOutputLowOnPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN7); SPI_transmitData(__MSP430_BASEADDRESS_USCI_A0__, init_spi[0]); while (!SPI_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A0__,SPI_TRANSMIT_INTERRUPT)) ; //For Sending a string in SPI spiprint(init_spi); //For Testing the MSP430 as a Master Receiver.Already You have enabled the interrupts. __bis_SR_register(LPM3_bits);//Enabling the Global Interrupt enable bit __no_operation(); } /*########################################################################################################*/ //Fuction Definitions void uartprint(unsigned char *word_uart) { unsigned int j_uart=0; unsigned char *temp_uart; temp_uart=word_uart; do { UART_transmitData(__MSP430_BASEADDRESS_USCI_A1__,temp_uart[j_uart]); /*Transmit Interrupt Flag sets when the Transmission Buffer is Empty.UART_getInterruptstatus() will give 1 if the Transmit interrupt flag is set in the Interrupt Flag register. Since You have not enabled Transmit interrupt,there will be no interrupt & the Transmit Flag bit will automatically clears once u write another data to the buffer */ while (!UART_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A1__,UART_TRANSMIT_INTERRUPT_FLAG)) ; j_uart++; }while(temp_uart[j_uart] != '\0'); } void spiprint(unsigned char *word_spi) { unsigned int j_spi=0; unsigned char *temp_spi; temp_spi=word_spi; do { GPIO_setOutputLowOnPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN7); SPI_transmitData(__MSP430_BASEADDRESS_USCI_A0__, temp_spi[j_spi]); while (!SPI_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A0__,SPI_TRANSMIT_INTERRUPT)) ; //__delay_cycles(1000); //UART_transmitData(__MSP430_BASEADDRESS_USCI_A1__,temp_spi[j_spi]); //while (!UART_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A1__,UART_TRANSMIT_INTERRUPT_FLAG)) ; //while (SPI_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A0__,SPI_RECEIVE_INTERRUPT)) ; __delay_cycles(10000); //GPIO_setOutputHighOnPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN7); j_spi++; //__delay_cycles(300); }while(temp_spi[j_spi]!='\0'); //}while(j_spi!=11); } /*########################################################################################################*/ //UART Interrupt Service Routine #pragma vector=USCI_A1_VECTOR __interrupt void USCI_A1_ISR (void) { switch (__even_in_range(UCA1IV,4)) { //Vector 2 - RXIFG case 2: //Echo back RXed character, confirm TX buffer is ready first as we are going to echo thru it. //USCI_A1 TX buffer ready? while (!UART_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A1__,UART_TRANSMIT_INTERRUPT_FLAG)) ; //Receive echoed data receivedData_UART = UART_receiveData(__MSP430_BASEADDRESS_USCI_A1__); //Transmit next data UART_transmitData(__MSP430_BASEADDRESS_USCI_A1__,receivedData_UART); break; default: break; } } //SPI Interrupt Service Routine. #pragma vector=USCI_A0_VECTOR __interrupt void USCI_A0_ISR (void) { unsigned char isr_uart[]="\r\nINSIDE ISR\r\n"; unsigned char bala = 'A'; GPIO_setOutputHighOnPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN7); switch (__even_in_range(UCA0IV,4)){ //Vector 2 - RXIFG case 2: uartprint(isr_uart); //USCI_A0 TX buffer ready? //Checking whether the TX Buffer has sent the previous data because we have to receive the //data once the transmit Buffer finishes sending the data. //If sent then the value of SPI_getInterruptstatus will be 1 and the while fails. //while (!SPI_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A0__,SPI_TRANSMIT_INTERRUPT)) ; //Use of below line is up to u, if u need a full duplex communication with slave. receivedData_SPI = SPI_receiveData(__MSP430_BASEADDRESS_USCI_A0__); while (SPI_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A0__,SPI_RECEIVE_INTERRUPT)) ; // __delay_cycles(100); UART_transmitData(__MSP430_BASEADDRESS_USCI_A1__,bala); while (!UART_getInterruptStatus(__MSP430_BASEADDRESS_USCI_A1__,UART_TRANSMIT_INTERRUPT_FLAG)) ; //GPIO_setOutputHighOnPin(__MSP430_BASEADDRESS_PORT1_R__,GPIO_PORT_P1,GPIO_PIN7); //Increment data //transmitData_SPI++; //Send next value //SPI_transmitData(__MSP430_BASEADDRESS_USCI_A0__,transmitData_SPI); //Delay between transmissions for slave to process information __delay_cycles(1000); break; default: break; } }