port.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 MSP430X 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 vPortSetupTimerInterrupt( 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. uint16_t *pusTopOfStack;
  67. uint32_t *pulTopOfStack, ulTemp;
  68. /*
  69. Place a few bytes of known values on the bottom of the stack.
  70. This is just useful for debugging and can be included if required.
  71. *pxTopOfStack = ( StackType_t ) 0x1111;
  72. pxTopOfStack--;
  73. *pxTopOfStack = ( StackType_t ) 0x2222;
  74. pxTopOfStack--;
  75. *pxTopOfStack = ( StackType_t ) 0x3333;
  76. pxTopOfStack--;
  77. */
  78. /* Data types are need either 16 bits or 32 bits depending on the data
  79. and code model used. */
  80. if( sizeof( pxCode ) == sizeof( uint16_t ) )
  81. {
  82. pusTopOfStack = ( uint16_t * ) pxTopOfStack;
  83. ulTemp = ( uint32_t ) pxCode;
  84. *pusTopOfStack = ( uint16_t ) ulTemp;
  85. }
  86. else
  87. {
  88. /* Make room for a 20 bit value stored as a 32 bit value. */
  89. pusTopOfStack = ( uint16_t * ) pxTopOfStack;
  90. pusTopOfStack--;
  91. pulTopOfStack = ( uint32_t * ) pusTopOfStack;
  92. *pulTopOfStack = ( uint32_t ) pxCode;
  93. }
  94. pusTopOfStack--;
  95. *pusTopOfStack = portFLAGS_INT_ENABLED;
  96. pusTopOfStack -= ( sizeof( StackType_t ) / 2 );
  97. /* From here on the size of stacked items depends on the memory model. */
  98. pxTopOfStack = ( StackType_t * ) pusTopOfStack;
  99. /* Next the general purpose registers. */
  100. #ifdef PRELOAD_REGISTER_VALUES
  101. *pxTopOfStack = ( StackType_t ) 0xffff;
  102. pxTopOfStack--;
  103. *pxTopOfStack = ( StackType_t ) 0xeeee;
  104. pxTopOfStack--;
  105. *pxTopOfStack = ( StackType_t ) 0xdddd;
  106. pxTopOfStack--;
  107. *pxTopOfStack = ( StackType_t ) pvParameters;
  108. pxTopOfStack--;
  109. *pxTopOfStack = ( StackType_t ) 0xbbbb;
  110. pxTopOfStack--;
  111. *pxTopOfStack = ( StackType_t ) 0xaaaa;
  112. pxTopOfStack--;
  113. *pxTopOfStack = ( StackType_t ) 0x9999;
  114. pxTopOfStack--;
  115. *pxTopOfStack = ( StackType_t ) 0x8888;
  116. pxTopOfStack--;
  117. *pxTopOfStack = ( StackType_t ) 0x5555;
  118. pxTopOfStack--;
  119. *pxTopOfStack = ( StackType_t ) 0x6666;
  120. pxTopOfStack--;
  121. *pxTopOfStack = ( StackType_t ) 0x5555;
  122. pxTopOfStack--;
  123. *pxTopOfStack = ( StackType_t ) 0x4444;
  124. pxTopOfStack--;
  125. #else
  126. pxTopOfStack -= 3;
  127. *pxTopOfStack = ( StackType_t ) pvParameters;
  128. pxTopOfStack -= 9;
  129. #endif
  130. /* A variable is used to keep track of the critical section nesting.
  131. This variable has to be stored as part of the task context and is
  132. initially set to zero. */
  133. *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
  134. /* Return a pointer to the top of the stack we have generated so this can
  135. be stored in the task control block for the task. */
  136. return pxTopOfStack;
  137. }
  138. /*-----------------------------------------------------------*/
  139. void vPortEndScheduler( void )
  140. {
  141. /* It is unlikely that the MSP430 port will get stopped. If required simply
  142. disable the tick interrupt here. */
  143. }
  144. /*-----------------------------------------------------------*/
  145. /*
  146. * Hardware initialisation to generate the RTOS tick.
  147. */
  148. void vPortSetupTimerInterrupt( void )
  149. {
  150. vApplicationSetupTimerInterrupt();
  151. }
  152. /*-----------------------------------------------------------*/
  153. #pragma vector=configTICK_VECTOR
  154. interrupt void vTickISREntry( void )
  155. {
  156. extern void vPortTickISR( void );
  157. __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
  158. #if configUSE_PREEMPTION == 1
  159. extern void vPortPreemptiveTickISR( void );
  160. vPortPreemptiveTickISR();
  161. #else
  162. extern void vPortCooperativeTickISR( void );
  163. vPortCooperativeTickISR();
  164. #endif
  165. }