dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
main.c
Go to the documentation of this file.
1 
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  * for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with this program; if not, see <http://www.gnu.org/licenses/>
27  */
28 
29 
30 /* OpenPilot Includes */
31 #include <pios.h>
32 #include <openpilot.h>
33 
34 #include "uavobjectsinit.h"
35 #include "systemmod.h"
36 
37 #include <board_hw_defs.c>
38 
39 #include <pios_hal.h>
40 #include <pios_rtc_priv.h>
42 
43 #include "hwshared.h"
44 
45 void PIOS_Board_Init(void);
46 
47 static bool inited;
48 
49 static struct flyingpicmd_cfg_fa cfg;
50 
51 uintptr_t adc_id;
52 static uint16_t msg_num;
53 
54 extern void TIM1_CC_IRQHandler(void);
55 extern void TIM1_BRK_UP_TRG_COM_IRQHandler(void);
56 extern void TIM2_IRQHandler(void);
57 extern void TIM3_IRQHandler(void);
58 extern void TIM14_IRQHandler(void);
59 extern void TIM15_IRQHandler(void);
60 extern void TIM16_IRQHandler(void);
61 extern void TIM17_IRQHandler(void);
62 extern void USART1_IRQHandler(void);
63 
64 const void *_interrupt_vectors[USART2_IRQn] __attribute((section(".interrupt_vectors"))) = {
65  [TIM1_BRK_UP_TRG_COM_IRQn] = TIM1_BRK_UP_TRG_COM_IRQHandler,
66  [TIM1_CC_IRQn] = TIM1_CC_IRQHandler,
67  [TIM3_IRQn] = TIM3_IRQHandler,
68  [TIM14_IRQn] = TIM14_IRQHandler,
69  [TIM15_IRQn] = TIM15_IRQHandler,
70  [TIM16_IRQn] = TIM16_IRQHandler,
71  [TIM17_IRQn] = TIM17_IRQHandler,
72  [USART1_IRQn] = USART1_IRQHandler,
73 };
74 
75 // The other side should give us a little time to deal with this,
76 // as we need to copy stuff and initialize hardware.
77 static void handle_cfg_fa(struct flyingpicmd_cfg_fa *cmd) {
79 
81  // This is not supposed to change types. Assert, which will
82  // trigger watchdog, which will trigger picking up new type.
84 
85  PIOS_HAL_ConfigurePort(cmd->receiver_protocol,
86  &pios_usart_rcvr_cfg,
88  NULL, // I2C
89  NULL,
90  &pios_ppm_cfg,
91  NULL, // PWM
92  PIOS_LED_HEARTBEAT, // It's all we have...
93  &pios_dsm_rcvr_cfg,
94  HWSHARED_DSMXMODE_AUTODETECT,
95  NULL // sbus: we have inverters
96  );
97  }
98 
99  // Copy so that we can refer to this later.
100  memcpy(&cfg, cmd, sizeof(cfg));
101 
102  uint16_t minimums[pios_servo_cfg.num_channels];
103  uint16_t maximums[pios_servo_cfg.num_channels];
104 
105  // Annoying reformat..
106  for (int i=0; i < pios_servo_cfg.num_channels; i++) {
107  minimums[i] = cfg.actuators[i].min;
108  maximums[i] = cfg.actuators[i].max;
109  }
110 
111  PIOS_Servo_SetMode(cfg.rate, FPPROTO_MAX_BANKS, maximums, minimums);
112 
113  /* OK, game on. From here on out we may end up generating PWM.
114  * so it's time to enable the watchdog. */
115  if (!inited) {
116  PIOS_WDG_Init();
117  inited = true;
118  }
119 }
120 
122  if (!inited) {
123  /* Can happen after a watchdog reset... In which case,
124  * wait for the upstream processor to tll us it's OK
125  */
126  return;
127  }
128 
129  for (int i=0; i < pios_servo_cfg.num_channels; i++) {
132  }
133 
135 
136  if (cmd->led_status) {
138  } else {
140  }
141 
142  PIOS_WDG_Clear();
143 }
144 
145 static void generate_status_message(int *resp_len)
146 {
147  tx_buf.id = FLYINGPIRESP_IO;
148 
149  struct flyingpiresp_io_10 *resp = &tx_buf.body.io_10;
150 
151  bzero(resp, sizeof(*resp));
152 
154 
155  for (int i = 0; i < FPPROTO_MAX_RCCHANS; i++) {
156  if (pios_rcvr_group_map[0]) {
157  resp->chan_data[i] =
158  /* 1-offset vs 0-offset grr */
160  } else {
161  resp->chan_data[i] = PIOS_RCVR_NODRIVER;
162  }
163  }
164 
165  for (int i = 0; i < FPPROTO_MAX_ADCCHANS; i++) {
167  }
168 
169  flyingpi_calc_crc(&tx_buf, true, resp_len);
170 
171  msg_num++;
172 }
173 
174 static void process_pio_message_impl(int *resp_len)
175 {
176  switch (rx_buf.id) {
178  handle_actuator_fc(&rx_buf.body.actuator_fc);
179  break;
180  case FLYINGPICMD_CFG:
181  handle_cfg_fa(&rx_buf.body.cfg_fa);
182  break;
183  default:
184  /* We got a message with an unknown type, but valid
185  * CRC. no bueno. */
186  PIOS_Assert(0);
187  break;
188  }
189 
190  /* If we get an edge and no clocking.. make sure the message is
191  * invalidated / not reused */
192  rx_buf.id = 0;
193 }
194 
195 static void process_pio_message(void *ctx, int len, int *resp_len)
196 {
197  (void) ctx;
198  (void) len;
199 
200  if (flyingpi_calc_crc(&rx_buf, false, NULL)) {
201  process_pio_message_impl(resp_len);
202  }
203 
204  generate_status_message(resp_len);
205 }
206 
207 int main()
208 {
210 
211  /* Brings up System using CMSIS functions, enables the LEDs. */
212  PIOS_SYS_Init();
213 
214 #if defined(PIOS_INCLUDE_ANNUNC)
215  const struct pios_annunc_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(0);
216  PIOS_Assert(led_cfg);
217  PIOS_ANNUNC_Init(led_cfg);
218 #endif /* PIOS_INCLUDE_ANNUNC */
219 
220 #if defined(PIOS_INCLUDE_ADC)
221  pios_internal_adc_t adc_dev;
222 
223  if (PIOS_INTERNAL_ADC_Init(&adc_dev, &internal_adc_cfg)) {
224  PIOS_Assert(0);
225  }
226 
227  if (PIOS_ADC_Init(&adc_id, &pios_internal_adc_driver, (uintptr_t )adc_dev)) {
228  PIOS_Assert(0);
229  }
230 #endif
231 
232  //outputs
236 
238  spislave_t spislave_dev;
239 
240  tx_buf.id = 0x33;
241  tx_buf.crc8 = 0x22;
242 
243  int initial_msg_len;
244  generate_status_message(&initial_msg_len);
245 
246  PIOS_SPISLAVE_Init(&spislave_dev, &pios_spislave_cfg, initial_msg_len);
247 
248  uint32_t i=0;
249 
250  while (1) {
251  PIOS_SPISLAVE_PollSS(spislave_dev);
252  PIOS_INTERNAL_ADC_DoStep(adc_dev);
253 
254  i++;
255  i &= 0x3ffff;
256 
257  if (!inited) {
258  // Distinctive "ready" blip when host is not controlling
259  // LED. On most of the time, which 3 quick flashes off.
260  if ((i == 0) || (i == 0x8000) || (i == 0x10000)) {
262  } else if ((i == 0x4000) || (i == 0xc000) || (i == 0x14000)) {
264  }
265  }
266  }
267 
268  return 0;
269 }
270 
int main(void)
Definition: main.c:441
int32_t PIOS_SPISLAVE_Init(spislave_t *spislave_id, const struct pios_spislave_cfg *cfg, int initial_tx_size)
int32_t PIOS_SPISLAVE_PollSS(spislave_t spislave_dev)
void TIM2_IRQHandler(void)
Definition: pios_tim.c:327
Main PiOS header to include all the compiled in PiOS options.
void TIM15_IRQHandler(void)
Definition: pios_tim.c:359
uintptr_t adc_id
Definition: main.c:51
int32_t PIOS_INTERNAL_ADC_Init(uintptr_t *internal_adc_id, const struct pios_internal_adc_cfg *cfg)
uintptr_t pios_rcvr_group_map[]
Definition: pios_hal.c:73
static void handle_cfg_fa(struct flyingpicmd_cfg_fa *cmd)
Definition: main.c:77
uint16_t adc_data[FPPROTO_MAX_ADCCHANS]
void PIOS_INTERNAL_ADC_DoStep(pios_internal_adc_t internal_adc_id)
void USART1_IRQHandler(void)
void TIM16_IRQHandler(void)
Definition: pios_tim.c:367
System module.
uint16_t values[FPPROTO_MAX_SERVOS]
static bool inited
Definition: main.c:47
struct pios_internal_adc_dev * pios_internal_adc_t
void PIOS_SYS_Init(void)
void PIOS_ANNUNC_Off(uint32_t annunc_id)
void PIOS_Board_Init(void)
Definition: pios_board.c:44
void PIOS_Servo_SetFraction(uint8_t servo, uint16_t fraction, uint16_t max_val, uint16_t min_val)
Definition: pios_servo.c:546
#define FPPROTO_MAX_ADCCHANS
void PIOS_WDG_Clear(void)
Clear the watchdog timer.
Definition: pios_wdg.c:147
static void generate_status_message(int *resp_len)
Definition: main.c:145
#define FLYINGPICMD_ACTUATOR
static struct flyingpicmd_cfg_fa cfg
Definition: main.c:49
const struct pios_com_driver pios_usart_com_driver
const struct pios_adc_driver pios_internal_adc_driver
int32_t PIOS_TIM_InitClock(const struct pios_tim_clock_cfg *cfg)
Definition: pios_tim.c:62
void TIM1_CC_IRQHandler(void)
Definition: pios_tim.c:317
static void handle_actuator_fc(struct flyingpicmd_actuator_fc *cmd)
Definition: main.c:121
uint16_t rate[FPPROTO_MAX_BANKS]
void PIOS_heap_initialize_blocks(void)
Definition: pios_heap.c:228
int32_t PIOS_ANNUNC_Init(const struct pios_annunc_cfg *cfg)
void PIOS_ANNUNC_On(uint32_t annunc_id)
uint8_t i
Definition: msp_messages.h:97
static const struct pios_tim_clock_cfg tim_1_cfg
#define FLYINGPIRESP_IO
#define FPPROTO_MAX_BANKS
#define FPPROTO_MAX_SERVOS
struct pios_spislave_dev * spislave_t
Definition: pios_spislave.h:39
int PIOS_Servo_SetMode(const uint16_t *out_rate, const int banks, const uint16_t *channel_max, const uint16_t *channel_min)
PIOS_Servo_SetMode Sets the PWM output frequency and resolution. An output rate of 0 indicates Synchr...
Definition: pios_servo.c:304
int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg)
Definition: pios_servo.c:109
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
int32_t PIOS_RCVR_Read(uintptr_t rcvr_id, uint8_t channel)
static const struct pios_tim_clock_cfg tim_14_cfg
void TIM14_IRQHandler(void)
Definition: pios_tim.c:351
int32_t PIOS_ADC_GetChannelRaw(uint32_t channel)
Includes PiOS and core architecture components.
uint8_t num_channels
uint16_t PIOS_WDG_Init()
Initialize the watchdog timer for a specified timeout.
Definition: pios_wdg.c:63
void PIOS_Servo_Update(void)
Definition: pios_servo.c:865
int32_t PIOS_ADC_Init(uintptr_t *adc_id, const struct pios_adc_driver *driver, uintptr_t lower_id)
static void process_pio_message(void *ctx, int len, int *resp_len)
Definition: main.c:195
void TIM17_IRQHandler(void)
Definition: pios_tim.c:375
static const struct pios_tim_clock_cfg tim_3_cfg
static ManualControlCommandData cmd
#define PIOS_Assert(test)
Definition: pios_debug.h:52
#define FPPROTO_MAX_RCCHANS
static uint16_t msg_num
Definition: main.c:52
uint16_t chan_data[FPPROTO_MAX_RCCHANS]
void TIM3_IRQHandler(void)
Definition: pios_tim.c:335
static bool flyingpi_calc_crc(struct flyingpi_msg *msg, bool fill_in, int *total_len)
struct flyingpicmd_cfg_fa::@15 actuators[FPPROTO_MAX_SERVOS]
const void *_interrupt_vectors[USART2_IRQn] __attribute((section(".interrupt_vectors")))
#define PIOS_LED_HEARTBEAT
Definition: pios_board.h:85
static void process_pio_message_impl(int *resp_len)
Definition: main.c:174
#define FLYINGPICMD_CFG