I need to use cJSON to parse the uart data. I found the file in SDK15.3 but how can I use the related function?
I need to use cJSON to parse the uart data. I found the file in SDK15.3 but how can I use the related function?
Hi,
just add sources and headers to your project and call functions as usual... you also need to add mem_manager library from SDK. cJSON has a good README with detailed instructions how to use this library.
Is there a routine example of how to use the memory management library?
Sorry, I still have doubts. I still don't understand the relationship between the memory management module and the cJSON library in the background_dfu example. Under extreme conditions, I need to parse 8192 bytes of data and build 47077 bytes of data (not including 3637 bytes of data in JSON format). What should I do?
In addition, how to increase the uart buffer? I am increasing the TX BUFFER and RX BUFFER to 8192, but under extreme conditions, I will send the constructed 47077 bytes of JSON format data through the serial port. TX BUFFER is obviously not enough
Hi,
sorry for late answer. If your MCU is 52840, there is no tight constraints on memory. You can expect about 70k for parsed data. Additionally, for printed output you have to allocate a buffer capable to hold maximum string you can build (48k). It's better to allocate this buffer as static array, not mixing with cJSON memory, and write an UART routine that will send characters directly from this array (otherwise you have to allocate ONE MORE buffer of that size as UART TX buffer...) Or maybe in your case it worth to look for a json library with serialization, that can use callbacks instead of preallocated buffers.
Hi,
I set UART_TX_BUF_SIZE is 8192. Can this make the uart send out nearly 48k without error? on one time?
Depends on how you've implemented it. You can allocate a very small buffer, then send your data fragment by fragment at APP_UART_TX_EMPTY event (if you use app_uart_fifo). WIth nrfx_uarte library you don't need any intermediate buffer, transmission is done directly from buffer you've passed to tx function (just make sure not to free it until it transferred completely). nRF52840 can transfer up to 65535 bytes with nrfx_uarte, but if you require compatibility with lower series, you have to split transmission by 255 bytes.
Depends on how you've implemented it. You can allocate a very small buffer, then send your data fragment by fragment at APP_UART_TX_EMPTY event (if you use app_uart_fifo). WIth nrfx_uarte library you don't need any intermediate buffer, transmission is done directly from buffer you've passed to tx function (just make sure not to free it until it transferred completely). nRF52840 can transfer up to 65535 bytes with nrfx_uarte, but if you require compatibility with lower series, you have to split transmission by 255 bytes.
OK. I will try that. But now there is another problem. When I use the cJSON_CreateNumber function, the system will crash. DEBUG found that when this function was called, it crashed in HardFault_Handler.
Further tracking reveals that as soon as I pass the external numeric data into the valuedouble of the cJSON structure, ie executing item->valuedouble=num(cJSON *item) will crash in HardFault_Handler
When I don't call cJSON_CreateNumber everything works fine, as soon as I call it, it will crash
How can I solve this problem?
Another new problem is that if I mask nrf_mem_init and cJSON_Init(). use malloc instead of nrf_malloc. Then cJSON_CreateNumber can be called normally, the item->valuedouble=num; in the function can be executed normally, if you use nrf_malloc, it will crash. (Excludes the extra effects, only calling cJSON_CreateNumber(2) in the main function.)
I think I found the reason. This issue can occur if the total number of blocks from XXS to XXL exceeds 255. This is independent of the block size, and I firmly believe that the size of each memory block I set is reasonable. Because, I use nrf_malloc to apply for the memory of the corresponding block size to determine whether to return null, and observe whether the number of successful applications corresponds to the number I set. The final result is correct. So how can I solve the HardFault_Handler caused by cJSON_CreateNumber?