Example #1
0
void flash(void) {
  unsigned int i;
  pin_toggle(LED2);
  for (i=0; i<20; i++) { delay(DELAY); }
  pin_toggle(LED2);
  for (i=0; i<20; i++) { delay(DELAY); }
}
Example #2
0
int
main(void)
{
	watchdog_off();
	clock_init_1MHz();

	/* set all pins to output high */
	port1_direction = 0xFF;
	port1_output = 0xFF;

	/* initialize serial clock, tx and rx parts */
	serial_init_clock();
	serial_init_tx();
	serial_init_rx();

	/* enable interrupts */
	__eint();

	pin_low(LED1);
	pin_high(LED2);

	while (1) {
		unsigned char c;

		pin_toggle(LED1);
		pin_toggle(LED2);

		c = serial_getchar();
		serial_putchar(c);
		if (c == '\r')
			serial_putchar('\n');
	}
}
Example #3
0
/* SysTick IRQ handler */
void SysTick_Handler(void) {
    g_msTicks++;

    // Boot LED Tasks
    if (!booted && g_msTicks > led_next_time) {
        led_next_time += 400;
        pin_toggle(PORT_A.power);
        pin_toggle(PORT_B.power);
        pin_toggle(PIN_LED);
    }
}
Example #4
0
/**
 * The main routine.
 */
int main(void)
{
	unsigned char cnt = 0;
	unsigned char c; 

	clock_init();

	pin_reserve(PIN_1_1);
	pin_reserve(PIN_1_2);

	serial_init(BAUDRATE);

	setup();
	
	for(;;) {

		c = serial_recv_blocking();

		if(c == SCRATCH_DATA_REQUEST) {	
			
			if(cnt++ == COMM_BLINK_RATE) {
				// blink COMM to show data was requested
				pin_toggle(COMM);
				cnt = 0;
			}

			read_samples();
			xmit_samples();
		}
	}

	return 0;
}
Example #5
0
int main() {
    pin_out(&led);
    while(1) {
        _delay_us(10);
        pin_toggle(&led);
    }
}
Example #6
0
static err_t low_level_output(struct netif *netif, struct pbuf *p)
{
    struct pbuf *q = NULL;
    int l = 0;

#if ETH_PAD_SIZE
    pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
#endif
    pin_toggle();

    if (g_bTimerStopped) {
        csi_kernel_timer_stop(timer_send_handle);
        csi_kernel_timer_start(timer_send_handle, csi_kernel_ms2tick(10000));
        g_bTimerStopped = 0;
    }

    csi_eth_mac_ex_send_frame_begin(eth_mac_handle, p->tot_len);

    for (q = p; q != NULL; q = q->next) {
        csi_eth_mac_ex_send_frame(eth_mac_handle, q->payload, q->len, 0);
        l = l + q->len;
    }

    csi_eth_mac_ex_send_frame_end(eth_mac_handle);
    pin_toggle();

    MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);

    if (((u8_t *)p->payload)[0] & 1) {
        /* broadcast or multicast packet */
        MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
    } else {
        /* unicast packet */
        MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
    }

    /* increase ifoutdiscards or ifouterrors on error */

#if ETH_PAD_SIZE
    pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif

    LINK_STATS_INC(link.xmit);

    return ERR_OK;
}
Example #7
0
/**
 int line:
 -----------            ----------------------
            |___________|   |_________________
            1           2 3 4
 1. a nic event arised, enc28j60 pulls int line down
    run ISR to tell tcpip thread to handle the event
 2. switch to tcpip thread, clear INTIE, save and clear all pending event
 3. handle the event one by one
 4. set INTIE, if new event is valid while handling event, enc28j60 pulls line down
    otherwise, keep the line high as usually

 If a falling edge of GPIO is missing, the line is always kept low,
 no gpio interrupt will be triggered any more.
 As consider of reliability, trigger interrupt by low level
 */
static void eth_int_handle(int32_t idx)
{
    pin_toggle();

    eth_gpio_config_int(0);

    tcpip_signal_netif_event(&lwip_netif, NIC_EVENT_INT, eth_handle_event);
}
Example #8
0
/**
 * def toggle(self)
 */
