From 3714b4e6dbecfa0225d744355e7647b4f60cee80 Mon Sep 17 00:00:00 2001 From: Mati Date: Tue, 18 Jun 2019 15:55:24 +0200 Subject: [PATCH] PowerUp sequence tests --- .../board/rt1051/cellular/rt1051_cellular.cpp | 156 ++++++++++-------- module-bsp/board/rt1051/common/board.cpp | 12 +- module-cellular/Modem/Modem.cpp | 88 ++++++++++ module-cellular/Modem/Modem.hpp | 28 ++++ source/main.cpp | 20 ++- 5 files changed, 225 insertions(+), 79 deletions(-) diff --git a/module-bsp/board/rt1051/cellular/rt1051_cellular.cpp b/module-bsp/board/rt1051/cellular/rt1051_cellular.cpp index a7c1e481a..27ed54fcf 100644 --- a/module-bsp/board/rt1051/cellular/rt1051_cellular.cpp +++ b/module-bsp/board/rt1051/cellular/rt1051_cellular.cpp @@ -17,34 +17,29 @@ #include "dma_config.h" #include "fsl_cache.h" +extern "C" { - -void LPUART1_IRQHandler(void) -{ +void LPUART1_IRQHandler(void) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; uint32_t isrReg = LPUART_GetStatusFlags(CELLULAR_UART_BASE); - static char characterReceived = 0; + static char characterReceived = 0; - if (bsp::RT1051Cellular::uartRxStreamBuffer != NULL) - { - if(isrReg & kLPUART_RxDataRegFullFlag) - { + if (bsp::RT1051Cellular::uartRxStreamBuffer != NULL) { + if (isrReg & kLPUART_RxDataRegFullFlag) { characterReceived = LPUART_ReadByte(CELLULAR_UART_BASE); - if (characterReceived != 0) - { - if (xStreamBufferSpacesAvailable(bsp::RT1051Cellular::uartRxStreamBuffer) < 8) - { + if (characterReceived != 0) { + if (xStreamBufferSpacesAvailable(bsp::RT1051Cellular::uartRxStreamBuffer) < 8) { //BSP_CellularDeassertRTS(); } - xStreamBufferSendFromISR( bsp::RT1051Cellular::uartRxStreamBuffer, - ( void * ) &characterReceived, - 1, - &xHigherPriorityTaskWoken ); + xStreamBufferSendFromISR(bsp::RT1051Cellular::uartRxStreamBuffer, + (void *) &characterReceived, + 1, + &xHigherPriorityTaskWoken); - if(xTimerResetFromISR(bsp::RT1051Cellular::rxTimeoutTimer,&xHigherPriorityTaskWoken) != pdTRUE){ + if (xTimerResetFromISR(bsp::RT1051Cellular::rxTimeoutTimer, &xHigherPriorityTaskWoken) != pdTRUE) { assert(0); } } @@ -54,12 +49,19 @@ void LPUART1_IRQHandler(void) LPUART_ClearStatusFlags(CELLULAR_UART_BASE, isrReg); - portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); + portEND_SWITCHING_ISR(xHigherPriorityTaskWoken); +} + } namespace bsp { - TimerHandle_t RT1051Cellular::rxTimeoutTimer = nullptr; + TimerHandle_t RT1051Cellular::rxTimeoutTimer = nullptr; + lpuart_edma_handle_t RT1051Cellular::uartDmaHandle = {}; + edma_handle_t RT1051Cellular::uartTxDmaHandle = {}; + TaskHandle_t RT1051Cellular::blockedTaskHandle = nullptr; + StreamBufferHandle_t RT1051Cellular::uartRxStreamBuffer = nullptr; + RT1051Cellular::RT1051Cellular() { @@ -67,13 +69,12 @@ namespace bsp { DMAInit(); uartRxStreamBuffer = xStreamBufferCreate(rxStreamBufferLength, rxStreamBufferNotifyWatermark); - if (uartRxStreamBuffer == NULL) - { + if (uartRxStreamBuffer == NULL) { LOG_ERROR("Cellular Uart error: Could not create the RX stream buffer!"); return; } - rxTimeoutTimer= xTimerCreate + rxTimeoutTimer = xTimerCreate ( /* Just a text name, not used by the RTOS kernel. */ "celltim", @@ -86,7 +87,7 @@ namespace bsp { /* The ID is used to store a count of the number of times the timer has expired, which is initialised to 0. */ - ( void * ) 0, + (void *) 0, /* Each timer calls the same callback when it expires. */ rxTimeoutTimerHandle @@ -96,17 +97,16 @@ namespace bsp { LPUART_GetDefaultConfig(&s_cellularConfig); - s_cellularConfig.baudRate_Bps = baudrate; - s_cellularConfig.dataBitsCount = kLPUART_EightDataBits; - s_cellularConfig.parityMode = kLPUART_ParityDisabled; - s_cellularConfig.isMsb = false; - s_cellularConfig.rxIdleType = kLPUART_IdleTypeStartBit; - s_cellularConfig.rxIdleConfig = kLPUART_IdleCharacter1; - s_cellularConfig.enableTx = false; - s_cellularConfig.enableRx = false; + s_cellularConfig.baudRate_Bps = baudrate; + s_cellularConfig.dataBitsCount = kLPUART_EightDataBits; + s_cellularConfig.parityMode = kLPUART_ParityDisabled; + s_cellularConfig.isMsb = false; + s_cellularConfig.rxIdleType = kLPUART_IdleTypeStartBit; + s_cellularConfig.rxIdleConfig = kLPUART_IdleCharacter1; + s_cellularConfig.enableTx = false; + s_cellularConfig.enableRx = false; - if (LPUART_Init(CELLULAR_UART_BASE, &s_cellularConfig, UartGetPeripheralClock()) != kStatus_Success) - { + if (LPUART_Init(CELLULAR_UART_BASE, &s_cellularConfig, UartGetPeripheralClock()) != kStatus_Success) { LOG_ERROR("Cellular Uart config error: Could not initialize the uart!"); return; } @@ -122,11 +122,18 @@ namespace bsp { RT1051Cellular::~RT1051Cellular() { - if(uartRxStreamBuffer){ + if (uartRxStreamBuffer) { vStreamBufferDelete(uartRxStreamBuffer); + uartRxStreamBuffer = nullptr; + } + + if(rxTimeoutTimer){ + xTimerDelete(rxTimeoutTimer,100); + rxTimeoutTimer = nullptr; } DisableRx(); + DisableTx(); NVIC_DisableIRQ(LPUART1_IRQn); LPUART_ClearStatusFlags(CELLULAR_UART_BASE, 0xFFFFFFFF); @@ -137,6 +144,10 @@ namespace bsp { MSPDeinit(); DMADeinit(); + memset(&uartDmaHandle,0,sizeof uartDmaHandle); + memset(&uartTxDmaHandle,0,sizeof uartTxDmaHandle); + blockedTaskHandle = nullptr; + } void RT1051Cellular::PowerUp() { @@ -179,24 +190,22 @@ namespace bsp { uint32_t RT1051Cellular::Write(void *buf, size_t nbytes) { lpuart_transfer_t sendXfer; - sendXfer.data = static_cast(buf); - sendXfer.dataSize = nbytes; + sendXfer.data = static_cast(buf); + sendXfer.dataSize = nbytes; EnableTx(); uartDmaHandle.userData = xTaskGetCurrentTaskHandle(); SCB_CleanInvalidateDCache(); - if (LPUART_SendEDMA(CELLULAR_UART_BASE, &uartDmaHandle, &sendXfer) != kStatus_Success) - { + if (LPUART_SendEDMA(CELLULAR_UART_BASE, &uartDmaHandle, &sendXfer) != kStatus_Success) { LOG_ERROR("Cellular: TX Failed!"); DisableTx(); return 0; } - auto ulNotificationValue = ulTaskNotifyTake( pdFALSE, 100 ); + auto ulNotificationValue = ulTaskNotifyTake(pdFALSE, 100); - if (ulNotificationValue == 0) - { + if (ulNotificationValue == 0) { LOG_ERROR("Cellular Uart error: TX Transmission timeout"); DisableTx(); return 0; @@ -207,62 +216,63 @@ namespace bsp { } uint32_t RT1051Cellular::Read(void *buf, size_t nbytes) { - return xStreamBufferReceive(uartRxStreamBuffer,buf,nbytes,0); + return xStreamBufferReceive(uartRxStreamBuffer, buf, nbytes, 0); } uint32_t RT1051Cellular::Wait(uint32_t timeout) { - if(blockedTaskHandle != nullptr){ + if (blockedTaskHandle != nullptr) { LOG_FATAL("Wait called simultaneously from more than one thread!"); return 0; } - if(xStreamBufferBytesAvailable(uartRxStreamBuffer)){ + if (xStreamBufferBytesAvailable(uartRxStreamBuffer)) { return 1; } blockedTaskHandle = xTaskGetCurrentTaskHandle(); - return ulTaskNotifyTake( pdTRUE, timeout ); + auto ret = ulTaskNotifyTake(pdTRUE, timeout); + blockedTaskHandle = nullptr; + return ret; } - void RT1051Cellular::MSPInit() { gpio_pin_config_t gpio_init_structure; - gpio_init_structure.direction = kGPIO_DigitalOutput; - gpio_init_structure.interruptMode = kGPIO_NoIntmode; + gpio_init_structure.direction = kGPIO_DigitalOutput; + gpio_init_structure.interruptMode = kGPIO_NoIntmode; /* * CELLULAR OUTPUT PINS WITH INITIAL VALUE 1 */ - gpio_init_structure.outputLogic = 1; + gpio_init_structure.outputLogic = 1; /* * CELLULAR OUTPUT PINS WITH INITIAL VALUE 0 */ - gpio_init_structure.outputLogic = 0; + gpio_init_structure.outputLogic = 0; - GPIO_PinInit(BSP_CELLULAR_UART_RTS_PORT, BSP_CELLULAR_UART_RTS_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_UART_DTR_PORT, BSP_CELLULAR_UART_DTR_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_AP_RDY_PORT, BSP_CELLULAR_AP_RDY_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_POWER_PORT, BSP_CELLULAR_POWER_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_RESET_PORT, BSP_CELLULAR_RESET_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_WAKEUP_PORT, BSP_CELLULAR_WAKEUP_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_SIMSEL_PORT, BSP_CELLULAR_SIMSEL_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_SIM_CARD_PRESENCE_PORT, BSP_CELLULAR_SIM_CARD_PRESENCE_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_UART_RTS_PORT, BSP_CELLULAR_UART_RTS_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_UART_DTR_PORT, BSP_CELLULAR_UART_DTR_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_AP_RDY_PORT, BSP_CELLULAR_AP_RDY_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_POWER_PORT, BSP_CELLULAR_POWER_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_RESET_PORT, BSP_CELLULAR_RESET_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_WAKEUP_PORT, BSP_CELLULAR_WAKEUP_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_SIMSEL_PORT, BSP_CELLULAR_SIMSEL_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_SIM_CARD_PRESENCE_PORT, BSP_CELLULAR_SIM_CARD_PRESENCE_PIN, &gpio_init_structure); /* * CELLULAR INPUT PINS */ - gpio_init_structure.direction = kGPIO_DigitalInput; - gpio_init_structure.interruptMode = kGPIO_IntRisingOrFallingEdge; - gpio_init_structure.outputLogic = 1; //< Don't care for input + gpio_init_structure.direction = kGPIO_DigitalInput; + gpio_init_structure.interruptMode = kGPIO_IntRisingOrFallingEdge; + gpio_init_structure.outputLogic = 1; //< Don't care for input - GPIO_PinInit(BSP_CELLULAR_RI_PORT, BSP_CELLULAR_RI_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_UART_CTS_PORT, BSP_CELLULAR_UART_CTS_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_SIM_CARD_1_INSERTED_PORT, BSP_CELLULAR_SIM_CARD_1_INSERTED_PIN, &gpio_init_structure); - GPIO_PinInit(BSP_CELLULAR_SIM_CARD_2_INSERTED_PORT, BSP_CELLULAR_SIM_CARD_2_INSERTED_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_RI_PORT, BSP_CELLULAR_RI_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_UART_CTS_PORT, BSP_CELLULAR_UART_CTS_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_SIM_CARD_1_INSERTED_PORT, BSP_CELLULAR_SIM_CARD_1_INSERTED_PIN, &gpio_init_structure); + GPIO_PinInit(BSP_CELLULAR_SIM_CARD_2_INSERTED_PORT, BSP_CELLULAR_SIM_CARD_2_INSERTED_PIN, &gpio_init_structure); /* GPIO_PortEnableInterrupts(BSP_CELLULAR_SIM_CARD_1_INSERTED_PORT, 1U << BSP_CELLULAR_SIM_CARD_1_INSERTED_PIN); GPIO_PortEnableInterrupts(BSP_CELLULAR_SIM_CARD_2_INSERTED_PORT, 1U << BSP_CELLULAR_SIM_CARD_2_INSERTED_PIN);*/ @@ -288,7 +298,8 @@ namespace bsp { } void RT1051Cellular::DMADeinit() { - LPUART_TransferAbortReceiveEDMA(CELLULAR_UART_BASE,&uartDmaHandle); + EDMA_AbortTransfer(&uartTxDmaHandle); + DMAMUX_DisableChannel(BSP_CELLULAR_UART_TX_DMA_DMAMUX_BASE, BSP_CELLULAR_UART_TX_DMA_CH); } @@ -299,10 +310,9 @@ namespace bsp { from application is use PLL3 source or OSC source */ if (CLOCK_GetMux(kCLOCK_UartMux) == 0) /* PLL3 div6 80M */ { - freq = (CLOCK_GetPllFreq(kCLOCK_PllUsb1) / UART_PERIPHERAL_PLL_DIVIDER) / (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U); - } - else - { + freq = (CLOCK_GetPllFreq(kCLOCK_PllUsb1) / UART_PERIPHERAL_PLL_DIVIDER) / + (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U); + } else { freq = CLOCK_GetOscFreq() / (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U); } @@ -311,16 +321,16 @@ namespace bsp { void RT1051Cellular::DMATxCompletedCb(LPUART_Type *base, lpuart_edma_handle_t *handle, status_t status, - void *userData) { + void *userData) { BaseType_t higherPriorTaskWoken = 0; - vTaskNotifyGiveFromISR( (TaskHandle_t)userData, &higherPriorTaskWoken ); + vTaskNotifyGiveFromISR((TaskHandle_t) userData, &higherPriorTaskWoken); portEND_SWITCHING_ISR(higherPriorTaskWoken); } void RT1051Cellular::rxTimeoutTimerHandle(TimerHandle_t xTimer) { - if(blockedTaskHandle){ + if (blockedTaskHandle) { xTaskNotifyGive(blockedTaskHandle); } diff --git a/module-bsp/board/rt1051/common/board.cpp b/module-bsp/board/rt1051/common/board.cpp index a9b62ef03..192ce5128 100644 --- a/module-bsp/board/rt1051/common/board.cpp +++ b/module-bsp/board/rt1051/common/board.cpp @@ -211,11 +211,21 @@ namespace bsp { } + void Power_SW_Init( void ) { + gpio_pin_config_t gpio_config = {kGPIO_DigitalInput, 0, kGPIO_NoIntmode}; + + GPIO_PinInit(BOARD_POWER_SW_GPIO, BOARD_POWER_SW_GPIO_PIN, &gpio_config); + gpio_config.direction = kGPIO_DigitalOutput; + GPIO_PinInit(BOARD_POWER_HOLD_GPIO, BOARD_POWER_HOLD_GPIO_PIN, &gpio_config); + GPIO_PinWrite(BOARD_POWER_HOLD_GPIO, BOARD_POWER_HOLD_GPIO_PIN, 1U); //set pwr pin to hold power switch + } + + void BoardInit(){ PINMUX_InitBootPins(); - //Power_SW_Init(); + Power_SW_Init(); BOARD_InitBootClocks(); BOARD_ConfigMPU(); diff --git a/module-cellular/Modem/Modem.cpp b/module-cellular/Modem/Modem.cpp index ab26430d9..de8b7b3de 100644 --- a/module-cellular/Modem/Modem.cpp +++ b/module-cellular/Modem/Modem.cpp @@ -10,3 +10,91 @@ #include "Modem.hpp" + +#include + +#include "log/log.hpp" + +#include "FreeRTOS.h" +#include "task.h" + +Modem::Modem() { + cellular = bsp::Cellular::Create(); +} + +Modem::~Modem() { + +} + + +void Modem::Init() { + + // At first send AT command to check if modem is turned on + if(SendAT("AT\r\n",500) == -1){ + // If no response, power up modem and try again + cellular->PowerUp(); + + uint32_t retries = 10; + while(--retries){ + if(SendAT("AT\r\n",500) == 0){ + return; + } + } + + if(retries == 0){ + LOG_FATAL("No communication with GSM modem"); + return; + } + } + +} + + +int Modem::SendAT(const char *cmd, uint32_t timeout) { + + char buff[256] = {0}; + + cellular->Write(const_cast(cmd),strlen(cmd)); + + if(cellular->Wait(500)){ + + vTaskDelay(500); + auto bytesRead = cellular->Read(buff,sizeof buff); + + if (memstr((char *) buff, bytesRead, "OK")) + { + LOG_DEBUG("Received OK"); + return 0; + } + else if (memstr((char *) buff, bytesRead, "ERROR")) + { + LOG_DEBUG("Received ERROR"); + return -1; + } + else{ + LOG_DEBUG("Received unknonwn response"); + return -1; + } + } + else{ + return -1; + } +} + +int Modem::memstr(const char *haystack, int length, const char *needle) { + + int i; + int j = 0; + if (needle[0] == '\0') + return 1; + for (i = 0; i < length; i++) + if (needle[j] == haystack[i]) + { + j++; + if (needle[j] == '\0') // Entire needle was found + return 1; + } + else + j = 0; + return 0; +} \ No newline at end of file diff --git a/module-cellular/Modem/Modem.hpp b/module-cellular/Modem/Modem.hpp index 0a653dcf7..8485c3855 100644 --- a/module-cellular/Modem/Modem.hpp +++ b/module-cellular/Modem/Modem.hpp @@ -12,8 +12,36 @@ #ifndef PUREPHONE_MODEM_HPP #define PUREPHONE_MODEM_HPP +#include +#include "cellular/bsp_cellular.hpp" class Modem { +public: + + Modem(); + ~Modem(); + + void Init(); + + int SendAT(const char* cmd,uint32_t timeout); + +private: + + /* +* Purpose: Compares two strings. +* strstr might not work because WebBox sends garbage before the first OK +* when it's not needed anymore +* Input: haystack - string to check +* length - length of string to check +* needle - reference string to compare to. must be null-terminated. +* Return: 1 if comparison was success, else 0 +*/ + int memstr( + const char *haystack, + int length, + const char *needle); + + std::unique_ptr cellular; }; diff --git a/source/main.cpp b/source/main.cpp index a3a875941..f714c95c3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -41,19 +41,29 @@ #include "vfs.hpp" #include "keyboard/keyboard.hpp" +//module-cellular +#include "Modem/Modem.hpp" + #include "SystemManager/SystemManager.hpp" class vfs vfs; class BlinkyService : public sys::Service { gui::Window* win = nullptr; + std::unique_ptr modem; public: BlinkyService(const std::string& name) : sys::Service(name) { - timer_id = CreateTimer(100,true); + timer_id = CreateTimer(1000,true); ReloadTimer(timer_id); + modem = std::make_unique(); +/* modem.reset(); + modem = std::make_unique();*/ + + modem->Init(); + } ~BlinkyService(){ @@ -71,7 +81,7 @@ public: void TickHandler(uint32_t id) override{ //auto msg = std::make_shared(500); //sys::Bus::SendUnicast(msg,"Blinky",this); - //LOG_DEBUG("Blinky service tick!"); + LOG_DEBUG("Blinky service tick!"); } // Invoked during initialization @@ -103,10 +113,10 @@ int SystemStart(sys::SystemManager* sysmgr) vfs.Init(); bool ret; - ret = sysmgr->CreateService(std::make_shared("ServiceGUI", 480, 600 ),sysmgr); +/* ret = sysmgr->CreateService(std::make_shared("ServiceGUI", 480, 600 ),sysmgr); ret |= sysmgr->CreateService(std::make_shared("ServiceEink"),sysmgr); ret |= sysmgr->CreateService(std::make_shared("EventManager"),sysmgr); - ret |= sysmgr->CreateService(std::make_shared(),sysmgr); + ret |= sysmgr->CreateService(std::make_shared(),sysmgr);*/ ret |= sysmgr->CreateService(std::make_shared("Blinky"),sysmgr); //vector with launchers to applications @@ -123,7 +133,7 @@ int SystemStart(sys::SystemManager* sysmgr) #endif //start application manager - ret |= sysmgr->CreateService(std::make_shared("ApplicationManager",sysmgr,applications),sysmgr ); + //ret |= sysmgr->CreateService(std::make_shared("ApplicationManager",sysmgr,applications),sysmgr ); if(ret){ return 0;