1.1 diff -r c19afa2c81db -r b63a3999e2e7 src/tc8250.c 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/src/tc8250.c Fri Apr 18 01:34:20 2014 -0600 1.4 @@ -0,0 +1,247 @@ 1.5 +#include <stdint.h> 1.6 +#include <stdbool.h> 1.7 +#include <malloc.h> 1.8 +#include <time.h> 1.9 +#include "tc8250.h" 1.10 + 1.11 +#ifndef TC8250_DEBUG 1.12 +#define NDEBUG 1.13 +#endif 1.14 +#include "utils.h" 1.15 + 1.16 +void tc8250_init(TC8250_CTX *ctx) 1.17 +{ 1.18 + ctx->chip_enable = false; 1.19 + ctx->address_latch_enable = false; 1.20 + ctx->write_enable = false; 1.21 + ctx->address = 0; 1.22 +} 1.23 + 1.24 +void tc8250_set_chip_enable(TC8250_CTX *ctx, bool enabled) 1.25 +{ 1.26 + LOG("tc8250_set_chip_enable %d\n", enabled); 1.27 + ctx->chip_enable = enabled; 1.28 +} 1.29 + 1.30 +void tc8250_set_address_latch_enable(TC8250_CTX *ctx, bool enabled) 1.31 +{ 1.32 + LOG("tc8250_set_address_latch_enable %d\n", enabled); 1.33 + ctx->address_latch_enable = enabled; 1.34 +} 1.35 + 1.36 +void tc8250_set_write_enable(TC8250_CTX *ctx, bool enabled) 1.37 +{ 1.38 + LOG("tc8250_set_write_enable %d\n", enabled); 1.39 + ctx->write_enable = enabled; 1.40 +} 1.41 + 1.42 +uint8_t get_second(TC8250_CTX *ctx) 1.43 +{ 1.44 + time_t t; 1.45 + struct tm g; 1.46 + uint8_t ret; 1.47 + t = time(NULL); 1.48 + gmtime_r(&t, &g); 1.49 + ret = g.tm_sec; 1.50 + return (ret); 1.51 +} 1.52 + 1.53 +uint8_t get_minute(TC8250_CTX *ctx) 1.54 +{ 1.55 + time_t t; 1.56 + struct tm g; 1.57 + uint8_t ret; 1.58 + t = time(NULL); 1.59 + gmtime_r(&t, &g); 1.60 + ret = g.tm_min; 1.61 + return (ret); 1.62 +} 1.63 + 1.64 +uint8_t get_hour(TC8250_CTX *ctx) 1.65 +{ 1.66 + time_t t; 1.67 + struct tm g; 1.68 + uint8_t ret; 1.69 + t = time(NULL); 1.70 + gmtime_r(&t, &g); 1.71 + ret = g.tm_hour; 1.72 + return (ret); 1.73 +} 1.74 + 1.75 +uint8_t get_day(TC8250_CTX *ctx) 1.76 +{ 1.77 + time_t t; 1.78 + struct tm g; 1.79 + uint8_t ret; 1.80 + t = time(NULL); 1.81 + gmtime_r(&t, &g); 1.82 + ret = g.tm_mday; 1.83 + return (ret); 1.84 +} 1.85 + 1.86 +uint8_t get_month(TC8250_CTX *ctx) 1.87 +{ 1.88 + time_t t; 1.89 + struct tm g; 1.90 + uint8_t ret; 1.91 + t = time(NULL); 1.92 + gmtime_r(&t, &g); 1.93 + ret = g.tm_mon; 1.94 + return (ret); 1.95 +} 1.96 + 1.97 +uint8_t get_year(TC8250_CTX *ctx) 1.98 +{ 1.99 + /*time_t t; 1.100 + struct tm g; 1.101 + uint8_t ret; 1.102 + t = time(NULL); 1.103 + gmtime_r(&t, &g); 1.104 + ret = g.tm_year; 1.105 + return (ret);*/ 1.106 + return (87); 1.107 +} 1.108 + 1.109 +uint8_t get_weekday(TC8250_CTX *ctx) 1.110 +{ 1.111 + time_t t; 1.112 + struct tm g; 1.113 + uint8_t ret; 1.114 + t = time(NULL); 1.115 + gmtime_r(&t, &g); 1.116 + ret = g.tm_wday; 1.117 + return (ret); 1.118 +} 1.119 + 1.120 +uint8_t tc8250_read_reg(TC8250_CTX *ctx) 1.121 +{ 1.122 + LOG("tc8250_read_reg %x\n", ctx->address); 1.123 + switch (ctx->address){ 1.124 + case ONE_SEC_DIGT: 1.125 + return (get_second(ctx) % 10); 1.126 + case TEN_SEC_DIGT: 1.127 + return (get_second(ctx) / 10); 1.128 + case ONE_MIN_DIGT: 1.129 + return (get_minute(ctx) % 10); 1.130 + case TEN_MIN_DIGT: 1.131 + return (get_minute(ctx) / 10); 1.132 + case ONE_HR_DIGT: 1.133 + return (get_hour(ctx) % 10); 1.134 + case TEN_HR_DIGT: 1.135 + return (get_hour(ctx) / 10); 1.136 + case ONE_DAY_DIGT: 1.137 + return (get_day(ctx) % 10); 1.138 + case TEN_DAY_DIGT: 1.139 + return (get_day(ctx) / 10); 1.140 + case ONE_MNTH_DIGT: 1.141 + return (get_month(ctx) % 10); 1.142 + case TEN_MNTH_DIGT: 1.143 + return (get_month(ctx) / 10); 1.144 + case ONE_YR_DIGT: 1.145 + return (get_year(ctx) % 10); 1.146 + case TEN_YR_DIGT: 1.147 + return (get_year(ctx) / 10); 1.148 + case WEEK_DAY: 1.149 + return (get_weekday(ctx) / 10); 1.150 + case TOUT_CONTROL: 1.151 + return (0); 1.152 + case PROTECT_KEY: 1.153 + return (0); 1.154 + case RTC_STATUS: 1.155 + return (0); 1.156 + default: 1.157 + return (0); 1.158 + } 1.159 +} 1.160 + 1.161 +void set_seconds(TC8250_CTX *ctx, uint8_t val) 1.162 +{ 1.163 +} 1.164 + 1.165 +void set_minutes(TC8250_CTX *ctx, uint8_t val) 1.166 +{ 1.167 +} 1.168 + 1.169 +void set_hours(TC8250_CTX *ctx, uint8_t val) 1.170 +{ 1.171 +} 1.172 + 1.173 +void set_days(TC8250_CTX *ctx, uint8_t val) 1.174 +{ 1.175 +} 1.176 + 1.177 +void set_months(TC8250_CTX *ctx, uint8_t val) 1.178 +{ 1.179 +} 1.180 + 1.181 +void set_years(TC8250_CTX *ctx, uint8_t val) 1.182 +{ 1.183 +} 1.184 + 1.185 +void set_weekday(TC8250_CTX *ctx, uint8_t val) 1.186 +{ 1.187 +} 1.188 + 1.189 +void tc8250_write_reg(TC8250_CTX *ctx, uint8_t val) 1.190 +{ 1.191 + LOG("tc8250_write_reg %x", val); 1.192 + if (ctx->address_latch_enable){ 1.193 + LOG(" address\n"); 1.194 + ctx->address = val; 1.195 + return; 1.196 + } 1.197 + if (ctx->chip_enable){ 1.198 + LOG(" %x\n", ctx->address); 1.199 + switch (ctx->address){ 1.200 + case ONE_SEC_DIGT: 1.201 + set_seconds(ctx, val % 10); 1.202 + break; 1.203 + case TEN_SEC_DIGT: 1.204 + set_seconds(ctx, val % 10 * 10); 1.205 + break; 1.206 + case ONE_MIN_DIGT: 1.207 + set_minutes(ctx, val % 10); 1.208 + break; 1.209 + case TEN_MIN_DIGT: 1.210 + set_minutes(ctx, val % 10 * 10); 1.211 + break; 1.212 + case ONE_HR_DIGT: 1.213 + set_hours(ctx, val % 10); 1.214 + break; 1.215 + case TEN_HR_DIGT: 1.216 + set_hours(ctx, val % 10 * 10); 1.217 + break; 1.218 + case ONE_DAY_DIGT: 1.219 + set_days(ctx, val % 10); 1.220 + break; 1.221 + case TEN_DAY_DIGT: 1.222 + set_days(ctx, val % 10 * 10); 1.223 + break; 1.224 + case ONE_MNTH_DIGT: 1.225 + set_months(ctx, val % 10); 1.226 + break; 1.227 + case TEN_MNTH_DIGT: 1.228 + set_months(ctx, val % 10 * 10); 1.229 + break; 1.230 + case ONE_YR_DIGT: 1.231 + set_years(ctx, val % 10); 1.232 + break; 1.233 + case TEN_YR_DIGT: 1.234 + set_years(ctx, val % 10 * 10); 1.235 + break; 1.236 + case WEEK_DAY: 1.237 + set_weekday(ctx, val % 10); 1.238 + break; 1.239 + case TOUT_CONTROL: 1.240 + break; 1.241 + case PROTECT_KEY: 1.242 + break; 1.243 + case RTC_STATUS: 1.244 + break; 1.245 + default: 1.246 + break; 1.247 + } 1.248 + }else{ 1.249 + LOG("\n"); 1.250 + } 1.251 +}