Compare commits
13 Commits
wifi
...
new_sntp_i
Author | SHA1 | Date | |
---|---|---|---|
c46e5dbb3b | |||
e8f9455c17
|
|||
9f907cb84e
|
|||
4b39b99848
|
|||
5d25490af7
|
|||
6ad1eda428
|
|||
70de623e5f
|
|||
7a60bb9c97
|
|||
3cefd16838
|
|||
3c6ddcb99b
|
|||
13a67dc660
|
|||
3f024510ae | |||
24a37e9e5f |
@ -61,7 +61,6 @@ void Error_Handler(void);
|
|||||||
#define eepromSpi hspi1
|
#define eepromSpi hspi1
|
||||||
#define etherSpi hspi2
|
#define etherSpi hspi2
|
||||||
#define debugUart huart1
|
#define debugUart huart1
|
||||||
#define modemUart huart2
|
|
||||||
#define mainsCnt htim1
|
#define mainsCnt htim1
|
||||||
#define LED_Red_Pin GPIO_PIN_13
|
#define LED_Red_Pin GPIO_PIN_13
|
||||||
#define LED_Red_GPIO_Port GPIOC
|
#define LED_Red_GPIO_Port GPIOC
|
||||||
@ -69,12 +68,6 @@ void Error_Handler(void);
|
|||||||
#define LED_Green_GPIO_Port GPIOC
|
#define LED_Green_GPIO_Port GPIOC
|
||||||
#define LED_Blue_Pin GPIO_PIN_15
|
#define LED_Blue_Pin GPIO_PIN_15
|
||||||
#define LED_Blue_GPIO_Port GPIOC
|
#define LED_Blue_GPIO_Port GPIOC
|
||||||
#define MODEM_RES_Pin GPIO_PIN_1
|
|
||||||
#define MODEM_RES_GPIO_Port GPIOA
|
|
||||||
#define MODEM_TX_Pin GPIO_PIN_2
|
|
||||||
#define MODEM_TX_GPIO_Port GPIOA
|
|
||||||
#define MODEM_RX_Pin GPIO_PIN_3
|
|
||||||
#define MODEM_RX_GPIO_Port GPIOA
|
|
||||||
#define EEPROM_CS_Pin GPIO_PIN_4
|
#define EEPROM_CS_Pin GPIO_PIN_4
|
||||||
#define EEPROM_CS_GPIO_Port GPIOA
|
#define EEPROM_CS_GPIO_Port GPIOA
|
||||||
#define EEPROM_SCK_Pin GPIO_PIN_5
|
#define EEPROM_SCK_Pin GPIO_PIN_5
|
||||||
|
@ -31,14 +31,12 @@
|
|||||||
/* USER CODE END Includes */
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
extern UART_HandleTypeDef huart1;
|
extern UART_HandleTypeDef huart1;
|
||||||
extern UART_HandleTypeDef huart2;
|
|
||||||
|
|
||||||
/* USER CODE BEGIN Private defines */
|
/* USER CODE BEGIN Private defines */
|
||||||
|
|
||||||
/* USER CODE END Private defines */
|
/* USER CODE END Private defines */
|
||||||
|
|
||||||
void MX_USART1_UART_Init(void);
|
void MX_USART1_UART_Init(void);
|
||||||
void MX_USART2_UART_Init(void);
|
|
||||||
|
|
||||||
/* USER CODE BEGIN Prototypes */
|
/* USER CODE BEGIN Prototypes */
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ void MX_GPIO_Init(void)
|
|||||||
HAL_GPIO_WritePin(GPIOC, LED_Red_Pin|LED_Green_Pin|LED_Blue_Pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(GPIOC, LED_Red_Pin|LED_Green_Pin|LED_Blue_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
/*Configure GPIO pin Output Level */
|
/*Configure GPIO pin Output Level */
|
||||||
HAL_GPIO_WritePin(GPIOA, MODEM_RES_Pin|EEPROM_CS_Pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(EEPROM_CS_GPIO_Port, EEPROM_CS_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
/*Configure GPIO pin Output Level */
|
/*Configure GPIO pin Output Level */
|
||||||
HAL_GPIO_WritePin(GPIOB, ETHER_RES_Pin|ETHER_CS_Pin|Debug_Signal_2_Pin|Debug_Signal_1_Pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(GPIOB, ETHER_RES_Pin|ETHER_CS_Pin|Debug_Signal_2_Pin|Debug_Signal_1_Pin, GPIO_PIN_RESET);
|
||||||
@ -64,12 +64,12 @@ void MX_GPIO_Init(void)
|
|||||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||||
|
|
||||||
/*Configure GPIO pins : PAPin PAPin */
|
/*Configure GPIO pin : PtPin */
|
||||||
GPIO_InitStruct.Pin = MODEM_RES_Pin|EEPROM_CS_Pin;
|
GPIO_InitStruct.Pin = EEPROM_CS_Pin;
|
||||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
HAL_GPIO_Init(EEPROM_CS_GPIO_Port, &GPIO_InitStruct);
|
||||||
|
|
||||||
/*Configure GPIO pin : PtPin */
|
/*Configure GPIO pin : PtPin */
|
||||||
GPIO_InitStruct.Pin = ADMIN_MODE_Pin;
|
GPIO_InitStruct.Pin = ADMIN_MODE_Pin;
|
||||||
|
@ -98,8 +98,7 @@ int main(void)
|
|||||||
MX_SPI2_Init();
|
MX_SPI2_Init();
|
||||||
MX_TIM1_Init();
|
MX_TIM1_Init();
|
||||||
MX_USART1_UART_Init();
|
MX_USART1_UART_Init();
|
||||||
MX_IWDG_Init();
|
// MX_IWDG_Init();
|
||||||
MX_USART2_UART_Init();
|
|
||||||
/* USER CODE BEGIN 2 */
|
/* USER CODE BEGIN 2 */
|
||||||
my_setup_2();
|
my_setup_2();
|
||||||
|
|
||||||
|
@ -94,7 +94,6 @@ int main(void)
|
|||||||
MX_TIM1_Init();
|
MX_TIM1_Init();
|
||||||
MX_USART1_UART_Init();
|
MX_USART1_UART_Init();
|
||||||
MX_IWDG_Init();
|
MX_IWDG_Init();
|
||||||
MX_USART2_UART_Init();
|
|
||||||
/* USER CODE BEGIN 2 */
|
/* USER CODE BEGIN 2 */
|
||||||
|
|
||||||
/* USER CODE END 2 */
|
/* USER CODE END 2 */
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
/* USER CODE END 0 */
|
/* USER CODE END 0 */
|
||||||
|
|
||||||
UART_HandleTypeDef huart1;
|
UART_HandleTypeDef huart1;
|
||||||
UART_HandleTypeDef huart2;
|
|
||||||
|
|
||||||
/* USART1 init function */
|
/* USART1 init function */
|
||||||
|
|
||||||
@ -45,25 +44,6 @@ void MX_USART1_UART_Init(void)
|
|||||||
Error_Handler();
|
Error_Handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
/* USART2 init function */
|
|
||||||
|
|
||||||
void MX_USART2_UART_Init(void)
|
|
||||||
{
|
|
||||||
|
|
||||||
huart2.Instance = USART2;
|
|
||||||
huart2.Init.BaudRate = 115200;
|
|
||||||
huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
|
||||||
huart2.Init.StopBits = UART_STOPBITS_1;
|
|
||||||
huart2.Init.Parity = UART_PARITY_NONE;
|
|
||||||
huart2.Init.Mode = UART_MODE_TX_RX;
|
|
||||||
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
||||||
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
||||||
if (HAL_UART_Init(&huart2) != HAL_OK)
|
|
||||||
{
|
|
||||||
Error_Handler();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
||||||
@ -100,33 +80,6 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
|||||||
|
|
||||||
/* USER CODE END USART1_MspInit 1 */
|
/* USER CODE END USART1_MspInit 1 */
|
||||||
}
|
}
|
||||||
else if(uartHandle->Instance==USART2)
|
|
||||||
{
|
|
||||||
/* USER CODE BEGIN USART2_MspInit 0 */
|
|
||||||
|
|
||||||
/* USER CODE END USART2_MspInit 0 */
|
|
||||||
/* USART2 clock enable */
|
|
||||||
__HAL_RCC_USART2_CLK_ENABLE();
|
|
||||||
|
|
||||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
||||||
/**USART2 GPIO Configuration
|
|
||||||
PA2 ------> USART2_TX
|
|
||||||
PA3 ------> USART2_RX
|
|
||||||
*/
|
|
||||||
GPIO_InitStruct.Pin = MODEM_TX_Pin;
|
|
||||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
||||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
||||||
HAL_GPIO_Init(MODEM_TX_GPIO_Port, &GPIO_InitStruct);
|
|
||||||
|
|
||||||
GPIO_InitStruct.Pin = MODEM_RX_Pin;
|
|
||||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
||||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
||||||
HAL_GPIO_Init(MODEM_RX_GPIO_Port, &GPIO_InitStruct);
|
|
||||||
|
|
||||||
/* USER CODE BEGIN USART2_MspInit 1 */
|
|
||||||
|
|
||||||
/* USER CODE END USART2_MspInit 1 */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
||||||
@ -152,24 +105,6 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
|||||||
|
|
||||||
/* USER CODE END USART1_MspDeInit 1 */
|
/* USER CODE END USART1_MspDeInit 1 */
|
||||||
}
|
}
|
||||||
else if(uartHandle->Instance==USART2)
|
|
||||||
{
|
|
||||||
/* USER CODE BEGIN USART2_MspDeInit 0 */
|
|
||||||
|
|
||||||
/* USER CODE END USART2_MspDeInit 0 */
|
|
||||||
/* Peripheral clock disable */
|
|
||||||
__HAL_RCC_USART2_CLK_DISABLE();
|
|
||||||
|
|
||||||
/**USART2 GPIO Configuration
|
|
||||||
PA2 ------> USART2_TX
|
|
||||||
PA3 ------> USART2_RX
|
|
||||||
*/
|
|
||||||
HAL_GPIO_DeInit(GPIOA, MODEM_TX_Pin|MODEM_RX_Pin);
|
|
||||||
|
|
||||||
/* USER CODE BEGIN USART2_MspDeInit 1 */
|
|
||||||
|
|
||||||
/* USER CODE END USART2_MspDeInit 1 */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* USER CODE BEGIN 1 */
|
/* USER CODE BEGIN 1 */
|
||||||
|
@ -97,11 +97,14 @@ C_SOURCES += \
|
|||||||
User/Src/ports.c \
|
User/Src/ports.c \
|
||||||
User/Src/wizHelper.c \
|
User/Src/wizHelper.c \
|
||||||
User/Src/networkAbstractionLayer_lan.c
|
User/Src/networkAbstractionLayer_lan.c
|
||||||
|
NETWORK_STACK=1
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(NETWORK), WiFi)
|
ifeq ($(NETWORK), WiFi)
|
||||||
C_SOURCES += \
|
C_SOURCES += \
|
||||||
User/Src/networkAbstractionLayer_wifi.c
|
User/Src/networkAbstractionLayer_wifi.c \
|
||||||
|
User/Src/modemCom.c
|
||||||
|
NETWORK_STACK=2
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# ASM sources
|
# ASM sources
|
||||||
@ -181,7 +184,7 @@ endif
|
|||||||
# compile gcc flags
|
# compile gcc flags
|
||||||
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections
|
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections
|
||||||
|
|
||||||
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -DNETWORK=$(NETWORK) -DVERSION="\"$(VERSION)\"" -Wall -Werror -fdata-sections -ffunction-sections
|
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -DNETWORK_STACK=$(NETWORK_STACK) -DVERSION="\"$(VERSION)\"" -Wall -Werror -fdata-sections -ffunction-sections
|
||||||
|
|
||||||
ifeq ($(DEBUG), 1)
|
ifeq ($(DEBUG), 1)
|
||||||
CFLAGS += -g -gdwarf-2
|
CFLAGS += -g -gdwarf-2
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
uint64_t networkSntpQuery();
|
uint64_t networkSntpQuery();
|
||||||
|
|
||||||
|
|
||||||
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen);
|
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen);
|
||||||
void networkImplInit();
|
void networkImplInit();
|
||||||
|
|
||||||
|
@ -1,62 +1,12 @@
|
|||||||
#include <modemCom.h>
|
#include <modemCom.h>
|
||||||
#include <main.h>
|
|
||||||
#include <usart.h>
|
#include <usart.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void modemISR() {
|
void modemTxCpltCallback(UART_HandleTypeDef *huart) {
|
||||||
uint32_t isrflags = READ_REG(modemUart.Instance->SR);
|
|
||||||
uint32_t cr1its = READ_REG(modemUart.Instance->CR1);
|
|
||||||
|
|
||||||
// RXNEIE doesn't need to be considered since it is always on and more over the
|
|
||||||
// RXNE flag is cleared by reading the DR, which is done in any case
|
|
||||||
if (((isrflags & USART_SR_RXNE) != RESET) || ((isrflags & (USART_SR_ORE | USART_SR_FE | USART_SR_PE | USART_SR_NE)) != RESET)) {
|
|
||||||
// Error flags are only valid together with the RX flag.
|
|
||||||
// They will be cleared by reading SR (already done above) followed by reading DR (below).
|
|
||||||
bool errorFound = false;
|
|
||||||
if ((isrflags & USART_SR_ORE) != RESET) {
|
|
||||||
mbusCommStats.uartOverrunCnt += 1;
|
|
||||||
errorFound = true;
|
|
||||||
}
|
|
||||||
if ((isrflags & USART_SR_FE) != RESET) {
|
|
||||||
mbusCommStats.uartFramingErrCnt += 1;
|
|
||||||
errorFound = true;
|
|
||||||
}
|
|
||||||
if ((isrflags & USART_SR_PE) != RESET) {
|
|
||||||
mbusCommStats.uartParityErrCnt += 1;
|
|
||||||
errorFound = true;
|
|
||||||
}
|
|
||||||
if ((isrflags & USART_SR_NE) != RESET) {
|
|
||||||
mbusCommStats.uartNoiseErrCnt += 1;
|
|
||||||
errorFound = true;
|
|
||||||
}
|
|
||||||
mbusCommStats.uartOctetCnt += 1;
|
|
||||||
// it is required to read the DR in any case here, not only when the buffer has space
|
|
||||||
// otherwise the interrupt flag won't be reset, particularly important in case of ORE
|
|
||||||
uint8_t data = (uint8_t)(mbusUart.Instance->DR & (uint8_t)0x00FF);
|
|
||||||
if ((! errorFound) &&
|
|
||||||
(mbusCommHandle.receiveBuffer.writeIdx < mbusCommHandle.receiveBuffer.size)) {
|
|
||||||
mbusCommHandle.receiveBuffer.buffer[mbusCommHandle.receiveBuffer.writeIdx] = data;
|
|
||||||
mbusCommHandle.receiveBuffer.writeIdx += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TXEIE needs to be considered since TXE is cleared by writing the DR, which isn't done
|
void modemRxCpltCallback(UART_HandleTypeDef *huart) {
|
||||||
// after the last octet sent
|
|
||||||
if (((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET)) {
|
|
||||||
if (mbusCommHandle.sendBuffer.readIdx < mbusCommHandle.sendBuffer.writeIdx) {
|
|
||||||
mbusUart.Instance->DR = mbusCommHandle.sendBuffer.buffer[mbusCommHandle.sendBuffer.readIdx];
|
|
||||||
mbusCommHandle.sendBuffer.readIdx += 1;
|
|
||||||
if (mbusCommHandle.sendBuffer.readIdx == mbusCommHandle.sendBuffer.writeIdx) {
|
|
||||||
__HAL_UART_DISABLE_IT(&mbusUart, UART_IT_TXE);
|
|
||||||
__HAL_UART_ENABLE_IT(&mbusUart, UART_IT_TC);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// advance the state for the engine only when the last octet is shifted out completely
|
|
||||||
if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET)) {
|
|
||||||
__HAL_UART_DISABLE_IT(&mbusUart, UART_IT_TC);
|
|
||||||
mbusCommHandle.state = MBCS_SENDING_DONE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,135 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <networkAbstractionLayer_impl.h>
|
#include <networkAbstractionLayer_impl.h>
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
|
#include <PontCoopScheduler.h>
|
||||||
#include <wizHelper.h>
|
#include <wizHelper.h>
|
||||||
#include <wizchip_conf.h>
|
#include <wizchip_conf.h>
|
||||||
#include <socket.h>
|
#include <socket.h>
|
||||||
|
#include <wizchip_conf.h>
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
|
static t_configBlock *config;
|
||||||
|
|
||||||
|
|
||||||
|
const uint16_t MAX_RECV_RETRY_COUNT = 100;
|
||||||
|
const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
|
||||||
|
|
||||||
|
const uint8_t SEND_LI_VN_MODE = 0xe3; // LI: unknown (3), VN: 4, Mode: Client (3)
|
||||||
|
typedef struct {
|
||||||
|
uint8_t li_vn_mode;
|
||||||
|
uint8_t stratum;
|
||||||
|
uint8_t poll;
|
||||||
|
uint8_t precision;
|
||||||
|
uint32_t rootdelay;
|
||||||
|
uint32_t rootdisp;
|
||||||
|
uint32_t refid;
|
||||||
|
uint64_t reftime;
|
||||||
|
uint64_t org;
|
||||||
|
uint64_t rec;
|
||||||
|
uint64_t xmt;
|
||||||
|
} ntpMsg_t;
|
||||||
|
|
||||||
|
extern uint8_t SNTP_SOCK;
|
||||||
|
|
||||||
|
const uint16_t NTP_PORT = 123;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
SNTP_STATE_IDLE,
|
||||||
|
SNTP_STATE_START,
|
||||||
|
SNTP_STATE_SEND,
|
||||||
|
SNTP_STATE_RECV,
|
||||||
|
SNTP_STATE_ERROR,
|
||||||
|
SNTP_STATE_DONE
|
||||||
|
} sntpState = SNTP_STATE_IDLE;
|
||||||
|
uint64_t seconds;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
static void networkSntpEngine(void *handle) {
|
||||||
|
static uint16_t retryCount = 0;
|
||||||
|
|
||||||
|
coloredMsg(LOG_BLUE, "nes, %u", sntpState);
|
||||||
|
|
||||||
|
switch (sntpState) {
|
||||||
|
case SNTP_STATE_START:
|
||||||
|
coloredMsg(LOG_BLUE, "nes, about to send");
|
||||||
|
uint8_t ntpAddr[4];
|
||||||
|
if (! wizDnsQuery(config->ntpServer, ntpAddr)) {
|
||||||
|
coloredMsg(LOG_BLUE, "nes, failed to resolve ntp server");
|
||||||
|
sntpState = SNTP_STATE_ERROR;
|
||||||
|
} else {
|
||||||
|
socket(SNTP_SOCK, Sn_MR_UDP, NTP_PORT, 0);
|
||||||
|
retryCount = 0;
|
||||||
|
uint8_t sockState = getSn_SR(SNTP_SOCK);
|
||||||
|
if (sockState == SOCK_UDP) {
|
||||||
|
ntpMsg_t ntpMsg;
|
||||||
|
memset(&ntpMsg, 0, sizeof(ntpMsg));
|
||||||
|
ntpMsg.li_vn_mode = SEND_LI_VN_MODE;
|
||||||
|
sendto(SNTP_SOCK, (uint8_t*)&ntpMsg, sizeof(ntpMsg), ntpAddr, NTP_PORT);
|
||||||
|
coloredMsg(LOG_BLUE, "nes, sent");
|
||||||
|
sntpState = SNTP_STATE_RECV;
|
||||||
|
schAdd(networkSntpEngine, NULL, 1000, 0);
|
||||||
|
} else {
|
||||||
|
coloredMsg(LOG_BLUE, "nes, socket in unexpected state: %d", sockState);
|
||||||
|
sntpState = SNTP_STATE_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SNTP_STATE_RECV:
|
||||||
|
coloredMsg(LOG_BLUE, "nes, check receive");
|
||||||
|
uint16_t recvLen = getSn_RX_RSR(SNTP_SOCK);
|
||||||
|
if (recvLen == 0) {
|
||||||
|
retryCount += 1;
|
||||||
|
if (retryCount > MAX_RECV_RETRY_COUNT) {
|
||||||
|
coloredMsg(LOG_BLUE, "nes max retry count reached, failed");
|
||||||
|
sntpState = SNTP_STATE_ERROR;
|
||||||
|
} else {
|
||||||
|
coloredMsg(LOG_BLUE, "nes, nothing received yet, try again");
|
||||||
|
schAdd(networkSntpEngine, NULL, 100, 0);
|
||||||
|
}
|
||||||
|
} else if (recvLen == sizeof(ntpMsg_t)) {
|
||||||
|
ntpMsg_t ntpMsg;
|
||||||
|
memset(&ntpMsg, 0, sizeof(ntpMsg_t));
|
||||||
|
uint8_t srcAddr[4];
|
||||||
|
uint16_t srcPort;
|
||||||
|
recvfrom(SNTP_SOCK, (uint8_t*)&ntpMsg, sizeof(ntpMsg_t), srcAddr, &srcPort);
|
||||||
|
close(SNTP_SOCK);
|
||||||
|
coloredMsg(LOG_BLUE, "nes, msg received from %d.%d.%d.%d:%d",
|
||||||
|
srcAddr[0], srcAddr[1], srcAddr[2], srcAddr[3],
|
||||||
|
srcPort);
|
||||||
|
coloredMsg(LOG_BLUE, "nes, received in the %d. cycles", retryCount);
|
||||||
|
seconds = ntpMsg.rec - UNIX_NTP_EPOCH_DIFF;
|
||||||
|
coloredMsg(LOG_BLUE, "nes, seconds: %lu", seconds);
|
||||||
|
sntpState = SNTP_STATE_DONE;
|
||||||
|
} else {
|
||||||
|
coloredMsg(LOG_BLUE, "nes, invalid number of octets received: %d", recvLen);
|
||||||
|
sntpState = SNTP_STATE_ERROR;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
coloredMsg(LOG_BLUE, "nes, unexpected state");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
uint64_t networkSntpQuery() {
|
uint64_t networkSntpQuery() {
|
||||||
return wizSntpQuery();
|
/*
|
||||||
|
uint64_t res = 0;
|
||||||
|
if (sntpState == SNTP_STATE_IDLE) {
|
||||||
|
sntpState = SNTP_STATE_START;
|
||||||
|
schAdd(networkSntpEngine, NULL, 1, 0);
|
||||||
|
} else if (sntpState == SNTP_STATE_ERROR) {
|
||||||
|
sntpState = SNTP_STATE_IDLE;
|
||||||
|
} else if (sntpState == SNTP_STATE_DONE) {
|
||||||
|
sntpState = SNTP_STATE_IDLE;
|
||||||
|
res = seconds;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
*/
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -39,5 +160,6 @@ int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufL
|
|||||||
}
|
}
|
||||||
|
|
||||||
void networkImplInit() {
|
void networkImplInit() {
|
||||||
|
config = getConfig();
|
||||||
wizInit();
|
wizInit();
|
||||||
}
|
}
|
||||||
|
@ -26,13 +26,9 @@ static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
|
|||||||
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
|
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
|
||||||
|
|
||||||
|
|
||||||
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
|
|
||||||
const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
|
|
||||||
|
|
||||||
|
|
||||||
extern const uint8_t DHCP_SOCK;
|
extern const uint8_t DHCP_SOCK;
|
||||||
extern const uint8_t DNS_SOCK;
|
extern const uint8_t DNS_SOCK;
|
||||||
extern const uint8_t SNTP_SOCK;
|
|
||||||
|
|
||||||
|
|
||||||
static bool networkAvailable = false;
|
static bool networkAvailable = false;
|
||||||
@ -133,40 +129,6 @@ bool wizDnsQuery(char *name, uint8_t *ip) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint64_t wizSntpQuery() {
|
|
||||||
uint64_t seconds = 0;
|
|
||||||
|
|
||||||
if (networkAvailable) {
|
|
||||||
coloredMsg(LOG_BLUE, "wizsq, about to call SNTP");
|
|
||||||
|
|
||||||
uint8_t ntpServer[4];
|
|
||||||
if (wizDnsQuery(config->ntpServer, ntpServer)) {
|
|
||||||
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
|
|
||||||
uint16_t cycles = 0;
|
|
||||||
uint32_t startTime = HAL_GetTick();
|
|
||||||
while (1) {
|
|
||||||
cycles += 1;
|
|
||||||
datetime curTime;
|
|
||||||
if (1 == SNTP_run(&curTime)) {
|
|
||||||
seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
|
|
||||||
coloredMsg(LOG_BLUE, "wizsq, cycles: %u, curTime: %lu", cycles, seconds);
|
|
||||||
uint32_t stopTime = HAL_GetTick();
|
|
||||||
coloredMsg(LOG_BLUE, "wizsq, duration: %u ms", stopTime - startTime);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (seconds == 0) {
|
|
||||||
coloredMsg(LOG_BLUE, "wizsq, time update failed");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
coloredMsg(LOG_BLUE, "wizsq, error when querying ntp server name");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return seconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void wizPhyLinkHandler(void *handle) {
|
static void wizPhyLinkHandler(void *handle) {
|
||||||
// this handler is anyhow called with a 1s period, so we reuse it for the DNS timer
|
// this handler is anyhow called with a 1s period, so we reuse it for the DNS timer
|
||||||
DNS_time_handler();
|
DNS_time_handler();
|
||||||
|
@ -13,39 +13,35 @@ Mcu.IP4=SPI2
|
|||||||
Mcu.IP5=SYS
|
Mcu.IP5=SYS
|
||||||
Mcu.IP6=TIM1
|
Mcu.IP6=TIM1
|
||||||
Mcu.IP7=USART1
|
Mcu.IP7=USART1
|
||||||
Mcu.IP8=USART2
|
Mcu.IPNb=8
|
||||||
Mcu.IPNb=9
|
|
||||||
Mcu.Name=STM32F103C(8-B)Tx
|
Mcu.Name=STM32F103C(8-B)Tx
|
||||||
Mcu.Package=LQFP48
|
Mcu.Package=LQFP48
|
||||||
Mcu.Pin0=PC13-TAMPER-RTC
|
Mcu.Pin0=PC13-TAMPER-RTC
|
||||||
Mcu.Pin1=PC14-OSC32_IN
|
Mcu.Pin1=PC14-OSC32_IN
|
||||||
Mcu.Pin10=PA6
|
Mcu.Pin10=PB11
|
||||||
Mcu.Pin11=PA7
|
Mcu.Pin11=PB12
|
||||||
Mcu.Pin12=PB10
|
Mcu.Pin12=PB13
|
||||||
Mcu.Pin13=PB11
|
Mcu.Pin13=PB14
|
||||||
Mcu.Pin14=PB12
|
Mcu.Pin14=PB15
|
||||||
Mcu.Pin15=PB13
|
Mcu.Pin15=PA8
|
||||||
Mcu.Pin16=PB14
|
Mcu.Pin16=PA9
|
||||||
Mcu.Pin17=PB15
|
Mcu.Pin17=PA10
|
||||||
Mcu.Pin18=PA8
|
Mcu.Pin18=PA12
|
||||||
Mcu.Pin19=PA9
|
Mcu.Pin19=PA13
|
||||||
Mcu.Pin2=PC15-OSC32_OUT
|
Mcu.Pin2=PC15-OSC32_OUT
|
||||||
Mcu.Pin20=PA10
|
Mcu.Pin20=PA14
|
||||||
Mcu.Pin21=PA12
|
Mcu.Pin21=PB5
|
||||||
Mcu.Pin22=PA13
|
Mcu.Pin22=PB6
|
||||||
Mcu.Pin23=PA14
|
Mcu.Pin23=VP_IWDG_VS_IWDG
|
||||||
Mcu.Pin24=PB5
|
Mcu.Pin24=VP_SYS_VS_Systick
|
||||||
Mcu.Pin25=PB6
|
|
||||||
Mcu.Pin26=VP_IWDG_VS_IWDG
|
|
||||||
Mcu.Pin27=VP_SYS_VS_Systick
|
|
||||||
Mcu.Pin3=PD0-OSC_IN
|
Mcu.Pin3=PD0-OSC_IN
|
||||||
Mcu.Pin4=PD1-OSC_OUT
|
Mcu.Pin4=PD1-OSC_OUT
|
||||||
Mcu.Pin5=PA1
|
Mcu.Pin5=PA4
|
||||||
Mcu.Pin6=PA2
|
Mcu.Pin6=PA5
|
||||||
Mcu.Pin7=PA3
|
Mcu.Pin7=PA6
|
||||||
Mcu.Pin8=PA4
|
Mcu.Pin8=PA7
|
||||||
Mcu.Pin9=PA5
|
Mcu.Pin9=PB10
|
||||||
Mcu.PinsNb=28
|
Mcu.PinsNb=25
|
||||||
Mcu.ThirdPartyNb=0
|
Mcu.ThirdPartyNb=0
|
||||||
Mcu.UserConstants=eepromSpi,hspi1;etherSpi,hspi2;debugUart,huart1;mainsCnt,htim1
|
Mcu.UserConstants=eepromSpi,hspi1;etherSpi,hspi2;debugUart,huart1;mainsCnt,htim1
|
||||||
Mcu.UserName=STM32F103C8Tx
|
Mcu.UserName=STM32F103C8Tx
|
||||||
@ -65,10 +61,6 @@ NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true\:false\:true
|
|||||||
NVIC.TIM1_CC_IRQn=true\:0\:0\:false\:false\:true\:true\:true
|
NVIC.TIM1_CC_IRQn=true\:0\:0\:false\:false\:true\:true\:true
|
||||||
NVIC.USART1_IRQn=true\:0\:0\:false\:false\:true\:true\:true
|
NVIC.USART1_IRQn=true\:0\:0\:false\:false\:true\:true\:true
|
||||||
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||||
PA1.GPIOParameters=GPIO_Label
|
|
||||||
PA1.GPIO_Label=MODEM_RES
|
|
||||||
PA1.Locked=true
|
|
||||||
PA1.Signal=GPIO_Output
|
|
||||||
PA10.GPIOParameters=GPIO_Label
|
PA10.GPIOParameters=GPIO_Label
|
||||||
PA10.GPIO_Label=Debug_RX
|
PA10.GPIO_Label=Debug_RX
|
||||||
PA10.Mode=Asynchronous
|
PA10.Mode=Asynchronous
|
||||||
@ -80,14 +72,6 @@ PA13.Mode=Serial_Wire
|
|||||||
PA13.Signal=SYS_JTMS-SWDIO
|
PA13.Signal=SYS_JTMS-SWDIO
|
||||||
PA14.Mode=Serial_Wire
|
PA14.Mode=Serial_Wire
|
||||||
PA14.Signal=SYS_JTCK-SWCLK
|
PA14.Signal=SYS_JTCK-SWCLK
|
||||||
PA2.GPIOParameters=GPIO_Label
|
|
||||||
PA2.GPIO_Label=MODEM_TX
|
|
||||||
PA2.Mode=Asynchronous
|
|
||||||
PA2.Signal=USART2_TX
|
|
||||||
PA3.GPIOParameters=GPIO_Label
|
|
||||||
PA3.GPIO_Label=MODEM_RX
|
|
||||||
PA3.Mode=Asynchronous
|
|
||||||
PA3.Signal=USART2_RX
|
|
||||||
PA4.GPIOParameters=GPIO_Label
|
PA4.GPIOParameters=GPIO_Label
|
||||||
PA4.GPIO_Label=EEPROM_CS
|
PA4.GPIO_Label=EEPROM_CS
|
||||||
PA4.Locked=true
|
PA4.Locked=true
|
||||||
@ -187,7 +171,7 @@ ProjectManager.StackSize=0x400
|
|||||||
ProjectManager.TargetToolchain=Makefile
|
ProjectManager.TargetToolchain=Makefile
|
||||||
ProjectManager.ToolChainLocation=
|
ProjectManager.ToolChainLocation=
|
||||||
ProjectManager.UnderRoot=false
|
ProjectManager.UnderRoot=false
|
||||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_SPI1_Init-SPI1-false-HAL-true,4-MX_SPI2_Init-SPI2-false-HAL-true,5-MX_TIM1_Init-TIM1-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,7-MX_IWDG_Init-IWDG-false-HAL-true
|
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_SPI1_Init-SPI1-false-HAL-true,4-MX_SPI2_Init-SPI2-false-HAL-true,5-MX_TIM1_Init-TIM1-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,7-MX_IWDG_Init-IWDG-false-HAL-true,8-MX_USART2_UART_Init-USART2-false-HAL-true
|
||||||
RCC.ADCFreqValue=36000000
|
RCC.ADCFreqValue=36000000
|
||||||
RCC.AHBFreq_Value=72000000
|
RCC.AHBFreq_Value=72000000
|
||||||
RCC.APB1CLKDivider=RCC_HCLK_DIV2
|
RCC.APB1CLKDivider=RCC_HCLK_DIV2
|
||||||
@ -230,8 +214,6 @@ TIM1.IPParameters=Channel-Input_Capture1_from_TI1,Prescaler,ClockDivision
|
|||||||
TIM1.Prescaler=13
|
TIM1.Prescaler=13
|
||||||
USART1.IPParameters=VirtualMode
|
USART1.IPParameters=VirtualMode
|
||||||
USART1.VirtualMode=VM_ASYNC
|
USART1.VirtualMode=VM_ASYNC
|
||||||
USART2.IPParameters=VirtualMode
|
|
||||||
USART2.VirtualMode=VM_ASYNC
|
|
||||||
VP_IWDG_VS_IWDG.Mode=IWDG_Activate
|
VP_IWDG_VS_IWDG.Mode=IWDG_Activate
|
||||||
VP_IWDG_VS_IWDG.Signal=IWDG_VS_IWDG
|
VP_IWDG_VS_IWDG.Signal=IWDG_VS_IWDG
|
||||||
VP_SYS_VS_Systick.Mode=SysTick
|
VP_SYS_VS_Systick.Mode=SysTick
|
||||||
|
@ -49,10 +49,7 @@ cp $IT_C $IT_C_BAK
|
|||||||
echo "// $PROCESSED" > $IT_C
|
echo "// $PROCESSED" > $IT_C
|
||||||
cat $IT_C_BAK | \
|
cat $IT_C_BAK | \
|
||||||
sed -e 's,\(/\* USER CODE BEGIN Includes \*/\),\1\n#include "main2.h"\n,' \
|
sed -e 's,\(/\* USER CODE BEGIN Includes \*/\),\1\n#include "main2.h"\n,' \
|
||||||
-e 's,\(/\* USER CODE BEGIN PFP \*/\),\1\nvoid modemISR();\n,' \
|
|
||||||
-e 's,\(/\* USER CODE BEGIN SysTick_IRQn 1 \*/\),\1\n SYSTICK_Callback();\n,' \
|
-e 's,\(/\* USER CODE BEGIN SysTick_IRQn 1 \*/\),\1\n SYSTICK_Callback();\n,' \
|
||||||
-e 's,\(HAL_UART_IRQHandler(&huart2);\),// \1,' \
|
|
||||||
-e 's,\(/\* USER CODE BEGIN UART5_IRQn 1 \*/\),\1\n modemISR();\n,' \
|
|
||||||
>> $IT_C
|
>> $IT_C
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user