davidzwa / advanced-iot-lisat

LISAT code for Avanced IoT course
1 stars 0 forks source link

Need to find simple 'multiple DMA/no-DMA' ADC example #4

Closed davidzwa closed 4 years ago

davidzwa commented 4 years ago

How to set up ADC at a certain pin and sample

davidzwa commented 4 years ago

Found question https://e2e.ti.com/support/microcontrollers/msp430/f/166/t/526409?Multiple-ADC-storage-using-MSP432-DMA which refers to https://e2e.ti.com/support/microcontrollers/msp430/f/166/t/526735

Here they use multiple IRQ's for interrupting the ADC channels. This is sync'ed ADC14_IRQHandler to make sure all ADC's are filled before continuing I guess? Nice concept if that's true.

davidzwa commented 4 years ago

Another post with more examples https://e2e.ti.com/support/microcontrollers/msp430/f/166/t/612768?MSP432P401R-Setting-up-DMA-for-multiple-repeating-ADC14-requests

Final answer gave this .c file adc14_multiple_channel_no_repeat_timera_source_03.zip

amjadmajid commented 4 years ago
/* --COPYRIGHT--,BSD_EX
 * Copyright (c) 2013, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *******************************************************************************
 *
 *                       MSP432 CODE EXAMPLE DISCLAIMER
 *
 * MSP432 code examples are self-contained low-level programs that typically
 * demonstrate a single peripheral function or device feature in a highly
 * concise manner. For this the code may rely on the device's power-on default
 * register values and settings such as the clock configuration and care must
 * be taken when combining code from several examples to avoid potential side
 * effects. Also see http://www.ti.com/tool/mspdriverlib for an API functional
 * library & https://dev.ti.com/pinmux/ for a GUI approach to peripheral configuration.
 *
 * --/COPYRIGHT--*/
//******************************************************************************
//******************************************************************************
//  MSP432P401 Demo - ADC14, Sample A1, AVcc Ref, Set P1.0 if A1 > 0.5*AVcc
//
//   Description: A single sample is made on A1 with reference to AVcc.
//   Software sets ADC14_CTL0_SC to start sample and conversion - ADC14_CTL0_SC
//   automatically cleared at EOC. ADC14 internal oscillator times sample (16x)
//   and conversion. In Mainloop MSP432 waits in LPM0 to save power until ADC14
//   conversion complete, ADC14_ISR will force exit from LPM0 in Mainloop on
//   reti. If A1 > 0.5*AVcc, P1.0 set, else reset. The full, correct handling of
//   and ADC14 interrupt is shown as well.
//
//
//                MSP432P401x
//             -----------------
//         /|\|              XIN|-
//          | |                 |
//          --|RST          XOUT|-
//            |                 |
//        >---|P5.4/A1      P1.0|-->LED
//
//   William Goh
//   Texas Instruments Inc.
//   June 2016 (updated) | November 2013 (created)
//   Built with CCSv6.1, IAR, Keil, GCC
//******************************************************************************
#include "ti/devices/msp432p4xx/inc/msp.h"

int main(void) {
    volatile unsigned int i;

    WDT_A->CTL = WDT_A_CTL_PW |             // Stop WDT
                 WDT_A_CTL_HOLD;

    // GPIO Setup
    P1->OUT &= ~BIT0;                       // Clear LED to start
    P1->DIR |= BIT0;                        // Set P1.0/LED to output
    P5->SEL1 |= BIT4;                       // Configure P5.4 for ADC
    P5->SEL0 |= BIT4;

    // Enable global interrupt
    __enable_irq();

    // Enable ADC interrupt in NVIC module
    NVIC->ISER[0] = 1 << ((ADC14_IRQn) & 31);

    // Sampling time, S&H=16, ADC14 on
    ADC14->CTL0 = ADC14_CTL0_SHT0_2 | ADC14_CTL0_SHP | ADC14_CTL0_ON;
    ADC14->CTL1 = ADC14_CTL1_RES_2;         // Use sampling timer, 12-bit conversion results

    ADC14->MCTL[0] |= ADC14_MCTLN_INCH_1;   // A1 ADC input select; Vref=AVCC
    ADC14->IER0 |= ADC14_IER0_IE0;          // Enable ADC conv complete interrupt

    SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk;   // Wake up on exit from ISR

    // Ensures SLEEPONEXIT takes effect immediately
    __DSB();

    while (1)
    {
        for (i = 20000; i > 0; i--);        // Delay

        // Start sampling/conversion
        ADC14->CTL0 |= ADC14_CTL0_ENC | ADC14_CTL0_SC;
        __sleep();

        __no_operation();                   // For debugger
    }
}

// ADC14 interrupt service routine
void ADC14_IRQHandler(void) {
    if (ADC14->MEM[0] >= 0x7FF)             // ADC12MEM0 = A1 > 0.5AVcc?
      P1->OUT |= BIT0;                      // P1.0 = 1
    else
      P1->OUT &= ~BIT0;                     // P1.0 = 0
}
amjadmajid commented 4 years ago
/* --COPYRIGHT--,BSD_EX
 * Copyright (c) 2013, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *******************************************************************************
 *
 *                       MSP432 CODE EXAMPLE DISCLAIMER
 *
 * MSP432 code examples are self-contained low-level programs that typically
 * demonstrate a single peripheral function or device feature in a highly
 * concise manner. For this the code may rely on the device's power-on default
 * register values and settings such as the clock configuration and care must
 * be taken when combining code from several examples to avoid potential side
 * effects. Also see http://www.ti.com/tool/mspdriverlib for an API functional
 * library & https://dev.ti.com/pinmux/ for a GUI approach to peripheral configuration.
 *
 * --/COPYRIGHT--*/
