Thursday 8 September 2016

TM4C123G - All Peripheral Test Source Code




Source code can be downloaded here...

Google Drive Link  
                   https://drive.google.com/file/d/0B02sL8YIqsjCQ05pMlQxQjJtVUE/view?usp=sharing

Total Workspace link   
                https://drive.google.com/file/d/0B02sL8YIqsjCZ2IwNTJ6bkdvQkU/view?usp=sharing

You can comment me , in case of any difficulties ...

//*****************************************************************************

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "inc/hw_ints.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "inc/hw_i2c.h"
#include "driverlib/systick.h"
#include "driverlib/i2c.h"
#include "driverlib/pwm.h"
#include "driverlib/ssi.h"
#include "driverlib/can.h"
#include "driverlib/adc.h"

#include <stdio.h>
#include <stdlib.h>

/*************   Enable Tests  ************/
//#define LED_TEST
//#define KEY_TEST
//#define GPIO_TEST  //Pins  PD0 PD1 PD2
//#define TIMER_TEST  // Go to Startup_CCS.C & enable there
//#define UART_TEST  // Use putty to send & read
//#define I2C_TEST  // PB2 & PB3
//#define PWM_TEST     //PB6 & PB7
//#define SPI_TEST    //! - SSI0Clk - PA2 mcp2515 will attempt
//! - SSI0Fss - PA3
//! - SSI0Rx  - PA4
//! - SSI0Tx  - PA5
#define CAN_TEST     //! - CAN0RX - PB4
//! - CAN0TX - PB5
//#define ADC_TEST
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>Blinky (blinky)</h1>
//!
//! A very simple example that blinks the on-board LED using direct register
//! access.
//
//*****************************************************************************

//*****************************************************************************
//
// Blink the on-board LED.
//
//*****************************************************************************


void mydelay(void)
{
volatile uint32_t ui32Loop;
for(ui32Loop = 0; ui32Loop < 290000; ui32Loop++)
{
     }
}
#ifdef LED_TEST
void LED_Blink(void)
{

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_1);

   //
   // Loop forever.
   //
   while(1)
   {
       GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
       GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
       mydelay();
       GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
       GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
       mydelay();
       GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
       GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
       mydelay();

   }
}

#endif


#ifdef KEY_TEST
void Key_Test(void)
{
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3|GPIO_PIN_2); //led

GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);//sw1
GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);

while(1)
{
if (GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4))
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
}

}

}
#endif

#ifdef GPIO_TEST
void GPIO_Test()
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

        //
        // Check if the peripheral access is enabled.
        //
        while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOD))
        {
        }
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2);
    while(1)
    {
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2);
        mydelay();
        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2,0);
    mydelay();
    }
}
#endif

#ifdef TIMER_TEST
void Timer_Test()
{

   SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
   GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_1);

   // Enable the peripherals used by this example.
   SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
   SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
   // Enable processor interrupts.
   IntMasterEnable();
   // Configure the two 32-bit periodic timers.
   TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
   TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
   TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet());
   TimerLoadSet(TIMER1_BASE, TIMER_A, ROM_SysCtlClockGet() / 10);
   //TimerLoadSet(TIMER1_BASE, TIMER_A, 0x5fffff);
      // Setup the interrupts for the timer timeouts.
   IntEnable(INT_TIMER0A);
   IntEnable(INT_TIMER1A);
   TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
   TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    // Enable the timers.
   TimerEnable(TIMER0_BASE, TIMER_A);
   TimerEnable(TIMER1_BASE, TIMER_A);
   while(1)
      {
      }

}

// The interrupt handler for the first timer interrupt.
//
//*****************************************************************************
char bit1=0;
char bit2=0;
void
Timer0IntHandler(void)
{
    TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    if(bit1)
    {
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
    bit1=0;
    }
    else
    {
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
    bit1=1;
    }
    IntMasterEnable();
}

void
Timer1IntHandler(void)
{
    TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

    if(bit2)
      {
      GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
      bit2=0;
      }
      else
      {
      GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
      bit2=1;
      }
    IntMasterEnable();
}



#endif
#ifdef UART_TEST

static uint32_t g_ui32Base = 0;
static const uint32_t g_ui32UARTBase[3] =
{
    UART0_BASE, UART1_BASE, UART2_BASE
};
static const uint32_t g_ui32UARTPeriph[3] =
{
    SYSCTL_PERIPH_UART0, SYSCTL_PERIPH_UART1, SYSCTL_PERIPH_UART2
};

