Beispiel #1
0
int Timer::init(Tmr::port_t port){
	Timer::port = port;
	tmr_action_t action;
	Tmr tmr(port);
	if(  tmr.init(1000000) < 0 ){
		return -1;
	}
	tmr.on();

	//initialize the interrupts
	action.channel = TMR_ACTION_CHANNEL_OC0;
	action.context = 0;
	action.callback = tmr_priv_expired;
	action.event = TMR_ACTION_EVENT_INTERRUPT;
	hwpl_tmr_setaction(port, &action);

	return 0;
}
int open_usecond_tmr(void){
	int err;
	tmr_attr_t cfg;
	tmr_action_t action;
	tmr_reqattr_t chan_req;
	device_periph_t tmr;


	tmr.port = clk_usecond_tmr;
	//Open the microsecond timer
	err = hwpl_tmr_open((device_cfg_t*)&tmr);
	if (err){
		return err;
	}

	memset(&cfg, 0, sizeof(tmr_attr_t));
	cfg.freq = 1000000;
	cfg.clksrc = TMR_CLKSRC_CPU;
	err = hwpl_tmr_setattr(tmr.port, &cfg);
	if ( err ){
		return err;
	}

	//Initialize the value of the timer to zero
	err = hwpl_tmr_set(tmr.port, (void*)0);
	if (err){
		return err;
	}

	//Set the reset output compare value to reset the clock every 32 seconds
	chan_req.channel = CAOSLIB_USECOND_TMR_RESET_OC;
	chan_req.value = CAOS_USECOND_PERIOD; //overflow every SCHED_TIMEVAL_SECONDS seconds
	err = hwpl_tmr_setoc(tmr.port, &chan_req);
	if (err){
		return -1;
	}

	action.channel = CAOSLIB_USECOND_TMR_RESET_OC;
	action.event = TMR_ACTION_EVENT_RESET|TMR_ACTION_EVENT_INTERRUPT;
	action.callback = usecond_overflow_event;
	err = hwpl_tmr_setaction(tmr.port, &action);
	if (err){
		return -1;
	}

	//Turn the timer on
	err = hwpl_tmr_on(tmr.port, NULL);
	if (err){
		return -1;
	}

	//This sets up the output compare unit used with the usleep() function
	chan_req.channel = CAOSLIB_USECOND_TMR_SLEEP_OC;
	chan_req.value = CAOS_USECOND_PERIOD + 1;
	err = hwpl_tmr_setoc(tmr.port, &chan_req);
	if ( err ){
		return -1;
	}

	action.channel = CAOSLIB_USECOND_TMR_SLEEP_OC;
	action.event = TMR_ACTION_EVENT_INTERRUPT;
	action.callback = priv_usecond_match_event;

	err = hwpl_tmr_setaction(tmr.port, &action);
	if ( err ){
		return -1;
	}

	return 0;
}
Beispiel #3
0
int Tmr::setaction(const tmr_action_t * action){
	return hwpl_tmr_setaction(port_, (void*)action);
}
Beispiel #4
0
int lcd_ioctl(const device_cfg_t * cfg, int request, void * ctl){
	mlcd_attr_t * attr = (mlcd_attr_t*)ctl;
	tmr_action_t action;
	pio_attr_t pattr;
	device_periph_t p;

	switch(request){
	case I_MLCD_GETATTR:
		attr->freq = LCD_FREQ; //LCD updates 30 times per second
		attr->h = LCD_HEIGHT;
		attr->w = LCD_WIDTH;
		attr->size = LCD_ROWS * LCD_COLS;
		attr->mem = mem;
		attr->hold = lcd_hold;
		attr->rows = LCD_ROWS;
		attr->cols = LCD_COLS/4;
		attr->orientation = (ORIENT_BOTTOM)|(ORIENT_LEFT);
		break;

	case I_MLCD_CLEAR:
		memset(mem, 0, LCD_ROWS * LCD_COLS);
		lcd_hold = 0x80;
		break;

	case I_MLCD_HOLD:
		if( LCD_HOLD_COUNT() < 127 ){
			lcd_hold++;
		}
		break;

	case I_MLCD_RELEASE:
		if( LCD_HOLD_COUNT() > 0 ){
			lcd_hold--;
			LCD_TOUCH();
		}
		break;

	case I_MLCD_INIT:

		//initialize the IO -- chip select first
		pattr.mask = LCD_SPI_CS_PINMASK;
		pattr.mode = PIO_MODE_OUTPUT;
		p.port = LCD_SPI_CS_PORT;
		hwpl_pio_open((device_cfg_t*)&p);
		hwpl_pio_setattr(LCD_SPI_CS_PORT, &pattr);
		hwpl_pio_setmask(LCD_SPI_CS_PORT, (void*)LCD_SPI_CS_PINMASK);

		//Now A0
		pattr.mask = LCD_SPI_A0_PINMASK;
		if( p.port != LCD_SPI_A0_PORT ){
			p.port = LCD_SPI_A0_PORT;
			hwpl_pio_open((device_cfg_t*)&p);
		}
		hwpl_pio_setattr(LCD_SPI_A0_PORT, &pattr);
		hwpl_pio_setmask(LCD_SPI_A0_PORT, (void*)LCD_SPI_A0_PINMASK);

		//configure the timer to update the LCD
		action.channel = LCD_USECOND_OC;
		action.event = TMR_ACTION_EVENT_INTERRUPT;
		action.callback = lcd_usecond_match_event;
		action.context = (void*)cfg;
		hwpl_tmr_setaction(LCD_USECOND_TMR, &action);

		const char start[] = {0xA0, 0xAE, 0xC0, 0xA2, 0x2F, 0x21, 0x81, 0x2F};
		int i;
		const char * p = (const char*)start;

		command_mode();
		hwpl_pio_clrmask(LCD_SPI_A0_PORT, (void*)LCD_SPI_A0_PINMASK); //enter command mode

		for(i=0; i < 8; i++){
			assert_cs();
			hwpl_spi_swap(LCD_SPI_PORT, (void*)(uint32_t)(*p++));
			deassert_cs();
		}

		//start the timer update to refresh the screen
		update_count();
		break;

	case I_MLCD_ON:
		command_mode();
		assert_cs();
		hwpl_spi_swap(LCD_SPI_PORT, (void*)0xAF);
		deassert_cs();
		break;

	case I_MLCD_OFF:
		command_mode();
		assert_cs();
		hwpl_spi_swap(LCD_SPI_PORT, (void*)0xAE);
		deassert_cs();
		break;

	default:
		errno = EINVAL;
		return -1;
	}


	return 0;
}