Jump to content
CorB

How to use an ADCpin for GPIO ?

Recommended Posts

Hi all,

 

I am making a simple setup to use MSP430 alike naming of the pins on the C2000 launchpad.

 

//LEFTSIDE PINS
//VCC 3.3V
#define P1_0 ADCINA6
#define P1_1 GPIO_Number_28
#define P1_2 GPIO_Number_29
#define P1_3 GPIO_Number_34
#define P1_4 ADCINA4
#define P1_5 GPIO_Number_18

#define P2_0 ADCINA2
#define P2_1 ADCINB2
#define P2_2 ADCINB4

//RIGHTSIDE PINS
//GND
#define P2_6 GPIO_Number_19
#define P2_7 GPIO_Number_12
//TEST ->NC
//RST ->RST
#define P1_7 GPIO_Number_16
#define P1_6 GPIO_Number_17
#define P2_5 GPIO_Number_6
#define P2_4 GPIO_Number_7
#define P2_3 ADCINB6

 

This allows me to use for instance:

GPIO_setMode(myGpio, P1_0, GPIO_0_Mode_GeneralPurpose);

 

Unfortunately it seems that the ADCpins cannot be used in this fashion as they are not assigned a normal GPIO number. In the code I am trying to use for SPI TI_Trey suggest to use :

EALLOW;
GpioCtrlRegs.AIOMUX1.bit.AIO4 = 0;
GpioCtrlRegs.AIODIR.bit.AIO4 = 1;
GpioDataRegs.AIODAT.bit.AIO4 = 1;
EDIS;

to activate ADCIN4 (P1_4) for output.

 

Anybody has a clue how we could make this easier ?

It would make it a lot simpler to translate code developed for an MSP430 boosterpack to be used on the C2000 as we can at least easily refer to the pins.

 

cheers

CorB

Share this post


Link to post
Share on other sites

Well you could do this:

 

#define AIO4toGPIO {EALLOW; \
GpioCtrlRegs.AIOMUX1.bit.AIO4 = 0; \
GpioCtrlRegs.AIODIR.bit.AIO4 = 1; \
GpioDataRegs.AIODAT.bit.AIO4 = 1; \
EDIS; }

 

There might also be functions in the driver library to do this for you...I forget. If there aren't, I can add them in a month or two....I'm sooo busy right now.

Share this post


Link to post
Share on other sites

Hi Trey,

 

Thanks for the feedback, it would be good to be able (in the longer term) to use the GPIO_setMode etc. functions on all GPIO pins. The ADCpins are also GPIO pins and now can only be approached through low level functions. I will see if I can allready wrap up something myself based on the info you have provided.

 

cheers

Cor

Share this post


Link to post
Share on other sites

Here's the list of all pins with corresponding GPIO and ADCnames, I am using this as a reference but will try to create a function that allows easy and simple setting of all the GPIO pins. I am now also going for a naming style that is equal to the pinnumber/name on the launchpad, so J1 and J2 (outer pingroups) and J5 / J6 for the inner pingroups.

 

 

.... code removed see final coding in later message #15

Share this post


Link to post
Share on other sites

Hi,

 

In the code below I have created 2 functions InputPin and OutputPin that can be used to set a GPIO (and soon ADC)-pin to be used as either input or output. You can use either the predefined GPIO_number setup or MSP430 pins or C2kPins as a name.

So

InputPin(GPIO_Number_12) //button

InputPin(J2_3)

InputPin(P2_7)

all set the same pin as an inputpin.

 

Surely not perfect and I am also working on getting the ADCpins to be controlled in the same simple fashion.

 

cheers

Cor

