static void i2c_wr_bit(i2cInfo *inf, bool b) { jshPinSetValue(inf->pinSDA, b); dly(inf); jshPinSetValue(inf->pinSCL, 1); dly(inf); int timeout = I2C_TIMEOUT; while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch if (!timeout) err("Timeout (wr)"); jshPinSetValue(inf->pinSCL, 0); jshPinSetValue(inf->pinSDA, 1); // stop forcing SDA (needed?) }
static bool i2c_rd_bit(i2cInfo *inf) { jshPinSetValue(inf->pinSDA, 1); // stop forcing SDA dly(inf); jshPinSetValue(inf->pinSCL, 1); // stop forcing SDA int timeout = I2C_TIMEOUT; while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch if (!timeout) err("Timeout (rd)"); dly(inf); bool b = jshPinGetValue(inf->pinSDA); jshPinSetValue(inf->pinSCL, 0); return b; }
int main(void) { LPC_GPIO1->DIR=(1<<6); while(1) { LPC_GPIO1->OUT = LPC_GPIO1->OUT&~(1<<6); dly(); LPC_GPIO1->OUT = LPC_GPIO1->OUT|(1<<6); dly(); } return 0 ; }
static void i2c_stop(i2cInfo *inf) { jshPinSetValue(inf->pinSDA, 0); dly(inf); jshPinSetValue(inf->pinSCL, 1); int timeout = I2C_TIMEOUT; while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch if (!timeout) err("Timeout (stop)"); dly(inf); jshPinSetValue(inf->pinSDA, 1); dly(inf); if (!jshPinGetValue(inf->pinSDA)) err("Arbitration (stop)"); dly(inf); inf->started = false; }
static void i2c_start(i2cInfo *inf) { if (inf->started) { // reset jshPinSetValue(inf->pinSDA, 1); dly(inf); jshPinSetValue(inf->pinSCL, 1); int timeout = I2C_TIMEOUT; while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch if (!timeout) err("Timeout (start)"); dly(inf); } if (!jshPinGetValue(inf->pinSDA)) err("Arbitration (start)"); jshPinSetValue(inf->pinSDA, 0); dly(inf); jshPinSetValue(inf->pinSCL, 0); dly(inf); inf->started = true; }
int16_t halCalibrateVlo() { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer dly(1000); // Wait for oscillators to settle uint16_t temp_BCSCTL1 = BCSCTL1; uint16_t temp_TACCTL0 = TACCTL0; uint16_t temp_TACTL = TACTL; BCSCTL1 |= DIVA_3; // Divide ACLK by 8 TACCTL0 = CM_1 + CCIS_1 + CAP; // Capture on ACLK TACTL = TASSEL_2 + MC_2 + TACLR; // Start TA, SMCLK(DCO), Continuous while ((TACCTL0 & CCIFG) == 0); // Wait until capture TACCR0 = 0; // Ignore first capture TACCTL0 &= ~CCIFG; // Clear CCIFG while ((TACCTL0 & CCIFG) == 0); // Wait for next capture unsigned int firstCapture = TACCR0; // Save first capture TACCTL0 &= ~CCIFG; // Clear CCIFG while ((TACCTL0 & CCIFG) ==0); // Wait for next capture unsigned long counts = (TACCR0 - firstCapture); // # of VLO clocks in 8Mhz BCSCTL1 = temp_BCSCTL1; // Restore original settings TACCTL0 = temp_TACCTL0; TACTL = temp_TACTL; //TACCTL0 = 0; TACTL = 0; // Clear Timer settings signed int aClk = ((unsigned int) (32000000l / counts)); if ((aClk > VLO_MIN) && (aClk < VLO_MAX)) return aClk; else return -1; }