port.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  3. * Copyright (C) 2021 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. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /* Kernel includes. */
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. #define portINITIAL_FORMAT_VECTOR ( ( StackType_t ) 0x4000 )
  32. /* Supervisor mode set. */
  33. #define portINITIAL_STATUS_REGISTER ( ( StackType_t ) 0x2000 )
  34. /* Used to keep track of the number of nested calls to taskENTER_CRITICAL(). This
  35. * will be set to 0 prior to the first task being started. */
  36. static uint32_t ulCriticalNesting = 0x9999UL;
  37. /*-----------------------------------------------------------*/
  38. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  39. TaskFunction_t pxCode,
  40. void * pvParameters )
  41. {
  42. *pxTopOfStack = ( StackType_t ) pvParameters;
  43. pxTopOfStack--;
  44. *pxTopOfStack = ( StackType_t ) 0xDEADBEEF;
  45. pxTopOfStack--;
  46. /* Exception stack frame starts with the return address. */
  47. *pxTopOfStack = ( StackType_t ) pxCode;
  48. pxTopOfStack--;
  49. *pxTopOfStack = ( portINITIAL_FORMAT_VECTOR << 16UL ) | ( portINITIAL_STATUS_REGISTER );
  50. pxTopOfStack--;
  51. *pxTopOfStack = ( StackType_t ) 0x0; /*FP*/
  52. pxTopOfStack -= 14; /* A5 to D0. */
  53. return pxTopOfStack;
  54. }
  55. /*-----------------------------------------------------------*/
  56. BaseType_t xPortStartScheduler( void )
  57. {
  58. extern void vPortStartFirstTask( void );
  59. ulCriticalNesting = 0UL;
  60. /* Configure the interrupts used by this port. */
  61. vApplicationSetupInterrupts();
  62. /* Start the first task executing. */
  63. vPortStartFirstTask();
  64. return pdFALSE;
  65. }
  66. /*-----------------------------------------------------------*/
  67. void vPortEndScheduler( void )
  68. {
  69. /* Not implemented as there is nothing to return to. */
  70. }
  71. /*-----------------------------------------------------------*/
  72. void vPortEnterCritical( void )
  73. {
  74. if( ulCriticalNesting == 0UL )
  75. {
  76. /* Guard against context switches being pended simultaneously with a
  77. * critical section being entered. */
  78. do
  79. {
  80. portDISABLE_INTERRUPTS();
  81. if( MCF_INTC0_INTFRCL == 0UL )
  82. {
  83. break;
  84. }
  85. portENABLE_INTERRUPTS();
  86. } while( 1 );
  87. }
  88. ulCriticalNesting++;
  89. }
  90. /*-----------------------------------------------------------*/
  91. void vPortExitCritical( void )
  92. {
  93. ulCriticalNesting--;
  94. if( ulCriticalNesting == 0 )
  95. {
  96. portENABLE_INTERRUPTS();
  97. }
  98. }
  99. /*-----------------------------------------------------------*/
  100. void vPortYieldHandler( void )
  101. {
  102. uint32_t ulSavedInterruptMask;
  103. ulSavedInterruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
  104. /* Note this will clear all forced interrupts - this is done for speed. */
  105. MCF_INTC0_INTFRCL = 0;
  106. vTaskSwitchContext();
  107. portCLEAR_INTERRUPT_MASK_FROM_ISR( ulSavedInterruptMask );
  108. }