int main(void) { // Initialize Timers, ADC, and clear bootloader, Arduino does these with init() in wiring.c initTimers(); //Timer0 Fast PWM mode, Timer1 & Timer2 Phase Correct PWM mode. init_ADC_single_conversion(EXTERNAL_AVCC); // warning AREF should only have a bypass cap init_uart0_after_bootloader(); // bootloader may have the UART setup // setup() // Set digital pins to control load init_load(); // Set digital pins to control solar init_pv(); // put ADC in free running Auto Trigger mode enable_ADC_auto_conversion(FREE_RUNNING); /* Initialize UART, it returns a pointer to FILE so redirect of stdin and stdout works*/ stdout = stdin = uartstream0_init(BAUD); /* Clear and setup the command buffer, (probably not needed at this point) */ initCommandBuffer(); sei(); // Enable global interrupts starts TIMER0, UART0, ADC and any other ISR's // this start up command should run cctest, e.g. after a reset. if (uart0_available() == 0) { strcpy_P(command_buf, PSTR("/0/cctest?")); command_done = 1; echo_on = 1; printf_P(PSTR("%s\r\n"), command_buf); } // loop() while(1) /* I am tyring to use non-blocking code */ { // check if character is available to assemble a command, e.g. non-blocking if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h { // get a character from stdin and use it to assemble a command AssembleCommand(getchar()); // address is the ascii value for '0' note: a null address will terminate the command string. StartEchoWhenAddressed('0'); } // check if a character is available, and if so flush transmit buffer and nuke the command in process. // A multi-drop bus can have another device start transmitting after getting an address byte so // the first byte is used as a warning, it is the onlly chance to detect a possible collision. if ( command_done && uart0_available() ) { // dump the transmit buffer to limit a collision uart0_flush(); initCommandBuffer(); //Enable the LT3652, which may have been turned off digitalWrite(SHUTDOWN, LOW); // trun off the load load_step(0); } // finish echo of the command line befor starting a reply (or the next part of a reply) if ( command_done && (uart0_availableForWrite() == UART_TX0_BUFFER_SIZE) ) { if ( !echo_on ) { // this happons when the address did not match initCommandBuffer(); } else { if (command_done == 1) { findCommand(); command_done = 10; } // do not overfill the serial buffer since that blocks looping, e.g. process a command in 32 byte chunks if ( (command_done >= 10) && (command_done < 250) ) { ProcessCmd(); } else { initCommandBuffer(); } } } } return 0; }
/** Determine if characters are available * * Check the number of characters available for immediate reading. If this * function returns a non-zero value the next call to uartRecv() will be * guaranteed to return immediately with a value. This function is only valid * if you are using the interrupt driven version of the UART. * * @return the number of characters available in the input buffer. */ uint8_t uartAvail() { return uart0_available(); }
int main(void) { /* Initialize UART, it returns a pointer to FILE so redirect of stdin and stdout works*/ stdout = stdin = uartstream0_init(BAUD); initCommandBuffer(); sei(); // Enable global interrupts starts the UART // non-blocking code in loop while(1) { // check if character is available to assemble a command, e.g. non-blocking if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h { // get a character from stdin and use it to assemble a command AssembleCommand(getchar()); // address is the ascii value for '0' note: a null address will terminate the command string. StartEchoWhenAddressed('0'); } // check if the character is available, and if so stop transmit and the command in process. // a multi-drop bus can have another device start transmitting after the second received byte so // there is little time to detect a possible collision if ( command_done && uart0_available() ) { // dump the transmit buffer to limit a collision uart0_flush(); initCommandBuffer(); } // finish echo of the command line befor starting a reply (or the next part of reply), also non-blocking. if ( command_done && (uart0_availableForWrite() == UART_TX0_BUFFER_SIZE) ) { if ( !echo_on ) { // this happons when the address did not match initCommandBuffer(); } else { // command is a pointer to string and arg[] is an array of pointers to strings // use findCommand to make them point to the correct places in the command line // this can only be done once, since spaces and delimeters are replaced with null termination if (command_done == 1) { findCommand(); command_done = 2; } if (command_done == 2) { if (command != '\0' ) { printf_P(PSTR("{\"cmd\": \"%s\"}\r\n"),command); command_done = 3; } else { initCommandBuffer(); } } if (command_done == 3) { printf_P(PSTR("{\"arg_count\": \"%d\"}\r\n"),arg_count); if (arg_count > 0) { command_done = 4; } else { initCommandBuffer(); } } if (command_done == 4) { printf_P(PSTR("{\"arg[0]\": \"%s\"}\r\n"),arg[0]); if (arg_count > 1) { command_done = 5; } else { initCommandBuffer(); } } if (command_done == 5) { printf_P(PSTR("{\"arg[1]\": \"%s\"}\r\n"),arg[1]); if (arg_count > 2) { command_done = 6; } else { initCommandBuffer(); } } if (command_done == 6) { printf_P(PSTR("{\"arg[2]\": \"%s\"}\r\n"),arg[2]); if (arg_count > 3) { command_done = 7; } else { initCommandBuffer(); } } if (command_done == 7) { printf_P(PSTR("{\"arg[3]\": \"%s\"}\r\n"),arg[3]); if (arg_count > 4) { command_done = 8; } else { initCommandBuffer(); } } if (command_done == 8) { printf_P(PSTR("{\"arg[4]\": \"%s\"}\r\n"),arg[4]); initCommandBuffer(); } } } } return 0; }
int main(void) { /* Initialize UART, it returns a pointer to FILE so redirect of stdin and stdout works*/ stdout = stdin = uartstream0_init(BAUD); /* Initialize I2C, with the internal pull-up*/ twi_init(1); /* Clear and setup the command buffer, (probably not needed at this point) */ initCommandBuffer(); sei(); // Enable global interrupts char rpu_addr = get_Rpu_address(); // set a default address if RPU manager not found if (rpu_addr == 0) { rpu_addr = '0'; } while(1) { // check if character is available to assemble a command, e.g. non-blocking if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h { // get a character from stdin and use it to assemble a command AssembleCommand(getchar()); // address is a char e.g. the ascii value for '0' warning: a null will terminate the command string. StartEchoWhenAddressed(rpu_addr); } // check if the character is available, and if so stop transmit and the command in process. // a multi-drop bus can have another device start transmitting after the second received byte so // there is little time to detect a possible collision if ( command_done && uart0_available() ) { // dump the transmit buffer to limit a collision uart0_flush(); initCommandBuffer(); } // finish echo of the command line befor starting a reply (or the next part of reply) if ( command_done && (uart0_availableForWrite() == UART_TX0_BUFFER_SIZE) ) { if ( !echo_on ) { // this happons when the address did not match initCommandBuffer(); } else { // command is a pointer to string and arg[] is an array of pointers to strings // use findCommand to make them point to the correct places in the command line // this can only be done once, since spaces and delimeters are replaced with null termination if (command_done == 1) { findCommand(); command_done = 10; } if ( (command_done >= 10) && (command_done < 250) ) { ProcessCmd(); } else { initCommandBuffer(); } } } } return 0; }
int main(void) { setup(); while(1) { // use LED to show if I2C has a bus manager blink(); // check if character is available to assemble a command, e.g. non-blocking if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h { // get a character from stdin and use it to assemble a command AssembleCommand(getchar()); // address is an ascii value, warning: a null address would terminate the command string. StartEchoWhenAddressed(rpu_addr); } // check if a character is available, and if so flush transmit buffer and nuke the command in process. // A multi-drop bus can have another device start transmitting after getting an address byte so // the first byte is used as a warning, it is the onlly chance to detect a possible collision. if ( command_done && uart0_available() ) { // dump the transmit buffer to limit a collision uart0_flush(); initCommandBuffer(); } // delay between ADC burst adc_burst(); // finish echo of the command line befor starting a reply (or the next part of a reply) if ( command_done && (uart0_availableForWrite() == UART_TX0_BUFFER_SIZE) ) { if ( !echo_on ) { // this happons when the address did not match initCommandBuffer(); } else { if (command_done == 1) { findCommand(); command_done = 10; } // do not overfill the serial buffer since that blocks looping, e.g. process a command in 32 byte chunks if ( (command_done >= 10) && (command_done < 250) ) { ProcessCmd(); } else { initCommandBuffer(); } } } } return 0; }