Exemple #1
0
bool app_openuart(void){
	//Open a uart
	uart_init(uart);
	uart_setdevice(uart,"/dev/ttyS41");
	if (!uart_configure(uart,B9600,false)){
		//Failed to open.
		printf_clr(CLR_YELLOW,"Try a different tty:?\n");
		uint8_t newname[64];
		fgets(newname,64,stdin);
		//Remove \n
		int len = strlen(newname);	
		newname[len-1] = 0;		

		uart_setdevice(uart,newname);
		if (!uart_configure(uart,B9600,false)){
			printf_clr(CLR_RED,"Nope.\n");
			return false;
		}
	}

	//Setup everything:
	fifo_init(&txfifo,txbuffer,UART_BUFFER_SIZE);
	fifo_init(&rxfifo,rxbuffer,UART_BUFFER_SIZE);
	//Attach fifo's to uart.
	uart->rxfifo = &rxfifo;
	uart->txfifo = &txfifo;

	printf("Attatching funcions to stream\n");
	stream_init(stream,uart);
	stream->write_start = &stream_writestart;
	stream->write_stop = &stream_writestop;

	bus_init(bus);
}
/*---------------------------------------------------------------------------*/
void
uart_set_speed(unsigned mode, unsigned ubr0,
		unsigned ubr1, unsigned umctl)
{
  /* store the setting */
  uart_speed_br0[mode] = ubr0;	/* baudrate */
  uart_speed_br1[mode] = ubr1;	/* baudrate */
  uart_speed_bmn[mode] = umctl;	/* modulation */

  /* reconfigure, if mode active */
  if(uart_mode == mode) {
    uart_configure(mode);
  }
}
static void handle_configure(const char *req, int *req_index)
{
    struct uart_config config = current_config;
    if (parse_option_list(req, req_index, &config) < 0) {
        send_error_response("einval");
        return;
    }

    if (uart_configure(uart, &config) >= 0) {
        current_config = config;
        send_ok_response();
    } else {
        send_error_response(uart_last_error());
    }
}
/*---------------------------------------------------------------------------*/
void
uart_set_mode(unsigned mode)
{
  /* do nothing if the mode is already set */
  if(mode == uart_mode) {
    return;
  }

  IE2 &= ~(URXIE1 | UTXIE1);		/* disable irq */
  uart_configure(mode);			/* configure uart parameters */
  uart_mode = mode;
	
  if(uart_handler[mode] != NULL) {
    IE2 |= URXIE1;			/* Enable USART1 RX interrupt */
  }
}
Exemple #5
0
void main(void) {
    char c;
    signed char length;
    unsigned char msgtype;
    unsigned char last_reg_recvd;
    i2c_comm ic;
    unsigned char msgbuffer[MSGLEN + 1];
    unsigned char i;
    uart_thread_struct uthread_data; // info for uart_lthread
    timer1_thread_struct t1thread_data; // info for timer1_lthread
    timer0_thread_struct t0thread_data; // info for timer0_lthread

    t1thread_data.new_move_msg=0;

    #ifdef __USE18F2680
    OSCCON = 0xFC; // see datasheet
    // We have enough room below the Max Freq to enable the PLL for this chip
    OSCTUNEbits.PLLEN = 1; // 4x the clock speed in the previous line
    #else
    #ifdef __USE18F45J10
    OSCCON = 0x82; // see datasheeet
    OSCTUNEbits.PLLEN = 0; // Makes the clock exceed the PIC's rated speed if the PLL is on
    #else
    #ifdef __USE18F26J50
    OSCCON = 0xE0; // see datasheeet
    OSCTUNEbits.PLLEN = 1;
    #else
    #ifdef __USE18F46J50
    OSCCON = 0xE0; //see datasheet
    //Alex:
    //OSCCON : OSCILLATOR CONTROL REGISTER
    //OSCCON[7] : IDLEN = ( 1 = Device enters Idle mode on SLEEP instruction ) or ( 0 = Device enters Sleep mode on SLEEP instruction )
    //OSCCON[6:4] : IRCF; 111 = 8 MHz, 110 = 4 MHz(2), 101 = 2 MHz, 100 = 1 MHz, 011 = 500 kHz, 010 = 250 kHz, 001 = 125 kHz, 000 = 31 kHz
    //OSCCON[3] : OSTS = Oscillator Start-up Time-out Status bit = ( 1 = Oscillator Start-up Timer time-out has expired; primary oscillator is running ) or ( 0 = Oscillator Start-up Timer time-out is running; primary oscillator is not ready )
    //OSCCON[2] : ( Unimplemented: Read as ?1? ) ??????
    //OSCCON[0:1] : SCS = System Clock Select bits
    OSCTUNEbits.PLLEN = 1;
    #else
    Something is messed up.
    The PIC selected is not supported or the preprocessor directives are wrong.
    #endif
    #endif
    #endif
    #endif

    // initialize my uart recv handling code
    //init_uart_recv(&uc);

    // initialize the i2c code
    //Alex:
    //Essentially just sets error and status flags to intial values ( ic is a struct )
    init_i2c(&ic);

    // init the timer1 lthread
    init_timer1_lthread(&t1thread_data);

    // initialize message queues before enabling any interrupts
    init_queues();

    #ifndef __USE18F26J50
    // set direction for PORTB to output
    TRISB = 0x0;
    LATB = 0x0;
    #endif

    // how to set up PORTA for input (for the V4 board with the PIC2680)
    /*
            PORTA = 0x0;	// clear the port
            LATA = 0x0;		// clear the output latch
            ADCON1 = 0x0F;	// turn off the A2D function on these pins
            // Only for 40-pin version of this chip CMCON = 0x07;	// turn the comparator off
            TRISA = 0x0F;	// set RA3-RA0 to inputs
     */

    // initialize Timers
    OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_128);

    
    #ifdef __USE18F26J50
    // MTJ added second argument for OpenTimer1()
    OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0x0);
    #else
    #ifdef __USE18F46J50
    OpenTimer1(TIMER_INT_ON & T1_SOURCE_FOSC_4 & T1_PS_1_8 & T1_16BIT_RW & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,0x0);

    //configure Timer 3
    /*
    TRISAbits.TRISA5 = 1;
    T3CON = 0x00;
    T3CONbits.TMR3CS = 0x2;
    T3CONbits.T3CKPS = 0x0;
    T3CONbits.RD16 = 0;
    T3CONbits.T3SYNC = 0;
    T3CONbits.TMR3ON = 1;
    RPINR6 = 0x02;
    */

    #else
    OpenTimer1(TIMER_INT_ON & T1_PS_1_8 & T1_16BIT_RW & T1_SOURCE_INT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF);
    #endif
    #endif

    // Decide on the priority of the enabled peripheral interrupts
    // 0 is low, 1 is high
    // Timer1 interrupt
    IPR1bits.TMR1IP = 0;
    // Timer3 interrupt
    IPR2bits.TMR3IP = 0;
    // USART RX interrupt
    IPR1bits.RCIP = 0;
    
    // I2C interrupt
    IPR1bits.SSPIP = 1;     

    // configure the hardware i2c device as a slave (0x9E -> 0x4F) or (0x9A -> 0x4D)
    #if 1
    // Note that the temperature sensor Address bits (A0, A1, A2) are also the
    // least significant bits of LATB -- take care when changing them
    // They *are* changed in the timer interrupt handlers if those timers are
    //   enabled.  They are just there to make the lights blink and can be
    //   disabled.
    //i2c_configure_slave(0x9E);
    i2c_configure_master(I2C_DEFAULT_PIC_ADDRESS);
    #else
    // If I want to test the temperature sensor from the ARM, I just make
    // sure this PIC does not have the same address and configure the
    // temperature sensor address bits and then just stay in an infinite loop
    i2c_configure_slave(0x9A);
  
    for (;;);
    #endif

    // must specifically enable the I2C interrupts
    PIE1bits.SSPIE = 1;

    
    
    

    /*
    // configure the hardware USART device
    #ifdef __USE18F26J50
    Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
        USART_CONT_RX & USART_BRGH_LOW, 0x19);
    #else
    #ifdef __USE18F46J50

    Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
        USART_CONT_RX & USART_BRGH_LOW, 0x19);

    #else
    OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
        USART_CONT_RX & USART_BRGH_LOW, 0x19);
    #endif
    #endif
    */

    // Alex: Set registers for debug output
    #ifdef DEBUG_MODE
    debug_configure();
    blip();
    blip1();
    blip2();
    blip3();
    blip4();
    #endif

    //uart_send_byte( 0x50 );
    //uart_send_byte( 0x54 );
    
    

    // Peripheral interrupts can have their priority set to high or low
    // enable high-priority interrupts and low-priority interrupts
    enable_interrupts();

    /* Junk to force an I2C interrupt in the simulator (if you wanted to)
    PIR1bits.SSPIF = 1;
    _asm
    goto 0x08
    _endasm;
     */    

    //Alex: Configure UART for transmit and recieve
    uart_configure();

    // Initialize snesor data buffer
    unsigned char sensor_data[MSGLEN];
    sensor_data[0] = MSGID_SENSOR_RESPONSE;
    for(i=1;i<MSGLEN;i++)
    {
        sensor_data[i] = 0x00;
    }

    // Initialize motor data buffer
    unsigned char motor_data[MSGLEN];
    motor_data[0] = MSGID_MOTOR_RESPONSE;
    for(i=1;i<MSGLEN;i++)
    {
        motor_data[i] = 0x00;
    }

    

    // printf() is available, but is not advisable.  It goes to the UART pin
    // on the PIC and then you must hook something up to that to view it.
    // It is also slow and is blocking, so it will perturb your code's operation
    // Here is how it looks: printf("Hello\r\n");


    // loop forever
    // This loop is responsible for "handing off" messages to the subroutines
    // that should get them.  Although the subroutines are not threads, but
    // they can be equated with the tasks in your task diagram if you
    // structure them properly.
    while (1) {
        // Call a routine that blocks until either on the incoming
        // messages queues has a message (this may put the processor into
        // an idle mode)
        block_on_To_msgqueues();


        // At this point, one or both of the queues has a message.  It
        // makes sense to check the high-priority messages first -- in fact,
        // you may only want to check the low-priority messages when there
        // is not a high priority message.  That is a design decision and
        // I haven't done it here.
        length = ToMainHigh_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer);
        if (length < 0) {
            // no message, check the error code to see if it is concern
            if (length != MSGQUEUE_EMPTY) {
                // This case be handled by your code.
            }
        } 
        else
        {
            switch (msgtype) {
                case MSGT_TIMER0:
                {
                    timer0_lthread(&t0thread_data, msgtype, length, msgbuffer);
                    break;
                };
                case MSGT_I2C_DATA:
                {

                    switch(msgbuffer[0])
                    {
                        case MSGID_SENSOR_RESPONSE:
                        {
                            for(i=2;i<MSGLEN-2;i++)
                            {
                                sensor_data[i] = msgbuffer[i];
                            }

                            send_uart_message( sensor_data );
                            
                            break;
                        }
                        case MSGID_MOTOR_RESPONSE:
                        {
                            for(i=2;i<MSGLEN-2;i++)
                            {
                                motor_data[i] = msgbuffer[i];
                            }

                            //motor_data[3] = 3;

                            send_uart_message( motor_data );

                            break;
                        }
                        default:
                        {

                            break;
                        }
                    }

                    
                }
                case MSGT_I2C_DBG:
                {
                    // Here is where you could handle debugging, if you wanted
                    // keep track of the first byte received for later use (if desired)
                    last_reg_recvd = msgbuffer[0];
                    break;
                };
                case MSGT_I2C_RQST:
                {
                    // Generally, this is *NOT* how I recommend you handle an I2C slave request
                    // I recommend that you handle it completely inside the i2c interrupt handler
                    // by reading the data from a queue (i.e., you would not send a message, as is done
                    // now, from the i2c interrupt handler to main to ask for data).
                    //
                    // The last byte received is the "register" that is trying to be read
                    // The response is dependent on the register.

                    break;
                };
                default:
                {
                    // Your code should handle this error

                    // Sometimes the best course of action is to do nothing

                    break;
                };
            };
        }

        

        // Check the low priority queue
        length = ToMainLow_recvmsg(MSGLEN, &msgtype, (void *) msgbuffer);
        if (length < 0) {
            // no message, check the error code to see if it is concern
            if (length != MSGQUEUE_EMPTY) {
                // Your code should handle this situation
            }
        } 
        else
        {
            unsigned char uart_response[UART_DATA_LENGTH];
            int jjj;
            for(jjj=0;jjj<UART_DATA_LENGTH;jjj++)
            {
                uart_response[jjj] = 0;
            }
            switch (msgtype)
            {

                case MSGT_TIMER1:
                {
                    
                    timer1_lthread(&t1thread_data, msgtype, length, msgbuffer);
                    break;
                };
                case MSGT_OVERRUN:
                {}
                case MSGT_UART_BAD_CHECKSUM:
                {                    
                    uart_response[0] = MSGID_UART_BAD_CHECKSUM; //Set Message ID
                    uart_response[1] = msgbuffer[0];
                    send_uart_message( uart_response );
                    break;
                }
                case MSGT_UART_BAD_COUNTER:
                {
                    uart_response[0] = MSGID_UART_BAD_COUNTER; //Set Message ID
                    uart_response[1] = msgbuffer[0];
                    uart_response[2] = msgbuffer[1];
                    send_uart_message( uart_response );
                    break;
                }
                case MSGT_UART_BAD_START:
                {
                    uart_response[0] = MSGID_UART_BAD_START; //Set Message ID
                    uart_response[1] = msgbuffer[0];
                    send_uart_message( uart_response );
                    break;
                }
                case MSGT_UART_BAD_END:
                {
                    uart_response[0] = MSGID_UART_BAD_END; //Set Message ID
                    uart_response[1] = msgbuffer[0];
                    send_uart_message( uart_response );
                    break;

                }
                case MSGT_UART_ACK_DATA:
                {
                    uart_response[0] = MSGID_UART_ACK; //Set Message ID
                    uart_response[1] = msgbuffer[0];
                    send_uart_message( uart_response );
                    break;

                }
                case MSGT_UART_DATA:
                {
                   
                    //uart_lthread(&uthread_data, msgtype, length, msgbuffer);


                    switch( msgbuffer[0] )
                    {
                        case MSGID_STATUS_REQUEST:
                        {

                            //send_uart_message( sensor_data );
                            break;
                        }
                        case MSGID_SENSOR_REQUEST:
                        {
                            send_uart_message( sensor_data );
                            break;
                        }
                        case MSGID_MOTOR_REQUEST:
                        {
                            send_uart_message( motor_data );
                            motor_data[1] = 0;
                            break;
                        }
                        case MSGID_MOVE:
                        {
                            // Copy msgbuffer over for timer 1 to deal with
                            for(i=0;i<UART_DATA_LENGTH;i++)
                            {
                                t1thread_data.move_msg[i] = msgbuffer[i];
                            }

                            t1thread_data.new_move_msg = 1;
                            
                            break;
                        }                        
                        default:
                        {
                            
                            break;
                        }


                    }

                    break;
                };
                default:
                {
                    // Your code should handle this error

                    // Sometimes the best course of action is to do nothing
                    
                    break;
                };
            };
        }
    }

}
Exemple #6
0
int main( void )
{
	// Init the semi-hosting.
	printf( "\n" );

	// DAC/DMA Setup
	NVIC_DisableIRQ( DMA_IRQn);
	InitializeDAC();
	InitializeDMA();

	/* Instantiate queue and semaphores */
	xQueueToneInput = xQueueCreate( DTMF_REQ_QUEUE_SIZE, sizeof( char ) );
	xQueueDMARequest = xQueueCreate( DMA_REQ_QUEUE_SIZE, sizeof( DAC_Setup_Message ));
	xIoQueue = xQueueCreate(16,sizeof(char));
	dacResponseHandle = xQueueCreate( DMA_COMP_QUEUE_SIZE, sizeof( DAC_Complete_Message ) );
	sampQ = xQueueCreate( 1, sizeof(DTMFSampleType *) );
	resultQ = xQueueCreate( 1, sizeof(struct DTMFResult_t) );
	lQueues.xIoInputQueue = xQueueCreate( 2, sizeof(xData) );
	lQueues.xDACQueue =     xQueueToneInput;


	if( sampQ != NULL &&
		resultQ != NULL &&
		xQueueToneInput != NULL &&
		xQueueDMARequest != NULL &&
		dacResponseHandle != NULL &&
		lQueues.xIoInputQueue != NULL &&
		lQueues.xDACQueue != NULL) {

	  xTaskCreate(  vTaskToneGenerator, /* Pointer to the function that implements the task. */
					  "ToneGenerator",          /* Text name for the task.  This is to facilitate debugging only. */
					  240,                      /* Stack depth in words. */
					  NULL,                     /* No input data */
					  configMAX_PRIORITIES-2,                        /* This task will run at priority 1. */
					  NULL );                   /* We are not using the task handle. */

	  #ifdef TONEGEN_INPUT_UNIT_TEST
	    xTaskCreate( vTaskToneRequestTest, "ToneRequestTest", 240, NULL, configMAX_PRIORITIES-2/*4*/, NULL );
	  #endif

	  #ifdef  TONEGEN_DMA_UNIT_TEST
	    xTaskCreate( vTaskDMAHandlerTest, "DMAHandlerTest", 240, NULL, 2, NULL );
	  #else
	    //============================================================================
	    // Create DAC and DMA Tasks
	    //============================================================================
	    xTaskCreate(  DAC_Handler,/* Pointer to the function that implements the task. */
						"DAC",            /* Text name for the task.  This is to facilitate debugging only. */
						240,              /* Stack depth in words. */
						(void*)xQueueDMARequest, /* Pass the text to be printed in as the task parameter. */
						configMAX_PRIORITIES-1,           /* This task will run at highest priority. */
						NULL );           /* We are not using the task handle. */

	    #endif

		xTaskCreate(	vAdcTask,
						"tADC",
						240,
						(void *)sampQ,
						2,
						NULL );

#ifdef __DTMF_PERF__
		TestBenchTaskParam.sampQ = sampQ;
		TestBenchTaskParam.resultQ = resultQ;
		xTaskCreate(	vTestBenchTask,
						"tTB",
						500,
						(void *)&TestBenchTaskParam,
						3,
						NULL );
#endif

		DTMFDetectTaskParam.sampQ = sampQ;
		DTMFDetectTaskParam.resultQ = resultQ;
		xTaskCreate(	vDTMFDetectTask,
						"tDetect",
						500,
						(void *)&DTMFDetectTaskParam,
						configMAX_PRIORITIES-3,
						NULL );

		xTaskCreate( uart_tx_handler, "Tx Task", 500, NULL, 2, NULL );
		xTaskCreate( uart_rx_handler, "Rx Task", 500, NULL, 2, NULL );
		uart_configure();



		/* Create four instances of the task that will write to the queue */
		xTaskCreate( gpioInterfaceTask, "Keypad_Task", 240, &xIoQueue, configMAX_PRIORITIES-1, NULL);
		xTaskCreate( vIoRxTask, "IO_Receiver", 240, NULL, configMAX_PRIORITIES-1, NULL );

		/* Start the scheduler so our tasks start executing. */
		vTaskStartScheduler();
	}

	/* If all is well we will never reach here as the scheduler will now be
	running.  If we do reach here then it is likely that there was insufficient
	heap available for the idle task to be created. */
	for( ;; );
	return 0;
}
Exemple #7
0
/**
* UART device in RT-Thread
*/
static rt_err_t fh_uart_configure(struct rt_serial_device *serial,
                                  struct serial_configure *cfg)
{
    int div;
    enum data_bits data_mode;
    enum stop_bits stop_mode;
    enum parity parity_mode;
    struct fh_uart *uart;

    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);
    uart = (struct fh_uart *)serial->parent.user_data;

    switch (cfg->data_bits)
    {
    case DATA_BITS_8:
        data_mode = UART_DATA_BIT8;
        break;
    case DATA_BITS_7:
        data_mode = UART_DATA_BIT7;
        break;
    case DATA_BITS_6:
        data_mode = UART_DATA_BIT6;
        break;
    case DATA_BITS_5:
        data_mode = UART_DATA_BIT5;
        break;
    default:
        data_mode = UART_DATA_BIT8;
        break;
    }

    switch (cfg->stop_bits)
    {
    case STOP_BITS_2:
        stop_mode = UART_STOP_BIT2;
        break;
    case STOP_BITS_1:
    default:
        stop_mode = UART_STOP_BIT1;
        break;
    }

    switch (cfg->parity)
    {
    case PARITY_ODD:
        parity_mode = UART_PARITY_ODD;
        break;
    case PARITY_EVEN:
        parity_mode = UART_PARITY_EVEN;
        break;
    case PARITY_NONE:
    default:
        parity_mode = UART_PARITY_NONE;
        break;
    }

    uart_disable_irq(uart->uart_port, UART_IER_ERBFI);

    uart_configure(uart->uart_port, data_mode,
                   stop_mode, parity_mode,
                   cfg->baud_rate, UART_CLOCK_FREQ);

    uart_enable_irq(uart->uart_port, UART_IER_ERBFI);

    return RT_EOK;
}
Exemple #8
0
static void initserial(void) {
	uart_configure();
}