Skip to content

Commit

Permalink
Allow for arbitrary GPIO control with FX3.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed Apr 21, 2021
1 parent fd86453 commit 4a9cc4a
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 75 deletions.
Binary file modified testproject-synchronous/USBIsoSource/Release/USBIsoSource.img
Binary file not shown.
91 changes: 52 additions & 39 deletions testproject-synchronous/USBIsoSource/cyfxisosrc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
#include "cyu3i2c.h"
#include <cyu3gpif.h> //XXX CNL#include <cyu3pib.h>#include <pib_regs.h>
#include <cyu3gpio.h>
#include <cyu3pib.h>

#include "fast_gpif2.cydsn/cyfxgpif2config.h"
//#include "fast_gpif2.h"
//#include "cyfxgpif2config.h"

int is_running;
CyU3PThread isoSrcAppThread; /* ISO loop application thread structure */

void CyFxIsoSrcApplnInit(void);

#ifdef DMAMULTI
CyU3PDmaMultiChannel glChHandleIsoSrc; /* DMA MANUAL_OUT channel handle */
#else
Expand Down Expand Up @@ -169,6 +169,7 @@ CyU3PDmaCBInput_t *input) /* Callback status. */
* when a SET_CONF event is received from the USB host. The endpoints
* are configured and the DMA pipe is setup in this function. */
void CyFxIsoSrcApplnStart(void) {
if( glIsApplnActive ) return;
uint16_t size = 0;
CyU3PEpConfig_t epCfg;
//CyU3PDmaBuffer_t buf_p;
Expand Down Expand Up @@ -302,6 +303,17 @@ void CyFxIsoSrcApplnStart(void) {
CyFxAppErrorHandler(apiRetStatus);
}

/* Load the GPIF configuration for our project.. This loads the state machine for interfacing with SRAM */
apiRetStatus = CyU3PGpifLoad(&CyFxGpifConfig);
if (apiRetStatus != CY_U3P_SUCCESS) {
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY,
"CyU3PGpifLoad failed, Error Code=%d\r\n", apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "CyU3PGpifLoad OK\r\n" );
CyU3PReturnStatus_t stat = CyU3PGpifSMStart(RUN, ALPHA_RUN);
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "CyU3PGpifSMStart = %d\r\n", stat );

/* Update the flag so that the application thread is notified of this. */
glIsApplnActive = CyTrue;
}
Expand All @@ -313,10 +325,12 @@ void CyFxIsoSrcApplnStop(void) {
CyU3PEpConfig_t epCfg;
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;

if( !glIsApplnActive ) return;

CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "De-initializing...\r\n");

//Stop the GPIF bus.
CyU3PGpifDisable(0);
CyU3PGpifDisable( CyTrue );

