use google code style
This commit is contained in:
		
							parent
							
								
									845459fe5b
								
							
						
					
					
						commit
						4c6dbb76c1
					
				| @ -1,11 +1,11 @@ | ||||
| --- | ||||
| Language:        Cpp | ||||
| # BasedOnStyle:  LLVM | ||||
| AccessModifierOffset: -2 | ||||
| # BasedOnStyle:  Google | ||||
| AccessModifierOffset: -1 | ||||
| AlignAfterOpenBracket: Align | ||||
| AlignConsecutiveAssignments: true | ||||
| AlignConsecutiveDeclarations: true | ||||
| AlignEscapedNewlines: Right | ||||
| AlignEscapedNewlines: Left | ||||
| AlignOperands:   true | ||||
| AlignTrailingComments: true | ||||
| AllowAllParametersOfDeclarationOnNextLine: true | ||||
| @ -13,13 +13,13 @@ AllowShortBlocksOnASingleLine: false | ||||
| AllowShortCaseLabelsOnASingleLine: true | ||||
| AllowShortFunctionsOnASingleLine: All | ||||
| AllowShortIfStatementsOnASingleLine: true | ||||
| AllowShortLoopsOnASingleLine: false | ||||
| AllowShortLoopsOnASingleLine: true | ||||
| AlwaysBreakAfterDefinitionReturnType: None | ||||
| AlwaysBreakAfterReturnType: None | ||||
| AlwaysBreakBeforeMultilineStrings: false | ||||
| AlwaysBreakTemplateDeclarations: MultiLine | ||||
| BinPackArguments: false | ||||
| BinPackParameters: false | ||||
| AlwaysBreakBeforeMultilineStrings: true | ||||
| AlwaysBreakTemplateDeclarations: Yes | ||||
| BinPackArguments: true | ||||
| BinPackParameters: true | ||||
| BraceWrapping:    | ||||
|   AfterClass:      false | ||||
|   AfterControlStatement: false | ||||
| @ -45,14 +45,14 @@ BreakConstructorInitializersBeforeComma: false | ||||
| BreakConstructorInitializers: BeforeColon | ||||
| BreakAfterJavaFieldAnnotations: false | ||||
| BreakStringLiterals: true | ||||
| ColumnLimit:     120 | ||||
| ColumnLimit:     80 | ||||
| CommentPragmas:  '^ IWYU pragma:' | ||||
| CompactNamespaces: false | ||||
| ConstructorInitializerAllOnOneLineOrOnePerLine: false | ||||
| ConstructorInitializerAllOnOneLineOrOnePerLine: true | ||||
| ConstructorInitializerIndentWidth: 4 | ||||
| ContinuationIndentWidth: 4 | ||||
| Cpp11BracedListStyle: true | ||||
| DerivePointerAlignment: false | ||||
| DerivePointerAlignment: true | ||||
| DisableFormat:   false | ||||
| ExperimentalAutoDetectBinPacking: false | ||||
| FixNamespaceComments: true | ||||
| @ -62,25 +62,27 @@ ForEachMacros: | ||||
|   - BOOST_FOREACH | ||||
| IncludeBlocks:   Preserve | ||||
| IncludeCategories:  | ||||
|   - Regex:           '^"(llvm|llvm-c|clang|clang-c)/' | ||||
|   - Regex:           '^<ext/.*\.h>' | ||||
|     Priority:        2 | ||||
|   - Regex:           '^(<|"(gtest|gmock|isl|json)/)' | ||||
|     Priority:        3 | ||||
|   - Regex:           '.*' | ||||
|   - Regex:           '^<.*\.h>' | ||||
|     Priority:        1 | ||||
| IncludeIsMainRegex: '(Test)?$' | ||||
| IndentCaseLabels: false | ||||
|   - Regex:           '^<.*' | ||||
|     Priority:        2 | ||||
|   - Regex:           '.*' | ||||
|     Priority:        3 | ||||
| IncludeIsMainRegex: '([-_](test|unittest))?$' | ||||
| IndentCaseLabels: true | ||||
| IndentPPDirectives: None | ||||
| IndentWidth:     2 | ||||
| IndentWrappedFunctionNames: false | ||||
| JavaScriptQuotes: Leave | ||||
| JavaScriptWrapImports: true | ||||
| KeepEmptyLinesAtTheStartOfBlocks: true | ||||
| KeepEmptyLinesAtTheStartOfBlocks: false | ||||
| MacroBlockBegin: '' | ||||
| MacroBlockEnd:   '' | ||||
| MaxEmptyLinesToKeep: 1 | ||||
| NamespaceIndentation: None | ||||
| ObjCBinPackProtocolList: Auto | ||||
| ObjCBinPackProtocolList: Never | ||||
| ObjCBlockIndentWidth: 2 | ||||
| ObjCSpaceAfterProperty: false | ||||
| ObjCSpaceBeforeProtocolList: true | ||||
| @ -91,8 +93,36 @@ PenaltyBreakFirstLessLess: 120 | ||||
| PenaltyBreakString: 1000 | ||||
| PenaltyBreakTemplateDeclaration: 10 | ||||
| PenaltyExcessCharacter: 1000000 | ||||
| PenaltyReturnTypeOnItsOwnLine: 60 | ||||
| PointerAlignment: Right | ||||
| PenaltyReturnTypeOnItsOwnLine: 200 | ||||
| PointerAlignment: Left | ||||
| RawStringFormats:  | ||||
|   - Language:        Cpp | ||||
|     Delimiters:       | ||||
|       - cc | ||||
|       - CC | ||||
|       - cpp | ||||
|       - Cpp | ||||
|       - CPP | ||||
|       - 'c++' | ||||
|       - 'C++' | ||||
|     CanonicalDelimiter: '' | ||||
|     BasedOnStyle:    google | ||||
|   - Language:        TextProto | ||||
|     Delimiters:       | ||||
|       - pb | ||||
|       - PB | ||||
|       - proto | ||||
|       - PROTO | ||||
|     EnclosingFunctions:  | ||||
|       - EqualsProto | ||||
|       - EquivToProto | ||||
|       - PARSE_PARTIAL_TEXT_PROTO | ||||
|       - PARSE_TEST_PROTO | ||||
|       - PARSE_TEXT_PROTO | ||||
|       - ParseTextOrDie | ||||
|       - ParseTextProtoOrDie | ||||
|     CanonicalDelimiter: '' | ||||
|     BasedOnStyle:    google | ||||
| ReflowComments:  true | ||||
| SortIncludes:    true | ||||
| SortUsingDeclarations: true | ||||
| @ -105,13 +135,13 @@ SpaceBeforeInheritanceColon: true | ||||
| SpaceBeforeParens: ControlStatements | ||||
| SpaceBeforeRangeBasedForLoopColon: true | ||||
| SpaceInEmptyParentheses: false | ||||
| SpacesBeforeTrailingComments: 1 | ||||
| SpacesBeforeTrailingComments: 2 | ||||
| SpacesInAngles:  false | ||||
| SpacesInContainerLiterals: true | ||||
| SpacesInCStyleCastParentheses: false | ||||
| SpacesInParentheses: false | ||||
| SpacesInSquareBrackets: false | ||||
| Standard:        Cpp11 | ||||
| Standard:        Auto | ||||
| TabWidth:        8 | ||||
| UseTab:          Never | ||||
| ... | ||||
|  | ||||
| @ -107,14 +107,16 @@ esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr) { | ||||
|       .pre_cb           = NULL, | ||||
|       .post_cb          = NULL, | ||||
|   }; | ||||
|   ret = spi_bus_add_device(config->spi_host, &device_config, &handle->device_handle); | ||||
|   ret = spi_bus_add_device(config->spi_host, &device_config, | ||||
|                            &handle->device_handle); | ||||
|   SX127X_ERROR_CHECK2(ret, spi_bus_add_device) | ||||
| 
 | ||||
|   // read version and check that it is compatible
 | ||||
|   uint8_t version; | ||||
|   ret = sx127x_read_register(handle, REG_VERSION, &version); | ||||
|   SX127X_ERROR_CHECK2(ret, sx127x_read_register); | ||||
|   SX127X_CHECK(version == 0x12, "unsupported version %#x", ESP_ERR_INVALID_VERSION, version); | ||||
|   SX127X_CHECK(version == 0x12, "unsupported version %#x", | ||||
|                ESP_ERR_INVALID_VERSION, version); | ||||
| 
 | ||||
|   ret = sx127x_write_config(handle); | ||||
|   SX127X_ERROR_CHECK(ret); | ||||
| @ -186,11 +188,14 @@ void sx127x_do_tx(sx127x_t *handle, sx127x_packet_t *packet) { | ||||
|   _TX_CHECK(sx127x_write_register(handle, REG_PAYLOAD_LENGTH, 0)); | ||||
| 
 | ||||
|   _TX_CHECK(sx127x_write_fifo(handle, packet->data, packet->data_len)); | ||||
|   _TX_CHECK(sx127x_write_register(handle, REG_PAYLOAD_LENGTH, packet->data_len)); | ||||
|   _TX_CHECK( | ||||
|       sx127x_write_register(handle, REG_PAYLOAD_LENGTH, packet->data_len)); | ||||
| 
 | ||||
|   _TX_CHECK(sx127x_write_register(handle, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_FS_TX)); | ||||
|   _TX_CHECK(sx127x_write_register(handle, REG_OP_MODE, | ||||
|                                   SX127X_LONG_RANGE | SX127X_MODE_FS_TX)); | ||||
|   vTaskDelay(pdMS_TO_TICKS(1)); | ||||
|   _TX_CHECK(sx127x_write_register(handle, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_TX)); | ||||
|   _TX_CHECK(sx127x_write_register(handle, REG_OP_MODE, | ||||
|                                   SX127X_LONG_RANGE | SX127X_MODE_TX)); | ||||
| 
 | ||||
|   // wait for transmission to finish
 | ||||
|   while (true) { | ||||
| @ -209,7 +214,8 @@ error: | ||||
|     ESP_LOGE(SX127X_TAG, "tx error: %s (%d)", error_name, ret); | ||||
|   } | ||||
|   // go back to rx mode
 | ||||
|   sx127x_write_register(handle, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); | ||||
|   sx127x_write_register(handle, REG_OP_MODE, | ||||
|                         SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); | ||||
| } | ||||
| 
 | ||||
| void sx127x_task(void *arg) { | ||||
| @ -245,7 +251,8 @@ void sx127x_task(void *arg) { | ||||
| esp_err_t sx127x_start(sx127x_t *handle) { | ||||
|   esp_err_t ret; | ||||
| 
 | ||||
|   SX127X_CHECK(handle->task_handle == NULL, "task already running", ESP_ERR_INVALID_STATE); | ||||
|   SX127X_CHECK(handle->task_handle == NULL, "task already running", | ||||
|                ESP_ERR_INVALID_STATE); | ||||
| 
 | ||||
|   handle->intr_semaphore = xSemaphoreCreateBinary(); | ||||
|   // handle->recv_packet_queue = xQueueCreate(8, 10);
 | ||||
| @ -253,7 +260,8 @@ esp_err_t sx127x_start(sx127x_t *handle) { | ||||
| 
 | ||||
|   handle->task_state = SX127X_TASK_RUNNING; | ||||
|   BaseType_t pdRet = | ||||
|       xTaskCreate(sx127x_task, "sx127x_task", TASK_STACK_SIZE, (void *)handle, TASK_PRIORITY, &handle->task_handle); | ||||
|       xTaskCreate(sx127x_task, "sx127x_task", TASK_STACK_SIZE, (void *)handle, | ||||
|                   TASK_PRIORITY, &handle->task_handle); | ||||
|   SX127X_CHECK(pdRet == pdPASS, "failed to create task", ESP_FAIL); | ||||
| 
 | ||||
|   ret = sx127x_write_register(handle, REG_DIO_MAPPING_1, 0x00); | ||||
| @ -270,7 +278,8 @@ esp_err_t sx127x_start(sx127x_t *handle) { | ||||
| 
 | ||||
|   ret = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1); | ||||
|   SX127X_ERROR_CHECK2(ret, gpio_install_isr_service); | ||||
|   ret = gpio_isr_handler_add(handle->config.irq_io_num, sx127x_isr, (void *)handle); | ||||
|   ret = gpio_isr_handler_add(handle->config.irq_io_num, sx127x_isr, | ||||
|                              (void *)handle); | ||||
|   SX127X_ERROR_CHECK2(ret, gpio_isr_handler_add); | ||||
| 
 | ||||
|   return ESP_OK; | ||||
| @ -279,7 +288,8 @@ esp_err_t sx127x_start(sx127x_t *handle) { | ||||
| esp_err_t sx127x_stop(sx127x_t *handle) { | ||||
|   esp_err_t ret; | ||||
| 
 | ||||
|   SX127X_CHECK(handle->task_handle != NULL, "task has not been started", ESP_ERR_INVALID_STATE); | ||||
|   SX127X_CHECK(handle->task_handle != NULL, "task has not been started", | ||||
|                ESP_ERR_INVALID_STATE); | ||||
|   handle->task_state = SX127X_TASK_STOPPING; | ||||
|   xTaskNotifyGive(handle->task_handle); | ||||
| 
 | ||||
| @ -295,15 +305,19 @@ esp_err_t sx127x_stop(sx127x_t *handle) { | ||||
|   return ESP_OK; | ||||
| } | ||||
| 
 | ||||
| esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, size_t data_len, TickType_t ticks_to_wait) { | ||||
|   SX127X_CHECK(handle->task_state == SX127X_TASK_RUNNING, "task not running", ESP_FAIL); | ||||
|   SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d", ESP_FAIL, data_len); | ||||
| esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, | ||||
|                              size_t data_len, TickType_t ticks_to_wait) { | ||||
|   SX127X_CHECK(handle->task_state == SX127X_TASK_RUNNING, "task not running", | ||||
|                ESP_FAIL); | ||||
|   SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d", | ||||
|                ESP_FAIL, data_len); | ||||
|   sx127x_packet_t packet; | ||||
|   packet.data_len = data_len; | ||||
|   packet.data     = heap_caps_malloc(data_len, MALLOC_CAP_DMA); | ||||
|   SX127X_CHECK(packet.data != NULL, "malloc error", ESP_ERR_NO_MEM); | ||||
|   memcpy(packet.data, data, data_len); | ||||
|   BaseType_t pdRet = xQueueSend(handle->tx_packet_queue, &packet, ticks_to_wait); | ||||
|   BaseType_t pdRet = | ||||
|       xQueueSend(handle->tx_packet_queue, &packet, ticks_to_wait); | ||||
|   SX127X_CHECK(pdRet == pdTRUE, "tx queue full", ESP_ERR_TIMEOUT); | ||||
|   return ESP_OK; | ||||
| } | ||||
|  | ||||
| @ -82,4 +82,5 @@ esp_err_t sx127x_start(sx127x_t *handle); | ||||
| 
 | ||||
| esp_err_t sx127x_stop(sx127x_t *handle); | ||||
| 
 | ||||
| esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, size_t data_len, TickType_t ticks_to_wait); | ||||
| esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, | ||||
|                              size_t data_len, TickType_t ticks_to_wait); | ||||
|  | ||||
| @ -87,7 +87,8 @@ typedef enum sx127x_op_mode { | ||||
| #else | ||||
| #define SX127X_CHECK(check, str, ret_val, ...)                         \ | ||||
|   if (!(check)) {                                                      \ | ||||
|     ESP_LOGE(SX127X_TAG, "%s:%d: check %s failed: " str, __FUNCTION__, __LINE__, #check, ##__VA_ARGS__);               \ | ||||
|     ESP_LOGE(SX127X_TAG, "%s:%d: check %s failed: " str, __FUNCTION__, \ | ||||
|              __LINE__, #check, ##__VA_ARGS__);                         \ | ||||
|     return (ret_val);                                                  \ | ||||
|   } | ||||
| #define SX127X_ERROR_CHECK(ret)                                      \ | ||||
| @ -95,7 +96,8 @@ typedef enum sx127x_op_mode { | ||||
|     esp_err_t _error_code = (ret);                                   \ | ||||
|     if (_error_code != ESP_OK) {                                     \ | ||||
|       const char *_error_name = esp_err_to_name(_error_code);        \ | ||||
|       ESP_LOGE(SX127X_TAG, "%s:%d: %s (%d)", __FUNCTION__, __LINE__, _error_name, _error_code);                        \ | ||||
|       ESP_LOGE(SX127X_TAG, "%s:%d: %s (%d)", __FUNCTION__, __LINE__, \ | ||||
|                _error_name, _error_code);                            \ | ||||
|       return _error_code;                                            \ | ||||
|     }                                                                \ | ||||
|   } | ||||
| @ -104,7 +106,8 @@ typedef enum sx127x_op_mode { | ||||
|     esp_err_t _error_code = (ret);                                    \ | ||||
|     if (_error_code != ESP_OK) {                                      \ | ||||
|       const char *_error_name = esp_err_to_name(_error_code);         \ | ||||
|       ESP_LOGE(SX127X_TAG, "%s:%d: %s failed: %s (%d)", __FUNCTION__, __LINE__, #fun, _error_name, _error_code);        \ | ||||
|       ESP_LOGE(SX127X_TAG, "%s:%d: %s failed: %s (%d)", __FUNCTION__, \ | ||||
|                __LINE__, #fun, _error_name, _error_code);             \ | ||||
|       return _error_code;                                             \ | ||||
|     }                                                                 \ | ||||
|   } | ||||
| @ -121,7 +124,8 @@ struct sx127x { | ||||
|   spi_device_handle_t device_handle; | ||||
|   TaskHandle_t        task_handle; | ||||
|   sx127x_task_state_t task_state; | ||||
|   // binary semaphore which is given when an interrupt is received from the radio
 | ||||
|   // binary semaphore which is given when an interrupt is received from the
 | ||||
|   // radio
 | ||||
|   SemaphoreHandle_t intr_semaphore; | ||||
|   // queue of packets received from the radio
 | ||||
|   QueueHandle_t rx_packet_queue; | ||||
|  | ||||
| @ -11,7 +11,8 @@ esp_err_t sx127x_write_register(sx127x_t *handle, uint8_t reg, uint8_t value) { | ||||
|   return sx127x_single_transfer(handle, reg | 0x80, value, NULL); | ||||
| } | ||||
| 
 | ||||
| esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, uint8_t to_slave, uint8_t *from_slave) { | ||||
| esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, | ||||
|                                  uint8_t to_slave, uint8_t *from_slave) { | ||||
|   spi_transaction_t trans; | ||||
|   memset(&trans, 0, sizeof(trans)); | ||||
|   trans.flags      = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA; | ||||
| @ -25,16 +26,19 @@ esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, uint8_t to_slav | ||||
|   if (from_slave) { | ||||
|     *from_slave = trans.rx_data[0]; | ||||
|   } | ||||
|   ESP_LOGV(SX127X_TAG, "sx127x_single_transfer(%#x, %#x, %#x)", addr, trans.tx_data[0], trans.rx_data[0]); | ||||
|   ESP_LOGV(SX127X_TAG, "sx127x_single_transfer(%#x, %#x, %#x)", addr, | ||||
|            trans.tx_data[0], trans.rx_data[0]); | ||||
|   return ESP_OK; | ||||
| } | ||||
| 
 | ||||
| esp_err_t sx127x_sleep(sx127x_t *handle) { | ||||
|   return sx127x_write_register(handle, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_SLEEP); | ||||
|   return sx127x_write_register(handle, REG_OP_MODE, | ||||
|                                SX127X_LONG_RANGE | SX127X_MODE_SLEEP); | ||||
| } | ||||
| 
 | ||||
| esp_err_t sx127x_standby(sx127x_t *handle) { | ||||
|   return sx127x_write_register(handle, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_STDBY); | ||||
|   return sx127x_write_register(handle, REG_OP_MODE, | ||||
|                                SX127X_LONG_RANGE | SX127X_MODE_STDBY); | ||||
| } | ||||
| 
 | ||||
| esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency) { | ||||
| @ -54,15 +58,19 @@ esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency) { | ||||
|   return ESP_OK; | ||||
| } | ||||
| 
 | ||||
| esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, sx127x_pa_boost_t pa_boost) { | ||||
| esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, | ||||
|                               sx127x_pa_boost_t pa_boost) { | ||||
|   esp_err_t ret; | ||||
|   if (pa_boost == SX127X_PA_BOOST_ENABLED) { | ||||
|     // PA BOOST
 | ||||
|     SX127X_CHECK(tx_power >= 2 && tx_power <= 20, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, tx_power); | ||||
|     ret = sx127x_write_register(handle, REG_PA_CONFIG, PA_BOOST | (tx_power - 2)); | ||||
|     SX127X_CHECK(tx_power >= 2 && tx_power <= 20, "invalid tx_power: %d", | ||||
|                  ESP_ERR_INVALID_ARG, tx_power); | ||||
|     ret = | ||||
|         sx127x_write_register(handle, REG_PA_CONFIG, PA_BOOST | (tx_power - 2)); | ||||
|   } else { | ||||
|     // RFO
 | ||||
|     SX127X_CHECK(tx_power <= 14, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, tx_power); | ||||
|     SX127X_CHECK(tx_power <= 14, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, | ||||
|                  tx_power); | ||||
|     ret = sx127x_write_register(handle, REG_PA_CONFIG, 0x70 | tx_power); | ||||
|   } | ||||
|   SX127X_ERROR_CHECK(ret); | ||||
| @ -71,8 +79,10 @@ esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, sx127x_pa_boos | ||||
|   return ESP_OK; | ||||
| } | ||||
| 
 | ||||
| esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, uint8_t spreading_factor) { | ||||
|   SX127X_CHECK(spreading_factor >= 6 && spreading_factor <= 12, "invalid spreading_factor", ESP_ERR_INVALID_ARG); | ||||
| esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, | ||||
|                                       uint8_t   spreading_factor) { | ||||
|   SX127X_CHECK(spreading_factor >= 6 && spreading_factor <= 12, | ||||
|                "invalid spreading_factor", ESP_ERR_INVALID_ARG); | ||||
|   // section 4.1.1.2 in SX1276 datasheet
 | ||||
|   uint8_t detection_optimize, detection_threshold; | ||||
|   if (spreading_factor == 6) { | ||||
| @ -82,24 +92,31 @@ esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, uint8_t spreading_factor | ||||
|     detection_optimize  = 0xc3; | ||||
|     detection_threshold = 0x0a; | ||||
|   } | ||||
|   SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_OPTIMIZE, detection_optimize)); | ||||
|   SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_THRESHOLD, detection_threshold)); | ||||
|   SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_OPTIMIZE, | ||||
|                                            detection_optimize)); | ||||
|   SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_THRESHOLD, | ||||
|                                            detection_threshold)); | ||||
| 
 | ||||
|   uint8_t modem_config_3; | ||||
|   SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); | ||||
|   SX127X_ERROR_CHECK( | ||||
|       sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); | ||||
|   modem_config_3 = (modem_config_3 & 0x03) | ((spreading_factor << 4) & 0xf0); | ||||
|   SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); | ||||
|   SX127X_ERROR_CHECK( | ||||
|       sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); | ||||
| 
 | ||||
|   handle->config.spreading_factor = spreading_factor; | ||||
|   return ESP_OK; | ||||
| } | ||||
| 
 | ||||
| esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, uint64_t signal_bandwidth) { | ||||
| esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, | ||||
|                                       uint64_t  signal_bandwidth) { | ||||
|   uint8_t bw_reg = sx127x_bw_to_reg(signal_bandwidth); | ||||
|   uint8_t modem_config_1; | ||||
|   SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_1, &modem_config_1)); | ||||
|   SX127X_ERROR_CHECK( | ||||
|       sx127x_read_register(handle, REG_MODEM_CONFIG_1, &modem_config_1)); | ||||
|   modem_config_1 = (modem_config_1 & 0x0f) | (bw_reg << 4); | ||||
|   SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_1, modem_config_1)); | ||||
|   SX127X_ERROR_CHECK( | ||||
|       sx127x_write_register(handle, REG_MODEM_CONFIG_1, modem_config_1)); | ||||
|   handle->config.signal_bandwidth = signal_bandwidth; | ||||
| 
 | ||||
|   // set low data rate optimization flag
 | ||||
| @ -113,13 +130,15 @@ esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, uint64_t signal_bandwidt | ||||
|   bool ldo = must_have_ldo || (handle->config.ldo == SX127X_LDO_ENABLED); | ||||
| 
 | ||||
|   uint8_t modem_config_3; | ||||
|   SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); | ||||
|   SX127X_ERROR_CHECK( | ||||
|       sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); | ||||
|   if (ldo) { | ||||
|     modem_config_3 |= (1 << 3); | ||||
|   } else { | ||||
|     modem_config_3 &= ~(1 << 3); | ||||
|   } | ||||
|   SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); | ||||
|   SX127X_ERROR_CHECK( | ||||
|       sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); | ||||
| 
 | ||||
|   return ESP_OK; | ||||
| } | ||||
| @ -172,13 +191,15 @@ esp_err_t sx127x_set_sync_word(sx127x_t *handle, uint8_t sync_word) { | ||||
| 
 | ||||
| esp_err_t sx127x_set_crc(sx127x_t *handle, sx127x_crc_t crc) { | ||||
|   uint8_t modem_config_2; | ||||
|   SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_2, &modem_config_2)); | ||||
|   SX127X_ERROR_CHECK( | ||||
|       sx127x_read_register(handle, REG_MODEM_CONFIG_2, &modem_config_2)); | ||||
|   if (crc == SX127X_CRC_ENABLED) { | ||||
|     modem_config_2 |= CONFIG2_CRC; | ||||
|   } else { | ||||
|     modem_config_2 &= ~CONFIG2_CRC; | ||||
|   } | ||||
|   SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_2, modem_config_2)); | ||||
|   SX127X_ERROR_CHECK( | ||||
|       sx127x_write_register(handle, REG_MODEM_CONFIG_2, modem_config_2)); | ||||
|   handle->config.crc = crc; | ||||
|   return ESP_OK; | ||||
| } | ||||
|  | ||||
| @ -2,7 +2,8 @@ | ||||
| 
 | ||||
| esp_err_t sx127x_read_register(sx127x_t *handle, uint8_t reg, uint8_t *value); | ||||
| esp_err_t sx127x_write_register(sx127x_t *handle, uint8_t reg, uint8_t value); | ||||
| esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, uint8_t to_slave, uint8_t *from_slave); | ||||
| esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, | ||||
|                                  uint8_t to_slave, uint8_t *from_slave); | ||||
| 
 | ||||
| esp_err_t sx127x_sleep(sx127x_t *handle); | ||||
| 
 | ||||
| @ -10,11 +11,14 @@ esp_err_t sx127x_standby(sx127x_t *handle); | ||||
| 
 | ||||
| esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency); | ||||
| 
 | ||||
| esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, sx127x_pa_boost_t pa_boost); | ||||
| esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, | ||||
|                               sx127x_pa_boost_t pa_boost); | ||||
| 
 | ||||
| esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, uint8_t spreading_factor); | ||||
| esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, | ||||
|                                       uint8_t   spreading_factor); | ||||
| 
 | ||||
| esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, uint64_t signal_bandwidth); | ||||
| esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, | ||||
|                                       uint64_t  signal_bandwidth); | ||||
| uint8_t   sx127x_bw_to_reg(uint64_t bandwidth); | ||||
| uint64_t  sx127x_reg_to_bw(uint8_t bandwidth_reg); | ||||
| 
 | ||||
| @ -23,4 +27,5 @@ esp_err_t sx127x_set_sync_word(sx127x_t *handle, uint8_t sync_word); | ||||
| esp_err_t sx127x_set_crc(sx127x_t *handle, sx127x_crc_t crc); | ||||
| 
 | ||||
| esp_err_t sx127x_write_fifo(sx127x_t *handle, char *data, size_t data_len); | ||||
| esp_err_t sx127x_read_fifo(sx127x_t *handle, char *data_out, size_t *data_len_out); | ||||
| esp_err_t sx127x_read_fifo(sx127x_t *handle, char *data_out, | ||||
|                            size_t *data_len_out); | ||||
|  | ||||
| @ -38,8 +38,10 @@ void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) { | ||||
|  * HAL callback function as prescribed by the U8G2 library.  This callback is | ||||
|  * invoked to handle SPI communications. | ||||
|  */ | ||||
| uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { | ||||
|   ESP_LOGV(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr); | ||||
| uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, | ||||
|                                void *arg_ptr) { | ||||
|   ESP_LOGV(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", | ||||
|            msg, arg_int, arg_ptr); | ||||
|   switch (msg) { | ||||
|     case U8X8_MSG_BYTE_SET_DC: | ||||
|       if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) { | ||||
| @ -48,7 +50,8 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void | ||||
|       break; | ||||
| 
 | ||||
|     case U8X8_MSG_BYTE_INIT: { | ||||
|     if (u8g2_esp32_hal.clk == U8G2_ESP32_HAL_UNDEFINED || u8g2_esp32_hal.mosi == U8G2_ESP32_HAL_UNDEFINED || | ||||
|       if (u8g2_esp32_hal.clk == U8G2_ESP32_HAL_UNDEFINED || | ||||
|           u8g2_esp32_hal.mosi == U8G2_ESP32_HAL_UNDEFINED || | ||||
|           u8g2_esp32_hal.cs == U8G2_ESP32_HAL_UNDEFINED) { | ||||
|         break; | ||||
|       } | ||||
| @ -105,7 +108,8 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void | ||||
|  * HAL callback function as prescribed by the U8G2 library.  This callback is | ||||
|  * invoked to handle I2C communications. | ||||
|  */ | ||||
| uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { | ||||
| uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, | ||||
|                                void *arg_ptr) { | ||||
| #define TXBUF_SIZE 32 | ||||
|   static uint8_t  txbuf[TXBUF_SIZE]; | ||||
|   static uint8_t *txbuf_ptr; | ||||
| @ -120,7 +124,8 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void | ||||
|     } | ||||
| 
 | ||||
|     case U8X8_MSG_BYTE_INIT: { | ||||
|     if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED) { | ||||
|       if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || | ||||
|           u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED) { | ||||
|         break; | ||||
|       } | ||||
| 
 | ||||
| @ -133,9 +138,8 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void | ||||
|       ESP_LOGV(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ); | ||||
|       conf.master.clk_speed = I2C_MASTER_FREQ_HZ; | ||||
|       ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf)); | ||||
|     ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, | ||||
|                                        conf.mode, | ||||
|                                        I2C_MASTER_RX_BUF_DISABLE, | ||||
|       ESP_ERROR_CHECK(i2c_driver_install( | ||||
|           I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, | ||||
|           I2C_MASTER_TX_BUF_DISABLE, | ||||
|           ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL1)); | ||||
|       break; | ||||
| @ -161,7 +165,8 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void | ||||
|       handle_i2c          = i2c_cmd_link_create(); | ||||
|       // ESP_LOGV(TAG, "Start I2C transfer to %02X.", i2c_address >> 1);
 | ||||
|       ESP_ERROR_CHECK(i2c_master_start(handle_i2c)); | ||||
|     ESP_ERROR_CHECK(i2c_master_write_byte(handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN)); | ||||
|       ESP_ERROR_CHECK(i2c_master_write_byte( | ||||
|           handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN)); | ||||
|       txbuf_ptr = txbuf; | ||||
|       break; | ||||
|     } | ||||
| @ -169,9 +174,11 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void | ||||
|     case U8X8_MSG_BYTE_END_TRANSFER: { | ||||
|       ESP_LOGV(TAG, "end I2C transfer. txbuf len: %d", txbuf_ptr - txbuf); | ||||
|       // ESP_LOG_BUFFER_HEXDUMP(TAG, txbuf, txbuf_ptr - txbuf, ESP_LOG_VERBOSE);
 | ||||
|     ESP_ERROR_CHECK(i2c_master_write(handle_i2c, txbuf, txbuf_ptr - txbuf, ACK_CHECK_EN)); | ||||
|       ESP_ERROR_CHECK( | ||||
|           i2c_master_write(handle_i2c, txbuf, txbuf_ptr - txbuf, ACK_CHECK_EN)); | ||||
|       ESP_ERROR_CHECK(i2c_master_stop(handle_i2c)); | ||||
|     ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, I2C_TIMEOUT_MS / portTICK_RATE_MS)); | ||||
|       ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, | ||||
|                                            I2C_TIMEOUT_MS / portTICK_RATE_MS)); | ||||
|       i2c_cmd_link_delete(handle_i2c); | ||||
|       break; | ||||
|     } | ||||
| @ -184,8 +191,11 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void | ||||
|  * HAL callback function as prescribed by the U8G2 library.  This callback is | ||||
|  * invoked to handle callbacks for GPIO and delay functions. | ||||
|  */ | ||||
| uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { | ||||
|   ESP_LOGV(TAG, "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr); | ||||
| uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, | ||||
|                                      void *arg_ptr) { | ||||
|   ESP_LOGV(TAG, | ||||
|            "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", | ||||
|            msg, arg_int, arg_ptr); | ||||
| 
 | ||||
|   switch (msg) { | ||||
|       // Initialize the GPIO and DELAY HAL functions.  If the pins for DC and
 | ||||
|  | ||||
| @ -34,12 +34,17 @@ typedef struct { | ||||
| 
 | ||||
| #define U8G2_ESP32_HAL_DEFAULT                              \ | ||||
|   {                                                         \ | ||||
|     U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED,            \ | ||||
|         U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED                                   \ | ||||
|     U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED,     \ | ||||
|         U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, \ | ||||
|         U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, \ | ||||
|         U8G2_ESP32_HAL_UNDEFINED                            \ | ||||
|   } | ||||
| 
 | ||||
| void    u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param); | ||||
| uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); | ||||
| uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); | ||||
| uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); | ||||
| uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, | ||||
|                                void *arg_ptr); | ||||
| uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, | ||||
|                                void *arg_ptr); | ||||
| uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, | ||||
|                                      void *arg_ptr); | ||||
| #endif /* U8G2_ESP32_HAL_H_ */ | ||||
| @ -59,7 +59,8 @@ void setup_oled(void) { | ||||
|   }; | ||||
|   u8g2_esp32_hal_init(u8g2_hal_config); | ||||
| 
 | ||||
