port.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  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. /* Scheduler includes. */
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. /*-----------------------------------------------------------
  32. * Implementation of functions defined in portable.h for the MSP430 port.
  33. *----------------------------------------------------------*/
  34. /* Constants required for hardware setup. The tick ISR runs off the ACLK,
  35. not the MCLK. */
  36. #define portACLK_FREQUENCY_HZ ( ( TickType_t ) 32768 )
  37. #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
  38. #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x08 )
  39. /* We require the address of the pxCurrentTCB variable, but don't want to know
  40. any details of its type. */
  41. typedef void TCB_t;
  42. extern volatile TCB_t * volatile pxCurrentTCB;
  43. /* Each task maintains a count of the critical section nesting depth. Each
  44. time a critical section is entered the count is incremented. Each time a
  45. critical section is exited the count is decremented - with interrupts only
  46. being re-enabled if the count is zero.
  47. usCriticalNesting will get set to zero when the scheduler starts, but must
  48. not be initialised to zero as this will cause problems during the startup
  49. sequence. */
  50. volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
  51. /*-----------------------------------------------------------*/
  52. /*
  53. * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
  54. * could have alternatively used the watchdog timer or timer 1.
  55. */
  56. void prvSetupTimerInterrupt( void );
  57. /*-----------------------------------------------------------*/
  58. /*
  59. * Initialise the stack of a task to look exactly as if a call to
  60. * portSAVE_CONTEXT had been called.
  61. *
  62. * See the header file portable.h.
  63. */
  64. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  65. {
  66. /*
  67. Place a few bytes of known values on the bottom of the stack.
  68. This is just useful for debugging and can be included if required.
  69. *pxTopOfStack = ( StackType_t ) 0x1111;
  70. pxTopOfStack--;
  71. *pxTopOfStack = ( StackType_t ) 0x2222;
  72. pxTopOfStack--;
  73. *pxTopOfStack = ( StackType_t ) 0x3333;
  74. pxTopOfStack--;
  75. */
  76. /* The msp430 automatically pushes the PC then SR onto the stack before
  77. executing an ISR. We want the stack to look just as if this has happened
  78. so place a pointer to the start of the task on the stack first - followed
  79. by the flags we want the task to use when it starts up. */
  80. *pxTopOfStack = ( StackType_t ) pxCode;
  81. pxTopOfStack--;
  82. *pxTopOfStack = portFLAGS_INT_ENABLED;
  83. pxTopOfStack--;
  84. /* Next the general purpose registers. */
  85. *pxTopOfStack = ( StackType_t ) 0x4444;
  86. pxTopOfStack--;
  87. *pxTopOfStack = ( StackType_t ) 0x5555;
  88. pxTopOfStack--;
  89. *pxTopOfStack = ( StackType_t ) 0x6666;
  90. pxTopOfStack--;
  91. *pxTopOfStack = ( StackType_t ) 0x7777;
  92. pxTopOfStack--;
  93. *pxTopOfStack = ( StackType_t ) 0x8888;
  94. pxTopOfStack--;
  95. *pxTopOfStack = ( StackType_t ) 0x9999;
  96. pxTopOfStack--;
  97. *pxTopOfStack = ( StackType_t ) 0xaaaa;
  98. pxTopOfStack--;
  99. *pxTopOfStack = ( StackType_t ) 0xbbbb;
  100. pxTopOfStack--;
  101. *pxTopOfStack = ( StackType_t ) 0xcccc;
  102. pxTopOfStack--;
  103. *pxTopOfStack = ( StackType_t ) 0xdddd;
  104. pxTopOfStack--;
  105. *pxTopOfStack = ( StackType_t ) 0xeeee;
  106. pxTopOfStack--;
  107. /* When the task starts is will expect to find the function parameter in
  108. R15. */
  109. *pxTopOfStack = ( StackType_t ) pvParameters;
  110. pxTopOfStack--;
  111. /* A variable is used to keep track of the critical section nesting.
  112. This variable has to be stored as part of the task context and is
  113. initially set to zero. */
  114. *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
  115. /* Return a pointer to the top of the stack we have generated so this can
  116. be stored in the task control block for the task. */
  117. return pxTopOfStack;
  118. }
  119. /*-----------------------------------------------------------*/
  120. void vPortEndScheduler( void )
  121. {
  122. /* It is unlikely that the MSP430 port will get stopped. If required simply
  123. disable the tick interrupt here. */
  124. }
  125. /*-----------------------------------------------------------*/
  126. /*
  127. * Hardware initialisation to generate the RTOS tick. This uses timer 0
  128. * but could alternatively use the watchdog timer or timer 1.
  129. */
  130. void prvSetupTimerInterrupt( void )
  131. {
  132. /* Ensure the timer is stopped. */
  133. TACTL = 0;
  134. /* Run the timer of the ACLK. */
  135. TACTL = TASSEL_1;
  136. /* Clear everything to start with. */
  137. TACTL |= TACLR;
  138. /* Set the compare match value according to the tick rate we want. */
  139. TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
  140. /* Enable the interrupts. */
  141. TACCTL0 = CCIE;
  142. /* Start up clean. */
  143. TACTL |= TACLR;
  144. /* Up mode. */
  145. TACTL |= MC_1;
  146. }
  147. /*-----------------------------------------------------------*/