#ifdef DMAMULTI
CyU3PDmaMultiChannelReset(&glChHandleIsoSrc);
Expand Down Expand Up @@ -407,7 +421,6 @@ uint32_t setupdat1 /* SETUP Data 1 */
CyU3PUsbSendEP0Data(16, (uint8_t*) sendback); //Sends back "hello"
if (KeepDataAlive == 0) {
CyFxIsoSrcApplnStart();
CyU3PGpifSMStart(START, ALPHA_START);
}
//CAN CONFIRM: VALUE IN THIS DOES NOT DETERMINE WHETHER OR NOT THE OUTPUT IS GOOD.
KeepDataAlive = 1200 * 2 * 50;
Expand Down Expand Up @@ -487,12 +500,18 @@ CyBool_t CyFxIsoSrcApplnLPMRqtCB(CyU3PUsbLinkPowerMode link_mode) {
* We don't expect errors to happen, and only print a message in case one is reported.
*/
static void PibEventCallback(CyU3PPibIntrType cbType, uint16_t cbArg) {
// CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "%d %d\n", cbType, cbArg );
if( cbType == CYU3P_PIB_INTR_DLL_UPDATE )
{
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "CYU3P_PIB_INTR_DLL_UPDATE\n", cbType, cbArg );
// This happens. It's the last thing that happens in synchronous mode.
}
else
if (cbType == CYU3P_PIB_INTR_ERROR) {
switch (CYU3P_GET_PIB_ERROR_TYPE(cbArg)) {
case CYU3P_PIB_ERR_THR2_WR_OVERRUN:
case CYU3P_PIB_ERR_THR1_WR_OVERRUN:
case CYU3P_PIB_ERR_THR0_WR_OVERRUN:
//CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "%d\r\n", KeepDataAlive);
DataOverrunErrors++;
LastOverrunMark = KeepDataAlive;
if (KeepDataAlive) {
Expand All @@ -503,6 +522,11 @@ static void PibEventCallback(CyU3PPibIntrType cbType, uint16_t cbArg) {
}
}
break;
case CYU3P_PIB_ERR_THR1_SCK_INACTIVE:
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "Thread Inactive\n" );
CyFxIsoSrcApplnStop();
KeepDataAlive = 0;
break;
case CYU3P_PIB_ERR_THR3_WR_OVERRUN:
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY,
"CYU3P_PIB_ERR_THR3_WR_OVERRUN\r\n");
Expand Down Expand Up @@ -531,25 +555,32 @@ static void PibEventCallback(CyU3PPibIntrType cbType, uint16_t cbArg) {
}
}

/* This function initializes the USB Module, sets the enumeration descriptors.
* This function does not start the ISO streaming and this is done only when
* SET_CONF event is received. */
void CyFxIsoSrcApplnInit(void) {
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
/* Entry function for the IsoSrcAppThread. */
void IsoSrcAppThread_Entry(uint32_t input) {
uint32_t evMask = CYFX_ISOAPP_CTRL_TASK;
uint32_t evStat;
CyU3PReturnStatus_t stat;

//XXX CNL Setup GPIF
/* Initialize the debug module */
CyFxIsoSrcApplnDebugInit();

CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "IsoSrcAppThread_Entry(...)\r\n");

/* Initialize the ISO loop application */
/* This function initializes the USB Module, sets the enumeration descriptors.
* This function does not start the ISO streaming and this is done only when
* SET_CONF event is received. */
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;

CyU3PPibClock_t pibClock;
CyU3PGpioClock_t gpioClock;
//CyU3PGpioSimpleConfig_t gpioConfig;
//CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;

CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "In IsoSrcAppThread_Entry(...)\r\n");

