diff --git a/Drivers/peripheral/inc/tremo_rcc.h b/Drivers/peripheral/inc/tremo_rcc.h index d64c551..5f9476d 100644 --- a/Drivers/peripheral/inc/tremo_rcc.h +++ b/Drivers/peripheral/inc/tremo_rcc.h @@ -28,9 +28,8 @@ extern "C" { #define RCC_FREQ_32M ((uint32_t)32000000) #define RCC_FREQ_30M ((uint32_t)30000000) #define RCC_FREQ_24M ((uint32_t)24000000) -#define RCC_FREQ_4M ((uint32_t)4000000) -#define RCC_FREQ_32768 ((uint32_t)32768) -#define RCC_FREQ_32000 ((uint32_t)32000) +#define RCC_FREQ_4M ((uint32_t)3600000) +#define RCC_FREQ_32768 ((uint32_t)32768) #define RCC_WAIT() \ do { \ diff --git a/Drivers/peripheral/inc/tremo_regs.h b/Drivers/peripheral/inc/tremo_regs.h index 5318f51..afa9f2b 100644 --- a/Drivers/peripheral/inc/tremo_regs.h +++ b/Drivers/peripheral/inc/tremo_regs.h @@ -464,8 +464,8 @@ typedef struct __RTC { __IO uint32_t CALENDAR_H; /*!< time year/month/date*/ __IO uint32_t CYC_MAX; /*!< cyc max value*/ __IO uint32_t SR; /*!< status register*/ - __I uint32_t SYN_DATA; /*!< syncronization time hour/minute/second*/ - __I uint32_t SYN_DATA_H; /*!< syncronization time year/month/date*/ + __I uint32_t ASYN_DATA; /*!< asynchronization time hour/minute/second*/ + __I uint32_t ASYN_DATA_H; /*!< asynchronization time year/month/date*/ __IO uint32_t CR1; /*!< control register 1*/ __IO uint32_t SR1; /*!< status register 1*/ __IO uint32_t CR2; /*!< control register 2*/ @@ -524,11 +524,11 @@ typedef struct { #define UART_CR_UART_EN ((uint32_t)0x00000001) -#define UART_CR_UART_MODE ((uint32_t)0x00000030) +#define UART_CR_UART_MODE ((uint32_t)0x00000300) #define UART_CR_UART_MODE_NONE ((uint32_t)0x00000000) -#define UART_CR_UART_MODE_RX ((uint32_t)0x00000020) -#define UART_CR_UART_MODE_TX ((uint32_t)0x00000010) -#define UART_CR_UART_MODE_TXRX ((uint32_t)0x00000030) +#define UART_CR_UART_MODE_RX ((uint32_t)0x00000200) +#define UART_CR_UART_MODE_TX ((uint32_t)0x00000100) +#define UART_CR_UART_MODE_TXRX ((uint32_t)0x00000300) #define UART_CR_FLOW_CTRL ((uint32_t)0x0000C000) #define UART_CR_FLOW_CTRL_NONE ((uint32_t)0x00000000) diff --git a/Drivers/peripheral/inc/tremo_spi.h b/Drivers/peripheral/inc/tremo_spi.h index b284c8f..b30bcd5 100644 --- a/Drivers/peripheral/inc/tremo_spi.h +++ b/Drivers/peripheral/inc/tremo_spi.h @@ -51,6 +51,7 @@ extern "C" { #define SSP_INTERRUPT_RX_TIMEOUT (1 << 1) /*!< RX timeout interrupt*/ #define SSP_INTERRUPT_RX_FIFO_TRIGGER (1 << 2) /*!< RX fifo trigger interrupt*/ #define SSP_INTERRUPT_TX_FIFO_TRIGGER (1 << 3) /*!< TX fifo trigger interrupt*/ +#define SSP_INTERRUPT_RX_OVERRUN_AND_TIMEOUT (0x3) /*!< RX fifo overrun and RX timeout interrupt*/ #define SSP_INTERRUPT_ALL (0xf) /*!< All interrupt*/ #define SSP_DMA_TX_EN (1 << 1) /*!< TX DMA enable*/ @@ -76,9 +77,7 @@ typedef struct { * @param ssp_interrupt SSP interrupt * @arg SSP_INTERRUPT_RX_FIFO_OVERRUN: RX fifo overrun interrupt * @arg SSP_INTERRUPT_RX_TIMEOUT: RX timeout interrupt - * @arg SSP_INTERRUPT_RX_FIFO_TRIGGER: RX fifo trigger interrupt - * @arg SSP_INTERRUPT_TX_FIFO_TRIGGER: TX fifo trigger interrupt - * @arg SSP_INTERRUPT_ALL: All interrupt + * @arg SSP_INTERRUPT_RX_OVERRUN_AND_TIMEOUT: RX fifo overrun and RX timeout interrupt * @return */ __STATIC_INLINE void ssp_clear_interrupt(ssp_typedef_t* SSPx, uint8_t ssp_interrupt) diff --git a/Drivers/peripheral/src/tremo_dma.c b/Drivers/peripheral/src/tremo_dma.c index 29ef088..5de8b44 100644 --- a/Drivers/peripheral/src/tremo_dma.c +++ b/Drivers/peripheral/src/tremo_dma.c @@ -78,9 +78,6 @@ void dma0_IRQHandler(void) dma_ch = 3; } - if (g_dma_callback_handler[0][dma_ch]) { - g_dma_callback_handler[0][dma_ch](); - } if (dma_ch == 0) { /*clear TFR int,channelx*/ TREMO_REG_WR(DMA_CLEAR_TFR_L_REG(0), 0x1); @@ -102,6 +99,9 @@ void dma0_IRQHandler(void) /*clear block int,channelx*/ TREMO_REG_WR(DMA_CLEAR_BLOCK_L_REG(0), 0x8); } + if (g_dma_callback_handler[0][dma_ch]) { + g_dma_callback_handler[0][dma_ch](); + } } void dma1_IRQHandler(void) @@ -118,9 +118,6 @@ void dma1_IRQHandler(void) dma_ch = 3; } - if (g_dma_callback_handler[1][dma_ch]) { - g_dma_callback_handler[1][dma_ch](); - } if (dma_ch == 0) { /*clear TFR int,channelx*/ TREMO_REG_WR(DMA_CLEAR_TFR_L_REG(1), 0x1); @@ -142,6 +139,9 @@ void dma1_IRQHandler(void) /*clear block int,channelx*/ TREMO_REG_WR(DMA_CLEAR_BLOCK_L_REG(1), 0x8); } + if (g_dma_callback_handler[1][dma_ch]) { + g_dma_callback_handler[1][dma_ch](); + } } static void set_dma_mode(dma_dev_t* dma, dma_config_reg_t* config_reg) diff --git a/Drivers/peripheral/src/tremo_gpio.c b/Drivers/peripheral/src/tremo_gpio.c index 62e12fc..164660e 100644 --- a/Drivers/peripheral/src/tremo_gpio.c +++ b/Drivers/peripheral/src/tremo_gpio.c @@ -97,7 +97,7 @@ void gpio_init(gpio_t* gpiox, uint8_t gpio_pin, gpio_mode_t mode) gpiox->ODR &= ~(1 << gpio_pin); gpiox->IER &= ~(1 << gpio_pin); gpiox->OER |= (1 << gpio_pin); - gpiox->PSR &= ~(1 << gpio_pin); + gpiox->PSR |= (1 << gpio_pin); } else { gpiox->OER &= ~(1 << gpio_pin); gpiox->IER &= ~(1 << gpio_pin); @@ -111,7 +111,7 @@ void gpio_init(gpio_t* gpiox, uint8_t gpio_pin, gpio_mode_t mode) gpiox->ODR &= ~(1 << gpio_pin); gpiox->IER &= ~(1 << gpio_pin); gpiox->OER &= ~(1 << gpio_pin); - gpiox->PSR &= ~(1 << gpio_pin); + gpiox->PSR |= (1 << gpio_pin); } else { gpiox->OER &= ~(1 << gpio_pin); gpiox->IER &= ~(1 << gpio_pin); @@ -160,11 +160,35 @@ void gpio_init(gpio_t* gpiox, uint8_t gpio_pin, gpio_mode_t mode) void gpio_write(gpio_t* gpiox, uint8_t gpio_pin, gpio_level_t level) { assert_param(IS_GPIO_PIN(gpiox, gpio_pin)); - - if (level != GPIO_LEVEL_LOW) - gpiox->BSR |= 1 << gpio_pin; - else - gpiox->BRR |= 1 << gpio_pin; + if (gpiox == GPIOD && gpio_pin > GPIO_PIN_7) { + if (((gpiox->ODR & (1 << gpio_pin)) == 0 ) && ((gpiox->IER & (1 << gpio_pin)) == 0 ) \ + && ((gpiox->OER & (1 << gpio_pin)) != 0) && ((gpiox->PSR & (1 << gpio_pin)) != 0)) { + if (level == GPIO_LEVEL_LOW) { + gpiox->ODR &= ~(1 << gpio_pin); + gpiox->IER &= ~(1 << gpio_pin); + gpiox->OER &= ~(1 << gpio_pin); + gpiox->PSR |= (1 << gpio_pin); + } + } else if (((gpiox->ODR & (1 << gpio_pin)) == 0 ) && ((gpiox->IER & (1 << gpio_pin)) == 0 ) && \ + ((gpiox->OER & (1 << gpio_pin)) == 0) && ((gpiox->PSR & (1 << gpio_pin)) != 0)) { + if (level != GPIO_LEVEL_LOW) { + gpiox->ODR &= ~(1 << gpio_pin); + gpiox->IER &= ~(1 << gpio_pin); + gpiox->OER |= (1 << gpio_pin); + gpiox->PSR |= (1 << gpio_pin); + } + } else { + if (level != GPIO_LEVEL_LOW) + gpiox->BSR |= 1 << gpio_pin; + else + gpiox->BRR |= 1 << gpio_pin; + } + } else { + if (level != GPIO_LEVEL_LOW) + gpiox->BSR |= 1 << gpio_pin; + else + gpiox->BRR |= 1 << gpio_pin; + } } /** diff --git a/Drivers/peripheral/src/tremo_lpuart.c b/Drivers/peripheral/src/tremo_lpuart.c index 987bd7c..4861b15 100644 --- a/Drivers/peripheral/src/tremo_lpuart.c +++ b/Drivers/peripheral/src/tremo_lpuart.c @@ -217,12 +217,10 @@ void lpuart_init(lpuart_t* lpuart, lpuart_init_t* uart_init) uint32_t freq = 0; lpuart_clk_freq = rcc_get_lpuart_clk_source(); - if (lpuart_clk_freq == RCC_CR1_LPUART_CLK_SEL_XO32K) { - freq = 32768; - } else if (lpuart_clk_freq == RCC_CR1_LPUART_CLK_SEL_RCO32K) { - freq = 32000; + if (lpuart_clk_freq == RCC_CR1_LPUART_CLK_SEL_XO32K || lpuart_clk_freq == RCC_CR1_LPUART_CLK_SEL_RCO32K) { + freq = RCC_FREQ_32768; } else { - freq = 4000000; + freq = RCC_FREQ_4M; } if (uart_init->low_level_wakeup == true) { tmp_value |= LPUART_CR0_LOW_LEVEL_WAKEUP; diff --git a/Drivers/peripheral/src/tremo_pwr.c b/Drivers/peripheral/src/tremo_pwr.c index 4b45a86..f867b8c 100644 --- a/Drivers/peripheral/src/tremo_pwr.c +++ b/Drivers/peripheral/src/tremo_pwr.c @@ -10,6 +10,8 @@ void deepsleep(uint32_t mode, uint32_t wfi) { + PWR->CR1 |= 1<<4; + if((TREMO_REG_RD(0x10002010) & 0x3) == 0) TREMO_REG_SET(PWR->CR1, (0xF<<20), (1<<20)); diff --git a/Drivers/peripheral/src/tremo_rcc.c b/Drivers/peripheral/src/tremo_rcc.c index 2e2fd8c..013c0bb 100644 --- a/Drivers/peripheral/src/tremo_rcc.c +++ b/Drivers/peripheral/src/tremo_rcc.c @@ -1,3 +1,4 @@ +#include "tremo_delay.h" #include "tremo_rcc.h" /** @@ -17,10 +18,7 @@ uint32_t rcc_get_clk_freq(rcc_clk_t clk) sysclk_freq = RCC_FREQ_48M; break; } - case RCC_CR0_SYSCLK_SEL_RCO32K: { - sysclk_freq = RCC_FREQ_32000; - break; - } + case RCC_CR0_SYSCLK_SEL_RCO32K: case RCC_CR0_SYSCLK_SEL_XO32K: { sysclk_freq = RCC_FREQ_32768; break; @@ -328,6 +326,7 @@ void rcc_set_lptimer0_clk_source(rcc_lptimer0_clk_source_t clk_source) if (clk_source == RCC_LPTIMER0_CLK_SOURCE_EXTCLK) { TREMO_REG_EN(RCC->CR1, RCC_CR1_LPTIMER0_EXTCLK_SEL_MASK, true); } else { + TREMO_REG_EN(RCC->CR1, RCC_CR1_LPTIMER0_EXTCLK_SEL_MASK, false); TREMO_REG_SET(RCC->CR1, RCC_CR1_LPTIMER0_CLK_SEL_MASK, clk_source); } } @@ -354,6 +353,7 @@ void rcc_set_lptimer1_clk_source(rcc_lptimer1_clk_source_t clk_source) if (clk_source == RCC_LPTIMER1_CLK_SOURCE_EXTCLK) { TREMO_REG_EN(RCC->CR1, RCC_CR1_LPTIMER1_EXTCLK_SEL_MASK, true); } else { + TREMO_REG_EN(RCC->CR1, RCC_CR1_LPTIMER1_EXTCLK_SEL_MASK, false); TREMO_REG_SET(RCC->CR1, RCC_CR1_LPTIMER1_CLK_SEL_MASK, clk_source); } } @@ -812,6 +812,8 @@ void rcc_enable_peripheral_clk(rcc_peripheral_t peripheral, bool new_state) while ((RCC->SR & RCC_SR_ALL_DONE) != RCC_SR_ALL_DONE) ; TREMO_REG_EN(RCC->CGR2, RCC_CGR2_LPUART_AON_CLK_EN_MASK, new_state); + while ((RCC->SR & RCC_SR_LPUART_AON_CLK_EN_DONE) != RCC_SR_LPUART_AON_CLK_EN_DONE) + ; break; } case RCC_PERIPHERAL_SSP0: { @@ -860,6 +862,8 @@ void rcc_enable_peripheral_clk(rcc_peripheral_t peripheral, bool new_state) while ((RCC->SR & RCC_SR_ALL_DONE) != RCC_SR_ALL_DONE) ; TREMO_REG_EN(RCC->CGR2, RCC_CGR2_LCD_AON_CLK_EN_MASK, new_state); + while ((RCC->SR & RCC_SR_LCD_AON_CLK_EN_DONE) != RCC_SR_LCD_AON_CLK_EN_DONE) + ; break; } case RCC_PERIPHERAL_LORA: { @@ -913,6 +917,8 @@ void rcc_enable_peripheral_clk(rcc_peripheral_t peripheral, bool new_state) while ((RCC->SR & RCC_SR_ALL_DONE) != RCC_SR_ALL_DONE) ; TREMO_REG_EN(RCC->CGR2, RCC_CGR2_LPTIMER0_AON_CLK_EN_MASK, new_state); + while ((RCC->SR & RCC_SR_LPTIM_AON_CLK_EN_DONE) != RCC_SR_LPTIM_AON_CLK_EN_DONE) + ; TREMO_REG_EN(RCC->CGR1, RCC_CGR1_LPTIMER0_CLK_EN_MASK, new_state); } else { @@ -921,8 +927,10 @@ void rcc_enable_peripheral_clk(rcc_peripheral_t peripheral, bool new_state) while ((RCC->SR & RCC_SR_ALL_DONE) != RCC_SR_ALL_DONE) ; TREMO_REG_EN(RCC->CGR2, RCC_CGR2_LPTIMER0_AON_CLK_EN_MASK, new_state); + while ((RCC->SR & RCC_SR_LPTIM_AON_CLK_EN_DONE) != RCC_SR_LPTIM_AON_CLK_EN_DONE) + ; - TREMO_REG_EN(RCC->CGR1, RCC_CGR1_LPTIMER0_PCLK_EN_MASK, new_state); + //TREMO_REG_EN(RCC->CGR1, RCC_CGR1_LPTIMER0_PCLK_EN_MASK, new_state); } break; @@ -935,6 +943,8 @@ void rcc_enable_peripheral_clk(rcc_peripheral_t peripheral, bool new_state) while ((RCC->SR & RCC_SR_ALL_DONE) != RCC_SR_ALL_DONE) ; TREMO_REG_EN(RCC->CGR2, RCC_CGR2_LPTIMER1_AON_CLK_EN_MASK, new_state); + while ((RCC->SR & RCC_SR_LPTIMER1_AON_CLK_EN_DONE) != RCC_SR_LPTIMER1_AON_CLK_EN_DONE) + ; TREMO_REG_EN(RCC->CGR1, RCC_CGR1_LPTIMER1_CLK_EN_MASK, new_state); } else { @@ -943,8 +953,10 @@ void rcc_enable_peripheral_clk(rcc_peripheral_t peripheral, bool new_state) while ((RCC->SR & RCC_SR_ALL_DONE) != RCC_SR_ALL_DONE) ; TREMO_REG_EN(RCC->CGR2, RCC_CGR2_LPTIMER1_AON_CLK_EN_MASK, new_state); + while ((RCC->SR & RCC_SR_LPTIMER1_AON_CLK_EN_DONE) != RCC_SR_LPTIMER1_AON_CLK_EN_DONE) + ; - TREMO_REG_EN(RCC->CGR1, RCC_CGR1_LPTIMER1_PCLK_EN_MASK, new_state); + //TREMO_REG_EN(RCC->CGR1, RCC_CGR1_LPTIMER1_PCLK_EN_MASK, new_state); } break; @@ -955,6 +967,8 @@ void rcc_enable_peripheral_clk(rcc_peripheral_t peripheral, bool new_state) while ((RCC->SR & RCC_SR_ALL_DONE) != RCC_SR_ALL_DONE) ; TREMO_REG_EN(RCC->CGR2, RCC_CGR2_IWDG_CLK_EN_MASK, new_state); + while ((RCC->SR & RCC_SR_IWDG_AON_CLK_EN_DONE) != RCC_SR_IWDG_AON_CLK_EN_DONE) + ; break; } @@ -969,6 +983,8 @@ void rcc_enable_peripheral_clk(rcc_peripheral_t peripheral, bool new_state) while ((RCC->SR & RCC_SR_ALL_DONE) != RCC_SR_ALL_DONE) ; TREMO_REG_EN(RCC->CGR2, RCC_CGR2_RTC_AON_CLK_EN_MASK, new_state); + while ((RCC->SR & RCC_SR_RTC_AON_CLK_EN_DONE) != RCC_SR_RTC_AON_CLK_EN_DONE) + ; break; } case RCC_PERIPHERAL_CRC: { @@ -1085,6 +1101,15 @@ void rcc_rst_peripheral(rcc_peripheral_t peripheral, bool new_state) TREMO_REG_EN(RCC->RST0, 1 << pos, !new_state); } + + if((!new_state) && (peripheral == RCC_PERIPHERAL_LPTIMER1 || + peripheral == RCC_PERIPHERAL_LPTIMER0 || + peripheral == RCC_PERIPHERAL_LCD || + peripheral == RCC_PERIPHERAL_RTC || + peripheral == RCC_PERIPHERAL_IWDG || + peripheral == RCC_PERIPHERAL_LPUART)) { + delay_us(92); + } } /** diff --git a/Drivers/peripheral/src/tremo_rtc.c b/Drivers/peripheral/src/tremo_rtc.c index da7caaa..c2cb0ac 100644 --- a/Drivers/peripheral/src/tremo_rtc.c +++ b/Drivers/peripheral/src/tremo_rtc.c @@ -445,7 +445,7 @@ void rtc_get_calendar(rtc_calendar_t* rtc_calendar) uint32_t syn_data_h; uint8_t temp; uint16_t subsecond_cnt; - float subsecond; + float subsecond = 0; if (rtc_calendar == NULL) { return; @@ -460,11 +460,7 @@ void rtc_get_calendar(rtc_calendar_t* rtc_calendar) } while (syn_data_h != RTC->CALENDAR_R_H); } while ((subsecond_cnt != rtc_get_subsecond_cnt()) || subsecond_cnt<1); - if (RCC_RTC_CLK_SOURCE_XO32K == rcc_get_rtc_clk_source()) { - subsecond = ((float)(((float)RTC_MICROSECOND) / 32768) * subsecond_cnt) + 0.5; - } else { - subsecond = ((float)(((float)RTC_MICROSECOND) / 32000) * subsecond_cnt) + 0.5; - } + subsecond = ((float)(((float)RTC_MICROSECOND) / 32768) * subsecond_cnt) + 0.5; rtc_calendar->subsecond = (uint32_t)subsecond; temp = syn_data & 0x0F; @@ -605,11 +601,7 @@ void rtc_set_alarm(uint8_t alarm_index, rtc_alarm_mask_t* alarm_mask, rtc_calend return; } if (alarm_mask->subsecMask != 0) { - if (RCC_RTC_CLK_SOURCE_XO32K == rcc_get_rtc_clk_source()) { - temp = ((float)(time->subsecond)) / ((float)(((float)RTC_MICROSECOND) / 32768)) + 0.5; - } else { - temp = ((float)(time->subsecond)) / ((float)(((float)RTC_MICROSECOND) / 32000)) + 0.5; - } + temp = ((float)(time->subsecond)) / ((float)(((float)RTC_MICROSECOND) / 32768)) + 0.5; rtc_check_syn(); *alarm_subsec_reg &= 0xFFFF0000; rtc_check_syn(); diff --git a/Drivers/peripheral/src/tremo_timer.c b/Drivers/peripheral/src/tremo_timer.c index 0fc2413..8ab9b71 100644 --- a/Drivers/peripheral/src/tremo_timer.c +++ b/Drivers/peripheral/src/tremo_timer.c @@ -789,8 +789,14 @@ int32_t timer_clear_ocxref(timer_gp_t* TIMERx, timer_clear_ocxref_t* clear_ocxre */ void timer_config_pwm(timer_gp_t* TIMERx, timer_oc_init_t* oc_init, timer_init_t* timerx_init, uint8_t channel) { - if (((oc_init->oc_mode).oc0m_mode != TIMER_OC0M_PWM1) && ((oc_init->oc_mode).oc0m_mode != TIMER_OC0M_PWM2)) { - return; + if ((channel == 0) || (channel == 2)) { + if (((oc_init->oc_mode).oc0m_mode != TIMER_OC0M_PWM1) && ((oc_init->oc_mode).oc0m_mode != TIMER_OC0M_PWM2)) { + return; + } + } else { + if (((oc_init->oc_mode).oc1m_mode != TIMER_OC1M_PWM1) && ((oc_init->oc_mode).oc1m_mode != TIMER_OC1M_PWM2)) { + return; + } } timer_config_oc(TIMERx, oc_init, channel); diff --git a/Drivers/sensor/bsp.c b/Drivers/sensor/bsp.c index bb608b8..5cd5d54 100644 --- a/Drivers/sensor/bsp.c +++ b/Drivers/sensor/bsp.c @@ -78,6 +78,58 @@ void BSP_sensor_Read( sensor_t *sensor_data ) void BSP_sensor_Init( void ) { + gpio_config_stop3_wakeup(GPIOC, GPIO_PIN_8,false,GPIO_LEVEL_LOW); + gpio_clear_interrupt(GPIOC, GPIO_PIN_8); + NVIC_DisableIRQ(GPIO_IRQn); + + rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOA, true); +// gpio_set_iomux(GPIOA, GPIO_PIN_6, 0); +// gpio_set_iomux(GPIOA, GPIO_PIN_7, 0); +// gpio_init(GPIOA, GPIO_PIN_6, GPIO_MODE_ANALOG); +// gpio_init(GPIOA, GPIO_PIN_7, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_1, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_0, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_3, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_2, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_14, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_15, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_9, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_4, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_5, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_8, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_11, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_12, GPIO_MODE_ANALOG); + gpio_init(GPIOA, GPIO_PIN_13, GPIO_MODE_ANALOG); + + rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOB, true); + gpio_init(GPIOB, GPIO_PIN_7, GPIO_MODE_ANALOG); + gpio_init(GPIOB, GPIO_PIN_8, GPIO_MODE_ANALOG); + gpio_init(GPIOB, GPIO_PIN_9, GPIO_MODE_ANALOG); + gpio_init(GPIOB, GPIO_PIN_10, GPIO_MODE_ANALOG); + gpio_init(GPIOB, GPIO_PIN_11, GPIO_MODE_ANALOG); + gpio_init(GPIOB, GPIO_PIN_12, GPIO_MODE_ANALOG); + gpio_init(GPIOB, GPIO_PIN_13, GPIO_MODE_ANALOG); + gpio_init(GPIOB, GPIO_PIN_14, GPIO_MODE_ANALOG); + gpio_init(GPIOB, GPIO_PIN_15, GPIO_MODE_ANALOG); + + rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOC, true); + gpio_init(GPIOC, GPIO_PIN_1, GPIO_MODE_ANALOG); + gpio_init(GPIOC, GPIO_PIN_0, GPIO_MODE_ANALOG); + gpio_init(GPIOC, GPIO_PIN_3, GPIO_MODE_ANALOG); + gpio_init(GPIOC, GPIO_PIN_4, GPIO_MODE_ANALOG); + gpio_init(GPIOC, GPIO_PIN_5, GPIO_MODE_ANALOG); + gpio_init(GPIOC, GPIO_PIN_8, GPIO_MODE_ANALOG); + gpio_init(GPIOC, GPIO_PIN_9, GPIO_MODE_ANALOG); + gpio_init(GPIOC, GPIO_PIN_12, GPIO_MODE_ANALOG); + gpio_init(GPIOC, GPIO_PIN_13, GPIO_MODE_ANALOG); + + rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOD, true); + gpio_init(GPIOD, GPIO_PIN_10, GPIO_MODE_ANALOG); + gpio_init(GPIOD, GPIO_PIN_14, GPIO_MODE_ANALOG); + + rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOC, true); + gpio_init(GPIOC, GPIO_PIN_8, GPIO_MODE_OUTPUT_PP_LOW); + rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOB, true); #if defined( ACCUHEALTH_LA66 ) diff --git a/Drivers/sensor/flash_eraseprogram.c b/Drivers/sensor/flash_eraseprogram.c index 963ca13..ef82543 100644 --- a/Drivers/sensor/flash_eraseprogram.c +++ b/Drivers/sensor/flash_eraseprogram.c @@ -77,10 +77,12 @@ void FLASH_erase_all_sensor_data_storage(uint32_t page_address) { for(uint32_t addr=FLASH_SENSOR_DATA_START_ADDR;addr1611878400)//20210129 00:00:00 { + UTC_Request=0; LOG_PRINTF(LL_DEBUG,"Sync time ok\r"); // Apply the new system time. SysTimeSet( sysTime ); @@ -2640,8 +2649,7 @@ LoRaMacStatus_t SendFrameOnChannel( uint8_t channel ) txConfig.MaxEirp = LoRaMacParams.MaxEirp; txConfig.AntennaGain = LoRaMacParams.AntennaGain; txConfig.PktLen = LoRaMacBufferPktLen; - - iwdg_reload(); + if(debug_flags==1) { TimerTime_t ts = TimerGetCurrentTime(); diff --git a/Middlewares/LoRa/LoRaMac-node/region/RegionAS923.c b/Middlewares/LoRa/LoRaMac-node/region/RegionAS923.c index 98acc06..796b0d7 100644 --- a/Middlewares/LoRa/LoRaMac-node/region/RegionAS923.c +++ b/Middlewares/LoRa/LoRaMac-node/region/RegionAS923.c @@ -345,14 +345,14 @@ PhyParam_t RegionAS923GetPhyParam( GetPhyParams_t* getPhy ) } case PHY_DEF_DOWNLINK_DWELL_TIME: { - if(dwelltime==1) - { - phyParam.Value = AS923_DEFAULT_DOWNLINK_DWELL_TIME; - } - else - { +// if(dwelltime==1) +// { +// phyParam.Value = AS923_DEFAULT_DOWNLINK_DWELL_TIME; +// } +// else +// { phyParam.Value = 0; - } +// } break; } case PHY_DEF_MAX_EIRP: diff --git a/Middlewares/LoRa/driver/rtc-board.c b/Middlewares/LoRa/driver/rtc-board.c index b6a7bda..0498fb5 100644 --- a/Middlewares/LoRa/driver/rtc-board.c +++ b/Middlewares/LoRa/driver/rtc-board.c @@ -339,9 +339,6 @@ TimerTime_t RtcConvertMsToTick( TimerTime_t timeoutValue ) double retVal = 0; uint32_t rtc_ticks_per_second = 32768; - if (RCC_RTC_CLK_SOURCE_RCO32K == rcc_get_rtc_clk_source()) - rtc_ticks_per_second = 32000; - retVal = round( ( ( double )timeoutValue ) * rtc_ticks_per_second / 1000); return( ( TimerTime_t )retVal ); } diff --git a/Middlewares/LoRa/driver/sx1262-board.c b/Middlewares/LoRa/driver/sx1262-board.c index 3426218..634368b 100644 --- a/Middlewares/LoRa/driver/sx1262-board.c +++ b/Middlewares/LoRa/driver/sx1262-board.c @@ -22,12 +22,14 @@ */ #include #include "delay.h" +#include "tremo_rcc.h" #include "tremo_gpio.h" #include "tremo_regs.h" #include "tremo_delay.h" #include "radio.h" #include "lora_config.h" #include "sx126x-board.h" +#include "tremo_rcc.h" #define BOARD_TCXO_WAKEUP_TIME 5 uint8_t gPaOptSetting = 0; @@ -61,7 +63,12 @@ uint16_t SpiInOut( uint16_t outData ) void SX126xLoracInit() { - LORAC->CR0 = 0x00000200; + rcc_enable_peripheral_clk(RCC_PERIPHERAL_LORA, false); + rcc_rst_peripheral(RCC_PERIPHERAL_LORA, true); + rcc_rst_peripheral(RCC_PERIPHERAL_LORA, false); + rcc_enable_peripheral_clk(RCC_PERIPHERAL_LORA, true); + + LORAC->CR0 = 0x00000200; LORAC->SSP_CR0 = 0x07; LORAC->SSP_CPSR = 0x02; @@ -72,7 +79,7 @@ void SX126xLoracInit() { delay_us(20); LORAC->NSS_CR = 0; - delay_us(20); + delay_us(110); LORAC->NSS_CR = 1; } @@ -95,8 +102,10 @@ uint32_t SX126xGetBoardTcxoWakeupTime( void ) void SX126xReset( void ) { - LORAC->CR1 |= 1<<5; //nreset - LORAC->CR1 &= ~(1<<7); //por + LORAC->CR1 &= ~(1<<5); //nreset + delay_us(100); + LORAC->CR1 |= 1<<5; //nreset release + LORAC->CR1 &= ~(1<<7); //por release LORAC->CR0 |= 1<<5; //irq0 LORAC->CR1 |= 0x1; //tcxo @@ -114,7 +123,7 @@ void SX126xWakeup( void ) BoardDisableIrq( ); LORAC->NSS_CR = 0; - delay_us(10); + delay_us(20); SpiInOut( RADIO_GET_STATUS ); SpiInOut( 0x00 ); diff --git a/Middlewares/LoRa/radio/sx126x/radio.c b/Middlewares/LoRa/radio/sx126x/radio.c index a59c472..b4e9843 100644 --- a/Middlewares/LoRa/radio/sx126x/radio.c +++ b/Middlewares/LoRa/radio/sx126x/radio.c @@ -580,7 +580,7 @@ bool RadioIsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh, { bool status = true; int16_t rssi = 0; - uint32_t carrierSenseTime = 0; + TimerTime_t carrierSenseTime = 0; RadioSetModem( modem ); diff --git a/Middlewares/LoRa/system/timer.c b/Middlewares/LoRa/system/timer.c index 4e1bc34..952d328 100644 --- a/Middlewares/LoRa/system/timer.c +++ b/Middlewares/LoRa/system/timer.c @@ -217,8 +217,8 @@ void SysTimeSet( SysTime_t sysTime ) SysTime_t calendarTime = { .Seconds = 0, .SubSeconds = 0 }; TimerTime_t cur_time = RtcGetTimerValue( ); - calendarTime.Seconds=(uint32_t)cur_time/1000; - calendarTime.SubSeconds=(uint16_t)cur_time%1000; + calendarTime.Seconds=(uint32_t)(cur_time/1000); + calendarTime.SubSeconds=(uint16_t)(cur_time%1000); // sysTime is epoch DeltaTime = SysTimeSub( sysTime, calendarTime ); @@ -246,8 +246,8 @@ SysTime_t SysTimeGet( void ) TimerTime_t cur_time = RtcGetTimerValue( ); - calendarTime.Seconds=(uint32_t)cur_time/1000; - calendarTime.SubSeconds=(uint16_t)cur_time%1000; + calendarTime.Seconds=(uint32_t)(cur_time/1000); + calendarTime.SubSeconds=(uint16_t)(cur_time%1000); DeltaTime.Seconds=(*((uint8_t *)(0x2000F000)))<<24|(*((uint8_t *)(0x2000F001)))<<16|(*((uint8_t *)(0x2000F002)))<<8|(*((uint8_t *)(0x2000F003))); DeltaTime.SubSeconds=(*((uint8_t *)(0x2000F004)))<<8|(*((uint8_t *)(0x2000F005))); diff --git a/Projects/Applications/DRAGINO-LRWAN-AT/cfg/gcc.ld b/Projects/Applications/DRAGINO-LRWAN-AT/cfg/gcc.ld index 091a4e2..5652f74 100644 --- a/Projects/Applications/DRAGINO-LRWAN-AT/cfg/gcc.ld +++ b/Projects/Applications/DRAGINO-LRWAN-AT/cfg/gcc.ld @@ -15,7 +15,7 @@ _STACK_SIZE = 0x1000; /* required amount of stack */ /* Specify the memory areas */ MEMORY { - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 256K + FLASH (rx) : ORIGIN = 0x0800D000, LENGTH = 256K RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64k } diff --git a/Projects/Applications/DRAGINO-LRWAN-AT/inc/version.h b/Projects/Applications/DRAGINO-LRWAN-AT/inc/version.h index 0f64eae..609ed21 100644 --- a/Projects/Applications/DRAGINO-LRWAN-AT/inc/version.h +++ b/Projects/Applications/DRAGINO-LRWAN-AT/inc/version.h @@ -68,7 +68,7 @@ Maintainer: Miguel Luis, Gregory Cristian and Wael Guibene /* Includes ------------------------------------------------------------------*/ -#define AT_VERSION_STRING "v1.2" +#define AT_VERSION_STRING "v1.3" #define AT_LoRaWan_VERSION_STRING "DR-LWS-007" /* Exported types ------------------------------------------------------------*/ diff --git a/Projects/Applications/DRAGINO-LRWAN-AT/src/command.c b/Projects/Applications/DRAGINO-LRWAN-AT/src/command.c index 525c868..37567e1 100644 --- a/Projects/Applications/DRAGINO-LRWAN-AT/src/command.c +++ b/Projects/Applications/DRAGINO-LRWAN-AT/src/command.c @@ -436,12 +436,14 @@ static int at_fdr_func(int opt, int argc, char *argv[]) ret = LWAN_SUCCESS; uint8_t status[128]={0}; memset(status, 0x00, 128); - + __disable_irq(); flash_erase_page(FLASH_USER_START_ADDR_CONFIG); + delay_ms(5); if(flash_program_bytes(FLASH_USER_START_ADDR_CONFIG,status,128)==ERRNO_FLASH_SEC_ERROR) { snprintf((char *)atcmd, ATCMD_SIZE, "FDR error\r\n"); } + __enable_irq(); delay_ms(100); system_reset(); @@ -2713,7 +2715,7 @@ static int at_timestamp_func(int opt, int argc, char *argv[]) uint32_t timestamp; SysTime_t sysTime = { 0 }; - timestamp=strtol((const char *)argv[0], NULL, 0); + timestamp=strtoul((const char *)argv[0], NULL, 0); if (timestamp > 1640044800) { sysTime.Seconds=timestamp; diff --git a/Projects/Applications/DRAGINO-LRWAN-AT/src/lora_app.c b/Projects/Applications/DRAGINO-LRWAN-AT/src/lora_app.c index c8aa34b..5881022 100644 --- a/Projects/Applications/DRAGINO-LRWAN-AT/src/lora_app.c +++ b/Projects/Applications/DRAGINO-LRWAN-AT/src/lora_app.c @@ -55,6 +55,8 @@ #include "tremo_system.h" #include "tremo_flash.h" #include "LoRaMacTest.h" +#include "tremo_delay.h" + /* product_id 1:LTC2 @@ -431,7 +433,7 @@ void LORA_Init (LoRaMainCallback_t *callbacks, LoRaParam_t* LoRaParam ) LOG_PRINTF(LL_DEBUG,"Frequency Band: "); #elif defined( USB_LORAWAN_ADAPTER_LBT ) LOG_PRINTF(LL_DEBUG,"\n\rDragino USB LoRaWAN adapter LBT\n\r"); - LOG_PRINTF(LL_DEBUG,"Image Version: v1.0\n\r"); + LOG_PRINTF(LL_DEBUG,"Image Version: v1.2\n\r"); #else LOG_PRINTF(LL_DEBUG,"\n\rDragino LA66 Device\n\r"); LOG_PRINTF(LL_DEBUG,"Image Version: "AT_VERSION_STRING"\n\r"); @@ -886,7 +888,9 @@ LoraState_t lora_config_otaa_get(void) void lora_config_duty_cycle_set(LoraState_t duty_cycle) { lora_config.duty_cycle = duty_cycle; + #if defined( REGION_EU868 ) LoRaMacTestSetDutyCycleOn((duty_cycle == LORA_ENABLE) ? 1 : 0); + #endif } LoraState_t lora_config_duty_cycle_get(void) @@ -1042,11 +1046,14 @@ void Flash_store_key(void) store_key_in_flash[i]=lora_config.AppSKey[j]; } + __disable_irq(); flash_erase_page(FLASH_USER_START_ADDR_KEY); + delay_ms(5); if(flash_program_bytes(FLASH_USER_START_ADDR_KEY,store_key_in_flash,128)==ERRNO_FLASH_SEC_ERROR) { LOG_PRINTF(LL_DEBUG,"write key error\r\n"); } + __enable_irq(); } void Flash_read_key(void) @@ -1250,11 +1257,14 @@ void Flash_Store_Config(void) store_config_in_flash[74]=decrypt_flag; store_config_in_flash[75]=mac_response_flag; + __disable_irq(); flash_erase_page(FLASH_USER_START_ADDR_CONFIG); + delay_ms(5); if(flash_program_bytes(FLASH_USER_START_ADDR_CONFIG,store_config_in_flash,128)==ERRNO_FLASH_SEC_ERROR) { LOG_PRINTF(LL_DEBUG,"write config error\r\n"); } + __enable_irq(); } void Flash_Read_Config(void) @@ -1292,8 +1302,10 @@ void Flash_Read_Config(void) { lora_config.duty_cycle=LORA_DISABLE; } - + + #if defined( REGION_EU868 ) LoRaMacTestSetDutyCycleOn((lora_config.duty_cycle == LORA_ENABLE) ? 1 : 0); + #endif mib.Type = MIB_PUBLIC_NETWORK; mib.Param.EnablePublicNetwork=read_config_in_flash[8]; @@ -1428,11 +1440,14 @@ static void new_firmware_update(void) status[0]=0x12; while(print_isdone()==0); + __disable_irq(); flash_erase_page(FLASH_USER_START_ADDR_CONFIG); + delay_ms(5); if(flash_program_bytes(FLASH_USER_START_ADDR_CONFIG,status,128)==ERRNO_FLASH_SEC_ERROR) { LOG_PRINTF(LL_DEBUG,"write config error\r\n"); } + __enable_irq(); } else if((current_firmware_ver!=firmware_ver_read_in_flash)) { @@ -1440,12 +1455,15 @@ static void new_firmware_update(void) status[0]=0x13; while(print_isdone()==0); + __disable_irq(); //add new configuration flash_erase_page(FLASH_USER_START_ADDR_CONFIG); + delay_ms(5); if(flash_program_bytes(FLASH_USER_START_ADDR_CONFIG,status,128)==ERRNO_FLASH_SEC_ERROR) { LOG_PRINTF(LL_DEBUG,"write config error\r\n"); } + __enable_irq(); } } } diff --git a/Projects/Applications/DRAGINO-LRWAN-AT/src/main.c b/Projects/Applications/DRAGINO-LRWAN-AT/src/main.c index 268b53f..bb2db46 100644 --- a/Projects/Applications/DRAGINO-LRWAN-AT/src/main.c +++ b/Projects/Applications/DRAGINO-LRWAN-AT/src/main.c @@ -307,7 +307,7 @@ void board_init() *((unsigned int *)(0x2000F004)) =0x00; *((unsigned int *)(0x2000F005)) =0x00; - for(int i=0;i<3328;i++) + for(uint16_t i=0;i<3328;i++) { *((unsigned int *)(SRAM_SENSOR_DATA_STORE_ACK_START_ADDR+i)) =0xEE; } @@ -350,6 +350,11 @@ int main(void) StartIWDGRefresh(TX_ON_EVENT); + if(debug_flags==1) + { + LOG_PRINTF(LL_DEBUG,"dragino_6601_ota\r\n"); + } + /* Configure the Lora Stack*/ LORA_Init( &LoRaMainCallbacks, &LoRaParamInit); @@ -669,11 +674,14 @@ static void LORA_RxData( lora_AppData_t *AppData ) memset(status, 0x00, 128); status[0]=0x12; + __disable_irq(); flash_erase_page(FLASH_USER_START_ADDR_CONFIG); + delay_ms(5); if(flash_program_bytes(FLASH_USER_START_ADDR_CONFIG,status,128)==ERRNO_FLASH_SEC_ERROR) { LOG_PRINTF(LL_DEBUG,"write config error\r\n"); } + __enable_irq(); atz_flags=1; }