OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [spi/] [current/] [src/] [spi.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      spi.c
4
//
5
//      Generic API for accessing devices attached to an SPI bus
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 2004 Free Software Foundation, Inc.                        
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//###DESCRIPTIONBEGIN####
41
//
42
// Author(s):     bartv
43
// Date:          2004-04-23
44
//
45
//###DESCRIPTIONEND####
46
//========================================================================
47
 
48
#include <pkgconf/infra.h>
49
#include <pkgconf/io_spi.h>
50
#include <cyg/infra/cyg_ass.h>
51
#include <cyg/io/spi.h>
52
 
53
// The simple transfer function just calls the transaction functions
54
// in order. The chip select should be dropped at the end of this
55
// transfer.
56
void
57
cyg_spi_transfer(cyg_spi_device* device, cyg_bool polled, cyg_uint32 count, const cyg_uint8* tx_data, cyg_uint8* rx_data)
58
{
59
    cyg_spi_transaction_begin(device);
60
    cyg_spi_transaction_transfer(device, polled, count, tx_data, rx_data, 1);
61
    cyg_spi_transaction_end(device);
62
}
63
 
64
// Tick is similarly simple.
65
void
66
cyg_spi_tick(cyg_spi_device* device, cyg_bool polled, cyg_uint32 count)
67
{
68
    cyg_spi_transaction_begin(device);
69
    cyg_spi_transaction_tick(device, polled, count);
70
    cyg_spi_transaction_end(device);
71
}
72
 
73
// get_config() and set_config() requires locking the bus, then accessing the
74
// bus-specific function via the bus structure. Only locking is needed, not
75
// setting up the bus for a transfer to this device, so the transaction begin
76
// function is inappropriate.
77
int
78
cyg_spi_get_config(cyg_spi_device* device, cyg_uint32 key, void* buf, cyg_uint32* len)
79
{
80
    int             result;
81
    cyg_spi_bus*    bus;
82
    CYG_CHECK_DATA_PTR(device, "valid SPI device pointer required");
83
 
84
    bus = device->spi_bus;
85
    CYG_CHECK_DATA_PTR(bus, "SPI device does not point at a valid bus structure");
86
    CYG_CHECK_FUNC_PTR(bus->spi_get_config, "SPI bus driver has not provided a get_config function");
87
 
88
    while (!cyg_drv_mutex_lock(&(bus->spi_lock)));
89
    result = (*(bus->spi_get_config))(device, key, buf, len);
90
    cyg_drv_mutex_unlock(&(bus->spi_lock));
91
 
92
    return result;
93
}
94
 
95
// set_config is basically just a clone of the above, but using a different
96
// bus-specific operation.
97
int
98
cyg_spi_set_config(cyg_spi_device* device, cyg_uint32 key, const void* buf, cyg_uint32* len)
99
{
100
    int             result;
101
    cyg_spi_bus*    bus;
102
    CYG_CHECK_DATA_PTR(device, "valid SPI device pointer required");
103
 
104
    bus = device->spi_bus;
105
    CYG_CHECK_DATA_PTR(bus, "SPI device does not point at a valid bus structure");
106
    CYG_CHECK_FUNC_PTR(bus->spi_set_config, "SPI bus driver has not provided a set_config function");
107
 
108
    while (!cyg_drv_mutex_lock(&(bus->spi_lock)));
109
    result = (*(bus->spi_set_config))(device, key, buf, len);
110
    cyg_drv_mutex_unlock(&(bus->spi_lock));
111
 
112
    return result;
113
}
114
 
115
// transaction-begin involves getting sole access to the bus, then calling
116
// the bus driver's begin function to set up the hardware appropriately for
117
// the target device.
118
void
119
cyg_spi_transaction_begin(cyg_spi_device* device)
120
{
121
    cyg_spi_bus*    bus;
122
    CYG_CHECK_DATA_PTR(device, "valid SPI device pointer required");
123
 
124
    bus = device->spi_bus;
125
    CYG_CHECK_DATA_PTR(bus, "SPI device does not point at a valid bus structure");
126
    CYG_CHECK_FUNC_PTR(bus->spi_transaction_begin, "SPI device has not provided a transaction_begin function");
127
 
128
    while (!cyg_drv_mutex_lock(&(bus->spi_lock)));
129
#ifdef CYGDBG_USE_ASSERTS    
130
    bus->spi_current_device = device;
131
#endif
132
 
133
    // This thread now has exclusive access to the bus
134
    (*(bus->spi_transaction_begin))(device);
135
 
136
    // All done. Return with the bus still locked.
137
}
138
 
139
// A variant of the above which returns immediately if some other thread
140
// is using the bus.
141
cyg_bool
142
cyg_spi_transaction_begin_nb(cyg_spi_device* device)
143
{
144
    cyg_bool        result  = false;
145
    cyg_spi_bus*    bus;
146
    CYG_CHECK_DATA_PTR(device, "valid SPI device pointer required");
147
 
148
    bus = device->spi_bus;
149
    CYG_CHECK_DATA_PTR(bus, "SPI device does not point at a valid bus structure");
150
    CYG_CHECK_FUNC_PTR(bus->spi_transaction_begin, "SPI device has not provided a transaction_begin function");
151
 
152
    if (cyg_drv_mutex_trylock(&(bus->spi_lock))) {
153
#ifdef CYGDBG_USE_ASSERTS        
154
        bus->spi_current_device = device;
155
#endif        
156
        (*(bus->spi_transaction_begin))(device);
157
        result = true;
158
    }
159
 
160
    return result;
161
}
162
 
163
void
164
cyg_spi_transaction_transfer(cyg_spi_device* device, cyg_bool polled,
165
                             cyg_uint32 count, const cyg_uint8* tx_data, cyg_uint8* rx_data,
166
                             cyg_bool drop_cs)
167
{
168
    cyg_spi_bus*    bus;
169
    CYG_CHECK_DATA_PTR(device, "valid SPI device pointer required");
170
 
171
    bus = device->spi_bus;
172
    CYG_CHECK_DATA_PTR(bus, "SPI device does not point at a valid bus structure");
173
    CYG_ASSERT(bus->spi_current_device == device, "SPI transfer requested without claiming the bus");
174
    CYG_CHECK_FUNC_PTR(bus->spi_transaction_transfer, "SPI device has not provided a transfer function");
175
    (*(bus->spi_transaction_transfer))(device, polled, count, tx_data, rx_data, drop_cs);
176
}
177
 
178
void
179
cyg_spi_transaction_tick(cyg_spi_device* device, cyg_bool polled, cyg_uint32 count)
180
{
181
    cyg_spi_bus*    bus;
182
    CYG_CHECK_DATA_PTR(device, "valid SPI device pointer required");
183
 
184
    bus = device->spi_bus;
185
    CYG_CHECK_DATA_PTR(bus, "SPI device does not point at a valid bus structure");
186
    CYG_ASSERT(bus->spi_current_device == device, "SPI ticks requested without claiming the bus");
187
    CYG_CHECK_FUNC_PTR(bus->spi_transaction_tick, "SPI device has not provided a tick function");
188
 
189
    (*(bus->spi_transaction_tick))(device, polled, count);
190
}
191
 
192
void
193
cyg_spi_transaction_end(cyg_spi_device* device)
194
{
195
    cyg_spi_bus*    bus;
196
    CYG_CHECK_DATA_PTR(device, "valid SPI device pointer required");
197
 
198
    bus = device->spi_bus;
199
    CYG_CHECK_DATA_PTR(bus, "SPI device does not point at a valid bus structure");
200
    CYG_ASSERT(bus->spi_current_device == device, "SPI transfer requested without claiming the bus");
201
    CYG_CHECK_FUNC_PTR(bus->spi_transaction_end, "SPI device has not provided an end function");
202
 
203
    // First, call the bus' end function.
204
    (*(bus->spi_transaction_end))(device);
205
 
206
    // Then release the SPI bus for other threads.
207
    cyg_drv_mutex_unlock(&(bus->spi_lock));
208
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.