Skip to content

Instantly share code, notes, and snippets.

@mattwilliamson
Created February 3, 2017 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwilliamson/3ea18635b29e14c5db54aea90b8e610c to your computer and use it in GitHub Desktop.
Save mattwilliamson/3ea18635b29e14c5db54aea90b8e610c to your computer and use it in GitHub Desktop.
Squeeze two floats into two uint16_t's
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