MAV'RIC
spi_buffered.h
1 /*******************************************************************************
2  * Copyright (c) 2009-2014, MAV'RIC Development Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  ******************************************************************************/
31 
32 /*******************************************************************************
33  * \file spi_buffered.h
34  *
35  * \author MAV'RIC Team
36  * \author Felix Schill
37  *
38  * \brief SPI functions for the ATMega / AVR microcontrollers
39  * This library provides buffered SPI send and receive
40  *
41  ******************************************************************************/
42 
43 
44 #ifndef SPI_BUFFERED_H
45 #define SPI_BUFFERED_H
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 #include <avr32/io.h>
52 #include "preprocessor.h"
53 #include <stdint.h>
54 #include "user_board.h"
55 #include "spi_master.h"
56 #include "dma_channel_config.h"
57 typedef void (function_pointer_t)(void);
58 
59 #define SPI_BUFFER_SIZE 32
60 
61 #define SPI_BUFFER_MASK (SPI_BUFFER_SIZE - 1)
62 
63 #define SPI_NUMBER 2
64 
65 
69 typedef struct
70 {
71  volatile avr32_spi_t *spi;
72  volatile uint8_t spi_out_buffer[SPI_BUFFER_SIZE];
73  volatile uint8_t spi_in_buffer[SPI_BUFFER_SIZE];
74  volatile uint8_t spi_in_buffer_head;
75  volatile uint8_t spi_in_buffer_tail;
76  volatile uint8_t spi_out_buffer_head;
77  volatile uint8_t spi_out_buffer_tail;
78  volatile uint8_t spi_receiver_on;
79  volatile uint8_t traffic;
80  volatile uint8_t transmission_in_progress;
81  volatile uint8_t automatic;
82  volatile function_pointer_t* callback_function;
83  struct spi_device adc_spi;
84 } spi_buffer_t;
85 
86 
94 void spi_buffered_init(volatile avr32_spi_t *spi, int32_t spi_index);
95 
103 uint8_t* spi_buffered_get_spi_in_buffer(int32_t spi_index);
104 
111 void spi_buffered_init_DMA(int32_t spi_index, int32_t block_size);
112 
121 void spi_buffered_trigger_DMA(int32_t spi_index, int32_t block_size);
122 
129 void spi_buffered_set_callback(int32_t spi_index, function_pointer_t* function_pointer);
130 
136 void spi_buffered_enable(int32_t spi_index);
137 
143 void spi_buffered_disable(int32_t spi_index);
144 
150 void spi_buffered_pause(int32_t spi_index);
151 
157 void spi_buffered_resume(int32_t spi_index);
158 
164 void spi_buffered_start(int32_t spi_index);
165 
171 void spi_buffered_activate_receive(int32_t spi_index);
172 
178 void spi_buffered_deactivate_receive(int32_t spi_index);
179 
185 void spi_buffered_clear_read_buffer(int32_t spi_index);
186 
194 uint8_t spi_buffered_get_traffic(int32_t spi_index);
195 
203 uint8_t spi_buffered_read(int32_t spi_index);
204 
214 void spi_buffered_write(int32_t spi_index, uint8_t value);
215 
221 void spi_buffered_transmit(int32_t spi_index);
222 
230 int8_t spi_buffered_is_transfered_finished(int32_t spi_index);
231 
238 void spi_buffered_flush_buffer(int32_t spi_index);
239 
247 uint8_t spi_buffered_bytes_available(int32_t spi_index);
248 
249 #ifdef __cplusplus
250 }
251 #endif
252 
253 #endif // SPI_BUFFERED_H
volatile uint8_t spi_in_buffer_head
The head of the SPI ingoing buffer.
Definition: spi_buffered.h:74
volatile uint8_t traffic
Read incoming data from SPI port.
Definition: spi_buffered.h:79
volatile uint8_t transmission_in_progress
Flag to know if there is a transmission going on.
Definition: spi_buffered.h:80
volatile function_pointer_t * callback_function
The callback function that gets called when the buffer is empty.
Definition: spi_buffered.h:82
The SPI buffer structure definition.
Definition: spi_buffered.h:69
volatile uint8_t spi_out_buffer_head
The head of the SPI outgoing buffer.
Definition: spi_buffered.h:76
volatile uint8_t spi_receiver_on
Flag to activate or not the SPI reception.
Definition: spi_buffered.h:78
volatile uint8_t spi_out_buffer_tail
The tail of the SPI outgoing buffer.
Definition: spi_buffered.h:77
volatile uint8_t spi_in_buffer_tail
The tail of the SPI ingoing buffer.
Definition: spi_buffered.h:75
volatile uint8_t automatic
Flag to send automatically over SPI or pause/stop transmission.
Definition: spi_buffered.h:81
volatile avr32_spi_t * spi
The pointer to the avr32 spi structure.
Definition: spi_buffered.h:71