cpu_port.c 21 KB

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