Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * \file
- *
- * \brief USB Standard I/O (stdio) Example
- *
- * Copyright (c) 2011 - 2014 Atmel Corporation. All rights reserved.
- *
- * \asf_license_start
- *
- * \page License
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * 3. The name of Atmel may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * 4. This software may only be redistributed and used in connection with an
- * Atmel microcontroller product.
- *
- * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
- * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * \asf_license_stop
- *
- */
- /**
- * \mainpage
- *
- * \section intro Introduction
- * This example demonstrates how to configure a C-library Standard
- * I/O interface to the ASF common USB Device CDC service. The initialization
- * routines, along with board and clock configuration constants, illustrate how
- * to perform serial I/O via a Communication Device Class (CDC) device protocol
- *
- * \section files Main Files
- * - stdio_usb_example.c: the example application.
- * - conf_board.h: board configuration
- * - conf_clock.h: board configuration
- * - stdio_usb.h: Common USB CDC Standard I/O Implementation
- * - read.c : System implementation function used by standard library
- * - write.c : System implementation function used by standard library
- *
- * \section example_description Description of the example
- * - Send message on USB CDC device to a Virtual Com Port.
- * - Performs echo of any received character
- *
- * \section contactinfo Contact Information
- * For further information, visit
- * <A href="http://www.atmel.com/avr">Atmel AVR</A>.\n
- */
- /**
- * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
- */
- #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 = 14400,
- // 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
- };
- struct spi_device SPI_DEVICE_EXAMPLE = {
- //! Board specific select id
- .id = SPI_SLAVECHIP_NUMBER
- };
- /**
- * \brief main function
- */
- int main (void)
- {
- sysclk_init(); // Initiate the system clock
- board_init(); // Initiate the board (==> init.c)
- spi_master_init(SPI_EXAMPLE); // Initiate the SPI Module 1 as Master
- spi_master_setup_device(SPI_EXAMPLE, &SPI_DEVICE_EXAMPLE, my_spi_options.spi_mode,
- my_spi_options.baudrate, 0); // Parametrization for the SPI Module
- spi_setupChipReg(SPI_EXAMPLE,&my_spi_options,sysclk_get_pba_hz());
- spi_enable(SPI_EXAMPLE);
- // Initialize interrupt vector table support.
- irq_initialize_vectors();
- // Enable interrupts
- cpu_irq_enable();
- stdio_usb_init(); // I am using USB, if you don't then delete this line
- delay_init(sysclk_get_cpu_hz()); // I am using delays too, so unnecessary for the SPI
- // Example for a write operation - in SPI there is no write-only operation - but in this code part is no recording for incoming data
- spi_selectChip(SPI_EXAMPLE, SPI_SLAVECHIP_NUMBER); // First you have to select the SPI Module
- //Wait for the transmitter to be ready
- while(!spi_is_tx_ready(SPI_EXAMPLE));
- spi_put(SPI_EXAMPLE,32); // write on CTRL_REG1 of an LIS3DH MEMS-Sensor from ST Microeletronics
- //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));
- spi_put(SPI_EXAMPLE,39); // 10 Hz for all axis and enabling all of them
- // Deselect the slave
- spi_unselectChip(SPI_EXAMPLE,SPI_SLAVECHIP_NUMBER);
- // First transmission finished
- // Second transmission, similar to the first one
- spi_selectChip(SPI_EXAMPLE, SPI_SLAVECHIP_NUMBER);
- //Wait for the transmitter to be ready
- while(!spi_is_tx_ready(SPI_EXAMPLE));
- spi_put(SPI_EXAMPLE,35); // schreibe auf CTRL_REG4
- //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));
- spi_put(SPI_EXAMPLE,8); // Default bis auf High Resolution
- // Deselect the slave
- spi_unselectChip(SPI_EXAMPLE,SPI_SLAVECHIP_NUMBER);
- gpio_clr_gpio_pin(LED3_GPIO); // Turn on the LED3 to signal, that the Sensor is ready
- // Read instructions (232 is the instruction / 136... are dummies)
- int bytes[] = {232, 136, 136, 136, 136, 136, 136}; // 236 = Z / 234 = Y / 232 = X
- Byte answers[7] = {0};
- int index = 0;
- while (true) {
- //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
- index = 0;
- // Sending all the bytes to the sensor and recording the data from the sensor
- while(index < 7){
- spi_put(SPI_EXAMPLE,bytes[index]);
- //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));
- // the following line records the sensor data
- answers[index] = spi_get(SPI_EXAMPLE);
- index++;
- }
- while(!spi_is_tx_empty(SPI_EXAMPLE));
- // Deselect the slave
- spi_unselectChip(SPI_EXAMPLE,SPI_SLAVECHIP_NUMBER);
- // Just a little lightshow to signal if the acceleration for x is positive or negative
- if(answers[2] & 0x80){
- gpio_set_gpio_pin(LED3_GPIO);
- }
- else{
- gpio_clr_gpio_pin(LED3_GPIO);
- }
- // Sending ASCII "G" and the sensor data via usb to a terminal programm (HTerm)
- udi_cdc_putc(71);
- index = 1;
- while(index < 7){
- udi_cdc_putc(answers[index]);
- index++;
- }
- delay_ms(100); // Short Delay
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement