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