void
UARTStdioConfig(uint32_t ui32PortNum, uint32_t ui32Baud, uint32_t ui32SrcClock)
{
    //
    // Check to make sure the UART peripheral is present.
    //
    if(!SysCtlPeripheralPresent(g_ui32UARTPeriph[ui32PortNum]))
    {
        return;
    }

    //
    // Select the base address of the UART.
    //
    g_ui32Base = g_ui32UARTBase[ui32PortNum];

    //
    // Enable the UART peripheral for use.
    //
    SysCtlPeripheralEnable(g_ui32UARTPeriph[ui32PortNum]);

    //
    // Configure the UART for 115200, n, 8, 1
    //
    UARTConfigSetExpClk(g_ui32Base, ui32SrcClock, ui32Baud,
                            (UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE |
                             UART_CONFIG_WLEN_8));


    // Enable the UART operation.
    //
    UARTEnable(g_ui32Base);
}

void
ConfigureUART(void)
{
    //
    // Enable the GPIO Peripheral used by the UART.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Enable UART0
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Configure GPIO Pins for UART mode.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Use the internal 16MHz oscillator as the UART clock source.
    //
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, 16000000);
}

void UART_Test(void)
{
ConfigureUART();
 //UARTprintf("Timers example\n");
UARTCharPut(g_ui32Base, 'V');
UARTCharPut(g_ui32Base, 'A');
UARTCharPut(g_ui32Base, 'R');
UARTCharPut(g_ui32Base, 'G');
UARTCharPut(g_ui32Base, 'H');
UARTCharPut(g_ui32Base, 'E');
UARTCharPut(g_ui32Base, 'S');
UARTCharPut(g_ui32Base, 'E');
while(1)
{
UARTCharPut(g_ui32Base,UARTCharGet(g_ui32Base));// echo char
}
}

#endif

#ifdef I2C_TEST

#define NUM_I2C_DATA 1
#define SLAVE_ADDRESS 0x3C

void I2C_Test(void )
{

    uint32_t pui32DataTx[NUM_I2C_DATA];
    uint32_t ui32Index;
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    GPIOPinConfigure(GPIO_PB2_I2C0SCL);
    GPIOPinConfigure(GPIO_PB3_I2C0SDA);
    GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
    GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); //Configure PUR for PB2
    GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD); //Configure OD for PB3
    GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_2|GPIO_PIN_3, GPIO_DIR_MODE_HW);      //Set direction by HW for PB2 and PB3

    I2CMasterInitExpClk(I2C0_BASE,SysCtlClockGet(),true);     //Set System clock and normal (100 kbps) transfer rate for I2C_0

    I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
    pui32DataTx[0] = 'I';
    pui32DataTx[1] = '2';
    pui32DataTx[2] = 'C';
   // ui32Index=0;
   for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
    {
        I2CMasterDataPut(I2C0_BASE, pui32DataTx[ui32Index]);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
    }
    while(1)
    {
    ;
    }

}
#endif

#ifdef PWM_TEST

void PWM_Test(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                  SYSCTL_XTAL_16MHZ);
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB6_M0PWM0);
GPIOPinConfigure(GPIO_PB7_M0PWM1);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7);
PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN |
                    PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 64000);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0,
                    PWMGenPeriodGet(PWM0_BASE, PWM_GEN_0) / 4);

PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1,
                    1000);
// PWMDeadBandEnable(PWM0_BASE, PWM_GEN_0, 160, 160);
PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT | PWM_OUT_0_BIT, true);
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
while(1)
{
;
}

}
#endif

#ifdef SPI_TEST

#define NUM_SSI_DATA            3


void SPI_Test(void)
{
uint32_t pui32DataTx[NUM_SSI_DATA];
   uint32_t pui32DataRx[NUM_SSI_DATA];
   uint32_t ui32Index;
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                      SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX);
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
                   GPIO_PIN_2);

/*  GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PA3_SSI0FSS);
   GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PA4_SSI0RX, GPIO_STRENGTH_4MA,
                        GPIO_PIN_TYPE_STD_WPU);
   GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PA2_SSI0CLK | GPIO_PA5_SSI0TX | GPIO_PA3_SSI0FSS,
                        GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD);

*/
SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_TI,
                       SSI_MODE_MASTER, 1000000, 8);
SSIEnable(SSI0_BASE);
while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]))
    {
    }
pui32DataTx[0] = 't';
pui32DataTx[1] = 'i';
pui32DataTx[2] = '!';
for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
   {
       SSIDataPut(SSI0_BASE, pui32DataTx[ui32Index]);
   }
//SSIDataGet(SSI0_BASE, &pui32DataRx[ui32Index]);
while(1)
{
;
}
}
#endif

#ifdef CAN_TEST


