int main(void) { int counter = 0; uint16_t rx_value = 0x42; /* Setup Rx/Tx buffers for USART */ buffer_init(send_buffer,BUFFER_SIZE); buffer_init(receive_buffer,BUFFER_SIZE); clock_setup(); gpio_setup(); usart_setup(); usart_print_string("SPI-DMA Test\n\r"); spi_setup(); while (1) { gpio_toggle(GPIOA, GPIO1); #ifdef LOOPBACK /* Print what is going to be sent on the SPI bus */ usart_print_string("Sending packet "); usart_print_int(counter); usart_print_string("\n\r"); spi_send(SPI1, (uint8_t) counter); rx_value = spi_read(SPI1); usart_print_string("Received packet "); usart_print_int(rx_value); usart_print_string("\n\r"); counter++; #else /* This is a 1-byte "reset" command to SD card */ spi_send(SPI1, 0x40); spi_send(SPI1, 0x00); spi_send(SPI1, 0x00); spi_send(SPI1, 0x00); spi_send(SPI1, 0x00); spi_send(SPI1, 0x95); /* Read the byte that just came in (use a loopback between MISO and MOSI * to get the same byte back) */ rx_value = spi_read(SPI1); #endif } return 0; }
int main(void) { int counter_tx = 0; int counter_rx = 0; cnt_state counter_state = TX_UP_RX_HOLD; int i = 0; /* Transmit and Receive packets, set transmit to index and receive to known unused value to aid in debugging */ #if USE_16BIT_TRANSFERS uint16_t tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; uint16_t rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42}; #else uint8_t tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; uint8_t rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42}; #endif transceive_status = DONE; clock_setup(); gpio_setup(); usart_setup(); usart_print_string("SPI-DMA Test\n\r"); spi_setup(); dma_setup(); /* Blink the LED (PA8) on the board with every transmitted byte. */ while (1) { /* LED on/off */ gpio_toggle(GPIOA, GPIO1); /* Print what is going to be sent on the SPI bus */ usart_print_string("Sending packet (tx len: "); usart_print_int(counter_tx); usart_print_string(")\n\r"); for (i = 0; i < counter_tx; i++) { usart_print_int(tx_packet[i]); usart_print_string(" "); } usart_print_string("\r\n"); /* Start a transceive */ if (spi_dma_transceive(tx_packet, counter_tx, rx_packet, counter_rx)) { usart_print_string("Attempted 0 length tx and rx packets\r\n"); } /* Wait until transceive complete. * This checks the state flag as well as follows the * procedure on the Reference Manual (RM0008 rev 14 * Section 25.3.9 page 692, the note.) */ while (transceive_status != DONE) ; while (!(SPI_SR(SPI2) & SPI_SR_TXE)) ; while (SPI_SR(SPI2) & SPI_SR_BSY) ; /* Print what was received on the SPI bus */ usart_print_string("Received Packet (rx len "); usart_print_int(counter_rx); usart_print_string(")\n\r"); for (i = 0; i < 16; i++) { usart_print_int(rx_packet[i]); usart_print_string(" "); } usart_print_string("\r\n\r\n"); /* Update counters * If we use the loopback method, we can not * have a rx length longer than the tx length. * Testing rx lengths longer than tx lengths * requires an actual slave device that will * return data. */ switch (counter_state) { case TX_UP_RX_HOLD: counter_tx++; if (counter_tx > 15) { counter_state = TX_HOLD_RX_UP; } break; case TX_HOLD_RX_UP: counter_rx++; if (counter_rx > 15) { counter_state = TX_DOWN_RX_DOWN; } break; case TX_DOWN_RX_DOWN: counter_tx--; counter_rx--; if (counter_tx < 1) { counter_state = TX_UP_RX_HOLD; } break; default: ; } /* Reset receive buffer for consistency */ for (i = 0; i < 16; i++) { rx_packet[i] = 0x42; } } return 0; }
// write a program-memory string to usart void usart_dumpvar(PGM_P name, int32_t value) { usart_print_pstr(name); usart_print_pstr(PSTR(": ")); usart_print_int(value); usart_print_ln(); }