main.c 36 KB

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