Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <asf.h>
- #define SPI_EXAMPLE (&AVR32_SPI1)
- #define SPI_SLAVECHIP_NUMBER (0)
- uint32_t status;
- spi_options_t my_spi_options={
- // The SPI channel to set up
- .reg = SPI_SLAVECHIP_NUMBER,
- // Preferred baudrate for the SPI.
- .baudrate = 10000,
- // Number of bits in each character (8 to 16).
- .bits = 8,
- // Delay before first clock pulse after selecting slave (in PBA clock periods). (min 5ns für STEVAL-MKI105V1)
- .spck_delay = 5, // ca. 20,0 ns
- // Delay between each transfer/character (in PBA clock periods).
- .trans_delay = 0,
- // Sets this chip to stay active after last transfer to it.
- .stay_act = 0,
- // Which SPI mode to use when transmitting.
- .spi_mode = SPI_MODE_0,
- // Disables the mode fault detection.
- // With this bit cleared, the SPI master mode will disable itself if another
- // master tries to address it.
- .modfdis = 1
- };
- static void spi_init_module(void)
- {
- sysclk_enable_pba_module(SYSCLK_SPI1);
- sysclk_enable_peripheral_clock(SPI_EXAMPLE);
- //Init SPI module as master
- spi_initMaster(SPI_EXAMPLE,&my_spi_options);
- //Setup configuration for chip connected to CS1
- spi_setupChipReg(SPI_EXAMPLE,&my_spi_options,sysclk_get_pba_hz());
- //Allow the module to transfer data
- spi_enable(SPI_EXAMPLE);
- }
- /**
- * \brief main function
- */
- int main (void)
- {
- sysclk_init();
- board_init();
- // Initialize interrupt vector table support.
- irq_initialize_vectors();
- // Enable interrupts
- cpu_irq_enable();
- /* Call a local utility routine to initialize C-Library Standard I/O over
- * a USB CDC protocol. Tunable parameters in a conf_usb.h file must be
- * supplied to configure the USB device correctly.
- */
- stdio_usb_init();
- delay_init(sysclk_get_cpu_hz());
- spi_init_module();
- // Get and echo characters forever.
- gpio_clr_gpio_pin(LED0_GPIO);
- int ch_int;
- int byte1;
- int byte2;
- //Buffer to send data to SPI slave
- uint16_t txdata;
- //Buffer to receive data from SPI slave
- uint16_t rxdata;
- while (true) {
- ch_int = udi_cdc_getc();
- if(ch_int == 71)
- {
- udi_cdc_putc(71);
- byte1 = udi_cdc_getc_wait();
- byte2 = udi_cdc_getc_wait();
- udi_cdc_putc(byte1);
- udi_cdc_putc(byte2);
- //Select given device on the SPI bus
- spi_selectChip(SPI_EXAMPLE, SPI_SLAVECHIP_NUMBER);
- //Wait for the transmitter to be ready
- while(!spi_is_tx_ready(SPI_EXAMPLE));
- // Send the data to slave
- txdata= byte1;
- spi_put(SPI_EXAMPLE,txdata);
- //Wait for a complete transmission
- while(!spi_is_tx_empty(SPI_EXAMPLE));
- //Wait for the transmitter to be ready
- while(!spi_is_tx_ready(SPI_EXAMPLE));
- // Send dummy data to slave (ie = 0x00)
- txdata=byte2;
- spi_put(SPI_EXAMPLE,txdata);
- //Wait for a complete transmission
- while(!spi_is_tx_empty(SPI_EXAMPLE));
- udi_cdc_putc(spi_get(SPI_EXAMPLE));
- udi_cdc_putc(spi_get(SPI_EXAMPLE));
- // Deselect the slave
- spi_unselectChip(SPI_EXAMPLE,SPI_SLAVECHIP_NUMBER);
- udi_cdc_putc(72);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement