|
Libopencm3 usb hid то ли я дурак то ли железка нет прерывания |
|
|
|
Nov 5 2015, 12:46
|

Группа: Новичок
Сообщений: 4
Регистрация: 5-11-15
Из: Серпухов,МО
Пользователь №: 89 185

|
Доброго времени суток товарищи! Теперь по теме. Пытаюсь заставить stm32 f103, а точнее отладочную плату OLIMEX P-103, общатсья с хостом по hid'у. С великой и ужасной spl всё работает, но мне хотелось бы разобратся с libopencm3 т.к. c ней работать гораздо приятнее и удобнее. Но вот беда гугл говорит мне что с моей проблемой и вообще с какими-либо проблемами ни-кто не сталкивался, как-будто ни кто её не использует либо с ней так всё хорошо что ни у кого нет проблем. Собрал из примеров небольшой проект, всё сделано по образу и подобию. Пример 1Пример 2И конечно же..Последний, третий пример стал основным донором кода. В неизменённом варианте он работоспособен но там буфер в конечную точку записывается по прерыванию sys_tick_handler, что для меня не приемлемо. Я в своём коде тоже могу из любого места записать этот буфер и он считается, но хотелось бы это делать в ответ на реквест из хоста. Вот мой код: CODE /* * This file is part of the libopencm3 project. * * Copyright © 2010 Gareth McMullin <gareth@blacksphere.co.nz> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <stdlib.h> #include <libopencm3/stm32/rcc.h> #include <libopencm3/stm32/gpio.h> #include <libopencm3/usb/usbd.h> #include <libopencm3/usb/hid.h> #include <libopencm3/cm3/systick.h> #include <libopencm3/cm3/nvic.h> #define ENDPOINT_ADDRESS_IN (0x81) #define ENDPOINT_ADDRESS_OUT (0x01) #define LED_PORT GPIOC #define LED_PIN GPIO12 /* Define this to include the DFU APP interface. */ #define INCLUDE_DFU_INTERFACE #ifdef INCLUDE_DFU_INTERFACE #include <libopencm3/cm3/scb.h> #include <libopencm3/usb/dfu.h> #endif static usbd_device *usbd_dev; const struct usb_device_descriptor dev_descr = { .bLength = USB_DT_DEVICE_SIZE, .bDescriptorType = USB_DT_DEVICE, .bcdUSB = 0x0200, .bDeviceClass = 0, .bDeviceSubClass = 0, .bDeviceProtocol = 0, .bMaxPacketSize0 = 64, .idVendor = 0x0483, .idProduct = 0x5710, .bcdDevice = 0x0200, .iManufacturer = 1, .iProduct = 2, .iSerialNumber = 3, .bNumConfigurations = 1, }; static const uint8_t hid_report_descriptor[] = { 0x06, 0xFF, 0xFF, // 04|2 , Usage Page (vendordefined?) 0x09, 0x01, // 08|1 , Usage (vendordefined 0xA1, 0x01, // A0|1 , Collection (Application) // Feature report 0x09, 0x06, // 08|1 , Usage (vendordefined) 0x09, 0x07, // 08|1 , Usage (vendordefined) 0x15, 0x00, // 14|1 , LogicalMinimum(0 for signed byte) 0x75, 0x08, // 74|1 , Report Size(8) =field size in bits = 1 byte 0x95, 0x08, //_0x04, // 94|1:ReportCount 0xB1, 0x02, // B0|1: Feature report 0xC0 // C0|0 , End Collection }; //static const uint8_t hid_report_descriptor[] = { // 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) // 0x09, 0x01, // USAGE (Consumer Control) // 0xa1, 0x01, // COLLECTION (Application) // 0x85, 0x01, // REPORT_ID (1) // 0x19, 0x00, // USAGE_MINIMUM (Unassigned) // 0x2a, 0x3c, 0x02, // USAGE_MAXIMUM (AC Format) // 0x15, 0x00, // LOGICAL_MINIMUM (0) // 0x26, 0x3c, 0x02, // LOGICAL_MAXIMUM (572) // 0x95, 0x01, // REPORT_COUNT (1) // 0x75, 0x10, // REPORT_SIZE (16) // 0x81, 0x00, // INPUT (Data,Var,Abs) // 0xc0 // END_COLLECTION //}; static const struct { struct usb_hid_descriptor hid_descriptor; struct { uint8_t bReportDescriptorType; uint16_t wDescriptorLength; } __attribute__((packed)) hid_report; } __attribute__((packed)) hid_function = { .hid_descriptor = { .bLength = sizeof(hid_function), .bDescriptorType = USB_DT_HID, .bcdHID = 0x0100, .bCountryCode = 0, .bNumDescriptors = 1, }, .hid_report = { .bReportDescriptorType = USB_DT_REPORT, .wDescriptorLength = sizeof(hid_report_descriptor), } }; static const struct usb_endpoint_descriptor hid_endpoints[] = {{ .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = ENDPOINT_ADDRESS_IN, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, .wMaxPacketSize = 8, .bInterval = 1, }, { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = ENDPOINT_ADDRESS_OUT, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, .wMaxPacketSize = 8, .bInterval = 1, }}; const struct usb_interface_descriptor hid_iface = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = 0, .bAlternateSetting = 0, .bNumEndpoints = 2, .bInterfaceClass = USB_CLASS_HID, .bInterfaceSubClass = 0, /* no boot */ .bInterfaceProtocol = 0, /* user */ .iInterface = 0, .endpoint = hid_endpoints, .extra = &hid_function, .extralen = sizeof(hid_function), }; #ifdef INCLUDE_DFU_INTERFACE const struct usb_dfu_descriptor dfu_function = { .bLength = sizeof(struct usb_dfu_descriptor), .bDescriptorType = DFU_FUNCTIONAL, .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, .wDetachTimeout = 255, .wTransferSize = 1024, .bcdDFUVersion = 0x011A, }; const struct usb_interface_descriptor dfu_iface = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = 1, .bAlternateSetting = 0, .bNumEndpoints = 0, .bInterfaceClass = 0xFE, .bInterfaceSubClass = 1, .bInterfaceProtocol = 1, .iInterface = 0, .extra = &dfu_function, .extralen = sizeof(dfu_function), }; #endif const struct usb_interface ifaces[] = {{ .num_altsetting = 1, .altsetting = &hid_iface, #ifdef INCLUDE_DFU_INTERFACE }, { .num_altsetting = 1, .altsetting = &dfu_iface, #endif }}; const struct usb_config_descriptor config = { .bLength = USB_DT_CONFIGURATION_SIZE, .bDescriptorType = USB_DT_CONFIGURATION, .wTotalLength = 0, #ifdef INCLUDE_DFU_INTERFACE .bNumInterfaces = 2, #else .bNumInterfaces = 1, #endif .bConfigurationValue = 1, .iConfiguration = 0, .bmAttributes = 0xC0, .bMaxPower = 0x32, .interface = ifaces, }; static const char *usb_strings[] = { "ZAO NPP SKICHEL", "HID Demo", "DEMO", }; /* Buffer used for control requests. */ uint8_t usbd_control_buffer[128]; static int hid_control_request(usbd_device *dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void (**complete)(usbd_device *dev, struct usb_setup_data *req)) { (void)complete; (void)dev; if((req->bmRequestType != ENDPOINT_ADDRESS_IN) || (req->bRequest != USB_REQ_GET_DESCRIPTOR) || (req->wValue != 0x2200)) return 0; /* Handle the HID report descriptor. */ *buf = (uint8_t *)hid_report_descriptor; *len = sizeof(hid_report_descriptor); return 1; } #ifdef INCLUDE_DFU_INTERFACE static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req) { (void)req; (void)dev; gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO10); gpio_set(GPIOA, GPIO10); scb_reset_core(); } static int dfu_control_request(usbd_device *dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void (**complete)(usbd_device *dev, struct usb_setup_data *req)) { (void)buf; (void)len; (void)dev; if ((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH)) return 0; /* Only accept class request. */ *complete = dfu_detach_complete; return 1; } #endif // uint8_t x = 0; static void data_rx(usbd_device *dev, uint8_t ep) { (void)ep; (void)dev; char buf[8]; // gpio_clear(LED_PORT, LED_PIN); //gpio_toggle(LED_PORT, LED_PIN); int len = usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_OUT, buf, 8); buf[0] = x; buf[1] = 8; buf[2] = 7; buf[3] = 6; buf[4] = 5; buf[5] = 4; buf[6] = 3; buf[7] = 2; if (len) { usbd_ep_write_packet(dev, ENDPOINT_ADDRESS_IN, buf, len); buf[len] = 0; } // gpio_set(LED_PORT, LED_PIN); } static void hid_set_config(usbd_device *dev, uint16_t wValue) { (void)wValue; usbd_ep_setup(dev, ENDPOINT_ADDRESS_IN, USB_ENDPOINT_ATTR_INTERRUPT, 8, NULL); usbd_ep_setup(dev, ENDPOINT_ADDRESS_OUT, USB_ENDPOINT_ATTR_INTERRUPT, 8, data_rx); usbd_register_control_callback( dev, USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE, USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, hid_control_request); #ifdef INCLUDE_DFU_INTERFACE usbd_register_control_callback( dev, USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, dfu_control_request); #endif systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); /* SysTick interrupt every N clock pulses: set reload to N-1 */ systick_set_reload(99999); systick_interrupt_enable(); systick_counter_enable(); } static void gpio_setup(void) { gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); } int main(void) { int i; rcc_clock_setup_in_hsi_out_48mhz(); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); gpio_set(GPIOC, GPIO11); gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); // gpio_setup(); // gpio_set(LED_PORT, LED_PIN); usbd_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev_descr, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer)); usbd_register_set_config_callback(usbd_dev, hid_set_config); for (i = 0; i < 0x80000; i++) __asm__("nop"); gpio_clear(GPIOC, GPIO11); while (1) usbd_poll(usbd_dev); } void sys_tick_handler(void) { // uint8_t buf[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // //buf[0] = x; // buf[0] = 9; // buf[1] = 8; // buf[2] = 7; // buf[3] = 6; // buf[4] = 5; // buf[5] = 4; // buf[6] = 3; // buf[7] = 2; // usbd_ep_write_packet(usbd_dev, ENDPOINT_ADDRESS_IN, buf, 8); } Вот для конечной точки назначается коллбек: CODE usbd_ep_setup(dev, ENDPOINT_ADDRESS_OUT, USB_ENDPOINT_ATTR_INTERRUPT, 8, data_rx); Но он никогда не вызывается вообще! Я в нём пробовал для контроля зажигать светодиод, ноль реакции! Хотя в примерах сделанно именно так разногласий с документацией библиотеки я не увидел. Но программа которая на хосту пытается что-либо прочесть ни как его не заводит.
|
|
|
|
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0
|
|
|