AVRly - AVR Development Resources
atmega_spi.h
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file atmega_spi.h
25 * @ingroup mcp48x2
26 * @author Jason Duffy
27 * @date 9th April 2022
28 * @brief Driver for SPI communication between the ATmega328P and other SPI
29 * compatible devices.
30 * @bug No known bugs.
31 * @details This file provides the basic SPI comms setup and initialisation
32 * instructions. The following ports/pins must be defined in pin_defines.h:
33 * SPI_DDR, SPI_PORT, SPI_MOSI SPI_MISO, SPI_SCK, SPI_SS.
34 */
35
36#ifndef ATMEGA_SPI_DOT_H
37#define ATMEGA_SPI_DOT_H
38
39#include <stdint.h>
40
41/**
42 * Enumerated constants for selecting the transfer mode, most significant bit
43 * first of least significant bit first.
44 */
45typedef enum
46{
47 lsb_first,
48 msb_first,
50
51/**
52 * Enumerated constants for selecting whether the host MCU is the controller or
53 * peripheral (formerly known as master/slave).
54 */
55typedef enum
56{
57 peripheral,
58 controller,
60
61/**
62 * Enumerated constants for selecting the SPI polarity mode, rising or falling
63 * on leading clock edge.
64 */
65typedef enum
66{
67 rising_edge,
68 falling_edge,
70
71/**
72 * Enumerated constants for selecting the SPI phase mode, on leading clock edge.
73 * lead_sample_rising_edge is default.
74 */
75typedef enum
76{
77 sample_leading_edge,
78 sample_trailing_edge,
80
81/**
82 * Enumerated constants for selecting the SPI clock rate. cpu_clk_div_4 is
83 * default.
84 */
85typedef enum
86{
87 cpu_clk_div_4,
88 cpu_clk_div_16,
89 cpu_clk_div_64,
90 cpu_clk_div_128,
92
93/**
94 * Enumerated constants for selecting whether the SPI clock speed is doubled.
95 */
96typedef enum
97{
98 single_speed,
99 double_speed,
101
102
103/**
104 * Initialisation routine to set up SPI comms. Must be called before any other
105 * functions in this file can be used.
106 * @param transfer_mode lsb_first or msb_first.
107 * @param control_mode controller or peripheral.
108 * @param polarity_mode rising_edge or falling_edge.
109 * @param phase_mode lead_sample_rising_edge or lead_setup_rising_edge.
110 * @param clk_rate Sets the speed of the SPI clock - divided down from F_CPU
111 * speed.
112 * @param dbl_clock single_speed or double_speed.
113 */
114void init_spi(spi_transfer_mode_t transfer_mode,
115 spi_control_mode_t control_mode,
116 spi_polarity_mode_t polarity_mode,
117 spi_phase_mode_t phase_mode,
118 spi_clk_rate_t clk_rate,
119 spi_dbl_clk_mode_t dbl_clock);
120
121
122/**
123 * Sends out a byte of data over SPI and returns the byte it receives.
124 * @param uint8_t data: The byte of data to be sent from the host MCU to the
125 * peripheral device.
126 * @return The data received from the peripheral device is returned.
127 */
128uint8_t spi_trade_byte(uint8_t data);
129
130
131/**
132 * Sends out a 16bit word of data over spi (in two bytes) and returns the byte
133 * it receives.
134 * @param uint16_t data: The byte of data to be sent from the host MCU to the
135 * peripheral device.
136 * @return The data received from the peripheral device is returned.
137 */
138uint16_t spi_trade_word(uint16_t data);
139
140
141#endif // ATMEGA_SPI_DOT_H
142
143
144/*** End of file. ***/
spi_dbl_clk_mode_t
Enumerated constants for selecting whether the SPI clock speed is doubled.
Definition: atmega_spi.h:97
spi_transfer_mode_t
Enumerated constants for selecting the transfer mode, most significant bit first of least significant...
Definition: atmega_spi.h:46
spi_phase_mode_t
Enumerated constants for selecting the SPI phase mode, on leading clock edge.
Definition: atmega_spi.h:76
void init_spi(spi_transfer_mode_t transfer_mode, spi_control_mode_t control_mode, spi_polarity_mode_t polarity_mode, spi_phase_mode_t phase_mode, spi_clk_rate_t clk_rate, spi_dbl_clk_mode_t dbl_clock)
Initialisation routine to set up SPI comms.
Definition: atmega_spi.c:53
spi_polarity_mode_t
Enumerated constants for selecting the SPI polarity mode, rising or falling on leading clock edge.
Definition: atmega_spi.h:66
spi_clk_rate_t
Enumerated constants for selecting the SPI clock rate.
Definition: atmega_spi.h:86
spi_control_mode_t
Enumerated constants for selecting whether the host MCU is the controller or peripheral (formerly kno...
Definition: atmega_spi.h:56
uint8_t spi_trade_byte(uint8_t data)
Sends out a byte of data over SPI and returns the byte it receives.
Definition: atmega_spi.c:85
uint16_t spi_trade_word(uint16_t data)
Sends out a 16bit word of data over spi (in two bytes) and returns the byte it receives.
Definition: atmega_spi.c:98