Skip to content

Instantly share code, notes, and snippets.

@embedded4ever
Created April 5, 2021 07:47
Show Gist options
  • Save embedded4ever/03bf28cdef5e01773a288185661334c9 to your computer and use it in GitHub Desktop.
Save embedded4ever/03bf28cdef5e01773a288185661334c9 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
/*
Time interface:
Witdh : 32 bits
Bit Range Name Desciription
0 - 4 Day 00001(1d) min
11111(31d) max
5 - 8 Mounth 0001(1d) min
1101(12d) max
9 - 14 Year 111111(63) max
15 - 19 Hour MSB bit for AM or PM 11101(12d) max
20 - 25 Minute 111111(63d) max
26 - 31 Second 111111(63d) max
*/
static uint32_t marshall_time ( unsigned int d,
unsigned int m,
unsigned int y,
unsigned int h,
unsigned int am_or_pm,
unsigned int mi,
unsigned int s )
{
uint32_t ret_time = 0;
ret_time |= (d);
ret_time |= (m << 5);
ret_time |= (y << 9);
ret_time |= (h << 15);
ret_time |= (am_or_pm << 19);
ret_time |= (mi << 20);
ret_time |= (s << 26);
return ret_time;
}
static uint32_t unmarshall_time (uint32_t time)
{
unsigned int d, m, y, h, min, s = 0;
d = time & (31);
m = (time & (15 << 5)) >> 5;
y = (time & (63 << 9)) >> 9;
h = (time & (15 << 15)) >> 15;
int am_or_pm = (time & (1 << 19)) >> 19;
min = (time & (63 << 20)) >> 20;
s = (time & (63 << 26)) >> 26;
printf("\r\n D : %u", d);
printf("\r\n M : %u", m);
printf("\r\n Y : %u", y);
printf("\r\n H : %u", h);
printf("\r\n am_or_pm %u", am_or_pm);
printf("\r\n Mi : %u", min);
printf("\r\n S : %u", s);
}
int main(int argc, char* argv[])
{
uint32_t ret_time = marshall_time(5, 4, 21, 9, 1, 32, 53);
unmarshall_time(ret_time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment