123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- /**
- ******************************************************************************
- * File Name : main.c
- * Description : Main program body
- ******************************************************************************
- *
- * COPYRIGHT(c) 2017 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f0xx_hal.h"
- /* USER CODE BEGIN Includes */
- #include <string.h>
- #include "EtherShield.h"
- #define NET_BUF_SIZE (1<<8)
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- SPI_HandleTypeDef hspi1;
- UART_HandleTypeDef huart1;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- uint32_t timer_out = 0, card_number=0, card_number_dbg=0, delay=0;
- uint8_t is_reading = 0;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- void Error_Handler(void);
- static void MX_GPIO_Init(void);
- static void MX_USART1_UART_Init(void);
- static void MX_SPI1_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- void error (float error_num, char infinite) {
- HAL_Delay(5000);
- NVIC_SystemReset();
- }
- /*
- void error (float error_num, char infinite) {
- //printf("%u\r\n", error_num);
- if (infinite)
- while (1) {
- int i = 0;
- while (i++ < ((int)((float)(error_num) / 1) + 1) ) {
- GPIOA->BSRR = LED_STATUS_Pin;
- HAL_Delay(1000 / error_num);
- GPIOA->BSRR = LED_STATUS_Pin << 16;
- HAL_Delay(1000 / error_num);
- }
- };
- GPIOA->BSRR = LED_STATUS_Pin;
- HAL_Delay(1000);
- GPIOA->BSRR = LED_STATUS_Pin << 16;
- HAL_Delay(100);
- int i=0;
- while (i++ < error_num) {
- GPIOA->BSRR = LED_STATUS_Pin;
- HAL_Delay(300);
- GPIOA->BSRR = LED_STATUS_Pin << 16;
- HAL_Delay(200);
- }
- HAL_Delay(500);
- NVIC_SystemReset();
- return;
- }
- static inline void blink(int times, int delay) {
- int i = 0;
- while (i++ < times) {
- GPIOA->BSRR = LED_STATUS_Pin;
- HAL_Delay(delay);
- GPIOA->BSRR = LED_STATUS_Pin << 16;
- HAL_Delay(delay);
- }
- return;
- }
- */
- void ES_PingCallback(void) {
- }
- uint16_t get_udp_data_len(uint8_t *buf)
- {
- int16_t i;
- i=(((int16_t)buf[IP_TOTLEN_H_P])<<8)|(buf[IP_TOTLEN_L_P]&0xff);
- i-=IP_HEADER_LEN;
- i-=8;
- if (i<=0){
- i=0;
- }
- return((uint16_t)i);
- }
- static uint16_t info_data_len = 0;
- uint16_t packetloop_icmp_udp(uint8_t *buf,uint16_t plen)
- {
- if(eth_type_is_arp_and_my_ip(buf,plen)){
- if (buf[ETH_ARP_OPCODE_L_P]==ETH_ARP_OPCODE_REQ_L_V){
- // is it an arp request
- make_arp_answer_from_request(buf);
- }
- return(0);
- }
- // check if ip packets are for us:
- if(eth_type_is_ip_and_my_ip(buf,plen)==0){
- return(0);
- }
- if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
- make_echo_reply_from_request(buf,plen);
- return(0);
- }
- if (buf[IP_PROTO_P]==IP_PROTO_UDP_V) {
- info_data_len=get_udp_data_len(buf);
- return(IP_HEADER_LEN+8+14);
- }
- return(0);
- }
- uint8_t net_sendbuf[NET_BUF_SIZE + 1];
- uint8_t net_recvbuf[NET_BUF_SIZE + 1];
- /*void HAL_Delay(__IO uint32_t Delay)
- {
- uint32_t tickstart = 0;
- tickstart = HAL_GetTick();
- while((HAL_GetTick() - tickstart) < Delay)
- {
- packetloop_icmp_udp(net_recvbuf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_recvbuf));
- }
- }*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_SPI1_Init();
- /* USER CODE BEGIN 2 */
- uint8_t local_mac[] = {0x02, 0x03, 0x04, 0x05, 0x06, 0x70};
- uint8_t remote_mac[] = {0x00, 0x1d, 0x7d, 0xe5, 0x56, 0xc5};
- uint8_t local_ip[] = {10, 4, 33, 126};
- uint8_t remote_ip[] = {10, 4, 33, 71};
- ES_enc28j60SpiInit(&hspi1);
- ES_enc28j60Init(local_mac);
- uint8_t enc28j60_rev = ES_enc28j60Revision();
- if (enc28j60_rev <= 0)
- error(2, 0);
- ES_init_ip_arp_udp_tcp(local_mac, local_ip, 80);
- net_sendbuf[0] = 0xfe;
- net_sendbuf[1] = 0xfe;
- net_sendbuf[2] = 0xfe;
- net_sendbuf[3] = 0xfe;
- ES_send_udp_data2(net_sendbuf, remote_mac, 4, 26525, remote_ip, 36401);
- packetloop_icmp_udp(net_recvbuf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_recvbuf)); // Just to flush the buffer
- net_sendbuf[0] = 0xff;
- net_sendbuf[1] = 0xff;
- net_sendbuf[2] = 0xff;
- net_sendbuf[3] = 0xff;
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1) {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- while (is_reading == 1) { // флаг чтения=1; is_reading is also controlled by interrupts
- if (timer_out < 60000) { // в течение этого времени должно всё передаться. в общем-то если не успевает - то какая-то херня в схеме.
- timer_out++;
- continue;
- }
- is_reading = 0;
- timer_out = 0;
- card_number >>= 1;
- card_number_dbg=card_number;
- memcpy(&net_sendbuf[4+sizeof(card_number)*0], &card_number, sizeof(card_number));
- memcpy(&net_sendbuf[4+sizeof(card_number)*1], &card_number, sizeof(card_number));
- memcpy(&net_sendbuf[4+sizeof(card_number)*2], &card_number, sizeof(card_number));
- ES_send_udp_data2(net_sendbuf, remote_mac, 4+3*sizeof(card_number), 26525, remote_ip, 36401);
- packetloop_icmp_udp(net_recvbuf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_recvbuf)); // Just to flush the buffer
- card_number = 0;
- }
- for (delay=0;delay<60000;delay++); // хуй знает, задержка на всякий случай :)
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
- RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
- PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* SPI1 init function */
- static void MX_SPI1_Init(void)
- {
- hspi1.Instance = SPI1;
- hspi1.Init.Mode = SPI_MODE_MASTER;
- hspi1.Init.Direction = SPI_DIRECTION_2LINES;
- hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
- hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
- hspi1.Init.NSS = SPI_NSS_SOFT;
- hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
- hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
- hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- hspi1.Init.CRCPolynomial = 7;
- hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
- hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
- if (HAL_SPI_Init(&hspi1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USART1 init function */
- static void MX_USART1_UART_Init(void)
- {
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 38400;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, LED_STATUS_Pin|LED_ETH_BUSY_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pins : DATA_0_Pin DATA_1_Pin */
- GPIO_InitStruct.Pin = DATA_0_Pin|DATA_1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pin : SPI1_NSS_Pin */
- GPIO_InitStruct.Pin = SPI1_NSS_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(SPI1_NSS_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pins : LED_STATUS_Pin LED_ETH_BUSY_Pin */
- GPIO_InitStruct.Pin = LED_STATUS_Pin|LED_ETH_BUSY_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* EXTI interrupt init*/
- HAL_NVIC_SetPriority(EXTI0_1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
- HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|