Advertisement
Guest User

main-file

a guest
Jul 29th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1.  
  2. #include <asf.h>
  3.  
  4.  
  5. #define SPI_EXAMPLE             (&AVR32_SPI1)
  6. #define SPI_SLAVECHIP_NUMBER    (0)
  7. uint32_t status;
  8. spi_options_t my_spi_options={
  9.     // The SPI channel to set up
  10.     .reg = SPI_SLAVECHIP_NUMBER,
  11.     // Preferred baudrate for the SPI.
  12.     .baudrate = 10000,
  13.     // Number of bits in each character (8 to 16).
  14.     .bits = 8,
  15.     // Delay before first clock pulse after selecting slave (in PBA clock periods). (min 5ns für STEVAL-MKI105V1)
  16.     .spck_delay = 5, // ca. 20,0 ns
  17.     // Delay between each transfer/character (in PBA clock periods).
  18.     .trans_delay = 0,
  19.     // Sets this chip to stay active after last transfer to it.
  20.     .stay_act = 0,
  21.     // Which SPI mode to use when transmitting.
  22.     .spi_mode = SPI_MODE_0,
  23.     // Disables the mode fault detection.
  24.     // With this bit cleared, the SPI master mode will disable itself if another
  25.     // master tries to address it.
  26.     .modfdis = 1
  27. };
  28.  
  29.  
  30. static void spi_init_module(void)
  31. {
  32.     sysclk_enable_pba_module(SYSCLK_SPI1);
  33.     sysclk_enable_peripheral_clock(SPI_EXAMPLE);
  34.     //Init SPI module as master
  35.     spi_initMaster(SPI_EXAMPLE,&my_spi_options);
  36.     //Setup configuration for chip connected to CS1
  37.     spi_setupChipReg(SPI_EXAMPLE,&my_spi_options,sysclk_get_pba_hz());
  38.     //Allow the module to transfer data
  39.     spi_enable(SPI_EXAMPLE);
  40.    
  41. }
  42.  
  43. /**
  44.  * \brief main function
  45.  */
  46. int main (void)
  47. {
  48.  
  49.     sysclk_init();
  50.     board_init();
  51.  
  52.     // Initialize interrupt vector table support.
  53.     irq_initialize_vectors();
  54.  
  55.     // Enable interrupts
  56.     cpu_irq_enable();
  57.  
  58.     /* Call a local utility routine to initialize C-Library Standard I/O over
  59.      * a USB CDC protocol. Tunable parameters in a conf_usb.h file must be
  60.      * supplied to configure the USB device correctly.
  61.      */
  62.     stdio_usb_init();
  63.     delay_init(sysclk_get_cpu_hz());
  64.     spi_init_module();
  65.  
  66.     // Get and echo characters forever.
  67.     gpio_clr_gpio_pin(LED0_GPIO);
  68.    
  69.     int ch_int;
  70.     int byte1;
  71.     int byte2;
  72.    
  73.     //Buffer to send data to SPI slave
  74.     uint16_t txdata;
  75.     //Buffer to receive data from SPI slave
  76.     uint16_t rxdata;
  77.  
  78.     while (true) {
  79.         ch_int = udi_cdc_getc();
  80.         if(ch_int == 71)
  81.         {
  82.             udi_cdc_putc(71);
  83.             byte1 = udi_cdc_getc_wait();
  84.             byte2 = udi_cdc_getc_wait();
  85.             udi_cdc_putc(byte1);
  86.             udi_cdc_putc(byte2);
  87.            
  88.             //Select given device on the SPI bus
  89.             spi_selectChip(SPI_EXAMPLE, SPI_SLAVECHIP_NUMBER);
  90.  
  91.             //Wait for the transmitter to be ready
  92.             while(!spi_is_tx_ready(SPI_EXAMPLE));
  93.             // Send the data to slave
  94.             txdata= byte1;
  95.             spi_put(SPI_EXAMPLE,txdata);
  96.             //Wait for a complete transmission
  97.             while(!spi_is_tx_empty(SPI_EXAMPLE));
  98.             //Wait for the transmitter to be ready
  99.             while(!spi_is_tx_ready(SPI_EXAMPLE));
  100.             // Send dummy data to slave (ie = 0x00)
  101.             txdata=byte2;
  102.             spi_put(SPI_EXAMPLE,txdata);
  103.             //Wait for a complete transmission
  104.             while(!spi_is_tx_empty(SPI_EXAMPLE));
  105.             udi_cdc_putc(spi_get(SPI_EXAMPLE));
  106.             udi_cdc_putc(spi_get(SPI_EXAMPLE));
  107.             // Deselect the slave
  108.             spi_unselectChip(SPI_EXAMPLE,SPI_SLAVECHIP_NUMBER);
  109.             udi_cdc_putc(72);
  110.  
  111.         }
  112.  
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement