throttle speed before ejection
[aversive.git] / modules / comm / spi / spi_host.c
1 /*  
2  *  Copyright Droids Corporation (2008)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19
20 /*
21  * Host wrapper for SPI
22  */
23
24 #include <aversive.h>
25 #include <aversive/parts.h>
26 #include <aversive/error.h>
27
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <spi.h>
32 #include <spi_config.h>
33
34 /* global vars */
35 static volatile uint8_t g_ss_number;
36 static volatile spi_mode_t g_spi_mode;
37 static volatile uint8_t g_slave_selected;
38 static volatile uint8_t g_spi_order;
39 static volatile spi_format_t g_spi_format;
40 static volatile spi_mode_t g_spi_mode;
41 static volatile spi_clk_rate_t g_spi_clk_rate;
42
43 /*
44  * Register a pin as SS line
45  * Returns a unique identifier, or -1 on error
46  * There is always the physical SS line registered as 0
47  */
48 int8_t spi_register_ss_line(volatile uint8_t *port, uint8_t bitnum)
49 {
50         DEBUG(E_SPI, "Trying to register new SS line: port 0x%x, bitnum %d", port, bitnum);
51         /* too much SS lines (try to change SPI_MAX_SLAVES) */
52         if (g_ss_number >= SPI_MAX_SLAVES+1)
53                 return -1;
54
55         NOTICE(E_SPI, "New Slave Line registered: %d", g_ss_number);
56         return g_ss_number++;
57 }
58
59
60 /*
61  *      Set data order (default: MSB first)
62  */
63 void spi_set_data_order(uint8_t order)
64 {
65         g_spi_order = order;
66 }
67
68 /*
69  *      Get data order
70  */
71 uint8_t spi_get_data_order(void)
72 {
73         return g_spi_order;
74 }
75
76
77 /*
78  *      Initialize SPI
79  */
80 void spi_init(spi_mode_t mode, spi_format_t format, spi_clk_rate_t clk_rate)
81 {
82         NOTICE(E_SPI, "Init SPI: mode %d, format %d, clk_rate %d",
83                mode, format, clk_rate);
84
85         /* SS pin is not driven by SPI hardware 
86          * This is taken care of by spi_register_ss_line()
87          * EVEN for the "default" SS line */
88         g_ss_number = 0;
89         g_spi_format = format;
90         g_spi_clk_rate = clk_rate;
91         g_slave_selected = FALSE;
92         g_spi_mode = SPI_MODE_MASTER;
93         NOTICE(E_SPI, "Init done");
94 }
95
96 /*
97  *      Returns the state of SPI
98  */
99 inline spi_mode_t spi_get_mode(void)
100 {
101         return g_spi_mode;
102 }
103
104 /*
105  *      Send a byte (and receive one)
106  *      Returns the received byte
107  */
108 uint8_t spi_send_and_receive_byte(uint8_t byte)
109 {
110         /* XXX */
111         return 0;
112 }
113
114 /*
115  *      Send a byte, discard the result
116  */
117 inline void spi_send_byte(uint8_t byte)
118 {
119         spi_send_and_receive_byte(byte);
120 }
121
122 /*
123  *      Receives a byte (sends a NULL)
124  */
125 uint8_t spi_receive_byte(void)
126 {
127         return spi_send_and_receive_byte(0x00);
128 }
129
130 /*
131  *      Activates the selected SS line
132  */
133 uint8_t spi_slave_select(uint8_t slave)
134 {
135         if (g_slave_selected) {
136                 ERROR(E_SPI, "A slave is already selected !");
137                 return EBUSY;
138         }
139
140         /* XXX */
141         g_slave_selected = TRUE;
142         return ESUCCESS;
143 }
144
145 /*
146  *      Desactivates the selected SS line
147  */
148 void spi_slave_deselect(uint8_t slave)
149 {
150         /* XXX */
151         g_slave_selected = FALSE;
152 }
153
154 /*
155  *      Display SS lines
156  */
157 void spi_display_ss_lines(void)
158 {
159 }