Вы находитесь на странице: 1из 6

/*

SoundSM

This state machine/service controls the adafruit using the shift register and
also performs low level shift register operations.
The other services would post to this function for sound effect with an
EventParam which will decide which sound pin to play.
Would also include the function to stop the sound

First Version: Nov 16, Friday

Data private to the module:

States:

*/

// the common headers for C99 types


#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

// the headers to access the GPIO subsystem


#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"

// the headers to access the TivaWare Library


#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"

#include "BITDEFS.H"

// Event & Services Framework


#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"

#include "SoundSM.h"

// readability defines

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

//********************For Serial DATA output from TIVA to shift


register******************
//defines for configuring the data pin
#define SOUND_SR_DATA_PORT_HI BIT0HI //Port A BIT5
#define SOUND_SR_DATA_PORT_LOW BIT0LO

//setting data pins


#define SOUND_SR_DATA_PIN_HI BIT5HI
#define SOUND_SR_DATA_PIN_LOW BIT5LO

#define SOUND_SR_DATA_SYSCTL_PRGPIO SYSCTL_PRGPIO_R0


#define SOUND_SR_DATA_GPIO_BASE GPIO_PORTA_BASE

//********************For SCLK output from TIVA to shift


register******************
//defines for the SCLK
#define SOUND_SR_SCLK_PORT_HI BIT0HI //Port A BIT6
#define SOUND_SR_SCLK_PORT_LOW BIT0LO

//setting data pins


#define SOUND_SR_SCLK_PIN_HI BIT6HI
#define SOUND_SR_SCLK_PIN_LOW BIT6LO

#define SOUND_SR_SCLK_SYSCTL_PRGPIO SYSCTL_PRGPIO_R0


#define SOUND_SR_SCLK_GPIO_BASE GPIO_PORTA_BASE

//********************For RCLK output from TIVA to shift


register******************

//defines for the SCLK


#define SOUND_SR_RCLK_PORT_HI BIT0HI //Port A BIT7
#define SOUND_SR_RCLK_PORT_LOW BIT0LO

//setting data pins


#define SOUND_SR_RCLK_PIN_HI BIT7HI
#define SOUND_SR_RCLK_PIN_LOW BIT7LO

#define SOUND_SR_RCLK_SYSCTL_PRGPIO SYSCTL_PRGPIO_R0


#define SOUND_SR_RCLK_GPIO_BASE GPIO_PORTA_BASE

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

/*DEFINES FROM SHIFT_REGISTER.H AND LCD_WRITE.H


#define DATA GPIO_PIN_0
#define SCLK GPIO_PIN_1
#define SCLK_HI BIT1HI
#define SCLK_LO BIT1LO

#define RCLK GPIO_PIN_2


#define RCLK_LO BIT2LO
#define RCLK_HI BIT2HI

*/
#define GET_MSB_IN_LSB(x) ((x & 0x80) >> 7)
#define ALL_BITS (0xff << 2)
#define ALL_HI (BIT0LO | BIT0HI)

//*********************************MODULE
DEFINES***********************************************
// an image of the last 8 bits written to the shift register
static uint8_t LocalRegisterImage = 0;
static uint8_t MyPriority;

//**********************Module
Functions***********************************************
static void SR_Sound_Write(uint8_t NewValue);

/****************************************************************************
Function
InitSoundSM

Parameters
uint8_t : the priorty of this service

Returns
true if an INIT event has been posted. Else False which is equivalent to an
error in posting the event

Description
A function to initialize the shift register
The data and sclk lines start from low and the RCLK starts from high

Notes

Author
****************************************************************************/

bool InitSoundSM(uint8_t Priority)


