Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement storing data in a pointer location #70

Closed
wants to merge 1 commit into from

Conversation

mattico
Copy link

@mattico mattico commented Aug 13, 2020

cc #67

This allows the BBuffer to use a pointed-to memory location rather than an owned array. Some use-cases would be shared-memory IPC, MMIO, or using a specific memory region on embedded. In my case, I want to use a 64KiB block SRAM for a DMA buffer on an STM32H7. It's not enough to just place the BBuffer there because I want the state variables to live in ITCM for speed, and I want to be able to use the whole buffer for data. There isn't an option for a typenum that's 2^16 - sizeof(BBuffer) so I can't even construct one of the right size currently.

I implemented this similar to other embedded libs I've seen by adding a BBStorage trait which is implemented by either a PtrSorage or an ArrayStorage. It worked out okay, but the ergonomics of the ArrayStorage suffer due to const fns being unable to return generic types. Perhaps someone can think of a solution.

@mattico
Copy link
Author

mattico commented Aug 29, 2020

Here's an example of using this to implement a DMA transfer on STM32: https://github.com/mattico/stm32h7xx-hal/blob/df7cfd347a87ad37f9d6b7dcbd69f2a9369fbd07/examples/dma_uart.rs

@timvisee
Copy link

timvisee commented Sep 14, 2020

I did attempt to use this on shared memory between processes, but failed.

I think the problem is that only the data buffer (buf) is placed at the pointed-to memory location. The other fields such as write and read are on the stack. So this doesn't actually allow sharing the bip buffer (with state) as a whole, but just the data buffer part. To use this for communication between two processes it would require allocating the rest of the fields on the pointed-to memory block as well. Is this correct (my knowledge is lacking here)?

Edit: yes, it seems you noted that here:

I'm trying to do a similar thing, making the buffer use a specific SRAM at a specific address ... but I don't want to use any of the SRAM memory for the bookkeeping variables.

@mattico
Copy link
Author

mattico commented Sep 15, 2020

Right, that's a feature for me, wish it was more helpful for you. I suppose what you need is placement-new or a custom Box that allocates in shared memory. You should be able to just move the BBBuffer into your shared memory just fine but you'll probably have to use a raw pointer.

@mattico mattico marked this pull request as ready for review November 12, 2020 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants