Contiki 2.6

board.c

Go to the documentation of this file.
00001 /** @file board.c
00002  * @brief Board file x STM32W108 Kits boards 
00003  *
00004  * This file implements a software layer to support all the ST kits boards
00005  * and deal with the difference in leds, buttons and sensors connected to the board.
00006  * This a bit too complex for customer boards adaptation and customer can simplify it
00007  * to adapt it to their hardware environment as they normally don't need to have the software
00008  * running on several boards.
00009  * The boards supported are:
00010  * - MB851 A
00011  * - MB851 B
00012  * - MB851 C
00013  * - MB954 A
00014  * - MB954 B
00015  * - MB950 A
00016  * - MB951 A
00017  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved.        -->
00018  */
00019 #include PLATFORM_HEADER
00020 #include BOARD_HEADER
00021 #ifdef EMBERZNET_HAL
00022 #include "stack/include/ember.h"
00023 #include "stack/include/error.h"
00024 #include "hal/hal.h"
00025 #define PORTA (0 << 3)
00026 #define PORTB (1 << 3)
00027 #define PORTC (2 << 3)
00028 #else
00029 #include "error.h"
00030 #include "hal/hal.h"
00031 #include "hal/micro/cortexm3/mfg-token.h"
00032 #endif
00033 #include <string.h>
00034 
00035 const LedResourceType LedsMB851A[] = {
00036   {
00037     "D1", /* Green LED */
00038     PORTB,
00039     6
00040   },
00041   {
00042     "D3", /* Yellow LED */
00043     PORTB,
00044     5
00045   }
00046 };
00047 
00048 const InfraRedLedResourceType infraRedLedMB851A = {
00049   "D5", /* Infrared LED */
00050   PORTB,
00051   4
00052 };
00053 
00054 
00055 const LedResourceType LedsMB954A[] = {
00056   {
00057     "D1", /* Green LED */
00058     PORTA,
00059     5
00060   },
00061   {
00062     "D3", /* Yellow LED */
00063     PORTA,
00064     0
00065   }
00066 };
00067 
00068 const ButtonResourceType ButtonsMB851A[] = {
00069   {
00070     "S1",
00071     PORTA,
00072     7
00073   }
00074 };
00075 
00076 const ButtonResourceType ButtonsMB954A[] = {
00077   {
00078     "S1",
00079     PORTB,
00080     3
00081   }
00082 };
00083 
00084 const ButtonResourceType ButtonsMB950A[] = {
00085   {
00086     "S1",
00087     PORTB,
00088     3
00089   },
00090   {
00091     "S2",
00092     PORTA,
00093     7
00094   },
00095   {
00096     "S3",
00097     PORTB,
00098     7
00099   },
00100   {
00101     "S4",
00102     PORTB,
00103     0
00104   },
00105   {
00106     "S5",
00107     PORTB,
00108     6
00109   }
00110 };
00111 
00112 const ButtonResourceType ButtonsMB951A[] = {
00113   {
00114     "S1",
00115     PORTA,
00116     3
00117   },
00118 };
00119 
00120 const TempSensorResourceType tempSensorMB851A = {
00121   "STLM20",
00122   PORTB,
00123   7,
00124   FALSE
00125 };
00126 
00127 const TempSensorResourceType tempSensorMB954A = {
00128   "STLM20",
00129   PORTA,
00130   4,
00131   TRUE
00132 };
00133 
00134 const MemsResourceType memsSensor = {
00135   "LIS302",
00136   0
00137 };
00138 
00139 const BoardIOType ioMB851A = {
00140   LedsMB851A,
00141   ButtonsMB851A,  
00142 };
00143 
00144 const BoardIOType ioMB954A = {
00145   LedsMB954A,
00146   ButtonsMB954A,  
00147 };
00148 
00149 const BoardIOType ioMB950A = {
00150   LedsMB954A,
00151   ButtonsMB950A,  
00152 };
00153 
00154 const BoardIOType ioMB951A = {
00155   LedsMB954A,
00156   ButtonsMB951A,  
00157 };
00158 
00159 const BoardResourcesType MB851A = {
00160   "MB851 A",
00161   (BOARD_HAS_MEMS | BOARD_HAS_TEMP_SENSOR | BOARD_HAS_FTDI),
00162   BUTTONS_MB851A,
00163   LEDS_MB851A,
00164   &ioMB851A,
00165   &infraRedLedMB851A,
00166   &memsSensor,
00167   &tempSensorMB851A,
00168 };
00169 
00170 const BoardResourcesType MB851B = {
00171   "MB851 B",
00172   (BOARD_HAS_MEMS | BOARD_HAS_TEMP_SENSOR | BOARD_HAS_FTDI),
00173   BUTTONS_MB851B,
00174   LEDS_MB851B,
00175   &ioMB851A,
00176   &infraRedLedMB851A,
00177   &memsSensor,
00178   &tempSensorMB851A,
00179 };
00180 
00181 const BoardResourcesType MB851C = {
00182   "MB851 C",
00183   (BOARD_HAS_MEMS | BOARD_HAS_TEMP_SENSOR | BOARD_HAS_STM32F | BOARD_HAS_EEPROM),
00184   BUTTONS_MB851C,
00185   LEDS_MB851C,
00186   &ioMB954A,
00187   &infraRedLedMB851A,
00188   &memsSensor,
00189   &tempSensorMB954A,
00190 };
00191 
00192 const BoardResourcesType MB954A = {
00193   "MB954 A",
00194   (BOARD_HAS_MEMS | BOARD_HAS_TEMP_SENSOR | BOARD_HAS_FTDI | BOARD_HAS_PA),
00195   BUTTONS_MB954A,
00196   LEDS_MB954A,
00197   &ioMB954A,
00198   &infraRedLedMB851A,
00199   &memsSensor,
00200   &tempSensorMB954A,
00201 };
00202 
00203 const BoardResourcesType MB954B = {
00204   "MB954 B",
00205   (BOARD_HAS_MEMS | BOARD_HAS_TEMP_SENSOR | BOARD_HAS_STM32F | BOARD_HAS_EEPROM | BOARD_HAS_PA),
00206   BUTTONS_MB954B,
00207   LEDS_MB954B,
00208   &ioMB954A,
00209   &infraRedLedMB851A,
00210   &memsSensor,
00211   &tempSensorMB954A,
00212 };
00213 
00214 const BoardResourcesType MB950A = {
00215   "MB950 A",
00216   (BOARD_HAS_MEMS | BOARD_HAS_TEMP_SENSOR | BOARD_HAS_STM32F),
00217   BUTTONS_MB950A,
00218   LEDS_MB950A,
00219   &ioMB950A,
00220   &infraRedLedMB851A,
00221   &memsSensor,
00222   &tempSensorMB954A,
00223 };
00224 
00225 const BoardResourcesType MB951A = {
00226   "MB951 A",
00227   (BOARD_HAS_STM32F),
00228   BUTTONS_MB951A,
00229   LEDS_MB951A,
00230   &ioMB951A,
00231   NULL,
00232   NULL,
00233   NULL,
00234 };
00235 
00236 static const BoardResourcesType *boardList [] = {
00237   &MB851A,
00238   &MB851B,
00239   &MB851C,
00240   &MB954A,
00241   &MB954B,
00242   &MB950A,
00243   &MB951A
00244 };
00245 
00246 BoardResourcesType const *boardDescription = NULL;
00247 
00248 #define PORT_NAME(x) ((x == PORTA) ? "A": ((x == PORTB) ? "B" : ((x == PORTC) ? "C": "?")))
00249 
00250 void halBoardInit(void)
00251 {
00252   char boardName[16];
00253   int8s i;
00254   boardDescription = NULL;
00255 #ifdef EMBERZNET_HAL
00256   halCommonGetToken(boardName, TOKEN_MFG_BOARD_NAME);
00257 #else
00258   halCommonGetMfgToken(boardName, TOKEN_MFG_BOARD_NAME);
00259 #endif
00260 
00261   i = 15;
00262   while ((i >= 0) && (boardName[i] == 0xFF)) {
00263     boardName[i] = 0;
00264     i--;
00265   }
00266 
00267   for (i = 0; i < (sizeof(boardList)/4) ; i++) 
00268     if (strcmp(boardName, (boardList[i])->name) == 0) {
00269       boardDescription = (BoardResourcesType *) boardList[i];
00270       break;
00271     }
00272 
00273   if (boardDescription == NULL) {
00274     /* Board type not identified default to MB851A also to support legacy boards */
00275     boardDescription = (BoardResourcesType *) &MB851A;
00276   }
00277   return;
00278 }
00279 BoardResourcesType const *halBoardGetDescription(void)
00280 {
00281   return boardDescription;
00282 }
00283 
00284 void halBoardPowerDown(void)
00285 {
00286   /* Set everything to input value */
00287   GPIO_PACFGL = (GPIOCFG_IN              <<PA0_CFG_BIT)|
00288                 (GPIOCFG_IN              <<PA1_CFG_BIT)|
00289                 (GPIOCFG_IN              <<PA2_CFG_BIT)|
00290                 (GPIOCFG_IN              <<PA3_CFG_BIT);
00291   GPIO_PACFGH = (GPIOCFG_IN              <<PA4_CFG_BIT)|  /* PTI EN */
00292                 (GPIOCFG_IN              <<PA5_CFG_BIT)|  /* PTI_DATA */
00293                 (GPIOCFG_IN              <<PA6_CFG_BIT)|
00294                 (GPIOCFG_IN              <<PA7_CFG_BIT);
00295   GPIO_PBCFGL = (GPIOCFG_IN              <<PB0_CFG_BIT)|
00296                 (GPIOCFG_IN              <<PB1_CFG_BIT)|  /* Uart TX */
00297                 (GPIOCFG_IN              <<PB2_CFG_BIT)|  /* Uart RX */
00298                 (GPIOCFG_IN              <<PB3_CFG_BIT);
00299   GPIO_PBCFGH = (GPIOCFG_IN              <<PB4_CFG_BIT)|
00300                 (GPIOCFG_IN              <<PB5_CFG_BIT)|
00301                 (GPIOCFG_IN              <<PB6_CFG_BIT)|
00302                 (GPIOCFG_IN              <<PB7_CFG_BIT);
00303   GPIO_PCCFGL = (GPIOCFG_IN              <<PC0_CFG_BIT)|
00304                 (GPIOCFG_IN              <<PC1_CFG_BIT)|
00305                 (GPIOCFG_IN              <<PC2_CFG_BIT)|
00306                 (GPIOCFG_IN              <<PC3_CFG_BIT);
00307   GPIO_PCCFGH = (GPIOCFG_IN              <<PC4_CFG_BIT)|
00308                 (GPIOCFG_IN              <<PC5_CFG_BIT)|
00309 #ifdef EMBERZNET_HAL
00310                 (CFG_C6                  <<PC6_CFG_BIT)|  /* OSC32K */
00311                 (CFG_C7                  <<PC7_CFG_BIT);  /* OSC32K */
00312 #else
00313                 (GPIOCFG_IN              <<PC6_CFG_BIT)|  /* OSC32K */
00314                 (GPIOCFG_IN              <<PC7_CFG_BIT);  /* OSC32K */
00315 #endif
00316 
00317 
00318   /* Configure GPIO for BUTTONSs */
00319   {
00320     ButtonResourceType *buttons = (ButtonResourceType *) boardDescription->io->buttons;
00321     int8u i;
00322     for (i = 0; i < boardDescription->buttons; i++) {
00323         halGpioConfig(PORTx_PIN(buttons[i].gpioPort, buttons[i].gpioPin), GPIOCFG_IN_PUD);
00324         halGpioSet(PORTx_PIN(buttons[i].gpioPort, buttons[i].gpioPin), GPIOOUT_PULLUP);
00325     }
00326   }
00327 
00328   /* Configure GPIO for LEDs */
00329   {
00330     LedResourceType *leds = (LedResourceType *) boardDescription->io->leds;
00331     int8u i;
00332     for (i = 0; i < boardDescription->leds; i++) {
00333           /* LED default off */
00334       halGpioConfig(PORTx_PIN(leds[i].gpioPort, leds[i].gpioPin), GPIOCFG_OUT);
00335       halGpioSet(PORTx_PIN(leds[i].gpioPort, leds[i].gpioPin), 1);
00336     }
00337   }
00338 
00339   /* Configure GPIO for power amplifier */
00340   if (boardDescription->flags & BOARD_HAS_PA) {
00341         /* SiGe Ant Sel to output */
00342         halGpioConfig(PORTB_PIN(5), GPIOCFG_OUT);
00343         halGpioSet(PORTB_PIN(5), 1);
00344     /* SiGe Standby */
00345     halGpioConfig(PORTB_PIN(6), GPIOCFG_OUT);
00346     halGpioSet(PORTB_PIN(6), 0);
00347   }
00348 
00349 }
00350 void halBoardPowerUp(void)
00351 {
00352   /* Set everything to input value */
00353   GPIO_PACFGL = (GPIOCFG_IN            <<PA0_CFG_BIT)|
00354                 (GPIOCFG_IN            <<PA1_CFG_BIT)|
00355                 (GPIOCFG_IN            <<PA2_CFG_BIT)|
00356                 (GPIOCFG_IN            <<PA3_CFG_BIT);
00357   GPIO_PACFGH = (GPIOCFG_IN            <<PA4_CFG_BIT)|  /* PTI EN */
00358                 (GPIOCFG_IN            <<PA5_CFG_BIT)|  /* PTI_DATA */
00359                 (GPIOCFG_IN            <<PA6_CFG_BIT)|
00360                 (GPIOCFG_IN            <<PA7_CFG_BIT);
00361   GPIO_PBCFGL = (GPIOCFG_IN            <<PB0_CFG_BIT)|
00362                 (GPIOCFG_OUT_ALT       <<PB1_CFG_BIT)|  /* Uart TX */
00363                 (GPIOCFG_IN            <<PB2_CFG_BIT)|  /* Uart RX */
00364                 (GPIOCFG_IN            <<PB3_CFG_BIT);
00365   GPIO_PBCFGH = (GPIOCFG_IN            <<PB4_CFG_BIT)|
00366                 (GPIOCFG_IN            <<PB5_CFG_BIT)|
00367                 (GPIOCFG_IN            <<PB6_CFG_BIT)|
00368                 (GPIOCFG_IN            <<PB7_CFG_BIT);
00369   GPIO_PCCFGL = (GPIOCFG_IN            <<PC0_CFG_BIT)|
00370                 (GPIOCFG_IN            <<PC1_CFG_BIT)|
00371                 (GPIOCFG_IN            <<PC2_CFG_BIT)|
00372                 (GPIOCFG_IN            <<PC3_CFG_BIT);
00373   GPIO_PCCFGH = (GPIOCFG_IN            <<PC4_CFG_BIT)|
00374                 (GPIOCFG_IN            <<PC5_CFG_BIT)|
00375 #ifdef EMBERZNET_HAL
00376                 (CFG_C6                  <<PC6_CFG_BIT)|  /* OSC32K */
00377                 (CFG_C7                  <<PC7_CFG_BIT);  /* OSC32K */
00378 #else
00379                 (GPIOCFG_IN              <<PC6_CFG_BIT)|  /* OSC32K */
00380                 (GPIOCFG_IN              <<PC7_CFG_BIT);  /* OSC32K */
00381 #endif
00382 
00383   /* Configure GPIO for I2C access */
00384   if ((boardDescription->flags & BOARD_HAS_MEMS) || (boardDescription->flags & BOARD_HAS_EEPROM)) {
00385     halGpioConfig(PORTA_PIN(1), GPIOCFG_OUT_ALT_OD);
00386     halGpioConfig(PORTA_PIN(2), GPIOCFG_OUT_ALT_OD);
00387   }
00388   /* Configure GPIO for ADC access (temp sensor) */
00389   if (boardDescription->flags & BOARD_HAS_TEMP_SENSOR) {
00390     halGpioConfig(PORTx_PIN(boardDescription->temperatureSensor->gpioPort,
00391                             boardDescription->temperatureSensor->gpioPin),
00392                   GPIOCFG_ANALOG);
00393   }
00394   /* Configure GPIO for LEDs */
00395   {
00396     LedResourceType *leds = (LedResourceType *) boardDescription->io->leds;
00397     int8u i;
00398     for (i = 0; i < boardDescription->leds; i++) {
00399           /* LED default off */
00400       halGpioConfig(PORTx_PIN(leds[i].gpioPort, leds[i].gpioPin), GPIOCFG_OUT);
00401       halGpioSet(PORTx_PIN(leds[i].gpioPort, leds[i].gpioPin), 1);
00402     }
00403   }
00404   /* Configure GPIO for BUTTONSs */
00405   {
00406     ButtonResourceType *buttons = (ButtonResourceType *) boardDescription->io->buttons;
00407     int8u i;
00408     for (i = 0; i < boardDescription->buttons; i++) {
00409         halGpioConfig(PORTx_PIN(buttons[i].gpioPort, buttons[i].gpioPin), GPIOCFG_IN_PUD);
00410         halGpioSet(PORTx_PIN(buttons[i].gpioPort, buttons[i].gpioPin), GPIOOUT_PULLUP);
00411     }
00412   }
00413 
00414   /* Configure GPIO for power amplifier */
00415   if (boardDescription->flags & BOARD_HAS_PA) {
00416     /* SiGe Ant Sel (default ceramic antenna) */
00417     halGpioConfig(PORTB_PIN(5), GPIOCFG_OUT);
00418     halGpioSet(PORTB_PIN(5), 1);
00419     /* SiGe Standby (default out of standby) */
00420     halGpioConfig(PORTB_PIN(6), GPIOCFG_OUT);
00421     halGpioSet(PORTB_PIN(6), 1);
00422     /* SiGe LNA (default LNA off )*/
00423     halGpioConfig(PORTB_PIN(7), GPIOCFG_OUT);
00424     halGpioSet(PORTB_PIN(7), 0);
00425     /* SiGe nTX Active */
00426     halGpioConfig(PORTC_PIN(5), GPIOCFG_OUT_ALT);
00427   }
00428 
00429 }
00430 
00431 #ifdef TEST_COMMANDS
00432 #include <stdio.h>
00433 void printLeds (LedResourceType *leds)
00434 {
00435   while (leds->name != NULL) {
00436     printf ("Led %s (port:%s, bit:%d)\r\n", leds->name, PORT_NAME(leds->gpioPort), leds->gpioPin);
00437     leds++;
00438   }
00439 }
00440 
00441 void printButtons (ButtonResourceType *buttons)
00442 {
00443   while (buttons->name != NULL) {
00444     printf ("Button %s (port:%s, bit:%d)\r\n", buttons->name, PORT_NAME(buttons->gpioPort), buttons->gpioPin);
00445     buttons++;
00446   }
00447 }
00448 
00449 void boardPrintStringDescription(void)
00450 {
00451   int8u i = 0;
00452 
00453   while (boardList[i] != NULL) {
00454     if ((boardDescription == boardList[i]) || (boardDescription == NULL)) {
00455       BoardResourcesType *ptr = boardList[i];
00456       printf ("*************************************\r\n");
00457       printf ("Board name = %s\r\n", ptr->name);
00458       printf ("*************************************\r\n");
00459       printLeds(ptr->leds);
00460       printButtons(ptr->buttons);
00461       if (ptr->mems) {
00462         printf ("MEMS = %s\r\n", ptr->mems->name);
00463       }
00464       if (ptr->temperatureSensor) {
00465         printf ("Temp sensor = %s, port:%s, pin:%d, adcFix:%s\r\n",
00466                 ptr->temperatureSensor->name,
00467                 PORT_NAME(ptr->temperatureSensor->gpioPort),
00468                 ptr->temperatureSensor->gpioPin,
00469                 ptr->temperatureSensor->adcFix ? "Yes" : "No"
00470                 );
00471       }
00472       printf ("EEProm:%s\r\n", ptr->EEProm ? "Yes" : "No");
00473       printf ("PC i/f:%s\r\n", ptr->FTDInotSTM32 ? "FTDI" : "STM32F");
00474       printf ("Power Amplifier:%s\r\n", ptr->PowerAmplifier ? "Yes" : "No");
00475     }
00476     i++;
00477   }
00478   return NULL;
00479 }
00480 #endif