canid

MSP430-NRF905-WIP

Jan 21st, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.73 KB | None | 0 0
  1. #include "msp430g2553.h"
  2. #include "stdint.h"
  3.  
  4. volatile char txData[32], rxData[32] = 1;
  5. volatile char rxDatum;              // debug purposes.
  6. volatile unsigned int i =0;         // global counter
  7. char myPayload = 0;
  8. char addrPos = 0;                   // start address suffix.
  9.  
  10. #define PWR_DWN     0
  11. #define STND_BY     1
  12. #define READ_RX     2
  13. #define SET_RX      3
  14. #define SET_TX      4
  15.  
  16. #define WRIT_CFG    0x00            // read configuration command prefix.
  17. #define READ_CFG    0x10            // write configuration command prefix.
  18. #define WRIT_TX     0x20            // write tx payload command.
  19. #define READ_TX     0x21            // read tx payload command.
  20.  
  21. #define LED1        BIT0
  22. #define MISO        BIT1
  23. #define uCK         BIT1
  24. #define MOSI        BIT2
  25. #define CD          BIT2
  26. #define CSN         BIT3
  27. #define DR          BIT3
  28. #define SCK         BIT4
  29. #define AM          BIT4
  30. #define CE          BIT5
  31. #define PWR         BIT6
  32. #define LED2        BIT6
  33. #define TX_EN       BIT7
  34.  
  35. /* Initialize USCI_A for SPI */
  36. void spi_init(void) {
  37.     P1SEL |= BIT1 | BIT2 | BIT4;    // Configure port.
  38.     P1SEL2 |= BIT1 | BIT2 | BIT4;   // ^
  39.  
  40.     /* USCI-A specific SPI setup */
  41.     UCA0CTL1 |= UCSWRST;
  42.     UCA0MCTL = 0x00;                // Clearing modulation control per TI user's guide recommendation
  43.     UCA0CTL0 = UCCKPH | UCMSB | UCMST | UCMODE_0 | UCSYNC;  // SPI mode 0, master
  44.     UCA0BR0 = 0x01;                 // SPI clocked at same speed as SMCLK
  45.     UCA0BR1 = 0x00;                 // ^
  46.     UCA0CTL1 = UCSSEL_2;            // Clock = SMCLK, clear UCSWRST and enables USCI_A module.
  47. }
  48.  
  49. /* Kill SPI module */
  50. void spi_kill(void) {
  51.     UCA0CTL1 | UCSWRST;             // put UCSI in reset mode.
  52. }
  53.  
  54. /* Read radio configuration from SPI */
  55. char spi_read_cfg() {
  56.     P1OUT &= ~CSN;                  // Pull CSN down.
  57.     while (UCA0STAT & UCBUSY);      // Delay while SPI busy.
  58.     UCA0TXBUF = 0x10;               // Fill tx buffer to send data.
  59.     while (!(IFG2&UCA0RXIFG));      // Delay til RX flag reset.
  60.     P1OUT |=CSN;                    // Pull CSN back up.
  61.     return UCA0RXBUF;               // Retrn contents of RX buffer.
  62. }
  63.  
  64. /* Send char value to SPI */
  65. char spi_transfer(char inb) {
  66.     P1OUT &= ~CSN;                  // Pull CSN down.
  67.     while (UCA0STAT & UCBUSY);      // Delay while SPI busy.
  68.     UCA0TXBUF = inb;                // Write value to transmit into TX buffer.
  69.     while ( !(IFG2 & UCA0RXIFG) );  // Wait for RXIFG indicating remote byte received via SOMI
  70.     P1OUT |=CSN;                    // Pull CSN back up.
  71.     return UCA0RXBUF;
  72. }
  73.  
  74. /* Send int value to SPI */
  75. int spi_transfer16(int inw) {
  76.     int retw;
  77.     UCA0TXBUF = (inw >> 8) & 0xFF;
  78.     while ( !(IFG2 & UCA0RXIFG) );
  79.     retw = UCA0RXBUF << 8;
  80.     UCA0TXBUF = inw & 0xFF;
  81.     while ( !(IFG2 & UCA0RXIFG) );
  82.     retw |= UCA0RXBUF;
  83.     return retw;
  84. }
  85.  
  86. /* Change radio mode */
  87. void frob(char mode) {
  88.     switch(mode) {
  89.         case PWR_DWN: // Power down the radio. Still listens for SPI.
  90.             P1OUT &= ~PWR; // Turn of PWR bit to port 1.
  91.             break;
  92.         case STND_BY: // Put the radio in standby. Still listens for SPI.
  93.             P1OUT |=PWR; // Turn on PWR bit to port 1.
  94.             P2OUT &= ~CE; // Turn off CE bit to port 2.
  95.             break;
  96.         case READ_RX: // Read the RX register out.
  97.             P1OUT |=PWR;
  98.             P1OUT &= ~TX_EN;
  99.             break;
  100.         case SET_RX: // Put the radio in RX mode.
  101.             P1OUT |=PWR; // Turn on PWR bit to port 1.
  102.             P1OUT &= ~TX_EN; // Turn off TX_EN bit to port 1.
  103.             P2OUT |=CE; // Turn on CE bit to port 2.
  104.  
  105.             break;
  106.         case SET_TX: // Put the radio in TX mode.
  107.             P1OUT |=PWR+TX_EN; // Turn on PWR and TX_EN bits to port 1.
  108.             P2OUT |=CE;  // Turn on CE bit to port 2.
  109.             break;
  110.     }
  111. }
  112.  
  113. void main(void) {
  114.  
  115.   WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
  116.   DCOCTL = CALDCO_16MHZ;                    // Set DCO to calibrated 16MHZ.
  117.   BCSCTL1 = CALBC1_16MHZ;                   // ^
  118.   BCSCTL2 = DIVS_2;                         // Set SMCLK divider to DCO/4
  119.  
  120.   /* configure port1 */
  121.   P1OUT = 0x00;                             // P1 setup for LED & reset output
  122.   P1DIR = 0xFB;                             // set SPI1, PWR and TX_EN to output
  123. //!  P1OUT |= BIT6;
  124.  
  125.   // configure port2
  126.   P2DIR = 0x04;                             // Set uCK, CD, DR and AM to input
  127.   P2OUT = 0x00;
  128.  
  129.   spi_init();
  130.  
  131.   P1OUT &= ~BIT5;                           // Now with SPI signals initialized,
  132.   P1OUT |= BIT5;                            // reset slave
  133.  
  134.   __delay_cycles(75);                 // Wait for slave to initialize
  135.  
  136.   frob(STND_BY);    // enter standby.
  137.  
  138.   for (i=0; i<16; i++) {
  139.     myPayload = READ_CFG|addrPos;
  140.     rxDatum = spi_read_cfg();
  141.     addrPos++;
  142.     //__bis_SR_register(LPM0_bits + GIE);       // sc& off with hailing frequencies open.
  143.   }
  144. }
  145.  
  146. // Test for valid RX and TX character
  147. #pragma vector=USCIAB0RX_VECTOR
  148. __interrupt void USCIA0RX_ISR()
  149. {
  150.   //volatile unsigned int i;
  151.   while (!(IFG2 & UCA0TXIFG));              // USCI_A0 TX buffer ready?
  152.   //UCA0TXBUF = TX_Data;                     // Send next value
  153.   P1OUT |= BIT0;
  154.   rxData[i] = UCA0RXBUF;
  155.   __delay_cycles(50);                     // Add time between transmissions to
  156. }                                           // make sure slave can keep up
Advertisement
Add Comment
Please, Sign In to add comment