/* Copyright 2010, Unpublished Work of Technologic Systems * All Rights Reserved. * * THIS WORK IS AN UNPUBLISHED WORK AND CONTAINS CONFIDENTIAL, * PROPRIETARY AND TRADE SECRET INFORMATION OF TECHNOLOGIC SYSTEMS. * ACCESS TO THIS WORK IS RESTRICTED TO (I) TECHNOLOGIC SYSTEMS * EMPLOYEES WHO HAVE A NEED TO KNOW TO PERFORM TASKS WITHIN THE SCOPE * OF THEIR ASSIGNMENTS AND (II) ENTITIES OTHER THAN TECHNOLOGIC * SYSTEMS WHO HAVE ENTERED INTO APPROPRIATE LICENSE AGREEMENTS. NO * PART OF THIS WORK MAY BE USED, PRACTICED, PERFORMED, COPIED, * DISTRIBUTED, REVISED, MODIFIED, TRANSLATED, ABRIDGED, CONDENSED, * EXPANDED, COLLECTED, COMPILED, LINKED, RECAST, TRANSFORMED, ADAPTED * IN ANY FORM OR BY ANY MEANS, MANUAL, MECHANICAL, CHEMICAL, * ELECTRICAL, ELECTRONIC, OPTICAL, BIOLOGICAL, OR OTHERWISE WITHOUT * THE PRIOR WRITTEN PERMISSION AND CONSENT OF TECHNOLOGIC SYSTEMS. * ANY USE OR EXPLOITATION OF THIS WORK WITHOUT THE PRIOR WRITTEN * CONSENT OF TECHNOLOGIC SYSTEMS COULD SUBJECT THE PERPETRATOR TO * CRIMINAL AND CIVIL LIABILITY. */ /******************************************************************************* * Program: * DIO Set and Get (dio.c) * Technologic Systems TS-7500 * * Summary: * This program will return the temperature in celcius when called. * Note: The scripted version of gettemp (ts7500.subr) does not round off * whereas this .c version does. Notice careful semaphore usage (sbuslock, * sbusunlock) within main. * * This program will only work on TS-7500/TS-7550 with a TS-752. For other * TS-75XX/TS-4500 based products please use i2ctemp.c in the same FTP directory. * * Usage: * ./gettemp * * Compile with: * gcc -mcpu=arm9 gettemp.c sbus.c -o dio * Version history: * 09/22/2009 - DH * Initial revision * 12/16/2010 - KB * Updated for new sbus.c API * *******************************************************************************/ #include "sbus.h" #include #include #include /******************************************************************************* * setdiopin: accepts a DIO register and value to place in that DIO pin. * Values can be 0 (low), 1 (high), or 2 (z - high impedance). *******************************************************************************/ void setdiopin(int pin, int val) { int pinOffSet; int dirPinOffSet; // For Register 0x66 only int outPinOffSet; // For Register 0x66 only // First, check for the high impedance case if (val == 2) { if (pin <= 40 && pin >= 37) { dirPinOffSet = pin - 33; sbus_poke16(0x66, sbus_peek16(0x66) & ~(1 << dirPinOffSet)); } else if (pin <= 36 && pin >= 21) { pinOffSet = pin - 21; sbus_poke16(0x6c, sbus_peek16(0x6c) & ~(1 << pinOffSet)); } else if (pin <= 20 && pin >= 5) { pinOffSet = pin - 5; sbus_poke16(0x72, sbus_peek16(0x72) & ~(1 << pinOffSet)); } } /******************************************************************* *0x66: DIO and tagmem control (RW) * bit 15-12: DIO input for pins 40(MSB)-37(LSB) (RO) * bit 11-8: DIO output for pins 40(MSB)-37(LSB) (RW) * bit 7-4: DIO direction for pins 40(MSB)-37(LSB) (1 - output) (RW) ********************************************************************/ else if (pin <= 40 && pin >= 37) { dirPinOffSet = pin - 33; // -37 + 4 = Direction; -37 + 8 = Output outPinOffSet = pin - 29; // set bit [pinOffset] to [val] of register [0x66] if(val) sbus_poke16(0x66, (sbus_peek16(0x66) | (1 << outPinOffSet))); else sbus_poke16(0x66, (sbus_peek16(0x66) & ~(1 << outPinOffSet))); // Make the specified pin into an output in direction bits sbus_poke16(0x66, sbus_peek16(0x66) | (1 << dirPinOffSet)); /// } /********************************************************************* *0x68: DIO input for pins 36(MSB)-21(LSB) (RO) *0x6a: DIO output for pins 36(MSB)-21(LSB) (RW) *0x6c: DIO direction for pins 36(MSB)-21(LSB) (1 - output) (RW) *********************************************************************/ else if (pin <= 36 && pin >= 21) { pinOffSet = pin - 21; // set bit [pinOffset] to [val] of register [0x6a] if(val) sbus_poke16(0x6a, (sbus_peek16(0x6a) | (1 << pinOffSet))); else sbus_poke16(0x6a, (sbus_peek16(0x6a) & ~(1 << pinOffSet))); // Make the specified pin into an output in direction register sbus_poke16(0x6c, sbus_peek16(0x6c) | (1 << pinOffSet)); /// } /********************************************************************* *0x6e: DIO input for pins 20(MSB)-5(LSB) (RO) *0x70: DIO output for pins 20(MSB)-5(LSB) (RW) *0x72: DIO direction for pins 20(MSB)-5(LSB) (1 - output) (RW) *********************************************************************/ else if (pin <= 20 && pin >= 5) { pinOffSet = pin - 5; if(val) sbus_poke16(0x70, (sbus_peek16(0x70) | (1 << pinOffSet))); else sbus_poke16(0x70, (sbus_peek16(0x70) & ~(1 << pinOffSet))); // Make the specified pin into an output in direction register sbus_poke16(0x72, sbus_peek16(0x72) | (1 << pinOffSet)); } } /******************************************************************************* * getdiopin: accepts a DIO pin number and returns its value. *******************************************************************************/ int getdiopin(int pin) { int pinOffSet; int pinValue = 99999; /******************************************************************* *0x66: DIO and tagmem control (RW) * bit 15-12: DIO input for pins 40(MSB)-37(LSB) (RO) * bit 11-8: DIO output for pins 40(MSB)-37(LSB) (RW) * bit 7-4: DIO direction for pins 40(MSB)-37(LSB) (1 - output) (RW) ********************************************************************/ if (pin <= 40 && pin >= 37) { pinOffSet = pin - 25; // -37 to get to 0, + 10 to correct offset // Obtain the specific pin value (1 or 0) pinValue = (sbus_peek16(0x66) >> pinOffSet) & 0x0001; } /********************************************************************* *0x68: DIO input for pins 36(MSB)-21(LSB) (RO) *0x6a: DIO output for pins 36(MSB)-21(LSB) (RW) *0x6c: DIO direction for pins 36(MSB)-21(LSB) (1 - output) (RW) *********************************************************************/ else if (pin <= 36 && pin >= 21) { pinOffSet = pin - 21; // Easier to understand when LSB = 0 and MSB = 15 // Obtain the specific pin value (1 or 0) pinValue = (sbus_peek16(0x68) >> pinOffSet) & 0x0001; } /********************************************************************* *0x6e: DIO input for pins 20(MSB)-5(LSB) (RO) *0x70: DIO output for pins 20(MSB)-5(LSB) (RW) *0x72: DIO direction for pins 20(MSB)-5(LSB) (1 - output) (RW) *********************************************************************/ else if (pin <= 20 && pin >= 5) { pinOffSet = pin - 5; // Easier to understand when LSB = 0 and MSB = 15 // Obtain the specific pin value (1 or 0) pinValue = (sbus_peek16(0x6e) >> pinOffSet) & 0x0001; } return pinValue; } /******************************************************************************* * gettemp: returns the CPU temperature in celcius *******************************************************************************/ float gettemp(void) { int n,x; int val = 0; float temp = 0; setdiopin(22,0); setdiopin(12,2); n=0; while(n < 13) { setdiopin(14,0); setdiopin(14,1); x = getdiopin(12); if (x == 0) val = val << 1; else val = (val << 1) | 1; n++; } setdiopin(22,2); setdiopin(14,2); if((val & 0x1000) != 0) { val=((~(val & 0xfff) & 0xfff) + 1); temp = val * -62500; return(temp / 1000000); } else { temp = val * 62500; return(temp / 1000000); } } /******************************************************************************* * Main * A sample program on how to use the gettemp function of the sbus API. *******************************************************************************/ int main() { float theTemp = 0; sbuslock(); theTemp = gettemp(); sbusunlock(); printf("%.1f\n", theTemp); return 0; }