pibClock.clkDiv = 4; //~400 MHz / 4. or 400 / 8 --- or 384 / 4 or 384 / 8
pibClock.clkSrc = CY_U3P_SYS_CLK;
pibClock.isHalfDiv = CyFalse; //Adds 0.5 to divisor
pibClock.isDllEnable = CyTrue; //For async or master-mode
pibClock.isDllEnable = CyFalse; //For async or master-mode
apiRetStatus = CyU3PPibInit(CyTrue, &pibClock);
if (apiRetStatus != CY_U3P_SUCCESS) {
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY,
Expand All @@ -558,7 +589,6 @@ void CyFxIsoSrcApplnInit(void) {
CyFxAppErrorHandler(apiRetStatus);
}


/* Initialize the GPIO module. This is used to update the indicator LED. */
gpioClock.fastClkDiv = 2;
gpioClock.slowClkDiv = 0;
Expand All @@ -569,8 +599,7 @@ void CyFxIsoSrcApplnInit(void) {
apiRetStatus = CyU3PGpioInit(&gpioClock, 0);
if (apiRetStatus != 0) {
/* Error Handling */
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY,
"CyU3PGpioInit failed, error code = %d\r\n", apiRetStatus);
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "CyU3PGpioInit failed, error code = %d\r\n", apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}

Expand Down Expand Up @@ -717,14 +746,11 @@ void CyFxIsoSrcApplnInit(void) {
CyFxAppErrorHandler(apiRetStatus);
}

CyU3PDebugPrint( CY_FX_DEBUG_PRIORITY, "Loading\r\n");

//CyU3PGpifSMStart(START, ALPHA_START);
CyU3PDebugPrint( CY_FX_DEBUG_PRIORITY, "Registering Callback\r\n");

/* Load the GPIF configuration for our project.. This loads the state machine for interfacing with SRAM */
apiRetStatus = CyU3PGpifLoad(&CyFxGpifConfig);
if (apiRetStatus != CY_U3P_SUCCESS) {
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY,
"CyU3PGpifLoad failed, Error Code=%d\r\n", apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}

/* callback to see if there is any overflow of data on the GPIF II side*/
//XXX Why is CYU3P_PIB_ERR_THR0_WR_OVERRUN getting called incessently?
Expand All @@ -734,28 +760,15 @@ void CyFxIsoSrcApplnInit(void) {


CyU3PDebugPrint( CY_FX_DEBUG_PRIORITY, "CyFxIsoSrcApplnInit Complete\r\n");
}

/* Entry function for the IsoSrcAppThread. */
void IsoSrcAppThread_Entry(uint32_t input) {
uint32_t evMask = CYFX_ISOAPP_CTRL_TASK;
uint32_t evStat;
CyU3PReturnStatus_t stat;

/* Initialize the debug module */
CyFxIsoSrcApplnDebugInit();

CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "IsoSrcAppThread_Entry(...)\r\n");

/* Initialize the ISO loop application */
CyFxIsoSrcApplnInit();

CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "CyFxIsoSrcApplnInit(...) done\r\n");

