//Send a frame of bytes via SPI unsigned char spiSendFrame(unsigned char* pBuffer, unsigned int size) { #ifndef withDMA unsigned long i = 0; // clock the actual data transfer and receive the bytes; spi_read automatically finds the Data Block for (i = 0; i < size; i++){ while (halSPITXREADY ==0); // wait while not ready for TX halSPI_SEND(pBuffer[i]); // write while (halSPIRXREADY ==0); // wait for RX buffer (full) pBuffer[i] = halSPIRXBUF; } #else /* Get the block */ /* DMA trigger is UART send */ DMACTL0 &= ~(DMA0TSEL_15); DMACTL0 |= (DMA0TSEL_9); /* Source DMA address: the data buffer. */ DMA0SA = (unsigned short)pBuffer; /* Destination DMA address: the UART send register. */ DMA0DA = U1TXBUF_; /* The size of the block to be transferred */ DMA0SZ = count; /* Configure the DMA transfer*/ DMA0CTL = DMAREQ | /* start transfer */ DMADT_0 | /* Single transfer mode */ DMASBDB | /* Byte mode */ DMAEN | /* Enable DMA */ DMASRCINCR1 | DMASRCINCR0; /* Increment the source address */ #endif return(0); }
//Send one byte via SPI unsigned char spiSendByte(const unsigned char data) { while (halSPITXREADY ==0); // wait while not ready for TX halSPI_SEND(data); // write while (halSPIRXREADY ==0); // wait for RX buffer (full) return (halSPIRXBUF); }
void halSPISetup(void) { U0CSR |= 0X40; //SPI_SETUP(0,2400,SPI_MASTER); U0UCR = 0x80; U0CSR = 0x00; P0SEL |= (SPI_SIMO + SPI_SOMI + SPI_UCLK); PERCFG |= 0x00; //IO_DIR_PORT_PIN(0,3,IO_OUT); // txd / MOSI //IO_DIR_PORT_PIN(0,2,IO_IN); // Rxd / MISO //IO_DIR_PORT_PIN(0,4,IO_OUT); // CTS / SS_N //IO_DIR_PORT_PIN(0,5,IO_OUT); // txd / SCK U0GCR = BAUD_E(115200, CLKSPD); U0BAUD = BAUD_M(115200); //U0GCR |= (SPI_MASTER & 0xE0)+ 0x20; //Configure SPI Polarity, Clock Phase, and Bit Order U0CSR = U0CSR & ~0x80; // Set U0CSR bit 7 to 0 for SPI Mode U0CSR = U0CSR & ~0x20; // Set U0CSR bit 5 to 0 for Master U0GCR = (U0GCR | 0x80); // Set U0GCR bit 7 to 1 for CPOL = 1 U0GCR = (U0GCR | 0x20); // Set U0GCR bit 5 to 1 for MSB first U0GCR = (U0GCR & ~0x40); // Set U0GCR bit 6 to 0 for CPHA = 0 U0CSR |= 0x40; URX0IF = 0; halSPI_SEND(DUMMY_CHAR); }
//Send one byte via SPI unsigned char spiSendByte(const unsigned char data) { while (halSPITXREADY); // wait while not ready for TX halSPI_SEND(data); // write while(!(U0CSR & U0CSR_TX_BYTE)); // wait U0CSR &= ~U0CSR_TX_BYTE; // clear flag while (halSPIRXREADY ==0); // wait for RX buffer (full) return (halSPIRXBUF); }
//Read a frame of bytes via SPI unsigned char spiReadFrame(unsigned char* pBuffer, unsigned int size) { #ifndef withDMA unsigned long i = 0; // clock the actual data transfer and receive the bytes; spi_read automatically finds the Data Block for (i = 0; i < size; i++){ while (halSPITXREADY ==0); // wait while not ready for TX halSPI_SEND(DUMMY_CHAR); // dummy write while (halSPIRXREADY ==0); // wait for RX buffer (full) pBuffer[i] = halSPIRXBUF; } #else U1IFG &= ~(URXIFG1 + URXIFG1); /* clear flags */ /* Get the block */ /* DMA trigger is UART1 receive for both DMA0 and DMA1 */ DMACTL0 &= ~(DMA0TSEL_15 | DMA1TSEL_15); DMACTL0 |= (DMA0TSEL_9 | DMA1TSEL_9); /* Source DMA address: receive register. */ DMA0SA = U1RXBUF_; /* Destination DMA address: the user data buffer. */ DMA0DA = (unsigned short)pBuffer; /* The size of the block to be transferred */ DMA0SZ = size; /* Configure the DMA transfer*/ DMA0CTL = DMAIE | /* Enable interrupt */ DMADT_0 | /* Single transfer mode */ DMASBDB | /* Byte mode */ DMAEN | /* Enable DMA */ DMADSTINCR1 | DMADSTINCR0; /* Increment the destination address */ /* We depend on the DMA priorities here. Both triggers occur at the same time, since the source is identical. DMA0 is handled first, and retrieves the byte. DMA1 is triggered next, and sends the next byte. */ /* Source DMA address: constant 0xFF (don't increment)*/ DMA1SA = U1TXBUF_; /* Destination DMA address: the transmit buffer. */ DMA1DA = U1TXBUF_; /* Increment the destination address */ /* The size of the block to be transferred */ DMA1SZ = count-1; /* Configure the DMA transfer*/ DMA1CTL = DMADT_0 | /* Single transfer mode */ DMASBDB | /* Byte mode */ DMAEN; /* Enable DMA */ /* Kick off the transfer by sending the first byte */ halMMC_SEND(0xFF); _EINT(); LPM0; // wait till done #endif return(0); }
//Send a frame of bytes via SPI unsigned char spiSendFrame(unsigned char* pBuffer, unsigned int size) { unsigned long i = 0; // clock the actual data transfer and receive the bytes; spi_read automatically finds the Data Block for (i = 0; i < size; i++){ while (halSPITXREADY); // wait while not ready for TX halSPI_SEND(pBuffer[i]); // write while(!(U0CSR & U0CSR_TX_BYTE)); // wait U0CSR &= ~U0CSR_TX_BYTE; // clear fl pBuffer[i] = halSPIRXBUF; } return(0); }