2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_USB_RCTX USB RC Transmitter/Joystick Functions
6 * @brief PIOS USB implementation for a HID Joystick
7 * @notes This implements transmitter/joystick emulation over HID reports
10 * @file pios_usb_rctx.c
11 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #ifdef PIOS_INCLUDE_USB_RCTX
35 #include "pios_usb_rctx_priv.h"
37 /* STM32 USB Library Definitions */
40 #define PIOS_USB_RCTX_NUM_CHANNELS 8
42 enum pios_usb_rctx_dev_magic
{
43 PIOS_USB_RCTX_DEV_MAGIC
= 0xAB98B745,
46 struct pios_usb_rctx_dev
{
47 enum pios_usb_rctx_dev_magic magic
;
48 const struct pios_usb_rctx_cfg
*cfg
;
54 uint16_t vals
[PIOS_USB_RCTX_NUM_CHANNELS
];
55 } __attribute__((packed
)) report
;
58 static bool PIOS_USB_RCTX_validate(struct pios_usb_rctx_dev
*usb_rctx_dev
)
60 return usb_rctx_dev
->magic
== PIOS_USB_RCTX_DEV_MAGIC
;
63 #ifdef PIOS_INCLUDE_FREERTOS
64 static struct pios_usb_rctx_dev
*PIOS_USB_RCTX_alloc(void)
66 struct pios_usb_rctx_dev
*usb_rctx_dev
;
68 usb_rctx_dev
= (struct pios_usb_rctx_dev
*)pios_malloc(sizeof(*usb_rctx_dev
));
73 usb_rctx_dev
->magic
= PIOS_USB_RCTX_DEV_MAGIC
;
77 static struct pios_usb_rctx_dev pios_usb_rctx_devs
[PIOS_USB_RCTX_MAX_DEVS
];
78 static uint8_t pios_usb_rctx_num_devs
;
79 static struct pios_usb_rctx_dev
*PIOS_USB_RCTX_alloc(void)
81 struct pios_usb_rctx_dev
*usb_rctx_dev
;
83 if (pios_usb_rctx_num_devs
>= PIOS_USB_RCTX_MAX_DEVS
) {
87 usb_rctx_dev
= &pios_usb_rctx_devs
[pios_usb_rctx_num_devs
++];
88 usb_rctx_dev
->magic
= PIOS_USB_RCTX_DEV_MAGIC
;
92 #endif /* ifdef PIOS_INCLUDE_FREERTOS */
94 static void PIOS_USB_RCTX_EP_IN_Callback(void);
95 static void PIOS_USB_RCTX_SendReport(struct pios_usb_rctx_dev
*usb_rctx_dev
);
97 /* Need a better way to pull these in */
98 extern void(*pEpInt_IN
[7]) (void);
100 int32_t PIOS_USB_RCTX_Init(uint32_t *usbrctx_id
, const struct pios_usb_rctx_cfg
*cfg
, uint32_t lower_id
)
102 PIOS_Assert(usbrctx_id
);
105 struct pios_usb_rctx_dev
*usb_rctx_dev
;
107 usb_rctx_dev
= (struct pios_usb_rctx_dev
*)PIOS_USB_RCTX_alloc();
112 /* Bind the configuration to the device instance */
113 usb_rctx_dev
->cfg
= cfg
;
114 usb_rctx_dev
->lower_id
= lower_id
;
116 /* Set the initial report buffer */
117 memset(&usb_rctx_dev
->report
, 0, sizeof(usb_rctx_dev
->report
));
119 pEpInt_IN
[cfg
->data_tx_ep
- 1] = PIOS_USB_RCTX_EP_IN_Callback
;
121 *usbrctx_id
= (uint32_t)usb_rctx_dev
;
129 static void PIOS_USB_RCTX_SendReport(struct pios_usb_rctx_dev
*usb_rctx_dev
)
131 #ifdef PIOS_INCLUDE_FREERTOS
132 bool need_yield
= false;
133 #endif /* PIOS_INCLUDE_FREERTOS */
135 usb_rctx_dev
->report
.id
= 3; /* FIXME: shouldn't hard-code this report ID */
137 UserToPMABufferCopy((uint8_t *)&usb_rctx_dev
->report
,
138 GetEPTxAddr(usb_rctx_dev
->cfg
->data_tx_ep
),
139 sizeof(usb_rctx_dev
->report
));
141 SetEPTxCount(usb_rctx_dev
->cfg
->data_tx_ep
, sizeof(usb_rctx_dev
->report
));
142 SetEPTxValid(usb_rctx_dev
->cfg
->data_tx_ep
);
144 #ifdef PIOS_INCLUDE_FREERTOS
148 #endif /* PIOS_INCLUDE_FREERTOS */
151 static void PIOS_USB_RCTX_EP_IN_Callback(void)
153 struct pios_usb_rctx_dev
*usb_rctx_dev
= (struct pios_usb_rctx_dev
*)pios_usb_rctx_id
;
155 bool valid
= PIOS_USB_RCTX_validate(usb_rctx_dev
);
159 if (!PIOS_USB_CheckAvailable(usb_rctx_dev
->lower_id
)) {
163 PIOS_USB_RCTX_SendReport(usb_rctx_dev
);
166 void PIOS_USB_RCTX_Update(uint32_t usbrctx_id
, const uint16_t channel
[], const int16_t channel_min
[], const int16_t channel_max
[], uint8_t num_channels
)
168 struct pios_usb_rctx_dev
*usb_rctx_dev
= (struct pios_usb_rctx_dev
*)usbrctx_id
;
170 bool valid
= PIOS_USB_RCTX_validate(usb_rctx_dev
);
174 if (!PIOS_USB_CheckAvailable(usb_rctx_dev
->lower_id
)) {
179 i
< PIOS_USB_RCTX_NUM_CHANNELS
&& i
< num_channels
;
181 int16_t min
= channel_min
[i
];
182 int16_t max
= channel_max
[i
];
183 uint16_t val
= channel
[i
];
185 if (channel_min
[i
] > channel_max
[i
]) {
186 /* This channel is reversed, flip min and max */
187 min
= channel_max
[i
];
188 max
= channel_min
[i
];
190 /* and flip val to be an offset from the lower end of the range */
191 val
= channel_min
[i
] - channel
[i
] + channel_max
[i
];
194 /* Scale channel linearly between min and max */
205 val
= (val
- min
) * (65535 / (max
- min
));
208 usb_rctx_dev
->report
.vals
[i
] = val
;
211 if (GetEPTxStatus(usb_rctx_dev
->cfg
->data_tx_ep
) == EP_TX_VALID
) {
212 /* Endpoint is already transmitting */
216 PIOS_USB_RCTX_SendReport(usb_rctx_dev
);
219 #endif /* PIOS_INCLUDE_USB_RCTX */