tc520.c

Go to the documentation of this file.
00001 
00042 #include "hw/hw_tc520.h"
00043 
00044 #include <cfg/macros.h>
00045 #include <cfg/compiler.h>
00046 
00047 #include <drv/tc520.h>
00048 #include <drv/timer.h>
00049 
00050 #warning FIXME:This implementation is obsolete. Refactor with KFile interface.
00051 
00052 #if 0
00053 
00054 #include <drv/ser.h>
00055 
00056 static Serial *spi_ser;
00057 
00058 #define TC520_CONVERSION_TIMEOUT ms_to_ticks(1000)
00059 #define INIT_LOAD_VALUE 0x00
00060 
00092 tc520_data_t tc520_read(void)
00093 {
00094     /* Start convertion and wait */
00095     CE_LOW();
00096     ticks_t start = timer_clock();
00097     do
00098     {
00099         /* timeout check */
00100         if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
00101         {
00102             ASSERT(0);
00103             CE_HIGH();
00104             return TC520_MAX_VALUE;
00105         }
00106     }
00107     while(DV_LOW());
00108 
00109     start = timer_clock();
00110     do
00111     {
00112         /* timeout check */
00113         if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
00114         {
00115             ASSERT(0);
00116             CE_HIGH();
00117             return TC520_MAX_VALUE;
00118         }
00119     }
00120     while(DV_HIGH());
00121 
00122     /* Ok, convertion finished, read result */
00123     CE_HIGH();
00124     READ_LOW();
00125 
00126     /* RX buffer could be dirty...*/
00127     ser_purge(spi_ser);
00128 
00129     /* I/O buffer */
00130     uint8_t buf[3] = "\x0\x0\x0";
00131 
00132     /* Dummy write to activate recv */
00133     ser_write(spi_ser, buf, sizeof(buf));
00134     ser_drain(spi_ser);
00135     READ_HIGH();
00136 
00137     /* recv */
00138     ASSERT(ser_read(spi_ser, buf, sizeof(buf)) == sizeof(buf));
00139 
00140     tc520_data_t res;
00141 
00142     /* Recompose data */
00143     res = (((tc520_data_t)(buf[0] & 0x3F)) << 10) | (((tc520_data_t)buf[1]) << 2) | (((tc520_data_t)buf[2]) >> 6);
00144 
00145     #define OVERRANGE_BIT BV(7)
00146     /* Handle overrange bit as 17th bit */
00147     if (buf[0] & OVERRANGE_BIT)
00148         res |= BV32(16);
00149 
00150     return res;
00151 }
00152 
00153 
00157 void tc520_init(Serial *spi_port)
00158 {
00159     spi_ser = spi_port;
00160     /* init io ports */
00161     TC520_HW_INIT;
00162     /* Send initial load value */
00163     LOAD_LOW();
00164     ser_putchar(INIT_LOAD_VALUE, spi_ser);
00165     ser_drain(spi_ser);
00166     LOAD_HIGH();
00167 }
00168 
00169 #endif