Callisto4Bootloader: PfmLib/platform_eeprom.c Source File

Callisto4Bootloader

platform_eeprom.c
Go to the documentation of this file.
1
14 /*******************************************************************************
15 * Includes
16 ******************************************************************************/
17
18 #include "platform-eeprom-header.h"
19
20 #include "at24c01d.h"
21 #include "fsl_lpi2c.h"
22 #include "fsl_clock.h"
23 #include "fsl_iomuxc.h"
24
25 #include <stdio.h>
26
27 /*******************************************************************************
28 * External Definitions
29 ******************************************************************************/
30
31 /*******************************************************************************
32 * Defines
33 ******************************************************************************/
34
35
36 #define LPI2C_CLOCK_SOURCE_SELECT (0U)
37 #define LPI2C_CLOCK_SOURCE_DIVIDER (5U)
38 #define LPI2C_MASTER_CLOCK_FREQUENCY ((CLOCK_GetFreq(kCLOCK_Usb1PllClk) / 8) / (LPI2C_CLOCK_SOURCE_DIVIDER + 1U))
39
40 #define LPI2C2_MASTER ((LPI2C_Type *)LPI2C2_BASE)
41 #define LPI2C2_BAUDRATE 100000U
42
43 /*******************************************************************************
44 * Structures
45 ******************************************************************************/
46
47 /*******************************************************************************
48 * Prototypes
49 ******************************************************************************/
50
51 /*******************************************************************************
52 * Variables
53 ******************************************************************************/
54
55 /*******************************************************************************
56 * Code
57 ******************************************************************************/
58
59 /*******************************************************************************
60 * Interface Implementation
61 ******************************************************************************/
62
63 /*******************************************************************************
64 * Local Functions
65 ******************************************************************************/
66
76 uint32_t eeprom_header_calc_checksum(void *pvBase, uint32_t uiNumBytes)
77 {
78 uint32_t * puiPtr = (uint32_t *) pvBase;
79 uint32_t uiChecksum;
80 uint32_t uiNumWords;
81 uint32_t i;
82
83 uiChecksum = 0U;
84 uiNumWords = ((uiNumBytes + 3) >> 2);
85 for (i = uiNumWords; i; i--)
86 {
87 uiChecksum += *puiPtr++;
88 }
89
90 return uiChecksum;
91 }
92
101 bool eeprom_header_read(stEEPROM_Header_t * pstHeader)
102 {
103 int32_t result;
104 uint32_t uiChecksum;
105
106 result = At24c01d_Read(PLATFORM_EEPROM_HEADER_OFFSET, (uint8_t *)pstHeader, sizeof(stEEPROM_Header_t));
107
108 //Check that we have read all the bytes in the header, not the length of the pointer (4 bytes)
109 if (result < (int32_t) sizeof(stEEPROM_Header_t))
110 {
111 // Unable to read full header from EEPROM, so inform user via serial and return
112 printf( "Error: EEPROM config header could not be read in %ld vs %d\r\n", result, sizeof(stEEPROM_Header_t));
113 return false;
114 }
115
116 if ((pstHeader->u16HeaderLength < 32U) || (pstHeader->u16HeaderLength > AT24C01D_SIZE_BYTES))
117 {
118 // Header length malformed, so inform user via serial and return
119 printf( "Error: EEPROM config header had unexpected length (%d)\r\n", (int) pstHeader->u16HeaderLength);
120 return false;
121 }
122
123 if (pstHeader->uiHeaderMagic != PLATFORM_EEPROM_HEADER_MAGIC)
124 {
125 // Header magic malformed, so inform user via serial and return
126 printf( "Error: EEPROM config header had missing magic number (%08x)\r\n", (int) pstHeader->uiHeaderMagic);
127 return false;
128 }
129
130 uiChecksum = eeprom_header_calc_checksum(pstHeader, pstHeader->u16HeaderLength);
131 if (uiChecksum != 0U)
132 {
133 // Header fails checksum test, so inform user via serial and return
134 printf( "Error: EEPROM config header failed checksum test\r\n");
135 return false;
136 }
137
138 #ifdef DEBUG
139 printf( "EEPROM config header is valid!\r\n" );
140 #endif
141
142 return true;
143 }
144
153 bool eeprom_header_write(const stEEPROM_Header_t * pstHeader)
154 {
155 int32_t result; // holds number of bytes successfully written out
156
157 result = At24c01d_Write(PLATFORM_EEPROM_HEADER_OFFSET, (uint8_t *)pstHeader, sizeof(stEEPROM_Header_t));
158 printf("%s wrote %ld vs %d\r\n", __PRETTY_FUNCTION__, result, sizeof(stEEPROM_Header_t));
159 //Check that we have written all the bytes in the header, not the length of the pointer (4 bytes)
160 return (result == (int32_t) sizeof(stEEPROM_Header_t));
161 }
162
168 void InitialiseI2CMasterClock(void)
169 {
170 /* Master clock setting for I2C */
171 CLOCK_SetMux(kCLOCK_Lpi2cMux, LPI2C_CLOCK_SOURCE_SELECT);
172 CLOCK_SetDiv(kCLOCK_Lpi2cDiv, LPI2C_CLOCK_SOURCE_DIVIDER);
173 }
174
180 void InitialiseI2C2(void)
181 {
182 lpi2c_master_config_t masterConfig;
183
184 IOMUXC_SetPinConfig(
185 IOMUXC_GPIO_B0_04_LPI2C2_SCL, /* GPIO_B0_04 PAD functional properties : */
186 0xD8B0U); /* Slew Rate Field: Slow Slew Rate
187 Drive Strength Field: R0/6
188 Speed Field: medium(100MHz)
189 Open Drain Enable Field: Open Drain Enabled
190 Pull / Keep Enable Field: Pull/Keeper Enabled
191 Pull / Keep Select Field: Keeper
192 Pull Up / Down Config. Field: 22K Ohm Pull Up
193 Hyst. Enable Field: Hysteresis Disabled */
194 IOMUXC_SetPinConfig(
195 IOMUXC_GPIO_B0_05_LPI2C2_SDA, /* GPIO_B0_05 PAD functional properties : */
196 0xD8B0U); /* Slew Rate Field: Slow Slew Rate
197 Drive Strength Field: R0/6
198 Speed Field: medium(100MHz)
199 Open Drain Enable Field: Open Drain Enabled
200 Pull / Keep Enable Field: Pull/Keeper Enabled
201 Pull / Keep Select Field: Keeper
202 Pull Up / Down Config. Field: 22K Ohm Pull Up
203 Hyst. Enable Field: Hysteresis Disabled */
204
205 // Note: Jeremy Mills found that these I2C pins should have their input path set to 1U.
206 IOMUXC_SetPinMux(
207 IOMUXC_GPIO_B0_04_LPI2C2_SCL, /* GPIO_B0_04 is configured as LPI2C2_SCL */
208 1U); /* Software Input On Field: Input Path is always on */
209 IOMUXC_SetPinMux(
210 IOMUXC_GPIO_B0_05_LPI2C2_SDA, /* GPIO_B0_05 is configured as LPI2C2_SDA */
211 1U); /* Software Input On Field: Input Path is always on */
212
213 /*
214 * masterConfig.debugEnable = false;
215 * masterConfig.ignoreAck = false;
216 * masterConfig.pinConfig = kLPI2C_2PinOpenDrain;
217 * masterConfig.baudRate_Hz = 100000U;
218 * masterConfig.busIdleTimeout_ns = 0;
219 * masterConfig.pinLowTimeout_ns = 0;
220 * masterConfig.sdaGlitchFilterWidth_ns = 0;
221 * masterConfig.sclGlitchFilterWidth_ns = 0;
222 */
223 LPI2C_MasterGetDefaultConfig(&masterConfig);
224
225 // Change the default baudrate configuration
226 masterConfig.baudRate_Hz = LPI2C2_BAUDRATE;
227
228 // Initialise the LPI2C master peripheral
229 LPI2C_MasterInit(LPI2C2_MASTER, &masterConfig, LPI2C_MASTER_CLOCK_FREQUENCY);
230 }
LPI2C_MASTER_CLOCK_FREQUENCY
#define LPI2C_MASTER_CLOCK_FREQUENCY
Definition: platform_eeprom.c:35
eeprom_header_calc_checksum
uint32_t eeprom_header_calc_checksum(void *pvBase, uint32_t uiNumBytes)
Calculate a config header checksum.
Definition: platform_eeprom.c:67
InitialiseI2C2
void InitialiseI2C2(void)
Initialise the I2C2 device.
Definition: platform_eeprom.c:171
LPI2C_CLOCK_SOURCE_DIVIDER
#define LPI2C_CLOCK_SOURCE_DIVIDER
Clock divider for master lpi2c clock source.
Definition: platform_eeprom.c:34
eeprom_header_write
bool eeprom_header_write(const stEEPROM_Header_t *pstHeader)
Write a platform configuration block out to the EEPROM.
Definition: platform_eeprom.c:144
i
volatile int i
Definition: excep_test.c:17
LPI2C2_BAUDRATE
#define LPI2C2_BAUDRATE
Definition: platform_eeprom.c:38
LPI2C2_MASTER
#define LPI2C2_MASTER
Definition: platform_eeprom.c:37
eeprom_header_read
bool eeprom_header_read(stEEPROM_Header_t *pstHeader)
Read an EEPROM platform configuration block.
Definition: platform_eeprom.c:92
InitialiseI2CMasterClock
void InitialiseI2CMasterClock(void)
Initialise the I2C master clock.
Definition: platform_eeprom.c:159
LPI2C_CLOCK_SOURCE_SELECT
#define LPI2C_CLOCK_SOURCE_SELECT
Select USB1 PLL (480 MHz) as master lpi2c clock source.
Definition: platform_eeprom.c:33