static mp_obj_t class_pin_toggle(mp_obj_t self_in)
{
    struct class_pin_t *self_p;

    self_p = MP_OBJ_TO_PTR(self_in);
    pin_toggle(&self_p->drv);

    return (mp_const_none);
}
Example #9
0
int16_t main(void) {
    init_clock();
    init_uart();
    init_pin();
    pin_digitalOut(&D[0]);

   while (1) {
        pin_toggle(&D[0]);
    }
}
Example #10
0
int main(void)
{
	init_led();
	init_usart();
	init_led();
	
	while (true) {
		eth_poll();		/* out of scope of this example */
		
		pin_toggle(LED);
	}
}
Example #11
0
int main(void) {
    setup();
    pwm_set_duty(0);
    pwm_set_direction(0);
    
    printf("%s\r\n", "STARTING LOOP");
    float encoder_master_count = 0;
    uint16_t current_ticks = 0;
    uint16_t previous_ticks = spi_read_ticks();
    float target_degs = 10;
    float motor_current = 0;  // current through motor in amperes
    float pid_command = 0;
    float theor_torque;
    bitset(&IEC0, 2);
    bitset(&IEC0, 6);
    
    while (1) {
        if (timer_flag(&timer2)) {
            // Blink green light to show normal operation.
            timer_lower(&timer2);
            led_toggle(&led2);
        }

        if (timer_flag(&timer3)) {
            timer_lower(&timer3);
            led_toggle(&led3);
            theor_torque = spring_model(degs);  // Outputs theoretical torque predicted by spring model
            cur_control.set_point = fabsf(theor_torque);
            cur_control.neg_set_point = read_sign(theor_torque);
            read_motor_current(&motor);
            cur_control.position = convert_motor_torque(motor.current);
            pid_command = PID_control(&cur_control);
            pwm_duty = pid_to_pwm(pid_command, cur_control.neg_set_point);
        }

        if (!sw_read(&sw2)) {
            // If switch 2 is pressed, the UART output terminal is cleared.
            printf("%s", clear);
        }
        
        ServiceUSB();
        current_ticks = spi_read_ticks();
        encoder_master_count = encoder_counter(current_ticks, previous_ticks, encoder_master_count);
        degs = count_to_deg(encoder_master_count);
        previous_ticks = current_ticks;
        pin_toggle(DEBUGD1);  // Heartbeat signal
        
    }
}
Example #12
0
// only tested with modes 0 and 2 (cpha = 0)
static uint8_t __spiSWSendByte(SPI_ABSTRACT_BUS* _spi, uint8_t data){
	SPI_SW* spi = (SPI_SW*)_spi;
	uint8_t rtn=0;
	uint8_t delay = _spi->clock / 3;	// delay loop is approx 3 cycles
	uint8_t cpha = _spi->mode & 1;

	for(int i=0; i<8; i++){						/* for each bit			*/
		if(!cpha){ // Mode 0 or 2
			data = __spiWriteMOSI(spi,data);		/* Set the output pin 	*/
			rtn  = __spiReadMISO(spi,rtn);			/* read input for modes 0 and 2	*/
			pin_toggle(spi->SCLK);					/* toggle clock (high) 	*/
			if(delay) _delay_loop_1(delay);			/* delay			  	*/
			pin_toggle(spi->SCLK);					/* toggle clock (low)	*/
		}else{		// Mode 1 or 3
			pin_toggle(spi->SCLK);					/* toggle clock (high) 	*/
			data = __spiWriteMOSI(spi,data);		/* Set the output pin 	*/
			if(delay) _delay_loop_1(delay);			/* delay			  	*/
			pin_toggle(spi->SCLK);					/* toggle clock (low)	*/
			rtn  = __spiReadMISO(spi,rtn);			/* read input for modes 1 and 3	*/
		}
	}											/* next bit				*/
	pin_high(spi->MOSI);						/* make MOSI high		*/
	return rtn;									/* return read byte		*/
}
Example #13
0
void main()
{
	usart_init(BAUD_115200);
	usart_isr_rx_enable(true); // enable RX interrupt handler

	// configure pins
	as_output(LED);

	// globally enable interrupts (for the USART_RX handler)
	sei();

	while (1) {
		usart_puts("Hello World!\r\n");

		pin_toggle(13); // blink the LED

		_delay_ms(500);
	}
}
Example #14
0
int main()
{
    struct pin_driver_t led;

    /* Start the system. */
    sys_start();

    /* Initialize the LED pin as output and set its value to 1. */
    pin_init(&led, &pin_led_dev, PIN_OUTPUT);
    pin_write(&led, 1);

    while (1) {
        /* Wait half a second. */
        thrd_sleep_us(500000);

        /* Toggle the LED on/off. */
        pin_toggle(&led);
    }

    return (0);
}
Example #15
0
int handle_packet_pin_control(unsigned char length, unsigned char *data)
{
	int s = PACKET_STAT_OK;

	// check if length matches for packet-data
	if(length != 2) {
		send_status_packet(PACKET_RETURN_INAVLID_DATA);
		return PACKET_STAT_ERR_DATA;
	}

	packet_data_in_pin_control *pd = (packet_data_in_pin_control *)&data[0];

	switch(pd->control) {
		case PIN_CONTROL_CLEAR:
			if((s = pin_clear(pd->pin)) != PACKET_STAT_OK) {
				send_status_packet(PACKET_RETURN_INVALID_PIN_COMMAND);
			}
			else {
				send_status_packet(PACKET_RETURN_ACK);
			}
			break;
		case PIN_CONTROL_SET:
			if((s = pin_set(pd->pin)) != PACKET_STAT_OK) {
				send_status_packet(PACKET_RETURN_INVALID_PIN_COMMAND);
			}
			else {
				send_status_packet(PACKET_RETURN_ACK);
			}
			break;
		case PIN_CONTROL_TOGGLE:
			if((s = pin_toggle(pd->pin)) != PACKET_STAT_OK) {
				send_status_packet(PACKET_RETURN_INVALID_PIN_COMMAND);
			}
			else {
				send_status_packet(PACKET_RETURN_ACK);
			}
			break;
		case PIN_CONTROL_DIGITAL_READ:
			if((s = pin_digital_read(pd->pin)) < 0) {
				send_status_packet(PACKET_RETURN_INVALID_PIN_COMMAND);
			}
			else {
				packet_data_out_digital_pin_read *pdo = (packet_data_out_digital_pin_read *)&outp.data[0];

				outp.start	= PACKET_OUTBOUND_START;
				outp.length	= 6;
				outp.type 	= PACKET_OUT_DIGITAL_PIN_READ;
	
				pdo->pin   = pd->pin;
				pdo->state = s;

				outp.crc = packet_calc_crc(&outp);

				packet_send(&outp);
			}
			break;
		case PIN_CONTROL_ANALOG_READ:
			if((s = pin_analog_read(pd->pin)) < 0) {
				send_status_packet(PACKET_RETURN_INVALID_PIN_COMMAND);
			}
			else {
				packet_data_out_analog_pin_read *pdo = (packet_data_out_analog_pin_read *)&outp.data[0];

				outp.start	= PACKET_OUTBOUND_START;
				outp.length	= 7;
				outp.type 	= PACKET_OUT_ANALOG_PIN_READ;
	
				pdo->pin       = pd->pin;
				pdo->value_lsb = (0x00FF & s);
				pdo->value_msb = (0x0F00 & s) >> 8;

				outp.crc = packet_calc_crc(&outp);

				packet_send(&outp);
			}
			break;
		case PIN_CONTROL_PULSELENGTH_READ:
			if((s = pin_pulselength_read(pd->pin)) < 0) {
				send_status_packet(PACKET_RETURN_INVALID_PIN_COMMAND);
			}
			else {
				packet_data_out_pulselength_read *pdo = 
							( packet_data_out_pulselength_read *)&outp.data[0];

				outp.start	= PACKET_OUTBOUND_START;
				outp.length	= 7;
				outp.type 	= PACKET_OUT_PULSELENGHT_READ;
	
				pdo->pin   = pd->pin;
				pdo->value_lsb = (0x00FF & s);
				pdo->value_msb = (0x7F00 & s) >> 8;

				outp.crc = packet_calc_crc(&outp);

				packet_send(&outp);
			}
			break;
		default:
			send_status_packet(PACKET_RETURN_INVALID_PIN_COMMAND);
			s = PACKET_STAT_ERR_DATA;
	}

	return s;
}
Example #16
0
static err_t eth_handle_event(struct netif *netif, uint32_t event)
{
    int link_status;
    uint32_t eth_event;

    pin_toggle();

    if (event == NIC_EVENT_API_RESET) {
        LOGI(TAG, "reset ethernet card.");
        /* restart init */
        eth_hard_reset();
        eth_mac_handle = csi_eth_mac_initialize(0, NULL);
        /* enable irq */
        csi_eth_mac_control(eth_mac_handle, CSI_ETH_MAC_CONTROL_RX, 1);
        eth_gpio_config_int(1);
        g_bTimerStopped = 1;
    } else {
        eth_event = eth_get_event(0);

        if (eth_event) {
            csi_kernel_timer_stop(timer_send_handle);
            g_bTimerStopped = 1;
            g_continue_send_error = 0;
        } else {
            //printf("invalid interrupt\n");
        }

        if (eth_event & 0x80) {
            LOGI(TAG, "reset ethernet card.");
            /* restart init */
            eth_hard_reset();
            eth_mac_handle = csi_eth_mac_initialize(0, NULL);
            /* enable irq */
        } else {
            if (eth_event & CSI_ETH_MAC_EVENT_LINK_CHANGE) {
                link_status = csi_eth_phy_get_linkstate(eth_phy_handle);

                if ((link_status != -1)) {
                    nic_link_stat = link_status;
                    LOGI(TAG, "Net:link %s", (link_status == 1) ? "up" : "down");

                    if (link_status == 1) {
                        //aos_post_event(EV_NET, CODE_NET_ON_CONNECTED, VALUE_NET_ETH);
                    } else {
                        //aos_post_event(EV_NET, CODE_NET_ON_DISCONNECT, VALUE_NET_ETH);
                    }
                }
            }

            if (eth_event == CSI_ETH_MAC_EVENT_RX_FRAME) {

                ethernetif_input(netif);

                /*if there are packets in rx buffer, interrupt will be triggered again*/
            }

            if (eth_event == CSI_ETH_MAC_EVENT_TX_FRAME) {

            }
            pin_toggle();
        }
        //Enable interrupt
        csi_eth_mac_control(eth_mac_handle, CSI_ETH_MAC_CONTROL_RX, 1);
        eth_gpio_config_int(1);
    }
    return 0;
}
Example #17
0
int main(void)
{
	clock_init();

	pin_reserve(PIN_1_1);
	pin_reserve(PIN_1_2);

	serial_init(9600);

	cio_print("** ROCKETuC - librocketcore PIN test **\n\r");

	dump_regs("initial");

	// invalid port
	if(pin_setup(0x30, PIN_FUNCTION_OUTPUT) == PIN_STAT_ERR_INVALPORT) {
		cio_print("0x30 is an invalid port\n\r");
	}
	
	// invalid pin
	if(pin_setup(0x2A, PIN_FUNCTION_OUTPUT) == PIN_STAT_ERR_INVALPIN) {
		cio_print("0x2A is an invalid pin\n\r");
	}

	// P1.1 + P1.2 are reserved for UART1
	if(pin_setup(PIN_1_1, PIN_FUNCTION_OUTPUT) == PIN_STAT_ERR_INVALPIN) {
		cio_print("0x11 is an invalid (reserved) pin\n\r");
	}
	if(pin_setup(PIN_1_2, PIN_FUNCTION_OUTPUT) == PIN_STAT_ERR_INVALPIN) {
		cio_print("0x12 is an invalid (reserved) pin\n\r");
	}

	// pins on port 2 do not support ADC
	int p;

	for(p = 0; p < 8; p++) { 
		if(pin_setup(PIN_2_0 + p, PIN_FUNCTION_ANALOG_IN) == PIN_STAT_ERR_UNSUPFUNC) {
			cio_printf("0x2%i does not support ADC\n\r", p);
		}
	}
	
	// set P1.0 + P1.6 + P2.5 to output (the build in LEDs)
	pin_setup(PIN_1_0, PIN_FUNCTION_OUTPUT);
	pin_setup(PIN_1_6, PIN_FUNCTION_OUTPUT);
	pin_setup(PIN_2_5, PIN_FUNCTION_OUTPUT);

	
	dump_regs("p1.0+p1.6+p2.5 output");

	// set P1.0 + P1.6 + P2.5 to HIGH
	pin_set(PIN_1_0);
	pin_set(PIN_1_6);
	pin_set(PIN_2_5);

	dump_regs("p1.0+p1.6+p2.5 set");

	// read P1.0 + P1.6 + p2.5 states
	cio_printf("P1.0 is %x\n\r", pin_digital_read(PIN_1_0));	
	cio_printf("P1.6 is %x\n\r", pin_digital_read(PIN_1_6));	
	cio_printf("P2.5 is %x\n\r", pin_digital_read(PIN_2_5));	
	
	// clear P1.0 + p1.6 + p2.5 to LOW 
	pin_clear(PIN_1_0);
	pin_clear(PIN_1_6);
	pin_clear(PIN_2_5);

	dump_regs("p1.0+p1.6+p2.5 clear");

	// read P1.0 + P1.6 + 2.5 states
	cio_printf("P1.0 is %x\n\r", pin_digital_read(PIN_1_0));	
	cio_printf("P1.6 is %x\n\r", pin_digital_read(PIN_1_6));	
	cio_printf("P2.5 is %x\n\r", pin_digital_read(PIN_2_5));	

	// toggle P1.0 + P1.6 + P2.5
	pin_toggle(PIN_1_0);
	pin_toggle(PIN_1_6);
	pin_toggle(PIN_2_5);

	dump_regs("p1.0+p1.6+p2.5 toggle");

	// read P1.0 + P1.6 states
	cio_printf("P1.0 is %x\n\r", pin_digital_read(PIN_1_0));	
	cio_printf("P1.6 is %x\n\r", pin_digital_read(PIN_1_6));	
	cio_printf("P2.5 is %x\n\r", pin_digital_read(PIN_2_5));	

	// toggle P1.0 + P1.6 + P2.5
	pin_toggle(PIN_1_0);
	pin_toggle(PIN_1_6);
	pin_toggle(PIN_2_5);

	dump_regs("p1.0+p1.6+p2.5 toggle");

	// read P1.0 + P1.6 states
	cio_printf("P1.0 is %x\n\r", pin_digital_read(PIN_1_0));	
	cio_printf("P1.6 is %x\n\r", pin_digital_read(PIN_1_6));	
	cio_printf("P2.5 is %x\n\r", pin_digital_read(PIN_2_5));	

	// set P1.3 to input float
	pin_setup(PIN_1_3, PIN_FUNCTION_INPUT_FLOAT);

	dump_regs("p1.3 input float");

	cio_print("Press button on P1.3 to continue ...");

	while(pin_digital_read(PIN_1_3)) __asm__("nop");

	cio_print(" OK\n\r");
	
	// set P2.3 to input pull-down 
	pin_setup(PIN_2_3, PIN_FUNCTION_INPUT_PULLDOWN);

	dump_regs("p2.3 input pull-down");

	cio_print("Press button on P2.3 to continue ...");

	while(!pin_digital_read(PIN_2_3)) __asm__("nop");

	cio_print(" OK\n\r");
	
	// set P2.4 to input pull-down 
	pin_setup(PIN_2_4, PIN_FUNCTION_INPUT_PULLUP);

	dump_regs("p2.4 input pull-up");

	cio_print("Press button on P2.4 to continue ...");

	while(pin_digital_read(PIN_2_4)) __asm__("nop");

	cio_print(" OK\n\r");

	int pl = 0;

	cio_print("Press button on P1.3 for pulselength read ...");

	delay(50000);

	pl = pin_pulselength_read(PIN_1_3);

	cio_printf(" OK, pl=%i\n\r", pl);

	cio_print("Press button on P2.3 for pulselength read ...");

	delay(50000);

	pl = pin_pulselength_read(PIN_2_3);

	cio_printf(" OK, pl=%i\n\r", pl);

	cio_print("Press button on P2.4 for pulselength read ...");

	delay(50000);

	pl = pin_pulselength_read(PIN_2_4);

	cio_printf(" OK, pl=%i\n\r", pl);
	
	pin_set(PIN_1_0);
	pin_clear(PIN_1_6);
	pin_clear(PIN_2_5);

	// set P1.5 to analog in
	int i = 0;

	cio_printf("setup 1.5 for analog in: %i\n\r", pin_setup(PIN_1_5, PIN_FUNCTION_ANALOG_IN));
	dump_regs("p1.5 analog in");
	
	int adcin1 = pin_analog_read(PIN_1_5); 
	int adcin2 = 0; 
	cio_printf("Analog read p1.5: %x\n\r", adcin1);

	// set P2.2 to PWM with period of 20ms and duty cycle of 7.5%
	cio_printf("setup 2.2 for PWM: %i\n\r", pin_setup(PIN_2_2, PIN_FUNCTION_PWM));
	dump_regs("p2.2 PWM");

	// only one of the two possible pins on port two are allowed to be set to PWM
	cio_printf("setup 2.1 for PWM: %i\n\r", pin_setup(PIN_2_1, PIN_FUNCTION_PWM));

	// period 
	pin_pwm_function(PIN_2_2, 20000);
	pin_pwm_control(PIN_2_2, adc2dc(adcin1));

	while (1) {
		delay();

		pin_toggle(PIN_1_0);
		pin_toggle(PIN_1_6);

		if(i++ % 2 == 0) {
			pin_toggle(PIN_2_5);
		}

		if(!pin_digital_read(PIN_1_3)) {
			pin_toggle(PIN_1_6);
			while(!pin_digital_read(PIN_1_3)) __asm__("nop");
		}

		adcin2 = pin_analog_read(PIN_1_5); 

		// only output ADC value if delta was more then 5
		if(adcin2 - adcin1 > 5 || adcin1 - adcin2 > 5) {
			adcin1 = adcin2;
			cio_printf("Analog read at p1.5: %x (%i)\n\r", adcin2, adcin2);
			pin_pwm_control(PIN_2_2, adc2dc(adcin1));
		}
	}

	return 0;
}