void
CANIntHandler(void)
{
  //  uint32_t ui32Status;
   // ui32Status = CANIntStatus(CAN0_BASE, CAN_INT_STS_CAUSE);

}
void CAN_Test(void)
{
tCANMsgObject sCANMessage;
  uint32_t ui32MsgData;
  uint8_t *pui8MsgData;

  pui8MsgData = (uint8_t *)&ui32MsgData;
  SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                    SYSCTL_XTAL_16MHZ);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
  GPIOPinConfigure(GPIO_PB4_CAN0RX);
  GPIOPinConfigure(GPIO_PB5_CAN0TX);
  GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
  CANInit(CAN0_BASE);
  CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);
  CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);
  IntEnable(INT_CAN0);
  CANEnable(CAN0_BASE);
  ui32MsgData = 7;
  sCANMessage.ui32MsgID = 0x1f;
  sCANMessage.ui32MsgIDMask = 0;
  sCANMessage.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
  sCANMessage.ui32MsgLen = sizeof(pui8MsgData);
  sCANMessage.pui8MsgData = pui8MsgData;


  while(1)
  {

      CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_TX);
      ui32MsgData++;
  }

}
#endif

#ifdef ADC_TEST

void ADC_Test()
{
uint32_t pui32ADC0Value[1];
SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                  SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_7);
ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE |
                                ADC_CTL_END);
ADCSequenceEnable(ADC0_BASE, 3);
ADCIntClear(ADC0_BASE, 3);
while(1)
{
     ADCProcessorTrigger(ADC0_BASE, 3);
     while(!ADCIntStatus(ADC0_BASE, 3, false))
     {
     }
     ADCIntClear(ADC0_BASE, 3);
     ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
     SysCtlDelay(SysCtlClockGet() / 12);
}
}
#endif


int
main(void)
{

    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //
    // Check if the peripheral access is enabled.
    //
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF))
    {
    }

#ifdef LED_TEST
    LED_Blink();
#endif

#ifdef KEY_TEST
    Key_Test();
#endif

#ifdef GPIO_TEST  //PD0 ,PD1,PD2
    GPIO_Test();
#endif

#ifdef TIMER_TEST
    Timer_Test();
#endif

#ifdef UART_TEST
    UART_Test();
#endif

#ifdef I2C_TEST
     I2C_Test();
#endif

#ifdef PWM_TEST
      PWM_Test();
#endif

#ifdef SPI_TEST
     SPI_Test();
#endif

#ifdef CAN_TEST
      CAN_Test();
#endif

#ifdef ADC_TEST
      ADC_Test();
#endif


}


For interrupt add the routine in startup_ccs.c file ,,

Arduino -Peripheral Test Source code for SPI, I2C,GPIO, PWM,ADC


This file can be downloaded here ...

https://drive.google.com/drive/folders/0B02sL8YIqsjCeE9Db0RFQlFWdEk?usp=sharing

Enable macros to test ...

#include <Wire.h>
#include <SPI.h>

//#define LED_TEST
//#define ADC_TEST  // Pin A0 VCC, GND
//#define GPIO_TEST
//#define PWM_TEST
//#define I2C_TEST //A4 (SDA) and A5 (SCL)
#define SPI_TEST //SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK)


#ifdef SPI_TEST
#define spi_readwrite SPI.transfer
#define spi_read() spi_readwrite(0x00)

int  mcp2515_readRegister(const int  address)                                                                  
{
    int  ret;
int MCP_READ=45;
//    MCP2515_SELECT();
    spi_readwrite(MCP_READ);
    spi_readwrite(address);
    ret = spi_read();
  //  MCP2515_UNSELECT();

    return ret;
}

#endif


#ifdef I2C_TEST
#define I2C_ADDRESS 0x50
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
  delay(5);
}

byte readEEPROM(int deviceaddress, unsigned int eeaddress )
{
  byte rdata = 0xFF;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress,1);
  if (Wire.available()) rdata = Wire.read();
  return rdata;
}
#endif


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
#ifdef LED_TEST
  pinMode(13, OUTPUT);
#endif

#ifdef GPIO_TEST
  pinMode(2, OUTPUT); //D2
  pinMode(12, OUTPUT);//D12
  pinMode(13, OUTPUT);//D13
#endif
 SPI.begin();

}

void loop() {
  // put your main code here, to run repeatedly:
#ifdef LED_TEST
  digitalWrite(13, HIGH);
  delay(1000);          
  digitalWrite(13, LOW);  
  delay(1000);            
#endif
#ifdef ADC_TEST
 int sensorValue = analogRead(A0);
 Serial.println("value");
 Serial.println(sensorValue);
 delay(1);  
#endif
#ifdef GPIO_TEST
  digitalWrite(2, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  delay(1000);          
  digitalWrite(2, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  delay(1000);            
#endif
#ifdef PWM_TEST
  analogWrite(9, 235);//pin D9
#endif
#ifdef  I2C_TEST
writeEEPROM(I2C_ADDRESS, 45, 123);
#endif
#ifdef SPI_TEST
{
mcp2515_readRegister(33);
}
#endif
}