コード例 #1
0
int ipm_console_receiver_init(struct device *d)
{
	const struct ipm_console_receiver_config_info *config_info =
		d->config->config_info;
	struct ipm_console_receiver_runtime_data *driver_data = d->driver_data;
	struct device *ipm;

	ipm = device_get_binding(config_info->bind_to);

	if (!ipm) {
		printk("unable to bind IPM console receiver to '%s'\n",
		       config_info->bind_to);
		return -EINVAL;
	}

	if (ipm_max_id_val_get(ipm) < 0xFF) {
		printk("IPM driver %s doesn't support 8-bit id values",
		       config_info->bind_to);
		return -EINVAL;
	}

	driver_data->ipm_device = ipm;
	driver_data->channel_disabled = 0;
	k_sem_init(&driver_data->sem, 0, UINT_MAX);
	ring_buf_init(&driver_data->rb, config_info->rb_size32,
		      config_info->ring_buf_data);

	ipm_register_callback(ipm, ipm_console_receive_callback, d);

	k_thread_create(&driver_data->rx_thread, config_info->thread_stack,
			CONFIG_IPM_CONSOLE_STACK_SIZE, ipm_console_thread, d,
			NULL, NULL, K_PRIO_COOP(IPM_CONSOLE_PRI), 0, 0);
	ipm_set_enabled(ipm, 1);

	return 0;
}
コード例 #2
0
ファイル: umtp.c プロジェクト: aabhasgarg/accessgrid
umtp_t umtp_init(char *addr, uint16_t port, umtp_flag flag, umtp_callback callback, m_probe_state mprobe_state)
{
    int mc_port;
    struct umtp *session;	
    char name[255], *umtp_addr = NULL, mc_addr[12], s_type[7];
    	
    int state, m_state, umtp_state, rx_state, ttl = DEFAULT_TTL;

#ifdef WIN32
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 2, 2 );
 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
	/* Tell the user that we could not find a usable */
	/* WinSock DLL.                                  */
	return NULL;
    }
#endif
	
    if(umtp_addr_valid(addr))	
	umtp_addr = umtp_host_addr(addr);
    else
    {
	printf("Address is not valid!\n");
#ifdef WIN32
	WSACleanup();
#endif
	return NULL;
    }

    if (flag == MASTER) strcpy(s_type, "MASTER");
    else                strcpy(s_type, "SERVER");

    session = (umtp_t)malloc(sizeof(umtp));
    memset(session, 0, sizeof(umtp));			
	
    session->port		= port;	
    session->allowed_endpoints	= NULL;
    session->active_groups	= NULL;
    session->s			= udp_init(umtp_addr, port, port, ttl);
    session->flag		= flag;	
	session->mstate		= mprobe_state;

    // Due to the problem in setting ttl value for socket session->s using udp_init
    if (setsockopt(session->s->fd, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &(session->s->ttl), sizeof(session->s->ttl)) != 0) 
    {	
	fprintf(stdout, "[umtp__init] setsockopt IP_MULTICAST_TTL");
        umtp_end = 1;
    }
	
    // put local ip address into session->addr
    if(( gethostname(name, sizeof(name))) >= 0) {
	session->addr = umtp_host_addr(name);
        if( session->addr == NULL ) 
	    umtp_end = 1;
    }	  
    else {		
	printf("gethostname failed");
	umtp_end = 1;
    }

    //session->log = fopen("umtp.log", "a");
    session->log = stdout;
    if( session->log == NULL )
    	printf("Error: can't open log file.\n");
		
    fprintf(session->log, "\n======    UMTP %s Initialization    ======\n", s_type);
    fprintf(session->log, " *** LOCAL IP ADDR [%s/%d]\n", session->addr, port);	

    ring_buf_init(4*1024);

    //=================================================================================//
    // UMTP Recv Thread
    //=================================================================================//

    umtp_state = pthread_create(&session->umtp_id, NULL, umtp_recv, (void *)session);
    if(umtp_state != 0)
    {
        fprintf(session->log, "Error in creating UMTP recv thread!\n");
        umtp_end = 1;
    }
    fprintf(session->log, " *** THREAD CREATE - UMTP recv\n");

    //=================================================================================//
    // UMTP Rx Handler Thread
    //=================================================================================//

    rx_state = pthread_create(&session->rx_id, NULL, umtp_rx_handler, (void *)session);
    if(rx_state != 0)
    {
        fprintf(session->log, "Error in creating UMTP RX Handler thread!\n");
        umtp_end = 1;
    }
    fprintf(session->log, " *** THREAD CREATE - UMTP RX Handler \n");

    //=================================================================================//
    // MPROBE Thread
    //=================================================================================//
    
    strcpy(mc_addr, "224.2.2.224");

    mc_port = 50000;

	if( mprobe_state == MPROBE_START )
	{
		session->m = udp_init(mc_addr, mc_port, mc_port, ttl);
		m_state = pthread_create(&session->m_id, NULL, mcast_check, (void *)session);
		if(m_state != 0)
		{
		fprintf(session->log, "Error in creating multicast check thread!\n");
		umtp_end = 1;
		}
		fprintf(session->log, " *** THREAD CREATE - MPROBE [%s/%d]\n", mc_addr, mc_port);
	}
	else
	{
		fprintf(session->log, " *** DO NOT CREATE MPROBE THREAD\n");
	}

    //=================================================================================//
    // UGMP Thread 
    //=================================================================================//
   
    session->s1 = udp_init(umtp_addr, port+1, port+1, ttl);
    state       = pthread_create(&session->u_id, NULL, ucast_recv, (void *)session);
    if(state != 0) 
    {
        fprintf(session->log, "Error in creating thread!\n");
        umtp_end = 1;
    }
    fprintf(session->log, " *** THREAD CREATE - UGMP [%s/%d]\n", session->addr, port+1);	
    
    //= end of thread creation ========================================================//
	
    if( session->flag == MASTER ) notify_error = 0; // notification of error to AG Connector	
    else notify_error = 1; // server doesn't need to notify
	
    // set new cookie value
    srand((unsigned)time(NULL)); 
    #ifndef WIN32
        lrand48();
    #endif
    session->local_cookie		= (uint16_t)lrand48();
    fprintf(session->log, " *** LOCAL COOKIE [%d]\n", session->local_cookie);

    // add umtp server as an endpoint
    umtp_add_endpoint(session, umtp_addr, port, (uint16_t)lrand48());
    if(session->s == NULL) 	umtp_end = 1;
    else {
        session->callback = callback;
        return session;
    }
    return session;
}
コード例 #3
0
ファイル: uart.c プロジェクト: DeepinDream/etootle-flight
void uart1_init(uint32_t baudRate)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    uart_process_init(&usart1_process, rxBuffer, UART_BUFFER_SIZE, &usart1_parser, USART1, uart_data_ready);
    ring_buf_init(usart_rx);
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    // UART1 TX -> PA.9,  RX -> PA.10

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // DMA TX Interrupt
    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    // USART1 Interrupt
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    USART_InitStructure.USART_BaudRate = baudRate;
    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_Init(USART1, &USART_InitStructure);

    // Transmit DMA
    DMA_DeInit(DMA1_Channel4);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & USART1->DR;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)txBuffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = sizeof(txBuffer);
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
    DMA_Init(DMA1_Channel4, &DMA_InitStructure);

    DMA_ITConfig(DMA1_Channel4, DMA_IT_TC | DMA_IT_TE, ENABLE);
    DMA1_Channel4->CNDTR = 0;

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    
    USART_Cmd(USART1, ENABLE);
    
    usart1_process.txBusy = 0;
}