Contiki 2.6

micro.c

Go to the documentation of this file.
00001 /** @file micro.c
00002  * @brief STM32W108 micro specific minimal HAL functions
00003  *
00004  *
00005  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved.        -->
00006  */
00007 
00008 #include PLATFORM_HEADER
00009 #include BOARD_HEADER
00010 #include "error.h"
00011 #include "hal/micro/micro-common.h"
00012 #include "hal/micro/cortexm3/micro-common.h"
00013 #include "micro/system-timer.h"
00014 #include "micro/adc.h"
00015 #include "micro/cortexm3/memmap.h"
00016 #include "micro/cortexm3/iap_bootloader.h"
00017 
00018 #include <stdlib.h>
00019 #include <string.h>
00020 
00021 extern void halBoardInit(void);
00022 
00023 void halInit(void)
00024 {
00025   //Disable the REG_EN external regulator enable signal.  Out of reset this
00026   //signal overrides PA7.  By disabling it early, PA7 is reclaimed as a GPIO.
00027   //If an external regulator is required, the following line of code should
00028   //be deleted.
00029   GPIO_DBGCFG &= ~GPIO_EXTREGEN;
00030   halInternalSetRegTrim(FALSE);
00031   halBoardInit();
00032   halPowerUp();
00033   halInternalCalibrateFastRc();
00034   
00035   #ifndef DISABLE_WATCHDOG
00036     halInternalEnableWatchDog();
00037   #endif
00038 
00039   halInternalStartSystemTimer();
00040 }
00041 
00042 
00043 void halReboot(void)
00044 {
00045   INTERRUPTS_OFF();
00046   
00047 
00048   //FCLK must be 6MHz to allow the SYSRESETREQ signal to cleanly
00049   //propagate and reset the chip.  Switch SYSCLK first since we need
00050   //the cycles used by switching FCLK to guarantee the SYSCLK is
00051   //stable and ready for SYSRESETREQ.
00052   OSC24M_CTRL = OSC24M_CTRL_RESET; //Guarantee SYSCLK is sourced from OSCHF
00053   CPU_CLKSEL = CPU_CLKSEL_RESET;   //Guarantee FCLK is sourced from PCLK
00054   
00055   SCS_AIRCR = (0x05FA0000 | SCS_AIRCR_SYSRESETREQ); // trigger the reset
00056   //NOTE: SYSRESETREQ is not the same as nRESET.  It will not do the debug
00057   //pieces: DWT, ITM, FPB, vector catch, etc
00058 }
00059 
00060 void halPowerDown(void)
00061 {
00062   halBoardPowerDown();
00063 }
00064 
00065 void halPowerUp(void)
00066 {
00067   halInternalInitAdc();
00068   halCommonCalibratePads();  
00069   halInternalSwitchToXtal();
00070   halBoardPowerUp();
00071 }
00072 
00073 static int16u seed0 = 0xbeef;
00074 static int16u seed1 = 0xface;
00075 
00076 void halCommonSeedRandom(int32u seed)
00077 {
00078   seed0 = (int16u) seed;
00079   if (seed0 == 0)
00080     seed0 = 0xbeef;
00081   seed1 = (int16u) (seed >> 16);
00082   if (seed1 == 0)
00083     seed1 = 0xface;
00084 }
00085 
00086 static int16u shift(int16u *val, int16u taps)
00087 {
00088   int16u newVal = *val;
00089 
00090   if (newVal & 0x8000)
00091     newVal ^= taps;
00092   *val = newVal << 1;
00093   return newVal;
00094 }
00095 
00096 int16u halCommonGetRandom(void)
00097 {
00098   return (shift(&seed0, 0x0062)
00099           ^ shift(&seed1, 0x100B));
00100 }
00101 
00102 void halCommonMemCopy(void *dest, const void *source, int8u bytes)
00103 {
00104   memcpy(dest, source, bytes);
00105 }
00106 
00107 int8s halCommonMemCompare(const void *source0, const void *source1, int8u bytes)
00108 {
00109   return memcmp(source0, source1, bytes);
00110 }
00111 
00112 void halCommonMemSet(void *dest, int8u val, int16u bytes)
00113 {
00114   memset(dest, val, bytes);
00115 }
00116 
00117 #pragma pack(1)
00118 typedef struct appSwitchStruct {
00119   int32u signature;
00120   int8u mode;
00121   int8u channel;
00122   union {
00123     int16u panID;
00124     int16u offset;
00125   } param;
00126 } appSwitchStructType;
00127 #pragma pack()
00128 static appSwitchStructType *appSwitch = (appSwitchStructType *) RAM_BOTTOM;
00129 
00130 StStatus halBootloaderStart(int8u mode, int8u channel, int16u panID)
00131 {
00132     if (mode ==  IAP_BOOTLOADER_MODE_UART) {
00133       int8u cut = *(volatile int8u *) 0x08040798;
00134       if (!( (halFixedAddressTable.baseTable.type == FIXED_ADDRESS_TABLE_TYPE) &&
00135            ( ( (halFixedAddressTable.baseTable.version & FAT_MAJOR_VERSION_MASK) 
00136                == 0x0000 ) &&
00137              (halFixedAddressTable.baseTable.version == 0x0003) //checking presence of valid version 
00138            ) && (cut >= 2) && (cut <= 3)))     
00139         /* Cut not supported */
00140         return ST_ERR_FATAL;
00141     } else {
00142       /* Check that OTA bootloader is at the base of the flash */
00143       if (*((int32u *) (MFB_BOTTOM + 28)) == IAP_BOOTLOADER_APP_SWITCH_SIGNATURE) {
00144          appSwitch->channel = ((channel >= 11) && (channel <= 26)) ? channel :IAP_BOOTLOADER_DEFAULT_CHANNEL;
00145          appSwitch->param.panID = panID;
00146       } else {
00147           return ST_ERR_FATAL;
00148       }
00149     }
00150     appSwitch->signature = IAP_BOOTLOADER_APP_SWITCH_SIGNATURE;
00151     appSwitch->mode = mode;
00152     halReboot();
00153   
00154   return (mode <= IAP_BOOTLOADER_MODE_OTA) ? ST_ERR_FATAL: ST_BAD_ARGUMENT;
00155 }