xref: /FreeRTOS-Plus-TCP-v4.0.0/source/portable/NetworkInterface/SH2A/NetworkInterface.c (revision 1ab6eb88857cf1011bf1f8349b43a1c34c7ced0f)
1 /*
2  * FreeRTOS+TCP <DEVELOPMENT BRANCH>
3  * Copyright (C) 2022 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * http://aws.amazon.com/freertos
25  * http://www.FreeRTOS.org
26  */
27 
28 /* Standard includes. */
29 #include <stdint.h>
30 
31 /* FreeRTOS includes. */
32 #include "FreeRTOS.h"
33 #include "task.h"
34 #include "queue.h"
35 #include "semphr.h"
36 
37 /* FreeRTOS+TCP includes. */
38 #include "FreeRTOS_UDP_IP.h"
39 #include "FreeRTOS_Sockets.h"
40 #include "NetworkBufferManagement.h"
41 
42 /* Hardware includes. */
43 #include "hwEthernet.h"
44 
45 /* Demo includes. */
46 #include "NetworkInterface.h"
47 
48 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1
49     #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer )    eProcessBuffer
50 #else
51     #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer )    eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
52 #endif
53 
54 /* When a packet is ready to be sent, if it cannot be sent immediately then the
55  * task performing the transmit will block for niTX_BUFFER_FREE_WAIT
56  * milliseconds.  It will do this a maximum of niMAX_TX_ATTEMPTS before giving
57  * up. */
58 #define niTX_BUFFER_FREE_WAIT       ( ( TickType_t ) 2UL / portTICK_PERIOD_MS )
59 #define niMAX_TX_ATTEMPTS           ( 5 )
60 
61 /* The length of the queue used to send interrupt status words from the
62  * interrupt handler to the deferred handler task. */
63 #define niINTERRUPT_QUEUE_LENGTH    ( 10 )
64 
65 /*-----------------------------------------------------------*/
66 
67 /*
68  * A deferred interrupt handler task that processes
69  */
70 extern void vEMACHandlerTask( void * pvParameters );
71 
72 /*-----------------------------------------------------------*/
73 
74 /* The queue used to communicate Ethernet events with the IP task. */
75 extern QueueHandle_t xNetworkEventQueue;
76 
77 /* The semaphore used to wake the deferred interrupt handler task when an Rx
78  * interrupt is received. */
79 SemaphoreHandle_t xEMACRxEventSemaphore = NULL;
80 /*-----------------------------------------------------------*/
81 
xNetworkInterfaceInitialise(void)82 BaseType_t xNetworkInterfaceInitialise( void )
83 {
84     BaseType_t xStatus, xReturn;
85 
86     /* Initialise the MAC. */
87     vInitEmac();
88 
89     while( lEMACWaitForLink() != pdPASS )
90     {
91         vTaskDelay( 20 );
92     }
93 
94     vSemaphoreCreateBinary( xEMACRxEventSemaphore );
95     configASSERT( xEMACRxEventSemaphore );
96 
97     /* The handler task is created at the highest possible priority to
98      * ensure the interrupt handler can return directly to it. */
99     xTaskCreate( vEMACHandlerTask, "EMAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
100     xReturn = pdPASS;
101 
102     return xReturn;
103 }
104 /*-----------------------------------------------------------*/
105 
xNetworkInterfaceOutput(NetworkBufferDescriptor_t * const pxNetworkBuffer)106 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxNetworkBuffer )
107 {
108     extern void vEMACCopyWrite( uint8_t * pucBuffer,
109                                 uint16_t usLength );
110 
111     vEMACCopyWrite( pxNetworkBuffer->pucBuffer, pxNetworkBuffer->xDataLength );
112 
113     /* Finished with the network buffer. */
114     vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
115 
116     return pdTRUE;
117 }
118 /*-----------------------------------------------------------*/
119