Advertisement
Guest User

Sample MSP432 SPI with DMA

a guest
Apr 10th, 2016
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.24 KB | None | 0 0
  1. /*
  2.  * -------------------------------------------
  3.  *    MSP432 DriverLib - v2_20_00_08
  4.  * -------------------------------------------
  5.  *
  6.  * --COPYRIGHT--,BSD,BSD
  7.  * Copyright (c) 2014, Texas Instruments Incorporated
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  *
  14.  * *  Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  *
  17.  * *  Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  *
  21.  * *  Neither the name of Texas Instruments Incorporated nor the names of
  22.  *    its contributors may be used to endorse or promote products derived
  23.  *    from this software without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  32.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  33.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  34.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  35.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  * --/COPYRIGHT--*/
  37. /*******************************************************************************
  38.  * MSP432 DMA - eUSCI SPI Transfer Using DMA
  39.  *
  40.  * Description: In this code example, the MSP432 's DMA controller is used in
  41.  * conjunction with the eUSCI_A0 SPI configuration to demonstrate how to use
  42.  * hardware triggered DMA transfers. One DMA transfer is setup, Channel 0 is
  43.  * for the DMA transfer. Once the transfers are setup and initialized, the
  44.  * device is put to sleep. While sleeping, the DMA controller will automatically
  45.  * transfer the data from the const array and out through the uESCI A0 SPI
  46.  * module as a master. When the DMA transfer is complete, an interrupt will be
  47.  * fired and the master will disable further interrupts
  48.  *
  49.  * This program runs infinitely until manually halted by the user.
  50.  *
  51.  *                MSP432P401
  52.  *             ------------------
  53.  *         /|\|                  |
  54.  *          | |                  |
  55.  *          --|RST    P1.1 (CLK) |
  56.  *            |       P1.3 (MOSI)|
  57.  *            |                  |
  58.  *
  59.  * Author: Ryan Brown
  60.  ******************************************************************************/
  61. /* DriverLib Includes */
  62. #include "driverlib.h"
  63.  
  64. /* Standard Includes */
  65. #include <stdint.h>
  66.  
  67. #include <string.h>
  68. #include <stdbool.h>
  69.  
  70. /* SPI Master Configuration Parameter */
  71. const eUSCI_SPI_MasterConfig spiMasterConfig =
  72. {
  73.         EUSCI_A_SPI_CLOCKSOURCE_SMCLK,             // MCLK Clock Source
  74.         12000000,                                  // MCLK = 12MHz
  75.         400000,                                    // SPICLK = 400khz
  76.         EUSCI_A_SPI_MSB_FIRST,                     // MSB First
  77.         EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,    // Phase
  78.         EUSCI_SPI_CLOCKPOLARITY_INACTIVITY_LOW,    // Low polarity
  79.         EUSCI_A_SPI_3PIN                           // 3Wire SPI Mode
  80. };
  81.  
  82. /* DMA Control Table */
  83. #ifdef ewarm
  84. #pragma data_alignment=256
  85. #else
  86. #pragma DATA_ALIGN(controlTable, 256)
  87. #endif
  88. uint8_t controlTable[256];
  89.  
  90. /* Extern */
  91. extern uint8_t data_array[];
  92.  
  93. int main(void)
  94. {
  95.     /* Halting Watchdog */
  96.     WDT_A_holdTimer();
  97.  
  98.     CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);
  99.  
  100.     /* Port 2.4 as GPIO used for CS active low */
  101.     GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN4);
  102.     GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN4);
  103.  
  104.     /* Selecting P2.1 P2.2 and P2.3 in SPI mode */
  105.     GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2,
  106.             GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
  107.  
  108.     /* Configuring SPI in 3wire master mode */
  109.     SPI_initMaster(EUSCI_A1_BASE, &spiMasterConfig);
  110.  
  111.     /* Enable SPI module */
  112.     SPI_enableModule(EUSCI_A1_BASE);
  113.  
  114.     /* Configuring DMA module */
  115.     DMA_enableModule();
  116.     DMA_setControlBase(controlTable);
  117.  
  118.     DMA_disableChannel(2);
  119.     /* Assigning Channel 2 to EUSCIA0TX0 */
  120.     DMA_assignChannel(DMA_CH2_EUSCIA1TX);
  121.  
  122.      /* Disabling channel attributes */
  123.     DMA_disableChannelAttribute(DMA_CH2_EUSCIA1TX,
  124.                                 UDMA_ATTR_ALTSELECT | UDMA_ATTR_USEBURST |
  125.                                 UDMA_ATTR_HIGH_PRIORITY |
  126.                                 UDMA_ATTR_REQMASK);
  127.  
  128.     /* Setting Control Indexes */
  129.     DMA_setChannelControl(UDMA_PRI_SELECT | DMA_CH2_EUSCIA1TX,
  130.             UDMA_SIZE_8 | UDMA_SRC_INC_8 | UDMA_DST_INC_NONE | UDMA_ARB_1);
  131.     DMA_setChannelTransfer(UDMA_PRI_SELECT | DMA_CH2_EUSCIA1TX,
  132.             UDMA_MODE_BASIC, data_array,
  133.             (void*) MAP_SPI_getTransmitBufferAddressForDMA(EUSCI_A1_BASE), 1024);
  134.  
  135.     /* Assigning/Enabling Interrupts */
  136.     DMA_assignInterrupt(DMA_INT1, 2);
  137.     Interrupt_enableInterrupt(INT_DMA_INT1);
  138.  
  139.     /* Drive CS Low to indicate start of the dtransfer */
  140.     GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN4);
  141.  
  142.     /* Now that the DMA is primed and setup, enabling the channels. The EUSCI
  143.      * hardware should take over and transfer/receive all bytes */
  144.     DMA_enableChannel(2);
  145.  
  146.     /* Polling to see if the TX buffer is ready */
  147.     while (!(SPI_getInterruptStatus(EUSCI_A1_BASE,EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
  148.  
  149.     //PCM_gotoLPM0InterruptSafe();
  150.  
  151.     while(1);
  152. }
  153.  
  154. /* Completion interrupt for eUSCIA0 TX */
  155. void dma_1_interrupt(void)
  156. {
  157.     /* Disabling the completion interrupt and disabling the DMA channels */
  158.     DMA_disableChannel(2);
  159.     DMA_disableInterrupt(INT_DMA_INT1);
  160.     /* Drive CS High to indicate end of the dtransfer */
  161.     GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN4);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement