bf16-brd-control.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "bf16-brd-control.h"
  2. #include "miner.h"
  3. #define BV(x) (1 << x)
  4. static uint32_t ctrl_read(uint8_t gpio, uint8_t reg)
  5. {
  6. gpio_rq_t rq;
  7. rq.gpioIndex = gpio;
  8. rq.regIndex = reg;
  9. gpio_read_ctrl(&rq);
  10. return rq.data;
  11. }
  12. static int8_t ctrl_write(uint8_t gpio, uint8_t reg, uint32_t data)
  13. {
  14. gpio_rq_t rq;
  15. rq.gpioIndex = gpio;
  16. rq.regIndex = reg;
  17. rq.data = data;
  18. return gpio_write_ctrl(&rq);
  19. }
  20. void brd_init(void)
  21. {
  22. uint32_t data = ctrl_read(GPIO0_INDEX, OE_REG_INDEX);
  23. data &= ~(BV(CH1_MSP_RST_PIN) | BV(LED_GREEN_PIN));
  24. ctrl_write(GPIO0_INDEX, OE_REG_INDEX, data);
  25. data = ctrl_read(GPIO1_INDEX, OE_REG_INDEX);
  26. data &= ~(BV(LED_RED_PIN) | BV(CH2_MSP_RST_PIN) | BV(CH1_SPI_RES_PIN));
  27. ctrl_write(GPIO1_INDEX, OE_REG_INDEX, data);
  28. data = ctrl_read(GPIO2_INDEX, OE_REG_INDEX);
  29. data &= ~(BV(BUZZER_PIN) | BV(CH2_SPI_RES_PIN));
  30. ctrl_write(GPIO2_INDEX, OE_REG_INDEX, data);
  31. }
  32. int get_hw_ver(void)
  33. {
  34. uint32_t data1 = ctrl_read(GPIO2_INDEX, DATAIN_REG_INDEX);
  35. uint32_t data2 = ctrl_read(GPIO1_INDEX, DATAIN_REG_INDEX);
  36. uint8_t result = BIT_STATE(data1, BRD_VER0_PIN);
  37. result |= BIT_STATE(data1, BRD_VER1_PIN) ? 2 : 0;
  38. result |= BIT_STATE(data1, BRD_VER2_PIN) ? 4 : 0;
  39. result |= BIT_STATE(data2, BRD_VER3_PIN) ? 8 : 0;
  40. return result;
  41. }
  42. int get_btn_fr(void)
  43. {
  44. uint32_t data = ctrl_read(GPIO2_INDEX, DATAIN_REG_INDEX);
  45. return BIT_INV_STATE(data, BRD_BUT1_PIN);
  46. }
  47. int get_btn_discovery(void)
  48. {
  49. uint32_t data = ctrl_read(GPIO0_INDEX, DATAIN_REG_INDEX);
  50. return BIT_INV_STATE(data, BRD_BUT2_PIN);
  51. }
  52. int get_ch1_det(void)
  53. {
  54. uint32_t data = ctrl_read(GPIO0_INDEX, DATAIN_REG_INDEX);
  55. return BIT_INV_STATE(data, BRD_DET1_PIN);
  56. }
  57. int get_ch2_det(void)
  58. {
  59. uint32_t data = ctrl_read(GPIO1_INDEX, DATAIN_REG_INDEX);
  60. return BIT_INV_STATE(data, BRD_DET2_PIN);
  61. }
  62. static uint8_t _direct_state_reg_index(uint8_t state)
  63. {
  64. return (state != 0) ? DATASET_REG_INDEX : DATACLR_REG_INDEX;
  65. }
  66. static uint8_t _inverse_state_reg_index(uint8_t state)
  67. {
  68. return (state != 0) ? DATACLR_REG_INDEX : DATASET_REG_INDEX;
  69. }
  70. int8_t set_buzzer(uint8_t state)
  71. {
  72. return ctrl_write(GPIO2_INDEX, _direct_state_reg_index(state), BV(BUZZER_PIN));
  73. }
  74. int8_t set_led_green(uint8_t state)
  75. {
  76. return ctrl_write(GPIO0_INDEX, _direct_state_reg_index(state), BV(LED_GREEN_PIN));
  77. }
  78. int8_t set_led_red(uint8_t state)
  79. {
  80. return ctrl_write(GPIO1_INDEX, _direct_state_reg_index(state), BV(LED_RED_PIN));
  81. }
  82. int8_t set_ch1_rst(uint8_t state)
  83. {
  84. return ctrl_write(GPIO0_INDEX, _inverse_state_reg_index(state), BV(CH1_MSP_RST_PIN));
  85. }
  86. int8_t set_ch2_rst(uint8_t state)
  87. {
  88. return ctrl_write(GPIO1_INDEX, _inverse_state_reg_index(state), BV(CH2_MSP_RST_PIN));
  89. }
  90. int8_t set_ch1_spi(uint8_t state)
  91. {
  92. return ctrl_write(GPIO2_INDEX, _direct_state_reg_index(state), BV(CH1_SPI_RES_PIN));
  93. }
  94. int8_t set_ch2_spi(uint8_t state)
  95. {
  96. return ctrl_write(GPIO1_INDEX, _direct_state_reg_index(state), BV(CH2_SPI_RES_PIN));
  97. }