void IO_SetBit(u8 bit, u8 value) { if(value) XGpio_DiscreteSet(&gpio, 1, (1u<<bit)); else XGpio_DiscreteClear(&gpio, 1, (1u<<bit)); }
/** * * This function does a minimal test on the GPIO device configured as OUTPUT * and driver as a example. * * * @param DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from * xparameters.h * @param GpioWidth is the width of the GPIO * * @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful * * @note None * ****************************************************************************/ void GpioOutputExample(Xuint16 DeviceId, Xuint32 GpioWidth) { volatile int Delay; Xuint32 LedBit; Xuint32 LedLoop; XStatus Status; int numTimes = 1; /* * Initialize the GPIO driver so that it's ready to use, * specify the device ID that is generated in xparameters.h */ Status = XGpio_Initialize(&GpioOutput, DeviceId); if (Status != XST_SUCCESS) { print("Gpio Instance Didn't Initialize!\r\n"); } /* * Set the direction for all signals to be outputs */ XGpio_SetDataDirection(&GpioOutput, 1, 0x0); /* * Set the GPIO outputs to low */ XGpio_DiscreteWrite(&GpioOutput, 1, 0x0); while (numTimes > 0) { for (LedBit = 0x0; LedBit < GpioWidth; LedBit++) { for (LedLoop = 0; LedLoop < 1; LedLoop++) { /* * Set the GPIO Output to High */ XGpio_DiscreteWrite(&GpioOutput, 1, 1 << LedBit); /* * Wait a small amount of time so the LED is visible */ for (Delay = 0; Delay < (500000/2); Delay++) { // Dummy read; to ensure the delay has no Cache effect XGpio_DiscreteRead(&GpioOutput, 1); } /* * Clear the GPIO Output */ XGpio_DiscreteClear(&GpioOutput, 1, 1 << LedBit); } } numTimes--; } }
/****************************************************************************** * This function displays the pixel buffer, pointed to with psScreenBuf, onto * the OLED display. * * @param psSpi is the pointer to the SPI driver structure. * @param psGpio is the pointer to the GPIO driver structure. * @param pchScreenBuf is the pointer to the pixel buffer to display on the * OLED screen. * * @return XST_SUCCESS - Everything went well * XST_FAILURE - Failure *****************************************************************************/ XStatus fnOledPixelToDisplay(XSpi *psSpi, XGpio *psGpio, const sPixmap_t *psScreenBuf) { XStatus Status; u32 iPg; u8 rgbWrBuf[10]; u8 u8LowerStartColumn = 0x00; u8 u8UpperStartColumn = 0x10; // Going through each character line (4) for(iPg = 0; iPg < OLED_MAX_PG_CNT; iPg++) { rgbWrBuf[0] = OLED_SET_PG_ADDR_CMD; rgbWrBuf[1] = iPg; rgbWrBuf[2] = u8LowerStartColumn; rgbWrBuf[3] = u8UpperStartColumn; XGpio_DiscreteClear(psGpio, 1, OLED_DC_MASK); u8TransferInProg = TRUE; Status = XSpi_Transfer(psSpi, rgbWrBuf, NULL, 4); if(Status != XST_SUCCESS) { return Status; } while(u8TransferInProg); XGpio_DiscreteSet(psGpio, 1, OLED_DC_MASK); // Writing the line to the OLED u8TransferInProg = TRUE; Status = XSpi_Transfer(psSpi, (u8 *)(psScreenBuf->rgbPixmap + (iPg * OLED_CONTROLLER_PG_SZ)), NULL, OLED_CONTROLLER_PG_SZ); if(Status != XST_SUCCESS) { return Status; } while(u8TransferInProg); } return XST_SUCCESS; }
void GpioHandler(void *InstancePtr) { /* Game System Interface (16 bits) #define GAME_TRUCK_IS_BASE_M 0x0001 #define GAME_HAVE_FLAG_M 0x0002 #define GAME_TRUCK_ALIVE_M 0x0004 #define GAME_TRUCK_REGISTERED_M 0x0008 #define GAME_TEAM_NUM_1_M 0x0010 #define GAME_TEAM_NUM_0_M 0x0020 #define GAME_STATE_1_M 0x0040 //stop, go, pause, win #define GAME_STATE_0_M 0x0080 #define GAME_KILL_SHOT 0x0100 #define GAME_PASS_SHOT 0x0200 #define GAME_REVIVE_SHOT 0x0400 #define GAME_HIT_ACK 0x0800 #define GAME_MISS_ACK 0x1000 #define HELIOS_ENABLE_GYRO 0x2000 #define GAME_NOT_IN_PLAY 0x4000 #define GAME_WAIT_TO_SHOOT 0x8000 */ XGpio *GpioPtr = (XGpio *)InstancePtr; u32 gameSystem; //u32 gameSystemChanged = 0; static u32 gameSystemPrevious = 0; /* * Disable the interrupt */ XGpio_InterruptDisable(GpioPtr, XGPIO_IR_CH2_MASK); /* * There should not be any other interrupts occuring other than the * the button changes */ if ((XGpio_InterruptGetStatus(GpioPtr) & XGPIO_IR_CH2_MASK) != XGPIO_IR_CH2_MASK) { return; } /* * Read state of push buttons and determine which ones changed * states from the previous interrupt. Save a copy of the buttons * for the next interrupt */ gameSystem = XGpio_DiscreteRead(GpioPtr, GAME_SYSTEM_GPIO_CHANNEL); //gameSystemChanged = gameSystem ^ gameSystemPrevious; if (((~gameSystemPrevious) & GAME_WAIT_TO_SHOOT) && (gameSystem & GAME_WAIT_TO_SHOOT)) { // Wait to shoot went from low to high XGpio_DiscreteClear(&Gpio, GAME_SYSTEM_GPIO_CHANNEL, CurrentShotType); CurrentShotType = 0; } /* Clear the interrupt such that it is no longer pending in the GPIO */ (void)XGpio_InterruptClear(GpioPtr, XGPIO_IR_CH2_MASK); /* * Enable the interrupt */ gameSystemPrevious = gameSystem; XGpio_InterruptEnable(GpioPtr, XGPIO_IR_CH2_MASK); }
/** * * This function does a minimal test on the GPIO device configured as OUTPUT * and driver as a example. * * * @param DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from * xparameters.h * @param GpioWidth is the width of the GPIO * * @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful * * @note None * ****************************************************************************/ int GpioOutputExample(u16 DeviceId, u32 GpioWidth) { volatile int Delay; u32 LedBit; u32 LedLoop; int Status; /* * Initialize the GPIO driver so that it's ready to use, * specify the device ID that is generated in xparameters.h */ Status = XGpio_Initialize(&GpioOutput, DeviceId); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Set the direction for all signals to be outputs */ XGpio_SetDataDirection(&GpioOutput, LED_CHANNEL, 0x0); /* * Set the GPIO outputs to low */ XGpio_DiscreteWrite(&GpioOutput, LED_CHANNEL, 0x0); for (LedBit = 0x0; LedBit < GpioWidth; LedBit++) { for (LedLoop = 0; LedLoop < LED_MAX_BLINK; LedLoop++) { /* * Set the GPIO Output to High */ XGpio_DiscreteWrite(&GpioOutput, LED_CHANNEL, 0xFFFFFFFF); #ifndef __SIM__ /* * Wait a small amount of time so the LED is visible */ for (Delay = 0; Delay < LED_DELAY; Delay++); #endif /* * Clear the GPIO Output */ XGpio_DiscreteClear(&GpioOutput, LED_CHANNEL, 0xFFFFFFFF); #ifndef __SIM__ /* * Wait a small amount of time so the LED is visible */ for (Delay = 0; Delay < LED_DELAY; Delay++); #endif } } return XST_SUCCESS; }
/****************************************************************************** * This function initializes the OLED display with the proper procedure. * * @param psSpi is the pointer to the SPI driver structure. * @param psGpio is the pointer to the GPIO driver structure. * * @return XST_SUCCESS - Everything went well * XST_FAILURE - Failure * * @note This is how the GPIO is mapped to the OLED controller: * gpio(0) = vbat * gpio(1) = vdd * gpio(2) = res * gpio(3) = dc *****************************************************************************/ XStatus fnOledDisplayInit(XSpi *psSpi, XGpio *psGpio) { XStatus Status; u8 rgbWrBuf[10]; // Clear the data/cmd bit XGpio_DiscreteClear(psGpio, 1, OLED_DC_MASK); // Start by turning VDD on and wait for the power to come up XGpio_DiscreteClear(psGpio, 1, OLED_VDD_MASK); MB_Sleep(1); // Send to SPI the display off command rgbWrBuf[0] = OLED_DISPLAY_OFF_CMD; u8TransferInProg = TRUE; Status = XSpi_Transfer(psSpi, rgbWrBuf, NULL, 1); while(u8TransferInProg); // Bring reset low and then high XGpio_DiscreteSet(psGpio, 1, OLED_RES_MASK); MB_Sleep(1); XGpio_DiscreteClear(psGpio, 1, OLED_RES_MASK); MB_Sleep(1); XGpio_DiscreteSet(psGpio, 1, OLED_RES_MASK); // Send the set charge pump and set pre-charge period commands rgbWrBuf[0] = 0x8D; rgbWrBuf[1] = 0x14; rgbWrBuf[2] = OLED_SET_PRECHARGE_PER_CMD; rgbWrBuf[3] = 0xF1; u8TransferInProg = TRUE; Status = XSpi_Transfer(psSpi, rgbWrBuf, NULL, 4); while(u8TransferInProg); // Turn on VCC and wait 100 ms XGpio_DiscreteClear(psGpio, 1, OLED_VBAT_MASK); MB_Sleep(100); // Set display contrast rgbWrBuf[0] = OLED_CONTRAST_CTRL_CMD; rgbWrBuf[1] = 0x0F; // Invert the display rgbWrBuf[2] = OLED_SET_SEGMENT_REMAP_CMD; rgbWrBuf[3] = OLED_SET_COM_DIR_CMD; // Select sequential COM configuration rgbWrBuf[4] = OLED_SET_COM_PINS_CMD; rgbWrBuf[5] = 0x00; rgbWrBuf[6] = 0xC0; rgbWrBuf[7] = 0x20; rgbWrBuf[8] = 0x00; // Turn display on rgbWrBuf[9]/*[6]*/ = OLED_DISPLAY_ON_CMD; u8TransferInProg = TRUE; Status = XSpi_Transfer(psSpi, rgbWrBuf, NULL, 10); while(u8TransferInProg); //Display Digilent logo fnOledPixelToDisplay(psSpi, psGpio, &sLogoPixmap); return Status; }
int main() { int i; init_platform(); print("--Starting...--\n\r"); /*for (i = 0; i < n_memory_ranges; i++) { test_memory_range(&memory_ranges[i]); }*/ int Status; /* Initialize the GPIO driver. If an error occurs then exit */ Status = XGpio_Initialize(&gpio_btn, XPAR_GPIO_BTN_DEVICE_ID); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* Initialize the GPIO driver. If an error occurs then exit */ Status = XGpio_Initialize(&gpio_led, XPAR_GPIO_LED_DEVICE_ID); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Perform a self-test on the GPIO. This is a minimal test and only * verifies that there is not any bus error when reading the data * register */ XGpio_SelfTest(&gpio_btn); XGpio_SelfTest(&gpio_led); XGpio_SetDataDirection(&gpio_btn, XGPIO_IR_CH1_MASK, 0x1); XGpio_SetDataDirection(&gpio_led, XGPIO_IR_CH1_MASK, 0x0); XGpio_DiscreteClear(&gpio_led, XGPIO_IR_CH1_MASK, 0xffff); param_setup(); XMcml_Initialize(&mcml, XPAR_MCML_0_DEVICE_ID); print("--Starting Kernel--\n\r"); XMcml_Start(&mcml); //cleanup_platform(); while (1) { if(XMcml_IsDone(&mcml)) break; } print("NOTE: MCML kernel is done.\n\r"); XGpio_DiscreteSet(&gpio_led, XGPIO_IR_CH1_MASK, 0x1); float * ar_z = (float *)memory_ranges[ARZ_CTRL].base; for(i=0; i< 10000; i++) { int whole, thousandths; float fval = *(ar_z++); whole = fval; thousandths = (fval - whole) * 1000; if(i % 100 == 0) xil_printf("\n\r"); xil_printf("%d.%3d *", whole, thousandths); } XGpio_DiscreteSet(&gpio_led, XGPIO_IR_CH1_MASK, 0x3); print("--DONE-- \n\r"); return 0; }
/** * This function is the Interrupt Service Routine for the GPIO device. It * will be called by the processor whenever an interrupt is asserted by the * device. * * This function will detect the push button on the board has changed state * and then turn on or off the LED. * * @param InstancePtr is the GPIO instance pointer to operate on. * It is a void pointer to meet the interface of an interrupt * processing function. * * @return None. * * @note None. * *****************************************************************************/ void GpioIsr(void *InstancePtr) { XGpio *GpioPtr = (XGpio *)InstancePtr; u32 Led; u32 LedState; u32 Buttons; u32 ButtonFound; u32 ButtonsChanged = 0; static u32 PreviousButtons; /* * Disable the interrupt */ XGpio_InterruptDisable(GpioPtr, BUTTON_INTERRUPT); /* Keep track of the number of interrupts that occur */ InterruptCount++; /* * There should not be any other interrupts occuring other than the * the button changes */ if ((XGpio_InterruptGetStatus(GpioPtr) & BUTTON_INTERRUPT) != BUTTON_INTERRUPT) { return; } /* * Read state of push buttons and determine which ones changed * states from the previous interrupt. Save a copy of the buttons * for the next interrupt */ Buttons = XGpio_DiscreteRead(GpioPtr, BUTTON_CHANNEL); ButtonsChanged = Buttons ^ PreviousButtons; PreviousButtons = Buttons; /* * Handle all button state changes that occurred since the last * interrupt */ while (ButtonsChanged != 0) { /* * Determine which button changed state and then get * the current state of the associated LED */ Led = MapButton2Led(ButtonsChanged, &ButtonFound); LedState = XGpio_DiscreteRead(GpioPtr, LED_CHANNEL) & Led; /* * Clear the button that is being processed so that it is * done and others can be handled also */ ButtonsChanged &= ~ButtonFound; /* Toggle the state of the LED */ if (LedState) { XGpio_DiscreteClear(GpioPtr, LED_CHANNEL, Led); } else { XGpio_DiscreteSet(GpioPtr, LED_CHANNEL, Led); } } /* Clear the interrupt such that it is no longer pending in the GPIO */ (void)XGpio_InterruptClear(GpioPtr, BUTTON_INTERRUPT); /* * Enable the interrupt */ XGpio_InterruptEnable(GpioPtr, BUTTON_INTERRUPT); }
void IO_Clear(u8 bit) { XGpio_DiscreteClear(&gpio, 1, (1u<<bit)); }
int wl_gpio_debug_initialize(){ XGpio_Initialize(&GPIO_debugpin, DEBUG_GPIO_DEVICE_ID); XGpio_DiscreteClear(&GPIO_debugpin, 1, 0xFF); return 0; }
inline void wl_clearDebugGPIO(u8 mask){ XGpio_DiscreteClear(&GPIO_debugpin, 1, mask); }
/** * * The purpose of this function is to illustrate how to use the GPIO level 1 * driver to turn on and off an LED. * * @param None * * @return XST_FAILURE to indicate that the GPIO Intialisation had failed. * * @note This function will not return if the test is running. * ******************************************************************************/ int main(void) { u32 Data; int Status; volatile int Delay; /* * Initialize the GPIO driver */ Status = XGpio_Initialize(&Gpio, GPIO_EXAMPLE_DEVICE_ID); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Set the direction for all signals to be inputs except the * LED output */ XGpio_SetDataDirection(&Gpio, LED_CHANNEL, ~LED); /* Loop forever blinking the LED */ while (1) { /* * Read the state of the data so that only the LED state can be * modified */ Data = XGpio_DiscreteRead(&Gpio, LED_CHANNEL); /* * Set the LED to the opposite state such that it blinks using * the first method, two methods are used for illustration * purposes only */ if (Data & LED) { XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, Data & ~LED); } else { XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, Data | LED); } /* Wait a small amount of time so the LED is visible */ for (Delay = 0; Delay < LED_DELAY; Delay++); /* * Read the state of the data so that only the LED state can be * modified */ Data = XGpio_DiscreteRead(&Gpio, LED_CHANNEL); /* * Set the LED to the opposite state such that it blinks using * the other API functions */ if (Data & LED) { XGpio_DiscreteClear(&Gpio, LED_CHANNEL, LED); } else { XGpio_DiscreteSet(&Gpio, LED_CHANNEL, LED); } /* Wait a small amount of time so the LED is visible */ for (Delay = 0; Delay < LED_DELAY; Delay++); } return XST_SUCCESS; }