|   u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb); | ||||
|   u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, | ||||
|                                          u8g2_esp32_gpio_and_delay_cb); | ||||
|   u8g2_InitDisplay(&u8g2); | ||||
|   u8g2_ClearDisplay(&u8g2); | ||||
|   u8g2_SetPowerSave(&u8g2, false); | ||||
| @ -120,9 +121,11 @@ void loraTask(void *params) { | ||||
|   struct Packet recvd_packet; | ||||
|   while (true) { | ||||
|     TickType_t delay_ticks = next_send - current_time; | ||||
|     BaseType_t didReceive  = xQueueReceive(lora_packet_isr_queue, &packet_len, delay_ticks); | ||||
|     BaseType_t didReceive = | ||||
|         xQueueReceive(lora_packet_isr_queue, &packet_len, delay_ticks); | ||||
|     if (didReceive) { | ||||
|       int packetSize = (packet_len > LORA_BUF_LEN - 1) ? (LORA_BUF_LEN - 1) : (packet_len); | ||||
|       int packetSize = | ||||
|           (packet_len > LORA_BUF_LEN - 1) ? (LORA_BUF_LEN - 1) : (packet_len); | ||||
|       // LoRa.setTimeout(50);
 | ||||
|       // LoRa.readBytes(recvd_packet.buffer, packetSize);
 | ||||
|       recvd_packet.buffer_len             = packetSize; | ||||
| @ -134,13 +137,15 @@ void loraTask(void *params) { | ||||
|     } | ||||
|     current_time = xTaskGetTickCount(); | ||||
|     if (current_time >= next_send) { | ||||
|       int written_bytes = snprintf(tx_buf, tx_buf_len, "hello world %d", packet_num); | ||||
|       int written_bytes = | ||||
|           snprintf(tx_buf, tx_buf_len, "hello world %d", packet_num); | ||||
|       if (written_bytes < 0) { | ||||
|         ESP_LOGE(TAG, "snprintf error: %d", written_bytes); | ||||
|         continue; | ||||
|       } | ||||
|       packet_num++; | ||||
|       esp_err_t ret = sx127x_send_packet(lora, tx_buf, written_bytes, 0); // 0 means error if queue full
 | ||||
|       esp_err_t ret = sx127x_send_packet(lora, tx_buf, written_bytes, | ||||
|                                          0);  // 0 means error if queue full
 | ||||
|       if (ret != ESP_OK) { | ||||
|         ESP_LOGE(TAG, "error sending packet: %d", ret); | ||||
|       } else { | ||||
| @ -166,16 +171,13 @@ void loop(void) { | ||||
|     u8g2_DrawStr(&u8g2, 4, 8, "Hello World!"); | ||||
|     char buf[32]; | ||||
|     memset(buf, 0, 32); | ||||
|     snprintf(buf, 32, "heap allc/free %d/%d", heap_info.total_allocated_bytes, heap_info.total_free_bytes); | ||||
|     snprintf(buf, 32, "heap allc/free %d/%d", heap_info.total_allocated_bytes, | ||||
|              heap_info.total_free_bytes); | ||||
|     u8g2_DrawStr(&u8g2, 4, 8 + 8, buf); | ||||
| 
 | ||||
|     if (packet.buffer_len) { | ||||
|       ESP_LOGI(TAG, | ||||
|                "lora received packet (len %d, rssi: %d, snr: %f): %s\n", | ||||
|                packet.buffer_len, | ||||
|                packet.rssi, | ||||
|                packet.snr, | ||||
|                packet.buffer); | ||||
|       ESP_LOGI(TAG, "lora received packet (len %d, rssi: %d, snr: %f): %s\n", | ||||
|                packet.buffer_len, packet.rssi, packet.snr, packet.buffer); | ||||
|       u8g2_SetFont(&u8g2, u8g2_font_4x6_mf); | ||||
|       snprintf(buf, 40, "lora pkt(rssi: %d, snr: %f)", packet.rssi, packet.snr); | ||||
|       u8g2_DrawStr(&u8g2, 4, 8 + 8 + 8, buf); | ||||
| @ -193,4 +195,6 @@ void loopTask(void *pvUser) { | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void app_main() { xTaskCreatePinnedToCore(loopTask, "loopTask", 8192, NULL, 1, NULL, 1); } | ||||
| void app_main() { | ||||
|   xTaskCreatePinnedToCore(loopTask, "loopTask", 8192, NULL, 1, NULL, 1); | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user