FreeRTOS First Example
main.c
Go to the documentation of this file.
1 
20 /* STD libraries*/
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 /* FreeRTOS includes */
25 #include "FreeRTOSConfig.h"
26 #include "FreeRTOS.h"
27 #include "task.h"
28 #include "semphr.h"
29 
30 /* emlib includes */
31 #include "em_chip.h"
32 
33 /* emdrv includes */
34 #include "sleep.h"
35 
36 /* our includes */
37 #include "BSP.h"
38 
40 #define TOGGLE_TASK_PRIORITY (tskIDLE_PRIORITY + 1)
41 
43 #define QUEUE_LENGTH (10)
44 
45 
47 QueueHandle_t adc_queue;
48 
49 
57 static void TaskPWMCtrl(void *pParameter) {
58 
59  (void) pParameter;
60  uint32_t my_pwm;
61  uint32_t recv_pwm;
62  uint32_t pwm_value;
63 
64  PWMConfig();
65 
66  for (;;) {
67  if (xQueueReceive(adc_queue, &recv_pwm, portMAX_DELAY)) {
68  my_pwm = recv_pwm;
69 
70  /*
71  * Value received from ADC tssk has a range from 0 to 4095 (12 bits)
72  * We may map these values to a 0 to PWM_FREQ in 6 steps
73  */
74  if (my_pwm > PWM_FREQ) {
75  my_pwm = PWM_FREQ;
76  }
77 
78  pwm_value = my_pwm;
79 
80  TIMER_CompareBufSet(TIMER1, 1, pwm_value);
81  }
82  }
83 }
84 
92 static void TaskADCRead(void *pParameter) {
93 
94  (void) pParameter;
95  uint32_t ADCvalue;
96 
97  ADCConfig();
98 
99  while (1) {
100  ADC_Start(ADC0, adcStartSingle);
101 
102  while (ADC0->STATUS & ADC_STATUS_SINGLEACT);
103 
104  ADCvalue = ADC_DataSingleGet(ADC0);
105  xQueueSend(adc_queue, &ADCvalue, portMAX_DELAY);
106 
107  vTaskDelay( pdMS_TO_TICKS(250));
108  }
109 }
110 
114 int main(void) {
115  /* Chip errata */
116  CHIP_Init();
117 
118  /* Init functions for our BSP */
119  BSP_Init();
120 
121  /* Init SLEEP library */
122  SLEEP_Init(NULL, NULL);
123 #if (configSLEEP_MODE < 3)
124  SLEEP_SleepBlockBegin((SLEEP_EnergyMode_t) (configSLEEP_MODE + 1));
125 #endif
126 
127  /* Create Queue */
128  adc_queue = xQueueCreate(QUEUE_LENGTH, sizeof(uint32_t));
129 
130  /* Create our first task */
131  xTaskCreate(TaskPWMCtrl, (const char *) "PWMCtrl",
132  configMINIMAL_STACK_SIZE, NULL, TOGGLE_TASK_PRIORITY, NULL);
133 
134  xTaskCreate(TaskADCRead, (const char *) "ADCRead",
135  configMINIMAL_STACK_SIZE, NULL, TOGGLE_TASK_PRIORITY, NULL);
136 
137  /* Start FreeRTOS Scheduler */
138  vTaskStartScheduler();
139 
140  return 0;
141 }
142 
143 /*
144  * @}
145  */
146 
#define QUEUE_LENGTH
Definition: main.c:43
int main(void)
main function
Definition: main.c:114
static void TaskADCRead(void *pParameter)
Get ADC value and sent it through queue.
Definition: main.c:92
void BSP_Init(void)
Init all BSP functions.
Definition: BSP.c:108
void ADCConfig(void)
ADC init.
Definition: BSP.c:159
static void TaskPWMCtrl(void *pParameter)
PWM Ctrl from value received through a queue.
Definition: main.c:57
void PWMConfig(void)
PWM (Timer) init.
Definition: BSP.c:145
#define TOGGLE_TASK_PRIORITY
Definition: main.c:40
#define PWM_FREQ
Definition: BSP.h:36
Basic BSP enabling printf.
QueueHandle_t adc_queue
Queue to send & receive ADC values.
Definition: main.c:47
volatile uint32_t pwm_value
Definition: BSP.c:16