Created
February 3, 2017 00:51
-
-
Save mattwilliamson/3ea18635b29e14c5db54aea90b8e610c to your computer and use it in GitHub Desktop.
Squeeze two floats into two uint16_t's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void writeRtcLatLng(void) | |
{ | |
uint32_t tmp; | |
// RTC Backup registers have the first 2 bytes reserved. So we get to use the second half of each. | |
// Write lat tp first two backup registers | |
tmp = *(uint32_t*)⪫ | |
rtcRegWrite(RTC_BKP_DR6, tmp >> 16); | |
rtcRegWrite(RTC_BKP_DR7, tmp & 0xffff); | |
// Read lon from third and fourth backup registers | |
tmp = *(uint32_t*)&lng; | |
rtcRegWrite(RTC_BKP_DR8, tmp >> 16); | |
rtcRegWrite(RTC_BKP_DR9, tmp & 0xffff); | |
printf("[readRtcLatLng] wrote to RTC backup registers lat: %f lng: %f\r\n", lat, lng); | |
} | |
void readRtcLatLng(void) | |
{ | |
uint32_t tmp; | |
// RTC Backup registers have the first 2 bytes reserved. So we get to use the second half of each. | |
// Read lat from first two backup registers | |
tmp = rtcRegRead(RTC_BKP_DR6); | |
tmp <<= 16; | |
tmp |= rtcRegRead(RTC_BKP_DR7); | |
lat = *(float*)&tmp; | |
// Read lon from third and fourth backup registers | |
tmp = rtcRegRead(RTC_BKP_DR8); | |
tmp <<= 16; | |
tmp |= rtcRegRead(RTC_BKP_DR9); | |
lng = *(float*)&tmp; | |
printf("[readRtcLatLng] read from RTC backup registers lat: %f lng: %f\r\n", lat, lng); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment