Exemple #1
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    int is_stdio_uart = 0;
    
    // determine the UART to use
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    if ((int)uart == NC) {
        error("Serial pinout mapping failed");
    }

    obj->uart = (LPC_USART_T *)uart;

    // enable fifos and default rx trigger level
    obj->uart->FCR = 1 << 0  // FIFO Enable - 0 = Disables, 1 = Enabled
                   | 0 << 1  // Rx Fifo Reset
                   | 0 << 2  // Tx Fifo Reset
                   | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars

    // disable irqs
    obj->uart->IER = 0 << 0  // Rx Data available irq enable
                   | 0 << 1  // Tx Fifo empty irq enable
                   | 0 << 2; // Rx Line Status irq enable
    
    // set default baud rate and format
    serial_baud (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);
    
    // pinout the chosen uart
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);
    
    // set rx/tx pins in PullUp mode
    if (tx != NC) {
        pin_mode(tx, PullUp);
    }
    if (rx != NC) {
        pin_mode(rx, PullUp);
    }
    
    switch (uart) {
        case UART_0: obj->index = 0; break;
        case UART_1: obj->index = 1; break;
        case UART_2: obj->index = 2; break;
        case UART_3: obj->index = 3; break;
    }
    uart_data[obj->index].sw_rts.pin = NC;
    uart_data[obj->index].sw_cts.pin = NC;
    serial_set_flow_control(obj, FlowControlNone, NC, NC);
    
    is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);

    if (is_stdio_uart) {
        stdio_uart_inited = 1;
        serial_baud (obj, STDIO_BAUD);
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #2
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    int is_stdio_uart = 0;
    
    int uart_n = get_available_uart();
    if (uart_n == -1) {
        error("No available UART");
    }
    obj->index = uart_n;
    obj->uart = (LPC_USART_TypeDef *)(LPC_USART0_BASE + (0x4000 * uart_n));
    uart_used |= (1 << uart_n);
    
    const SWM_Map *swm;
    uint32_t regVal;
    
    swm = &SWM_UART_TX[uart_n];
    regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
    LPC_SWM->PINASSIGN[swm->n] = regVal |  (tx   << swm->offset);
    
    swm = &SWM_UART_RX[uart_n];
    regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
    LPC_SWM->PINASSIGN[swm->n] = regVal |  (rx   << swm->offset);
    
    /* uart clock divided by 1 */
    LPC_SYSCON->UARTCLKDIV = 1;
    
    /* disable uart interrupts */
    NVIC_DisableIRQ((IRQn_Type)(UART0_IRQn + uart_n));
    
    /* Enable UART clock */
    LPC_SYSCON->SYSAHBCLKCTRL |= (1 << (14 + uart_n));
    
    /* Peripheral reset control to UART, a "1" bring it out of reset. */
    LPC_SYSCON->PRESETCTRL &= ~(0x1 << (3 + uart_n));
    LPC_SYSCON->PRESETCTRL |=  (0x1 << (3 + uart_n));
    
    UARTSysClk = SystemCoreClock / LPC_SYSCON->UARTCLKDIV;
    
    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);
    
    /* Clear all status bits. */
    obj->uart->STAT = CTS_DELTA | DELTA_RXBRK;
    
    /* enable uart interrupts */
    NVIC_EnableIRQ((IRQn_Type)(UART0_IRQn + uart_n));
    
    /* Enable UART interrupt */
    // obj->uart->INTENSET = RXRDY | TXRDY | DELTA_RXBRK;
    
    /* Enable UART */
    obj->uart->CFG |= UART_EN;
    
    is_stdio_uart = ((tx == USBTX) && (rx == USBRX));
    
    if (is_stdio_uart) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #3
0
/* initialize GPS listener on serial port */
void gps_init(uint8_t port) {
   // initialize serial port and set baud rate
   serial_init(port);
   serial_baud(port, 57600);

   // set GPS to 57600 baud
   /* uint8_t gps_initstr[] = { 
      0xA0, 0xA1, // header
      0x00, 0x04, // length
      0x05, 0x00, 0x04, 0x01, // payload
      0x00, // checksum
      0x0D, 0x0A // tail
   }; */
   // set GPS to 5 Hz
   uint8_t gps_initstr[] = { 
      0xA0, 0xA1, // header
      0x00, 0x03, // length
      0x0E, 0x05, 0x00, // payload
      0x0B, // checksum 1110 ^ 0101 = 1011 (b
      0x0D, 0x0A // tail
   };
   volatile uint16_t gps_len = sizeof(gps_initstr);
   tx_buffer(port, gps_initstr, (uint16_t*)&gps_len);
   while( gps_len );
   // */

   gps_port = port;
}
int uartadapter_uart_baud(int baud_rate)
{
	int ret = 0;
    	serial_baud(&ua_sobj, baud_rate);
	
	return ret;
}
int uartadapter_uart_open(char *uartname, ua_uart_set_str *puartpara)
{
	PinName uart_tx,uart_rx;

	if(!strcmp("uart0", uartname)){
		uart_tx = PA_7;
		uart_rx = PA_6;
		ua_uart_config[0].hal_uart_adp.BaudRate     = puartpara->BaudRate;
		ua_uart_config[0].hal_uart_adp.FlowControl  = puartpara->FlowControl;
		ua_uart_config[0].hal_uart_adp.WordLen      = puartpara->number;
		ua_uart_config[0].hal_uart_adp.Parity           = puartpara->parity;
		ua_uart_config[0].hal_uart_adp.StopBit         = puartpara->StopBits;
	}else{
		ua_printf(UA_ERROR, "please check uart name!");
		return RTW_ERROR;
	}

	/*initial uart */
	serial_init(&ua_sobj,uart_tx,uart_rx);
    	serial_baud(&ua_sobj,puartpara->BaudRate);
    	serial_format(&ua_sobj, puartpara->number, (SerialParity)puartpara->parity, puartpara->StopBits);
	
	/*uart irq handle*/
	serial_irq_handler(&ua_sobj, uartadapter_uart_irq, (uint32_t)&ua_sobj);
	serial_irq_set(&ua_sobj, RxIrq, 1);	
	serial_irq_set(&ua_sobj, TxIrq, 1);	

	return 0;
}
Exemple #6
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    int is_stdio_uart = 0;
    
    // determine the UART to use
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    MBED_ASSERT((int)uart != NC);
    
    obj->uart = (LPC_UART_TypeDef *)uart;
    // enable power
    switch (uart) {
        case UART_0: LPC_SC->PCONP |= 1 <<  3; break;
        case UART_1: LPC_SC->PCONP |= 1 <<  4; break;
        case UART_2: LPC_SC->PCONP |= 1 << 24; break;
        case UART_3: LPC_SC->PCONP |= 1 << 25; break;
    }
    
    // enable fifos and default rx trigger level
    obj->uart->FCR = 1 << 0  // FIFO Enable - 0 = Disables, 1 = Enabled
                   | 0 << 1  // Rx Fifo Reset
                   | 0 << 2  // Tx Fifo Reset
                   | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars

    // disable irqs
    obj->uart->IER = 0 << 0  // Rx Data available irq enable
                   | 0 << 1  // Tx Fifo empty irq enable
                   | 0 << 2; // Rx Line Status irq enable
    
    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);
    
    // pinout the chosen uart
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);
    
    // set rx/tx pins in PullUp mode
    if (tx != NC) {
        pin_mode(tx, PullUp);
    }
    if (rx != NC) {
        pin_mode(rx, PullUp);
    }
    
    switch (uart) {
        case UART_0: obj->index = 0; break;
        case UART_1: obj->index = 1; break;
        case UART_2: obj->index = 2; break;
        case UART_3: obj->index = 3; break;
    }
    
    is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
    
    if (is_stdio_uart) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #7
0
/*---------------------------------------------------------------------------*/
void
slip_arch_init(unsigned long ubr)
{
	serial_init(&_contiki_slip,USBTX,USBRX);
	serial_baud(&_contiki_slip,ubr);
	serial_irq_handler(&_contiki_slip,&slip_isr,1);
	serial_irq_set(&_contiki_slip,RxIrq,1);
}
Exemple #8
0
/**
 * open serial port
 * @param port - COMn port name
 * @param baud - baud rate
 * @returns 1 for success and 0 for failure
 */
int serial_init(const char* port, unsigned long baud)
{
    struct termios sparm;

    /* open the port */
#ifdef MACOSX
    hSerial = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);
#else
    hSerial = open(port, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
#endif
    if(hSerial == -1) {
        //printf("error: opening '%s' -- %s\n", port, strerror(errno));
        return 0;
    }

    signal(SIGINT, sigint_handler);

    /* set the terminal to exclusive mode */
    if (ioctl(hSerial, TIOCEXCL) != 0) {
        //printf("error: can't open terminal for exclusive access\n");
        close(hSerial);
        return 0;
    }

    fcntl(hSerial, F_SETFL, 0);

    /* set the baud rate */
    if (!serial_baud(baud)) {
        close(hSerial);
        return 0;
    }

    /* get the current options */
    chk("tcgetattr", tcgetattr(hSerial, &old_sparm));
    sparm = old_sparm;

    /* set raw input */
#ifdef MACOSX
    cfmakeraw(&sparm);
    sparm.c_cc[VTIME] = 0;
    sparm.c_cc[VMIN] = 1;
#else
    memset(&sparm, 0, sizeof(sparm));
    sparm.c_cflag     = CS8 | CLOCAL | CREAD;
    sparm.c_lflag     = 0; // &= ~(ICANON | ECHO | ECHOE | ISIG);
    sparm.c_oflag     = 0; // &= ~OPOST;

    sparm.c_iflag     = IGNPAR | IGNBRK;
    sparm.c_cc[VTIME] = 0;
    sparm.c_cc[VMIN] = 1;
#endif

    /* set the options */
    chk("tcflush", tcflush(hSerial, TCIFLUSH));
    chk("tcsetattr", tcsetattr(hSerial, TCSANOW, &sparm));

    return 1;
}
Exemple #9
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    int is_stdio_uart = 0;
    
    // determine the UART to use
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    MBED_ASSERT((int)uart != NC);

    obj->uart = (LPC_USART_Type *)uart;
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
    
    // [TODO] Consider more elegant approach
    // disconnect USBTX/RX mapping mux, for case when switching ports
#ifdef USBTX
    pin_function(USBTX, 0);
    pin_function(USBRX, 0);
#endif

    // enable fifos and default rx trigger level
    obj->uart->FCR = 1 << 0  // FIFO Enable - 0 = Disables, 1 = Enabled
                   | 0 << 1  // Rx Fifo Reset
                   | 0 << 2  // Tx Fifo Reset
                   | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars
    
    // disable irqs
    obj->uart->IER = 0 << 0  // Rx Data available irq enable
                   | 0 << 1  // Tx Fifo empty irq enable
                   | 0 << 2; // Rx Line Status irq enable
    
    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);
    
    // pinout the chosen uart
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);
    
    // set rx/tx pins in PullUp mode
    if (tx != NC) {
        pin_mode(tx, PullUp);
    }
    if (rx != NC) {
        pin_mode(rx, PullUp);
    }
    
    switch (uart) {
        case UART_0: obj->index = 0; break;
    }
    
    is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
    
    if (is_stdio_uart) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #10
0
uart_socket_t* uart_open(uart_set_str *puartpara)
{
	PinName uart_tx = PA_7;//PA_4; //PA_7
	PinName uart_rx = PA_6;//PA_0; //PA_6
	uart_socket_t *u;

	u = (uart_socket_t *)RtlZmalloc(sizeof(uart_socket_t));
	if(!u){
		uart_printf("%s(): Alloc memory for uart_socket failed!\n", __func__);
		return NULL;
	}
	
	/*initial uart */
	serial_init(&u->sobj, uart_tx,uart_rx);
	serial_baud(&u->sobj,puartpara->BaudRate);
	serial_format(&u->sobj, puartpara->number, (SerialParity)puartpara->parity, puartpara->StopBits);

	/*uart irq handle*/
	serial_irq_handler(&u->sobj, uart_irq, (int)u);
	serial_irq_set(&u->sobj, RxIrq, 1);
	serial_irq_set(&u->sobj, TxIrq, 1);

#if UART_SOCKET_USE_DMA_TX
   	serial_send_comp_handler(&u->sobj, (void*)uart_send_stream_done, (uint32_t)u);
#endif

	/*alloc a socket*/
 	u->fd = lwip_allocsocketsd();
	if(u->fd == -1){
		uart_printf("Failed to alloc uart socket!\n");
		goto Exit2;
	}
	/*init uart related semaphore*/
	RtlInitSema(&u->action_sema, 0);
	RtlInitSema(&u->tx_sema, 1);
	RtlInitSema(&u->dma_tx_sema, 1);
	
	/*create uart_thread to handle send&recv data*/
	{
#define	UART_ACTION_STACKSIZE 512
#define	UART_ACTION_PRIORITY	1
		if(xTaskCreate(uart_action_handler, ((const char*)"uart_action"), UART_ACTION_STACKSIZE, u, UART_ACTION_PRIORITY, NULL) != pdPASS){
			uart_printf("%s xTaskCreate(uart_action) failed", __FUNCTION__);
			goto Exit1;
		}
	}
	return u;
Exit1:
	/* Free uart related semaphore */
	RtlFreeSema(&u->action_sema);
	RtlFreeSema(&u->tx_sema);	
	RtlFreeSema(&u->dma_tx_sema);		
Exit2:
	RtlMfree((u8*)u, sizeof(uart_socket_t));
	return NULL;
}
int uartadapter_control_set_req_handle(u8 *pbuf, u32 sz)
{
	u8 *p = pbuf;
	u8 type = 0, len = 0;

       ua_printf(UA_DEBUG, "\n===>uartadapter_control_set_req_handle()");
	UA_PRINT_DATA(pbuf, sz);

	while(p < (pbuf+sz)){
		type = *p++;
		len = *p++;
		ua_printf(UA_DEBUG, "type=%d len=%d\n", type, len);
		switch(type)
		{
			case UART_CTRL_TYPE_BAUD_RATE:
				ua_uart_config[0].hal_uart_adp.BaudRate = *(u32 *)p;
       			ua_printf(UA_INFO, "SET UART BAUD_RATE to %d.\n", ua_uart_config[0].hal_uart_adp.BaudRate);
				serial_baud(&ua_sobj, ua_uart_config[0].hal_uart_adp.BaudRate);
				break;
			case UART_CTRL_TYPE_WORD_LEN:
				ua_uart_config[0].hal_uart_adp.WordLen = *p;
       			ua_printf(UA_INFO, "SET UART WORD_LEN to %d.\n", ua_uart_config[0].hal_uart_adp.WordLen);
				serial_format(&ua_sobj, 
							ua_uart_config[0].hal_uart_adp.WordLen, 
							(SerialParity)ua_uart_config[0].hal_uart_adp.Parity,
							ua_uart_config[0].hal_uart_adp.StopBit);
				break;
			case UART_CTRL_TYPE_PARITY:
				ua_uart_config[0].hal_uart_adp.Parity = *p;
       			ua_printf(UA_INFO, "SET UART PARITY to %d.\n", ua_uart_config[0].hal_uart_adp.Parity);
				serial_format(&ua_sobj, 
							ua_uart_config[0].hal_uart_adp.WordLen, 
							(SerialParity)ua_uart_config[0].hal_uart_adp.Parity,
							ua_uart_config[0].hal_uart_adp.StopBit);
				break;
			case UART_CTRL_TYPE_STOP_BIT:
				ua_uart_config[0].hal_uart_adp.StopBit = *p;
       			ua_printf(UA_INFO, "SET UART STOP_BIT to %d.\n", ua_uart_config[0].hal_uart_adp.StopBit);
				serial_format(&ua_sobj, 
							ua_uart_config[0].hal_uart_adp.WordLen, 
							(SerialParity)ua_uart_config[0].hal_uart_adp.Parity,
							ua_uart_config[0].hal_uart_adp.StopBit);
				break;
			case UART_CTRL_TYPE_FLOW_CTRL:
				ua_uart_config[0].hal_uart_adp.FlowControl = *p;
       			ua_printf(UA_INFO, "SET UART FLOW_CTRL to %d.\n", ua_uart_config[0].hal_uart_adp.FlowControl);
       			ua_printf(UA_INFO, "SET UART FLOW_CTRL not support now.\n");
				//TODO
				break;
		}
		p += len;
	}
	return 0;
}
void UARTClass1::begin( const uint32_t dwBaudRate )
{
	
	serial_init(&(this->sobj),UART1_TX,UART1_RX);

	serial_baud(&(this->sobj),dwBaudRate);
	serial_format(&(this->sobj), 8, ParityNone, 1);
	serial_set_pullNone();

    serial_irq_handler(&(this->sobj), uart_irq, (uint32_t)&(this->sobj));
    serial_irq_set(&(this->sobj), RxIrq, 1);
    serial_irq_set(&(this->sobj), TxIrq, 1);
}
Exemple #13
0
IRSendRev::IRSendRev(uint8_t receivePin, uint8_t transmitPin) {
    this->receivePin = receivePin;
    this->transmitPin = transmitPin;

    pUART = malloc( sizeof(serial_t) );

    serial_init((serial_t *)pUART, (PinName)(g_APinDescription[transmitPin].pinname), (PinName)(g_APinDescription[receivePin].pinname));
    serial_baud((serial_t *)pUART, 38*1000*2);
    serial_format((serial_t *)pUART, 8, ParityNone, 1);

    // initialize receiving
    pGpioIrqRecv = malloc( sizeof(gpio_irq_t) );
    pTimerRecv = malloc( sizeof(gtimer_t) );
}
Exemple #14
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    int is_stdio_uart = 0;
    
    // determine the UART to use
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    if ((int)uart == NC) {
        error("Serial pinout mapping failed");
    }
    
    obj->uart = (LPC_UART_TypeDef *)uart;
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
    
    // enable fifos and default rx trigger level
    obj->uart->FCR = 1 << 0  // FIFO Enable - 0 = Disables, 1 = Enabled
                   | 0 << 1  // Rx Fifo Reset
                   | 0 << 2  // Tx Fifo Reset
                   | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars
    
    // disable irqs
    obj->uart->IER = 0 << 0  // Rx Data available irq enable
                   | 0 << 1  // Tx Fifo empty irq enable
                   | 0 << 2; // Rx Line Status irq enable
    
    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);
    
    // pinout the chosen uart
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);
    
    // set rx/tx pins in PullUp mode
    pin_mode(tx, PullUp);
    pin_mode(rx, PullUp);
    
    switch (uart) {
        case UART_0: obj->index = 0; break;
    }
    
    is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
    
    if (is_stdio_uart) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #15
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    // determine the UART to use -- for mcu's with multiple uart connections
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    
    if ((int)uart == NC) {
        error("Serial pinout mapping failed");       
    }
    
    obj->uart = (NRF_UART_Type *)uart;
    
    //pin configurations -- 
    //outputs    
    NRF_GPIO->DIR |= (1<<tx);//TX_PIN_NUMBER);
    NRF_GPIO->DIR |= (1<<RTS_PIN_NUMBER);

    NRF_GPIO->DIR &= ~(1<<rx);//RX_PIN_NUMBER);
    NRF_GPIO->DIR &= ~(1<<CTS_PIN_NUMBER);
    
    obj->uart->PSELRTS = RTS_PIN_NUMBER;
    obj->uart->PSELTXD = tx;//TX_PIN_NUMBER;
    
    //inputs
    obj->uart->PSELCTS = CTS_PIN_NUMBER;
    obj->uart->PSELRXD = rx;//RX_PIN_NUMBER;
    
    
    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);
    
    obj->uart->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);;
    obj->uart->TASKS_STARTTX = 1;
    obj->uart->TASKS_STARTRX = 1;
    obj->uart->EVENTS_RXDRDY =0;
    
    obj->index = 0;
    
    // set rx/tx pins in PullUp mode
    pin_mode(tx, PullUp);
    pin_mode(rx, PullUp);
    
    if (uart == STDIO_UART) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #16
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    UARTName uart = UART_0;

    MBED_ASSERT((int)uart != NC);

    obj->uart = (NRF_UART_Type *)uart;

    //pin configurations --
    //outputs
    NRF_GPIO->DIR |= (1 << tx); //TX_PIN_NUMBER);
    NRF_GPIO->DIR |= (1 << RTS_PIN_NUMBER);

    NRF_GPIO->DIR &= ~(1 << rx); //RX_PIN_NUMBER);
    NRF_GPIO->DIR &= ~(1 << CTS_PIN_NUMBER);

    obj->uart->PSELRTS = RTS_PIN_NUMBER;
    obj->uart->PSELTXD = tx; //TX_PIN_NUMBER;

    //inputs
    obj->uart->PSELCTS = CTS_PIN_NUMBER;
    obj->uart->PSELRXD = rx; //RX_PIN_NUMBER;


    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);

    obj->uart->ENABLE        = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
    obj->uart->TASKS_STARTTX = 1;
    obj->uart->TASKS_STARTRX = 1;
    obj->uart->EVENTS_RXDRDY = 0;

    obj->index = 0;

    // set rx/tx pins in PullUp mode
    if (tx != NC) {
        pin_mode(tx, PullUp);
    }
    if (rx != NC) {
        pin_mode(rx, PullUp);
    }

    if (uart == STDIO_UART) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #17
0
struct interface_t * interface_serial_alloc(const char * device, int baud)
{
	struct interface_t * iface;
	struct serial_ctx_t * ctx;
	struct termios cfg;
	speed_t speed = serial_baud(baud);
	int fd;

	fd = open(device, O_RDWR | O_NOCTTY | O_SYNC);
	if(fd < 0)
		return 0;

	if(tcgetattr(fd, &cfg))
	{
		close(fd);
		return 0;
	}
	cfmakeraw(&cfg);
	cfsetispeed(&cfg, speed);
	cfsetospeed(&cfg, speed);

	cfg.c_cc[VMIN] = 0;
    cfg.c_cc[VTIME] = 5;
	if(tcsetattr(fd, TCSANOW, &cfg))
	{
		close(fd);
		return 0;
	}

	iface = malloc(sizeof(struct interface_t));
	if(!iface)
		return 0;

	ctx = malloc(sizeof(struct serial_ctx_t));
	if(!ctx)
	{
		free(iface);
		return 0;
	}

	ctx->fd = fd;
	iface->read = serial_read;
	iface->write = serial_write;
	iface->priv = ctx;

	return iface;
}
void UARTClass::begin(const uint32_t BaudRate, uint32_t rx_pin, uint32_t tx_pin)
{
	PinName	pin_rx, pin_tx;
	UARTName uart = UART_0;
	serial_t obj;
	
	pin_rx = Pin_nRF51822_to_Arduino(rx_pin);
	pin_tx = Pin_nRF51822_to_Arduino(tx_pin);
	
	obj.uart = (NRF_UART_Type *)uart;
	serial_init(&obj, pin_tx, pin_rx);
	serial_set_flow_control(&obj, FlowControlNone, (PinName)NC, (PinName)NC);
	serial_baud(&obj, BaudRate);
	/* TXIrq has a error, don't use it */
	serial_irq_set(&obj, RxIrq, 1);
	//serial_irq_set(&obj, TxIrq, 1);
}
Exemple #19
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    UARTName uart = UART_0;
    obj->uart = (NRF_UART_Type *)uart;

    //pin configurations --
    NRF_GPIO->OUT |= (1 << tx);
    NRF_GPIO->OUT |= (1 << RTS_PIN_NUMBER);
    NRF_GPIO->DIR |= (1 << tx); //TX_PIN_NUMBER);
    NRF_GPIO->DIR |= (1 << RTS_PIN_NUMBER);

    NRF_GPIO->DIR &= ~(1 << rx); //RX_PIN_NUMBER);
    NRF_GPIO->DIR &= ~(1 << CTS_PIN_NUMBER);


    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);

    obj->uart->ENABLE        = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
    obj->uart->TASKS_STARTTX = 1;
    obj->uart->TASKS_STARTRX = 1;
    obj->uart->EVENTS_RXDRDY = 0;
    // dummy write needed or TXDRDY trails write rather than leads write.
    //  pins are disconnected so nothing is physically transmitted on the wire
    obj->uart->TXD = 0;

    obj->index = 0;
    
    obj->uart->PSELRTS = RTS_PIN_NUMBER;
    obj->uart->PSELTXD = tx; //TX_PIN_NUMBER;
    obj->uart->PSELCTS = CTS_PIN_NUMBER;
    obj->uart->PSELRXD = rx; //RX_PIN_NUMBER;

    // set rx/tx pins in PullUp mode
    if (tx != NC) {
        pin_mode(tx, PullUp);
    }
    if (rx != NC) {
        pin_mode(rx, PullUp);
    }

    if (uart == STDIO_UART) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #20
0
void config_uart()
{
	// setup uart
	serial_init(&mysobj, UART_TX, UART_RX);
	serial_baud(&mysobj, 38400);
	serial_format(&mysobj, 8, ParityNone, 1);

	serial_irq_handler(&mysobj, uart_irq_callback, (uint32_t)&mysobj);
	serial_irq_set(&mysobj, RxIrq, 1);
	serial_irq_set(&mysobj, TxIrq, 1);

	// config uart rx as gpio wakeup pin
	gpio_irq_t gpio_rx_wake;
	gpio_irq_init(&gpio_rx_wake, UART_RX, gpio_uart_rx_irq_callback, NULL);
	gpio_irq_set(&gpio_rx_wake, IRQ_FALL, 1);   // Falling Edge Trigger
	gpio_irq_enable(&gpio_rx_wake);
}
Exemple #21
0
// This function sets the current object as the "listening"
// one and returns true if it replaces another 
bool SoftwareSerial::listen()
{
    bool ret = false;

    serial_init((serial_t *)pUART, (PinName)g_APinDescription[transmitPin].pinname, (PinName)g_APinDescription[receivePin].pinname);
    serial_baud((serial_t *)pUART, speed);
    serial_format((serial_t *)pUART, data_bits, (SerialParity)parity, stop_bits);

    serial_irq_handler((serial_t *)pUART, (uart_irq_handler)handle_interrupt, (uint32_t)this);
    serial_irq_set((serial_t *)pUART, RxIrq, 1);
    serial_irq_set((serial_t *)pUART, TxIrq, 1);

    if (flowctrl) {
        serial_set_flow_control((serial_t *)pUART, FlowControlRTSCTS, (PinName)0, (PinName)0);
    }

    return ret;
}
Exemple #22
0
/*
 * UART interface
 */
static rt_err_t ameba_uart_configure (struct rt_serial_device *serial, struct serial_configure *cfg)
{
     rt_uint32_t baud_div;
     struct device_uart * uart;

     RT_ASSERT(serial != RT_NULL);
     serial->config = *cfg;

     uart = serial->parent.user_data;
     RT_ASSERT(uart != RT_NULL);

     /* Set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
     serial_format(&uart->serial, 8, ParityNone, 1);

     /* set baudrate */
     serial_baud(&uart->serial, 115200);

     return (RT_EOK);
}
Exemple #23
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    int is_stdio_uart = 0;

    // determine the UART to use
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    if ((int)uart == NC) {
        error("Serial pinout mapping failed");
    }

    RCC->APB2ENR |= RCC_APB2ENR_AFIOEN ;
    switch (uart) {
    case UART_1: RCC->APB2ENR |= RCC_APB2ENR_USART1EN; break;
    case UART_2: RCC->APB1ENR |= RCC_APB1ENR_USART2EN; break;
    }
    obj->uart = (USART_TypeDef *)uart;

    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);

    // pinout the chosen uart
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);

    // set rx/tx pins in PullUp mode
    pin_mode(tx, PullUp);
    pin_mode(rx, PullUp);

    obj->uart->CR1 = USART_CR1_UE | USART_CR1_TE | USART_CR1_RE;

    switch (uart) {
        case UART_1: obj->index = 0; break;
    }

    is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);

    if (is_stdio_uart) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #24
