Contiki 2.5
board.c
Go to the documentation of this file.
1 /** @file board.c
2  * @brief Board file x STM32W108 Kits boards
3  *
4  * This file implements a software layer to support all the ST kits boards
5  * and deal with the difference in leds, buttons and sensors connected to the board.
6  * This a bit too complex for customer boards adaptation and customer can simplify it
7  * to adapt it to their hardware environment as they normally don't need to have the software
8  * running on several boards.
9  * The boards supported are:
10  * - MB851 A
11  * - MB851 B
12  * - MB851 C
13  * - MB954 A
14  * - MB954 B
15  * - MB950 A
16  * - MB951 A
17  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. -->
18  */
19 #include PLATFORM_HEADER
20 #include BOARD_HEADER
21 #ifdef EMBERZNET_HAL
22 #include "stack/include/ember.h"
23 #include "stack/include/error.h"
24 #include "hal/hal.h"
25 #define PORTA (0 << 3)
26 #define PORTB (1 << 3)
27 #define PORTC (2 << 3)
28 #else
29 #include "error.h"
30 #include "hal/hal.h"
32 #endif
33 #include <string.h>
34 
35 const LedResourceType LedsMB851A[] = {
36  {
37  "D1", /* Green LED */
38  PORTB,
39  6
40  },
41  {
42  "D3", /* Yellow LED */
43  PORTB,
44  5
45  }
46 };
47 
48 const InfraRedLedResourceType infraRedLedMB851A = {
49  "D5", /* Infrared LED */
50  PORTB,
51  4
52 };
53 
54 
55 const LedResourceType LedsMB954A[] = {
56  {
57  "D1", /* Green LED */
58  PORTA,
59  5
60  },
61  {
62  "D3", /* Yellow LED */
63  PORTA,
64  0
65  }
66 };
67 
68 const ButtonResourceType ButtonsMB851A[] = {
69  {
70  "S1",
71  PORTA,
72  7
73  }
74 };
75 
76 const ButtonResourceType ButtonsMB954A[] = {
77  {
78  "S1",
79  PORTB,
80  3
81  }
82 };
83 
84 const ButtonResourceType ButtonsMB950A[] = {
85  {
86  "S1",
87  PORTB,
88  3
89  },
90  {
91  "S2",
92  PORTA,
93  7
94  },
95  {
96  "S3",
97  PORTB,
98  7
99  },
100  {
101  "S4",
102  PORTB,
103  0
104  },
105  {
106  "S5",
107  PORTB,
108  6
109  }
110 };
111 
112 const ButtonResourceType ButtonsMB951A[] = {
113  {
114  "S1",
115  PORTA,
116  3
117  },
118 };
119 
120 const TempSensorResourceType tempSensorMB851A = {
121  "STLM20",
122  PORTB,
123  7,
124  FALSE
125 };
126 
127 const TempSensorResourceType tempSensorMB954A = {
128  "STLM20",
129  PORTA,
130  4,
131  TRUE
132 };
133 
134 const MemsResourceType memsSensor = {
135  "LIS302",
136  0
137 };
138 
139 const BoardIOType ioMB851A = {
140  LedsMB851A,
141  ButtonsMB851A,
142 };
143 
144 const BoardIOType ioMB954A = {
145  LedsMB954A,
146  ButtonsMB954A,
147 };
148 
149 const BoardIOType ioMB950A = {
150  LedsMB954A,
151  ButtonsMB950A,
152 };
153 
154 const BoardIOType ioMB951A = {
155  LedsMB954A,
156  ButtonsMB951A,
157 };
158 
159 const BoardResourcesType MB851A = {
160  "MB851 A",
163  LEDS_MB851A,
164  &ioMB851A,
165  &infraRedLedMB851A,
166  &memsSensor,
167  &tempSensorMB851A,
168 };
169 
170 const BoardResourcesType MB851B = {
171  "MB851 B",
174  LEDS_MB851B,
175  &ioMB851A,
176  &infraRedLedMB851A,
177  &memsSensor,
178  &tempSensorMB851A,
179 };
180 
181 const BoardResourcesType MB851C = {
182  "MB851 C",
185  LEDS_MB851C,
186  &ioMB954A,
187  &infraRedLedMB851A,
188  &memsSensor,
189  &tempSensorMB954A,
190 };
191 
192 const BoardResourcesType MB954A = {
193  "MB954 A",
196  LEDS_MB954A,
197  &ioMB954A,
198  &infraRedLedMB851A,
199  &memsSensor,
200  &tempSensorMB954A,
201 };
202 
203 const BoardResourcesType MB954B = {
204  "MB954 B",
207  LEDS_MB954B,
208  &ioMB954A,
209  &infraRedLedMB851A,
210  &memsSensor,
211  &tempSensorMB954A,
212 };
213 
214 const BoardResourcesType MB950A = {
215  "MB950 A",
218  LEDS_MB950A,
219  &ioMB950A,
220  &infraRedLedMB851A,
221  &memsSensor,
222  &tempSensorMB954A,
223 };
224 
225 const BoardResourcesType MB951A = {
226  "MB951 A",
229  LEDS_MB951A,
230  &ioMB951A,
231  NULL,
232  NULL,
233  NULL,
234 };
235 
236 static const BoardResourcesType *boardList [] = {
237  &MB851A,
238  &MB851B,
239  &MB851C,
240  &MB954A,
241  &MB954B,
242  &MB950A,
243  &MB951A
244 };
245 
246 BoardResourcesType const *boardDescription = NULL;
247 
248 #define PORT_NAME(x) ((x == PORTA) ? "A": ((x == PORTB) ? "B" : ((x == PORTC) ? "C": "?")))
249 
250 void halBoardInit(void)
251 {
252  char boardName[16];
253  int8s i;
254  boardDescription = NULL;
255 #ifdef EMBERZNET_HAL
256  halCommonGetToken(boardName, TOKEN_MFG_BOARD_NAME);
257 #else
258  halCommonGetMfgToken(boardName, TOKEN_MFG_BOARD_NAME);
259 #endif
260 
261  i = 15;
262  while ((i >= 0) && (boardName[i] == 0xFF)) {
263  boardName[i] = 0;
264  i--;
265  }
266 
267  for (i = 0; i < (sizeof(boardList)/4) ; i++)
268  if (strcmp(boardName, (boardList[i])->name) == 0) {
269  boardDescription = (BoardResourcesType *) boardList[i];
270  break;
271  }
272 
273  if (boardDescription == NULL) {
274  /* Board type not identified default to MB851A also to support legacy boards */
275  boardDescription = (BoardResourcesType *) &MB851A;
276  }
277  return;
278 }
280 {
281  return boardDescription;
282 }
283 
285 {
286  /* Set everything to input value */
287  GPIO_PACFGL = (GPIOCFG_IN <<PA0_CFG_BIT)|
288  (GPIOCFG_IN <<PA1_CFG_BIT)|
289  (GPIOCFG_IN <<PA2_CFG_BIT)|
290  (GPIOCFG_IN <<PA3_CFG_BIT);
291  GPIO_PACFGH = (GPIOCFG_IN <<PA4_CFG_BIT)| /* PTI EN */
292  (GPIOCFG_IN <<PA5_CFG_BIT)| /* PTI_DATA */
293  (GPIOCFG_IN <<PA6_CFG_BIT)|
294  (GPIOCFG_IN <<PA7_CFG_BIT);
295  GPIO_PBCFGL = (GPIOCFG_IN <<PB0_CFG_BIT)|
296  (GPIOCFG_IN <<PB1_CFG_BIT)| /* Uart TX */
297  (GPIOCFG_IN <<PB2_CFG_BIT)| /* Uart RX */
298  (GPIOCFG_IN <<PB3_CFG_BIT);
299  GPIO_PBCFGH = (GPIOCFG_IN <<PB4_CFG_BIT)|
300  (GPIOCFG_IN <<PB5_CFG_BIT)|
301  (GPIOCFG_IN <<PB6_CFG_BIT)|
302  (GPIOCFG_IN <<PB7_CFG_BIT);
303  GPIO_PCCFGL = (GPIOCFG_IN <<PC0_CFG_BIT)|
304  (GPIOCFG_IN <<PC1_CFG_BIT)|
305  (GPIOCFG_IN <<PC2_CFG_BIT)|
306  (GPIOCFG_IN <<PC3_CFG_BIT);
307  GPIO_PCCFGH = (GPIOCFG_IN <<PC4_CFG_BIT)|
308  (GPIOCFG_IN <<PC5_CFG_BIT)|
309 #ifdef EMBERZNET_HAL
310  (CFG_C6 <<PC6_CFG_BIT)| /* OSC32K */
311  (CFG_C7 <<PC7_CFG_BIT); /* OSC32K */
312 #else
313  (GPIOCFG_IN <<PC6_CFG_BIT)| /* OSC32K */
314  (GPIOCFG_IN <<PC7_CFG_BIT); /* OSC32K */
315 #endif
316 
317 
318  /* Configure GPIO for BUTTONSs */
319  {
320  ButtonResourceType *buttons = (ButtonResourceType *) boardDescription->io->buttons;
321  int8u i;
322  for (i = 0; i < boardDescription->buttons; i++) {
323  halGpioConfig(PORTx_PIN(buttons[i].gpioPort, buttons[i].gpioPin), GPIOCFG_IN_PUD);
324  halGpioSet(PORTx_PIN(buttons[i].gpioPort, buttons[i].gpioPin), GPIOOUT_PULLUP);
325  }
326  }
327 
328  /* Configure GPIO for LEDs */
329  {
330  LedResourceType *leds = (LedResourceType *) boardDescription->io->leds;
331  int8u i;
332  for (i = 0; i < boardDescription->leds; i++) {
333  /* LED default off */
334  halGpioConfig(PORTx_PIN(leds[i].gpioPort, leds[i].gpioPin), GPIOCFG_OUT);
335  halGpioSet(PORTx_PIN(leds[i].gpioPort, leds[i].gpioPin), 1);
336  }
337  }
338 
339  /* Configure GPIO for power amplifier */
340  if (boardDescription->flags & BOARD_HAS_PA) {
341  /* SiGe Ant Sel to output */
342  halGpioConfig(PORTB_PIN(5), GPIOCFG_OUT);
343  halGpioSet(PORTB_PIN(5), 1);
344  /* SiGe Standby */
345  halGpioConfig(PORTB_PIN(6), GPIOCFG_OUT);
346  halGpioSet(PORTB_PIN(6), 0);
347  }
348 
349 }
350 void halBoardPowerUp(void)
351 {
352  /* Set everything to input value */
353  GPIO_PACFGL = (GPIOCFG_IN <<PA0_CFG_BIT)|
354  (GPIOCFG_IN <<PA1_CFG_BIT)|
355  (GPIOCFG_IN <<PA2_CFG_BIT)|
356  (GPIOCFG_IN <<PA3_CFG_BIT);
357  GPIO_PACFGH = (GPIOCFG_IN <<PA4_CFG_BIT)| /* PTI EN */
358  (GPIOCFG_IN <<PA5_CFG_BIT)| /* PTI_DATA */
359  (GPIOCFG_IN <<PA6_CFG_BIT)|
360  (GPIOCFG_IN <<PA7_CFG_BIT);
361  GPIO_PBCFGL = (GPIOCFG_IN <<PB0_CFG_BIT)|
362  (GPIOCFG_OUT_ALT <<PB1_CFG_BIT)| /* Uart TX */
363  (GPIOCFG_IN <<PB2_CFG_BIT)| /* Uart RX */
364  (GPIOCFG_IN <<PB3_CFG_BIT);
365  GPIO_PBCFGH = (GPIOCFG_IN <<PB4_CFG_BIT)|
366  (GPIOCFG_IN <<PB5_CFG_BIT)|
367  (GPIOCFG_IN <<PB6_CFG_BIT)|
368  (GPIOCFG_IN <<PB7_CFG_BIT);
369  GPIO_PCCFGL = (GPIOCFG_IN <<PC0_CFG_BIT)|
370  (GPIOCFG_IN <<PC1_CFG_BIT)|
371  (GPIOCFG_IN <<PC2_CFG_BIT)|
372  (GPIOCFG_IN <<PC3_CFG_BIT);
373  GPIO_PCCFGH = (GPIOCFG_IN <<PC4_CFG_BIT)|
374  (GPIOCFG_IN <<PC5_CFG_BIT)|
375 #ifdef EMBERZNET_HAL
376  (CFG_C6 <<PC6_CFG_BIT)| /* OSC32K */
377  (CFG_C7 <<PC7_CFG_BIT); /* OSC32K */
378 #else
379  (GPIOCFG_IN <<PC6_CFG_BIT)| /* OSC32K */
380  (GPIOCFG_IN <<PC7_CFG_BIT); /* OSC32K */
381 #endif
382 
383  /* Configure GPIO for I2C access */
384  if ((boardDescription->flags & BOARD_HAS_MEMS) || (boardDescription->flags & BOARD_HAS_EEPROM)) {
385  halGpioConfig(PORTA_PIN(1), GPIOCFG_OUT_ALT_OD);
386  halGpioConfig(PORTA_PIN(2), GPIOCFG_OUT_ALT_OD);
387  }
388  /* Configure GPIO for ADC access (temp sensor) */
389  if (boardDescription->flags & BOARD_HAS_TEMP_SENSOR) {
391  boardDescription->temperatureSensor->gpioPin),
392  GPIOCFG_ANALOG);
393  }
394  /* Configure GPIO for LEDs */
395  {
396  LedResourceType *leds = (LedResourceType *) boardDescription->io->leds;
397  int8u i;
398  for (i = 0; i < boardDescription->leds; i++) {
399  /* LED default off */
400  halGpioConfig(PORTx_PIN(leds[i].gpioPort, leds[i].gpioPin), GPIOCFG_OUT);
401  halGpioSet(PORTx_PIN(leds[i].gpioPort, leds[i].gpioPin), 1);
402  }
403  }
404  /* Configure GPIO for BUTTONSs */
405  {
406  ButtonResourceType *buttons = (ButtonResourceType *) boardDescription->io->buttons;
407  int8u i;
408  for (i = 0; i < boardDescription->buttons; i++) {
409  halGpioConfig(PORTx_PIN(buttons[i].gpioPort, buttons[i].gpioPin), GPIOCFG_IN_PUD);
410  halGpioSet(PORTx_PIN(buttons[i].gpioPort, buttons[i].gpioPin), GPIOOUT_PULLUP);
411  }
412  }
413 
414  /* Configure GPIO for power amplifier */
415  if (boardDescription->flags & BOARD_HAS_PA) {
416  /* SiGe Ant Sel (default ceramic antenna) */
417  halGpioConfig(PORTB_PIN(5), GPIOCFG_OUT);
418  halGpioSet(PORTB_PIN(5), 1);
419  /* SiGe Standby (default out of standby) */
420  halGpioConfig(PORTB_PIN(6), GPIOCFG_OUT);
421  halGpioSet(PORTB_PIN(6), 1);
422  /* SiGe LNA (default LNA off )*/
423  halGpioConfig(PORTB_PIN(7), GPIOCFG_OUT);
424  halGpioSet(PORTB_PIN(7), 0);
425  /* SiGe nTX Active */
426  halGpioConfig(PORTC_PIN(5), GPIOCFG_OUT_ALT);
427  }
428 
429 }
430 
431 #ifdef TEST_COMMANDS
432 #include <stdio.h>
433 void printLeds (LedResourceType *leds)
434 {
435  while (leds->name != NULL) {
436  printf ("Led %s (port:%s, bit:%d)\r\n", leds->name, PORT_NAME(leds->gpioPort), leds->gpioPin);
437  leds++;
438  }
439 }
440 
441 void printButtons (ButtonResourceType *buttons)
442 {
443  while (buttons->name != NULL) {
444  printf ("Button %s (port:%s, bit:%d)\r\n", buttons->name, PORT_NAME(buttons->gpioPort), buttons->gpioPin);
445  buttons++;
446  }
447 }
448 
449 void boardPrintStringDescription(void)
450 {
451  int8u i = 0;
452 
453  while (boardList[i] != NULL) {
454  if ((boardDescription == boardList[i]) || (boardDescription == NULL)) {
455  BoardResourcesType *ptr = boardList[i];
456  printf ("*************************************\r\n");
457  printf ("Board name = %s\r\n", ptr->name);
458  printf ("*************************************\r\n");
459  printLeds(ptr->leds);
460  printButtons(ptr->buttons);
461  if (ptr->mems) {
462  printf ("MEMS = %s\r\n", ptr->mems->name);
463  }
464  if (ptr->temperatureSensor) {
465  printf ("Temp sensor = %s, port:%s, pin:%d, adcFix:%s\r\n",
466  ptr->temperatureSensor->name,
467  PORT_NAME(ptr->temperatureSensor->gpioPort),
469  ptr->temperatureSensor->adcFix ? "Yes" : "No"
470  );
471  }
472  printf ("EEProm:%s\r\n", ptr->EEProm ? "Yes" : "No");
473  printf ("PC i/f:%s\r\n", ptr->FTDInotSTM32 ? "FTDI" : "STM32F");
474  printf ("Power Amplifier:%s\r\n", ptr->PowerAmplifier ? "Yes" : "No");
475  }
476  i++;
477  }
478  return NULL;
479 }
480 #endif