#include "msp430x54xA.h" #include "hal_MSP-EXP430F5438.h" #include "hal_lcd.h" #include #include "hal_pmm.h" #define SYSTEM_CLOCK 23986176 #define USB_PORT_OUT P5OUT #define USB_PORT_SEL P5SEL #define USB_PORT_DIR P5DIR #define USB_PORT_REN P5REN #define USB_PIN_TXD BIT6 #define USB_PIN_RXD BIT7 #define USB_PORT_BAUDRATE 57600 #define UART_A3_PSEL P10SEL #define UART_A3_PDIR P10DIR #define UART_A3_TXD_MASK BIT4 #define UART_A3_RXD_MASK BIT5 #define UART_A3_BAUDRATE 57600 char rxBuffer[256]; //Buffer to keep received messages/responses/events from module char rxReadIndex = 0; char rxWriteIndex = 0; volatile char dataReady = 0; //variable to declare is message is completely received (to be implemented) volatile unsigned int i; // volatile to prevent optimization unsigned int SetVCore (unsigned char level); void initSystemClock(); //unsigned char contrast = 0x66; //unsigned char backlight = 8; void lcdStart(); void portsInit(); /************************************** MAIN *****************************************/ /********************** sends command and keeps idle - stop **************************/ /********************** debugging and check memory contents **************************/ /********************** rxBuffer[] should contain response **************************/ unsigned char MST_Data = 0x80; // Initialize data values unsigned char MST_Data2 = 0xFF; unsigned char SLV_Data = 0xE5; // Slave-Data, Roko unsigned char tempvar; int j = 0; // SPI-Configurationfunction void spi_init (); // Code added by Roko uint8_t ADRESS_DATA_FORMAT=0x31; uint8_t ADRESS_BW_RATE=0x2C; uint8_t ADRESS_FIFO_CTL=0x80; uint8_t ADRESS_POWER_CTRL=0x2D; // Code added by Roko uint8_t DATA_FORMAT=0x0A; // 100 Hz output rate uint8_t BW_RATE=0x0A; // uint8_t FIFO_CTL=0x80; // uint8_t POWER_CTRL=0x08; // // Variables for measurement uint8_t DATAX0=0x32; // adress uint8_t DATAX1=0x33; // adress uint8_t DATAY0=0x34; // adress uint8_t DATAY1=0x35; // adress uint8_t DATAZ0=0x36; // adress uint8_t DATAZ1=0x37; // adress // Results measurements uint8_t X0; uint8_t X1; uint8_t Y0; uint8_t Y1; uint8_t Z0; uint8_t Z1; void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer //initSystemClock(); // It's 4 wires, but in reality 3-wire mode for MSP430, since the CS is manually controlled. Still full duplex (and not half) P10DIR |= 0x40; // CS pin. P10SEL |= 0x0E; // or BIT1 + BIT2 + BIT3; UCB3CTL1 |= UCSWRST; // **Put state machine in reset** !!! UCB3CTL0 |= UCMST + UCSYNC + UCCKPL + UCMSB;// + UCCKPH; // 3-pin, 8-bit SPI master //UCB3CTL1 |= UCSSEL_1; // ACLK = ~32.768kHz, UCB3CTL1 |= UCSSEL_2; // SMCLK //UCB3BR0 = 0; // UCB3BR0 = 0x02; // UCB3BR1 = 0; // UCB3CTL1 &= ~UCSWRST; // **Initialize USCI state machine** UCB3IE |= UCRXIE; // Enable USCI_B3 RX interrupt P10OUT |= 0x40; //set high (CS active low) __delay_cycles(100); // Wait for slave to initialize //******* ROUTINE TO SEND //TEST_ID // P10OUT &= ~0x40; //select slave // // while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? // UCB3TXBUF = MST_Data; // Transmit first character // // spi_init(); __bis_SR_register(GIE); // enable global interrupts while(1) { } } #pragma vector=USCI_B3_VECTOR __interrupt void USCI_B3_ISR(void) { switch(__even_in_range(UCB3IV,12)) { case 0: break; // Vector 0 - no interrupt case 2: // Vector 2 - RXIFG P10OUT&=~0x40; //****Measurements*************** UCB3TXBUF=DATAX0; // USCI_B0 TX buffer ready? while (!(UCB3IFG&UCTXIFG)); UCB3TXBUF=0xFF; while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready? X0=UCB3RXBUF; while (!(UCB3STAT&UCBUSY)==0); // UCB3TXBUF = MST_Data2; // Transmit first character, Address(ROKO) UCB3TXBUF=DATAX1; // Transmit first character, Address(ROKO) while (!(UCB3IFG&UCTXIFG)); UCB3TXBUF=0xFF; // Dummy-Byte while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready?? X1=UCB3RXBUF; while (!(UCB3STAT&UCBUSY)==0); while (!(UCB3IFG&UCTXIFG)); UCB3TXBUF=DATAY0; // Transmit first character, Address(ROKO) while (!(UCB3IFG&UCTXIFG)); UCB3TXBUF=0xFF; // Dummy-Byte while (!(UCB3IFG&UCRXIFG)); // USCI_B0 TX buffer ready? Y0=UCB3RXBUF; while (!(UCB3STAT&UCBUSY)==0); while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=DATAY1; while (!(UCB3IFG&UCTXIFG)); UCB3TXBUF=0xFF; // Dummy-Byte while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready? Y1=UCB3RXBUF; while (!(UCB3STAT&UCBUSY)==0); // Transmit first character, Address(ROKO) while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=DATAZ0; // Transmit first character, Address(ROKO) while (!(UCB3IFG&UCTXIFG)); UCB3TXBUF=0xFF; // Dummy-Byte while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready? Z0=UCB3RXBUF; while (!(UCB3STAT&UCBUSY)==0); while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=DATAZ1; // Transmit first character, Address(ROKO) while (!(UCB3IFG&UCTXIFG)); UCB3TXBUF=0xFF; // Dummy-Byte while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready? Z1=UCB3RXBUF; while (!(UCB3STAT&UCBUSY)==0); P10OUT|=0x40; // while (!(UCB3IFG&UCTXIFG)); // USCI_A0 TX buffer ready? // tempvar = UCB3RXBUF; // if (tempvar == SLV_Data) // Test for correct character RX'd // tempvar = 0xAA; //tempvar=0xAA; // else // UCB3TXBUF = MST_Data2; // Send next value (Roko, sending Dummy byte) // j++; // Zeile zum Debuggen __delay_cycles(40); // Add time between transmissions to // make sure slave can process information (seems to avoid putting CS to high) // if(j==2) // tempvar++; break; case 4: break; // Vector 4 - TXIFG default: break; } } void spi_init () { //******** Configuration ***** Roko while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=ADRESS_DATA_FORMAT; // Transmit first character, Address(ROKO) while (!(UCB3STAT&UCBUSY)==0); while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=DATA_FORMAT; // Transmit second character, Address(ROKO) while (!(UCB3STAT&UCBUSY)==0); P10OUT|=0x40; __delay_cycles(40); //------------------------------- P10OUT&=~0x40; while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=ADRESS_BW_RATE; // Transmit first character, Address(ROKO) while (!(UCB3STAT&UCBUSY)==0); while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=BW_RATE; // Transmit second character, Address(ROKO) while (!(UCB3STAT&UCBUSY)==0); P10OUT|=0x40; __delay_cycles(40); //-------------------------------- P10OUT&=~0x40; while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=ADRESS_FIFO_CTL; // Transmit first character, Address(ROKO) while (!(UCB3STAT&UCBUSY)==0); while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=FIFO_CTL; // Transmit second character, Address(ROKO) while (!(UCB3STAT&UCBUSY)==0); P10OUT|=0x40; __delay_cycles(40); //--------------------------------- P10OUT&=~0x40; while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=ADRESS_POWER_CTRL; // Transmit first character, Address(ROKO) while (!(UCB3STAT&UCBUSY)==0); P10OUT|=0x40; __delay_cycles(40); //--------------------------------------- P10OUT&=~0x40; while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready? UCB3TXBUF=POWER_CTRL; // Transmit first character, Address(ROKO) while (!(UCB3STAT&UCBUSY)==0); P10OUT|=0x40; __delay_cycles(40); //---------------------------------------- };