{
puts("\r \n Initializing SoundSM");

// set up the ports by enabling the peripheral clock, waiting for the
// peripheral to be ready and setting the direction
HWREG(SYSCTL_RCGCGPIO) |= SOUND_SR_DATA_PORT_HI; //Port enable for DATA
HWREG(SYSCTL_RCGCGPIO) |= SOUND_SR_SCLK_PORT_HI; //Port enable for SCLK
HWREG(SYSCTL_RCGCGPIO) |= SOUND_SR_RCLK_PORT_HI; //Port enable for RCLK

while ((HWREG(SYSCTL_PRGPIO) & SOUND_SR_DATA_SYSCTL_PRGPIO) !=


SOUND_SR_DATA_SYSCTL_PRGPIO)
{}

while ((HWREG(SYSCTL_PRGPIO) & SOUND_SR_SCLK_SYSCTL_PRGPIO) !=


SOUND_SR_SCLK_SYSCTL_PRGPIO)
{}

while ((HWREG(SYSCTL_PRGPIO) & SOUND_SR_RCLK_SYSCTL_PRGPIO) !=


SOUND_SR_RCLK_SYSCTL_PRGPIO)
{}

//enable DATA bit of ports for digital output


HWREG(SOUND_SR_DATA_GPIO_BASE + GPIO_O_DEN) |= SOUND_SR_DATA_PIN_HI; //
Setting it as Digital IO
HWREG(SOUND_SR_DATA_GPIO_BASE + GPIO_O_DIR) |= SOUND_SR_DATA_PIN_HI; //
Setting it as Digital Ouptut

//enable SCLK bit of ports for digital output


HWREG(SOUND_SR_SCLK_GPIO_BASE + GPIO_O_DEN) |= SOUND_SR_SCLK_PIN_HI; //
Setting it as Digital IO
HWREG(SOUND_SR_SCLK_GPIO_BASE + GPIO_O_DIR) |= SOUND_SR_SCLK_PIN_HI; //
Setting it as Digital Ouptut

//enable RCLK bit of ports for digital output


HWREG(SOUND_SR_RCLK_GPIO_BASE + GPIO_O_DEN) |= SOUND_SR_RCLK_PIN_HI; //
Setting it as Digital IO
HWREG(SOUND_SR_RCLK_GPIO_BASE + GPIO_O_DIR) |= SOUND_SR_RCLK_PIN_HI; //
Setting it as Digital Ouptut

//Not required -- Sample the button port pin and use it to initialize
LastButtonState
//LastPinState = HWREG(LEAF_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &
LEAF_SENSOR_PIN_HI;

// start with the data & sclk lines low and the RCLK line high
HWREG(SOUND_SR_DATA_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_DATA_PIN_LOW;
HWREG(SOUND_SR_SCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_SCLK_PIN_LOW;
HWREG(SOUND_SR_RCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) |=
SOUND_SR_RCLK_PIN_HI;

//bringing all pins of adafruit to high


SR_Sound_Write(ALL_HI);

puts("\r \n Sound Hardware initialization done");

ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;

//Just checking the sound service


//ThisEvent.EventType = SOUND_PLAY;
// ThisEvent.EventParam = 4;

if (ES_PostToService(MyPriority, ThisEvent) == true)


{
return true;
}
else
{
return false;
}
//return true; //saying that no
}

/****************************************************************************
Function
PostSoundSM

Parameters
ES_Event_t ThisEvent ,the event to post to the queue

Returns
bool false if the Enqueue operation failed, true otherwise

Description
Posts an event to this state machine's queue
Notes

Author
****************************************************************************/
bool PostSoundSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunSoundSM

Parameters
ES_Event_t : the event from its service queue to process in this State
Machine.
The EventParam will carry the index of the sound to be played.

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description

Notes

Author

****************************************************************************/
ES_Event_t RunSoundSM(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

uint8_t NewValue;

if (ThisEvent.EventType == SOUND_STOP)
{
puts("\r \n Event received to stop the sound");
SR_Sound_Write(ALL_HI);
}

if (ThisEvent.EventType == SOUND_PLAY)
{
//puts("\r \n Event received by the SoundSM");
switch (ThisEvent.EventParam)
{
case 0:
{
puts("\r \n Received in Sound Case 0");
SR_Sound_Write( ALL_HI);
NewValue = BIT0LO;
SR_Sound_Write( NewValue);
}
break;

case 1:
{
puts("\r \n Received in Sound Case 1");
SR_Sound_Write( ALL_HI);
NewValue = BIT1LO;
SR_Sound_Write( NewValue);
}
break;

case 2:
{
SR_Sound_Write( ALL_HI);
NewValue = BIT2LO;
SR_Sound_Write( NewValue);
}
break;

case 3:
{
SR_Sound_Write( ALL_HI);
NewValue = BIT3LO;
SR_Sound_Write( NewValue);
}
break;

case 4:
{
SR_Sound_Write( ALL_HI);
NewValue = BIT4LO;
SR_Sound_Write( NewValue);
}
break;
}
}
return ReturnEvent;
}

/****************************************************************************
Function
SR_GetCurrentRegister

Parameters

Returns
uint8_t

Description
This function gives the last provided value to the shift register

Notes
Author

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

uint8_t SR_Sound_GetCurrentRegister(void)
{
return LocalRegisterImage;
}

//******************************************HELPER
FUNCTIONS******************************************

// This function writes to the shift register given the uint8_t NewValue
static void SR_Sound_Write(uint8_t NewValue)
{
uint8_t BitCounter = 0;
LocalRegisterImage = NewValue; // save a local copy
//printf("\r \n NewValue %d", NewValue);
// lower the register clock
HWREG(SOUND_SR_RCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_RCLK_PIN_LOW;

// shift out the data while pulsing the serial clock


// Isolate the MSB of NewValue, put it into the LSB position and output to
port
// raise SCLK
// lower SCLK
// finish looping through bits in NewValue
while (BitCounter < 8)
{
uint8_t LeftMostBit = GET_MSB_IN_LSB(NewValue);
//printf("\r \n Bit %d at Count %d", LeftMostBit, BitCounter);

//pushing data to port B bit 0


if (LeftMostBit == 0)
{
HWREG(SOUND_SR_DATA_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_DATA_PIN_LOW;
}
else
{
HWREG(SOUND_SR_DATA_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) |=
SOUND_SR_DATA_PIN_HI;
}

//pulsing SCLK
HWREG(SOUND_SR_SCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) |=
SOUND_SR_SCLK_PIN_HI;
HWREG(SOUND_SR_SCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_SCLK_PIN_LOW;

NewValue = NewValue << 1;


BitCounter += 1;
}
// raise the register clock to latch the new data
HWREG(SOUND_SR_RCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) |=
SOUND_SR_RCLK_PIN_HI;
}

Вам также может понравиться