Advertisement
Guest User

main.c

a guest
Aug 5th, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.39 KB | None | 0 0
  1. /**
  2.  * \file
  3.  *
  4.  * \brief USB Standard I/O (stdio) Example
  5.  *
  6.  * Copyright (c) 2011 - 2014 Atmel Corporation. All rights reserved.
  7.  *
  8.  * \asf_license_start
  9.  *
  10.  * \page License
  11.  *
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions are met:
  14.  *
  15.  * 1. Redistributions of source code must retain the above copyright notice,
  16.  *    this list of conditions and the following disclaimer.
  17.  *
  18.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  19.  *    this list of conditions and the following disclaimer in the documentation
  20.  *    and/or other materials provided with the distribution.
  21.  *
  22.  * 3. The name of Atmel may not be used to endorse or promote products derived
  23.  *    from this software without specific prior written permission.
  24.  *
  25.  * 4. This software may only be redistributed and used in connection with an
  26.  *    Atmel microcontroller product.
  27.  *
  28.  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31.  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38.  * POSSIBILITY OF SUCH DAMAGE.
  39.  *
  40.  * \asf_license_stop
  41.  *
  42.  */
  43.  
  44. /**
  45.  * \mainpage
  46.  *
  47.  * \section intro Introduction
  48.  * This example demonstrates how to configure a C-library Standard
  49.  * I/O interface to the ASF common USB Device CDC service. The initialization
  50.  * routines, along with board and clock configuration constants, illustrate how
  51.  * to perform serial I/O via a Communication Device Class (CDC) device protocol
  52.  *
  53.  * \section files Main Files
  54.  * - stdio_usb_example.c: the example application.
  55.  * - conf_board.h: board configuration
  56.  * - conf_clock.h: board configuration
  57.  * - stdio_usb.h: Common USB CDC Standard I/O Implementation
  58.  * - read.c : System implementation function used by standard library
  59.  * - write.c : System implementation function used by standard library
  60.  *
  61.  * \section example_description Description of the example
  62.  *   - Send message on USB CDC device to a Virtual Com Port.
  63.  *   - Performs echo of any received character
  64.  *
  65.  * \section contactinfo Contact Information
  66.  * For further information, visit
  67.  * <A href="http://www.atmel.com/avr">Atmel AVR</A>.\n
  68.  */
  69.  /**
  70.  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  71.  */
  72.  
  73. #include <asf.h>
  74.  
  75.  
  76. #define SPI_EXAMPLE             (&AVR32_SPI1)
  77. #define SPI_SLAVECHIP_NUMBER    (0)
  78. uint32_t status;
  79. spi_options_t my_spi_options={
  80.     // The SPI channel to set up
  81.     .reg = SPI_SLAVECHIP_NUMBER,
  82.     // Preferred baudrate for the SPI.
  83.     .baudrate = 14400,
  84.     // Number of bits in each character (8 to 16).
  85.     .bits = 8,
  86.     // Delay before first clock pulse after selecting slave (in PBA clock periods). (min 5ns für STEVAL-MKI105V1)
  87.     .spck_delay = 5, // ca. 20,0 ns
  88.     // Delay between each transfer/character (in PBA clock periods).
  89.     .trans_delay = 0,
  90.     // Sets this chip to stay active after last transfer to it.
  91.     .stay_act = 0,
  92.     // Which SPI mode to use when transmitting.
  93.     .spi_mode = SPI_MODE_0,
  94.     // Disables the mode fault detection.
  95.     // With this bit cleared, the SPI master mode will disable itself if another
  96.     // master tries to address it.
  97.     .modfdis = 1
  98. };
  99.  
  100. struct spi_device SPI_DEVICE_EXAMPLE = {
  101.     //! Board specific select id
  102.     .id = SPI_SLAVECHIP_NUMBER
  103. };
  104.  
  105. /**
  106.  * \brief main function
  107.  */
  108. int main (void)
  109. {  
  110.     sysclk_init(); // Initiate the system clock
  111.     board_init(); // Initiate the board (==> init.c)
  112.     spi_master_init(SPI_EXAMPLE); // Initiate the SPI Module 1 as Master
  113.     spi_master_setup_device(SPI_EXAMPLE, &SPI_DEVICE_EXAMPLE, my_spi_options.spi_mode,
  114.     my_spi_options.baudrate, 0); // Parametrization for the SPI Module
  115.     spi_setupChipReg(SPI_EXAMPLE,&my_spi_options,sysclk_get_pba_hz());
  116.     spi_enable(SPI_EXAMPLE);           
  117.     // Initialize interrupt vector table support.
  118.     irq_initialize_vectors();
  119.     // Enable interrupts
  120.     cpu_irq_enable();
  121.     stdio_usb_init(); // I am using USB, if you don't then delete this line
  122.     delay_init(sysclk_get_cpu_hz()); // I am using delays too, so unnecessary for the SPI
  123.    
  124.    
  125.     // Example for a write operation - in SPI there is no write-only operation - but in this code part is no recording for incoming data
  126.     spi_selectChip(SPI_EXAMPLE, SPI_SLAVECHIP_NUMBER); // First you have to select the SPI Module
  127.    
  128.     //Wait for the transmitter to be ready
  129.     while(!spi_is_tx_ready(SPI_EXAMPLE));
  130.     spi_put(SPI_EXAMPLE,32); // write on CTRL_REG1 of an LIS3DH MEMS-Sensor from ST Microeletronics
  131.     //Wait for a complete transmission
  132.     while(!spi_is_tx_empty(SPI_EXAMPLE));
  133.     //Wait for the transmitter to be ready
  134.     while(!spi_is_tx_ready(SPI_EXAMPLE));
  135.     spi_put(SPI_EXAMPLE,39);                // 10 Hz for all axis and enabling all of them
  136.     // Deselect the slave
  137.     spi_unselectChip(SPI_EXAMPLE,SPI_SLAVECHIP_NUMBER);
  138.    
  139.     // First transmission finished
  140.    
  141.    
  142.     // Second transmission, similar to the first one
  143.    
  144.     spi_selectChip(SPI_EXAMPLE, SPI_SLAVECHIP_NUMBER);
  145.        
  146.     //Wait for the transmitter to be ready
  147.     while(!spi_is_tx_ready(SPI_EXAMPLE));
  148.     spi_put(SPI_EXAMPLE,35); // schreibe auf CTRL_REG4
  149.     //Wait for a complete transmission
  150.     while(!spi_is_tx_empty(SPI_EXAMPLE));
  151.     //Wait for the transmitter to be ready
  152.     while(!spi_is_tx_ready(SPI_EXAMPLE));
  153.     spi_put(SPI_EXAMPLE,8);             // Default bis auf High Resolution
  154.     // Deselect the slave
  155.     spi_unselectChip(SPI_EXAMPLE,SPI_SLAVECHIP_NUMBER);
  156.        
  157.    
  158.     gpio_clr_gpio_pin(LED3_GPIO); // Turn on the LED3 to signal, that the Sensor is ready
  159.  
  160.     // Read instructions (232 is the instruction / 136... are dummies)
  161.     int bytes[] = {232, 136, 136, 136, 136, 136, 136}; // 236 = Z / 234 = Y / 232 = X
  162.     Byte answers[7] = {0};
  163.     int index = 0;
  164.    
  165.     while (true) {
  166.  
  167.         //Select given device on the SPI bus
  168.         spi_selectChip(SPI_EXAMPLE, SPI_SLAVECHIP_NUMBER);
  169.            
  170.         //Wait for the transmitter to be ready
  171.         while(!spi_is_tx_ready(SPI_EXAMPLE));
  172.         // Send the data to slave
  173.         index = 0;
  174.         // Sending all the bytes to the sensor and recording the data from the sensor
  175.         while(index < 7){
  176.             spi_put(SPI_EXAMPLE,bytes[index]);
  177.             //Wait for a complete transmission
  178.             while(!spi_is_tx_empty(SPI_EXAMPLE));
  179.             //Wait for the transmitter to be ready
  180.             while(!spi_is_tx_ready(SPI_EXAMPLE));
  181.             // the following line records the sensor data
  182.             answers[index] = spi_get(SPI_EXAMPLE);
  183.             index++;
  184.         }
  185.         while(!spi_is_tx_empty(SPI_EXAMPLE));
  186.         // Deselect the slave
  187.         spi_unselectChip(SPI_EXAMPLE,SPI_SLAVECHIP_NUMBER);
  188.    
  189.         // Just a little lightshow to signal if the acceleration for x is positive or negative
  190.         if(answers[2] & 0x80){
  191.             gpio_set_gpio_pin(LED3_GPIO);
  192.         }
  193.         else{
  194.             gpio_clr_gpio_pin(LED3_GPIO);
  195.         }
  196.        
  197.         // Sending ASCII "G" and the sensor data via usb to a terminal programm (HTerm)
  198.         udi_cdc_putc(71);
  199.         index = 1;
  200.         while(index < 7){
  201.             udi_cdc_putc(answers[index]);
  202.             index++;
  203.         }
  204.         delay_ms(100); // Short Delay
  205.  
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement