/****************************************************************************
Module
main.c
Description
starter main() function for Events and Services Framework applications
Notes
*****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_Port.h"
#include "PWM_PIC32.h"
#include "PIC32_AD_Lib.h"
#include "PIC32_SPI_HAL.h"
#include "DM_Display.h"
#define AVATAR_MIN_PULSE 1250
#define AVATAR_MAX_PULSE 5000
#define TOKEN_MIN_PULSE 1400
#define OBSTACLE_NEUTRAL_PULSE 3750
#define AVATAR_SERVO_CH 1
#define TOKEN_SERVO_CH 2
#define OBSTACLE_SERVO_CH 3
void main(void)
{
ES_Return_t ErrorType = Success;
_HW_PIC32Init(); // basic PIC hardware init
// Your hardware initialization function calls go here
// set up PWM
TRISBbits.TRISB4 = 0;
TRISBbits.TRISB5 = 0;
PWMSetup_BasicConfig(3);
PWMSetup_AssignChannelToTimer(AVATAR_SERVO_CH, _Timer2_);
PWMSetup_AssignChannelToTimer(TOKEN_SERVO_CH, _Timer2_);
PWMSetup_AssignChannelToTimer(OBSTACLE_SERVO_CH, _Timer2_);
PWMSetup_SetFreqOnTimer(50, _Timer2_);
PWMSetup_MapChannelToOutputPin(AVATAR_SERVO_CH, PWM_RPB4);
PWMSetup_MapChannelToOutputPin(TOKEN_SERVO_CH, PWM_RPB5);
PWMSetup_MapChannelToOutputPin(OBSTACLE_SERVO_CH, PWM_RPB9);
// set servos to initial positions
PWMOperate_SetPulseWidthOnChannel(AVATAR_MAX_PULSE, AVATAR_SERVO_CH);
PWMOperate_SetPulseWidthOnChannel(TOKEN_MIN_PULSE, TOKEN_SERVO_CH);
PWMOperate_SetPulseWidthOnChannel(OBSTACLE_NEUTRAL_PULSE, OBSTACLE_SERVO_CH);
// initialize input port
TRISBbits.TRISB13 = 1; // start button digital input
TRISAbits.TRISA0 = 1; // mic analog input
TRISAbits.TRISA1 = 1; // potentiometer analog input
TRISBbits.TRISB2 = 1; // distance sensor analog input
ANSELBbits.ANSB13 = 0;
ANSELAbits.ANSA0 = 1;
ANSELAbits.ANSA1 = 1;
ANSELBbits.ANSB2 = 1;
ADC_ConfigAutoScan(BIT0HI | BIT1HI | BIT4HI);
// initialize SPI
SPI_Module_t mySPI = SPI_SPI1;
SPI_SamplePhase_t myPhase = SPI_SMP_MID;
SPI_PinMap_t SSPin = SPI_RPB15;
SPI_PinMap_t SDOPin = SPI_RPB11;
SPI_Clock_t clkState = SPI_CLK_HI;
SPI_ActiveEdge_t clkEdge = SPI_SECOND_EDGE;
SPI_XferWidth_t dataWidth = SPI_16BIT;
uint32_t period = 10000; // SPI period should be 10 us for 100 kHz bit clock
// configure SPI1
SPISetup_BasicConfig(mySPI);
SPISetup_SetLeader(mySPI, myPhase);
SPISetup_SetBitTime(mySPI, period);
SPISetup_MapSSOutput(mySPI, SSPin);
SPISetup_MapSDOutput(mySPI, SDOPin);
SPISetup_SetClockIdleState(mySPI, clkState);
SPISetup_SetActiveEdge(mySPI, clkEdge);
SPISetup_SetXferWidth(mySPI, dataWidth);
SPISetEnhancedBuffer(mySPI, true);
// enable SPI
SPISetup_EnableSPI(mySPI);
while (false == DM_TakeInitDisplayStep()) {
// take steps until the function returns true
}
// now initialize the Events and Services Framework and start it running
ErrorType = ES_Initialize(ES_Timer_RATE_1mS);
if (ErrorType == Success)
{
ErrorType = ES_Run();
}
//if we got to here, there was an error
switch (ErrorType)
{
case FailedPost:
{
ErrorType = 0; // a hack to get a readable assert message
assert( FailedPost == ErrorType );
//DB_printf("Failed on attempt to Post\n");
}
break;
case FailedPointer:
{
ErrorType = 0; // a hack to get a readable assert message
assert( FailedPointer == ErrorType );
// DB_printf("Failed on NULL pointer\n");
}
break;
case FailedInit:
{
ErrorType = 0; // a hack to get a readable assert message
assert( FailedInit == ErrorType );
// DB_printf("Failed Initialization\n");
}
break;
default:
{
ErrorType = 0; // a hack to get a readable assert message
assert( FailedOther == ErrorType );
// DB_printf("Other Failure\n");
}
break;
}
for ( ; ;)
{
;
}
}