comparison Discovery/Src/externLogbookFlash.c @ 273:5fe136480a47

Merged in janlmulder/ostc4/write-from-sim (pull request #20) Cleanup and debug capabilty to write logbook from simulator
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Sun, 28 Apr 2019 07:05:23 +0000
parents 1b9847d40e81
children c7e665e0b08f
comparison
equal deleted inserted replaced
267:cf6ad20380fb 273:5fe136480a47
113 113
114 114
115 /* Exported variables --------------------------------------------------------*/ 115 /* Exported variables --------------------------------------------------------*/
116 116
117 /* Private variables ---------------------------------------------------------*/ 117 /* Private variables ---------------------------------------------------------*/
118 uint32_t actualAddress = 0; 118 static uint32_t actualAddress = 0;
119 uint32_t entryPoint = 0; 119 static uint32_t entryPoint = 0;
120 120
121 uint32_t actualPointerHeader = 0; 121 static uint32_t actualPointerHeader = 0;
122 uint32_t actualPointerSample = 0; 122 static uint32_t actualPointerSample = 0;
123 uint32_t LengthLeftSampleRead = 0; 123 static uint32_t LengthLeftSampleRead = 0;
124 uint32_t actualPointerDevicedata = 0; 124 static uint32_t actualPointerDevicedata = 0;
125 uint32_t actualPointerVPM = 0; 125 static uint32_t actualPointerVPM = 0;
126 uint32_t actualPointerSettings = 0; 126 static uint32_t actualPointerSettings = 0;
127 uint32_t actualPointerFirmware = 0; 127 static uint32_t actualPointerFirmware = 0;
128 uint32_t actualPointerFirmware2 = 0; 128 static uint32_t actualPointerFirmware2 = 0;
129 129
130 /* Private function prototypes -----------------------------------------------*/ 130 /* Private function prototypes -----------------------------------------------*/
131 void chip_unselect(void); 131 static void chip_unselect(void);
132 void chip_select(void); 132 static void chip_select(void);
133 void error_led_on(void); 133 static void error_led_on(void);
134 void error_led_off(void); 134 static void error_led_off(void);
135 135
136 void write_spi(uint8_t data, uint8_t unselect_CS_afterwards); 136 static void write_spi(uint8_t data, uint8_t unselect_CS_afterwards);
137 uint8_t read_spi(uint8_t unselect_CS_afterwards); 137 static uint8_t read_spi(uint8_t unselect_CS_afterwards);
138 void write_address(uint8_t unselect_CS_afterwards); 138 static void write_address(uint8_t unselect_CS_afterwards);
139 static void Error_Handler_extflash(void); 139 static void Error_Handler_extflash(void);
140 static void wait_chip_not_busy(void); 140 static void wait_chip_not_busy(void);
141 void ext_flash_incf_address(uint8_t type); 141 static void ext_flash_incf_address(uint8_t type);
142 //void ext_flash_incf_address_ring(void); 142 //void ext_flash_incf_address_ring(void);
143 void ext_flash_decf_address_ring(uint8_t type); 143 static void ext_flash_decf_address_ring(uint8_t type);
144 144
145 void ext_flash_erase4kB(void); 145 static void ext_flash_erase4kB(void);
146 void ext_flash_erase32kB(void); 146 static void ext_flash_erase32kB(void);
147 void ext_flash_erase64kB(void); 147 static void ext_flash_erase64kB(void);
148 uint8_t ext_flash_erase_if_on_page_start(void); 148 static uint8_t ext_flash_erase_if_on_page_start(void);
149 149
150 void ef_write_block(uint8_t * sendByte, uint32_t length, uint8_t type, uint8_t do_not_erase); 150 static void ef_write_block(uint8_t * sendByte, uint32_t length, uint8_t type, uint8_t do_not_erase);
151 151
152 void ext_flash_read_block(uint8_t *getByte, uint8_t type); 152 static void ext_flash_read_block(uint8_t *getByte, uint8_t type);
153 void ext_flash_read_block_multi(void *getByte, uint32_t size, uint8_t type); 153 static void ext_flash_read_block_multi(void *getByte, uint32_t size, uint8_t type);
154 void ext_flash_read_block_stop(void); 154 static void ext_flash_read_block_stop(void);
155 155
156 static void ef_hw_rough_delay_us(uint32_t delayUs); 156 static void ef_hw_rough_delay_us(uint32_t delayUs);
157 static void ef_erase_64K(uint32_t blocks); 157 static void ef_erase_64K(uint32_t blocks);
158 158
159 void ext_flash_overwrite_sample_without_erase(uint8_t *pSample, uint16_t length); 159 static void ext_flash_overwrite_sample_without_erase(uint8_t *pSample, uint16_t length);
160 160
161 void ext_flash_disable_protection(void); 161 static void ext_flash_disable_protection(void);
162 162
163 _Bool ext_flash_test_remaining_space_of_page_empty(uint32_t pointer, uint16_t length); 163 static _Bool ext_flash_test_remaining_space_of_page_empty(uint32_t pointer, uint16_t length);
164 void ext_flash_set_to_begin_of_next_page(uint32_t *pointer, uint8_t type); 164 static void ext_flash_set_to_begin_of_next_page(uint32_t *pointer, uint8_t type);
165 static void ext_flash_find_start(void);
165 166
166 167
167 /* Exported functions --------------------------------------------------------*/ 168 /* Exported functions --------------------------------------------------------*/
168 169
169 void ext_flash_write_firmware(uint8_t *pSample1, uint32_t length1)//, uint8_t *pSample2, uint32_t length2) 170 void ext_flash_write_firmware(uint8_t *pSample1, uint32_t length1)//, uint8_t *pSample2, uint32_t length2)
729 730
730 SSettings *settings = settingsGetPointer(); 731 SSettings *settings = settingsGetPointer();
731 settings->logFlashNextSampleStartAddress = actualPointerSample; 732 settings->logFlashNextSampleStartAddress = actualPointerSample;
732 } 733 }
733 734
734 void ext_flash_overwrite_sample_without_erase(uint8_t *pSample, uint16_t length) 735 static void ext_flash_overwrite_sample_without_erase(uint8_t *pSample, uint16_t length)
735 { 736 {
736 ef_write_block(pSample,length, EF_SAMPLE, 1); 737 ef_write_block(pSample,length, EF_SAMPLE, 1);
737 } 738 }
738 739
739 740
1007 // ext_flash_repair_dive_numbers_starting_count_helper 1008 // ext_flash_repair_dive_numbers_starting_count_helper
1008 /// @brief 1009 /// @brief
1009 /// @date 22-June-2016 1010 /// @date 22-June-2016
1010 1011
1011 // =============================================================================== 1012 // ===============================================================================
1012 uint16_t ext_flash_repair_dive_numbers_starting_count_helper(uint8_t *data, uint8_t *change64k, uint16_t startNumber, uint8_t lastLogId) 1013 static uint16_t ext_flash_repair_dive_numbers_starting_count_helper(uint8_t *data, uint8_t *change64k, uint16_t startNumber, uint8_t lastLogId)
1013 { 1014 {
1014 const uint32_t headerStep = 0x800; 1015 const uint32_t headerStep = 0x800;
1015 uint8_t actualLogId = 0; 1016 uint8_t actualLogId = 0;
1016 uint16_t oldNumber = 0; 1017 uint16_t oldNumber = 0;
1017 uint16_t actualNumber = 0; 1018 uint16_t actualNumber = 0;
1101 settingsGetPointer()->totalDiveCounter = lastCount; 1102 settingsGetPointer()->totalDiveCounter = lastCount;
1102 } 1103 }
1103 return lastCount; 1104 return lastCount;
1104 } 1105 }
1105 1106
1106 1107 /*
1107 void OLD_ext_flash_repair_SPECIAL_dive_numbers_starting_count_with(uint16_t startCount) 1108 void OLD_ext_flash_repair_SPECIAL_dive_numbers_starting_count_with(uint16_t startCount)
1108 { 1109 {
1109 uint16_t counterStorage[256]; 1110 uint16_t counterStorage[256];
1110 uint8_t start = 0xFF; 1111 uint8_t start = 0xFF;
1111 uint32_t logCopyDataPtr = 0; 1112 uint32_t logCopyDataPtr = 0;
1144 1145
1145 count = start + 1; 1146 count = start + 1;
1146 startAbsolute = settingsGetPointer()->lastDiveLogId; 1147 startAbsolute = settingsGetPointer()->lastDiveLogId;
1147 1148
1148 1149
1149 /* 1150
1150 if(start%2) 1151 if(start%2)
1151 { 1152 {
1152 if(counterStorage[start] != startCount) 1153 if(counterStorage[start] != startCount)
1153 { 1154 {
1154 // clear data 1155 // clear data
1181 actualAddress = HEADERSTART + (0x800 * (id-1)); 1182 actualAddress = HEADERSTART + (0x800 * (id-1));
1182 ef_write_block(data,0x800*2,EF_HEADER, 0); 1183 ef_write_block(data,0x800*2,EF_HEADER, 0);
1183 } 1184 }
1184 start--; 1185 start--;
1185 } 1186 }
1186 */ 1187
1187 // for(int count = start; count > -1; count -= 2) 1188 // for(int count = start; count > -1; count -= 2)
1188 1189
1189 while(count > 0) 1190 while(count > 0)
1190 { 1191 {
1191 // clear data 1192 // clear data
1287 } 1288 }
1288 } 1289 }
1289 releaseFrame(97,logCopyDataPtr); 1290 releaseFrame(97,logCopyDataPtr);
1290 settingsGetPointer()->totalDiveCounter = startCount; 1291 settingsGetPointer()->totalDiveCounter = startCount;
1291 } 1292 }
1292 1293 */
1293 1294
1294 // =============================================================================== 1295 // ===============================================================================
1295 // ext_flash_repair_dive_log 1296 // ext_flash_repair_dive_log
1296 /// @brief This function 1297 /// @brief This function
1297 /// does set 1298 /// does set
1334 } 1335 }
1335 ext_flash_find_start(); 1336 ext_flash_find_start();
1336 } 1337 }
1337 1338
1338 1339
1339 void ext_flash_find_start(void) 1340 static void ext_flash_find_start(void)
1340 { 1341 {
1341 uint8_t id; 1342 uint8_t id;
1342 uint8_t header1, header2; 1343 uint8_t header1, header2;
1343 convert_Type dataStart, dataEnd; 1344 convert_Type dataStart, dataEnd;
1344 1345
1420 } 1421 }
1421 1422
1422 1423
1423 #endif 1424 #endif
1424 1425
1425 void ext_flash_disable_protection(void) 1426 static void ext_flash_disable_protection(void)
1426 { 1427 {
1427 /* 1428 /*
1428 extFlashStatusBit8_Type status; 1429 extFlashStatusBit8_Type status;
1429 1430
1430 status.uw = 0; 1431 status.uw = 0;
1473 write_spi(status.uw,RELEASE); // new status 1474 write_spi(status.uw,RELEASE); // new status
1474 */ 1475 */
1475 } 1476 }
1476 1477
1477 1478
1478 void ext_flash_erase_chip(void) 1479 /*void ext_flash_erase_chip(void)
1479 { 1480 {
1480 wait_chip_not_busy(); 1481 wait_chip_not_busy();
1481 write_spi(0x06,RELEASE); 1482 write_spi(0x06,RELEASE);
1482 write_spi(0x60,RELEASE); 1483 write_spi(0x60,RELEASE);
1483 wait_chip_not_busy(); 1484 wait_chip_not_busy();
1484 } 1485 }*/
1485 1486
1486 void ext_flash_erase_firmware(void) 1487 void ext_flash_erase_firmware(void)
1487 { 1488 {
1488 uint32_t size, blocks_64k; 1489 uint32_t size, blocks_64k;
1489 1490
1523 1524
1524 ext_flash_enable_protection(); 1525 ext_flash_enable_protection();
1525 } 1526 }
1526 1527
1527 1528
1528 void ext_flash_erase4kB(void) 1529 static void ext_flash_erase4kB(void)
1529 { 1530 {
1530 wait_chip_not_busy(); 1531 wait_chip_not_busy();
1531 write_spi(0x06,RELEASE);/* WREN */ 1532 write_spi(0x06,RELEASE);/* WREN */
1532 write_spi(0x20,HOLDCS);/* sector erase cmd */ 1533 write_spi(0x20,HOLDCS);/* sector erase cmd */
1533 write_address(RELEASE); 1534 write_address(RELEASE);
1534 } 1535 }
1535 1536
1536 /* be carefull - might not work with entire family and other products 1537 /* be carefull - might not work with entire family and other products
1537 * see page 14 of LOGBOOK_V3_S25FS-S_00-271247.pdf 1538 * see page 14 of LOGBOOK_V3_S25FS-S_00-271247.pdf
1538 */ 1539 */
1539 void ext_flash_erase32kB(void) 1540 static void ext_flash_erase32kB(void)
1540 { 1541 {
1541 uint32_t actualAddress_backup; 1542 uint32_t actualAddress_backup;
1542 1543
1543 actualAddress_backup = actualAddress; 1544 actualAddress_backup = actualAddress;
1544 actualAddress = 0; 1545 actualAddress = 0;
1548 write_address(RELEASE); 1549 write_address(RELEASE);
1549 actualAddress = actualAddress_backup; 1550 actualAddress = actualAddress_backup;
1550 } 1551 }
1551 1552
1552 1553
1553 void ext_flash_erase64kB(void) 1554 static void ext_flash_erase64kB(void)
1554 { 1555 {
1555 wait_chip_not_busy(); 1556 wait_chip_not_busy();
1556 write_spi(0x06,RELEASE);/* WREN */ 1557 write_spi(0x06,RELEASE);/* WREN */
1557 write_spi(0xD8,HOLDCS);/* sector erase cmd */ 1558 write_spi(0xD8,HOLDCS);/* sector erase cmd */
1558 write_address(RELEASE); 1559 write_address(RELEASE);
1565 write_spi(0x03,HOLDCS); /* WREN */ 1566 write_spi(0x03,HOLDCS); /* WREN */
1566 write_address(HOLDCS); 1567 write_address(HOLDCS);
1567 } 1568 }
1568 1569
1569 /* 4KB, 32KB, 64 KB, not the upper 16 MB with 4 Byte address at the moment */ 1570 /* 4KB, 32KB, 64 KB, not the upper 16 MB with 4 Byte address at the moment */
1570 uint8_t ext_flash_erase_if_on_page_start(void) 1571 static uint8_t ext_flash_erase_if_on_page_start(void)
1571 { 1572 {
1572 if(actualAddress < 0x00008000) 1573 if(actualAddress < 0x00008000)
1573 { 1574 {
1574 /* 4K Byte is 0x1000 */ 1575 /* 4K Byte is 0x1000 */
1575 if((actualAddress & 0xFFF) == 0) 1576 if((actualAddress & 0xFFF) == 0)
1599 } 1600 }
1600 return 0; 1601 return 0;
1601 } 1602 }
1602 1603
1603 1604
1604 void ext_flash_read_block(uint8_t *getByte, uint8_t type) 1605 static void ext_flash_read_block(uint8_t *getByte, uint8_t type)
1605 { 1606 {
1606 *getByte = read_spi(HOLDCS);/* read data */ 1607 *getByte = read_spi(HOLDCS);/* read data */
1607 ext_flash_incf_address(type); 1608 ext_flash_incf_address(type);
1608 } 1609 }
1609 1610
1610 1611
1611 void ext_flash_read_block_multi(void *getByte, uint32_t size, uint8_t type) 1612 static void ext_flash_read_block_multi(void *getByte, uint32_t size, uint8_t type)
1612 { 1613 {
1613 uint8_t *data; 1614 uint8_t *data;
1614 data = getByte; 1615 data = getByte;
1615 1616
1616 for(uint32_t i=0;i<size;i++) 1617 for(uint32_t i=0;i<size;i++)
1619 ext_flash_incf_address(type); 1620 ext_flash_incf_address(type);
1620 } 1621 }
1621 } 1622 }
1622 1623
1623 1624
1624 void ext_flash_read_block_stop(void) 1625 static void ext_flash_read_block_stop(void)
1625 { 1626 {
1626 chip_unselect(); 1627 chip_unselect();
1627 } 1628 }
1628 1629
1629 1630
1630 /* Private functions ---------------------------------------------------------*/ 1631 /* Private functions ---------------------------------------------------------*/
1631 1632
1632 void ef_write_block(uint8_t * sendByte, uint32_t length, uint8_t type, uint8_t do_not_erase) 1633 static void ef_write_block(uint8_t * sendByte, uint32_t length, uint8_t type, uint8_t do_not_erase)
1633 { 1634 {
1634 uint32_t remaining_page_size, remaining_length, remaining_space_to_ring_end; 1635 uint32_t remaining_page_size, remaining_length, remaining_space_to_ring_end;
1635 1636
1636 if(!length) 1637 if(!length)
1637 return; 1638 return;
1743 break; 1744 break;
1744 } 1745 }
1745 } 1746 }
1746 1747
1747 1748
1748 _Bool ext_flash_test_remaining_space_of_page_empty(uint32_t pointer, uint16_t length) 1749 static _Bool ext_flash_test_remaining_space_of_page_empty(uint32_t pointer, uint16_t length)
1749 { 1750 {
1750 if((pointer & 0xFFF) == 0) 1751 if((pointer & 0xFFF) == 0)
1751 return 1; 1752 return 1;
1752 1753
1753 uint32_t backup = actualAddress; 1754 uint32_t backup = actualAddress;
1775 actualAddress = backup; 1776 actualAddress = backup;
1776 return 1; 1777 return 1;
1777 } 1778 }
1778 1779
1779 1780
1780 void ext_flash_set_to_begin_of_next_page(uint32_t *pointer, uint8_t type) 1781 static void ext_flash_set_to_begin_of_next_page(uint32_t *pointer, uint8_t type)
1781 { 1782 {
1782 uint32_t ringStart, ringStop; 1783 uint32_t ringStart, ringStop;
1783 1784
1784 switch(type) 1785 switch(type)
1785 { 1786 {
1828 HAL_Delay(25); 1829 HAL_Delay(25);
1829 } 1830 }
1830 } 1831 }
1831 1832
1832 1833
1833 void chip_unselect(void) 1834 static void chip_unselect(void)
1834 { 1835 {
1835 HAL_GPIO_WritePin(EXTFLASH_CSB_GPIO_PORT,EXTFLASH_CSB_PIN,GPIO_PIN_SET); // chip select 1836 HAL_GPIO_WritePin(EXTFLASH_CSB_GPIO_PORT,EXTFLASH_CSB_PIN,GPIO_PIN_SET); // chip select
1836 } 1837 }
1837 1838
1838 void chip_select(void) 1839 static void chip_select(void)
1839 { 1840 {
1840 HAL_GPIO_WritePin(EXTFLASH_CSB_GPIO_PORT,EXTFLASH_CSB_PIN,GPIO_PIN_RESET); // chip select 1841 HAL_GPIO_WritePin(EXTFLASH_CSB_GPIO_PORT,EXTFLASH_CSB_PIN,GPIO_PIN_RESET); // chip select
1841 } 1842 }
1842 1843
1843 void error_led_on(void) 1844 static void error_led_on(void)
1844 { 1845 {
1845 HAL_GPIO_WritePin(OSCILLOSCOPE_GPIO_PORT,OSCILLOSCOPE_PIN,GPIO_PIN_SET); 1846 HAL_GPIO_WritePin(OSCILLOSCOPE_GPIO_PORT,OSCILLOSCOPE_PIN,GPIO_PIN_SET);
1846 } 1847 }
1847 1848
1848 void error_led_off(void) 1849 static void error_led_off(void)
1849 { 1850 {
1850 HAL_GPIO_WritePin(OSCILLOSCOPE_GPIO_PORT,OSCILLOSCOPE_PIN,GPIO_PIN_RESET); 1851 HAL_GPIO_WritePin(OSCILLOSCOPE_GPIO_PORT,OSCILLOSCOPE_PIN,GPIO_PIN_RESET);
1851 } 1852 }
1852 1853
1853 1854
1854 uint8_t read_spi(uint8_t unselect_CS_afterwards) 1855 static uint8_t read_spi(uint8_t unselect_CS_afterwards)
1855 { 1856 {
1856 uint8_t byte; 1857 uint8_t byte;
1857 1858
1858 chip_select(); 1859 chip_select();
1859 1860
1868 1869
1869 return byte; 1870 return byte;
1870 } 1871 }
1871 1872
1872 1873
1873 void write_spi(uint8_t data, uint8_t unselect_CS_afterwards) 1874 static void write_spi(uint8_t data, uint8_t unselect_CS_afterwards)
1874 { 1875 {
1875 chip_select(); 1876 chip_select();
1876 1877
1877 if(HAL_SPI_Transmit(&hspiDisplay, &data, 1, 10000) != HAL_OK) 1878 if(HAL_SPI_Transmit(&hspiDisplay, &data, 1, 10000) != HAL_OK)
1878 Error_Handler_extflash(); 1879 Error_Handler_extflash();
1883 if(unselect_CS_afterwards) 1884 if(unselect_CS_afterwards)
1884 chip_unselect(); 1885 chip_unselect();
1885 } 1886 }
1886 1887
1887 1888
1888 void write_address(uint8_t unselect_CS_afterwards) 1889 static void write_address(uint8_t unselect_CS_afterwards)
1889 { 1890 {
1890 uint8_t hi, med ,lo; 1891 uint8_t hi, med ,lo;
1891 1892
1892 hi = (actualAddress >> 16) & 0xFF; 1893 hi = (actualAddress >> 16) & 0xFF;
1893 med = (actualAddress >> 8) & 0xFF; 1894 med = (actualAddress >> 8) & 0xFF;
1914 } 1915 }
1915 chip_unselect(); 1916 chip_unselect();
1916 } 1917 }
1917 1918
1918 1919
1919 void ext_flash_incf_address(uint8_t type) 1920 static void ext_flash_incf_address(uint8_t type)
1920 { 1921 {
1921 uint32_t ringStart, ringStop; 1922 uint32_t ringStart, ringStop;
1922 1923
1923 actualAddress += 1; 1924 actualAddress += 1;
1924 1925
1961 if((actualAddress < ringStart) || (actualAddress > ringStop)) 1962 if((actualAddress < ringStart) || (actualAddress > ringStop))
1962 actualAddress = ringStart; 1963 actualAddress = ringStart;
1963 } 1964 }
1964 1965
1965 1966
1966 void ext_flash_decf_address_ring(uint8_t type) 1967 static void ext_flash_decf_address_ring(uint8_t type)
1967 { 1968 {
1968 uint32_t ringStart, ringStop; 1969 uint32_t ringStart, ringStop;
1969 1970
1970 switch(type) 1971 switch(type)
1971 { 1972 {