FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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