MAV'RIC
|
00001 /******************************************************************************* 00002 * Copyright (c) 2009-2016, MAV'RIC Development Team 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of the copyright holder nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software without 00017 * specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 ******************************************************************************/ 00031 00032 /******************************************************************************* 00033 * \file spi_buffered.h 00034 * 00035 * \author MAV'RIC Team 00036 * \author Felix Schill 00037 * 00038 * \brief SPI functions for the ATMega / AVR microcontrollers 00039 * This library provides buffered SPI send and receive 00040 * 00041 ******************************************************************************/ 00042 00043 00044 #ifndef SPI_BUFFERED_H 00045 #define SPI_BUFFERED_H 00046 00047 #ifdef __cplusplus 00048 extern "C" { 00049 #endif 00050 00051 #include <avr32/io.h> 00052 #include "libs/asf/avr32/utils/preprocessor/preprocessor.h" 00053 #include <stdint.h> 00054 #include "libs/asf/common/boards/user_board/user_board.h" 00055 #include "libs/asf/common/services/spi/spi_master.h" 00056 #include "dma_channel_config.h" 00057 typedef void (function_pointer_t)(void); 00058 00059 #define SPI_BUFFER_SIZE 32 ///< The SPI buffer size, this has to be a power of 2 00060 00061 #define SPI_BUFFER_MASK (SPI_BUFFER_SIZE - 1) ///< The SPI buffer mask 00062 00063 #define SPI_NUMBER 2 ///< The number of SPI 00064 00065 00069 typedef struct 00070 { 00071 volatile avr32_spi_t* spi; 00072 volatile uint8_t spi_out_buffer[SPI_BUFFER_SIZE]; 00073 volatile uint8_t spi_in_buffer[SPI_BUFFER_SIZE]; 00074 volatile uint8_t spi_in_buffer_head; 00075 volatile uint8_t spi_in_buffer_tail; 00076 volatile uint8_t spi_out_buffer_head; 00077 volatile uint8_t spi_out_buffer_tail; 00078 volatile uint8_t spi_receiver_on; 00079 volatile uint8_t traffic; 00080 volatile uint8_t transmission_in_progress; 00081 volatile uint8_t automatic; 00082 volatile function_pointer_t* callback_function; 00083 struct spi_device adc_spi; 00084 } spi_buffer_t; 00085 00086 00094 void spi_buffered_init(volatile avr32_spi_t* spi, int32_t spi_index); 00095 00103 uint8_t* spi_buffered_get_spi_in_buffer(int32_t spi_index); 00104 00111 void spi_buffered_init_DMA(int32_t spi_index, int32_t block_size); 00112 00121 void spi_buffered_trigger_DMA(int32_t spi_index, int32_t block_size); 00122 00129 void spi_buffered_set_callback(int32_t spi_index, function_pointer_t* function_pointer); 00130 00136 void spi_buffered_enable(int32_t spi_index); 00137 00143 void spi_buffered_disable(int32_t spi_index); 00144 00150 void spi_buffered_pause(int32_t spi_index); 00151 00157 void spi_buffered_resume(int32_t spi_index); 00158 00164 void spi_buffered_start(int32_t spi_index); 00165 00171 void spi_buffered_activate_receive(int32_t spi_index); 00172 00178 void spi_buffered_deactivate_receive(int32_t spi_index); 00179 00185 void spi_buffered_clear_read_buffer(int32_t spi_index); 00186 00194 uint8_t spi_buffered_get_traffic(int32_t spi_index); 00195 00203 uint8_t spi_buffered_read(int32_t spi_index); 00204 00214 void spi_buffered_write(int32_t spi_index, uint8_t value); 00215 00221 void spi_buffered_transmit(int32_t spi_index); 00222 00230 int8_t spi_buffered_is_transfered_finished(int32_t spi_index); 00231 00238 void spi_buffered_flush_buffer(int32_t spi_index); 00239 00247 uint8_t spi_buffered_bytes_available(int32_t spi_index); 00248 00249 #ifdef __cplusplus 00250 } 00251 #endif 00252 00253 #endif // SPI_BUFFERED_H