#include "sample.h"
#include "stdarg.h"
#include "stdlib.h"
#include "DSO.h"
#include "unistd.h"
#include "system.h"

const int speed_table[25] = {
		1, //1S
		2, //500ms
		5, //200ms
		10, //100ms
		20, //50ms
		50, //20ms
		100, //10ms
		200, //5ms
		500, //2ms
		1000, //1ms
		2000, //500us
		5000, //200us
		10000, //100us
		20000, //50us
		50000, //20us
		100000, //10us
		200000, //5us
		500000, //2us
		1000000, //1us
		2000000, //500ns
		5000000, //200ns
		10000000, //100ns
		20000000, //50ns
		50000000, //20ns
		100000000 //10ns
		};

void set_sample_speed(int speed) {
	int speed_word;

	if (speed == 100000000) {
		speed_word = 0;
	} else {
		speed_word = 100000000 / speed - 1;
	}

	IOWR(PIO_SFREQ_BASE, 0, speed_word); //òΪ50M
}

void sample_init(void) {
	set_sample_speed(1 * 1000); //òΪ500K
	IOWR(PIO_TRIG_SET_BASE, 0, 100 | 0x8000); //ôģʽΪͨBѹΪ0V

	IOWR_MASTER_WR_CTRL_ADDR(MASTER_WR_CTRL_BASE, DATA_MEM_BASE);
	//ָݴ洢λΪƬRAM
	IOWR_MASTER_WR_CTRL_LENGTH(MASTER_WR_CTRL_BASE, 1024*2);
	//ָΪ2048ֽ
}

void start_sample() {
	//תʹź10һ壬ת
	IOWR(PIO_EN_BASE, 0, 1);
	IOWR(PIO_EN_BASE, 0, 0);
}

int get_sample_state() {
	//ȡ״̬Ĵ
	return IORD(PIO_STATE_BASE, 0);
}
