b5ef669f91142105cc4ca889959adb4bb65d2ed7
1 /*-----------------------------------------------------------------------------
2 * LinkOsLayer.cpp
3 * Copyright acontis technologies GmbH, Weingarten, Germany
4 * Response Vladimir Pustovalov
5 * Description SYS/BIOS platform layer for EtherCAT link layer
6 *---------------------------------------------------------------------------*/
8 /*-INCLUDES------------------------------------------------------------------*/
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
13 #include "LinkOsLayer.h"
14 #include "EcLink.h"
16 // sys/bios specific includes
17 #include <xdc/std.h>
19 #include <xdc/runtime/Error.h>
20 #include <xdc/runtime/System.h>
21 #include <xdc/runtime/Timestamp.h>
23 #include <ti/sysbios/knl/Task.h>
24 #include <ti/sysbios/knl/Clock.h>
28 static EC_PF_LINKOSDBGMSGHK S_pfLinkOsDbgMsgHook = EC_NULL;
31 /*******************************************************************************
32 *
33 * LinkOsPlatformInit - one time platform initialization
34 *
35 */
36 static EC_T_DWORD G_dwTicksIn1Usec = 0;
38 EC_T_VOID LinkOsPlatformInit()
39 {
40 // Init performance counter
41 xdc_runtime_Types_FreqHz freq;
42 xdc_runtime_Timestamp_getFreq(&freq);
43 EC_T_UINT64 freqU64 = freq.lo + ((EC_T_UINT64)freq.hi << 32);
45 G_dwTicksIn1Usec = (EC_T_DWORD)(freqU64 / 1000000);
47 }
49 /*******************************************************************************
50 *
51 * LinkOsSysDelay - delay in MicroSeconds
52 *
53 */
54 EC_T_VOID LinkOsSysDelay(EC_T_DWORD dwTimeUsec)
55 {
56 LinkOsDbgAssert(G_dwTicksIn1Usec != 0);
58 EC_T_UINT64 startTicks = OsMeasGetCounterTicks();
60 // try to use system delay in msec if possible
61 if ( dwTimeUsec > 1000 )
62 {
63 EC_T_DWORD dwDelayInMs = dwTimeUsec / 1000;
65 NoOsSleep(dwDelayInMs);
66 }
68 EC_T_UINT64 delayInTicks = (EC_T_UINT64)dwTimeUsec * G_dwTicksIn1Usec;
69 EC_T_UINT64 ticksPassed = OsMeasGetCounterTicks() - startTicks;
71 while (ticksPassed < delayInTicks)
72 {
73 ticksPassed = OsMeasGetCounterTicks() - startTicks;
74 }
75 }
78 /*******************************************************************************
79 *
80 * SysAllocDmaBuffer - allocate DMA buffer
81 *
82 */
83 EC_T_VOID LinkOsAllocDmaBuffer(
84 EC_T_VOID* pvDmaObject
85 ,EC_T_BYTE** ppbyVirtAddrCached
86 ,EC_T_BYTE** ppbyVirtAddrUncached
87 ,EC_T_BYTE** ppbyPhysAddr
88 ,EC_T_DWORD dwSize
89 )
90 {
91 ECLINKOS_UNREFPARM(pvDmaObject);
92 EC_T_DWORD dwAddr;
94 dwAddr = (EC_T_DWORD) malloc(dwSize);
95 *ppbyPhysAddr = (EC_T_BYTE*)dwAddr;
96 *ppbyVirtAddrCached = (EC_T_BYTE*)dwAddr;
97 *ppbyVirtAddrUncached = (EC_T_BYTE*)dwAddr;
99 /* printf("DMA alloc phys 0x%08x, virt 0x%08x\n", *ppbyPhysAddr, *ppbyVirtAddrCached); */
100 }
103 /*******************************************************************************
104 *
105 * SysFreeDmaBuffer - free DMA buffer
106 *
107 */
108 EC_T_VOID LinkOsFreeDmaBuffer(
109 EC_T_VOID* pvDmaObject
110 ,EC_T_BYTE* pbyPhysAddr
111 ,EC_T_BYTE* pbyVirtAddrCached
112 ,EC_T_BYTE* pbyVirtAddrUncached
113 ,EC_T_DWORD dwSize
114 )
115 {
116 ECLINKOS_UNREFPARM(pvDmaObject);
117 ECLINKOS_UNREFPARM(pbyPhysAddr);
118 ECLINKOS_UNREFPARM(dwSize);
119 ECLINKOS_UNREFPARM(pbyVirtAddrUncached);
121 free(((EC_T_VOID*)pbyVirtAddrCached));
122 }
125 EC_T_BYTE* LinkOsMapMemory(EC_T_BYTE* pbyPhysical, EC_T_DWORD dwLen)
126 {
127 ECLINKOS_UNREFPARM( dwLen );
128 return pbyPhysical;
129 }
131 EC_T_VOID LinkOsUnmapMemory(EC_T_BYTE* pbyVirtual, EC_T_DWORD dwLen)
132 {
133 ECLINKOS_UNREFPARM( dwLen );
134 ECLINKOS_UNREFPARM( pbyVirtual );
135 }
138 #ifdef ASSERT_SUSPEND
139 /********************************************************************************/
140 /** \brief
141 *
142 * \return
143 */
144 EC_T_VOID LinkOsDbgAssertFunc(EC_T_BOOL bAssertCondition, EC_T_CHAR* szFile, EC_T_DWORD dwLine)
145 {
146 if( !bAssertCondition )
147 {
148 LinkOsDbgMsg( "ASSERTION in file %s, line %d\n", (int)szFile, (int)dwLine, 3,4,5,6 );
149 }
150 }
151 #endif
153 /********************************************************************************/
154 /** \brief Add OS Layer Debug Message hook
155 *
156 * \return N/A
157 */
158 EC_T_VOID LinkOsAddDbgMsgHook(EC_PF_LINKOSDBGMSGHK pfOsDbgMsgHook)
159 {
160 S_pfLinkOsDbgMsgHook = pfOsDbgMsgHook;
161 }
163 extern "C" int UARTVprintf(const char* pStr, va_list vaArgs);
165 /********************************************************************************/
166 /** \brief Puts string to the system output and UART
167 *
168 */
169 int LinkOsVprintf(const char *szFormat, va_list vaArgs)
170 {
171 int res = xdc_runtime_System_printf_va__E(szFormat, vaArgs);
173 res = UARTVprintf(const_cast<char *>(szFormat), vaArgs);
175 return res;
176 }
178 /********************************************************************************/
179 /** \brief
180 *
181 * \return
182 */
183 EC_T_VOID LinkOsDbgMsg(const EC_T_CHAR* szFormat, ...)
184 {
185 EC_T_BOOL bPrintMsg = EC_TRUE;
186 va_list vaArgs;
188 va_start(vaArgs, szFormat);
189 if( S_pfLinkOsDbgMsgHook != EC_NULL )
190 {
191 bPrintMsg = (*S_pfLinkOsDbgMsgHook)(szFormat, vaArgs);
192 }
193 if( bPrintMsg )
194 {
195 LinkOsVprintf(szFormat, vaArgs);
196 }
197 va_end(vaArgs);
198 }
200 /*******************************************************************************
201 *
202 * SysInterruptInitialize - init and enable isr and irq
203 *
204 */
205 EC_T_BOOL LinkOsInterruptInitialize
206 ( EC_T_LINKOS_IRQ_PARM* pInterruptParm )
207 {
208 return EC_FALSE;
209 }
211 /*******************************************************************************
212 *
213 * SysInterruptDisable - disable irq
214 *
215 */
216 EC_T_VOID LinkOsInterruptDisable(EC_T_LINKOS_IRQ_PARM* pInterruptParm)
217 {
218 ECLINKOS_UNREFPARM(pInterruptParm);
219 }
221 void NoOsSleep(EC_T_DWORD dwMsec)
222 {
223 /* convert Msec to ticks */
224 EC_T_DWORD nTicks = (dwMsec * 1000) / Clock_tickPeriod;
226 if ( nTicks == 0 )
227 nTicks = 1;
229 Task_sleep(nTicks);
230 }
232 /*-END OF SOURCE FILE--------------------------------------------------------*/