LoRa node acquiring analog input and sending to LoRa Server - Working ok

Dependencies:   BufferedSerial SX1276GenericLib-node1 mbed

Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by ST

Committer:
sagilar
Date:
Wed Aug 08 22:33:19 2018 +0000
Revision:
12:a6a37ba1feff
Parent:
10:f84959c2e044
LoRa node acquiring analog input and sending to LoRa Server - Working ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:c43b6919ae15 1 /*
Helmut64 0:c43b6919ae15 2 * Copyright (c) 2017 Helmut Tschemernjak
Helmut64 0:c43b6919ae15 3 * 30826 Garbsen (Hannover) Germany
Helmut64 0:c43b6919ae15 4 * Licensed under the Apache License, Version 2.0);
Helmut64 0:c43b6919ae15 5 */
sagilar 12:a6a37ba1feff 6 #include "main.h"
sagilar 12:a6a37ba1feff 7 //#include "mbed_events.h"
sagilar 12:a6a37ba1feff 8 #define MAX_NUMBER_OF_EVENTS 10
Helmut64 0:c43b6919ae15 9 DigitalOut myled(LED1);
Helmut64 0:c43b6919ae15 10 BufferedSerial *ser;
sagilar 12:a6a37ba1feff 11 InterruptIn btn(USER_BUTTON);
Helmut64 0:c43b6919ae15 12
sagilar 12:a6a37ba1feff 13 int main()
sagilar 12:a6a37ba1feff 14 {
Helmut64 0:c43b6919ae15 15 SystemClock_Config();
Helmut64 0:c43b6919ae15 16 ser = new BufferedSerial(USBTX, USBRX);
sagilar 12:a6a37ba1feff 17 ser->baud(115200);
Helmut64 0:c43b6919ae15 18 ser->format(8);
Helmut64 0:c43b6919ae15 19 myled = 1;
sagilar 12:a6a37ba1feff 20 btn.fall(&onButtonEvent);
Helmut64 0:c43b6919ae15 21 SX1276PingPong();
Helmut64 0:c43b6919ae15 22 }
Helmut64 0:c43b6919ae15 23
Helmut64 0:c43b6919ae15 24
Helmut64 0:c43b6919ae15 25
Helmut64 0:c43b6919ae15 26
Helmut64 0:c43b6919ae15 27 void SystemClock_Config(void)
Helmut64 0:c43b6919ae15 28 {
Helmut64 0:c43b6919ae15 29 #ifdef B_L072Z_LRWAN1_LORA
sagilar 12:a6a37ba1feff 30 /*
Helmut64 0:c43b6919ae15 31 * The L072Z_LRWAN1_LORA clock setup is somewhat differnt from the Nucleo board.
Helmut64 0:c43b6919ae15 32 * It has no LSE.
Helmut64 0:c43b6919ae15 33 */
Helmut64 0:c43b6919ae15 34 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Helmut64 0:c43b6919ae15 35 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Helmut64 0:c43b6919ae15 36
Helmut64 0:c43b6919ae15 37 /* Enable HSE Oscillator and Activate PLL with HSE as source */
Helmut64 0:c43b6919ae15 38 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Helmut64 0:c43b6919ae15 39 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
Helmut64 0:c43b6919ae15 40 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Helmut64 0:c43b6919ae15 41 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Helmut64 0:c43b6919ae15 42 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Helmut64 0:c43b6919ae15 43 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Helmut64 0:c43b6919ae15 44 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6;
Helmut64 0:c43b6919ae15 45 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;
Helmut64 0:c43b6919ae15 46
Helmut64 0:c43b6919ae15 47 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Helmut64 0:c43b6919ae15 48 // Error_Handler();
Helmut64 0:c43b6919ae15 49 }
Helmut64 0:c43b6919ae15 50
Helmut64 0:c43b6919ae15 51 /* Set Voltage scale1 as MCU will run at 32MHz */
Helmut64 0:c43b6919ae15 52 __HAL_RCC_PWR_CLK_ENABLE();
Helmut64 0:c43b6919ae15 53 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Helmut64 0:c43b6919ae15 54
Helmut64 0:c43b6919ae15 55 /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
Helmut64 0:c43b6919ae15 56 while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};
Helmut64 0:c43b6919ae15 57
Helmut64 0:c43b6919ae15 58 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
Helmut64 0:c43b6919ae15 59 clocks dividers */
Helmut64 0:c43b6919ae15 60 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
Helmut64 0:c43b6919ae15 61 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Helmut64 0:c43b6919ae15 62 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Helmut64 0:c43b6919ae15 63 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
Helmut64 0:c43b6919ae15 64 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
Helmut64 0:c43b6919ae15 65 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
Helmut64 0:c43b6919ae15 66 // Error_Handler();
Helmut64 0:c43b6919ae15 67 }
Helmut64 0:c43b6919ae15 68 #endif
Helmut64 0:c43b6919ae15 69 }
Helmut64 0:c43b6919ae15 70
Helmut64 0:c43b6919ae15 71 void dump(const char *title, const void *data, int len, bool dwords)
Helmut64 0:c43b6919ae15 72 {
Helmut64 0:c43b6919ae15 73 dprintf("dump(\"%s\", 0x%x, %d bytes)", title, data, len);
Helmut64 0:c43b6919ae15 74
Helmut64 0:c43b6919ae15 75 int i, j, cnt;
Helmut64 0:c43b6919ae15 76 unsigned char *u;
Helmut64 0:c43b6919ae15 77 const int width = 16;
Helmut64 0:c43b6919ae15 78 const int seppos = 7;
Helmut64 0:c43b6919ae15 79
Helmut64 0:c43b6919ae15 80 cnt = 0;
Helmut64 0:c43b6919ae15 81 u = (unsigned char *)data;
Helmut64 0:c43b6919ae15 82 while (len > 0) {
Helmut64 0:c43b6919ae15 83 ser->printf("%08x: ", (unsigned int)data + cnt);
Helmut64 0:c43b6919ae15 84 if (dwords) {
Helmut64 0:c43b6919ae15 85 unsigned int *ip = ( unsigned int *)u;
Helmut64 0:c43b6919ae15 86 ser->printf(" 0x%08x\r\n", *ip);
Helmut64 0:c43b6919ae15 87 u+= 4;
Helmut64 0:c43b6919ae15 88 len -= 4;
Helmut64 0:c43b6919ae15 89 cnt += 4;
Helmut64 0:c43b6919ae15 90 continue;
Helmut64 0:c43b6919ae15 91 }
Helmut64 0:c43b6919ae15 92 cnt += width;
Helmut64 0:c43b6919ae15 93 j = len < width ? len : width;
Helmut64 0:c43b6919ae15 94 for (i = 0; i < j; i++) {
Helmut64 0:c43b6919ae15 95 ser->printf("%2.2x ", *(u + i));
Helmut64 0:c43b6919ae15 96 if (i == seppos)
Helmut64 0:c43b6919ae15 97 ser->putc(' ');
Helmut64 0:c43b6919ae15 98 }
Helmut64 0:c43b6919ae15 99 ser->putc(' ');
Helmut64 0:c43b6919ae15 100 if (j < width) {
Helmut64 0:c43b6919ae15 101 i = width - j;
Helmut64 0:c43b6919ae15 102 if (i > seppos + 1)
Helmut64 0:c43b6919ae15 103 ser->putc(' ');
Helmut64 0:c43b6919ae15 104 while (i--) {
Helmut64 0:c43b6919ae15 105 printf("%s", " ");
Helmut64 0:c43b6919ae15 106 }
Helmut64 0:c43b6919ae15 107 }
Helmut64 0:c43b6919ae15 108 for (i = 0; i < j; i++) {
Helmut64 0:c43b6919ae15 109 int c = *(u + i);
Helmut64 0:c43b6919ae15 110 if (c >= ' ' && c <= '~')
Helmut64 0:c43b6919ae15 111 ser->putc(c);
Helmut64 0:c43b6919ae15 112 else
Helmut64 0:c43b6919ae15 113 ser->putc('.');
Helmut64 0:c43b6919ae15 114 if (i == seppos)
Helmut64 0:c43b6919ae15 115 ser->putc(' ');
Helmut64 0:c43b6919ae15 116 }
Helmut64 0:c43b6919ae15 117 len -= width;
Helmut64 0:c43b6919ae15 118 u += width;
Helmut64 0:c43b6919ae15 119 ser->printf("\r\n");
Helmut64 0:c43b6919ae15 120 }
Helmut64 0:c43b6919ae15 121 ser->printf("--\r\n");
Helmut64 0:c43b6919ae15 122 }