main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #include <string.h>
  38. #include "EtherShield.h"
  39. #define NET_BUF_SIZE (1<<8)
  40. /* USER CODE END Includes */
  41. /* Private variables ---------------------------------------------------------*/
  42. SPI_HandleTypeDef hspi1;
  43. UART_HandleTypeDef huart1;
  44. /* USER CODE BEGIN PV */
  45. /* Private variables ---------------------------------------------------------*/
  46. uint32_t timer_out = 0, card_number=0, card_number_dbg=0, delay=0;
  47. uint8_t is_reading = 0;
  48. /* USER CODE END PV */
  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);
  51. void Error_Handler(void);
  52. static void MX_GPIO_Init(void);
  53. static void MX_USART1_UART_Init(void);
  54. static void MX_SPI1_Init(void);
  55. /* USER CODE BEGIN PFP */
  56. /* Private function prototypes -----------------------------------------------*/
  57. void error (float error_num, char infinite) {
  58. HAL_Delay(5000);
  59. NVIC_SystemReset();
  60. }
  61. /*
  62. void error (float error_num, char infinite) {
  63. //printf("%u\r\n", error_num);
  64. if (infinite)
  65. while (1) {
  66. int i = 0;
  67. while (i++ < ((int)((float)(error_num) / 1) + 1) ) {
  68. GPIOA->BSRR = LED_STATUS_Pin;
  69. HAL_Delay(1000 / error_num);
  70. GPIOA->BSRR = LED_STATUS_Pin << 16;
  71. HAL_Delay(1000 / error_num);
  72. }
  73. };
  74. GPIOA->BSRR = LED_STATUS_Pin;
  75. HAL_Delay(1000);
  76. GPIOA->BSRR = LED_STATUS_Pin << 16;
  77. HAL_Delay(100);
  78. int i=0;
  79. while (i++ < error_num) {
  80. GPIOA->BSRR = LED_STATUS_Pin;
  81. HAL_Delay(300);
  82. GPIOA->BSRR = LED_STATUS_Pin << 16;
  83. HAL_Delay(200);
  84. }
  85. HAL_Delay(500);
  86. NVIC_SystemReset();
  87. return;
  88. }
  89. static inline void blink(int times, int delay) {
  90. int i = 0;
  91. while (i++ < times) {
  92. GPIOA->BSRR = LED_STATUS_Pin;
  93. HAL_Delay(delay);
  94. GPIOA->BSRR = LED_STATUS_Pin << 16;
  95. HAL_Delay(delay);
  96. }
  97. return;
  98. }
  99. */
  100. void ES_PingCallback(void) {
  101. }
  102. uint16_t get_udp_data_len(uint8_t *buf)
  103. {
  104. int16_t i;
  105. i=(((int16_t)buf[IP_TOTLEN_H_P])<<8)|(buf[IP_TOTLEN_L_P]&0xff);
  106. i-=IP_HEADER_LEN;
  107. i-=8;
  108. if (i<=0){
  109. i=0;
  110. }
  111. return((uint16_t)i);
  112. }
  113. static uint16_t info_data_len = 0;
  114. uint16_t packetloop_icmp_udp(uint8_t *buf,uint16_t plen)
  115. {
  116. if(eth_type_is_arp_and_my_ip(buf,plen)){
  117. if (buf[ETH_ARP_OPCODE_L_P]==ETH_ARP_OPCODE_REQ_L_V){
  118. // is it an arp request
  119. make_arp_answer_from_request(buf);
  120. }
  121. return(0);
  122. }
  123. // check if ip packets are for us:
  124. if(eth_type_is_ip_and_my_ip(buf,plen)==0){
  125. return(0);
  126. }
  127. if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
  128. make_echo_reply_from_request(buf,plen);
  129. return(0);
  130. }
  131. if (buf[IP_PROTO_P]==IP_PROTO_UDP_V) {
  132. info_data_len=get_udp_data_len(buf);
  133. return(IP_HEADER_LEN+8+14);
  134. }
  135. return(0);
  136. }
  137. uint8_t net_sendbuf[NET_BUF_SIZE + 1];
  138. uint8_t net_recvbuf[NET_BUF_SIZE + 1];
  139. /*void HAL_Delay(__IO uint32_t Delay)
  140. {
  141. uint32_t tickstart = 0;
  142. tickstart = HAL_GetTick();
  143. while((HAL_GetTick() - tickstart) < Delay)
  144. {
  145. packetloop_icmp_udp(net_recvbuf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_recvbuf));
  146. }
  147. }*/
  148. /* USER CODE END PFP */
  149. /* USER CODE BEGIN 0 */
  150. /* USER CODE END 0 */
  151. int main(void)
  152. {
  153. /* USER CODE BEGIN 1 */
  154. /* USER CODE END 1 */
  155. /* MCU Configuration----------------------------------------------------------*/
  156. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  157. HAL_Init();
  158. /* Configure the system clock */
  159. SystemClock_Config();
  160. /* Initialize all configured peripherals */
  161. MX_GPIO_Init();
  162. MX_USART1_UART_Init();
  163. MX_SPI1_Init();
  164. /* USER CODE BEGIN 2 */
  165. uint8_t local_mac[] = {0x02, 0x03, 0x04, 0x05, 0x06, 0x70};
  166. uint8_t remote_mac[] = {0x00, 0x1d, 0x7d, 0xe5, 0x56, 0xc5};
  167. uint8_t local_ip[] = {10, 4, 33, 126};
  168. uint8_t remote_ip[] = {10, 4, 33, 71};
  169. ES_enc28j60SpiInit(&hspi1);
  170. ES_enc28j60Init(local_mac);
  171. uint8_t enc28j60_rev = ES_enc28j60Revision();
  172. if (enc28j60_rev <= 0)
  173. error(2, 0);
  174. ES_init_ip_arp_udp_tcp(local_mac, local_ip, 80);
  175. net_sendbuf[0] = 0xfe;
  176. net_sendbuf[1] = 0xfe;
  177. net_sendbuf[2] = 0xfe;
  178. net_sendbuf[3] = 0xfe;
  179. ES_send_udp_data2(net_sendbuf, remote_mac, 4, 26525, remote_ip, 36401);
  180. packetloop_icmp_udp(net_recvbuf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_recvbuf)); // Just to flush the buffer
  181. net_sendbuf[0] = 0xff;
  182. net_sendbuf[1] = 0xff;
  183. net_sendbuf[2] = 0xff;
  184. net_sendbuf[3] = 0xff;
  185. /* USER CODE END 2 */
  186. /* Infinite loop */
  187. /* USER CODE BEGIN WHILE */
  188. while (1) {
  189. /* USER CODE END WHILE */
  190. /* USER CODE BEGIN 3 */
  191. while (is_reading == 1) { // флаг чтения=1; is_reading is also controlled by interrupts
  192. if (timer_out < 60000) { // в течение этого времени должно всё передаться. в общем-то если не успевает - то какая-то херня в схеме.
  193. timer_out++;
  194. continue;
  195. }
  196. is_reading = 0;
  197. timer_out = 0;
  198. card_number >>= 1;
  199. card_number_dbg=card_number;
  200. memcpy(&net_sendbuf[4+sizeof(card_number)*0], &card_number, sizeof(card_number));
  201. memcpy(&net_sendbuf[4+sizeof(card_number)*1], &card_number, sizeof(card_number));
  202. memcpy(&net_sendbuf[4+sizeof(card_number)*2], &card_number, sizeof(card_number));
  203. ES_send_udp_data2(net_sendbuf, remote_mac, 4+3*sizeof(card_number), 26525, remote_ip, 36401);
  204. packetloop_icmp_udp(net_recvbuf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_recvbuf)); // Just to flush the buffer
  205. card_number = 0;
  206. }
  207. for (delay=0;delay<60000;delay++); // хуй знает, задержка на всякий случай :)
  208. }
  209. /* USER CODE END 3 */
  210. }
  211. /** System Clock Configuration
  212. */
  213. void SystemClock_Config(void)
  214. {
  215. RCC_OscInitTypeDef RCC_OscInitStruct;
  216. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  217. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  218. /**Initializes the CPU, AHB and APB busses clocks
  219. */
  220. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  221. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  222. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  223. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  224. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  225. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
  226. RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  227. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  228. {
  229. Error_Handler();
  230. }
  231. /**Initializes the CPU, AHB and APB busses clocks
  232. */
  233. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  234. |RCC_CLOCKTYPE_PCLK1;
  235. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  236. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  237. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  238. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  239. {
  240. Error_Handler();
  241. }
  242. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  243. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
  244. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  245. {
  246. Error_Handler();
  247. }
  248. /**Configure the Systick interrupt time
  249. */
  250. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  251. /**Configure the Systick
  252. */
  253. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  254. /* SysTick_IRQn interrupt configuration */
  255. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  256. }
  257. /* SPI1 init function */
  258. static void MX_SPI1_Init(void)
  259. {
  260. hspi1.Instance = SPI1;
  261. hspi1.Init.Mode = SPI_MODE_MASTER;
  262. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  263. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  264. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  265. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  266. hspi1.Init.NSS = SPI_NSS_SOFT;
  267. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  268. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  269. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  270. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  271. hspi1.Init.CRCPolynomial = 7;
  272. hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  273. hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  274. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  275. {
  276. Error_Handler();
  277. }
  278. }
  279. /* USART1 init function */
  280. static void MX_USART1_UART_Init(void)
  281. {
  282. huart1.Instance = USART1;
  283. huart1.Init.BaudRate = 38400;
  284. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  285. huart1.Init.StopBits = UART_STOPBITS_1;
  286. huart1.Init.Parity = UART_PARITY_NONE;
  287. huart1.Init.Mode = UART_MODE_TX_RX;
  288. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  289. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  290. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  291. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  292. if (HAL_UART_Init(&huart1) != HAL_OK)
  293. {
  294. Error_Handler();
  295. }
  296. }
  297. /** Configure pins as
  298. * Analog
  299. * Input
  300. * Output
  301. * EVENT_OUT
  302. * EXTI
  303. */
  304. static void MX_GPIO_Init(void)
  305. {
  306. GPIO_InitTypeDef GPIO_InitStruct;
  307. /* GPIO Ports Clock Enable */
  308. __HAL_RCC_GPIOF_CLK_ENABLE();
  309. __HAL_RCC_GPIOA_CLK_ENABLE();
  310. __HAL_RCC_GPIOB_CLK_ENABLE();
  311. /*Configure GPIO pin Output Level */
  312. HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_RESET);
  313. /*Configure GPIO pin Output Level */
  314. HAL_GPIO_WritePin(GPIOA, LED_STATUS_Pin|LED_ETH_BUSY_Pin, GPIO_PIN_RESET);
  315. /*Configure GPIO pins : DATA_0_Pin DATA_1_Pin */
  316. GPIO_InitStruct.Pin = DATA_0_Pin|DATA_1_Pin;
  317. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  318. GPIO_InitStruct.Pull = GPIO_NOPULL;
  319. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  320. /*Configure GPIO pin : SPI1_NSS_Pin */
  321. GPIO_InitStruct.Pin = SPI1_NSS_Pin;
  322. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  323. GPIO_InitStruct.Pull = GPIO_NOPULL;
  324. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  325. HAL_GPIO_Init(SPI1_NSS_GPIO_Port, &GPIO_InitStruct);
  326. /*Configure GPIO pins : LED_STATUS_Pin LED_ETH_BUSY_Pin */
  327. GPIO_InitStruct.Pin = LED_STATUS_Pin|LED_ETH_BUSY_Pin;
  328. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  329. GPIO_InitStruct.Pull = GPIO_NOPULL;
  330. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  331. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  332. /* EXTI interrupt init*/
  333. HAL_NVIC_SetPriority(EXTI0_1_IRQn, 0, 0);
  334. HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
  335. HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  336. HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
  337. }
  338. /* USER CODE BEGIN 4 */
  339. /* USER CODE END 4 */
  340. /**
  341. * @brief This function is executed in case of error occurrence.
  342. * @param None
  343. * @retval None
  344. */
  345. void Error_Handler(void)
  346. {
  347. /* USER CODE BEGIN Error_Handler */
  348. /* User can add his own implementation to report the HAL error return state */
  349. while(1)
  350. {
  351. }
  352. /* USER CODE END Error_Handler */
  353. }
  354. #ifdef USE_FULL_ASSERT
  355. /**
  356. * @brief Reports the name of the source file and the source line number
  357. * where the assert_param error has occurred.
  358. * @param file: pointer to the source file name
  359. * @param line: assert_param error line source number
  360. * @retval None
  361. */
  362. void assert_failed(uint8_t* file, uint32_t line)
  363. {
  364. /* USER CODE BEGIN 6 */
  365. /* User can add his own implementation to report the file name and line number,
  366. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  367. /* USER CODE END 6 */
  368. }
  369. #endif
  370. /**
  371. * @}
  372. */
  373. /**
  374. * @}
  375. */
  376. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/