main.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,p THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* USER CODE END Header */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "main.h"
  42. /* Private includes ----------------------------------------------------------*/
  43. /* USER CODE BEGIN Includes */
  44. /* USER CODE END Includes */
  45. /* Private typedef -----------------------------------------------------------*/
  46. /* USER CODE BEGIN PTD */
  47. /* USER CODE END PTD */
  48. /* Private define ------------------------------------------------------------*/
  49. /* USER CODE BEGIN PD */
  50. #define LORA_TIMER_CNT 600000 / 2
  51. /* USER CODE END PD */
  52. /* Private macro -------------------------------------------------------------*/
  53. /* USER CODE BEGIN PM */
  54. /* USER CODE END PM */
  55. /* Private variables ---------------------------------------------------------*/
  56. I2C_HandleTypeDef hi2c2;
  57. TIM_HandleTypeDef htim6;
  58. UART_HandleTypeDef huart1;
  59. UART_HandleTypeDef huart2;
  60. DMA_HandleTypeDef hdma_usart1_rx;
  61. /* USER CODE BEGIN PV */
  62. uint8_t rx1_data[buf_size];
  63. uint8_t rx2_data[buf_size];
  64. uint8_t ring_buf[buf_size];
  65. uint8_t count_in1 = 0, count_out1 = 0;
  66. uint8_t count_in2 = 0, count_out2 = 0;
  67. //uint8_t count_in3 = 0, count_out = 0;
  68. uint8_t UartDataisReved;
  69. uint8_t LoraDataSend;
  70. uint8_t RGB_SensorIDAutoset = 0;
  71. volatile uint32_t UartTimerCnt = 0;
  72. volatile uint32_t LedTimerCnt = 0;
  73. volatile uint32_t LoraTxTimerCnt = 0;
  74. volatile uint32_t LoraAckTimerCnt = 0;
  75. uint8_t buf[buf_size] = {0,};
  76. uint8_t buf1[buf_size] = {0,};
  77. uint8_t buf2[buf_size] = {0,};
  78. Default_SX1276_t Default_SX1276;
  79. SX1276_hw_t SX1276_hw;
  80. SX1276_t SX1276;
  81. uint8_t MyControllerID = 0;
  82. uint8_t SensorID = 0;
  83. typedef enum{
  84. LoraRx_mode = 0,
  85. LoraTx_mode ,
  86. };
  87. /* USER CODE END PV */
  88. /* Private function prototypes -----------------------------------------------*/
  89. void SystemClock_Config(void);
  90. static void MX_GPIO_Init(void);
  91. static void MX_DMA_Init(void);
  92. static void MX_TIM6_Init(void);
  93. static void MX_USART1_UART_Init(void);
  94. static void MX_USART2_UART_Init(void);
  95. static void MX_I2C2_Init(void);
  96. static void MX_NVIC_Init(void);
  97. /* USER CODE BEGIN PFP */
  98. void RGB_SensorIDAutoSet(uint8_t set);
  99. uint8_t RGB_SensorIDAutoGet(void);
  100. void UartDataRecvSet(uint8_t val);
  101. #if 0 // PYJ.2019.04.19_BEGIN --
  102. void Uart_dataCheck(uint8_t* cnt);
  103. #else
  104. void Uart_dataCheck(uint8_t*,uint8_t* cnt);
  105. #endif // PYJ.2019.04.19_END --
  106. void Uart1_Data_Send(uint8_t* data,uint8_t size);
  107. /* USER CODE END PFP */
  108. /* Private user code ---------------------------------------------------------*/
  109. /* USER CODE BEGIN 0 */
  110. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  111. {
  112. if(huart->Instance == USART1)//RGB Comunication
  113. {
  114. buf1[count_in1] = rx1_data[0];//(uint8_t)USART2->DR;
  115. if(++count_in1>=buf_size){ count_in1 = 0; }
  116. // printf("data %02x \r\n",rx1_data[0]);
  117. #if 0 // PYJ.2019.04.19_BEGIN --
  118. if(buf[count_in1++] == 0xEB){
  119. if(buf[bluecell_length] == (count_in1 - 3))
  120. UartDataRecvSet(1);
  121. else
  122. count_in1 = 0;
  123. }
  124. #endif // PYJ.2019.04.19_END --
  125. // HAL_UART_Receive_IT(&huart1,&rx1_data[0],1);
  126. }
  127. if(huart->Instance == USART2) // Lora?? ?? Â???¹Â???¢Ë??Å ?? ?Â�¬?Џ
  128. {
  129. buf2[count_in2] = rx2_data[0];//(uint8_t)USART2->DR;
  130. if(++count_in2>=buf_size){ count_in2 = 0; }
  131. // if(buf[count_in++] == 0xEB)UartDataRecvSet(1);
  132. #if 0 // PYJ.2019.04.19_BEGIN --
  133. if(buf[count_in2++] == 0xEB){
  134. if(buf[bluecell_length] == (count_in2 - 3))
  135. UartDataRecvSet(2);
  136. else
  137. count_in1 = 0;
  138. // printf("UART 2 %d",((count_in2 -1) - 3));
  139. }
  140. #endif // PYJ.2019.04.19_END --
  141. HAL_UART_Receive_IT(&huart2,&rx2_data[0],1);
  142. }
  143. #if 0 // PYJ.2019.04.13_BEGIN --
  144. if(huart->Instance == USART3) //GUI ?? ?? Â???¹Â???¢Ë??Å ?? Port
  145. {
  146. buf[count_in3] = rx3_data[0];//(uint8_t)USART2->DR;
  147. if(buf[count_in3++] == 0xEB)UartDataRecvSet(3);
  148. /*ring_buf[count_in] = rx2_data[0];//(uint8_t)USART2->DR;
  149. if(++count_in>=buf_size) count_in=0;*/
  150. HAL_UART_Receive_IT(&huart3,&rx3_data[0],1);
  151. }
  152. #endif // PYJ.2019.04.13_END --
  153. }
  154. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  155. {
  156. if(htim->Instance == TIM6){
  157. UartTimerCnt++;
  158. LedTimerCnt++;
  159. LoraTxTimerCnt++;
  160. LoraAckTimerCnt++;
  161. }
  162. }
  163. void LoraDataSendSet(uint8_t val){
  164. LoraDataSend = val;
  165. }
  166. uint8_t LoraDataSendGet(void){
  167. return LoraDataSend;
  168. }
  169. void UartDataRecvSet(uint8_t val){
  170. UartDataisReved = val;
  171. }
  172. uint8_t UartDataRecvGet(void){
  173. return UartDataisReved;
  174. }
  175. void RGB_SensorIDAutoSet(uint8_t set){
  176. RGB_SensorIDAutoset = set;
  177. }
  178. uint8_t RGB_SensorIDAutoGet(void){
  179. return RGB_SensorIDAutoset;
  180. }
  181. void Uart2_Data_Send(uint8_t* data,uint8_t size){
  182. HAL_UART_Transmit(&huart2, data,size, 10);
  183. }
  184. void Uart1_Data_Send(uint8_t* data,uint8_t size){
  185. HAL_UART_Transmit(&huart1, data,size, 10);
  186. }
  187. int _write (int file, uint8_t *ptr, uint16_t len)
  188. {
  189. HAL_UART_Transmit (&huart1, ptr, len, 10);
  190. return len;
  191. }
  192. #if 0 // PYJ.2019.04.19_BEGIN --
  193. void Uart_dataCheck(uint8_t* cnt){
  194. etError crccheck = 0;
  195. #if 0
  196. for(uint8_t i = 0; i < (* cnt); i++){
  197. printf("%02x ",buf[i]);
  198. }
  199. printf("\r\n");
  200. #endif
  201. crccheck = STH30_CheckCrc(&buf[bluecell_type],buf[bluecell_length],buf[buf[bluecell_length] + 1]);
  202. if(crccheck == CHECKSUM_ERROR){
  203. for(uint8_t i = 0; i < (*cnt); i++){
  204. printf("%02x ",buf[i]);
  205. }
  206. printf("Original CRC : %02x RecvCRC : %02x \r\n",crccheck,buf[buf[bluecell_length] + 1]);
  207. }
  208. else if(crccheck == NO_ERROR){
  209. RGB_Controller_Func(&buf[bluecell_stx]);
  210. }
  211. else{
  212. printf("What Happen?\r\n");
  213. /*NOP*/
  214. }
  215. *cnt = 0;
  216. memset(buf,0x00,buf_size);
  217. }
  218. #else
  219. void Uart_dataCheck(uint8_t* Que_Buf,uint8_t* cnt){
  220. etError crccheck = 0;
  221. #if 0
  222. for(uint8_t i = 0; i < (* cnt); i++){
  223. printf("%02x ",*Que_Buf[i]);
  224. }
  225. printf("\r\n");
  226. #endif
  227. crccheck = STH30_CheckCrc(&Que_Buf[bluecell_type],Que_Buf[bluecell_length],Que_Buf[Que_Buf[bluecell_length] + 1]);
  228. if(crccheck == CHECKSUM_ERROR){
  229. for(uint8_t i = 0; i < (*cnt); i++){
  230. printf("%02x ",Que_Buf[i]);
  231. }
  232. printf("Original CRC : %02x RecvCRC : %02x \r\n",crccheck,Que_Buf[Que_Buf[bluecell_length] + 1]);
  233. }
  234. else if(crccheck == NO_ERROR){
  235. RGB_Controller_Func(&Que_Buf[bluecell_stx]);
  236. }
  237. else{
  238. printf("What Happen?\r\n");
  239. /*NOP*/
  240. }
  241. //*cnt = 0;
  242. memset(Que_Buf,0x00,buf_size);
  243. }
  244. #endif // PYJ.2019.04.19_END --
  245. void RGB_Sensor_PowerOnOff(uint8_t id){
  246. uint8_t SensorSerchStart_cmd[5] = {0xbe,RGB_Controller_Init_Start,2,STH30_CreateCrc(&SensorSerchStart_cmd[bluecell_type],SensorSerchStart_cmd[bluecell_length]),0xeb};
  247. uint8_t SensorSerchEnd_cmd[5] = {0xbe,RGB_Controller_Init_End,2,STH30_CreateCrc(&SensorSerchEnd_cmd[bluecell_type],SensorSerchEnd_cmd[bluecell_length]),0xeb};
  248. // printf("%d Power ON \r\n",id);
  249. switch(id){
  250. case 0:
  251. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  252. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_SET);
  253. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_SET);
  254. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_SET);
  255. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_SET);
  256. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_SET);
  257. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_SET);
  258. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_SET);
  259. break;
  260. case 1:
  261. Uart1_Data_Send(&SensorSerchStart_cmd[bluecell_stx], SensorSerchStart_cmd[bluecell_length] + 3);
  262. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_RESET);
  263. HAL_Delay(50);
  264. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  265. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_RESET);
  266. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_RESET);
  267. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_RESET);
  268. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_RESET);
  269. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_RESET);
  270. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_RESET);
  271. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_RESET);
  272. break;
  273. case 2:
  274. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  275. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_SET);
  276. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_RESET);
  277. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_RESET);
  278. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_RESET);
  279. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_RESET);
  280. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_RESET);
  281. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_RESET);
  282. break;
  283. case 3:
  284. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  285. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_SET);
  286. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_SET);
  287. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_RESET);
  288. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_RESET);
  289. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_RESET);
  290. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_RESET);
  291. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_RESET);
  292. break;
  293. case 4:
  294. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  295. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_SET);
  296. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_SET);
  297. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_SET);
  298. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_RESET);
  299. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_RESET);
  300. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_RESET);
  301. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_RESET);
  302. break;
  303. case 5:
  304. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  305. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_SET);
  306. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_SET);
  307. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_SET);
  308. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_SET);
  309. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_RESET);
  310. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_RESET);
  311. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_RESET);
  312. break;
  313. case 6:
  314. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  315. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_SET);
  316. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_SET);
  317. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_SET);
  318. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_SET);
  319. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_SET);
  320. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_RESET);
  321. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_RESET);
  322. break;
  323. case 7:
  324. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  325. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_SET);
  326. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_SET);
  327. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_SET);
  328. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_SET);
  329. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_SET);
  330. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_SET);
  331. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_RESET);
  332. break;
  333. case 8:
  334. Uart1_Data_Send(&SensorSerchEnd_cmd[bluecell_stx], SensorSerchEnd_cmd[bluecell_length] + 3);
  335. HAL_GPIO_WritePin(SENSOR_EN1_GPIO_Port,SENSOR_EN1_Pin,GPIO_PIN_SET);
  336. HAL_GPIO_WritePin(SENSOR_EN2_GPIO_Port,SENSOR_EN2_Pin,GPIO_PIN_SET);
  337. HAL_GPIO_WritePin(SENSOR_EN3_GPIO_Port,SENSOR_EN3_Pin,GPIO_PIN_SET);
  338. HAL_GPIO_WritePin(SENSOR_EN4_GPIO_Port,SENSOR_EN4_Pin,GPIO_PIN_SET);
  339. HAL_GPIO_WritePin(SENSOR_EN5_GPIO_Port,SENSOR_EN5_Pin,GPIO_PIN_SET);
  340. HAL_GPIO_WritePin(SENSOR_EN6_GPIO_Port,SENSOR_EN6_Pin,GPIO_PIN_SET);
  341. HAL_GPIO_WritePin(SENSOR_EN7_GPIO_Port,SENSOR_EN7_Pin,GPIO_PIN_SET);
  342. HAL_GPIO_WritePin(SENSOR_EN8_GPIO_Port,SENSOR_EN8_Pin,GPIO_PIN_SET);
  343. break;
  344. }
  345. }
  346. #define StartAddr ((uint32_t)0x08030000)
  347. #if 1 // PYJ.2019.03.19_BEGIN --
  348. //----------------------------------------------------
  349. #define FLASH_USER StartAddr
  350. #define START_ADDR FLASH_USER
  351. #define END_ADDR FLASH_USER + 262144 // 256K
  352. //----------------------------------------------------
  353. #if 0 // PYJ.2019.03.20_BEGIN --
  354. void test_write() // ?“°ê¸°í•¨?ˆ˜
  355. {
  356. __HAL_RCC_TIM7_CLK_DISABLE(); // 매ì�¸???�´ë¨¸ë?? ? •ì§??•©?‹ˆ?‹¤
  357. uint32_t Address = 0;
  358. Address = StartAddr;
  359. // printf("================First============ \r\n");
  360. // for(uint8_t i=0;i<16;i++)
  361. // {
  362. // printf("%08x: %X\r\n", Address, *(uint32_t*)Address);
  363. // Address += 4;
  364. // }
  365. // HAL_FLASH_Unlock(); // lock ??�
  366. // HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, START_ADDR, (uint32_t)0x12345678); //test
  367. // HAL_FLASH_Lock(); // lock ?ž ê·¸ê¸°
  368. // __HAL_RCC_TIM7_CLK_ENABLE(); // 매ì�¸???�´ë¨¸ë?? ?ž¬?‹œ?ž‘?•©?‹ˆ?‹¤
  369. Address = StartAddr;
  370. printf("================Second============ \r\n");
  371. //while(Address < 0x0803FFFF)
  372. for(uint16_t i = 0; i<37273 ; i++)
  373. {
  374. printf("%02X", *(uint8_t*)Address);
  375. Address ++;
  376. }
  377. printf("%08x:",Address);
  378. }
  379. #endif // PYJ.2019.03.20_END --
  380. #define DATA_16_1 ((uint32_t)0x1234)
  381. #define DATA_16_2 ((uint32_t)0x5678)
  382. #if 1 // PYJ.2019.03.20_BEGIN --
  383. void test_read(void) // ?“°ê¸°í•¨?ˆ˜
  384. {
  385. uint32_t Address = 0x08000000;
  386. uint8_t aa = 0;
  387. for(uint32_t i = Address; i <= Address + 0x35d8; i++ ){
  388. printf("%02X ",*(uint8_t*)i);
  389. aa++;
  390. if(aa > 15){
  391. printf("\n");
  392. aa= 0;
  393. }
  394. }
  395. }
  396. #endif // PYJ.2019.03.20_END --
  397. #define ADDR_FLASH_PAGE_TEST ((uint32_t)0x08030000) /* Base @ of Page 127, 1 Kbytes */
  398. #define FLASH_USER_START_ADDR ADDR_FLASH_PAGE_TEST /* Start @ of user Flash area */
  399. #define FLASH_USER_END_ADDR ADDR_FLASH_PAGE_TEST + ((uint32_t)0x0000FFFF) /* End @ of user Flash area */
  400. void Flash_RGB_Data_Write(uint32_t Addr,uint8_t* data){
  401. uint16_t temp_Red = 0,temp_Green = 0,temp_Blue = 0;
  402. temp_Red = ((data[bluecell_red_H] << 8) |data[bluecell_red_L]); //R
  403. temp_Green= ((data[bluecell_green_H] << 8) |data[bluecell_green_L]); //G
  404. temp_Blue = ((data[bluecell_blue_H] << 8) |data[bluecell_blue_L]); //B
  405. HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Addr + 0 , (uint16_t)temp_Red);
  406. HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Addr + 2 , (uint16_t)temp_Green);
  407. HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Addr + 4 , (uint16_t)temp_Blue);
  408. }
  409. void Flash_write(uint8_t* data) // ?“°ê¸°í•¨?ˆ˜
  410. {
  411. /*Variable used for Erase procedure*/
  412. // static FLASH_EraseInitTypeDef EraseInitStruct;
  413. uint32_t Address = 0;//, PAGEError = 0;
  414. /* Fill EraseInit structure*/
  415. // EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  416. // EraseInitStruct.PageAddress = FLASH_USER_START_ADDR;
  417. // EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
  418. Address = START_ADDR;
  419. __HAL_RCC_TIM7_CLK_DISABLE(); // 매ì�¸???�´ë¨¸ë?? ? •ì§??•©?‹ˆ?‹¤
  420. HAL_FLASH_Unlock(); // lock ??�
  421. // if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){
  422. // printf("Erase Failed \r\n");
  423. // }else{
  424. // printf("Erase Success \r\n");
  425. // }
  426. switch(data[bluecell_dstid]){
  427. case 1:
  428. Address += 0;
  429. break;
  430. case 2:
  431. Address += 6;
  432. break;
  433. case 3:
  434. Address += 12;
  435. break;
  436. case 4:
  437. Address += 18;
  438. break;
  439. case 5:
  440. Address += 24;
  441. break;
  442. case 6:
  443. Address += 30;
  444. break;
  445. case 7:
  446. Address += 36;
  447. break;
  448. case 8:
  449. Address += 42;
  450. break;
  451. }
  452. Flash_RGB_Data_Write(Address,&data[bluecell_stx]);
  453. HAL_FLASH_Lock(); // lock ?ž ê·¸ê¸°
  454. __HAL_RCC_TIM7_CLK_ENABLE(); // 매ì�¸???�´ë¨¸ë?? ?ž¬?‹œ?ž‘?•©?‹ˆ?‹¤
  455. }
  456. void Flash_InitRead(void) // ?“°ê¸°í•¨?ˆ˜
  457. {
  458. uint32_t Address = 0;
  459. Address = StartAddr;
  460. for(uint8_t i = 1; i <= 8; i++ ){
  461. RGB_SensorRedLimit_Buf[i] = (*(uint16_t*)Address);
  462. // printf("%08x : %04X \n",Address ,*(uint16_t*)Address);
  463. Address += 2;
  464. RGB_SensorGreenLimit_Buf[i] = (*(uint16_t*)Address);
  465. // printf("%08x : %04X \n",Address ,*(uint16_t*)Address);
  466. Address += 2;
  467. RGB_SensorBlueLimit_Buf[i] = (*(uint16_t*)Address);
  468. // printf("%08x : %04X \n",Address ,*(uint16_t*)Address);
  469. Address += 2;
  470. }
  471. }
  472. #endif // PYJ.2019.03.19_END --
  473. Default_SX1276_t Default_SX1276 =
  474. {
  475. SX1276_917MHZ,
  476. SX1276_POWER_17DBM,
  477. SX1276_LORA_SF_8,
  478. SX1276_LORA_BW_20_8KHZ,
  479. 10,
  480. SX1276_LORA_G1,
  481. SX1276_15dBm,
  482. };
  483. int master;
  484. int ret;
  485. char buffer[100];
  486. int message_length;
  487. int message;
  488. void Lora_Initialize(void){
  489. SX1276_hw.dio0.port = SX1276_DIO0_GPIO_Port;
  490. SX1276_hw.dio0.pin = SX1276_DIO0_Pin;
  491. SX1276_hw.nss.port = GPIOA;
  492. SX1276_hw.nss.pin = GPIO_PIN_15;
  493. SX1276_hw.reset.port = SX1276_RESET_GPIO_Port;
  494. SX1276_hw.reset.pin = SX1276_RESET_Pin;
  495. // SX1276_hw.spi = &hspi3;
  496. SX1276.hw = &SX1276_hw;
  497. // printf("Configuring LoRa module\r\n");
  498. SX1276_begin(&SX1276, Default_SX1276.frequency, Default_SX1276.power,
  499. Default_SX1276.LoRa_Rate,Default_SX1276.LoRa_BW, 10,Default_SX1276.LoRa_Lna,Default_SX1276.LoRa_Pa_boost);
  500. // printf("Done configuring LoRaModule\r\n");
  501. master = 0;
  502. if (master == 1) {
  503. ret = SX1276_LoRaEntryTx(&SX1276, LORA_MAX_DATA_CNT, 2000);
  504. } else {
  505. ret = SX1276_LoRaEntryRx(&SX1276, LORA_MAX_DATA_CNT, 2000);
  506. }
  507. }
  508. /* USER CODE END 0 */
  509. /**
  510. * @brief The application entry point.
  511. * @retval int
  512. */
  513. int main(void)
  514. {
  515. /* USER CODE BEGIN 1 */
  516. uint8_t SensorSerchStart_cmd[5] = {0xbe,RGB_Controller_Init_Start,2,STH30_CreateCrc(&SensorSerchStart_cmd[bluecell_type],SensorSerchStart_cmd[bluecell_length]),0xeb};
  517. uint8_t StatusRequest_data[RGB_SensorDataRequest_Length] = {0xbe,RGB_Status_Data_Request,RGB_SensorDataRequest_Length - 3,MyControllerID,SensorID,STH30_CreateCrc(&StatusRequest_data[bluecell_type],StatusRequest_data[bluecell_length]),0xeb};
  518. uint8_t IDAutoSetRequest_data[RGB_SensorIDAutoSetRequest_Length] = {0xbe,RGB_SensorID_SET,RGB_SensorIDAutoSetRequest_Length - 3,MyControllerID,SensorID,STH30_CreateCrc(&IDAutoSetRequest_data[bluecell_type],IDAutoSetRequest_data[bluecell_length]),0xeb};
  519. uint8_t cnt1 = 0,cnt2=0,uartdatarecv = 0;
  520. uint8_t data1[100]= {0,};
  521. uint8_t data2[100]= {0,};
  522. /* USER CODE END 1 */
  523. /* MCU Configuration--------------------------------------------------------*/
  524. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  525. HAL_Init();
  526. /* USER CODE BEGIN Init */
  527. /* USER CODE END Init */
  528. /* Configure the system clock */
  529. SystemClock_Config();
  530. /* USER CODE BEGIN SysInit */
  531. /* USER CODE END SysInit */
  532. /* Initialize all configured peripherals */
  533. MX_GPIO_Init();
  534. MX_DMA_Init();
  535. MX_TIM6_Init();
  536. MX_USART1_UART_Init();
  537. MX_USART2_UART_Init();
  538. MX_I2C2_Init();
  539. /* Initialize interrupts */
  540. MX_NVIC_Init();
  541. /* USER CODE BEGIN 2 */
  542. HAL_TIM_Base_Start_IT(&htim6);
  543. RGB_SensorIDAutoSet(1);
  544. HAL_UART_Receive_DMA(&huart1, rx1_data, 1);
  545. HAL_UART_Receive_IT(&huart2, &rx2_data[0],1);
  546. setbuf(stdout, NULL); // \n ?�„ ? �?�„ ?–„ë§?
  547. Uart1_Data_Send(&SensorSerchStart_cmd[bluecell_stx], SensorSerchStart_cmd[bluecell_length] + 3);
  548. #if 0 // PYJ.2019.03.04_BEGIN --
  549. printf("****************************************\r\n");
  550. printf("RGB Project\r\n");
  551. printf("Build at %s %s\r\n", __DATE__, __TIME__);
  552. printf("Copyright (c) 2019. BLUECELL\r\n");
  553. printf("****************************************\r\n");
  554. #endif // PYJ.2019.03.04_END --
  555. Flash_InitRead();
  556. RGB_Data_Init();
  557. Lora_Initialize();
  558. /* USER CODE END 2 */
  559. /* Infinite loop */
  560. /* USER CODE BEGIN WHILE */
  561. while (1)
  562. {
  563. if(LoraTxTimerCnt > LORA_TIMER_CNT){
  564. LoraTxTimerCnt = 0;
  565. // LoraDataSendSet(1);
  566. }
  567. RGB_Alarm_Operate();//LED ALARM CHECK
  568. if(LoraDataSendGet() == LoraTx_mode){
  569. // LoraDataSendSet(LoraRx_mode);
  570. memcpy(&buffer[0],&Lora_Buf[0],LORA_MAX_DATA_CNT);
  571. message_length = Lora_Max_Amount + 3;////RGB Data 60byte + stx + etx + crc
  572. ret = SX1276_LoRaEntryTx(&SX1276, message_length, 2000);
  573. ret = SX1276_LoRaTxPacket(&SX1276, &buffer[0], message_length, 2000);
  574. // printf("Tx buffer : ");
  575. // for(uint8_t i = 0; i < sizeof(LoraDataRequest_t); i++)
  576. // printf("%02x ",buffer[i]);
  577. // printf("\n");
  578. LoraDataSendSet(LoraRx_mode);
  579. ret = SX1276_LoRaEntryRx(&SX1276, LORA_MAX_DATA_CNT, 2000);
  580. }else{
  581. ret = SX1276_LoRaRxPacket(&SX1276);
  582. if (ret > 0) {
  583. SX1276_read(&SX1276, &buffer[0], ret);
  584. Uart_dataCheck(&buffer[bluecell_stx],&ret);
  585. // printf("Received Data : ");
  586. // for(uint8_t i = 0; i < ret; i++)
  587. // printf("%02x ", buffer[i]);
  588. // printf("\n");
  589. }
  590. }
  591. if(count_in1 != count_out1){ // <-------
  592. data1[cnt1++] = buf1[count_out1++];
  593. if(count_out1 >= 100){ count_out1 = 0; }
  594. UartTimerCnt = 0;
  595. UartDataRecvSet(1);
  596. }
  597. if(count_in2 != count_out2){ // <-------
  598. data2[cnt2++] = buf2[count_out2++];
  599. if(count_out2 >= 100){ count_out2 = 0; }
  600. UartTimerCnt = 0;
  601. UartDataRecvSet(2);
  602. }
  603. uartdatarecv = UartDataRecvGet();
  604. if(uartdatarecv == 1 && UartTimerCnt > 100){
  605. cnt1 = 0;
  606. UartDataRecvSet(0);
  607. Uart_dataCheck(&data1[0],&count_in1);
  608. memset(&data1[0],0,100);
  609. }
  610. if(uartdatarecv == 2 && UartTimerCnt > 100){
  611. cnt2 = 0;
  612. UartDataRecvSet(0);
  613. Uart_dataCheck(&data2[0],&count_in2);
  614. memset(&data2[0],0,100);
  615. }
  616. if(LedTimerCnt > 500){
  617. if(RGB_SensorIDAutoGet() == 1){
  618. if(SensorID == 0){memset(&SensorID_buf[0],0x00,8);SensorID_Cnt = 0;}
  619. IDAutoSetRequest_data[bluecell_srcid + 1] = ++SensorID;//DST ID
  620. if(IDAutoSetRequest_data[bluecell_srcid + 1] > 8){
  621. RGB_SensorIDAutoSet(0);
  622. RGB_Sensor_PowerOnOff(0);
  623. SensorID = 0;
  624. }else{
  625. RGB_Sensor_PowerOnOff(IDAutoSetRequest_data[4]);
  626. HAL_Delay(100);
  627. RGB_Controller_Func(&IDAutoSetRequest_data[bluecell_stx]);
  628. }
  629. }
  630. /* else{
  631. // RGB_Controller_Func(&StatusRequest_data[bluecell_stx]);
  632. }*/
  633. HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_15);
  634. LedTimerCnt = 0;
  635. }
  636. /* USER CODE END WHILE */
  637. /* USER CODE BEGIN 3 */
  638. }
  639. /* USER CODE END 3 */
  640. }
  641. /**
  642. * @brief System Clock Configuration
  643. * @retval None
  644. */
  645. void SystemClock_Config(void)
  646. {
  647. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  648. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  649. /**Initializes the CPU, AHB and APB busses clocks
  650. */
  651. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  652. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  653. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  654. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  655. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  656. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  657. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
  658. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  659. {
  660. Error_Handler();
  661. }
  662. /**Initializes the CPU, AHB and APB busses clocks
  663. */
  664. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  665. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  666. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  667. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  668. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  669. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  670. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  671. {
  672. Error_Handler();
  673. }
  674. }
  675. /**
  676. * @brief NVIC Configuration.
  677. * @retval None
  678. */
  679. static void MX_NVIC_Init(void)
  680. {
  681. /* DMA1_Channel5_IRQn interrupt configuration */
  682. HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
  683. HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
  684. /* USART1_IRQn interrupt configuration */
  685. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  686. HAL_NVIC_EnableIRQ(USART1_IRQn);
  687. /* USART2_IRQn interrupt configuration */
  688. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  689. HAL_NVIC_EnableIRQ(USART2_IRQn);
  690. /* TIM6_IRQn interrupt configuration */
  691. HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0);
  692. HAL_NVIC_EnableIRQ(TIM6_IRQn);
  693. }
  694. /**
  695. * @brief I2C2 Initialization Function
  696. * @param None
  697. * @retval None
  698. */
  699. static void MX_I2C2_Init(void)
  700. {
  701. /* USER CODE BEGIN I2C2_Init 0 */
  702. /* USER CODE END I2C2_Init 0 */
  703. /* USER CODE BEGIN I2C2_Init 1 */
  704. /* USER CODE END I2C2_Init 1 */
  705. hi2c2.Instance = I2C2;
  706. hi2c2.Init.ClockSpeed = 100000;
  707. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  708. hi2c2.Init.OwnAddress1 = 0;
  709. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  710. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  711. hi2c2.Init.OwnAddress2 = 0;
  712. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  713. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  714. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  715. {
  716. Error_Handler();
  717. }
  718. /* USER CODE BEGIN I2C2_Init 2 */
  719. /* USER CODE END I2C2_Init 2 */
  720. }
  721. /**
  722. * @brief TIM6 Initialization Function
  723. * @param None
  724. * @retval None
  725. */
  726. static void MX_TIM6_Init(void)
  727. {
  728. /* USER CODE BEGIN TIM6_Init 0 */
  729. /* USER CODE END TIM6_Init 0 */
  730. TIM_MasterConfigTypeDef sMasterConfig = {0};
  731. /* USER CODE BEGIN TIM6_Init 1 */
  732. /* USER CODE END TIM6_Init 1 */
  733. htim6.Instance = TIM6;
  734. htim6.Init.Prescaler = 1600-1;
  735. htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  736. htim6.Init.Period = 10-1;
  737. htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  738. if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  739. {
  740. Error_Handler();
  741. }
  742. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  743. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  744. if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  745. {
  746. Error_Handler();
  747. }
  748. /* USER CODE BEGIN TIM6_Init 2 */
  749. /* USER CODE END TIM6_Init 2 */
  750. }
  751. /**
  752. * @brief USART1 Initialization Function
  753. * @param None
  754. * @retval None
  755. */
  756. static void MX_USART1_UART_Init(void)
  757. {
  758. /* USER CODE BEGIN USART1_Init 0 */
  759. /* USER CODE END USART1_Init 0 */
  760. /* USER CODE BEGIN USART1_Init 1 */
  761. /* USER CODE END USART1_Init 1 */
  762. huart1.Instance = USART1;
  763. huart1.Init.BaudRate = 115200;
  764. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  765. huart1.Init.StopBits = UART_STOPBITS_1;
  766. huart1.Init.Parity = UART_PARITY_NONE;
  767. huart1.Init.Mode = UART_MODE_TX_RX;
  768. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  769. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  770. if (HAL_UART_Init(&huart1) != HAL_OK)
  771. {
  772. Error_Handler();
  773. }
  774. /* USER CODE BEGIN USART1_Init 2 */
  775. /* USER CODE END USART1_Init 2 */
  776. }
  777. /**
  778. * @brief USART2 Initialization Function
  779. * @param None
  780. * @retval None
  781. */
  782. static void MX_USART2_UART_Init(void)
  783. {
  784. /* USER CODE BEGIN USART2_Init 0 */
  785. /* USER CODE END USART2_Init 0 */
  786. /* USER CODE BEGIN USART2_Init 1 */
  787. /* USER CODE END USART2_Init 1 */
  788. huart2.Instance = USART2;
  789. huart2.Init.BaudRate = 115200;
  790. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  791. huart2.Init.StopBits = UART_STOPBITS_1;
  792. huart2.Init.Parity = UART_PARITY_NONE;
  793. huart2.Init.Mode = UART_MODE_TX_RX;
  794. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  795. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  796. if (HAL_UART_Init(&huart2) != HAL_OK)
  797. {
  798. Error_Handler();
  799. }
  800. /* USER CODE BEGIN USART2_Init 2 */
  801. /* USER CODE END USART2_Init 2 */
  802. }
  803. /**
  804. * Enable DMA controller clock
  805. */
  806. static void MX_DMA_Init(void)
  807. {
  808. /* DMA controller clock enable */
  809. __HAL_RCC_DMA1_CLK_ENABLE();
  810. }
  811. /**
  812. * @brief GPIO Initialization Function
  813. * @param None
  814. * @retval None
  815. */
  816. static void MX_GPIO_Init(void)
  817. {
  818. GPIO_InitTypeDef GPIO_InitStruct = {0};
  819. /* GPIO Ports Clock Enable */
  820. __HAL_RCC_GPIOC_CLK_ENABLE();
  821. __HAL_RCC_GPIOD_CLK_ENABLE();
  822. __HAL_RCC_GPIOA_CLK_ENABLE();
  823. __HAL_RCC_GPIOB_CLK_ENABLE();
  824. /*Configure GPIO pin Output Level */
  825. HAL_GPIO_WritePin(GPIOC, BOOT_LED_Pin|SX1276_DIO4_Pin|SX1276_DIO5_Pin|SENSOR_EN4_Pin
  826. |SENSOR_EN5_Pin|SENSOR_EN6_Pin|SENSOR_EN7_Pin|LED_CH1_Pin
  827. |LED_CH2_Pin|LED_CH3_Pin, GPIO_PIN_RESET);
  828. /*Configure GPIO pin Output Level */
  829. HAL_GPIO_WritePin(GPIOA, SX1276_DIO0_Pin|SX1276_DIO1_Pin|SX1276_DIO2_Pin|SX1276_DIO3_Pin
  830. |SENSOR_EN8_Pin|SX1276_NSS_Pin, GPIO_PIN_RESET);
  831. /*Configure GPIO pin Output Level */
  832. HAL_GPIO_WritePin(GPIOB, SX1276_RESET_Pin|LED_ALARM_Pin|SENSOR_EN1_Pin|SENSOR_EN2_Pin
  833. |SENSOR_EN3_Pin|SX1276_CLK_Pin|SX1276_MOSI_Pin|LED_CH5_Pin
  834. |LED_CH6_Pin|LED_CH7_Pin|LED_CH8_Pin, GPIO_PIN_RESET);
  835. /*Configure GPIO pin Output Level */
  836. HAL_GPIO_WritePin(LED_CH4_GPIO_Port, LED_CH4_Pin, GPIO_PIN_RESET);
  837. /*Configure GPIO pins : BOOT_LED_Pin SX1276_DIO4_Pin SX1276_DIO5_Pin SENSOR_EN4_Pin
  838. SENSOR_EN5_Pin SENSOR_EN6_Pin SENSOR_EN7_Pin LED_CH1_Pin
  839. LED_CH2_Pin LED_CH3_Pin */
  840. GPIO_InitStruct.Pin = BOOT_LED_Pin|SX1276_DIO4_Pin|SX1276_DIO5_Pin|SENSOR_EN4_Pin
  841. |SENSOR_EN5_Pin|SENSOR_EN6_Pin|SENSOR_EN7_Pin|LED_CH1_Pin
  842. |LED_CH2_Pin|LED_CH3_Pin;
  843. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  844. GPIO_InitStruct.Pull = GPIO_NOPULL;
  845. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  846. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  847. /*Configure GPIO pins : SX1276_DIO0_Pin SX1276_DIO1_Pin SX1276_DIO2_Pin SX1276_DIO3_Pin
  848. SENSOR_EN8_Pin SX1276_NSS_Pin */
  849. GPIO_InitStruct.Pin = SX1276_DIO0_Pin|SX1276_DIO1_Pin|SX1276_DIO2_Pin|SX1276_DIO3_Pin
  850. |SENSOR_EN8_Pin|SX1276_NSS_Pin;
  851. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  852. GPIO_InitStruct.Pull = GPIO_NOPULL;
  853. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  854. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  855. /*Configure GPIO pins : SX1276_RESET_Pin LED_ALARM_Pin SENSOR_EN1_Pin SENSOR_EN2_Pin
  856. SENSOR_EN3_Pin SX1276_CLK_Pin SX1276_MOSI_Pin LED_CH5_Pin
  857. LED_CH6_Pin LED_CH7_Pin LED_CH8_Pin */
  858. GPIO_InitStruct.Pin = SX1276_RESET_Pin|LED_ALARM_Pin|SENSOR_EN1_Pin|SENSOR_EN2_Pin
  859. |SENSOR_EN3_Pin|SX1276_CLK_Pin|SX1276_MOSI_Pin|LED_CH5_Pin
  860. |LED_CH6_Pin|LED_CH7_Pin|LED_CH8_Pin;
  861. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  862. GPIO_InitStruct.Pull = GPIO_NOPULL;
  863. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  864. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  865. /*Configure GPIO pin : LED_CH4_Pin */
  866. GPIO_InitStruct.Pin = LED_CH4_Pin;
  867. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  868. GPIO_InitStruct.Pull = GPIO_NOPULL;
  869. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  870. HAL_GPIO_Init(LED_CH4_GPIO_Port, &GPIO_InitStruct);
  871. /*Configure GPIO pin : SX1276_MISO_Pin */
  872. GPIO_InitStruct.Pin = SX1276_MISO_Pin;
  873. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  874. GPIO_InitStruct.Pull = GPIO_NOPULL;
  875. HAL_GPIO_Init(SX1276_MISO_GPIO_Port, &GPIO_InitStruct);
  876. }
  877. /* USER CODE BEGIN 4 */
  878. /* USER CODE END 4 */
  879. /**
  880. * @brief This function is executed in case of error occurrence.
  881. * @retval None
  882. */
  883. void Error_Handler(void)
  884. {
  885. /* USER CODE BEGIN Error_Handler_Debug */
  886. /* User can add his own implementation to report the HAL error return state */
  887. /* USER CODE END Error_Handler_Debug */
  888. }
  889. #ifdef USE_FULL_ASSERT
  890. /**
  891. * @brief Reports the name of the source file and the source line number
  892. * where the assert_param error has occurred.
  893. * @param file: pointer to the source file name
  894. * @param line: assert_param error line source number
  895. * @retval None
  896. */
  897. void assert_failed(uint8_t *file, uint32_t line)
  898. {
  899. /* USER CODE BEGIN 6 */
  900. /* User can add his own implementation to report the file name and line number,
  901. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  902. /* USER CODE END 6 */
  903. }
  904. #endif /* USE_FULL_ASSERT */
  905. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/