spibus_npl.c

Go to the documentation of this file.
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 <cfg/spi.h>
00045 
00046 #include <sys/timer.h>
00047 #include <sys/nutdebug.h>
00048 
00049 #include <stdlib.h>
00050 #include <memdebug.h>
00051 #include <errno.h>
00052 
00053 #include <dev/cy2239x.h>
00054 #include <dev/npl.h>
00055 #include <dev/spibus_npl.h>
00056 
00057 #ifndef NPL_MMC_CLOCK
00058 #define NPL_MMC_CLOCK   12500000
00059 #endif
00060 
00068 static int NplSpiSetup(NUTSPINODE * node)
00069 {
00070 #if defined(NUT_PLL_NPLCLK1)
00071     uint32_t clk;
00072     uint32_t clkdiv;
00073 
00074     NUTASSERT(node != NULL);
00075     NUTASSERT(node->node_stat != NULL);
00076     NUTASSERT(node->node_bus != NULL);
00077     NUTASSERT(node->node_bus->bus_base != 0);
00078 
00079     /* Query the PLL number routed to Clock B. */
00080     clk = Cy2239xGetPll(NUT_PLL_NPLCLK1);
00081     /* Get the frequency of this PLL. */
00082     clk = Cy2239xPllGetFreq((int)clk, 7);
00083     /* Calculate the required divider value. */
00084     clkdiv = (clk + NPL_MMC_CLOCK - 10) / NPL_MMC_CLOCK;
00085 
00086     /* 
00087      * Not sure about the Cy-routines. The DIVSEL bit specifies which
00088      * divider is used, which is indirectly connected to S2, which is
00089      * high by default. For now set both dividers. 
00090      */
00091     if (Cy2239xSetDivider(NUT_PLL_NPLCLK1, 1, (int)clkdiv)) {
00092         return -1;
00093     }
00094     if (Cy2239xSetDivider(NUT_PLL_NPLCLK1, 0, (int)clkdiv)) {
00095         return -1;
00096     }
00097 
00098     /* Update interface parameters. */
00099     node->node_rate = clk / clkdiv;
00100     node->node_mode &= ~SPI_MODE_UPDATE;
00101 #endif
00102 
00103     return 0;
00104 }
00105 
00115 static int NplSpiChipSelect(uint_fast8_t cs, uint_fast8_t hi)
00116 {
00117     int rc = 0;
00118 
00119     switch (cs) {
00120     case 0:
00121         if (hi) {
00122             outb(NPL_XER, inb(NPL_XER) | NPL_MMCS);
00123         } else {
00124             outb(NPL_XER, inb(NPL_XER) & ~NPL_MMCS);
00125         }
00126         break;
00127     case 1:
00128         if (hi) {
00129             outb(NPL_XER, inb(NPL_XER) | NPL_NPCS0);
00130         } else {
00131             outb(NPL_XER, inb(NPL_XER) & ~NPL_NPCS0);
00132         }
00133         break;
00134     default:
00135         errno = EIO;
00136         rc = -1;
00137         break;
00138     }
00139     return rc;
00140 }
00141 
00153 int NplSpiBusSelect(NUTSPINODE * node, uint32_t tmo)
00154 {
00155     int rc;
00156 
00157     /* Sanity check. */
00158     NUTASSERT(node != NULL);
00159     NUTASSERT(node->node_bus != NULL);
00160     NUTASSERT(node->node_stat != NULL);
00161 
00162     /* Allocate the bus. */
00163     rc = NutEventWait(&node->node_bus->bus_mutex, tmo);
00164     if (rc) {
00165         errno = EIO;
00166     } else {
00167         /* If the mode update bit is set, then update our shadow registers. */
00168         if (node->node_mode & SPI_MODE_UPDATE) {
00169             NplSpiSetup(node);
00170         }
00171 
00172         /* Finally activate the node's chip select. */
00173         rc = NplSpiChipSelect(node->node_cs, 0);
00174         if (rc) {
00175             /* Release the bus in case of an error. */
00176             NutEventPost(&node->node_bus->bus_mutex);
00177         }
00178     }
00179     return rc;
00180 }
00181 
00190 int NplSpiBusDeselect(NUTSPINODE * node)
00191 {
00192     /* Sanity check. */
00193     NUTASSERT(node != NULL);
00194     NUTASSERT(node->node_bus != NULL);
00195 
00196     /* Deactivate the node's chip select. */
00197     NplSpiChipSelect(node->node_cs, 1);
00198 
00199     /* Release the bus. */
00200     NutEventPost(&node->node_bus->bus_mutex);
00201 
00202     return 0;
00203 }
00204 
00215 int NplSpiBusNodeInit(NUTSPINODE * node)
00216 {
00217     int rc;
00218     NUTSPIBUS *bus;
00219 
00220     /* Sanity check. */
00221     NUTASSERT(node != NULL);
00222     NUTASSERT(node->node_bus != NULL);
00223     bus = node->node_bus;
00224 
00225     /* Try to deactivate the node's chip select. */
00226     rc = NplSpiChipSelect(node->node_cs, 1);
00227 
00228     if (rc == 0) {
00229         NplSpiSetup(node);
00230     }
00231     return rc;
00232 }
00233 
00248 int NplSpiBusPollTransfer(NUTSPINODE * node, CONST void *txbuf, void *rxbuf, int xlen)
00249 {
00250     uint8_t b = 0xff;
00251     uint8_t *txp = (uint8_t *) txbuf;
00252     uint8_t *rxp = (uint8_t *) rxbuf;
00253     uintptr_t base;
00254 
00255     /* Sanity check. */
00256     NUTASSERT(node != NULL);
00257     NUTASSERT(node->node_bus != NULL);
00258     NUTASSERT(node->node_bus->bus_base != 0);
00259     base = node->node_bus->bus_base;
00260 
00261     while (xlen--) {
00262         if (txp) {
00263             b = *txp++;
00264         }
00265         /* Transmission starts by writing the transmit data. */
00266         outb(NPL_MMCDR, b);
00267         /* Wait for receiver data register full. */
00268         while ((inb(NPL_SLR) & NPL_MMCREADY) == 0);
00269         /* Read incoming data. */
00270         b = inb(NPL_MMCDR);
00271         if (rxp) {
00272             *rxp++ = b;
00273         }
00274     }
00275     return 0;
00276 }
00277 
00281 NUTSPIBUS spiBusNpl = {
00282     NULL,                   
00283     NULL,                   
00284     0,                      
00285     NULL,                   
00286     NplSpiBusNodeInit,      
00287     NplSpiBusSelect,        
00288     NplSpiBusDeselect,      
00289     NplSpiBusPollTransfer,  
00290     NutSpiBusWait,          
00291     NutSpiBusSetMode,       
00292     NutSpiBusSetRate,       
00293     NutSpiBusSetBits        
00294 };

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/