Skip to content
Snippets Groups Projects
main.c 4.05 KiB
/*
 * main.c
 *
 * Created: 29/09/2012 2:13:52 AM
 *  Author: mdryden
 */ 

#include "main.h"

//Internal global variables
int16_t p1, p2, p3, p4;
uint16_t u1, u2, u3, u4;
uint8_t p5, o1, o2, o3;

//Internal function declarations
int8_t command_handler(char command);

int8_t command_handler(char command){
	/**
	 * Deals with commands over USB
	 *
	 * Calls functions in 
	 * @param command Command character input.
	 */
	
	int16_t pcv1, pcv2;
	uint16_t pct1, pct2;
	
	switch (command){
		case 'A': //ADS Buffer/rate/PGA values from ads1255.h
			scanf("%hhx%hhx%hhx",&o1,&o2,&o3);
			printf("#A: %x %x %x\r\n",o1,o2,o3);
			ads1255_setup(o1, o2, o3);
			break;
			
		case 'G': //Gain - start gain, autogain on/off, (min gain, max gain), low/high switch thresholds
			//scanf("%1u%1u%7li%7li",&g_gain,&autogain_enable,&overcurrent_threshold,&undercurrent_threshold);
			scanf("%u",&g_gain);
			printf("#G: %u\r\n", g_gain);
			pot_set_gain(); //uses global g_gain, so no params
			break;
			
		case 'L': //LSV - start, stop, slope
			scanf("%u%u%i%i%i%i%u",&pct1,&pct2,&pcv1,&pcv2,&p1,&p2,&u1);
			precond(pcv1,pct1,pcv2,pct2);
			lsv_experiment(p1,p2,u1,2);
			break;
			
		case 'C': //CV - v1, v2, start, scans, slope
			scanf("%u%u%i%i%i%i%i%hhu%u",&pct1,&pct2,&pcv1,&pcv2,&p1,&p2,&p3,&p5,&u1);
			precond(pcv1,pct1,pcv2,pct2);
			cv_experiment(p1,p2,p3,p5,u1);
			break;
			
		case 'S': //SWV - start, stop, step size, pulse_height, frequency, scans
			scanf("%u%u%i%i%i%i%u%u%u%u",&pct1,&pct2,&pcv1,&pcv2,&p1,&p2,&u1,&u2,&u3,&u4);
			precond(pcv1,pct1,pcv2,pct2);
			swv_experiment(p1,p2,u1,u2,u3,u4);
			break;
			
		case 'D': //DPV - start, stop, step size, pulse_height, period, width
			scanf("%u%u%i%i%i%i%u%u%u%u",&pct1,&pct2,&pcv1,&pcv2,&p1,&p2,&u1,&u2,&u3,&u4);
			precond(pcv1,pct1,pcv2,pct2);
			dpv_experiment(p1,p2,u1,u2,u3,u4);
			break;
			
		#if BOARD_VER_MAJOR == 1 && BOARD_VER_MINOR >= 2
		case 'P': //potentiometry - time, OCP/poteniometry
			scanf("%u%hhu",&pct1, &o1);
			pot_experiment(pct1, o1);
			break;
		#endif
			
		case 'R': //CA - steps, step_dac[], step_seconds[]
			scanf("%hhu",&p5); //get number of steps
			printf("#Steps: %u\n\r", p5);
			
			//allocate arrays for steps
			uint16_t * step_dac = malloc(p5*sizeof(uint16_t));
			uint16_t * step_seconds = malloc(p5*sizeof(uint16_t));
			
			//check for successful allocation
			if (!step_dac || !step_seconds){ 
				printf("#ERR: Could not allocate memory\n\r");
				break;
			}
			
			uint8_t i;
			
			for (i=0; i<p5; i++){
				scanf("%u", &step_dac[i]); // get voltage steps (dac units)
				printf("#DAC: %u\n\r", step_dac[i]);
			}
			
			for (i=0; i<p5; i++){
				scanf("%u", &step_seconds[i]); //get step durations (seconds)
				printf("#Time: %u\n\r", step_seconds[i]);
			}

			ca_experiment(p5, step_dac, step_seconds);
			
			//free arrays
			free(step_dac);
			free(step_seconds);
			
			break;
		
		case 'V': //check version
			printf("V%u.%u\n", BOARD_VER_MAJOR, BOARD_VER_MINOR);
			break;
		
		default:
			printf("#ERR: Command %c not recognized\n\r", command);
			return 1;
	}
	printf("no\n\r");
	return 0;
}

int main(void){
	
	board_init();
	pot_init();
	pmic_init();
	
	irq_initialize_vectors();
	cpu_irq_enable();
	sleepmgr_init();
	sysclk_init(); //Disables ALL peripheral clocks D:
	rtc_init();
	sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);
	
	pmic_set_scheduling(PMIC_SCH_ROUND_ROBIN);
	
	delay_ms(500);
	
	stdio_usb_init();
	stdio_usb_enable();
	
	ads1255_init_pins();
	ads1255_init_module();

	PORTD.INT0MASK = PIN5_bm;
	PORTD.INT1MASK = PIN5_bm;
	PORTD.INTCTRL = PORT_INT0LVL_OFF_gc | PORT_INT1LVL_OFF_gc;
	
	max5443_init_pins();
	max5443_init_module();
	
	ads1255_wakeup();
	ads1255_rdatac();
	ads1255_standby();
	
	//Wait for application connection - Get 'c', reply 'K', get 'k'
	while(1){
		while(getchar() != 'c');
		putchar('#');
		if (getchar() == 'k')
			break;
		printf("\n\r");
	}	
	
	ads1255_setup(ADS_BUFF_ON,ADS_DR_60,ADS_PGA_2);
	
	autogain_enable = 0;
	g_gain = POT_GAIN_30k;
	pot_set_gain();
	
	program_loop:
		while(getchar() != '!');
		printf ("C\r\n");
		command_handler(getchar());
	goto program_loop;
}