00001 /* 00002 * Copyright (C) 2008-2009 by egnite GmbH 00003 * 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 3. Neither the name of the copyright holders nor the names of 00016 * contributors may be used to endorse or promote products derived 00017 * from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00022 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00023 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00024 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00025 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00026 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00027 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00028 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00029 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00030 * SUCH DAMAGE. 00031 * 00032 * For additional information see http://www.ethernut.de/ 00033 */ 00034 00044 #include <sys/device.h> 00045 #include <sys/nutdebug.h> 00046 00047 #include <dev/spibus.h> 00048 00072 int NutRegisterSpiDevice(NUTDEVICE * dev, NUTSPIBUS * bus, int cs) 00073 { 00074 int rc; 00075 NUTSPINODE *node; 00076 00077 NUTASSERT(dev != NULL); 00078 NUTASSERT(dev->dev_icb != NULL); 00079 node = (NUTSPINODE *) dev->dev_icb; 00080 00081 NUTASSERT(bus != NULL); 00082 NUTASSERT(bus->bus_initnode != NULL); 00083 00084 /* Attach the bus controller driver and set the chip select number 00085 ** before calling the bus controller initialization routine. */ 00086 node->node_bus = bus; 00087 node->node_cs = cs; 00088 rc = (*bus->bus_initnode) (node); 00089 00090 /* If the bus had been successfully initialized for this device, 00091 ** then set up a mutex lock for the bus and register the device. */ 00092 if (rc == 0) { 00093 NutEventPost(&bus->bus_mutex); 00094 rc = NutRegisterDevice(dev, 0, 0); 00095 } 00096 return rc; 00097 } 00098 00111 int NutSpiBusWait(NUTSPINODE * node, uint32_t tmo) 00112 { 00113 return 0; 00114 } 00115 00154 uint_fast16_t NutSpiBusSetMode(NUTSPINODE * node, uint_fast16_t mode) 00155 { 00156 uint16_t rc = node->node_mode; 00157 00158 if (mode != SPI_CURRENT_MODE) { 00159 node->node_mode = mode | SPI_MODE_UPDATE; 00160 } 00161 return rc; 00162 } 00163 00177 uint_fast32_t NutSpiBusSetRate(NUTSPINODE * node, uint_fast32_t rate) 00178 { 00179 uint32_t rc = node->node_rate; 00180 00181 if (rate != SPI_CURRENT_RATE) { 00182 node->node_rate = rate; 00183 node->node_mode |= SPI_MODE_UPDATE; 00184 } 00185 return rc; 00186 } 00187 00201 uint_fast8_t NutSpiBusSetBits(NUTSPINODE * node, uint_fast8_t bits) 00202 { 00203 uint_fast8_t rc = node->node_bits; 00204 00205 if (bits != SPI_CURRENT_BITS) { 00206 node->node_bits = bits; 00207 node->node_mode |= SPI_MODE_UPDATE; 00208 } 00209 return rc; 00210 }