comparison BootLoader/Src/tComm_mini.c @ 1003:f1b78bc8de10 BootloaderOstc5

Bootloader improve init sequence: Initialization of the BT module failed with the previous version. Root cause could be that the commands were send to fast to the module and the error counter expired before the configuration was stored. To avoid this problem a timer has been added which delays the transmission of commands by 100ms.
author Ideenmodellierer
date Thu, 01 May 2025 17:52:17 +0200
parents 39f28cd9dc46
children 0dd92e9b70a2
comparison
equal deleted inserted replaced
1002:23e94766d00a 1003:f1b78bc8de10
133 133
134 134
135 static uint8_t receive_update_data_cpu2(void); 135 static uint8_t receive_update_data_cpu2(void);
136 uint8_t receive_update_data_cpu2_sub(uint8_t* pBuffer); 136 uint8_t receive_update_data_cpu2_sub(uint8_t* pBuffer);
137 137
138
139 /* #define OSTC4_HW */
138 /* Exported functions --------------------------------------------------------*/ 140 /* Exported functions --------------------------------------------------------*/
139 141
140 void tComm_init(void) 142 void tComm_init(void)
141 { 143 {
142 tCscreen.FBStartAdress = 0; 144 tCscreen.FBStartAdress = 0;
1680 1682
1681 1683
1682 uint8_t tComm_GetBTCmdStr(BTCmd cmdId, char* pCmdStr) 1684 uint8_t tComm_GetBTCmdStr(BTCmd cmdId, char* pCmdStr)
1683 { 1685 {
1684 uint8_t ret = 0; 1686 uint8_t ret = 0;
1685 1687 #ifndef OSTC4_HW
1686 switch (cmdId) 1688 switch (cmdId)
1687 { 1689 {
1688 case BT_CMD_ECHO: sprintf(pCmdStr,"ATE0\r"); 1690 case BT_CMD_ECHO: sprintf(pCmdStr,"ATE0\r");
1689 ret = 1; 1691 ret = 1;
1690 break; 1692 break;
1702 ret = 1; 1704 ret = 1;
1703 break; 1705 break;
1704 default: 1706 default:
1705 break; 1707 break;
1706 } 1708 }
1709 #else
1710 switch (cmdId)
1711 {
1712 case BT_CMD_ECHO: sprintf(pCmdStr,"ATE0\r");
1713 ret = 1;
1714 break;
1715 case BT_CMD_SILENCE: strcpy(pCmdStr,"ATS30=0\r");
1716 ret = 1;
1717
1718 break;
1719 case BT_CMD_ESCAPE_DELAY: strcpy(pCmdStr,"ATS12=10\r");
1720 ret = 1;
1721
1722 break;
1723 case BT_CMD_SIGNAL_POLL: strcpy(pCmdStr,"AT+BSTPOLL=100\r");
1724 ret = 1;
1725
1726 break;
1727 case BT_CMD_BAUDRATE_115: strcpy(pCmdStr,"AT%B8\r");
1728 ret = 1;
1729 break;
1730
1731 case BT_CMD_BAUDRATE_460: strcpy(pCmdStr,"AT%B22\r");
1732 ret = 1;
1733 break;
1734 case BT_CMD_NAME: strcpy(pCmdStr,"AT+BNAME=OSTC4-12345\r");
1735 ret = 1;
1736 break;
1737 case BT_CMD_EXIT_CMD: strcpy(pCmdStr,"ATO\r");
1738 ret = 1;
1739 break;
1740 default:
1741 break;
1742 }
1743 #endif
1707 return ret; 1744 return ret;
1708 } 1745 }
1709 1746
1710 void tComm_StartBlueModConfig() 1747 void tComm_StartBlueModConfig()
1711 { 1748 {
1719 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&RxBuffer[index], 1, 10); 1756 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&RxBuffer[index], 1, 10);
1720 if(index < UART_CMD_BUF_SIZE) index++; 1757 if(index < UART_CMD_BUF_SIZE) index++;
1721 }while(answer == HAL_OK); 1758 }while(answer == HAL_OK);
1722 } 1759 }
1723 1760
1761 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow)
1762 {
1763 if(ticksstart <= ticksnow)
1764 return ticksnow - ticksstart;
1765 else
1766 return 0xFFFFFFFF - ticksstart + ticksnow;
1767 }
1768
1724 uint8_t tComm_HandleBlueModConfig() 1769 uint8_t tComm_HandleBlueModConfig()
1725 { 1770 {
1726 static uint8_t RestartModule = 1; /* used to do power off / on cycle */ 1771 static uint8_t RestartModule = 1; /* used to do power off / on cycle */
1727 static uint8_t ConfigRetryCnt = 0; /* Retry count without power cycle */ 1772 static uint8_t ConfigRetryCnt = 0; /* Retry count without power cycle */
1773 static uint8_t lastConfigStep = BM_CONFIG_OFF;
1774 static uint32_t cmdStartTick = 0;
1728 1775
1729 char TxBuffer[UART_CMD_BUF_SIZE]; 1776 char TxBuffer[UART_CMD_BUF_SIZE];
1730 uint8_t CmdSize = 0; 1777 uint8_t CmdSize = 0;
1731 1778
1732 uint8_t result = HAL_OK; 1779 uint8_t result = HAL_OK;
1733 1780
1734 memset(TxBuffer, 0, sizeof(TxBuffer)); 1781 memset(TxBuffer, 0, sizeof(TxBuffer));
1735 1782
1736 switch (BmTmpConfig) 1783 if(lastConfigStep != BmTmpConfig)
1737 { 1784 {
1738 case BM_CONFIG_ECHO: tComm_GetBTCmdStr (BT_CMD_ECHO, TxBuffer); 1785 ConfigRetryCnt = 0;
1739 break; 1786 lastConfigStep = BmTmpConfig;
1740 case BM_CONFIG_SILENCE: tComm_GetBTCmdStr (BT_CMD_SILENCE, TxBuffer); 1787 }
1741 break; 1788
1742 case BM_CONFIG_ESCAPE_DELAY: tComm_GetBTCmdStr (BT_CMD_ESCAPE_DELAY, TxBuffer); 1789 if(time_elapsed_ms(cmdStartTick, HAL_GetTick()) > 100)
1743 break; 1790 {
1744 case BM_CONFIG_SIGNAL_POLL: tComm_GetBTCmdStr(BT_CMD_SIGNAL_POLL, TxBuffer); 1791 switch (BmTmpConfig)
1745 break; 1792 {
1746 case BM_CONFIG_BAUD: 1793 case BM_CONFIG_ECHO: tComm_GetBTCmdStr (BT_CMD_ECHO, TxBuffer);
1747 #ifdef ENABLE_FAST_COMM 1794 break;
1748 tComm_GetBTCmdStr(BT_CMD_BAUDRATE_460, TxBuffer); 1795 case BM_CONFIG_SILENCE: tComm_GetBTCmdStr (BT_CMD_SILENCE, TxBuffer);
1796 break;
1797 case BM_CONFIG_ESCAPE_DELAY: tComm_GetBTCmdStr (BT_CMD_ESCAPE_DELAY, TxBuffer);
1798 break;
1799 case BM_CONFIG_SIGNAL_POLL: tComm_GetBTCmdStr(BT_CMD_SIGNAL_POLL, TxBuffer);
1800 break;
1801 case BM_CONFIG_BAUD:
1802 #ifdef ENABLE_FAST_COMM
1803 tComm_GetBTCmdStr(BT_CMD_BAUDRATE_460, TxBuffer);
1804 #else
1805 BmTmpConfig = BM_CONFIG_DONE;
1806 #endif
1807 break;
1808 case BM_CONFIG_RETRY: ConfigRetryCnt--;
1809 HAL_Delay(1);
1810 if(ConfigRetryCnt == 0)
1811 {
1812 MX_Bluetooth_PowerOn();
1813 tComm_StartBlueModConfig();
1814 }
1815 break;
1816 case BM_CONFIG_DONE:
1817 case BM_CONFIG_OFF:
1818 ConfigRetryCnt = 0;
1819 RestartModule = 1;
1820 break;
1821
1822
1823 #ifndef OSTC4_HW
1824 /* the procedure below is just needed for the initial bluetooth module initialization */
1825 case BM_INIT_TRIGGER_ON: HAL_Delay(2000);
1826 HAL_GPIO_WritePin(BLE_UBLOX_DSR_GPIO_PORT,BLE_UBLOX_DSR_PIN,GPIO_PIN_RESET);
1827 BmTmpConfig++;
1828 break;
1829 case BM_INIT_TRIGGER_OFF: HAL_Delay(1);
1830 HAL_GPIO_WritePin(BLE_UBLOX_DSR_GPIO_PORT,BLE_UBLOX_DSR_PIN,GPIO_PIN_SET);
1831 HAL_Delay(2000);
1832 BmTmpConfig++;
1833 break;
1834 case BM_INIT_ECHO: sprintf(TxBuffer,"ATE0\r");
1835 break;
1836 case BM_INIT_FACTORY: sprintf(TxBuffer,"AT+UFACTORY\r"); /*Set to factory defined configuration */
1837 break;
1838 case BM_INIT_MODE: sprintf(TxBuffer,"AT+UMSM=1\r"); /* start in Data mode */
1839 break;
1840 case BM_INIT_BLE: sprintf(TxBuffer,"AT+UBTLE=2\r"); /* Bluetooth low energy Peripheral */
1841 break;
1842 case BM_INIT_NAME: sprintf(TxBuffer,"AT+UBTLN=OSTC5-12345\r"); /* Bluetooth name */
1843 if(hardwareDataGetPointer()->primarySerial != 0xFFFF) /* module reinit? => restore old name */
1844 {
1845 gfx_number_to_string(5,1,&TxBuffer[15],hardwareDataGetPointer()->primarySerial);
1846 hardware_programmPrimaryBluetoothNameSet();
1847 }
1848 break;
1849 case BM_INIT_SSP_IDO_OFF: sprintf(TxBuffer,"AT+UDSC=0,0\r"); /* Disable SPP Server on ID0 */
1850 break;
1851 case BM_INIT_SSP_IDO_ON: sprintf(TxBuffer,"AT+UDSC=0,3\r"); /* SPP Server on ID0 */
1852 break;
1853 case BM_INIT_SSP_ID1_OFF: sprintf(TxBuffer,"AT+UDSC=1,0\r"); /* Disable SPS Server on ID1 */
1854 break;
1855 case BM_INIT_SSP_ID1_ON: sprintf(TxBuffer,"AT+UDSC=1,6\r"); /* SPS Server on ID1 */
1856 break;
1857 case BM_INIT_STORE: sprintf(TxBuffer,"AT&W0\r"); /* write settings into eeprom */
1858 break;
1859 case BM_INIT_RESTART: sprintf(TxBuffer,"AT+CPWROFF\r"); /* reboot module */
1860 break;
1861 case BM_INIT_DONE: BmTmpConfig = BM_CONFIG_ECHO;
1862 break;
1863 default:
1864 break;
1865 }
1749 #else 1866 #else
1750 BmTmpConfig = BM_CONFIG_DONE;
1751 #endif
1752 break;
1753 case BM_CONFIG_RETRY: ConfigRetryCnt--;
1754 HAL_Delay(1);
1755 if(ConfigRetryCnt == 0)
1756 {
1757 MX_Bluetooth_PowerOn();
1758 tComm_StartBlueModConfig();
1759 }
1760 break;
1761 case BM_CONFIG_DONE:
1762 case BM_CONFIG_OFF:
1763 ConfigRetryCnt = 0;
1764 RestartModule = 1;
1765 break;
1766 /* the procedure below is just needed for the initial bluetooth module initialization */
1767 case BM_INIT_TRIGGER_ON: HAL_Delay(2000); 1867 case BM_INIT_TRIGGER_ON: HAL_Delay(2000);
1768 HAL_GPIO_WritePin(BLE_UBLOX_DSR_GPIO_PORT,BLE_UBLOX_DSR_PIN,GPIO_PIN_RESET);
1769 BmTmpConfig++; 1868 BmTmpConfig++;
1770 break; 1869 break;
1771 case BM_INIT_TRIGGER_OFF: HAL_Delay(1); 1870 case BM_INIT_TRIGGER_OFF: HAL_Delay(1);
1772 HAL_GPIO_WritePin(BLE_UBLOX_DSR_GPIO_PORT,BLE_UBLOX_DSR_PIN,GPIO_PIN_SET);
1773 HAL_Delay(2000); 1871 HAL_Delay(2000);
1774 BmTmpConfig++; 1872 BmTmpConfig++;
1775 break; 1873 break;
1776 case BM_INIT_ECHO: sprintf(TxBuffer,"ATE0\r"); 1874 case BM_INIT_ECHO:
1875 case BM_INIT_ECHO2: sprintf(TxBuffer,"ATE0\r");
1777 break; 1876 break;
1778 case BM_INIT_FACTORY: sprintf(TxBuffer,"AT+UFACTORY\r"); /*Set to factory defined configuration */ 1877 case BM_INIT_FACTORY: sprintf(TxBuffer,"AT&F1\r"); /*Set to factory defined configuration */
1779 break; 1878 break;
1780 case BM_INIT_MODE: sprintf(TxBuffer,"AT+UMSM=1\r"); /* start in Data mode */ 1879 case BM_INIT_MODE: BmTmpConfig++;
1781 break; 1880 break;
1782 case BM_INIT_BLE: sprintf(TxBuffer,"AT+UBTLE=2\r"); /* Bluetooth low energy Peripheral */ 1881 case BM_INIT_BLE: BmTmpConfig++;
1783 break; 1882 break;
1784 case BM_INIT_NAME: sprintf(TxBuffer,"AT+UBTLN=OSTC5-12345\r"); /* Bluetooth name */ 1883 case BM_INIT_NAME: sprintf(TxBuffer,"AT+BNAME=OSTC4-12345\r"); /* Bluetooth name */
1785 if(hardwareDataGetPointer()->primarySerial != 0xFFFF) /* module reinit? => restore old name */ 1884 if(hardwareDataGetPointer()->primarySerial != 0xFFFF) /* module reinit? => restore old name */
1786 { 1885 {
1787 gfx_number_to_string(5,1,&TxBuffer[15],hardwareDataGetPointer()->primarySerial); 1886 gfx_number_to_string(5,1,&TxBuffer[15],hardwareDataGetPointer()->primarySerial);
1788 hardware_programmPrimaryBluetoothNameSet(); 1887 hardware_programmPrimaryBluetoothNameSet();
1789 } 1888 }
1790 break; 1889 break;
1791 case BM_INIT_SSP_IDO_OFF: sprintf(TxBuffer,"AT+UDSC=0,0\r"); /* Disable SPP Server on ID0 */ 1890 case BM_INIT_SSP_IDO_OFF: BmTmpConfig++;
1792 break; 1891 break;
1793 case BM_INIT_SSP_IDO_ON: sprintf(TxBuffer,"AT+UDSC=0,3\r"); /* SPP Server on ID0 */ 1892 case BM_INIT_SSP_IDO_ON: BmTmpConfig++;
1794 break; 1893 break;
1795 case BM_INIT_SSP_ID1_OFF: sprintf(TxBuffer,"AT+UDSC=1,0\r"); /* Disable SPS Server on ID1 */ 1894 case BM_INIT_SSP_ID1_OFF: BmTmpConfig++;
1796 break; 1895 break;
1797 case BM_INIT_SSP_ID1_ON: sprintf(TxBuffer,"AT+UDSC=1,6\r"); /* SPS Server on ID1 */ 1896 case BM_INIT_SSP_ID1_ON: BmTmpConfig++;
1798 break; 1897 break;
1799 case BM_INIT_STORE: sprintf(TxBuffer,"AT&W\r"); /* write settings into eeprom */ 1898 case BM_INIT_STORE: sprintf(TxBuffer,"AT&W\r"); /* write settings into eeprom */
1800 break; 1899 break;
1801 case BM_INIT_RESTART: sprintf(TxBuffer,"AT+CPWROFF\r"); /* reboot module */ 1900 case BM_INIT_RESTART: sprintf(TxBuffer,"AT+RESET\r"); /* reboot module */
1802 break; 1901 break;
1803 case BM_INIT_DONE: BmTmpConfig = BM_CONFIG_ECHO; 1902 case BM_INIT_DONE: BmTmpConfig = BM_CONFIG_ECHO;
1804 break; 1903 break;
1805 default: 1904 default:
1806 break; 1905 break;
1807 } 1906 }
1808 if(TxBuffer[0] != 0) /* forward command to module */ 1907 #endif
1809 { 1908
1810 CmdSize = strlen(TxBuffer); 1909 if(TxBuffer[0] != 0) /* forward command to module */
1811 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK)
1812 { 1910 {
1813 result = tComm_CheckAnswerOK(); 1911 CmdSize = strlen(TxBuffer);
1814 1912 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK)
1815 if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate != 460800)) /* is com already switched to fast speed? */
1816 { 1913 {
1817 HAL_UART_DeInit(&UartHandle); 1914 result = tComm_CheckAnswerOK();
1818 HAL_Delay(1); 1915
1819 UartHandle.Init.BaudRate = 460800; 1916 if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate != 460800)) /* is com already switched to fast speed? */
1820 HAL_UART_Init(&UartHandle);
1821 }
1822 else if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate == 460800)) /* This shut not happen because default speed is 115200 => update module configuration */
1823 {
1824 tComm_GetBTCmdStr(BT_CMD_BAUDRATE_115, TxBuffer);
1825
1826 CmdSize = strlen(TxBuffer);
1827 HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000);
1828 HAL_UART_DeInit(&UartHandle);
1829 HAL_Delay(10);
1830 UartHandle.Init.BaudRate = 115200;
1831 HAL_UART_Init(&UartHandle);
1832 sprintf(TxBuffer,"AT&W\r"); /* write configuration */
1833 CmdSize = strlen(TxBuffer);
1834 HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000);
1835 }
1836 if(result == HAL_OK)
1837 {
1838 BmTmpConfig++;
1839 if(BmTmpConfig == BM_CONFIG_RETRY)
1840 {
1841 BmTmpConfig = BM_CONFIG_DONE;
1842 }
1843 }
1844 if(BmTmpConfig == BM_CONFIG_ECHO)
1845 {
1846 BmTmpConfig = BM_CONFIG_DONE;
1847 ConfigRetryCnt = 0;
1848 RestartModule = 1;
1849 }
1850 }
1851 }
1852 else /* no command for the configuration step found => skip step */
1853 {
1854 if((BmTmpConfig > BM_CONFIG_OFF) && (BmTmpConfig < BM_CONFIG_DONE))
1855 {
1856 BmTmpConfig++;
1857 }
1858 }
1859 if(result != HAL_OK)
1860 {
1861 ConfigRetryCnt++;
1862 if(ConfigRetryCnt > 3) /* Configuration failed => switch off module */
1863 {
1864 MX_Bluetooth_PowerOff();
1865 if(RestartModule)
1866 {
1867 RestartModule = 0; /* only one try */
1868 ConfigRetryCnt = 200; /* used for delay to startup module again */
1869
1870 if((BmTmpConfig == BM_CONFIG_ECHO) || (BmTmpConfig == BM_INIT_ECHO)) /* the module did not answer even once => try again with alternative baud rate */
1871 { 1917 {
1872 HAL_UART_DeInit(&UartHandle); 1918 HAL_UART_DeInit(&UartHandle);
1873 HAL_Delay(1); 1919 HAL_Delay(1);
1874 UartHandle.Init.BaudRate = 460800; 1920 UartHandle.Init.BaudRate = 460800;
1875 HAL_UART_Init(&UartHandle); 1921 HAL_UART_Init(&UartHandle);
1876 } 1922 }
1877 BmTmpConfig = BM_CONFIG_RETRY; 1923 else if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate == 460800)) /* This shut not happen because default speed is 115200 => update module configuration */
1924 {
1925 tComm_GetBTCmdStr(BT_CMD_BAUDRATE_115, TxBuffer);
1926
1927 CmdSize = strlen(TxBuffer);
1928 HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000);
1929 HAL_UART_DeInit(&UartHandle);
1930 HAL_Delay(10);
1931 UartHandle.Init.BaudRate = 115200;
1932 HAL_UART_Init(&UartHandle);
1933 sprintf(TxBuffer,"AT&W\r"); /* write configuration */
1934 CmdSize = strlen(TxBuffer);
1935 HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000);
1936 }
1937 if(result == HAL_OK)
1938 {
1939 BmTmpConfig++;
1940 cmdStartTick = HAL_GetTick();
1941 if(BmTmpConfig == BM_CONFIG_RETRY)
1942 {
1943 BmTmpConfig = BM_CONFIG_DONE;
1944 }
1945 }
1946 if(BmTmpConfig == BM_CONFIG_ECHO)
1947 {
1948 BmTmpConfig = BM_CONFIG_DONE;
1949 ConfigRetryCnt = 0;
1950 RestartModule = 1;
1951 }
1878 } 1952 }
1879 else /* even restarting module failed => switch bluetooth off */ 1953 }
1954 else /* no command for the configuration step found => skip step */
1955 {
1956 if((BmTmpConfig > BM_CONFIG_OFF) && (BmTmpConfig < BM_CONFIG_DONE))
1880 { 1957 {
1881 ConfigRetryCnt = 0; 1958 BmTmpConfig++;
1882 BmTmpConfig = BM_CONFIG_OFF; 1959 }
1960 }
1961 if(result != HAL_OK)
1962 {
1963 ConfigRetryCnt++;
1964 if(ConfigRetryCnt > 3) /* Configuration failed => switch off module */
1965 {
1966 MX_Bluetooth_PowerOff();
1967 if(RestartModule)
1968 {
1969 RestartModule = 0; /* only one try */
1970 ConfigRetryCnt = 200; /* used for delay to startup module again */
1971
1972 if((BmTmpConfig == BM_CONFIG_ECHO) || (BmTmpConfig == BM_INIT_ECHO)) /* the module did not answer even once => try again with alternative baud rate */
1973 {
1974 HAL_UART_DeInit(&UartHandle);
1975 HAL_Delay(1);
1976 UartHandle.Init.BaudRate = 460800;
1977 HAL_UART_Init(&UartHandle);
1978 }
1979 BmTmpConfig = BM_CONFIG_RETRY;
1980 }
1981 else /* even restarting module failed => switch bluetooth off */
1982 {
1983 ConfigRetryCnt = 0;
1984 BmTmpConfig = BM_CONFIG_OFF;
1985 }
1883 } 1986 }
1884 } 1987 }
1885 } 1988 }
1886 return result; 1989 return result;
1887 } 1990 }