interrupt(PORT1_VECTOR) PORT1_ISR(void) { P1IFG &= ~BIT0; // Clear interrupt flag #else void exti2_isr(void) { exti_reset_request(EXTI2); // Clear interrupt flag #endif int s; // set ACK payload to next squence number ptx.data[0]++; s = nrf_write_ack_pl(&ptx, 0); if(s == NRF_ERR_TX_FULL) { cio_printf("Unable to send back ACK payload (TX_FULL)\n\r"); } // receive non-blocking since IRQ already indicates that payload arived s = nrf_receive(&prx); if(s == NRF_ERR_RX_FULL) { cio_printf("Unable to receive any more (RX_FULL)\n\r"); } else { cio_printf("Received payload: %c; sending back %u\n\r", prx.data[0], ptx.data[0]); } }
int shell_cmd_laser(shell_cmd_args *args) { //XOR current state of laser output pin //when low acts as ground to complete circuit //If 2.3 DIR is 0 then laser is on P2DIR ^= BIT3; //toggle if(~(P2DIR & BIT3)){ cio_printf("Laser disabled.\n\r"); } else{ cio_printf("Laser enabled.\n\r"); } __delay_cycles(50); return 0; }
int main(void) { clock_init(); serial_init(9600); nrf_init(); cio_print("nRF2401 v0.1, TestClient\n\r"); nrf_configure_esb_rx(); nrf_dump_regs(&nrf_reg_def); static nrf_payload p; int s; // set payload size according to the payload size configured for the RX channel p.size = 1; while (1) { s = nrf_receive_blocking(&p); cio_printf("Received payload: %x; bytes received: %u\n\r", p.data[0], s); } return 0; }
void dump_regs(const char *msg) { cio_printf("REGDUMP(%s): P1DIR=%x; P2DIR=%x; P1OUT=%x; P2OUT=%x; P1REN=%x; P2REN=%x; P1IN=%x, P2IN=%x\n\r", msg, P1DIR, P2DIR, P1OUT, P2OUT, P1REN, P2REN, P1IN, P2IN); }
/******************************************* SHELL CALLBACK HANDLERS *******************************************/ int shell_cmd_help(shell_cmd_args *args) { int k; for(k = 0; k < my_shell_cmds.count; k++) { cio_printf("%s: %s\n\r", my_shell_cmds.cmds[k].cmd, my_shell_cmds.cmds[k].desc); } return 0; }
int shell_cmd_argt(shell_cmd_args *args) { int k; cio_print((char *)"args given:\n\r"); for(k = 0; k < args->count; k++) { cio_printf(" - %s\n\r", args->args[k].val); } return 0; }
void nrf_dump_regs(nrf_regs *r) { int i; int j; cio_print("\n\r** START nRF2401 Register DUMP **\n\r"); nrf_reg_buf buf; for(i = 0; i < r->count; i++) { nrf_read_reg(i, &buf); if(r->data[i].size == 0) continue; cio_printf("%s: ", r->data[i].name); for(j = 0; j < buf.size; j++) { cio_printb(buf.data[j], 8); cio_printf(" (%u) ", buf.data[j]); } cio_print("\n\r - "); for(j = 0; j < r->data[i].fields->count; j++) { cio_printf("%u[%u]:%s=%u ", j, r->data[i].fields->data[j].size, r->data[i].fields->data[j].name, nrf_get_reg_field(i, j, &buf)); } cio_print("-\n\r"); } cio_print("** END nRF2401 Register DUMP **\n\r"); }
int shell_cmd_ysweep(shell_cmd_args *args) { // Move forward toward the maximum position for (cntr = MIN_DUTYCYCLE; cntr < (MAX_DUTYCYCLE)/2; cntr++) { TA1CCR1 = cntr; __delay_cycles(2000); } // Move backward toward the manimum step value for (cntr = ((MAX_DUTYCYCLE)/2); cntr > MIN_DUTYCYCLE; cntr--) { TA1CCR1 = cntr; __delay_cycles(2000); } cio_printf("Vertical patrol complete.\n\r"); return 0; }
int shell_cmd_encr(shell_cmd_args *args) { if(args->count == 0) { cio_print((char *) "ERROR, no string to encrypt\n\r"); return -1; } if(strlen(args->args[0].val) > 20) { cio_print((char *) "ERROR, string to encrypt is too long\n\r"); return -1; } int k; char string[21]; strncpy(string, args->args[0].val, 20); for(k = 0; k < strlen(string); k++) { string[k] = string[k] ^ key[k]; } cio_printf("encrypted string: %s\n\r", string); return 0; }
int pin_setup(unsigned char pin, unsigned char function) { int port; int bit; if((port = pin2port(pin)) < 0) return port; if((bit = pin2bit(pin)) < 0) return bit; unsigned char f; f = pin_function(pin); #ifdef PIN_DBG cio_printf("pin %x has current function %x\n\r", pin, f); #endif // see if PIN is already configured for the given function if(f == function) { return PIN_STAT_OK; } // PIN is reserved else if(f == PIN_FUNCTION_RESERVED) { return PIN_STAT_ERR_UNSUPFUNC; } switch(function) { case PIN_FUNCTION_INPUT_FLOAT: if(!pin_has_capabilities(pin, PIN_CAP_INPUT)) { return PIN_STAT_ERR_UNSUPFUNC; } if(port == 1) { P1DIR &= ~bit; // make sure to clear OUT flag for the pin P1REN &= ~bit; // disable pull-up/down P1SEL &= ~bit; // remove option } else if(port == 2) { P2DIR &= ~bit; // make sure to clear OUT flag for the pin P2REN &= ~bit; // disable pull-up/down P2SEL &= ~bit; // remove option } break; case PIN_FUNCTION_INPUT_PULLUP: if(!pin_has_capabilities(pin, PIN_CAP_INPUT_RE)) { return PIN_STAT_ERR_UNSUPFUNC; } if(port == 1) { P1DIR &= ~bit; // make sure to clear OUT flag for the pin P1OUT |= bit; // setting out to HIGH enables pull-up P1REN |= bit; // enable pull-up/down P1SEL &= ~bit; // remove option } else if(port == 2) { P2DIR &= ~bit; // make sure to clear OUT flag for the pin P2OUT |= bit; // setting out to HIGH enables pull-up P2REN |= bit; // enable pull-up/down P2SEL &= ~bit; // remove option } break; case PIN_FUNCTION_INPUT_PULLDOWN: if(!pin_has_capabilities(pin, PIN_CAP_INPUT_RE)) { return PIN_STAT_ERR_UNSUPFUNC; } if(port == 1) { P1DIR &= ~bit; // make sure to clear OUT flag for the pin P1OUT &= ~bit; // setting out to LOW enables pull-down P1REN |= bit; // enable pull-up/down P1SEL &= ~bit; // remove option } else if(port == 2) { P2DIR &= ~bit; // make sure to clear OUT flag for the pin P2OUT &= ~bit; // setting out to LOW enables pull-down P2REN |= bit; // enable pull-up/down P2SEL &= ~bit; // remove option } break; case PIN_FUNCTION_OUTPUT: if(!pin_has_capabilities(pin, PIN_CAP_OUTPUT)) { return PIN_STAT_ERR_UNSUPFUNC; } if(port == 1) { P1DIR |= bit; // set direction to out P1OUT &= ~bit; // set to LOW initially P1REN &= ~bit; // disable pull-up/down P1SEL &= ~bit; // remove option } else if(port == 2) { P2DIR |= bit; // set direction to out P2OUT &= ~bit; // set to LOW initially P2REN &= ~bit; // disable pull-up/down P2SEL &= ~bit; // remove option } break; case PIN_FUNCTION_ANALOG_IN: if(!pin_has_capabilities(pin, PIN_CAP_ANALOG_IN)) { return PIN_STAT_ERR_UNSUPFUNC; } P1DIR &= ~bit; // make sure to clear OUT flag for the pin P1REN &= ~bit; // disable pull-up/down P1SEL &= ~bit; // remove option // VCC as +VRef, VSS as -VRef, 16 x ADC10CLKs ADC10CTL0 = SREF_0 + ADC10SHT_2 + REFON + ADC10ON; break; case PIN_FUNCTION_PWM: if(!pin_has_capabilities(pin, PIN_CAP_PWM)) { return PIN_STAT_ERR_UNSUPFUNC; } if(port == 1) { // only one pin on port 1 is able to perform PWM unsigned char pf = pin_with_function(PIN_1_0, function); if(pf > 0 && pf < PIN_2_0) { return PIN_STAT_ERR_UNSUPFUNC; } P1DIR |= bit; // set direction to out P1OUT &= ~bit; // set to LOW initially P1REN &= ~bit; // disable pull-up/down P1SEL |= bit; // select TA option } else if(port == 2) { // only one pin on port 2 is able to perform PWM if(pin_with_function(PIN_2_0, function)) { return PIN_STAT_ERR_UNSUPFUNC; } P2DIR |= bit; // set direction to out P2OUT &= ~bit; // set to LOW initially P2REN &= ~bit; // disable pull-up/down P2SEL |= bit; // select TA option } break; default: return PIN_STAT_ERR_UNSUPFUNC; } pin_set_curr_func(pin, function); return PIN_STAT_OK; }
int main(void) { /* Initialize Leds mounted on STM32F0-discovery */ STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Turn on LED3 and LED4 */ STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); /* Initialize other outputs and inputs */ STM_EVAL_LEDInit(RELAIS1); STM_EVAL_LEDInit(RELAIS2); STM_EVAL_LEDInit(OUT_12V_1); STM_EVAL_LEDInit(OUT_12V_2); STM_EVAL_PBInit(IN_12V_1, BUTTON_MODE_PDOWN); STM_EVAL_PBInit(IN_12V_2, BUTTON_MODE_PDOWN); /* Initialize the User_Button */ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); /* Setup SysTick Timer for 1 msec interrupts. */ if (SysTick_Config(SystemCoreClock / 1000)) { /* Capture error */ while (1); } usart_1_init(); #ifndef WIFI_CONNECTED /* Output a message on Hyperterminal using printf function */ cio_printf("%s", welcome_msg); #else wifi_intit_pins(); wifi_init_connections(); // If the button is pressed on power on, // do not init the WiFi module to keep current WiFi connections // and save development time if (!STM_EVAL_PBGetState(BUTTON_USER)) wifi_init_ap_mode(); #endif while (1) { // Everything should be non-blocking #ifndef WIFI_CONNECTED if (serve_command_promt(line_buffer, LINEBUFFERSIZE, "microcli> ") > 0) { shell_process(line_buffer); } #else if (wifi_get_msg(line_buffer, LINEBUFFERSIZE, &cid) > 0) { bool add_header = (cid != -1); // send back to the current connection if (add_header) { cio_printf("\x1bS%c", cid); cio_printf("microcli> %s\r\n", line_buffer); } shell_process(line_buffer); if (add_header) { cio_print("[end]\r\n"); cio_print("\x1b" "E"); } } #endif if (timeout_0 == 0) { static uint8_t counter = 0; switch (counter++) { case 0: /* Toggle LED4 */ if (options.elements[TOGGLE_LEDS_IDX].value == 1) STM_EVAL_LEDToggle(LED4); break; case 1: /* Toggle LED3 */ if (options.elements[TOGGLE_LEDS_IDX].value == 1) STM_EVAL_LEDToggle(LED3); break; case 2: break; default: counter = 0; } timeout_0 = TIMEOUT_0_DEFAULT; } if (options.elements[REALTIME_MSG_IDX].value == 1) { enum {WAITFOR_PRESSED, WAITFOR_RELEASED}; static uint8_t what = WAITFOR_PRESSED; char msg[30]; msg[0] = '\0'; if (what == WAITFOR_PRESSED) { if (UserButtonPressed != 0x00) // Interrupt based, don't miss an event //if (STM_EVAL_PBGetState(BUTTON_USER) == 1) { strcpy(msg, "User button pressed"); what = WAITFOR_RELEASED; } } else if (what == WAITFOR_RELEASED) { if (STM_EVAL_PBGetState(BUTTON_USER) == 0) { UserButtonPressed = 0x00; strcpy(msg, "User button released"); what = WAITFOR_PRESSED; } } if (msg[0] != '\0') { #ifndef WIFI_CONNECTED cio_printf("\r\n[info] %s %i\r\n", msg, uptime); #else // send message to each client for(int i=0; i<16; i++) { if(wifi_connections[i].state == CONNECTED) { cio_printf("\x1bS%c[info] %s %i\r\n\x1b""E", wifi_connections[i].cid, msg, uptime); } } #endif } } } }
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; }
int main(void){ int count = 0; short int retval; short int c,v,save; WDTCTL = WDTHOLD | WDTPW; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; // turn the LEDs off P1DIR &= ~(0b00000001); P1DIR &= ~(0b01000000); serial_init(9600); // The check is done in a loop for (;;) { // get status from assembly file with Status function retval = Status(); // turn the LEDs off P1DIR &= ~(0b00000001); P1DIR &= ~(0b01000000); // increment the value by one count = count + 1; save = count; // check if c or v are on if (retval & 1 == 1) { c = 1; // red light on P1DIR |= 0b00000001; // delay to see the lights __delay_cycles(100000); } else if (retval & 0x0100 == 1) { v = 1; // green light on P1DIR |= 0b01000000; // delay to see the lights __delay_cycles(100000); } // print the values of carry before the flag if (c == 1) { cio_printf("Value before carry: %u\n\r", save); } // print the values of overflow before the flag if (v == 1) { cio_printf("Value before overflow: %u\n\r", save); } } return 0; }