Ram Gandikota
/
ABCD
A metronome using the FRDM K64F board
pal/Source/PAL-Impl/pal_init.c@0:a7a43371b306, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:40:18 2017 +0000
- Revision:
- 0:a7a43371b306
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:a7a43371b306 | 1 | /* |
ram54288 | 0:a7a43371b306 | 2 | * Copyright (c) 2016 ARM Limited. All rights reserved. |
ram54288 | 0:a7a43371b306 | 3 | * SPDX-License-Identifier: Apache-2.0 |
ram54288 | 0:a7a43371b306 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
ram54288 | 0:a7a43371b306 | 5 | * not use this file except in compliance with the License. |
ram54288 | 0:a7a43371b306 | 6 | * You may obtain a copy of the License at |
ram54288 | 0:a7a43371b306 | 7 | * |
ram54288 | 0:a7a43371b306 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:a7a43371b306 | 9 | * |
ram54288 | 0:a7a43371b306 | 10 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:a7a43371b306 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
ram54288 | 0:a7a43371b306 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:a7a43371b306 | 13 | * See the License for the specific language governing permissions and |
ram54288 | 0:a7a43371b306 | 14 | * limitations under the License. |
ram54288 | 0:a7a43371b306 | 15 | */ |
ram54288 | 0:a7a43371b306 | 16 | |
ram54288 | 0:a7a43371b306 | 17 | |
ram54288 | 0:a7a43371b306 | 18 | #include "pal.h" |
ram54288 | 0:a7a43371b306 | 19 | #include "pal_plat_rtos.h" |
ram54288 | 0:a7a43371b306 | 20 | #include "pal_plat_network.h" |
ram54288 | 0:a7a43371b306 | 21 | #include "pal_macros.h" |
ram54288 | 0:a7a43371b306 | 22 | |
ram54288 | 0:a7a43371b306 | 23 | //this variable must be a int32_t for using atomic increment |
ram54288 | 0:a7a43371b306 | 24 | static int32_t g_palIntialized = 0; |
ram54288 | 0:a7a43371b306 | 25 | |
ram54288 | 0:a7a43371b306 | 26 | |
ram54288 | 0:a7a43371b306 | 27 | palStatus_t pal_init() |
ram54288 | 0:a7a43371b306 | 28 | { |
ram54288 | 0:a7a43371b306 | 29 | |
ram54288 | 0:a7a43371b306 | 30 | palStatus_t status = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 31 | int32_t currentInitValue; |
ram54288 | 0:a7a43371b306 | 32 | // get the return value of g_palIntialized+1 to save it locally |
ram54288 | 0:a7a43371b306 | 33 | currentInitValue = pal_osAtomicIncrement(&g_palIntialized,1); |
ram54288 | 0:a7a43371b306 | 34 | // if increased for the 1st time |
ram54288 | 0:a7a43371b306 | 35 | if (1 == currentInitValue) |
ram54288 | 0:a7a43371b306 | 36 | { |
ram54288 | 0:a7a43371b306 | 37 | DEBUG_PRINT("Init for the 1st time, initializing the modules\r\n"); |
ram54288 | 0:a7a43371b306 | 38 | status = pal_plat_RTOSInitialize(NULL); |
ram54288 | 0:a7a43371b306 | 39 | if (PAL_SUCCESS == status) |
ram54288 | 0:a7a43371b306 | 40 | { |
ram54288 | 0:a7a43371b306 | 41 | |
ram54288 | 0:a7a43371b306 | 42 | status = pal_plat_socketsInit(NULL); |
ram54288 | 0:a7a43371b306 | 43 | if (PAL_SUCCESS != status) |
ram54288 | 0:a7a43371b306 | 44 | { |
ram54288 | 0:a7a43371b306 | 45 | DEBUG_PRINT("init of network module has failed with status %d\r\n",status); |
ram54288 | 0:a7a43371b306 | 46 | } |
ram54288 | 0:a7a43371b306 | 47 | } |
ram54288 | 0:a7a43371b306 | 48 | else |
ram54288 | 0:a7a43371b306 | 49 | { |
ram54288 | 0:a7a43371b306 | 50 | DEBUG_PRINT("init of RTOS module has failed with status %d\r\n",status); |
ram54288 | 0:a7a43371b306 | 51 | } |
ram54288 | 0:a7a43371b306 | 52 | } |
ram54288 | 0:a7a43371b306 | 53 | // if failed decrees the value of g_palIntialized |
ram54288 | 0:a7a43371b306 | 54 | if (PAL_SUCCESS != status) |
ram54288 | 0:a7a43371b306 | 55 | { |
ram54288 | 0:a7a43371b306 | 56 | pal_plat_socketsTerminate(NULL); |
ram54288 | 0:a7a43371b306 | 57 | pal_plat_RTOSDestroy(); |
ram54288 | 0:a7a43371b306 | 58 | pal_osAtomicIncrement(&g_palIntialized, -1); |
ram54288 | 0:a7a43371b306 | 59 | } |
ram54288 | 0:a7a43371b306 | 60 | return status; |
ram54288 | 0:a7a43371b306 | 61 | } |
ram54288 | 0:a7a43371b306 | 62 | |
ram54288 | 0:a7a43371b306 | 63 | |
ram54288 | 0:a7a43371b306 | 64 | void pal_destroy() |
ram54288 | 0:a7a43371b306 | 65 | { |
ram54288 | 0:a7a43371b306 | 66 | int32_t currentInitValue; |
ram54288 | 0:a7a43371b306 | 67 | // get the current value of g_palIntialized locally |
ram54288 | 0:a7a43371b306 | 68 | currentInitValue = pal_osAtomicIncrement(&g_palIntialized, -1); |
ram54288 | 0:a7a43371b306 | 69 | if (0 == currentInitValue) |
ram54288 | 0:a7a43371b306 | 70 | { |
ram54288 | 0:a7a43371b306 | 71 | DEBUG_PRINT("Destroying modules\r\n"); |
ram54288 | 0:a7a43371b306 | 72 | pal_plat_RTOSDestroy(); |
ram54288 | 0:a7a43371b306 | 73 | pal_plat_socketsTerminate(NULL); |
ram54288 | 0:a7a43371b306 | 74 | } |
ram54288 | 0:a7a43371b306 | 75 | } |