main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2017 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "main.h"
  35. #include "stm32f0xx_hal.h"
  36. /* USER CODE BEGIN Includes */
  37. #define CARDS 3
  38. int32_t reader_id[CARDS] = {2, 4, 6};
  39. uint32_t card_number[CARDS];
  40. uint32_t started_at[CARDS];
  41. #define NET_DATBUFSIZE (1<<6)
  42. #include <string.h>
  43. #define ETH_DISABLED 0
  44. #define ENC28J60 1
  45. #define WIZNET 2
  46. //#define ETH_MODULE ETH_DISABLED
  47. //#define ETH_MODULE ENC28J60
  48. //#define ETH_MODULE WIZNET
  49. #if ETH_MODULE == ENC28J60
  50. # include "EtherShield.h"
  51. #endif
  52. #if ETH_MODULE == WIZNET
  53. # define _WIZCHIP_ 5500
  54. # include "wizchip_conf.h"
  55. # include "socket.h"
  56. //# define WIZNET_BURST
  57. //# define SPI_DMA
  58. # ifdef SPI_DMA
  59. # define HAL_SPI_Receive_Auto(a, b, c) HAL_SPI_Receive_DMA(a, b, c)
  60. # define HAL_SPI_Transmit_Auto(a, b, c) HAL_SPI_Transmit_DMA(a, b, c)
  61. # else
  62. # define HAL_SPI_Receive_Auto(a, b, c) HAL_SPI_Receive(a, b, c, 0xffffffff)
  63. # define HAL_SPI_Transmit_Auto(a, b, c) HAL_SPI_Transmit(a, b, c, 0xffffffff)
  64. # endif
  65. # define NET_LOCAL_PORT 30320
  66. # define NET_REMOTE_PORT 30320
  67. #endif
  68. #define error(...) {Error_Handler();}
  69. /* USER CODE END Includes */
  70. /* Private variables ---------------------------------------------------------*/
  71. SPI_HandleTypeDef hspi1;
  72. /* USER CODE BEGIN PV */
  73. /* Private variables ---------------------------------------------------------*/
  74. uint32_t i=0;
  75. /* USER CODE END PV */
  76. /* Private function prototypes -----------------------------------------------*/
  77. void SystemClock_Config(void);
  78. void Error_Handler(void);
  79. static void MX_GPIO_Init(void);
  80. static void MX_SPI1_Init(void);
  81. /* USER CODE BEGIN PFP */
  82. /* Private function prototypes -----------------------------------------------*/
  83. #if ETH_MODULE != ETH_DISABLED
  84. static uint8_t remote_mac[] = {0xf2, 0x00, 0x00, 0x0a, 0x00, 0x00};
  85. static uint8_t remote_ip[] = {10, 180, 239, 1};
  86. #endif
  87. #if ETH_MODULE == ENC28J60
  88. # define NET_HEADERS_LENGTH (ETH_HEADER_LEN + IP_HEADER_LEN + UDP_HEADER_LEN)
  89. # define NET_BUFSIZE (NET_DATBUFSIZE + NET_HEADERS_LENGTH)
  90. # define net_senddatbuf ((uint8_t *)(&net_sendbuf[NET_HEADERS_LENGTH]))
  91. #else
  92. # define NET_BUFSIZE NET_DATBUFSIZE
  93. # define net_senddatbuf net_sendbuf
  94. #endif
  95. static uint8_t net_sendbuf[NET_BUFSIZE];
  96. #if ETH_MODULE == ENC28J60
  97. void ES_PingCallback(void) {
  98. }
  99. uint16_t get_udp_data_len(uint8_t *buf)
  100. {
  101. int16_t i;
  102. i=(((int16_t)buf[IP_TOTLEN_H_P])<<8)|(buf[IP_TOTLEN_L_P]&0xff);
  103. i-=IP_HEADER_LEN;
  104. i-=8;
  105. if (i<=0){
  106. i=0;
  107. }
  108. return((uint16_t)i);
  109. }
  110. static uint16_t info_data_len = 0;
  111. uint16_t packetloop_icmp_udp(uint8_t *buf,uint16_t plen)
  112. {
  113. if(eth_type_is_arp_and_my_ip(buf,plen)){
  114. if (buf[ETH_ARP_OPCODE_L_P]==ETH_ARP_OPCODE_REQ_L_V){
  115. // is it an arp request
  116. make_arp_answer_from_request(buf);
  117. }
  118. return 0;
  119. }
  120. // check if ip packets are for us:
  121. if(eth_type_is_ip_and_my_ip(buf,plen)==0){
  122. return 0;
  123. }
  124. if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
  125. make_echo_reply_from_request(buf,plen);
  126. return 0;
  127. }
  128. if (buf[IP_PROTO_P]==IP_PROTO_UDP_V) {
  129. info_data_len=get_udp_data_len(buf);
  130. return(IP_HEADER_LEN+8+14);
  131. }
  132. return 0;
  133. }
  134. uint8_t net_recvbuf[NET_BUFSIZE + 1];
  135. /*void HAL_Delay(__IO uint32_t Delay)
  136. {
  137. uint32_t tickstart = 0;
  138. tickstart = HAL_GetTick();
  139. while((HAL_GetTick() - tickstart) < Delay)
  140. {
  141. packetloop_icmp_udp(net_recvbuf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_recvbuf));
  142. }
  143. }*/
  144. static uint8_t local_mac[] = {0x02, 0x00, 0x04, 0x05, 0x01, 0x03};
  145. static uint8_t local_ip[] = {10, 180, 239, 3};
  146. static void eth_init() {
  147. /*ETHERNET_LED_GPIO->BSRR = ETHERNET_LED_PIN;
  148. HAL_Delay(1000);
  149. ETHERNET_LED_GPIO->BSRR = ETHERNET_LED_PIN << 16;*/
  150. ES_enc28j60SpiInit(&hspi1);
  151. ES_enc28j60Init(local_mac);
  152. uint8_t enc28j60_rev = ES_enc28j60Revision();
  153. if (enc28j60_rev <= 0)
  154. error(2);
  155. ES_init_ip_arp_udp_tcp(local_mac, local_ip, 80);
  156. return;
  157. }
  158. static void send(int32_t reader_id, int len) {
  159. ES_send_udp_data2(net_sendbuf, remote_mac, len, 26517+reader_id, remote_ip, 36400);
  160. }
  161. #endif
  162. #if ETH_MODULE == WIZNET
  163. static void w5500_cs_select() {
  164. ETHERNET_LED_GPIO->BSRR = ETHERNET_LED_PIN;
  165. HAL_GPIO_WritePin(ETHERNET_CS_GPIO, ETHERNET_CS_PIN, GPIO_PIN_RESET); //CS LOW
  166. }
  167. static void w5500_cs_deselect() {
  168. HAL_GPIO_WritePin(ETHERNET_CS_GPIO, ETHERNET_CS_PIN, GPIO_PIN_SET); //CS HIGH
  169. ETHERNET_LED_GPIO->BSRR = ETHERNET_LED_PIN << 16;
  170. }
  171. #ifdef WIZNET_BURST
  172. static void w5500_read_burst(uint8_t *pBuf, uint16_t len) {
  173. HAL_SPI_Receive_Auto(&hspi1, pBuf, len);
  174. return;
  175. }
  176. static void w5500_write_burst(uint8_t *pBuf, uint16_t len) {
  177. HAL_SPI_Transmit_Auto(&hspi1, pBuf, len);
  178. return;
  179. }
  180. #endif
  181. static void w5500_write(uint8_t b) {
  182. HAL_SPI_Transmit_Auto(&hspi1, &b, 1);
  183. }
  184. static uint8_t w5500_read(void) {
  185. uint8_t rbuf;
  186. HAL_SPI_Receive_Auto(&hspi1, &rbuf, 1);
  187. return rbuf;
  188. }
  189. static int open_net_socket(uint8_t sock_id)
  190. {
  191. uint8_t sd, sck_state;
  192. sd = socket(sock_id, Sn_MR_UDP, NET_LOCAL_PORT, 0);//SF_IO_NONBLOCK);
  193. if(sd != sock_id) {
  194. error(8);
  195. }
  196. do {
  197. getsockopt(sd , SO_STATUS, &sck_state);
  198. } while (sck_state != SOCK_UDP);
  199. return sd;
  200. }
  201. uint8_t bufSize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
  202. static int net;
  203. static void eth_init() {
  204. reg_wizchip_cs_cbfunc(w5500_cs_select, w5500_cs_deselect);
  205. #ifdef W5500_BURST
  206. reg_wizchip_spiburst_cbfunc(w5500_read_burst, w5500_write_burst);
  207. #endif
  208. reg_wizchip_spi_cbfunc(w5500_read, w5500_write);
  209. #if _WIZCHIP_ == 5500
  210. wizphy_reset();
  211. #endif
  212. if (wizchip_init(bufSize[0], bufSize[1]) != 0)
  213. error(2);
  214. wiz_NetInfo netInfo = { .mac = {0x02, 0x00, 0x04, 0x05, 0x01, 0x02}, // MAC address
  215. .ip = {10, 180, 239, 2}, // IP address
  216. .sn = {255, 255, 255, 0}, // IP netmask
  217. .gw = {10, 180, 239, 1}}; // IP gateway
  218. #if _WIZCHIP_ != 5100
  219. uint8_t tmp;
  220. do {
  221. tmp = wizphy_getphylink();
  222. if (tmp == -1) {
  223. error(4);
  224. }
  225. } while (tmp == PHY_LINK_OFF);
  226. #endif
  227. wizchip_setnetinfo(&netInfo);
  228. net = open_net_socket(0);
  229. return;
  230. }
  231. static void send(int32_t reader_id, int len) {
  232. #error not implemented, yet
  233. // TODO: use different source port for differed reader_id
  234. sendto(net_sendbuf, len, remote_ip, NET_REMOTE_PORT);
  235. }
  236. #endif
  237. #if ETH_MODULE != WIZNET && ETH_MODULE != ENC28J60
  238. # error Ethernet is disabled!
  239. static void eth_init() {
  240. }
  241. static void send(int32_t reader_id, int len) {
  242. }
  243. #endif
  244. static void sendSpecialPackage(char c) {
  245. net_senddatbuf[0] = 0xfe;
  246. net_senddatbuf[1] = 0xfe;
  247. net_senddatbuf[2] = 0xfe;
  248. net_senddatbuf[3] = 0xfe;
  249. send(-1, 4);
  250. net_senddatbuf[0] = 0xff;
  251. net_senddatbuf[1] = 0xfe;
  252. net_senddatbuf[2] = 0xfd;
  253. net_senddatbuf[3] = 0xfc;
  254. }
  255. /*static void sendErrorPackage(char c) {
  256. sendSpecialPackage(0xfd);
  257. }*/
  258. static void sendAlivePackage() {
  259. sendSpecialPackage(0xfe);
  260. }
  261. /* USER CODE END PFP */
  262. /* USER CODE BEGIN 0 */
  263. void gotBit(uint8_t card_idx, uint8_t bitValue) {
  264. if (started_at[card_idx] == 0) {
  265. started_at[card_idx] = HAL_GetTick();
  266. }
  267. card_number[card_idx] <<= 1;
  268. if (bitValue) {
  269. card_number[card_idx] |= 1;
  270. }
  271. return;
  272. }
  273. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
  274. switch (GPIO_Pin) {
  275. case DATA0_0_Pin:
  276. gotBit(0, 0);
  277. break;
  278. case DATA0_1_Pin:
  279. gotBit(0, 1);
  280. break;
  281. case DATA1_0_Pin:
  282. gotBit(1, 0);
  283. break;
  284. case DATA1_1_Pin:
  285. gotBit(1, 1);
  286. break;
  287. case DATA2_0_Pin:
  288. gotBit(2, 0);
  289. break;
  290. case DATA2_1_Pin:
  291. gotBit(2, 1);
  292. break;
  293. }
  294. return;
  295. }
  296. /* USER CODE END 0 */
  297. int main(void)
  298. {
  299. /* USER CODE BEGIN 1 */
  300. /* USER CODE END 1 */
  301. /* MCU Configuration----------------------------------------------------------*/
  302. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  303. HAL_Init();
  304. /* Configure the system clock */
  305. SystemClock_Config();
  306. /* Initialize all configured peripherals */
  307. MX_GPIO_Init();
  308. MX_SPI1_Init();
  309. /* USER CODE BEGIN 2 */
  310. GPIOA->BSRR = LED_STATUS_Pin;
  311. HAL_Delay(250);
  312. GPIOA->BSRR = LED_STATUS_Pin << 16;
  313. HAL_Delay(250);
  314. ETHERNET_LED_GPIO->BSRR = ETHERNET_LED_PIN;
  315. eth_init();
  316. GPIOA->BSRR = LED_STATUS_Pin;
  317. sendAlivePackage();
  318. /* USER CODE END 2 */
  319. /* Infinite loop */
  320. /* USER CODE BEGIN WHILE */
  321. int card_idx = 0;
  322. while (card_idx < CARDS) {
  323. card_number[card_idx++] = 0;
  324. }
  325. uint32_t previousAliveTick = 0;
  326. while (1) {
  327. packetloop_icmp_udp(net_recvbuf, ES_enc28j60PacketReceive(sizeof(net_sendbuf)-1, net_recvbuf));
  328. uint32_t currentTick = HAL_GetTick();
  329. /*if (currentTick >= 1000 * (3600 * 24 + 60)) { // Periodically reboot just in case
  330. NVIC_SystemReset();
  331. }*/
  332. int card_idx = 0;
  333. while (card_idx < CARDS) {
  334. if (started_at[card_idx] && (currentTick - started_at[card_idx] >= 200)) {
  335. started_at[card_idx] = 0;
  336. break;
  337. }
  338. card_idx++;
  339. }
  340. if (card_idx >= CARDS) {
  341. if (currentTick - previousAliveTick > 1000) {
  342. previousAliveTick = currentTick;
  343. sendAlivePackage();
  344. }
  345. continue;
  346. }
  347. card_number[card_idx] >>= 1;
  348. /*if (!card_number[card_idx]) {
  349. sendErrorPackage();
  350. continue;
  351. }*/
  352. memcpy(&net_senddatbuf[4+sizeof(card_number[card_idx])*0], &card_number[card_idx], sizeof(card_number[card_idx]));
  353. memcpy(&net_senddatbuf[4+sizeof(card_number[card_idx])*1], &card_number[card_idx], sizeof(card_number[card_idx])); // On case of data corruption
  354. memcpy(&net_senddatbuf[4+sizeof(card_number[card_idx])*2], &card_number[card_idx], sizeof(card_number[card_idx])); // On case of data corruption
  355. send(reader_id[card_idx], 4+3*sizeof(card_number[card_idx]));
  356. card_number[card_idx] = 0;
  357. }
  358. /* USER CODE END WHILE */
  359. /* USER CODE BEGIN 3 */
  360. /* USER CODE END 3 */
  361. }
  362. /** System Clock Configuration
  363. */
  364. void SystemClock_Config(void)
  365. {
  366. RCC_OscInitTypeDef RCC_OscInitStruct;
  367. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  368. /**Initializes the CPU, AHB and APB busses clocks
  369. */
  370. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  371. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  372. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  373. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  374. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  375. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
  376. RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  377. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  378. {
  379. Error_Handler();
  380. }
  381. /**Initializes the CPU, AHB and APB busses clocks
  382. */
  383. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  384. |RCC_CLOCKTYPE_PCLK1;
  385. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  386. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  387. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  388. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  389. {
  390. Error_Handler();
  391. }
  392. /**Configure the Systick interrupt time
  393. */
  394. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  395. /**Configure the Systick
  396. */
  397. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  398. /* SysTick_IRQn interrupt configuration */
  399. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  400. }
  401. /* SPI1 init function */
  402. static void MX_SPI1_Init(void)
  403. {
  404. hspi1.Instance = SPI1;
  405. hspi1.Init.Mode = SPI_MODE_MASTER;
  406. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  407. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  408. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  409. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  410. hspi1.Init.NSS = SPI_NSS_SOFT;
  411. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  412. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  413. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  414. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  415. hspi1.Init.CRCPolynomial = 7;
  416. hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  417. hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  418. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  419. {
  420. Error_Handler();
  421. }
  422. }
  423. /** Configure pins as
  424. * Analog
  425. * Input
  426. * Output
  427. * EVENT_OUT
  428. * EXTI
  429. */
  430. static void MX_GPIO_Init(void)
  431. {
  432. GPIO_InitTypeDef GPIO_InitStruct;
  433. /* GPIO Ports Clock Enable */
  434. __HAL_RCC_GPIOF_CLK_ENABLE();
  435. __HAL_RCC_GPIOA_CLK_ENABLE();
  436. __HAL_RCC_GPIOB_CLK_ENABLE();
  437. /*Configure GPIO pin Output Level */
  438. HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_RESET);
  439. /*Configure GPIO pin Output Level */
  440. HAL_GPIO_WritePin(LED_STATUS_GPIO_Port, LED_STATUS_Pin, GPIO_PIN_RESET);
  441. /*Configure GPIO pins : DATA0_0_Pin DATA0_1_Pin DATA1_0_Pin DATA1_1_Pin
  442. DATA2_1_Pin */
  443. GPIO_InitStruct.Pin = DATA0_0_Pin|DATA0_1_Pin|DATA1_0_Pin|DATA1_1_Pin
  444. |DATA2_1_Pin;
  445. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  446. GPIO_InitStruct.Pull = GPIO_NOPULL;
  447. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  448. /*Configure GPIO pin : DATA2_0_Pin */
  449. GPIO_InitStruct.Pin = DATA2_0_Pin;
  450. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  451. GPIO_InitStruct.Pull = GPIO_NOPULL;
  452. HAL_GPIO_Init(DATA2_0_GPIO_Port, &GPIO_InitStruct);
  453. /*Configure GPIO pin : SPI1_NSS_Pin */
  454. GPIO_InitStruct.Pin = SPI1_NSS_Pin;
  455. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  456. GPIO_InitStruct.Pull = GPIO_NOPULL;
  457. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  458. HAL_GPIO_Init(SPI1_NSS_GPIO_Port, &GPIO_InitStruct);
  459. /*Configure GPIO pin : LED_STATUS_Pin */
  460. GPIO_InitStruct.Pin = LED_STATUS_Pin;
  461. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  462. GPIO_InitStruct.Pull = GPIO_NOPULL;
  463. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  464. HAL_GPIO_Init(LED_STATUS_GPIO_Port, &GPIO_InitStruct);
  465. /* EXTI interrupt init*/
  466. HAL_NVIC_SetPriority(EXTI0_1_IRQn, 0, 0);
  467. HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
  468. HAL_NVIC_SetPriority(EXTI2_3_IRQn, 0, 0);
  469. HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);
  470. HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  471. HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
  472. }
  473. /* USER CODE BEGIN 4 */
  474. /* USER CODE END 4 */
  475. /**
  476. * @brief This function is executed in case of error occurrence.
  477. * @param None
  478. * @retval None
  479. */
  480. void Error_Handler(void)
  481. {
  482. /* USER CODE BEGIN Error_Handler */
  483. /* User can add his own implementation to report the HAL error return state */
  484. ETHERNET_LED_GPIO->BSRR = ETHERNET_LED_PIN;
  485. GPIOA->BSRR = LED_STATUS_Pin << 16;
  486. while(1)
  487. {
  488. }
  489. /* USER CODE END Error_Handler */
  490. }
  491. #ifdef USE_FULL_ASSERT
  492. /**
  493. * @brief Reports the name of the source file and the source line number
  494. * where the assert_param error has occurred.
  495. * @param file: pointer to the source file name
  496. * @param line: assert_param error line source number
  497. * @retval None
  498. */
  499. void assert_failed(uint8_t* file, uint32_t line)
  500. {
  501. /* USER CODE BEGIN 6 */
  502. /* User can add his own implementation to report the file name and line number,
  503. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  504. /* USER CODE END 6 */
  505. }
  506. #endif
  507. /**
  508. * @}
  509. */
  510. /**
  511. * @}
  512. */
  513. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/