1 | // 25AA040A instructions
|
2 | const uint8_t EEPROM_READ = 0x03; // 03h Normal Read Data
|
3 | const uint8_t EEPROM_WRITE = 0x02; // 02h Page Program
|
4 | const uint8_t EEPROM_WRDI = 0x04; // 04h Write Disable
|
5 | const uint8_t EEPROM_WREN = 0x06; // 06h Write Enable
|
6 | const uint8_t EEPROM_RDSR = 0x05; // 05h Read Status Register
|
7 | const uint8_t EEPROM_WRSR = 0x01; // 01h Write Status Register
|
8 | const uint8_t EEPROM_RDSR2 = 0x35; // Read Status Register 2
|
9 | const uint8_t EEPROM_RDSR3 = 0x15; // Read Status Register 3
|
10 | const uint8_t RESETENABELE = 0x66;
|
11 | const uint8_t RESETDIVICE = 0x99;
|
12 | const uint8_t CHIPERASE = 0x60;
|
13 | const uint8_t ID = 0x9F;
|
14 | const uint8_t ERASE = 0x44;
|
15 | const uint8_t BLOCKERASE = 0x20;
|
16 |
|
17 | // Variablen
|
18 | char spi_buf[20];
|
19 | uint8_t addr[3];
|
20 | uint8_t wip;
|
21 |
|
22 | // CS pin should default high
|
23 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
|
24 |
|
25 | // Enable write enable latch (allow write operations)
|
26 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
27 | memStat = HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_WREN, 1, 100);
|
28 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
|
29 |
|
30 | // Read status register
|
31 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
32 | memStat = HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_RDSR, 1, 100);
|
33 | memStat = HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 1, 100);
|
34 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
|
35 |
|
36 | // Test bytes to write to EEPROM
|
37 | spi_buf[0] = 0xBB;
|
38 | spi_buf[1] = 0xCD;
|
39 | spi_buf[2] = 0xEF;
|
40 |
|
41 | // Set starting address
|
42 | addr[0] = 0x05;
|
43 |
|
44 | // Write 3 bytes starting at given address
|
45 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
46 | HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_WRITE, 1, 100);
|
47 | HAL_SPI_Transmit(&hspi1, (uint8_t *)&addr, 3, 100);
|
48 | HAL_SPI_Transmit(&hspi1, (uint8_t *)spi_buf, 3, 100);
|
49 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
|
50 |
|
51 | // Clear buffer
|
52 | spi_buf[0] = 0;
|
53 | spi_buf[1] = 0;
|
54 | spi_buf[2] = 0;
|
55 |
|
56 | // Wait until WIP bit is cleared
|
57 | wip = 1;
|
58 | while (wip)
|
59 | {
|
60 | // Read status register
|
61 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
62 | HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_RDSR, 1, 100);
|
63 | HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 1, 100);
|
64 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
|
65 |
|
66 | // Mask out WIP bit
|
67 | wip = spi_buf[0] & 0b00000001;
|
68 | }
|
69 |
|
70 | // Read the 3 bytes back
|
71 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
72 | HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_READ, 1, 100);
|
73 | HAL_SPI_Transmit(&hspi1, (uint8_t *)&addr, 3, 100);
|
74 | HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 3, 100);
|
75 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
|
76 |
|
77 | // Read status register
|
78 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
79 | HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_RDSR, 1, 100);
|
80 | HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 1, 100);
|
81 | HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
|