//******************************************************************************
//  MSP432P401 Demo- ADC14, Sample A2-A3 Differential,Set P1.0 if i/p> 1V
//
//   Description: A single sample is made on differential channel A2-A3
//   with reference to AVcc.
//   A resistor ladder is used to provide two different voltage inputs one at
//   A2 and the other at A3. Since equal resistors are used, the input voltage
//   at A2 and A3 are 2/3Vin and 1/3Vin respectively.
//   Vin is varied from 1V to 3V. When Vin = 3V, A2 = 2V and A3 = 1V providing
//   a differential voltage of 1V across the ADC input.
//   If A2-A3 >= 1V, P1.0 set, else reset. The full, correct handling of
//   and ADC14 interrupt is shown as well.
//
//
//                msp432p401
//  Vin= 1V-3V -----------------
//     |      |                 |
//    10K     |                 |
//     |      |                 |
//     |      |                 |
//     -------| A2=2/3Vin       |
//     |      |                 |
//     10K    |                 |
//     |      |                 |
//     -------| A3=1/3Vin       |
//     |      |                 |
//    10K     |             P1.0|-->LED
//     |      |                 |
//    GND
//
//   Dung Dang
//   Texas Instruments Inc.
//   October 2015 (updated) | November 2013 (created)
//   Built with Code Composer Studio V6.0
//******************************************************************************
#include "ti/devices/msp432p4xx/inc/msp.h"
#include "stdint.h"

int main(void) {
    volatile unsigned int i;
    WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;                 // Stop WDT

    // GPIO Setup
    P1->OUT &= ~BIT0;                           // Clear LED to start
    P1->DIR |= BIT0;                            // Set P1.0/LED to output
    P5->SEL1 |= BIT3 | BIT2;                    // Configure P5.3/2 (A2/3) for ADC
    P5->SEL0 |= BIT3 | BIT2;

    // Enable global interrupt
    __enable_irq();

    NVIC->ISER[0] = 1 << ((ADC14_IRQn) & 31);         // Enable ADC interrupt in NVIC module

    // Configure ADC14
    ADC14->CTL0 = ADC14_CTL0_SHT0_2 | ADC14_CTL0_SHP | ADC14_CTL0_ON;          // Sampling time, S&H=16, ADC14 on
    ADC14->CTL1 = ADC14_CTL1_RES_2;                   // Use sampling timer, 12-bit conversion results
    ADC14->MCTL[0] |= ADC14_MCTLN_INCH_2 | ADC14_MCTLN_DIF;     // A2 ADC input in dif. mode select; Vref=AVCC
    ADC14->IER0 |= ADC14_IER0_IE0;                    // Enable ADC conv complete interrupt

    SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk;   // Wake up on exit from ISR

    // Ensures SLEEPONEXIT occurs immediately
    __DSB();

    while (1)
    {
        for (i = 20000; i > 0; i--);           // Delay
      ADC14->CTL0 |= ADC14_CTL0_ENC | ADC14_CTL0_SC;         // Start sampling/conversion

      __sleep();

      __no_operation();                        // For debugger
    }
}

// ADC14 interrupt service routine

void ADC14_IRQHandler(void) {
    if (ADC14->MEM[0] >= 0x07FF)                   // ADC12MEM0 = A1 > 0.5AVcc?
      P1->OUT |= BIT0;                           // P1.0 = 1
    else
      P1->OUT &= ~BIT0;                          // P1.0 = 0
}
amjadmajid commented 4 years ago

The preferred way is to use a periodic timer to trigger the ADC sampling.

  1. ADC config https://github.com/amjadmajid/O_Robot/blob/master/System/ir_distance.c
  2. Function controller() is called periodically to sample the 3 ADCs https://github.com/amjadmajid/O_Robot/blob/master/Behaviors/controller.c
  3. Notice how we hooked the controller() function to timer A1 in the controller_init() function by passing its pointer.
  4. The timer configuration https://github.com/amjadmajid/O_Robot/blob/master/System/timer_A1.c
davidzwa commented 4 years ago

Hmm I thought we were going to couple the digital pin of the PMM-mics to an interrupt and let that trigger the IRQ for the ADC. Isnt that easier?

davidzwa commented 4 years ago

https://github.com/davidzwa/advanced-iot-lisat/blob/df685db5b83c9ee70cef86060bd34709f5eb6894/examples-ccs-v10/boostxl_edumkii_microphonefft_msp432p401r_MSP_EXP432P401R_nortos_ccs/main.c#L143

I find this kind of declaration quite nice, as it leaves detail to a set of underlying setup functions.

davidzwa commented 4 years ago

mic_code.zip

davidzwa commented 4 years ago

On-hold until we have ESP8266 code stable (which replaces this).

davidzwa commented 4 years ago

Found a good example with the TI-Driver library + CMSIS. No DMA is quite easy with SysConfig.

Name adcbufmultichannel_MSP_EXP432P401R_tirtos_ccs