board.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "board.h"
  2. #include <string.h>
  3. #include "xparameters.h"
  4. #include "xtmrctr.h"
  5. #include "xintc.h"
  6. #include "xgpio.h"
  7. #define TMRCTR_DEVICE_ID XPAR_TMRCTR_0_DEVICE_ID
  8. #define TMRCTR_BASEADDR XPAR_TMRCTR_0_BASEADDR
  9. #define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID
  10. #define TMRCTR_INTERRUPT_ID XPAR_MICROBLAZE_0_AXI_INTC_AXI_TIMER_0_INTERRUPT_INTR
  11. #define TIMER_CNTR_0 0
  12. #define GPIO_OUTPUT_DEVICE_ID XPAR_GPIO_0_DEVICE_ID
  13. #define LED_CHANNEL 1
  14. // reset after 1 sec
  15. #define RESET_VALUE (0xFFFFFFFF - XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ)
  16. XIntc InterruptController;
  17. XTmrCtr xil_timer;
  18. XGpio GpioOutput;
  19. void TimerCounterHandler(void *CallBackRef, u8 TmrCtrNumber);
  20. static uint32_t gpio_flag = 0xFFFF;
  21. std::uint64_t gpt_system_tick = 0;
  22. std::uint32_t gpio_addr;
  23. void Board::toggle_led() {
  24. * ((volatile u32 *) gpio_addr) = gpio_flag;
  25. gpio_flag ^= 0xFFFF;
  26. }
  27. void Board::init_start() {
  28. XGpio_Initialize(&GpioOutput, GPIO_OUTPUT_DEVICE_ID);
  29. XGpio_SetDataDirection(&GpioOutput, LED_CHANNEL, 0x0);
  30. XTmrCtr_Initialize(&xil_timer, TMRCTR_DEVICE_ID);
  31. XTmrCtr_SelfTest(&xil_timer, TIMER_CNTR_0);
  32. XIntc_Initialize(&InterruptController, INTC_DEVICE_ID);
  33. XIntc_Connect(&InterruptController,
  34. TMRCTR_INTERRUPT_ID,
  35. (XInterruptHandler)XTmrCtr_InterruptHandler,
  36. (void*) &xil_timer);
  37. Xil_ExceptionInit();
  38. Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
  39. (Xil_ExceptionHandler) XIntc_InterruptHandler,
  40. &InterruptController);
  41. XTmrCtr_SetHandler(&xil_timer,
  42. TimerCounterHandler,
  43. &xil_timer);
  44. XTmrCtr_SetOptions(&xil_timer,
  45. TIMER_CNTR_0,
  46. XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION);
  47. XTmrCtr_SetResetValue(&xil_timer, TIMER_CNTR_0, RESET_VALUE);
  48. gpio_addr = GpioOutput.BaseAddress + XGPIO_DATA_OFFSET;
  49. }
  50. void Board::enable_interrupts() {
  51. microblaze_enable_interrupts();
  52. XIntc_Start(&InterruptController, XIN_REAL_MODE);
  53. XIntc_Enable(&InterruptController, TMRCTR_INTERRUPT_ID);
  54. Xil_ExceptionEnable();
  55. }
  56. void Board::disable_interrupts() {
  57. microblaze_disable_interrupts();
  58. }
  59. void Board::enable_timer() {
  60. XTmrCtr_Start(&xil_timer, TIMER_CNTR_0);
  61. }
  62. void Board::disable_timer() {
  63. XTmrCtr_Stop(&xil_timer, TIMER_CNTR_0);
  64. XIntc_Disable(&InterruptController, TMRCTR_INTERRUPT_ID);
  65. }
  66. rl::TimePoint Board::get_now() {
  67. return gpt_system_tick;
  68. }
  69. void adjust_timer(const rotor_light::Duration &value) {
  70. gpt_system_tick += value;
  71. }
  72. void Board::delay() {}
  73. // on interrupt add 1 second
  74. void TimerCounterHandler(void *CallBackRef, u8 TmrCtrNumber)
  75. {
  76. gpt_system_tick ++;
  77. }