Firmware SDK
main.c
1 #include <twr_scheduler.h>
2 #include <twr_system.h>
3 #include <twr_error.h>
4 #include <twr_log.h>
5 #include <twr_led.h>
6 #include <twr_timer.h>
7 #include <twr_sleep.h>
8 
9 void application_init(void);
10 
11 void application_task(void *param);
12 
13 void application_error(twr_error_t code);
14 
15 int main(void)
16 {
17  twr_system_init();
18 
19  while (twr_tick_get() < 500)
20  {
21  continue;
22  }
23 
25 
26  twr_scheduler_register(application_task, NULL, 0);
27 
28  application_init();
29 
31 }
32 
33 __attribute__((weak)) void application_init(void)
34 {
35 }
36 
37 __attribute__((weak)) void application_task(void *param)
38 {
39  (void) param;
40 }
41 
42 __attribute__((weak)) void application_idle()
43 {
44  twr_sleep();
45 }
46 
47 __attribute__((weak)) void application_error(twr_error_t code)
48 {
49 #ifdef RELEASE
50 
51  (void) code;
52 
53  twr_system_reset();
54 
55 #else
56 
58 
59  twr_tick_t timeout = 0;
60 
61  int cnt = 0;
62 
65 
66  while (true)
67  {
68  if (cnt % 3 == 0)
69  {
70  switch (code)
71  {
72  case TWR_ERROR_NOT_ENOUGH_TASKS:
73  {
74  twr_log_error("TWR_ERROR_NOT_ENOUGH_TASKS");
75  break;
76  }
77  case TWR_ERROR_LOG_NOT_INITIALIZED:
78  {
79  twr_log_error("TWR_ERROR_LOG_NOT_INITIALIZED");
80  break;
81  }
82  case TWR_ERROR_ERROR_UNLOCK:
83  {
84  twr_log_error("TWR_ERROR_ERROR_UNLOCK");
85  break;
86  }
87  case TWR_ERROR_CALLBACK:
88  {
89  twr_log_error("TWR_ERROR_CALLBACK");
90  break;
91  }
92  case TWR_ERROR_INVALID_PARAMETER:
93  {
94  twr_log_error("TWR_ERROR_INVALID_PARAMETER");
95  break;
96  }
97  default:
98  {
99  break;
100  }
101  }
102  }
103 
105 
106  timeout = twr_tick_get() + ((cnt > 1 && cnt < 5) ? 1000 : 300);
107 
108  while (timeout > twr_tick_get())
109  {
110  continue;
111  }
112 
114 
115  timeout = twr_tick_get() + (cnt == 7 ? 2000 : 300);
116 
117  while (timeout > twr_tick_get())
118  {
119  continue;
120  }
121 
122  if (cnt++ == 8)
123  {
124  cnt = 0;
125  }
126  }
127 
128 #endif
129 }
130 
131 void NMI_Handler(void)
132 {
133 }
134 
135 void SVC_Handler(void)
136 {
137 }
138 
139 void PendSV_Handler(void)
140 {
141 }
142 
143 void SysTick_Handler(void)
144 {
145  HAL_IncTick();
146  HAL_SYSTICK_IRQHandler();
147 }
void twr_gpio_set_output(twr_gpio_channel_t channel, int state)
Set output state for GPIO channel.
Definition: twr_gpio.c:471
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
Definition: twr_gpio.c:367
@ TWR_GPIO_MODE_OUTPUT
GPIO channel operates as output.
Definition: twr_gpio.h:108
@ TWR_GPIO_LED
GPIO channel LED.
Definition: twr_gpio.h:69
void void void void void twr_log_error(const char *format,...) __attribute__((format(printf
Log ERROR message (annotated in log as <E>)
void twr_log_init(twr_log_level_t level, twr_log_timestamp_t timestamp)
Initialize logging facility.
Definition: twr_log.c:22
@ TWR_LOG_LEVEL_DEBUG
Log level DEBUG.
Definition: twr_log.h:29
@ TWR_LOG_TIMESTAMP_ABS
Timestamp logging enabled (absolute time format)
Definition: twr_log.h:53
void twr_scheduler_run(void)
Run task scheduler (this call never ends)
Definition: twr_scheduler.c:29
void twr_scheduler_init(void)
Initialize task scheduler.
Definition: twr_scheduler.c:24
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
Definition: twr_scheduler.c:53
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
Definition: twr_tick.c:7
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16