cpu_port.c 21 KB

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