nrf24l01_stc8h1k.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * Example code of SPI driving NRF24L01 module
  16. *
  17. * Pin connection:
  18. * P35(SS, Ignored) => CSN
  19. * P34(MOSI) => MOSI
  20. * P33(MISO) => MISO
  21. * P32(SPCLK) => CLK
  22. * P36(INT2) => IRQ
  23. * P37(IO) => CE
  24. *
  25. * test-board: Minimum System; test-MCU: STC8H1K08,STC8H3K32S2
  26. */
  27. #include "nrf24l01.h"
  28. #include <stdio.h>
  29. const NRF24_SCEN CURRENT_SCEN = NRF24_SCEN_HALF_DUPLEX;
  30. extern uint8_t __IDATA xbuf[NRF24_PLOAD_WIDTH + 1];
  31. void SPI_Init(void)
  32. {
  33. // SPI frequency
  34. SPI_SetClockPrescaler(SPI_ClockPreScaler_16);
  35. // Clock is low when idle
  36. SPI_SetClockPolarity(HAL_State_OFF);
  37. // Data transfer is driven by lower SS pin
  38. SPI_SetClockPhase(SPI_ClockPhase_LeadingEdge);
  39. // MSB first
  40. SPI_SetDataOrder(SPI_DataOrder_MSB);
  41. // Define the output pins
  42. SPI_SetPort(SPI_AlterPort_P35_P34_P33_P32);
  43. // Ignore SS pin, use MSTR to swith between master/slave mode
  44. SPI_IgnoreSlaveSelect(HAL_State_ON);
  45. // Master mode
  46. SPI_SetMasterMode(HAL_State_ON);
  47. // Start SPI
  48. SPI_SetEnabled(HAL_State_ON);
  49. }
  50. void GPIO_Init(void)
  51. {
  52. // Configure GPIO pins before SPI and device
  53. // MISO(P33) MOSI(P34)
  54. GPIO_P3_SetMode(GPIO_Pin_4, GPIO_Mode_InOut_QBD);
  55. // SCLK(P32) CSN(P35) CE(P37)
  56. GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_7, GPIO_Mode_Output_PP);
  57. // IRQ(P36)
  58. GPIO_P3_SetMode(GPIO_Pin_6, GPIO_Mode_Input_HIP);
  59. }
  60. void INT_Init()
  61. {
  62. EXTI_Int2_SetIntState(HAL_State_ON);
  63. EXTI_Global_SetIntState(HAL_State_ON);
  64. }
  65. INTERRUPT(Int2_Routine, EXTI_VectInt2)
  66. {
  67. NRF24L01_HandelIrqFlag();
  68. }
  69. void main(void)
  70. {
  71. uint8_t __CODE tmp[] = {
  72. 0x1F, 0x80, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  73. 0x21, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28,
  74. 0x31, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x38,
  75. 0x41, 0x12, 0x13, 0x14, 0x15, 0x16, 0x37, 0x48};
  76. uint8_t succ = 0, err = 0;
  77. SYS_SetClock();
  78. GPIO_Init();
  79. // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt
  80. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  81. UART1_TxString("UART Initialized\r\n");
  82. SPI_Init();
  83. UART1_TxString("SPI Initialized\r\n");
  84. while (NRF24L01_Check() == 1)
  85. {
  86. UART1_TxString("Check failed\r\n");
  87. SYS_Delay(1000);
  88. }
  89. UART1_TxString("NRF24L01 Checked\r\n");
  90. switch (CURRENT_SCEN)
  91. {
  92. case NRF24_SCEN_TX:
  93. NRF24L01_Init(NRF24_MODE_TX);
  94. UART1_TxString("NRF24L01 Initialized\r\n");
  95. while (1)
  96. {
  97. if (NRF24L01_WriteFast(tmp) == 0)
  98. {
  99. NRF24L01_ResetTX();
  100. err++;
  101. }
  102. else
  103. {
  104. succ++;
  105. }
  106. if (err >= 255 || succ >= 255)
  107. {
  108. UART1_TxHex(err);
  109. UART1_TxHex(succ);
  110. UART1_TxChar('.');
  111. err = 0;
  112. succ = 0;
  113. }
  114. SYS_Delay(50);
  115. }
  116. break;
  117. case NRF24_SCEN_RX:
  118. NRF24L01_Init(NRF24_MODE_RX);
  119. INT_Init();
  120. while (1);
  121. break;
  122. case NRF24_SCEN_HALF_DUPLEX:
  123. NRF24L01_Init(NRF24_MODE_RX);
  124. INT_Init();
  125. while (1)
  126. {
  127. NRF24L01_Tx(tmp);
  128. SYS_Delay(1000);
  129. }
  130. break;
  131. default:
  132. UART1_TxString("Unknown scen\r\n");
  133. break;
  134. }
  135. }