xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength ) { /* NOTE: The baud rate used by this driver is determined by the hardware parameterization of the UART Lite peripheral, and the baud value passed to this function has no effect. */ ( void ) ulWantedBaud; /* Create the queues used to hold Rx and Tx characters. */ xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) ); xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) ); /* Only initialise the UART if the queues were created correctly. */ if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) ) { XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID ); XUartLite_ResetFifos( &xUART ); XUartLite_DisableInterrupt( &xUART ); if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS ) { /* xPortInstallInterruptHandler() could fail if vPortSetupInterruptController() has not been called prior to this function. */ XUartLite_EnableInterrupt( &xUART ); } } /* There is only one port so the handle is not used. */ return ( xComPortHandle ) 0; }
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength ) { portBASE_TYPE xStatus; /* The standard demo header file requires a baud rate to be passed into this function. However, in this case the baud rate is configured when the hardware is generated, leaving the ulWantedBaud parameter redundant. */ ( void ) ulWantedBaud; /* Create the queue used to hold Rx characters. */ xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) ); /* If the queue was created correctly, then setup the serial port hardware. */ if( xRxedChars != NULL ) { xStatus = XUartLite_Initialize( &xUartLiteInstance, XPAR_UARTLITE_1_DEVICE_ID ); if( xStatus == XST_SUCCESS ) { /* Complete initialisation of the UART and its associated interrupts. */ XUartLite_ResetFifos( &xUartLiteInstance ); /* Install the handlers that the standard Xilinx library interrupt service routine will call when Rx and Tx events occur respectively. */ XUartLite_SetRecvHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvRxHandler, NULL ); XUartLite_SetSendHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvTxHandler, NULL ); /* Install the standard Xilinx library interrupt handler itself. *NOTE* The xPortInstallInterruptHandler() API function must be used for this purpose. */ xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_UARTLITE_1_VEC_ID, ( XInterruptHandler ) XUartLite_InterruptHandler, &xUartLiteInstance ); /* Enable the interrupt in the peripheral. */ XUartLite_EnableIntr( xUartLiteInstance.RegBaseAddress ); /* Enable the interrupt in the interrupt controller. *NOTE* The vPortEnableInterrupt() API function must be used for this purpose. */ vPortEnableInterrupt( XPAR_INTC_0_UARTLITE_1_VEC_ID ); } configASSERT( xStatus == pdPASS ); } /* This demo file only supports a single port but something must be returned to comply with the standard demo header file. */ return ( xComPortHandle ) 0; }
/** * * Runs a self-test on the device hardware. Since there is no way to perform a * loopback in the hardware, this test can only check the state of the status * register to verify it is correct. This test assumes that the hardware * device is still in its reset state, but has been initialized with the * Initialize function. * * @param InstancePtr is a pointer to the XUartLite instance. * * @return * - XST_SUCCESS if the self-test was successful. * - XST_FAILURE if the self-test failed, the status register * value was not correct * * @note None. * ******************************************************************************/ int XUartLite_SelfTest(XUartLite *InstancePtr) { u32 StatusRegister; /* * Assert validates the input arguments */ Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); /* * Reset the FIFO's before reading the status register. * It is likely that the Uartlite IP may not always have an * empty Tx and Rx FIFO when a selftest is performed if more than one * uartlite instance is present in the h/w design. */ XUartLite_ResetFifos(InstancePtr); /* * Read the Status register value to check if it is the correct value * after a reset */ StatusRegister = XUartLite_ReadReg(InstancePtr->RegBaseAddress, XUL_STATUS_REG_OFFSET); /* * If the status register is any other value other than * XUL_SR_TX_FIFO_EMPTY then the test is a failure since this is * the not the value after reset */ if (StatusRegister != XUL_SR_TX_FIFO_EMPTY) { return XST_FAILURE; } return XST_SUCCESS; }
void InitInterrupts() { // Initialize wireless uart XUartLite_Initialize(&(wireless.uart), XPAR_WIRELESS_UART_DEVICE_ID); XUartLite_ResetFifos(&(wireless.uart)); Wireless_Init(&wireless); // Initialize gameboard uart XUartLite_Initialize(&gameboard_uart, XPAR_GAMEBOARD_UART_DEVICE_ID); XUartLite_ResetFifos(&gameboard_uart); // Initialize the interrupt controller XIntc_Initialize(&InterruptController, XPAR_INTC_0_DEVICE_ID); // Connect the wireless uart to the interrupt controller XIntc_Connect(&InterruptController, XPAR_XPS_INTC_0_WIRELESS_UART_INTERRUPT_INTR, (XInterruptHandler)XUartLite_InterruptHandler, (void *)&(wireless.uart)); // Connect the gameboard uart to the interrupt controller XIntc_Connect(&InterruptController, XPAR_XPS_INTC_0_GAMEBOARD_UART_INTERRUPT_INTR, (XInterruptHandler)XUartLite_InterruptHandler, (void *)&gameboard_uart); //XIntc_Connect(&InterruptController, XPAR_INTC_0_GPIO_0_VEC_ID, // (Xil_ExceptionHandler)GpioHandler, &Gpio); XIntc_Start(&InterruptController, XIN_REAL_MODE); // Enable interrupts for serial controllers XIntc_Enable(&InterruptController, XPAR_XPS_INTC_0_WIRELESS_UART_INTERRUPT_INTR); XIntc_Enable(&InterruptController, XPAR_XPS_INTC_0_GAMEBOARD_UART_INTERRUPT_INTR); // Enable interrupts for the GPIO //XIntc_Enable(&InterruptController, XPAR_INTC_0_GPIO_0_VEC_ID); Xil_ExceptionInit(); Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XIntc_InterruptHandler, &InterruptController); Xil_ExceptionEnable(); // Set up send/receive handlers for wireless uart XUartLite_SetSendHandler(&(wireless.uart), WirelessSendHandler, &(wireless.uart)); XUartLite_SetRecvHandler(&(wireless.uart), WirelessRecvHandler, &(wireless.uart)); //XUartLite_SetRecvHandler(&(wireless.uart), WirelessRecvHandlerNonBlocking, &(wireless.uart)); Wireless_Debug("Wireless should be set up now"); // Set up send/receive handlers for gameboard uart XUartLite_SetSendHandler(&gameboard_uart, GameboardSendHandler, &gameboard_uart); XUartLite_SetRecvHandler(&gameboard_uart, GameboardRecvHandler, &gameboard_uart); XUartLite_EnableInterrupt(&(wireless.uart)); XUartLite_EnableInterrupt(&gameboard_uart); // Enable interrupts for GPIO //XGpio_InterruptEnable(&Gpio, XGPIO_IR_CH2_MASK); //XGpio_InterruptGlobalEnable(&Gpio); // Set up PIT XExceptionHandler pithandler = &my_pitHandler; XExc_RegisterHandler(XEXC_ID_PIT_INT, pithandler, 0); XTime_PITEnableAutoReload(); // PIT should be set to 1ms XTime_PITSetInterval(300000); XExc_mEnableExceptions(XEXC_ALL); XTime_PITEnableInterrupt(); XTime_PITClearInterrupt(); }
int main(void) { LOG_INFO("UART CTP FE echo test\n"); init_platform(); tx_buffer = cbuffer_new(); rx_buffer = cbuffer_new(); int Status; u16 DeviceId = UARTLITE_DEVICE_ID; /* * Initialize the UartLite driver so that it's ready to use. */ Status = XUartLite_Initialize(&UartLite, DeviceId); if (Status != XST_SUCCESS) { LOG_ERROR ("Error: could not initialize UART\n"); return XST_FAILURE; } XUartLite_ResetFifos(&UartLite); /* * Perform a self-test to ensure that the hardware was built correctly. */ Status = XUartLite_SelfTest(&UartLite); if (Status != XST_SUCCESS) { LOG_ERROR ("Error: self test failed\n"); return XST_FAILURE; } /* * Connect the UartLite to the interrupt subsystem such that interrupts can * occur. This function is application specific. */ Status = SetupInterruptSystem(&UartLite); if (Status != XST_SUCCESS) { LOG_ERROR ("Error: could not setup interrupts\n"); return XST_FAILURE; } /* * Setup the handlers for the UartLite that will be called from the * interrupt context when data has been sent and received, specify a * pointer to the UartLite driver instance as the callback reference so * that the handlers are able to access the instance data. */ XUartLite_SetSendHandler(&UartLite, SendHandler, &UartLite); XUartLite_SetRecvHandler(&UartLite, RecvHandler, &UartLite); /* * Enable the interrupt of the UartLite so that interrupts will occur. */ XUartLite_EnableInterrupt(&UartLite); // bootstrap the READ LOG_DEBUG("Bootstrapping READ\n"); XUartLite_Recv(&UartLite, (u8*)&rx_tmp_buffer, sizeof(uint32_t)); LOG_INFO("Starting loop\n"); /* LOG_DEBUG("Sending 'wtf!'\n"); currently_sending = 1; char help[4] = "wtf!"; unsigned int ret = XUartLite_Send(&UartLite, (u8*)help, 4); LOG_DEBUG("WTF send complete return: %x\n", ret); */ /* echo received data forever */ unsigned int heartbeat = 0; while (1) { if (heartbeat++ % (1 << 8)) { //LOG_DEBUG("bump %x\n", heartbeat); } while (cbuffer_size(rx_buffer) && cbuffer_freespace(tx_buffer)) { uint32_t data = cbuffer_pop_front(rx_buffer); //LOG_DEBUG("Echoing data word %x\n", data); cbuffer_push_back(tx_buffer, data); } if (!currently_sending && cbuffer_size(tx_buffer)) { LOG_DEBUG("\nREINT SEND\n"); currently_sending = 1; /* if (XUartLite_IsSending(&UartLite)) { LOG_DEBUG("UART STAT: sending\n"); } else { LOG_DEBUG("UART STAT: idle\n"); } */ unsigned int to_send = cbuffer_contiguous_data_size(tx_buffer) * sizeof(uint32_t); u8* output_ptr = (u8*)&(tx_buffer->data[tx_buffer->pos]); //LOG_DEBUG("REINIT %x\n", to_send); //LOG_DEBUG("SENDADDR %x\n", output_ptr); XUartLite_Send(&UartLite, output_ptr, to_send); } } }
int main() { /********************** TouchSensor Setup *********************/ Status = TouchSensor_Initialize(&touchSensor, XPAR_TOUCHSENSOR_0_DEVICE_ID); if (Status != XST_SUCCESS) { // printf("TouchSensor initialisation error\n\r\r"); } TouchSensorButtons_InitializeManager(&Manager, &touchSensor, &PrintTouchCoordinates); button_t GameboardGridButton; Button_SetGridDim(&GameboardGridButton, SQUARE_DIM, SQUARE_DIM, BOARD_OFFSET_X, BOARD_OFFSET_Y, BOARD_SIZE, BOARD_SIZE); Button_AssignHandler(&GameboardGridButton, &HandleGameboardTouch); TouchSensorButtons_RegisterButton(&Manager, &GameboardGridButton); TouchSensorButtons_EnableButton(&GameboardGridButton); button_t WhiteModeButton; Button_SetGridDim(&WhiteModeButton, 50, 50, 300, 10 , 3, 1); Button_AssignHandler(&WhiteModeButton, &HandleWhiteModeTouch); TouchSensorButtons_RegisterButton(&Manager, &WhiteModeButton); TouchSensorButtons_EnableButton(&WhiteModeButton); button_t BlackModeButton; Button_SetGridDim(&BlackModeButton, 50, 50, 300, 75, 3, 1); Button_AssignHandler(&BlackModeButton, &HandleBlackModeTouch); TouchSensorButtons_RegisterButton(&Manager, &BlackModeButton); TouchSensorButtons_EnableButton(&BlackModeButton); button_t ResetButton; Button_SetRectDim(&ResetButton, 100, 50, 325, 200); Button_AssignHandler(&ResetButton, &HandleResetTouch); TouchSensorButtons_RegisterButton(&Manager, &ResetButton); TouchSensorButtons_EnableButton(&ResetButton); /********************** BoardCount Accelerator Setup********************/ initialize_accelerator(&board_count_accelerator, XPAR_GENERATE_BOARD_COUNTS_TOP_0_S_AXI_CTRL_BASEADDR); /********************** TFT Setup *********************/ blackScreen(); TFT_Initialize(&tft, XPAR_TFT_PERHIPHERAL_0_DEVICE_ID); TFT_SetImageAddress(&tft, XPAR_MCB_DDR2_S0_AXI_BASEADDR); TFT_SetBrightness(&tft, 7); TFT_TurnOn(&tft); //Render Buttons //TouchSensorButtons_RenderButton(&MyCircle, GREEN, &tft); //TouchSensorButtons_RenderButton(&MyRect, RED, &tft); TouchSensorButtons_RenderButton(&GameboardGridButton, BLUE, &tft); TouchSensorButtons_RenderButton(&ResetButton, ORANGE, &tft); TouchSensorButtons_RenderButton(&WhiteModeButton, WHITE, &tft); TouchSensorButtons_RenderButton(&BlackModeButton, RED, &tft); Gameboard.TftPtr = &tft; /********************** UART Setup *********************/ Status = XUartLite_Initialize(&Uart, XPAR_UARTLITE_1_DEVICE_ID); if (Status != XST_SUCCESS) { printf("XUartLite initialization error\n\r"); } XUartLite_SetRecvHandler(&Uart, (XUartLite_Handler) &RecvUartCommand, &Uart); /********************** Interrupt Controller Setup *********************/ /* * Initialize the interrupt controller driver so that it's ready to use, * using the device ID that is generated in xparameters.h */ Status = XIntc_Initialize(&InterruptController, XPAR_AXI_INTC_0_DEVICE_ID); if (Status != XST_SUCCESS) { //printf("Interrupt controller initialization error\n\r"); } /* * Connect the device driver handler that will be called when an interrupt * for the device occurs, the device driver handler performs the specific * interrupt processing for the device */ Status = XIntc_Connect(&InterruptController, XPAR_AXI_INTC_0_TOUCHSENSOR_0_INTERRUPT_INTR, (XInterruptHandler)TouchSensorButtons_InterruptHandler, &Manager); if (Status != XST_SUCCESS) { //printf("Interrupt controller connect error\n\r"); } Status = XIntc_Connect(&InterruptController, XPAR_AXI_INTC_0_RS232_UART_1_INTERRUPT_INTR, (XInterruptHandler)XUartLite_InterruptHandler, &Uart); if (Status != XST_SUCCESS) { printf("Interrupt controller connect to Uart error\n\r"); } Status = XIntc_Start(&InterruptController, XIN_REAL_MODE); if (Status != XST_SUCCESS) { //printf("Interrupt controller start error\n\r"); } XIntc_Enable(&InterruptController, XPAR_AXI_INTC_0_TOUCHSENSOR_0_INTERRUPT_INTR); XIntc_Enable(&InterruptController, XPAR_AXI_INTC_0_RS232_UART_1_INTERRUPT_INTR); XUartLite_ResetFifos(&Uart); XUartLite_EnableInterrupt(&Uart); AI_PLAYER ai = default_ai(); PLAYER Player1, Player2; Player1.num = P1; Player2.num = P2; Gameboard_Initialize(&Gameboard,human,fpga); microblaze_enable_interrupts(); while (1) { PLAYER Curr_P, Opp_P; //Workaround to enable multiple resets on turn 0. if (Gameboard.MoveBufferSize == -1) Gameboard.MoveBufferSize++; int CurrentMove = Gameboard.MoveBufferSize; player_mode_t CurrentPlayerMode = (CurrentMove % 2) ? Gameboard.WhiteMode : Gameboard.BlackMode; Curr_P = (CurrentMove % 2) ? Player1 : Player2; Opp_P = (CurrentMove % 2) ? Player2 : Player1; switch (CurrentPlayerMode){ case human: TouchSensorButtons_EnableButton(&GameboardGridButton); break; case fpga: TouchSensorButtons_DisableButton(&GameboardGridButton); HandleAiMove(ai, Gameboard.master_board, Curr_P, Opp_P); break; case uart: TouchSensorButtons_DisableButton(&GameboardGridButton); if(Gameboard.MoveBufferSize > 0){ SendUartCommand(&Uart, Gameboard.MoveBuffer[Gameboard.MoveBufferSize-1].X, Gameboard.MoveBuffer[Gameboard.MoveBufferSize-1].Y); } break; default: TouchSensorButtons_EnableButton(&GameboardGridButton); break; } /* if (check_board_full(Gameboard.master_board)) { //printf("Gameboard full\n\r"); break; } */ if (check_board_win(Gameboard.master_board, Curr_P)) { xil_printf("Player %s won\n\r", (Curr_P.num == P1) ? "white" : "black"); //Spin waiting on reset while(Gameboard.MoveBufferSize != -1); } while(CurrentMove == Gameboard.MoveBufferSize); } return 0; }