main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /***
  15. * Demo: XL2400 / WL2400 SOP8 2.4GHz RF
  16. *
  17. * Pin connection:
  18. * P35(SS, Ignored) => CSN
  19. * P34(MOSI) => DATA
  20. * P32(SPCLK) => SCK
  21. * VDD1 => 3.3V
  22. * XC1,XC2 => 16MHz OSC
  23. * GND => GND
  24. *
  25. * test-board: Minimum System; test-MCU: STC8H1K08,STC8H3K64S2
  26. */
  27. #include "fw_hal.h"
  28. #include "xl2400.h"
  29. // 0:TX, 1:RX
  30. #define XL2400_MODE 1
  31. __CODE uint8_t TX_ADDRESS[5] = {0x11,0x33,0x33,0x33,0x11};
  32. __CODE uint8_t RX_ADDRESS[5] = {0x33,0x55,0x33,0x44,0x33};
  33. extern uint8_t *xbuf_data;
  34. uint8_t XL2400_PrintStatus(void);
  35. void SPI_Init(void)
  36. {
  37. // SPI frequency
  38. SPI_SetClockPrescaler(SPI_ClockPreScaler_16);
  39. // Clock is low when idle
  40. SPI_SetClockPolarity(HAL_State_OFF);
  41. // Data transfer is driven by lower SS pin
  42. SPI_SetClockPhase(SPI_ClockPhase_LeadingEdge);
  43. // MSB first
  44. SPI_SetDataOrder(SPI_DataOrder_MSB);
  45. // Define the output pins
  46. SPI_SetPort(SPI_AlterPort_P35_P34_P33_P32);
  47. // Ignore SS pin, use MSTR to swith between master/slave mode
  48. SPI_IgnoreSlaveSelect(HAL_State_ON);
  49. // Master mode
  50. SPI_SetMasterMode(HAL_State_ON);
  51. // Start SPI
  52. SPI_SetEnabled(HAL_State_ON);
  53. }
  54. void GPIO_Init(void)
  55. {
  56. // Configure GPIO pins before SPI and device
  57. // MISO(P33) MOSI(P34)
  58. GPIO_P3_SetMode(GPIO_Pin_4, GPIO_Mode_InOut_QBD);
  59. // SCLK(P32) CSN(P35) CE(P37)
  60. GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_7, GPIO_Mode_Output_PP);
  61. }
  62. int main(void)
  63. {
  64. __CODE uint8_t tmp[] = {
  65. 0x1F, 0x80, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  66. 0x21, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28,
  67. 0x31, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x38,
  68. 0x41, 0x12, 0x13, 0x14, 0x15, 0x16, 0x37, 0x48};
  69. uint8_t i, j, status;
  70. SYS_SetClock();
  71. GPIO_Init();
  72. // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt
  73. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  74. UART1_TxString("UART Initialized\r\n");
  75. SPI_Init();
  76. UART1_TxString("SPI Initialized\r\n");
  77. while (XL2400_SPI_Test() == HAL_ERROR)
  78. {
  79. UART1_TxString(" - check failed\r\n");
  80. SYS_Delay(1000);
  81. }
  82. UART1_TxString(" - check passed\r\n");
  83. XL2400_Init();
  84. XL2400_SetPower(XL2400_RF_0DB);
  85. if (XL2400_MODE == 0)
  86. {
  87. // TX
  88. XL2400_SetChannel(78);
  89. XL2400_SetTxAddress(RX_ADDRESS);
  90. XL2400_SetRxAddress(TX_ADDRESS);
  91. XL2400_SetTxMode();
  92. UART1_TxString("XL2400 TX Initialized\r\n");
  93. while(1)
  94. {
  95. //XL2400_PrintStatus();
  96. XL2400_Tx(tmp, XL2400_PLOAD_WIDTH);
  97. SYS_Delay(100);
  98. }
  99. }
  100. else
  101. {
  102. // RX
  103. XL2400_SetChannel(77);
  104. XL2400_SetTxAddress(RX_ADDRESS);
  105. XL2400_SetRxAddress(TX_ADDRESS);
  106. UART1_TxString("XL2400 RX Initialized\r\n");
  107. while(1)
  108. {
  109. XL2400_WakeUp();
  110. XL2400_SetRxMode();
  111. while (--j)
  112. {
  113. status = XL2400_Rx();
  114. if (status & RX_DR_FLAG)
  115. {
  116. UART1_TxString(" <\r\n");
  117. }
  118. }
  119. //XL2400_PrintStatus();
  120. XL2400_Sleep();
  121. SYS_Delay(10);
  122. }
  123. }
  124. }