/* Wait for any vendor specific requests to arrive, and then handle them. */
for (;;) {
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "STATIN\n" );
stat = CyU3PEventGet(&glAppEvent, evMask, CYU3P_EVENT_OR_CLEAR, &evStat,
CYU3P_WAIT_FOREVER);
CyU3PDebugPrint(CY_FX_DEBUG_PRIORITY, "STAT: %d\n", stat );
if (stat == CY_U3P_SUCCESS) {
if (evStat & CYFX_ISOAPP_CTRL_TASK) {
uint8_t bRequest, bReqType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Project Name: fast_gpif2.cyfx
* Time : 04/12/2021 22:02:29
* Time : 04/15/2021 20:09:04
* Device Type: FX3
* Project Type: GPIF2
*
Expand All @@ -21,21 +21,24 @@
/* Summary
Number of states in the state machine
*/
#define CY_NUMBER_OF_STATES 4
#define CY_NUMBER_OF_STATES 6

/* Summary
Mapping of user defined state names to state indices
*/
#define START 0
#define STATE0 2
#define STATE3 3
#define STATE4 1
#define STATE4 4
#define RUN 5
#define DO_NOTHING 1


/* Summary
Initial value of early outputs from the state machine.
*/
#define ALPHA_START 0x0
#define ALPHA_START 0x4
#define ALPHA_RUN 0x0


/* Summary
Expand All @@ -52,23 +55,25 @@ uint16_t CyFxGpifTransition[] = {
waveform table.
*/
CyU3PGpifWaveData CyFxGpifWavedata[] = {
{{0x2E739C01,0x00001500,0x800000A0},{0x00000000,0x00000000,0x00000000}},
{{0x00000001,0x00000000,0x80000800},{0x00000000,0x00000000,0x00000000}},
{{0x00000000,0x00000000,0x00000000},{0x00000000,0x00000000,0x00000000}},
{{0x1E739303,0x24001500,0x80000010},{0x00000000,0x00000000,0x00000000}},
{{0x1E739402,0x20002900,0x80000040},{0x00000000,0x00000000,0x00000000}},
{{0x1E739303,0x24001500,0x80000010},{0x00000000,0x00000000,0x00000000}}
{{0x2E739C04,0x00001500,0x800000A0},{0x00000000,0x00000000,0x00000000}}
};

/* Summary
Table that maps state indices to the descriptor table indices.
*/
uint8_t CyFxGpifWavedataPosition[] = {
0,1,2,1
0,1,2,3,3,4
};

/* Summary
GPIF II configuration register values.
*/
uint32_t CyFxGpifRegValue[] = {
0x80008300, /* CY_U3P_PIB_GPIF_CONFIG */
0x80008308, /* CY_U3P_PIB_GPIF_CONFIG */
0x00000003, /* CY_U3P_PIB_GPIF_BUS_CONFIG */
0x00000000, /* CY_U3P_PIB_GPIF_BUS_CONFIG2 */
0x00000046, /* CY_U3P_PIB_GPIF_AD_CONFIG */
Expand Down Expand Up @@ -138,7 +143,7 @@ uint32_t CyFxGpifRegValue[] = {
0x00000000, /* CY_U3P_PIB_GPIF_LAMBDA_STAT */
0x00000000, /* CY_U3P_PIB_GPIF_ALPHA_STAT */
0x00000000, /* CY_U3P_PIB_GPIF_BETA_STAT */
0x00000000, /* CY_U3P_PIB_GPIF_WAVEFORM_CTRL_STAT */
0x00040000, /* CY_U3P_PIB_GPIF_WAVEFORM_CTRL_STAT */
0x00000000, /* CY_U3P_PIB_GPIF_WAVEFORM_SWITCH */
0x00000000, /* CY_U3P_PIB_GPIF_WAVEFORM_SWITCH_TIMEOUT */
0x00000000, /* CY_U3P_PIB_GPIF_CRC_CONFIG */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<InterfaceType>Slave</InterfaceType>
<CommunicationType>Synchronous</CommunicationType>
<ClockSource>External</ClockSource>
<ClockEdge>Positive</ClockEdge>
<ClockEdge>Negative</ClockEdge>
<Endianness>LittleEndian</Endianness>
<DataBusWidth>Bit8</DataBusWidth>
<AddressBuswidth>0</AddressBuswidth>
Expand Down Expand Up @@ -121,9 +121,26 @@
<CounterInterrupt>Mask</CounterInterrupt>
</Action>
</State>
<State ElementId="STARTSTATE2" StateType="StartState">
<DisplayName>RUN</DisplayName>
<RepeatUntillNextTransition>True</RepeatUntillNextTransition>
<RepeatCount>0</RepeatCount>
</State>
<State ElementId="STATE2" StateType="NormalState">
<DisplayName>DO_NOTHING</DisplayName>
<RepeatUntillNextTransition>True</RepeatUntillNextTransition>
<RepeatCount>0</RepeatCount>
<Action ElementId="IN_DATA0" ActionType="IN_DATA">
<DataSourceSink>Register</DataSourceSink>
<ThreadNumber>Thread0</ThreadNumber>
<SampleData>True</SampleData>
<WriteDataIntoDataSink>False</WriteDataIntoDataSink>
</Action>
</State>
<Transition ElementId="TRANSITION2" SourceState="STATE3" DestinationState="STATE0" Equation="ADDR_CNT_HIT" />
<Transition ElementId="TRANSITION3" SourceState="STATE0" DestinationState="STATE3" Equation="DATA_CNT_HIT" />
<Transition ElementId="TRANSITION0" SourceState="STARTSTATE0" DestinationState="STATE4" Equation="LOGIC_ONE" />
<Transition ElementId="TRANSITION5" SourceState="STARTSTATE2" DestinationState="STATE4" Equation="LOGIC_ONE" />
<Transition ElementId="TRANSITION1" SourceState="STATE4" DestinationState="STATE0" Equation="LOGIC_ONE" />
<Transition ElementId="TRANSITION4" SourceState="STARTSTATE0" DestinationState="STATE2" Equation="LOGIC_ONE" />
</StateMachine>
</GPIFIIModel>
Loading

0 comments on commit 4a9cc4a

Please sign in to comment.