0
void main(void)
{
    // sample text
    char rc;
    serial_t    sobj;

    // mbed uart test
    serial_init(&sobj,UART_TX,UART_RX);
    serial_baud(&sobj,38400);
    serial_format(&sobj, 8, ParityNone, 1);

    uart_send_string(&sobj, "UART API Demo...\r\n");
    uart_send_string(&sobj, "Hello World!!\r\n");
    while(1){
        uart_send_string(&sobj, "\r\n8195a$");
        rc = serial_getc(&sobj);
        serial_putc(&sobj, rc);
    }
}
Exemple #25
0
void IRSendRev::Send(unsigned char *idata, unsigned char ifreq)
{
    int i, j;

    int len = idata[0];
    unsigned char c;
    unsigned char start_high    = idata[1];
    unsigned char start_low     = idata[2];
    unsigned char nshort        = idata[3];
    unsigned char nlong         = idata[4];
    unsigned char datalen       = idata[5];

    uint32_t bytecount;
    unsigned char *pClockData;

    bytecount = start_high * ifreq / 100;
    pClockData = (unsigned char *) malloc (bytecount);
    for (i=0; i<bytecount; i++) {
        pClockData[i] = 0x55;
    }
    serial_baud((serial_t *)pUART, ifreq*1000*2);

    serial_send_blocked((serial_t *)pUART, (char *)pClockData, bytecount, 10);

    HalDelayUs(start_low * 50);

    bytecount = nshort * ifreq / 100;
    for (i=0; i<datalen; i++) {
        c = idata[i+6];
        for (j=0; j<8; j++) {
            serial_send_blocked((serial_t *)pUART, (char *)pClockData, bytecount, 1);
            if (c & (0x01 << (7-j))) {
                HalDelayUs(nlong * 50);
            } else {
                HalDelayUs(nshort * 50);
            }
        }
    }
    serial_send_blocked((serial_t *)pUART, (char *)pClockData, bytecount, 1);

    free(pClockData);
}
Exemple #26
0
void uart_socket_example_1(void *param)
{
	    // sample text
    char rc;
    serial_t    sobj;

    // mbed uart test
    serial_init(&sobj,PA_7,PA_6);
    serial_baud(&sobj,9600);
    serial_format(&sobj, 8, ParityNone, 1);

    uart_send_string(&sobj, "UART API Demo...\r\n");
    uart_send_string(&sobj, "Hello World!!\r\n");
    while(1){
        //uart_send_string(&sobj, "\r\n8195a$");
        rc = serial_getc(&sobj);
        //serial_putc(&sobj, rc);
        printf("%x\n",rc);
    }
}
int serial_init(const char *port, unsigned long baud)
{
    char fullPort[20];

    sprintf(fullPort, "\\\\.\\%s", port);

    hSerial = CreateFile(
        fullPort,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);

    if (hSerial == INVALID_HANDLE_VALUE)
        return FALSE;

    /* Set the baud rate. Always succeeds with mingw. */
    if (!serial_baud(baud)) {
        serial_done();
        return 0;
    }


    GetCommTimeouts(hSerial, &original_timeouts);
    timeouts = original_timeouts;
    timeouts.ReadIntervalTimeout = MAXDWORD;
    timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
    timeouts.WriteTotalTimeoutConstant = 1;
    timeouts.WriteTotalTimeoutMultiplier = 1;

    /* setup device buffers */
    SetupComm(hSerial, 10000, 10000);

    /* purge any information in the buffer */
    PurgeComm(hSerial, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

    return TRUE;
}
Exemple #28
0
//******************************************************************************
void serial_init(serial_t *obj, PinName tx, PinName rx)
{
    // Determine which uart is associated with each pin
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);

    // Make sure that both pins are pointing to the same uart
    MBED_ASSERT(uart != (UARTName)NC);

    // Set the obj pointer to the proper uart
    obj->uart = (mxc_uart_regs_t*)uart;

    // Set the uart index
    obj->index = MXC_UART_BASE_TO_INSTANCE(obj->uart);

    // Configure the pins
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);

    // Flush the RX and TX FIFOs, clear the settings
    obj->uart->ctrl = ( MXC_F_UART_CTRL_TX_FIFO_FLUSH |  MXC_F_UART_CTRL_RX_FIFO_FLUSH);

    // Disable interrupts
    obj->uart->inten = 0;
    obj->uart->intfl = 0;

    // Configure to default settings
    serial_baud(obj, DEFAULT_BAUD);
    serial_format(obj, 8, ParityNone, 1);

    // Manage stdio UART
    if(uart == STDIO_UART) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #29
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    volatile uint8_t dummy ;
    int is_stdio_uart = 0;
    // determine the UART to use
    uint32_t uart_tx = pinmap_peripheral(tx, PinMap_UART_TX);
    uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX);
    uint32_t uart = pinmap_merge(uart_tx, uart_rx);

    MBED_ASSERT((int)uart != NC);

    obj->serial.uart = (struct st_scif *)SCIF[uart];
    // enable power
    switch (uart) {
    case UART0:
        CPG.STBCR4 &= ~(1 <<  7);
        break;
    case UART1:
        CPG.STBCR4 &= ~(1 <<  6);
        break;
    case UART2:
        CPG.STBCR4 &= ~(1 <<  5);
        break;
    case UART3:
        CPG.STBCR4 &= ~(1 <<  4);
        break;
    case UART4:
        CPG.STBCR4 &= ~(1 <<  3);
        break;
    case UART5:
        CPG.STBCR4 &= ~(1 <<  2);
        break;
    case UART6:
        CPG.STBCR4 &= ~(1 <<  1);
        break;
    case UART7:
        CPG.STBCR4 &= ~(1 <<  0);
        break;
    }
    dummy = CPG.STBCR4;

    /* ==== SCIF initial setting ==== */
    /* ---- Serial control register (SCSCR) setting ---- */
    /* B'00 : Internal CLK */
    obj->serial.uart->SCSCR = 0x0000u;          /* SCIF transmitting and receiving operations stop */

    /* ---- FIFO control register (SCFCR) setting ---- */
    /* Transmit FIFO reset & Receive FIFO data register reset */
    obj->serial.uart->SCFCR = 0x0006;

    /* ---- Serial status register (SCFSR) setting ---- */
    dummy = obj->serial.uart->SCFSR;
    obj->serial.uart->SCFSR = (dummy & 0xFF6Cu);         /* ER,BRK,DR bit clear */

    /* ---- Line status register (SCLSR) setting ---- */
    /* ORER bit clear */
    obj->serial.uart->SCLSR = 0;

    /* ---- Serial extension mode register (SCEMR) setting ----
    b7 BGDM - Baud rate generator double-speed mode  : Normal mode
    b0 ABCS - Base clock select in asynchronous mode : Base clock is 16 times the bit rate */
    obj->serial.uart->SCEMR = 0x0000u;

    /* ---- Bit rate register (SCBRR) setting ---- */
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);

    /* ---- FIFO control register (SCFCR) setting ---- */
    obj->serial.uart->SCFCR = 0x0030u;

    /* ---- Serial port register (SCSPTR) setting ----
    b1 SPB2IO - Serial port break output : disabled
    b0 SPB2DT - Serial port break data   : High-level */
    obj->serial.uart->SCSPTR = 0x0003u;    // SPB2IO = 1, SPB2DT = 1

    /* ---- Line status register (SCLSR) setting ----
    b0 ORER - Overrun error detect : clear */

    if (obj->serial.uart->SCLSR & 0x0001) {
        obj->serial.uart->SCLSR = 0u;      // ORER clear
    }

    // pinout the chosen uart
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);

    switch (uart) {
    case UART0:
        obj->serial.index = 0;
        break;
    case UART1:
        obj->serial.index = 1;
        break;
    case UART2:
        obj->serial.index = 2;
        break;
    case UART3:
        obj->serial.index = 3;
        break;
    case UART4:
        obj->serial.index = 4;
        break;
    case UART5:
        obj->serial.index = 5;
        break;
    case UART6:
        obj->serial.index = 6;
        break;
    case UART7:
        obj->serial.index = 7;
        break;
    }
    uart_data[obj->serial.index].sw_rts.pin = NC;
    uart_data[obj->serial.index].sw_cts.pin = NC;

    /* ---- Serial control register (SCSCR) setting ---- */
    /* Setting the TE and RE bits enables the TxD and RxD pins to be used. */
    obj->serial.uart->SCSCR = 0x0070;

    is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);

    if (is_stdio_uart) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
void SerialBase::baud(int baudrate) {
    lock();
    serial_baud(&_serial, baudrate);
    _baud = baudrate;
    unlock();
}