cpu_port.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. ************************************************************************************************************************
  3. * File : cpu_port.c
  4. * By : xyou
  5. * Version : V1.00.00
  6. *
  7. * By : prife
  8. * Version : V1.00.01
  9. ************************************************************************************************************************
  10. */
  11. /*
  12. *********************************************************************************************************
  13. * INCLUDE FILES
  14. *********************************************************************************************************
  15. */
  16. #include <rtthread.h>
  17. #include <windows.h>
  18. #include <mmsystem.h>
  19. #include <stdio.h>
  20. #include "cpu_port.h"
  21. /*
  22. *********************************************************************************************************
  23. * WinThread STRUCTURE
  24. * Windows runs each task in a thread.
  25. * The context switch is managed by the threads.So the task stack does not have to be managed directly,
  26. * although the stack stack is still used to hold an WinThreadState structure this is the only thing it
  27. * will be ever hold.
  28. * the structure indirectly maps the task handle to a thread handle
  29. *********************************************************************************************************
  30. */
  31. typedef struct
  32. {
  33. void *Param; //Thread param
  34. void (*Entry)(void *); //Thread entry
  35. void (*Exit)(void); //Thread exit
  36. HANDLE ThreadHandle;
  37. DWORD ThreadID;
  38. }win_thread_t;
  39. const DWORD MS_VC_EXCEPTION=0x406D1388;
  40. #pragma pack(push,8)
  41. typedef struct tagTHREADNAME_INFO
  42. {
  43. DWORD dwType; // Must be 0x1000.
  44. LPCSTR szName; // Pointer to name (in user addr space).
  45. DWORD dwThreadID; // Thread ID (-1=caller thread).
  46. DWORD dwFlags; // Reserved for future use, must be zero.
  47. } THREADNAME_INFO;
  48. #pragma pack(pop)
  49. /*
  50. *********************************************************************************************************
  51. * LOCAL DEFINES
  52. *********************************************************************************************************
  53. */
  54. #define MAX_INTERRUPT_NUM ((rt_uint32_t)sizeof(rt_uint32_t) * 8)
  55. /*
  56. * Simulated interrupt waiting to be processed.this is a bit mask where each bit represent one interrupt
  57. * so a maximum of 32 interrupts can be simulated
  58. */
  59. static volatile rt_uint32_t CpuPendingInterrupts = 0;
  60. /*
  61. * An event used to inform the simulated interrupt processing thread (a high priority thread
  62. * that simulated interrupt processing) that an interrupt is pending
  63. */
  64. static HANDLE hInterruptEventHandle = NULL;
  65. /*
  66. * Mutex used to protect all the simulated interrupt variables that are accessed by multiple threads
  67. */
  68. static HANDLE hInterruptEventMutex = NULL;
  69. /*
  70. * Handler for all the simulate software interrupts.
  71. * The first two positions are used the Yield and Tick interrupt so are handled slightly differently
  72. * all the other interrupts can be user defined
  73. */
  74. static rt_uint32_t (*CpuIsrHandler[MAX_INTERRUPT_NUM])(void) = {0};
  75. /*
  76. * Handler for OSTick Thread
  77. */
  78. static HANDLE OSTick_Thread;
  79. static DWORD OSTick_ThreadID;
  80. static HANDLE OSTick_SignalPtr;
  81. static TIMECAPS OSTick_TimerCap;
  82. static MMRESULT OSTick_TimerID;
  83. /*
  84. * flag in interrupt handling
  85. */
  86. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  87. rt_uint32_t rt_thread_switch_interrupt_flag;
  88. /*
  89. *********************************************************************************************************
  90. * PRIVATE FUNCTION PROTOTYPES
  91. *********************************************************************************************************
  92. */
  93. //static void WinThreadScheduler(void);
  94. void WinThreadScheduler(void);
  95. rt_uint32_t YieldInterruptHandle(void);
  96. rt_uint32_t SysTickInterruptHandle(void);
  97. static DWORD WINAPI ThreadforSysTickTimer(LPVOID lpParam);
  98. static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
  99. static void SetThreadName(DWORD dwThreadID, char* threadName)
  100. {
  101. THREADNAME_INFO info;
  102. info.dwType = 0x1000;
  103. info.szName = threadName;
  104. info.dwThreadID = dwThreadID;
  105. info.dwFlags = 0;
  106. __try
  107. {
  108. RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
  109. }
  110. __except(EXCEPTION_EXECUTE_HANDLER)
  111. {
  112. }
  113. }
  114. /*
  115. *********************************************************************************************************
  116. * rt_hw_stack_init()
  117. * Description : Initialize stack of thread
  118. * Argument(s) : void *pvEntry,void *pvParam,rt_uint8_t *pStackAddr,void *pvExit
  119. * Return(s) : rt_uint8_t*
  120. * Caller(s) : rt_thread_init or rt_thread_create
  121. * Note(s) : none
  122. *********************************************************************************************************
  123. */
  124. static DWORD WINAPI thread_run( LPVOID lpThreadParameter )
  125. {
  126. rt_thread_t tid = rt_thread_self();
  127. win_thread_t *pWinThread = (win_thread_t *)lpThreadParameter;
  128. SetThreadName(GetCurrentThreadId(), tid->name);
  129. pWinThread->Entry(pWinThread->Param);
  130. pWinThread->Exit();
  131. return 0;
  132. }
  133. rt_uint8_t* rt_hw_stack_init(void *pEntry,void *pParam,rt_uint8_t *pStackAddr,void *pExit)
  134. {
  135. win_thread_t *pWinThread = NULL;
  136. /*
  137. * In this simulated case a stack is not initialized
  138. * The thread handles the context switching itself. The WinThreadState object is placed onto the stack
  139. * that was created for the task
  140. * so the stack buffer is still used,just not in the conventional way.
  141. */
  142. pWinThread = (win_thread_t *)(pStackAddr - sizeof(win_thread_t));
  143. pWinThread->Entry = pEntry;
  144. pWinThread->Param = pParam;
  145. pWinThread->Exit = pExit;
  146. pWinThread->ThreadHandle = NULL;
  147. pWinThread->ThreadID = 0;
  148. /* Create the winthread */
  149. pWinThread->ThreadHandle = CreateThread(NULL,
  150. 0,
  151. (LPTHREAD_START_ROUTINE) thread_run,
  152. pWinThread,
  153. CREATE_SUSPENDED,
  154. &(pWinThread->ThreadID));
  155. SetThreadAffinityMask(pWinThread->ThreadHandle,
  156. 0x01);
  157. SetThreadPriorityBoost(pWinThread->ThreadHandle,
  158. TRUE);
  159. SetThreadPriority(pWinThread->ThreadHandle,
  160. THREAD_PRIORITY_IDLE);
  161. return (rt_uint8_t*)pWinThread;
  162. } /*** rt_hw_stack_init ***/
  163. /*
  164. *********************************************************************************************************
  165. * rt_hw_interrupt_disable()
  166. * Description : disable cpu interrupts
  167. * Argument(s) : void
  168. * Return(s) : rt_base_t
  169. * Caller(s) : Applicatios or os_kernel
  170. * Note(s) : none
  171. *********************************************************************************************************
  172. */
  173. rt_base_t rt_hw_interrupt_disable(void)
  174. {
  175. if(hInterruptEventMutex != NULL)
  176. {
  177. WaitForSingleObject(hInterruptEventMutex,INFINITE);
  178. }
  179. return 0;
  180. } /*** rt_hw_interrupt_disable ***/
  181. /*
  182. *********************************************************************************************************
  183. * rt_hw_interrupt_enable()
  184. * Description : enable cpu interrupts
  185. * Argument(s) : rt_base_t level
  186. * Return(s) : void
  187. * Caller(s) : Applications or os_kernel
  188. * Note(s) : none
  189. *********************************************************************************************************
  190. */
  191. void rt_hw_interrupt_enable(rt_base_t level)
  192. {
  193. level = level;
  194. if (hInterruptEventMutex != NULL)
  195. {
  196. ReleaseMutex(hInterruptEventMutex);
  197. }
  198. } /*** rt_hw_interrupt_enable ***/
  199. /*
  200. *********************************************************************************************************
  201. * rt_hw_context_switch_interrupt()
  202. * Description : switch thread's contex
  203. * Argument(s) : void
  204. * Return(s) : void
  205. * Caller(s) : os kernel
  206. * Note(s) : none
  207. *********************************************************************************************************
  208. */
  209. void rt_hw_context_switch_interrupt(rt_uint32_t from,
  210. rt_uint32_t to)
  211. {
  212. if(rt_thread_switch_interrupt_flag != 1)
  213. {
  214. rt_thread_switch_interrupt_flag = 1;
  215. // set rt_interrupt_from_thread
  216. rt_interrupt_from_thread = *((rt_uint32_t *)(from));
  217. }
  218. rt_interrupt_to_thread = *((rt_uint32_t *)(to));
  219. //trigger YIELD exception(cause context switch)
  220. TriggerSimulateInterrupt(CPU_INTERRUPT_YIELD);
  221. } /*** rt_hw_context_switch_interrupt ***/
  222. void rt_hw_context_switch(rt_uint32_t from,
  223. rt_uint32_t to)
  224. {
  225. if(rt_thread_switch_interrupt_flag != 1)
  226. {
  227. rt_thread_switch_interrupt_flag = 1;
  228. // set rt_interrupt_from_thread
  229. rt_interrupt_from_thread = *((rt_uint32_t *)(from));
  230. }
  231. // set rt_interrupt_to_thread
  232. rt_interrupt_to_thread = *((rt_uint32_t *)(to));
  233. //trigger YIELD exception(cause contex switch)
  234. TriggerSimulateInterrupt(CPU_INTERRUPT_YIELD);
  235. } /*** rt_hw_context_switch ***/
  236. /*
  237. *********************************************************************************************************
  238. * rt_hw_context_switch_to()
  239. * Description : switch to new thread
  240. * Argument(s) : rt_uint32_t to //the stack address of the thread which will switch to
  241. * Return(s) : void
  242. * Caller(s) : rt_thread schecale
  243. * Note(s) : this function is used to perform the first thread switch
  244. *********************************************************************************************************
  245. */
  246. void rt_hw_context_switch_to(rt_uint32_t to)
  247. {
  248. //set to thread
  249. rt_interrupt_to_thread = *((rt_uint32_t *)(to));
  250. //clear from thread
  251. rt_interrupt_from_thread = 0;
  252. //set interrupt to 1
  253. rt_thread_switch_interrupt_flag = 1;
  254. //start WinThreadScheduler
  255. WinThreadScheduler();
  256. //never reach here!
  257. return;
  258. } /*** rt_hw_context_switch_to ***/
  259. /*
  260. *********************************************************************************************************
  261. * TriggerSimulateInterrupt()
  262. * Description : Trigger a simulated interrupts handle
  263. * Argument(s) : t_uint32_t IntIndex
  264. * Return(s) : void
  265. * Caller(s) : Applications
  266. * Note(s) : none
  267. *********************************************************************************************************
  268. */
  269. void TriggerSimulateInterrupt(rt_uint32_t IntIndex)
  270. {
  271. if((IntIndex < MAX_INTERRUPT_NUM) && (hInterruptEventMutex != NULL))
  272. {
  273. /* Yield interrupts are processed even when critical nesting is non-zero */
  274. WaitForSingleObject(hInterruptEventMutex,
  275. INFINITE);
  276. CpuPendingInterrupts |= (1 << IntIndex);
  277. SetEvent(hInterruptEventHandle);
  278. ReleaseMutex(hInterruptEventMutex);
  279. }
  280. } /*** TriggerSimulateInterrupt ***/
  281. /*
  282. *********************************************************************************************************
  283. * RegisterSimulateInterrupt()
  284. * Description : Register a interrupt handle to simulate paltform
  285. * Argument(s) : rt_uint32_t IntIndex,rt_uint32_t (*IntHandler)(void)
  286. * Return(s) : void
  287. * Caller(s) : Applications
  288. * Note(s) : none
  289. *********************************************************************************************************
  290. */
  291. void RegisterSimulateInterrupt(rt_uint32_t IntIndex,rt_uint32_t (*IntHandler)(void))
  292. {
  293. if(IntIndex < MAX_INTERRUPT_NUM)
  294. {
  295. if (hInterruptEventMutex != NULL)
  296. {
  297. WaitForSingleObject(hInterruptEventMutex,
  298. INFINITE);
  299. CpuIsrHandler[IntIndex] = IntHandler;
  300. ReleaseMutex(hInterruptEventMutex);
  301. }
  302. else
  303. {
  304. CpuIsrHandler[IntIndex] = IntHandler;
  305. }
  306. }
  307. } /*** RegisterSimulateInterrupt ***/
  308. /*
  309. *********************************************************************************************************
  310. * PRIVATE FUNCTION
  311. *********************************************************************************************************
  312. */
  313. /*
  314. *********************************************************************************************************
  315. * WinThreadScheduler()
  316. * Description : Handle all simulate interrupts
  317. * Argument(s) : void
  318. * Return(s) : static void
  319. * Caller(s) : os scachle
  320. * Note(s) : none
  321. *********************************************************************************************************
  322. */
  323. #define WIN_WM_MIN_RES (1)
  324. void WinThreadScheduler(void)
  325. {
  326. HANDLE hInterruptObjectList[2];
  327. HANDLE hThreadHandle;
  328. rt_uint32_t SwitchRequiredMask;
  329. rt_uint32_t i;
  330. win_thread_t *WinThreadFrom;
  331. win_thread_t *WinThreadTo;
  332. /*
  333. * Install the interrupt handlers used bye scheduler itself
  334. */
  335. RegisterSimulateInterrupt(CPU_INTERRUPT_YIELD,
  336. YieldInterruptHandle);
  337. RegisterSimulateInterrupt(CPU_INTERRUPT_TICK,
  338. SysTickInterruptHandle);
  339. /*
  340. * Create the events and mutex that are used to synchronise all the WinThreads
  341. */
  342. hInterruptEventMutex = CreateMutex(NULL,
  343. FALSE,
  344. NULL);
  345. hInterruptEventHandle = CreateEvent(NULL,
  346. FALSE,
  347. FALSE,
  348. NULL);
  349. if((hInterruptEventMutex == NULL) || (hInterruptEventHandle == NULL))
  350. {
  351. return;
  352. }
  353. /*
  354. * Set the priority of this WinThread such that it is above the priority of the WinThreads
  355. * that run rt-threads.
  356. * This is higher priority is required to ensure simulate interrupts take priority over rt-threads
  357. */
  358. hThreadHandle = GetCurrentThread();
  359. if(hThreadHandle == NULL)
  360. {
  361. return;
  362. }
  363. if (SetThreadPriority(hThreadHandle,
  364. THREAD_PRIORITY_HIGHEST) == 0)
  365. {
  366. return;
  367. }
  368. SetThreadPriorityBoost(hThreadHandle,
  369. TRUE);
  370. SetThreadAffinityMask(hThreadHandle,
  371. 0x01);
  372. /*
  373. * Start the thread that simulates the timer peripheral to generate tick interrupts.
  374. */
  375. OSTick_Thread = CreateThread(NULL,
  376. 0,
  377. ThreadforSysTickTimer,
  378. 0,
  379. CREATE_SUSPENDED,
  380. &OSTick_ThreadID);
  381. if(OSTick_Thread == NULL)
  382. {
  383. //Display Error Message
  384. return;
  385. }
  386. SetThreadPriority(OSTick_Thread,
  387. THREAD_PRIORITY_NORMAL);
  388. SetThreadPriorityBoost(OSTick_Thread,
  389. TRUE);
  390. SetThreadAffinityMask(OSTick_Thread,
  391. 0x01);
  392. /*
  393. * Set timer Caps
  394. */
  395. if (timeGetDevCaps(&OSTick_TimerCap,
  396. sizeof(OSTick_TimerCap)) != TIMERR_NOERROR)
  397. {
  398. CloseHandle(OSTick_Thread);
  399. return;
  400. }
  401. if (OSTick_TimerCap.wPeriodMin < WIN_WM_MIN_RES)
  402. {
  403. OSTick_TimerCap.wPeriodMin = WIN_WM_MIN_RES;
  404. }
  405. if(timeBeginPeriod(OSTick_TimerCap.wPeriodMin) != TIMERR_NOERROR)
  406. {
  407. CloseHandle(OSTick_Thread);
  408. return;
  409. }
  410. OSTick_SignalPtr = CreateEvent(NULL,TRUE,FALSE,NULL);
  411. if(OSTick_SignalPtr == NULL)
  412. {
  413. // disp error message
  414. timeEndPeriod(OSTick_TimerCap.wPeriodMin);
  415. CloseHandle(OSTick_Thread);
  416. return;
  417. }
  418. OSTick_TimerID = timeSetEvent((UINT ) (1000 / RT_TICK_PER_SECOND) ,
  419. (UINT ) OSTick_TimerCap.wPeriodMin,
  420. (LPTIMECALLBACK ) OSTick_SignalPtr,
  421. (DWORD_PTR ) NULL,
  422. (UINT ) (TIME_PERIODIC | TIME_CALLBACK_EVENT_SET));
  423. if(OSTick_TimerID == 0)
  424. {
  425. //disp
  426. CloseHandle(OSTick_SignalPtr);
  427. timeEndPeriod(OSTick_TimerCap.wPeriodMin);
  428. CloseHandle(OSTick_Thread);
  429. return;
  430. }
  431. /*
  432. * Start OS Tick Thread an release Interrupt Mutex
  433. */
  434. ResumeThread(OSTick_Thread);
  435. ReleaseMutex( hInterruptEventMutex );
  436. //trigger YEILD INTERRUPT
  437. TriggerSimulateInterrupt(CPU_INTERRUPT_YIELD);
  438. /*
  439. * block on the mutex that ensure exclusive access to the simulated interrupt objects
  440. * and the events that signals that a simulated interrupt should be processed.
  441. */
  442. hInterruptObjectList[0] = hInterruptEventHandle;
  443. hInterruptObjectList[1] = hInterruptEventMutex;
  444. while (1)
  445. {
  446. WaitForMultipleObjects(sizeof(hInterruptObjectList) / sizeof(HANDLE),
  447. hInterruptObjectList,
  448. TRUE,
  449. INFINITE);
  450. /*
  451. * Used to indicate whether the simulate interrupt processing has necessitated a contex
  452. * switch to another thread
  453. */
  454. SwitchRequiredMask = 0;
  455. /*
  456. * For each interrupt we are interested in processing ,each of which is represented
  457. * by a bit in the 32bit CpuPendingInterrupts variable.
  458. */
  459. for (i = 0; i < MAX_INTERRUPT_NUM; ++i)
  460. {
  461. /* is the simulated interrupt pending ? */
  462. if (CpuPendingInterrupts & (1UL << i))
  463. {
  464. /* Is a handler installed ?*/
  465. if (CpuIsrHandler[i] != NULL)
  466. {
  467. /* Run the actual handler */
  468. if (CpuIsrHandler[i]() != 0)
  469. {
  470. SwitchRequiredMask |= (1UL << i);
  471. }
  472. }
  473. /* Clear the interrupt pending bit */
  474. CpuPendingInterrupts &= ~(1UL << i);
  475. }
  476. }
  477. if(SwitchRequiredMask != 0)
  478. {
  479. WinThreadFrom = (win_thread_t *)rt_interrupt_from_thread;
  480. WinThreadTo = (win_thread_t *)rt_interrupt_to_thread;
  481. if ((WinThreadFrom != NULL) && (WinThreadFrom->ThreadHandle != NULL))
  482. {
  483. SuspendThread(WinThreadFrom->ThreadHandle);
  484. }
  485. ResumeThread(WinThreadTo->ThreadHandle);
  486. }
  487. ReleaseMutex(hInterruptEventMutex);
  488. }
  489. } /*** WinThreadScheduler ***/
  490. /*
  491. *********************************************************************************************************
  492. * ThreadforSysTickTimer()
  493. * Description : win thread to simulate a systick timer
  494. * Argument(s) : LPVOID lpParam
  495. * Return(s) : static DWORD WINAPI
  496. * Caller(s) : none
  497. * Note(s) : This is not a real time way of generating tick events as the next wake time should be relative
  498. * to the previous wake time,not the time Sleep() is called.
  499. * It is done this way to prevent overruns in this very non real time simulated/emulated environment
  500. *********************************************************************************************************
  501. */
  502. static DWORD WINAPI ThreadforSysTickTimer(LPVOID lpParam)
  503. {
  504. (void)lpParam; //prevent compiler warnings
  505. for(;;)
  506. {
  507. /*
  508. * Wait until the timer expires and we can access the simulated interrupt variables.
  509. */
  510. WaitForSingleObject(OSTick_SignalPtr,INFINITE);
  511. ResetEvent(OSTick_SignalPtr);
  512. /*
  513. * Trigger a systick interrupt
  514. */
  515. TriggerSimulateInterrupt(CPU_INTERRUPT_TICK);
  516. }
  517. return 0;
  518. } /*** prvThreadforSysTickTimer ***/
  519. /*
  520. *********************************************************************************************************
  521. * SysTickInterruptHandle()
  522. * Description : Interrupt handle for systick
  523. * Argument(s) : void
  524. * Return(s) : rt_uint32_t
  525. * Caller(s) : none
  526. * Note(s) : none
  527. *********************************************************************************************************
  528. */
  529. rt_uint32_t SysTickInterruptHandle(void)
  530. {
  531. /* enter interrupt */
  532. rt_interrupt_enter();
  533. rt_tick_increase();
  534. /* leave interrupt */
  535. rt_interrupt_leave();
  536. return 0;
  537. } /*** SysTickInterruptHandle ***/
  538. /*
  539. *********************************************************************************************************
  540. * YieldInterruptHandle()
  541. * Description : Interrupt handle for Yield
  542. * Argument(s) : void
  543. * Return(s) : rt_uint32_t
  544. * Caller(s) : none
  545. * Note(s) : none
  546. *********************************************************************************************************
  547. */
  548. rt_uint32_t YieldInterruptHandle(void)
  549. {
  550. /*
  551. * if rt_thread_switch_interrupt_flag = 1 yield already handled
  552. */
  553. if(rt_thread_switch_interrupt_flag != 0)
  554. {
  555. rt_thread_switch_interrupt_flag = 0;
  556. /* return thread switch request = 1 */
  557. return 1;
  558. }
  559. return 0;
  560. } /*** YieldInterruptHandle ***/