main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, Emulate SPI on GPIO pins
  16. *
  17. * Pin connection:
  18. * P35 => CSN
  19. * P34 => DATA
  20. * P32 => 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 0
  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. uint8_t XL2400_PrintStatus(void);
  34. void GPIO_Init(void)
  35. {
  36. GPIO_P3_SetMode(GPIO_Pin_4, GPIO_Mode_InOut_QBD);
  37. // SCLK(P32) CSN(P35)
  38. GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_5, GPIO_Mode_Output_PP);
  39. }
  40. int main(void)
  41. {
  42. __CODE uint8_t tmp[] = {
  43. 0x1F, 0x80, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  44. 0x21, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28,
  45. 0x31, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x38,
  46. 0x41, 0x12, 0x13, 0x14, 0x15, 0x16, 0x37, 0x48};
  47. uint8_t i, j, status;
  48. SYS_SetClock();
  49. GPIO_Init();
  50. // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt
  51. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  52. UART1_TxString("UART Initialized\r\n");
  53. while (XL2400_SPI_Test() == HAL_ERROR)
  54. {
  55. UART1_TxString(" - check failed\r\n");
  56. SYS_Delay(1000);
  57. }
  58. UART1_TxString(" - check passed\r\n");
  59. XL2400_Init();
  60. XL2400_SetPower(XL2400_RF_0DB);
  61. #if XL2400_MODE == 0
  62. XL2400_SetChannel(78);
  63. XL2400_SetTxAddress(RX_ADDRESS);
  64. XL2400_SetRxAddress(TX_ADDRESS);
  65. XL2400_SetTxMode();
  66. UART1_TxString("XL2400 TX Initialized\r\n");
  67. while(1)
  68. {
  69. //XL2400_PrintStatus();
  70. status = XL2400_Tx(tmp, XL2400_PLOAD_WIDTH);
  71. i++;
  72. if (status == 0x20)
  73. {
  74. j++;
  75. }
  76. if (i == 0)
  77. {
  78. UART1_TxHex(j);
  79. UART1_TxString("\r\n");
  80. j = 0;
  81. }
  82. // >= 2ms
  83. SYS_Delay(3);
  84. }
  85. #else
  86. // RX
  87. XL2400_SetChannel(77);
  88. XL2400_SetTxAddress(RX_ADDRESS);
  89. XL2400_SetRxAddress(TX_ADDRESS);
  90. XL2400_WakeUp();
  91. UART1_TxString("XL2400 RX Initialized\r\n");
  92. while(1)
  93. {
  94. XL2400_SetRxMode();
  95. while (--j)
  96. {
  97. status = XL2400_Rx();
  98. if (status & RX_DR_FLAG)
  99. {
  100. UART1_TxString(".");
  101. }
  102. }
  103. //XL2400_PrintStatus();
  104. //XL2400_Sleep();
  105. //SYS_Delay(1);
  106. }
  107. #endif
  108. }