5
+ − 1 /**
+ − 2 ******************************************************************************
+ − 3 * @copyright heinrichs weikamp
+ − 4 * @file data_central_mini.c - bootloader only -
36
+ − 5 * @author heinrichs weikamp gmbh
5
+ − 6 * @date 10-November-2014
+ − 7 * @version V1.0.3
+ − 8 * @since 10-Nov-2014
+ − 9 * @brief
+ − 10 * @bug
+ − 11 * @warning
+ − 12 @verbatim
+ − 13
+ − 14 @endverbatim
+ − 15 ******************************************************************************
+ − 16 * @attention
+ − 17 *
+ − 18 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2>
+ − 19 *
+ − 20 ******************************************************************************
+ − 21 */
+ − 22
+ − 23 /* Includes ------------------------------------------------------------------*/
+ − 24 #include <string.h>
+ − 25 #include "data_central.h"
+ − 26 #include "stm32f4xx_hal.h"
+ − 27 #include "crcmodel.h"
+ − 28
+ − 29 void translateDate(uint32_t datetmpreg, RTC_DateTypeDef *sDate)
+ − 30 {
+ − 31 datetmpreg = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
+ − 32
+ − 33 /* Fill the structure fields with the read parameters */
+ − 34 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
+ − 35 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
+ − 36 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
+ − 37 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
+ − 38
+ − 39 /* Convert the date structure parameters to Binary format */
+ − 40 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
+ − 41 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
+ − 42 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
+ − 43 }
+ − 44
+ − 45 void translateTime(uint32_t tmpreg, RTC_TimeTypeDef *sTime)
+ − 46 {
+ − 47 tmpreg = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
+ − 48
+ − 49 /* Fill the structure fields with the read parameters */
+ − 50 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
+ − 51 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
+ − 52 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
+ − 53 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
+ − 54
+ − 55 /* Convert the time structure parameters to Binary format */
+ − 56 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
+ − 57 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
+ − 58 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
+ − 59 sTime->SubSeconds = 0;
+ − 60 }
+ − 61
+ − 62
+ − 63 /* This is derived from crc32b but does table lookup. First the table
+ − 64 itself is calculated, if it has not yet been set up.
+ − 65 Not counting the table setup (which would probably be a separate
+ − 66 function), when compiled to Cyclops with GCC, this function executes in
+ − 67 7 + 13n instructions, where n is the number of bytes in the input
+ − 68 message. It should be doable in 4 + 9n instructions. In any case, two
+ − 69 of the 13 or 9 instrucions are load byte.
+ − 70 This is Figure 14-7 in the text. */
+ − 71
36
+ − 72 /* http://www.hackersdelight.org/ i guess ;-) *hw */
5
+ − 73
+ − 74 uint32_t crc32c_checksum(uint8_t* message, uint16_t length, uint8_t* message2, uint16_t length2) {
+ − 75 int i, j;
+ − 76 uint32_t byte, crc, mask;
+ − 77 static unsigned int table[256] = {0};
+ − 78
+ − 79 /* Set up the table, if necessary. */
+ − 80 if (table[1] == 0) {
+ − 81 for (byte = 0; byte <= 255; byte++) {
+ − 82 crc = byte;
+ − 83 for (j = 7; j >= 0; j--) { // Do eight times.
+ − 84 mask = -(crc & 1);
+ − 85 crc = (crc >> 1) ^ (0xEDB88320 & mask);
+ − 86 }
+ − 87 table[byte] = crc;
+ − 88 }
+ − 89 }
+ − 90
+ − 91 /* Through with table setup, now calculate the CRC. */
+ − 92 i = 0;
+ − 93 crc = 0xFFFFFFFF;
+ − 94 while (length--) {
+ − 95 byte = message[i];
+ − 96 crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF];
+ − 97 i = i + 1;
+ − 98 }
+ − 99 if(length2)
+ − 100 {
+ − 101 i = 0;
+ − 102 while (length2--) {
+ − 103 byte = message2[i];
+ − 104 crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF];
+ − 105 i = i + 1;
+ − 106 }
+ − 107 }
+ − 108 return ~crc;
+ − 109 }
+ − 110
+ − 111
+ − 112 uint32_t CRC_CalcBlockCRC_moreThan768000(uint32_t *buffer1, uint32_t *buffer2, uint32_t words)
+ − 113 {
+ − 114 cm_t crc_model;
+ − 115 uint32_t word_to_do;
+ − 116 uint8_t byte_to_do;
+ − 117 int i;
+ − 118
+ − 119 // Values for the STM32F generator.
+ − 120
+ − 121 crc_model.cm_width = 32; // 32-bit CRC
+ − 122 crc_model.cm_poly = 0x04C11DB7; // CRC-32 polynomial
+ − 123 crc_model.cm_init = 0xFFFFFFFF; // CRC initialized to 1's
+ − 124 crc_model.cm_refin = FALSE; // CRC calculated MSB first
+ − 125 crc_model.cm_refot = FALSE; // Final result is not bit-reversed
+ − 126 crc_model.cm_xorot = 0x00000000; // Final result XOR'ed with this
+ − 127
+ − 128 cm_ini(&crc_model);
+ − 129
+ − 130 while (words--)
+ − 131 {
+ − 132 // The STM32F10x hardware does 32-bit words at a time!!!
+ − 133 if(words > (768000/4))
+ − 134 word_to_do = *buffer2++;
+ − 135 else
+ − 136 word_to_do = *buffer1++;
+ − 137
+ − 138 // Do all bytes in the 32-bit word.
+ − 139
+ − 140 for (i = 0; i < sizeof(word_to_do); i++)
+ − 141 {
+ − 142 // We calculate a *byte* at a time. If the CRC is MSB first we
+ − 143 // do the next MS byte and vica-versa.
+ − 144
+ − 145 if (crc_model.cm_refin == FALSE)
+ − 146 {
+ − 147 // MSB first. Do the next MS byte.
+ − 148
+ − 149 byte_to_do = (uint8_t) ((word_to_do & 0xFF000000) >> 24);
+ − 150 word_to_do <<= 8;
+ − 151 }
+ − 152 else
+ − 153 {
+ − 154 // LSB first. Do the next LS byte.
+ − 155
+ − 156 byte_to_do = (uint8_t) (word_to_do & 0x000000FF);
+ − 157 word_to_do >>= 8;
+ − 158 }
+ − 159
+ − 160 cm_nxt(&crc_model, byte_to_do);
+ − 161 }
+ − 162 }
+ − 163
+ − 164 // Return the final result.
+ − 165
+ − 166 return (cm_crc(&crc_model));
+ − 167 }
+ − 168
+ − 169
+ − 170 uint32_t CRC_CalcBlockCRC(uint32_t *buffer, uint32_t words)
+ − 171 {
+ − 172 cm_t crc_model;
+ − 173 uint32_t word_to_do;
+ − 174 uint8_t byte_to_do;
+ − 175 int i;
+ − 176
+ − 177 // Values for the STM32F generator.
+ − 178
+ − 179 crc_model.cm_width = 32; // 32-bit CRC
+ − 180 crc_model.cm_poly = 0x04C11DB7; // CRC-32 polynomial
+ − 181 crc_model.cm_init = 0xFFFFFFFF; // CRC initialized to 1's
+ − 182 crc_model.cm_refin = FALSE; // CRC calculated MSB first
+ − 183 crc_model.cm_refot = FALSE; // Final result is not bit-reversed
+ − 184 crc_model.cm_xorot = 0x00000000; // Final result XOR'ed with this
+ − 185
+ − 186 cm_ini(&crc_model);
+ − 187
+ − 188 while (words--)
+ − 189 {
+ − 190 // The STM32F10x hardware does 32-bit words at a time!!!
+ − 191
+ − 192 word_to_do = *buffer++;
+ − 193
+ − 194 // Do all bytes in the 32-bit word.
+ − 195
+ − 196 for (i = 0; i < sizeof(word_to_do); i++)
+ − 197 {
+ − 198 // We calculate a *byte* at a time. If the CRC is MSB first we
+ − 199 // do the next MS byte and vica-versa.
+ − 200
+ − 201 if (crc_model.cm_refin == FALSE)
+ − 202 {
+ − 203 // MSB first. Do the next MS byte.
+ − 204
+ − 205 byte_to_do = (uint8_t) ((word_to_do & 0xFF000000) >> 24);
+ − 206 word_to_do <<= 8;
+ − 207 }
+ − 208 else
+ − 209 {
+ − 210 // LSB first. Do the next LS byte.
+ − 211
+ − 212 byte_to_do = (uint8_t) (word_to_do & 0x000000FF);
+ − 213 word_to_do >>= 8;
+ − 214 }
+ − 215
+ − 216 cm_nxt(&crc_model, byte_to_do);
+ − 217 }
+ − 218 }
+ − 219
+ − 220 // Return the final result.
+ − 221
+ − 222 return (cm_crc(&crc_model));
+ − 223 }
+ − 224