ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Суббота
13 июля
463830 Топик полностью
Nikolay801_ (15.11.2013 14:51, просмотров: 115) ответил йцукен на Кстати, о Киркорове. Хотели поглядеть на мой код - наслаждайтесь
Нормальный в общем то код, можно при желании по придираться, но не буду заниматься ерундой. Если обратится к обсуждаемому здесь вопросу то, во первых перепишем Ваш код под ст либу, получится примерно следующее: ///////////////////////////////////////////////////////////////////////// // #include "cdc_f103.h" #include "stm32f10x.h" //#include "myf10x.h" //#include "usb9.h" //#include "f103usb.h" #include "CircBuf.h" //#define USB_SOFT_CONNECT /////////////////////////////////////////////////////////////////////////// #ifdef SEND_DEBUG_INFO #define LOG_SIZE 1024 uint8_t Log[LOG_SIZE]; int LogIdx = 0; void log_put( uint8_t x ) { if ( LogIdx < LOG_SIZE ) Log[LogIdx++] = x; } uint8_t hex_tab[17] = "0123456789ABCDEF"; void log_put_hex( uint8_t x ) { log_put( hex_tab[(x>>4)&0x0F] ); log_put( hex_tab[x&0x0F] ); } void log_puts( const uint8_t* s ) { while ( *s ) log_put( *s++ ); } #define DEBUG_PUT(x) log_put(x) #define DEBUG_PUT_HEX(x) log_put_hex(x) #define DEBUG_PUTS(x) log_puts(x) #else #define DEBUG_PUT(x) #define DEBUG_PUT_HEX(x) #define DEBUG_PUTS(x) #endif //////////////////////////////////////////////////////////////////////////// // 29 PA8 USB_CONN // 32 PA11 USBDM // 33 PA12 USBDP // 40 PB4 -RTS (OD) // 41 PB5 -DSR (OD) // 42 PB6 TXD // 43 PB7 RXD //inline void Pin2( bool On ) { if ( On ) GPIOC->BSRR = (1<<13); else GPIOC->BRR = (1<<13); } //inline void Pin3( bool On ) { if ( On ) GPIOC->BSRR = (1<<14); else GPIOC->BRR = (1<<14); } //inline void Pin38( bool On ) { if ( On ) GPIOA->BSRR = (1<<15); else GPIOA->BRR = (1<<15); } //inline void Pin39( bool On ) { if ( On ) GPIOB->BSRR = (1<<3); else GPIOB->BRR = (1<<3); } //inline void Pin43( bool On ) { if ( On ) GPIOB->BSRR = (1<<7); else GPIOB->BRR = (1<<7); } //inline void Pin45( bool On ) { if ( On ) GPIOB->BSRR = (1<<8); else GPIOB->BRR = (1<<8); } //inline void Pin46( bool On ) { if ( On ) GPIOB->BSRR = (1<<9); else GPIOB->BRR = (1<<9); } #ifdef USB_SOFT_CONNECT inline void UsbConn( bool On ) { if ( On ) GPIO_SetBits(GPIOA, GPIO_Pin_8); else GPIO_ResetBits(GPIOA, GPIO_Pin_8); } #endif int current_config = 0; int in_interrupt_buffers = 0; int in_bulk_buffers = 0; int out_bulk_buffers = 0; void usb_interrupt_in( void ) { in_interrupt_buffers = 1; } void usb_bulk_out( void ) { out_bulk_buffers = 1; } void usb_bulk_in( void ) { in_bulk_buffers = 1; } #define RX_BUF_SZ 256 uint8_t rx_buf[RX_BUF_SZ]; circ_buf_uint8_t uart_rx( rx_buf, RX_BUF_SZ ); #define TX_BUF_SZ 256 uint8_t tx_buf[TX_BUF_SZ]; circ_buf_uint8_t uart_tx( tx_buf, TX_BUF_SZ ); uint8_t tmp_buf[64]; // temporary bufer to hold usb in/out packet extern "C" void USART1_IRQHandler( void ) { if ( USART_GetITStatus(USART1, USART_SR_RXNE ) == SET) { uart_rx.put(USART_ReceiveData(USART1)); } if ( USART_GetITStatus(USART1, USART_SR_ORE ) == SET) { USART_ReceiveData(USART1); } if ( USART_GetITStatus(USART1, USART_SR_TXE ) == SET) { if ( uart_tx.not_empty() ) USART_SendData(USART1, uart_tx.get()); else USART_ITConfig(USART1, USART_CR1_TXEIE, DISABLE); } } void send_uart_pkt( void ) { static bool FullPktSent = false; int len = uart_rx.len_vol_in(); if ( len > USB_EP_BULK_IN_SZ ) len = USB_EP_BULK_IN_SZ; for ( int j = 0; j < len; j++ ) tmp_buf[j] = uart_rx.get(); if ( len != 0 || FullPktSent ) { f103_usb_ep_write( USB_EP_BULK_IN, tmp_buf, len ); in_bulk_buffers = 0; FullPktSent = ( len == USB_EP_BULK_IN_SZ ); } } void get_uart_pkt( void ) { int len = f103_usb_ep_read( USB_EP_BULK_OUT, tmp_buf, USB_EP_BULK_OUT_SZ ); out_bulk_buffers = 0; for ( int j = 0; j < len; j++ ) uart_tx.put( tmp_buf[j] ); USART_ITConfig(USART1, USART_CR1_TXEIE, ENABLE); } void usb_config_change( uint8_t config ) { if ( config == 1 ) in_interrupt_buffers = in_bulk_buffers = 1; else in_interrupt_buffers = in_bulk_buffers = 0; out_bulk_buffers = 0; current_config = config; } void usb_start_of_frame() { } inline void __delay( uint32_t w ) { for ( volatile uint32_t j = 0; j < w; j++ ) ; } /* void __delay_ms( uint32_t ms ) { ms *= SYSTICK_FREQ / 1000UL; uint32_t t0 = tick_count; while ( (tick_count-t0) < ms ) ; } */ void hw_init( void ); void main( void ) { hw_init(); f103_usb_hw_init(); #ifdef USB_SOFT_CONNECT UsbConn( false ); __delay( 5000000UL ); UsbConn( true ); #endif for(;;) { f103_usb_test_irq(); if ( in_bulk_buffers ) send_uart_pkt(); if ( out_bulk_buffers && ( uart_tx.free_vol_out() > USB_EP_BULK_OUT_SZ ) ) get_uart_pkt(); } } //////////////////////////////////////////////////////////////////////////////// void hw_init( void ) { GPIO_InitTypeDef GPIO_InitType = {0}; __disable_interrupt(); NVIC_SetPriorityGrouping( 3 ); // no sublevels /* настройка клоков была в system_stm32f10x.c, поэтому выкидываем то что здесь было*/ RCC_AHBPeriphClockCmd(RCC_AHBENR_DMA1EN, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN, ENABLE); // RCC->APB1RSTR |= ( RCC_APB1RSTR_TIM2RST | RCC_APB1RSTR_TIM3RST ); // RCC->APB1RSTR &= ~( RCC_APB1RSTR_TIM2RST | RCC_APB1RSTR_TIM3RST ); RCC_APB2PeriphResetCmd( RCC_APB2RSTR_IOPARST | RCC_APB2RSTR_IOPBRST | RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_AFIORST | RCC_APB2RSTR_USART1RST, ENABLE); RCC_APB2PeriphResetCmd( RCC_APB2RSTR_IOPARST | RCC_APB2RSTR_IOPBRST | RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_AFIORST | RCC_APB2RSTR_USART1RST, DISABLE ); // Configure pins #ifdef USB_SOFT_CONNECT GPIO_InitType.GPIO_Pin = GPIO_Pin_9; GPIO_InitType.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitType.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitType); #endif // 40 PB4 -RTS (OD) // 41 PB5 -DSR (OD) // 42 PB6 TXD // 43 PB7 RXD GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, DISABLE); // use PB4 as GPIO GPIO_InitType.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; // -RTS (OD) -DSR (OD) GPIO_InitType.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitType); GPIO_InitType.GPIO_Pin = GPIO_Pin_6; // USART1_TX GPIO_InitType.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitType); GPIO_InitType.GPIO_Pin = GPIO_Pin_7; // USART1_RX GPIO_InitType.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitType); // debug GPIO_InitType.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // pin 45 pin 46 GPIO_InitType.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitType); USART_InitTypeDef USART_InitStructure = {0}; USART_InitStructure.USART_BaudRate = 38400; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* USART configuration */ USART_Init(USART1, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART1, ENABLE); NVIC_SetPriority( USART1_IRQn, 0 ); // highest NVIC_EnableIRQ( USART1_IRQn ); __enable_interrupt(); } Причем часть Вашего кода который заменился кодом ст либы и этот замещающий код эстешной либы, в общем то идентичны. Ну может они различны в реализации (на мой взгляд не значительно), но функционально идентичны. Особенно четко просматривается на PinInit -> GPIO_Init, просто братья близнецы. Ну не вижу я принципиальной разницы между Вашей реализацией и эстешной. При этом эстешная либа уже написана, а Вашу часть еще нужно было написать, а это примерно 1/4 сырца, грубо говоря 25% лишней работы.
Будь ты проклят, Перри-Утконос!