Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:03

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSBSPsARMLPC24XX_dma
0007  *
0008  * @brief Direct memory access (DMA) support.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2008, 2011 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #include <rtems/endian.h>
0037 
0038 #include <bsp/lpc24xx.h>
0039 #include <bsp/dma.h>
0040 #include <bsp/io.h>
0041 
0042 /**
0043  * @brief Table that indicates if a channel is currently occupied.
0044  */
0045 static bool lpc24xx_dma_channel_occupation [GPDMA_CH_NUMBER];
0046 
0047 void lpc24xx_dma_initialize(void)
0048 {
0049   /* Enable module power */
0050   lpc24xx_module_enable(LPC24XX_MODULE_GPDMA, LPC24XX_MODULE_PCLK_DEFAULT);
0051 
0052   /* Disable module */
0053   GPDMA_CONFIG = 0;
0054 
0055   /* Reset registers */
0056   GPDMA_SOFT_SREQ = 0;
0057   GPDMA_SOFT_BREQ = 0;
0058   GPDMA_SOFT_LSREQ = 0;
0059   GPDMA_SOFT_LBREQ = 0;
0060   GPDMA_SYNC = 0;
0061   GPDMA_CH0_CFG = 0;
0062   GPDMA_CH1_CFG = 0;
0063 
0064   /* Enable module */
0065   #if BYTE_ORDER == LITTLE_ENDIAN
0066     GPDMA_CONFIG = GPDMA_CONFIG_EN;
0067   #else
0068     GPDMA_CONFIG = GPDMA_CONFIG_EN | GPDMA_CONFIG_MODE;
0069   #endif
0070 }
0071 
0072 rtems_status_code lpc24xx_dma_channel_obtain(unsigned channel)
0073 {
0074   if (channel < GPDMA_CH_NUMBER) {
0075     rtems_interrupt_level level;
0076     bool occupation = true;
0077 
0078     rtems_interrupt_disable(level);
0079     occupation = lpc24xx_dma_channel_occupation [channel];
0080     lpc24xx_dma_channel_occupation [channel] = true;
0081     rtems_interrupt_enable(level);
0082 
0083     return occupation ? RTEMS_RESOURCE_IN_USE : RTEMS_SUCCESSFUL;
0084   } else {
0085     return RTEMS_INVALID_ID;
0086   }
0087 }
0088 
0089 void lpc24xx_dma_channel_release(unsigned channel)
0090 {
0091   if (channel < GPDMA_CH_NUMBER) {
0092     lpc24xx_dma_channel_occupation [channel] = false;
0093   }
0094 }
0095 
0096 void lpc24xx_dma_channel_disable(unsigned channel, bool force)
0097 {
0098   if (channel < GPDMA_CH_NUMBER) {
0099     volatile lpc24xx_dma_channel *ch = GPDMA_CH_BASE_ADDR(channel);
0100     uint32_t cfg = ch->cfg;
0101 
0102     if (!force) {
0103       /* Halt */
0104       ch->cfg |= GPDMA_CH_CFG_HALT;
0105 
0106       /* Wait for inactive */
0107       do {
0108         cfg = ch->cfg;
0109       } while ((cfg & GPDMA_CH_CFG_ACTIVE) != 0);
0110     }
0111 
0112     /* Disable */
0113     ch->cfg &= ~GPDMA_CH_CFG_EN;
0114   }
0115 }