src/tc8250.c

Fri, 18 Apr 2014 01:34:20 -0600

author
andrew@localhost
date
Fri, 18 Apr 2014 01:34:20 -0600
changeset 151
b63a3999e2e7
permissions
-rw-r--r--

added RTC emulation (attempts to set the date are ignored, and the year is currently hardcoded to 1987 because UNIX PC SysV has a few Y2K bugs)

     1 #include <stdint.h>
     2 #include <stdbool.h>
     3 #include <malloc.h>
     4 #include <time.h>
     5 #include "tc8250.h"
     7 #ifndef TC8250_DEBUG
     8 #define NDEBUG
     9 #endif
    10 #include "utils.h"
    12 void tc8250_init(TC8250_CTX *ctx)
    13 {
    14 	ctx->chip_enable = false;
    15 	ctx->address_latch_enable = false;
    16 	ctx->write_enable = false;
    17 	ctx->address = 0;
    18 }
    20 void tc8250_set_chip_enable(TC8250_CTX *ctx, bool enabled)
    21 {
    22 	LOG("tc8250_set_chip_enable %d\n", enabled);
    23 	ctx->chip_enable = enabled;
    24 }
    26 void tc8250_set_address_latch_enable(TC8250_CTX *ctx, bool enabled)
    27 {
    28 	LOG("tc8250_set_address_latch_enable %d\n", enabled);
    29 	ctx->address_latch_enable = enabled;
    30 }
    32 void tc8250_set_write_enable(TC8250_CTX *ctx, bool enabled)
    33 {
    34 	LOG("tc8250_set_write_enable %d\n", enabled);
    35 	ctx->write_enable = enabled;
    36 }
    38 uint8_t get_second(TC8250_CTX *ctx)
    39 {
    40 	time_t t;
    41 	struct tm g;
    42 	uint8_t ret;
    43 	t = time(NULL);
    44 	gmtime_r(&t, &g);
    45 	ret = g.tm_sec;
    46 	return (ret);
    47 }
    49 uint8_t get_minute(TC8250_CTX *ctx)
    50 {
    51 	time_t t;
    52 	struct tm g;
    53 	uint8_t ret;
    54 	t = time(NULL);
    55 	gmtime_r(&t, &g);
    56 	ret = g.tm_min;
    57 	return (ret);
    58 }
    60 uint8_t get_hour(TC8250_CTX *ctx)
    61 {
    62 	time_t t;
    63 	struct tm g;
    64 	uint8_t ret;
    65 	t = time(NULL);
    66 	gmtime_r(&t, &g);
    67 	ret = g.tm_hour;
    68 	return (ret);
    69 }
    71 uint8_t get_day(TC8250_CTX *ctx)
    72 {
    73 	time_t t;
    74 	struct tm g;
    75 	uint8_t ret;
    76 	t = time(NULL);
    77 	gmtime_r(&t, &g);
    78 	ret = g.tm_mday;
    79 	return (ret);
    80 }
    82 uint8_t get_month(TC8250_CTX *ctx)
    83 {
    84 	time_t t;
    85 	struct tm g;
    86 	uint8_t ret;
    87 	t = time(NULL);
    88 	gmtime_r(&t, &g);
    89 	ret = g.tm_mon;
    90 	return (ret);
    91 }
    93 uint8_t get_year(TC8250_CTX *ctx)
    94 {
    95 	/*time_t t;
    96 	struct tm g;
    97 	uint8_t ret;
    98 	t = time(NULL);
    99 	gmtime_r(&t, &g);
   100 	ret = g.tm_year;
   101 	return (ret);*/
   102 	return (87);
   103 }
   105 uint8_t get_weekday(TC8250_CTX *ctx)
   106 {
   107 	time_t t;
   108 	struct tm g;
   109 	uint8_t ret;
   110 	t = time(NULL);
   111 	gmtime_r(&t, &g);
   112 	ret = g.tm_wday;
   113 	return (ret);
   114 }
   116 uint8_t tc8250_read_reg(TC8250_CTX *ctx)
   117 {
   118 	LOG("tc8250_read_reg %x\n", ctx->address);
   119 	switch (ctx->address){
   120 		case ONE_SEC_DIGT:
   121 			return (get_second(ctx) % 10);
   122 		case TEN_SEC_DIGT:
   123 			return (get_second(ctx) / 10);
   124 		case ONE_MIN_DIGT:
   125 			return (get_minute(ctx) % 10);
   126 		case TEN_MIN_DIGT:
   127 			return (get_minute(ctx) / 10);
   128 		case ONE_HR_DIGT:
   129 			return (get_hour(ctx) % 10);
   130 		case TEN_HR_DIGT:
   131 			return (get_hour(ctx) / 10);
   132 		case ONE_DAY_DIGT:
   133 			return (get_day(ctx) % 10);
   134 		case TEN_DAY_DIGT:
   135 			return (get_day(ctx) / 10);
   136 		case ONE_MNTH_DIGT:
   137 			return (get_month(ctx) % 10);
   138 		case TEN_MNTH_DIGT:
   139 			return (get_month(ctx) / 10);
   140 		case ONE_YR_DIGT:
   141 			return (get_year(ctx) % 10);
   142 		case TEN_YR_DIGT:
   143 			return (get_year(ctx) / 10);
   144 		case WEEK_DAY:
   145 			return (get_weekday(ctx) / 10);
   146 		case TOUT_CONTROL:
   147 			return (0);
   148 		case PROTECT_KEY:
   149 			return (0);
   150 		case RTC_STATUS:
   151 			return (0);
   152 		default:
   153 			return (0);
   154 	}
   155 }
   157 void set_seconds(TC8250_CTX *ctx, uint8_t val)
   158 {
   159 }
   161 void set_minutes(TC8250_CTX *ctx, uint8_t val)
   162 {
   163 }
   165 void set_hours(TC8250_CTX *ctx, uint8_t val)
   166 {
   167 }
   169 void set_days(TC8250_CTX *ctx, uint8_t val)
   170 {
   171 }
   173 void set_months(TC8250_CTX *ctx, uint8_t val)
   174 {
   175 }
   177 void set_years(TC8250_CTX *ctx, uint8_t val)
   178 {
   179 }
   181 void set_weekday(TC8250_CTX *ctx, uint8_t val)
   182 {
   183 }
   185 void tc8250_write_reg(TC8250_CTX *ctx, uint8_t val)
   186 {
   187 	LOG("tc8250_write_reg %x", val);
   188 	if (ctx->address_latch_enable){
   189 		LOG(" address\n");
   190 		ctx->address = val;
   191 		return;
   192 	}
   193 	if (ctx->chip_enable){
   194 		LOG(" %x\n", ctx->address);
   195 		switch (ctx->address){
   196 			case ONE_SEC_DIGT:
   197 				set_seconds(ctx, val % 10);
   198 				break;
   199 			case TEN_SEC_DIGT:
   200 				set_seconds(ctx, val % 10 * 10);
   201 				break;
   202 			case ONE_MIN_DIGT:
   203 				set_minutes(ctx, val % 10);
   204 				break;
   205 			case TEN_MIN_DIGT:
   206 				set_minutes(ctx, val % 10 * 10);
   207 				break;
   208 			case ONE_HR_DIGT:
   209 				set_hours(ctx, val % 10);
   210 				break;
   211 			case TEN_HR_DIGT:
   212 				set_hours(ctx, val % 10 * 10);
   213 				break;
   214 			case ONE_DAY_DIGT:
   215 				set_days(ctx, val % 10);
   216 				break;
   217 			case TEN_DAY_DIGT:
   218 				set_days(ctx, val % 10 * 10);
   219 				break;
   220 			case ONE_MNTH_DIGT:
   221 				set_months(ctx, val % 10);
   222 				break;
   223 			case TEN_MNTH_DIGT:
   224 				set_months(ctx, val % 10 * 10);
   225 				break;
   226 			case ONE_YR_DIGT:
   227 				set_years(ctx, val % 10);
   228 				break;
   229 			case TEN_YR_DIGT:
   230 				set_years(ctx, val % 10 * 10);
   231 				break;
   232 			case WEEK_DAY:
   233 				set_weekday(ctx, val % 10);
   234 				break;
   235 			case TOUT_CONTROL:
   236 				break;
   237 			case PROTECT_KEY:
   238 				break;
   239 			case RTC_STATUS:
   240 				break;
   241 			default:
   242 				break;
   243 		}
   244 	}else{
   245 		LOG("\n");
   246 	}
   247 }