Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "msp430g2553.h"
- #include "stdint.h"
- volatile char txData[32], rxData[32] = 1;
- volatile char rxDatum; // debug purposes.
- volatile unsigned int i =0; // global counter
- char myPayload = 0;
- char addrPos = 0; // start address suffix.
- #define PWR_DWN 0
- #define STND_BY 1
- #define READ_RX 2
- #define SET_RX 3
- #define SET_TX 4
- #define WRIT_CFG 0x00 // read configuration command prefix.
- #define READ_CFG 0x10 // write configuration command prefix.
- #define WRIT_TX 0x20 // write tx payload command.
- #define READ_TX 0x21 // read tx payload command.
- #define LED1 BIT0
- #define MISO BIT1
- #define uCK BIT1
- #define MOSI BIT2
- #define CD BIT2
- #define CSN BIT3
- #define DR BIT3
- #define SCK BIT4
- #define AM BIT4
- #define CE BIT5
- #define PWR BIT6
- #define LED2 BIT6
- #define TX_EN BIT7
- /* Initialize USCI_A for SPI */
- void spi_init(void) {
- P1SEL |= BIT1 | BIT2 | BIT4; // Configure port.
- P1SEL2 |= BIT1 | BIT2 | BIT4; // ^
- /* USCI-A specific SPI setup */
- UCA0CTL1 |= UCSWRST;
- UCA0MCTL = 0x00; // Clearing modulation control per TI user's guide recommendation
- UCA0CTL0 = UCCKPH | UCMSB | UCMST | UCMODE_0 | UCSYNC; // SPI mode 0, master
- UCA0BR0 = 0x01; // SPI clocked at same speed as SMCLK
- UCA0BR1 = 0x00; // ^
- UCA0CTL1 = UCSSEL_2; // Clock = SMCLK, clear UCSWRST and enables USCI_A module.
- }
- /* Kill SPI module */
- void spi_kill(void) {
- UCA0CTL1 | UCSWRST; // put UCSI in reset mode.
- }
- /* Read radio configuration from SPI */
- char spi_read_cfg() {
- P1OUT &= ~CSN; // Pull CSN down.
- while (UCA0STAT & UCBUSY); // Delay while SPI busy.
- UCA0TXBUF = 0x10; // Fill tx buffer to send data.
- while (!(IFG2&UCA0RXIFG)); // Delay til RX flag reset.
- P1OUT |=CSN; // Pull CSN back up.
- return UCA0RXBUF; // Retrn contents of RX buffer.
- }
- /* Send char value to SPI */
- char spi_transfer(char inb) {
- P1OUT &= ~CSN; // Pull CSN down.
- while (UCA0STAT & UCBUSY); // Delay while SPI busy.
- UCA0TXBUF = inb; // Write value to transmit into TX buffer.
- while ( !(IFG2 & UCA0RXIFG) ); // Wait for RXIFG indicating remote byte received via SOMI
- P1OUT |=CSN; // Pull CSN back up.
- return UCA0RXBUF;
- }
- /* Send int value to SPI */
- int spi_transfer16(int inw) {
- int retw;
- UCA0TXBUF = (inw >> 8) & 0xFF;
- while ( !(IFG2 & UCA0RXIFG) );
- retw = UCA0RXBUF << 8;
- UCA0TXBUF = inw & 0xFF;
- while ( !(IFG2 & UCA0RXIFG) );
- retw |= UCA0RXBUF;
- return retw;
- }
- /* Change radio mode */
- void frob(char mode) {
- switch(mode) {
- case PWR_DWN: // Power down the radio. Still listens for SPI.
- P1OUT &= ~PWR; // Turn of PWR bit to port 1.
- break;
- case STND_BY: // Put the radio in standby. Still listens for SPI.
- P1OUT |=PWR; // Turn on PWR bit to port 1.
- P2OUT &= ~CE; // Turn off CE bit to port 2.
- break;
- case READ_RX: // Read the RX register out.
- P1OUT |=PWR;
- P1OUT &= ~TX_EN;
- break;
- case SET_RX: // Put the radio in RX mode.
- P1OUT |=PWR; // Turn on PWR bit to port 1.
- P1OUT &= ~TX_EN; // Turn off TX_EN bit to port 1.
- P2OUT |=CE; // Turn on CE bit to port 2.
- break;
- case SET_TX: // Put the radio in TX mode.
- P1OUT |=PWR+TX_EN; // Turn on PWR and TX_EN bits to port 1.
- P2OUT |=CE; // Turn on CE bit to port 2.
- break;
- }
- }
- void main(void) {
- WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
- DCOCTL = CALDCO_16MHZ; // Set DCO to calibrated 16MHZ.
- BCSCTL1 = CALBC1_16MHZ; // ^
- BCSCTL2 = DIVS_2; // Set SMCLK divider to DCO/4
- /* configure port1 */
- P1OUT = 0x00; // P1 setup for LED & reset output
- P1DIR = 0xFB; // set SPI1, PWR and TX_EN to output
- //! P1OUT |= BIT6;
- // configure port2
- P2DIR = 0x04; // Set uCK, CD, DR and AM to input
- P2OUT = 0x00;
- spi_init();
- P1OUT &= ~BIT5; // Now with SPI signals initialized,
- P1OUT |= BIT5; // reset slave
- __delay_cycles(75); // Wait for slave to initialize
- frob(STND_BY); // enter standby.
- for (i=0; i<16; i++) {
- myPayload = READ_CFG|addrPos;
- rxDatum = spi_read_cfg();
- addrPos++;
- //__bis_SR_register(LPM0_bits + GIE); // sc& off with hailing frequencies open.
- }
- }
- // Test for valid RX and TX character
- #pragma vector=USCIAB0RX_VECTOR
- __interrupt void USCIA0RX_ISR()
- {
- //volatile unsigned int i;
- while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
- //UCA0TXBUF = TX_Data; // Send next value
- P1OUT |= BIT0;
- rxData[i] = UCA0RXBUF;
- __delay_cycles(50); // Add time between transmissions to
- } // make sure slave can keep up
Advertisement
Add Comment
Please, Sign In to add comment