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