void error (const PropWare::ErrorCode err) { // Set the Quickstart LEDs for output (used to display the error code) PropWare::SimplePort debugLEDs(PropWare::Port::P16, 8, PropWare::Pin::OUT); while (1) { debugLEDs.write((uint32_t) err); waitcnt(CLKFREQ / 5 + CNT); debugLEDs.write(0); waitcnt(CLKFREQ / 5 + CNT); } }
int getQTIs(int highPin, int lowPin) // Function - getQTIs { int dt = (CLKFREQ / 1000000) * 230; // Set up 230 us time increment set_outputs(highPin, lowPin, 0b1111); // highPin...lowPin set high set_directions(highPin, lowPin, 0b1111); // highPin...lowPin set to output waitcnt(dt + CNT); // Wait 230 us set_directions(highPin, lowPin, 0b0000); // highPin...lowPin st to input waitcnt(dt + CNT); // Wait 230 us int qtis = get_states(highPin, lowPin); // Get 4-bit pattern QTIs apply return qtis; // Return val containing pattern }
void error (const PropWare::ErrorCode err) { PropWare::SimplePort debugLEDs(PropWare::Port::P16, 8, PropWare::Pin::OUT); pwSyncOut.printf("Unknown error: %u\n", err); while (1) { debugLEDs.write((uint32_t) err); waitcnt(100 * MILLISECOND); debugLEDs.write(0); waitcnt(100 * MILLISECOND); } }
int main() // Main function { All_LED( 0 ); InitSerial(); Sensors_Start( PIN_SDI, PIN_SDO, PIN_SCL, PIN_CS_AG, PIN_CS_M, PIN_CS_ALT, PIN_LED, (int)&LEDValue[0], LED_COUNT ); loopTimer = CNT; int filter = 256; while(1) { //Read ALL inputs from the sensors into local memory, starting at Temperature memcpy( &sens, Sensors_Address(), Sensors_ParamsSize ); S4_Put( 0, '$' ); // Send a $ as a signature (like a packet start) LogInt( sens.GyroY ); // Sends an integer, followed by a space LogInt( sens.AccelX ); // Sends an integer, followed by a space LogInt( sens.AccelZ ); // Sends an integer, followed by a space S4_Put( 0, 13 ); // Send a carriage return ++counter; loopTimer += 80000000 / 100; waitcnt( loopTimer ); } }
/** * @example L3G_Demo.cpp * * Read the gyrometer data and print it to the terminal * * @include PropWare_L3G/CMakeLists.txt */ int main () { int16_t gyroValues[3]; PropWare::SPI *spi = PropWare::SPI::get_instance(); PropWare::L3G gyro(spi, CS); gyro.start(); gyro.set_dps(PropWare::L3G::DPS_500); // Though this functional call is not necessary (default value is 0), I // want to bring attention to this function. It will determine whether the // l3g_read* functions will always explicitly set the SPI modes before // each call, or assume that the SPI driver is still running in the proper // configuration gyro.always_set_spi_mode(1); while (1) { gyro.read_all(gyroValues); //pwOut << "Gyro vals DPS... X: " << gyro.convert_to_dps(gyroValues[PropWare::L3G::X]) // << "\tY: " << gyro.convert_to_dps(gyroValues[PropWare::L3G::Y]) // << "\tZ: " << gyro.convert_to_dps(gyroValues[PropWare::L3G::Z]) << '\n'; pwOut << "Gyro vals DPS... X: " << gyroValues[PropWare::L3G::X] << "\tY: " << gyroValues[PropWare::L3G::Y] << "\tZ: " << gyroValues[PropWare::L3G::Z] << '\n'; waitcnt(50*MILLISECOND + CNT); } return 0; }
void ultrasonic_run() { while(1) { high(PIN_RANGE_TRIG); waitcnt(CNT + CLKFREQ/100000); // 10 uS low(PIN_RANGE_TRIG); int duration = pulse_in(PIN_RANGE_ECHO, 1)/58; if (duration > 400) duration = 400; range = duration; waitcnt(CNT + CLKFREQ/17); } }
__attribute__((constructor)) void init(void) { /* assert tx pin low for QuickStart */ OUTA &= ~(1<<30); DIRA |= (1<<30); /* wait a second for terminal to start */ //sleep(1); waitcnt(CLKFREQ+CNT); }
/** * drain waits for all bytes to be sent */ void FdSerial_drain(FdSerial_t *data) { while(data->tx_tail != data->tx_head) ; /* wait for transmission */ waitcnt(_clkfreq + data->ticks); }
void pause(int secs) { while (secs > 0) { waitcnt(_CNT+_clkfreq); --secs; } }
void imu_run() { while(1) { last = CNT; waitcnt(CNT + CLKFREQ/1000*IMU_UPDATE_DELAY); imu_update(); } }
terminal *simpleterm_open(void) { if(dport_ptr != 0) return dport_ptr; dport_ptr = serial_open(31,30,0,115200); waitcnt(CLKFREQ+CNT); return dport_ptr; }
// Function runs in another cog void ms_timer(void *par) { dt = CLKFREQ/1000; int ticks = CNT; while(1) { waitcnt(ticks+=dt); t++; } }
void Listener::init () { this->m_listener.set_rx_mask(RX_PIN); this->m_listener.set_baud_rate(BAUD_RATE); this->m_listener.set_parity(PARITY); memset(m_buffer, 0, sizeof(m_buffer)); // A very short wait to ensure the main cog has finished printing its "I'm ready" statement before we start // printing ours waitcnt(10 * MILLISECOND + CNT); }
void pwm_run() { servo_start(); waitcnt(CNT + CLKFREQ/2); while(1) { for (int i=0;i<4;i++) servo_set(motors[i]->pin, motors[i]->current_val); } }
int32_t screenSpin::Fadein(void) { int32_t Contrastnum; Contrastnum = 0; while (Contrastnum < 16) { Contrast(Contrastnum); Contrastnum = (Contrastnum + 1); waitcnt(((CLKFREQ / 8000) + CNT)); } return 0; }
int32_t screenSpin::Fadeout(void) { int32_t Contrastnum; Contrastnum = 15; while (Contrastnum > (-1)) { Contrast(Contrastnum); Contrastnum = (Contrastnum - 1); waitcnt(((CLKFREQ / 8000) + CNT)); } return 0; }
void wait(int time) // pause function definition { // If dt not initialized, set it up to 1 us. /* if(!st_iodt) // If dt not initialized { set_io_dt(CLKFREQ/1000000); // Set up timed I/O time increment set_io_timeout(CLKFREQ/4); // Set up timeout } */ time *= st_iodt; // Calculate system clock ticks waitcnt(st_mark += time); // Wait for system clock target }
int Sirc::getPulseWidth(){ //Var we are using to count time. int count = 0; //Wait for incoming signal //Start waiting for the pin to get low. //This indicates the start of a transmission. while(true){ //Check if the pin is low. if(input(pin) == 0){ //Get out of the loop. break; } //Increase the counter. //Each tick is 100 microseconds. count++; //Check if the timeout is enabled. //Check if we need to time out already. if(timeout > 0 && count >= (timeout*10)){ //Return the timeout value (-1). return TIMEOUT; } //Wait for 100 microseconds. waitcnt(CNT+100*us); } //Reset the counter. //We are going to use it to count the pulse width now. count = 0; //Run until the pin becomes high again. while(input(pin) == 0){ //Increase the counter. //Each tick represents 100 us. count++; //Wait for 100 microseconds. waitcnt(CNT+100*us); } //Return the counted value. return count; }
/** * @example DuplexUART_Demo.cpp * * Write "Hello world!" out via UART protocol and receive an echo * * @include PropWare_DuplexUART/CMakeLists.txt */ int main () { uint32_t threadStack[256]; Listener listener(threadStack); PropWare::SimplexUART speaker(TX_PIN); // Start our new cog and initialize the speaking UART speaker.set_baud_rate(BAUD_RATE); speaker.set_parity(PARITY); pwSyncOut.printf("New cog ID: %d. Ready to send!!!\n", PropWare::Runnable::invoke(listener)); while (1) { waitcnt(200 * MILLISECOND + CNT); speaker.puts(TEST_STRING); } }
int main(int argc, char* argv[]) { #if defined(__USE_XMM__) int mask = 0x00008000; /* only toggle the C3 board LED */ #else int mask = 0x3fffffff; /* toggle all pins except the serial */ #endif int freq; freq = CLKFREQ; DIRA = mask; OUTA = mask; for(;;) { OUTA ^= mask; waitcnt(freq+CNT); } }
void SPIShiftIn_fast (const uint8_t bits, void *data, const uint8_t bytes) { uint8_t *par8; uint16_t *par16; uint32_t *par32; // Wait until idle state, then send function and mode bits SPIWait(); g_mailbox = SPI_FUNC_READ_FAST | (bits << SPI_BITS_OFFSET); // Wait for a value to be written while ((uint32_t) -1 == g_mailbox) waitcnt(SPI_TIMEOUT_WIGGLE_ROOM + CNT); // Determine if output variable is char, short or long and write data to that location switch (bytes) { case 1: par8 = data; *par8 = g_mailbox; break; case 2: par16 = data; *par16 = g_mailbox; break; case 4: par32 = data; *par32 = g_mailbox; break; default: #ifdef SPI_DEBUG SPIError(SPI_INVALID_BYTE_SIZE); #else return; #endif break; } // Signal that value is saved and GAS cog can continue execution g_mailbox = -1; }
void pw(void *par) { FRQA = 1; FRQB = 1; int pin; unsigned int dt = tCycle; unsigned int t = CNT; while(1) { waitcnt(t+=dt); if(ctra != CTRA) { if(ctra != 0) { pin = CTRA & 0b111111; DIRA &= ~(1 << pin); } CTRA = ctra; pin = CTRA & 0b111111; DIRA |= (1 << pin); } if(ctrb != CTRB) { if(ctrb != 0) { pin = CTRB & 0b111111; DIRB &= ~(1 << pin); } CTRB = ctrb; pin = CTRB & 0b111111; DIRA |= (1 << pin); } PHSA = -ticksA; PHSB = -ticksB; } }
// Main function void main (void) { uint8_t err; char c; sd_file f, f2; #ifndef LOW_RAM_MODE /* Option 1: Create at least one new sd_buffer variable * * An extra 526 bytes of memory are required to create a new sd_buffer * for the file variable, but speed will be increased if files are * being switched often. Using this option will allow the directory * contents to be kept in RAM while a file is loaded. * */ sd_buffer fileBuf; // If extra RAM is available sd_buffer fileBuf2; f.buf = &fileBuf; f2.buf = &fileBuf2; #else /* Option 2: Use the generic buffer [i.e. g_sd_buf] as the buffer * * Good for low-RAM situations due to the re-use of g_sd_buf. Speed is * decreased when multiple files are used often. * */ f.buf = &g_sd_buf; f2.buf = &g_sd_buf; #endif #ifdef DEBUG printf("Beginning SD card initialization...\n"); #endif // Start your engines!!! if ((err = SDStart(MOSI, MISO, SCLK, CS, -1))) error(err); #ifdef DEBUG printf("SD routine started. Mounting now...\n"); #endif if ((err = SDMount())) error(err); #ifdef DEBUG printf("FAT partition mounted!\n"); #endif #ifdef TEST_SHELL SD_Shell(&f); #elif (defined TEST_WRITE) // Create a blank file and copy the contents of STUFF.TXT into it SDfopen(OLD_FILE, &f, SD_FILE_MODE_R); SDfopen(NEW_FILE, &f2, SD_FILE_MODE_R_PLUS); #ifdef DEBUG printf("Both files opened...\n"); #endif while (!SDfeof(&f)) { c = SDfgetc(&f); SDfputc(c, &f2); #ifdef _STDIO_H putchar(SDfgetc(&f2)); #endif } #ifdef DEBUG printf("\nFile printed...\n"); printf("Now closing read-only file!\n"); #endif SDfclose(&f); #ifdef DEBUG printf("***Now closing the modified file!***\n"); #endif SDfclose(&f2); #ifdef DEBUG printf("Files closed...\n"); SDfopen(NEW_FILE, &f2, SD_FILE_MODE_R); printf("File opened for a second time, now printing new contents...\n"); while (!SDfeof(&f2)) putchar(SDfgetc(&f2)); SDfclose(&f2); #endif SDUnmount(); #else SDchdir("JAZZ"); SDfopen("DESKTOP.INI", &f, SD_FILE_MODE_R); while (!SDfeof(&f)) #ifdef DEBUG putchar(SDfgetc(&f)); #endif #endif #ifdef DEBUG printf("Execution complete!\n"); #endif GPIODirModeSet(BIT_16, GPIO_DIR_OUT); while (1) { GPIOPinToggle(BIT_16); waitcnt(CLKFREQ/2 + CNT); } }
void test123::Pauseabit(void) { waitcnt((40000000 + CNT)); }
void btest005::pausems(uint32_t ms) { uint32_t tim; tim = ms * mscycles; waitcnt(tim + getcnt()); }
/****************************** start main routine ******************************/ int main(void) { char input_buffer[INPUT_BUFFER]; //buffer for user input waitcnt(500000 + _CNT); //wait until initialization is complete printf("XMMC-cogc demo v%s",VERSION); //display startup message /* start monitoring the real time clock DS3231 */ int cog; rtc_cb.rtc.tdb_lock = locknew(); lockclr(rtc_cb.rtc.tdb_lock); cog = start_rtc(&rtc_cb.rtc); if(cog == -1) { printf("** error attempting to start rtc cog\n cognew returned %i\n\n",cog); return 1; } printf(" DS3231 monitored by code running on cog %i\n",cog); /* set all cogs to not running */ parA.A.cog = -1; parB.B.cog = -1; parC.C.cog = -1; /* loop forever */ while(1) { printf("\nenter command > "); //prompt user fgets(input_buffer,INPUT_BUFFER,stdin); //get a line of input input_buffer[strlen(input_buffer)-1] = '\0'; //get rid of trailing new line character switch(process(input_buffer)) //test input,take appropriate action { case 0: //startA if(start_cogA(&parA.A)== -1) printf(" problem starting cogA\n"); else printf(" cogA started\n"); break; case 1: //startB if(start_cogB(&parB.B)== -1) printf(" problem starting cogB\n"); else printf(" cogB started\n"); break; case 2: //startC if(start_cogC(&parC.C)== -1) printf(" problem starting cogC\n"); else printf(" cogC started\n"); break; case 3: //stopA cogstop(parA.A.cog); printf(" cog A stopped\n"); parA.A.cog = -1; break; case 4: //stopB cogstop(parB.B.cog); printf(" cog B stopped\n"); parB.B.cog = -1; break; case 5: //stopC cogstop(parC.C.cog); printf(" cog C stopped\n"); parC.C.cog = -1; break; case 6: //queryA if(parA.A.cog == -1) printf("cog A is not running\n"); else parA.A.query_flag = 1; break; case 7: //queryB if(parB.B.cog == -1) printf("cog B is not running\n"); else parB.B.query_flag = 1; break; case 8: //queryC if(parC.C.cog == -1) printf("cog C is not running\n"); else parC.C.query_flag = 1; break; case 9: //status status(); break; case 10: //exit printf("exiting program\n"); return 0; case 11: //time printf("%s, %i:%02i:%02i %i/%i/%i\n\n", day_names_long[rtc_cb.rtc.dow-1], rtc_cb.rtc.hour, rtc_cb.rtc.min, rtc_cb.rtc.sec, rtc_cb.rtc.month, rtc_cb.rtc.day, rtc_cb.rtc.year+2000); break; default: printf("<%s> is not a valid command\n",input_buffer); } } return 0; }
// Wait for command to be cleared, signifying receipt int32_t screen_init(int32_t ChipSelect, int32_t DataCommand, int32_t TheData, int32_t TheClock, int32_t Reset, int32_t VCC_state, int32_t Type) { // Startup the SPI system screen_start(); // Initialize variables and initialize the display self->CS = ChipSelect; self->DC = DataCommand; self->DATA = TheData; self->CLK = TheClock; self->RST = Reset; self->vccstate = VCC_state; self->displayType = Type; if (self->displayType == TYPE_128X32) { self->displayWidth = SSD1306_LCDWIDTH; self->displayHeight = SSD1306_LCDHEIGHT32; } else { self->displayWidth = SSD1306_LCDWIDTH; self->displayHeight = SSD1306_LCDHEIGHT64; } // Setup reset and pin direction screen_HIGH(self->RST); // VDD (3.3V) goes high at start; wait for a ms waitcnt(((CLKFREQ / 100000) + CNT)); // force reset low screen_LOW(self->RST); // wait 10ms waitcnt(((CLKFREQ / 100000) + CNT)); // remove reset screen_HIGH(self->RST); if (self->displayType == TYPE_128X32) { // ************************************ // Init sequence for 128x32 OLED module // ************************************ screen_ssd1306_Command(SSD1306_DISPLAYOFF); screen_ssd1306_Command(SSD1306_SETDISPLAYCLOCKDIV); screen_ssd1306_Command(128); screen_ssd1306_Command(SSD1306_SETMULTIPLEX); screen_ssd1306_Command(31); screen_ssd1306_Command(SSD1306_SETDISPLAYOFFSET); screen_ssd1306_Command(0); screen_ssd1306_Command((SSD1306_SETSTARTLINE | 0x0)); screen_ssd1306_Command(SSD1306_CHARGEPUMP); if (self->vccstate == SSD1306_EXTERNALVCC) { screen_ssd1306_Command(16); } else { screen_ssd1306_Command(20); } screen_ssd1306_Command(SSD1306_MEMORYMODE); screen_ssd1306_Command(0); screen_ssd1306_Command((SSD1306_SEGREMAP | 0x1)); screen_ssd1306_Command(SSD1306_COMSCANDEC); screen_ssd1306_Command(SSD1306_SETCOMPINS); screen_ssd1306_Command(2); screen_ssd1306_Command(SSD1306_SETCONTRAST); screen_ssd1306_Command(143); screen_ssd1306_Command(SSD1306_SETPRECHARGE); if (self->vccstate == SSD1306_EXTERNALVCC) { screen_ssd1306_Command(34); } else { // SSD1306_SWITCHCAPVCC screen_ssd1306_Command(241); } screen_ssd1306_Command(SSD1306_SETVCOMDETECT); screen_ssd1306_Command(64); screen_ssd1306_Command(SSD1306_DISPLAYALLON_RESUME); screen_ssd1306_Command(SSD1306_NORMALDISPLAY); // --turn on oled panel screen_ssd1306_Command(SSD1306_DISPLAYON); } else { // ************************************ // Init sequence for 128x64 OLED module // ************************************ screen_ssd1306_Command(SSD1306_DISPLAYOFF); // low col = 0 screen_ssd1306_Command(SSD1306_SETLOWCOLUMN); // hi col = 0 screen_ssd1306_Command(SSD1306_SETHIGHCOLUMN); // line #0 screen_ssd1306_Command(SSD1306_SETSTARTLINE); screen_ssd1306_Command(SSD1306_SETCONTRAST); if (self->vccstate == SSD1306_EXTERNALVCC) { screen_ssd1306_Command(159); } else { screen_ssd1306_Command(207); } screen_ssd1306_Command(161); screen_ssd1306_Command(SSD1306_NORMALDISPLAY); screen_ssd1306_Command(SSD1306_DISPLAYALLON_RESUME); screen_ssd1306_Command(SSD1306_SETMULTIPLEX); screen_ssd1306_Command(63); screen_ssd1306_Command(SSD1306_SETDISPLAYOFFSET); // No offset screen_ssd1306_Command(0); screen_ssd1306_Command(SSD1306_SETDISPLAYCLOCKDIV); screen_ssd1306_Command(128); screen_ssd1306_Command(SSD1306_SETPRECHARGE); if (self->vccstate == SSD1306_EXTERNALVCC) { screen_ssd1306_Command(34); } else { screen_ssd1306_Command(241); } screen_ssd1306_Command(SSD1306_SETVCOMDETECT); screen_ssd1306_Command(64); screen_ssd1306_Command(SSD1306_SETCOMPINS); screen_ssd1306_Command(18); screen_ssd1306_Command(SSD1306_MEMORYMODE); screen_ssd1306_Command(0); screen_ssd1306_Command((SSD1306_SEGREMAP | 0x1)); screen_ssd1306_Command(SSD1306_COMSCANDEC); screen_ssd1306_Command(SSD1306_CHARGEPUMP); if (self->vccstate == SSD1306_EXTERNALVCC) { screen_ssd1306_Command(16); } else { screen_ssd1306_Command(20); } // --turn on oled panel screen_ssd1306_Command(SSD1306_DISPLAYON); } self->crsrX = 0; self->crsrY = 0; self->charSize = LARGE; invert(0); //screen_AutoUpdateOff(); // commented 8/23 5:26 PM //clear(); return 0; }
void pause(int time) // pause function definition { // If st_pauseTicks not initialized, set it up to 1 ms. // if(!st_pauseTicks) set_pause_dt(CLKFREQ/1000); time *= st_pauseTicks; // Calculate system clock ticks waitcnt(time+CNT); // Wait for system clock target }
void pause() { waitcnt(CLKFREQ+CNT); }
void quad_time(int power, int time) { int tmp = CNT; quad_power(power); waitcnt(tmp + CLKFREQ*time); }