PS. It seems that we cannot use all the ADCpins as GPIO (see page 89 in http://www.ti.com/li...3c/sprufn3c.pdf), the AIOMux1 register only allows AIO2/4/6/10/12/14 to be used as GPIO. This corresponds to ADCINA2, ADCINA4,ADCINA6, ADCINB2, ADCINB4, ADCINB6 on the launchpad.These are the ADCpins on J1 and J2 (or P1 and P2) so they can also be used with MSP430 Boosterpacks

 

.... code removed ... see later message #15

Share this post


Link to post
Share on other sites

EDIT WARNING: Ive had a few pins not working yet as planned ... code is therefore not yet READY !!!

 

OK. here we go. The code below was tested (BTW NOT ALL PINS/combinations HAVE BEEN TESTED !) with both an external LED (tested on a normal GPIO and an ADC-GPIO pin) and an external button on a few pins.

 

Basically there are 2 functions that control the pins (all 6 ADC GPIO pins and pins until GPIO_Number_31);

void SetPin(uint16_t pinNumber, GPIO_Direction_e pinmode)

and

void PinData(uint16_t pinNumber, GPIO_Function data)

 

Based on those 2 the other functions are just predefined calls:

OutputPin() and InputPin() can be called to set a pin (using any naming convention that I supplied in the defines)

PinLow(), PinHigh() and PinToggle() can be called to set the state of a pin.

NOTE: GPIOpins12-31 have by default a pullup resistor enabled after startup, in this code a call to OutputPin/InputPin clears the pullups. So NO pin actually has a pullup. If you need it a call will be necessary to the GpioCtrlRegs.GPAPUD register with the appropriate bit set for a given GPIO. The ADC GPIO pins do NOT have a pullup register.

 

cheers,

 

Cor

BTW: No smoke was detected during testing ... I can guarantee that your testing will also be smokeless ....

 

 

... code removed ... see later message #15

Edited by CorB

Share this post


Link to post
Share on other sites

Ive been testing several GPIO and ADCpins, oddly not all of the pins work OK. The current test - not complete - shows that several GPIOpins (number<16) seem to be working and the same can be said for ADC pins (ADCINA group). I will test all pins on J1/J2 to see if a real pattern emerges.

 

During this test I only check if setting the pin HIGH or LOW actually raises/lowers the voltage on the pin to lit up an LED (with 220 ohm resistor)

Share this post


Link to post
Share on other sites

Ive been testing several GPIO and ADCpins, oddly not all of the pins work OK. The current test - not complete - shows that several GPIOpins (number<16) seem to be working and the same can be said for ADC pins (ADCINA group). I will test all pins on J1/J2 to see if a real pattern emerges.

 

During this test I only check if setting the pin HIGH or LOW actually raises/lowers the voltage on the pin to lit up an LED (with 220 ohm resistor)

You can do a quick check without any hardware by using the real time feature :

 

Real-Time Control - A C2000 Debugging Gem

 

Share this post


Link to post
Share on other sites

Hi

 

The current checking method is ever so simple, I connect an LED to a pin run the code (which travels over the pins) and see if it blinks ... thusfar I have ODD results.

 

When I issue this code:

 

GpioCtrlRegs.GPAMUX2.bit.GPIO19=0;

GpioCtrlRegs.GPADIR.bit.GPIO19=1;

GpioDataRegs.GPACLEAR.bit.GPIO19=1;

LED3 (GPIO03) lits up ...

 

I would like to know if i am the only one .....

 

EDIT: warning about toggle effects removed ... they were it seems a glitch ... still unclear why the GPIO0-3 pins sometimes lit up ...

Edited by CorB

Share this post


Link to post
Share on other sites

Hi

 

The current checking method is ever so simple, I connect an LED to a pin run the code (which travels over the pins) and see if it blinks ... thusfar I have ODD results.

 

When I issue this code:

 

GpioCtrlRegs.GPAMUX2.bit.GPIO19=0;

GpioCtrlRegs.GPADIR.bit.GPIO19=1;

GpioDataRegs.GPACLEAR.bit.GPIO19=1;

LED3 (GPIO03) lits up ...

in fact when I toggle GPIO19 ... LED3 blinks ...

 

I would like to know if i am the only one .....

Toggle is going to blink the LED each time you issue toggle. If a CLEAR is lighting up your LED, it must be sinking current.

Share this post


Link to post
Share on other sites

The LED connection is OK. Works on other pins too. Can you check if you can get a high and a low output on GPIO19 ? I am getting - using the same kind of code - good output on GPIO0-7 ... ADCINA2, ADCINA4 and ADCINA6.

Share this post


Link to post
Share on other sites

Hi,

 

After debugging and help from a fellowuser (DanAndDusty) the code is now working as planned, I will remove all older code to avoid people using the wrong coding. Here's the final setup, can be used in a library.

 

Thanks for all that have helped,

 

regards

CorB

 


typedef enum  // create GPIO functionality
{
   GPIO_Low=0,    //
   GPIO_High,
   GPIO_Toggle   //

} GPIO_Function;


//PINOUT SECTION

//ADCpins are numbered>=100, when >200 they are also GPIO enabled pins
#define ADCINA0 100  //
#define ADCINA1 101
#define ADCINA2 202  // can be used as GPIO
#define ADCINA3 103
#define ADCINA4 204  // can be used as GPIO
#define ADCINA6 206  // can be used as GPIO
#define ADCINA7 107

#define ADCINB1 109
#define ADCINB2 210 // can be used as GPIO
#define ADCINB3 111
#define ADCINB4 212 // can be used as GPIO
#define ADCINB6 214 // can be used as GPIO
#define ADCINB7 115

//MSP430 pinnumber
//LEFTSIDE PINS 
//VCC 
#define P1_0 ADCINA6
#define P1_1 GPIO_Number_28
#define P1_2 GPIO_Number_29
#define P1_3 GPIO_Number_34
#define P1_4 ADCINA4          //OK
#define P1_5 GPIO_Number_18
#define P2_0 ADCINA2
#define P2_1 ADCINB2
#define P2_2 ADCINB4

//RIGHTSIDE PINS
//GND
#define P2_6 GPIO_Number_19
#define P2_7 GPIO_Number_12
//TEST ->NC
//RST ->RST
#define P1_7 GPIO_Number_16
#define P1_6 GPIO_Number_17
#define P2_5 GPIO_Number_6
#define P2_4 GPIO_Number_7
#define P2_3 ADCINB6

#define MSP_Button P1_2
#define MSP_LED0 P1_3
#define MSP_LED1 P1_6

//C2k PINNUMBERS
//LEFT OUTER CONNECTOR
//J1_1 VCC
#define J1_2 ADCINA6           //OK
#define J1_3 GPIO_Number_28    //
#define J1_4 GPIO_Number_29
#define J1_5 GPIO_Number_34
#define J1_6 ADCINA4           //ok
#define J1_7 GPIO_Number_18    // not ok blinks led02
#define J1_8 ADCINA2          // ok
#define J1_9 ADCINB2
#define J1_10 ADCINB4         // blinks led0

//RIGHT OUTER CONNECTOR
//J2_1 GND
#define J2_2 GPIO_Number_19 // not OK blinks led3
#define J2_3 GPIO_Number_12
//J2_4 TEST ->NC
//J2_5 RST ->RST
#define J2_6 GPIO_Number_16
#define J2_7 GPIO_Number_17 //Not OK blinks led1
#define J2_8 GPIO_Number_6  //OK
#define J2_9 GPIO_Number_7  //OK
#define J2_10 ADCINB6       // not OK

//LEFT INNER CONNECTOR
//J5_1 VCC
//J5_2 GND
#define J5_3 ADCINA7
#define J5_4 ADCINA3
#define J5_5 ADCINA1
#define J5_6 ADCINA0
#define J5_7 ADCINB1
#define J5_8 ADCINB3
#define J5_9 ADCINB7
//J5_10 NC

//RIGHT INNER CONNECTOR
#define J6_1 GPIO_Number_0
#define J6_2 GPIO_Number_1
#define J6_3 GPIO_Number_2
#define J6_4 GPIO_Number_3
#define J6_5 GPIO_Number_4
#define J6_6 GPIO_Number_5
#define J6_7 GPIO_Number_32
#define J6_8 GPIO_Number_33
//J6_9 NC
//J6_10 NC

#define C2K_button GPIO_Number_12
#define C2K_Led0 GPIO_Number_0
#define C2K_Led1 GPIO_Number_1
#define C2K_Led2 GPIO_Number_2
#define C2K_Led3 GPIO_Number_3

///***** functions 
void SetPin(uint32_t pinNumber, GPIO_Direction_e pinmode) // 32GPIOpins and 6 ADCPins can be set pinmode 0=input, 1=output
{
uint32_t realpin;

EALLOW;
realpin=pinNumber;

if (pinNumber<32) // default "real" GPIOpins
{
if (pinNumber<16) // pins 0 to 15
{ GpioCtrlRegs.GPAMUX1.all &= ~((unsigned long)3 << (realpin*2)); // set 2 bits for this pin to 00 = default GPIO function
}

if (pinNumber>15) // pins 16 to 31
   {  realpin=pinNumber-16;
      GpioCtrlRegs.GPAMUX2.all &= ~((unsigned long)3 << (realpin*2)); // set 2 bits for this pin to 00 = default GPIO function
}


if (pinmode==GPIO_Direction_Output)
{  GpioCtrlRegs.GPADIR.all |= ((unsigned long)1<<pinNumber);
   }
else

{ GpioCtrlRegs.GPADIR.all &= ~((unsigned long)1<<pinNumber); //  set the pin to input, set bit=0
}

GpioCtrlRegs.GPAPUD.all |= ((unsigned long)1<<pinNumber); // always disable pullup by default, normal default after reset is GPIO0_11 are disabled, others are enabled

}

if (pinNumber>200) // ADC pins  that can be used as GPIO
{
realpin=pinNumber-200;

GpioCtrlRegs.AIOMUX1.all &= ~((unsigned long)3<<(realpin*2)); // write 2 bits (00=GPIO) in the AIOmux register

if (pinmode==GPIO_Direction_Output)
{  GpioCtrlRegs.AIODIR.all |= ((unsigned long)1<<realpin); // set as output set bit=1
}
else
{  GpioCtrlRegs.AIODIR.all &= ~((unsigned long)1<<realpin); // set as input set bit=0
}
}

EDIS;

}

// set pin low, high or togglepin
void PinData(uint32_t pinNumber, GPIO_Function data)
{ uint32_t realpin;
EALLOW;
 realpin=pinNumber;
 if (pinNumber<32)
  {
switch (data)
   {
 case 0: GpioDataRegs.GPADAT.all    &= ~((unsigned long)1<<realpin); //  set the pin low
 break;
 case 1: GpioDataRegs.GPADAT.all    |= ((unsigned long)1<<realpin); //  set the pin high
 break;
 case 2: GpioDataRegs.GPATOGGLE.all |= ((unsigned long)1<<realpin); // toggle pin
 break;
}
  }

 if (pinNumber>200) // adc GPIO pins
 { realpin=pinNumber-200;
   switch (data)
     {
     case 0: GpioDataRegs.AIODAT.all    &=  ~((unsigned long)1<<realpin); //  set the pin low
     break;
     case 1: GpioDataRegs.AIODAT.all    |=  ((unsigned long)1<<realpin); //  set the pin high
     break;
     case 2: GpioDataRegs.AIOTOGGLE.all |=  ((unsigned long)1<<realpin); // toggle pin
     break;
  }

 }

 EDIS;
}


//**** abstract functions 
void InputPin(uint16_t pinNumber) // set for the 1st 32 GPIO pins and all ADCpins
{ SetPin(pinNumber,GPIO_Direction_Input);

}

void OutputPin(uint16_t pinNumber) // set for the 1st 32 GPIO pins  and all ADCpins
{ SetPin(pinNumber,GPIO_Direction_Output);
}


void PinHigh(uint16_t pinNumber)
{  PinData(pinNumber, GPIO_High);
}

void PinLow(uint16_t pinNumber)
{  PinData(pinNumber, GPIO_Low);}

void PinToggle(uint16_t pinNumber)
{  PinData(pinNumber, GPIO_Toggle);
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...