FreeRTOS First Example
BSP.c
Go to the documentation of this file.
1 
12 #include "BSP.h"
13 
14 
16 volatile uint32_t pwm_value = PWM_FREQ/6;
17 
18 
22 static void setupSWOForPrint(void) {
23  /* Enable GPIO clock. */
24  CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO;
25 
26  /* Enable Serial wire output pin */
27  GPIO->ROUTE |= GPIO_ROUTE_SWOPEN;
28 
29 #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_LEOPARD_FAMILY) || defined(_EFM32_WONDER_FAMILY)
30  /* Set location 0 */
31  GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC0;
32 
33  /* Enable output on pin - GPIO Port F, Pin 2 */
34  GPIO->P[5].MODEL &= ~(_GPIO_P_MODEL_MODE2_MASK);
35  GPIO->P[5].MODEL |= GPIO_P_MODEL_MODE2_PUSHPULL;
36 #else
37  /* Set location 1 */
38  GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK))
39  | GPIO_ROUTE_SWLOCATION_LOC1;
40  /* Enable output on pin */
41  GPIO->P[2].MODEH &= ~(_GPIO_P_MODEH_MODE15_MASK);
42  GPIO->P[2].MODEH |= GPIO_P_MODEH_MODE15_PUSHPULL;
43 #endif
44 
45  /* Enable debug clock AUXHFRCO */
46  CMU->OSCENCMD = CMU_OSCENCMD_AUXHFRCOEN;
47 
48  /* Wait until clock is ready */
49  while (!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY))
50  ;
51 
52  /* Enable trace in core debug */
53  CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
54  ITM->LAR = 0xC5ACCE55;
55  ITM->TER = 0x0;
56  ITM->TCR = 0x0;
57  TPI->SPPR = 2;
58  TPI->ACPR = 0xf;
59  ITM->TPR = 0x0;
60  DWT->CTRL = 0x400003FE;
61  ITM->TCR = 0x0001000D;
62  TPI->FFCR = 0x00000100;
63  ITM->TER = 0x1;
64 }
65 
69 int _write(int file, const char *ptr, int len) {
70  int x;
71  for (x = 0; x < len; x++) {
72  ITM_SendChar(*ptr++);
73  }
74  return (len);
75 }
76 
80 static void BSP_LedInit(void) {
81  CMU_ClockEnable(cmuClock_GPIO, true);
82  GPIO_PinModeSet(gpioPortD, 7, gpioModePushPullDrive, 0); /* LED */
83 }
84 
85 void LedOn(void) {
86  GPIO_PinOutSet(gpioPortD, 7);
87 }
88 
89 void LedOff(void) {
90  GPIO_PinOutClear(gpioPortD, 7);
91 }
92 
93 void LedToggle(void) {
94  GPIO_PinOutToggle(gpioPortD, 7);
95 }
96 
102 static void BSP_ButtonsInit() {
103  CMU_ClockEnable(cmuClock_GPIO, true);
104  GPIO_PinModeSet(gpioPortD, 8, gpioModeInput, 0); /* Boto 0 */
105  GPIO_PinModeSet(gpioPortB, 11, gpioModeInput, 0); /* Boto 1 */
106 }
107 
108 void BSP_Init(void) {
109  BSP_LedInit();
110  BSP_ButtonsInit();
112 }
113 
114 
116 TIMER_InitCC_TypeDef timerCCInit = {
117  .eventCtrl = timerEventEveryEdge,
118  .edge = timerEdgeNone,
119  .prsSel = timerPRSSELCh0,
120  .cufoa = timerOutputActionNone,
121  .cofoa = timerOutputActionSet,
122  .cmoa = timerOutputActionClear,
123  .mode = timerCCModePWM,
124  .filter = false,
125  .prsInput = false,
126  .coist = false,
127  .outInvert = false,
128  };
129 
130 TIMER_Init_TypeDef timerInit = {
131  .enable = true,
132  .debugRun = true,
133  .prescale = timerPrescale256,
134  .clkSel = timerClkSelHFPerClk,
135  .fallAction = timerInputActionNone,
136  .riseAction = timerInputActionNone,
137  .mode = timerModeUp,
138  .dmaClrAct = false,
139  .quadModeX4 = false,
140  .oneShot = false,
141  .sync = false,
142  };
145 void PWMConfig(void) {
146  CMU_ClockEnable(cmuClock_TIMER1, true);
147 
148  /* Block CPu to sleep in EM2, Timers only work while CPU on EM0 or EM1 */
149  SLEEP_SleepBlockBegin(sleepEM2);
150 
151  TIMER_InitCC(TIMER1, 1, &timerCCInit);
152  TIMER1->ROUTE |= (TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC4);
153 
154  TIMER_TopSet(TIMER1, PWM_FREQ);
155  TIMER_CompareBufSet(TIMER1, 1, pwm_value);
156  TIMER_Init(TIMER1, &timerInit);
157 }
158 
159 void ADCConfig(void) {
160  ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
161  ADC_InitSingle_TypeDef singleInit = ADC_INITSINGLE_DEFAULT;
162 
163  /* Enable clock for ADC peripheral */
164  CMU_ClockEnable(cmuClock_ADC0, true);
165 
166  /* Block CPu to sleep in EM2, ADC only works while CPU on EM0 or EM1 */
167  SLEEP_SleepBlockBegin(sleepEM2);
168 
169  /* default options are OK */
170  ADC_Init(ADC0, &init);
171 
172  /* Select right input channel and reference */
173  singleInit.reference = adcRefVDD;
174  singleInit.input = adcSingleInpCh6;
175 
176  ADC_InitSingle(ADC0, &singleInit);
177 }
static void BSP_LedInit(void)
Init LED GPIO.
Definition: BSP.c:80
static void setupSWOForPrint(void)
Initialize SWO to be used by printf.
Definition: BSP.c:22
void LedOff(void)
Switch off LED.
Definition: BSP.c:89
void BSP_Init(void)
Init all BSP functions.
Definition: BSP.c:108
int _write(int file, const char *ptr, int len)
write function to be used by printf
Definition: BSP.c:69
void ADCConfig(void)
ADC init.
Definition: BSP.c:159
void PWMConfig(void)
PWM (Timer) init.
Definition: BSP.c:145
#define PWM_FREQ
Definition: BSP.h:36
void LedToggle(void)
Switch off LED.
Definition: BSP.c:93
void LedOn(void)
Switch on LED.
Definition: BSP.c:85
Basic BSP enabling printf.
static void BSP_ButtonsInit()
Init buttons GPIOs.
Definition: BSP.c:102
volatile uint32_t pwm_value
Definition: BSP.c:16