Skip to content

Commit 5d1a60b

Browse files
committed
Fix small typo in bare-metal reg access api article
1 parent e1a49c6 commit 5d1a60b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

content/post/bare-metal-register-access-api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class Register {
293293
};
294294
```
295295

296-
With this piece of code we can now use this class for any randon 32-bit register. Notice that we take advantage of function pointers or closures in order to modify a register value and abstract the `read-write-modify` cycle. We don't know how the register will be modified, as that should depends on the user code, but we know for sure that the register needs to be read first and later written after all modifications are done. That is a handy functional approach to this problem that we will keep using later.
296+
With this piece of code we can now use this class for any random 32-bit register. Notice that we take advantage of function pointers or closures in order to modify a register value and abstract the `read-write-modify` cycle. We don't know how the register will be modified, as that should depends on the user code, but we know for sure that the register needs to be read first and later written after all modifications are done. That is a handy functional approach to this problem that we will keep using later.
297297

298298
However, you may ask what is the impact of using a closure as a function parameter, isn't that too costly in runtime? Well, no! Given that the function is marked `inline` the compiler should be clever enough to figure it out. In fact you can see GCC does it [here](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:1,endLineNumber:37,positionColumn:1,positionLineNumber:37,selectionStartColumn:1,selectionStartLineNumber:37,startColumn:1,startLineNumber:37),source:'%0A%23include+%3Ccstdint%3E%0A%0Aclass+Register+%7B%0A+public:%0A++Register(volatile+uint32_t*+addr)+:+m_addr(addr)+%7B%7D%0A%0A++uint32_t+read()+%7B%0A++++return+*m_addr%3B%0A++%7D%0A%0A++void+write(uint32_t+value)+%7B%0A++++*m_addr+%3D+value%3B%0A++%7D%0A%0A++void+modify(auto+mod_functor)+%7B%0A++++*m_addr+%3D+mod_functor(*m_addr)%3B%0A++%7D%0A%0A+private:%0A++volatile+uint32_t*+const+m_addr%3B%0A%7D%3B%0A%0Avoid+modify_reg()+%7B%0A++uint32_t+status_reg_mem%3B%0A++Register+status_reg+%7B%26status_reg_mem%7D%3B%0A++%0A++status_reg.modify(%5B%3D%5D(uint32_t+r)+%7B%0A++++uint32_t+w+%3D+r%3B%0A++++if+(r+%26+1)+%7B%0A++++++w+%7C%3D+2%3B%0A++++%7D%0A++++return+w%3B%0A++%7D)%3B%0A%7D%0A%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:49.99999999999999,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:arm1021,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-Os+-std%3Dgnu%2B%2B20+-mcpu%3Dcortex-m0plus',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1,tree:'1'),l:'5',n:'0',o:'ARM+gcc+10.2.1+(none)+(C%2B%2B,+Editor+%231,+Compiler+%231)',t:'0')),k:49.99999999999999,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4).
299299

0 commit comments

Comments
 (0)