diff options
| author | Alexandros Liarokapis <[email protected]> | 2024-10-01 17:59:04 +0300 |
|---|---|---|
| committer | Alexandros Liarokapis <[email protected]> | 2024-10-15 12:29:12 +0300 |
| commit | f0d2ebdc7ead41307155b083790b8450ca2b7eac (patch) | |
| tree | 3f976a73e34f81818126476b2dc896c750000a6b /embassy-stm32/src/dma/ringbuffer/tests | |
| parent | c991ddb76662cc7da5e847f33c1a446f17822887 (diff) | |
stm32: fix ringbugger overrun errors due to bad dma wrap-around behavior
Diffstat (limited to 'embassy-stm32/src/dma/ringbuffer/tests')
| -rw-r--r-- | embassy-stm32/src/dma/ringbuffer/tests/mod.rs | 83 |
1 files changed, 4 insertions, 79 deletions
diff --git a/embassy-stm32/src/dma/ringbuffer/tests/mod.rs b/embassy-stm32/src/dma/ringbuffer/tests/mod.rs index 1800eae69..6fabedb83 100644 --- a/embassy-stm32/src/dma/ringbuffer/tests/mod.rs +++ b/embassy-stm32/src/dma/ringbuffer/tests/mod.rs | |||
| @@ -2,13 +2,14 @@ use std::{cell, vec}; | |||
| 2 | 2 | ||
| 3 | use super::*; | 3 | use super::*; |
| 4 | 4 | ||
| 5 | #[allow(dead_code)] | 5 | #[allow(unused)] |
| 6 | #[derive(PartialEq, Debug)] | 6 | #[derive(PartialEq, Debug)] |
| 7 | enum TestCircularTransferRequest { | 7 | enum TestCircularTransferRequest { |
| 8 | ResetCompleteCount(usize), | 8 | ResetCompleteCount(usize), |
| 9 | PositionRequest(usize), | 9 | PositionRequest(usize), |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | #[allow(unused)] | ||
| 12 | struct TestCircularTransfer { | 13 | struct TestCircularTransfer { |
| 13 | len: usize, | 14 | len: usize, |
| 14 | requests: cell::RefCell<vec::Vec<TestCircularTransferRequest>>, | 15 | requests: cell::RefCell<vec::Vec<TestCircularTransferRequest>>, |
| @@ -39,6 +40,7 @@ impl DmaCtrl for TestCircularTransfer { | |||
| 39 | } | 40 | } |
| 40 | 41 | ||
| 41 | impl TestCircularTransfer { | 42 | impl TestCircularTransfer { |
| 43 | #[allow(unused)] | ||
| 42 | pub fn new(len: usize) -> Self { | 44 | pub fn new(len: usize) -> Self { |
| 43 | Self { | 45 | Self { |
| 44 | requests: cell::RefCell::new(vec![]), | 46 | requests: cell::RefCell::new(vec![]), |
| @@ -46,6 +48,7 @@ impl TestCircularTransfer { | |||
| 46 | } | 48 | } |
| 47 | } | 49 | } |
| 48 | 50 | ||
| 51 | #[allow(unused)] | ||
| 49 | pub fn setup(&self, mut requests: vec::Vec<TestCircularTransferRequest>) { | 52 | pub fn setup(&self, mut requests: vec::Vec<TestCircularTransferRequest>) { |
| 50 | requests.reverse(); | 53 | requests.reverse(); |
| 51 | self.requests.replace(requests); | 54 | self.requests.replace(requests); |
| @@ -55,84 +58,6 @@ impl TestCircularTransfer { | |||
| 55 | const CAP: usize = 16; | 58 | const CAP: usize = 16; |
| 56 | 59 | ||
| 57 | #[test] | 60 | #[test] |
| 58 | fn dma_index_dma_sync_syncs_position_to_last_read_if_sync_takes_place_on_same_dma_cycle() { | ||
| 59 | let mut dma = TestCircularTransfer::new(CAP); | ||
| 60 | dma.setup(vec![ | ||
| 61 | TestCircularTransferRequest::PositionRequest(4), | ||
| 62 | TestCircularTransferRequest::ResetCompleteCount(0), | ||
| 63 | TestCircularTransferRequest::PositionRequest(7), | ||
| 64 | ]); | ||
| 65 | let mut index = DmaIndex::default(); | ||
| 66 | index.dma_sync(CAP, &mut dma); | ||
| 67 | assert_eq!(index.complete_count, 0); | ||
| 68 | assert_eq!(index.pos, 7); | ||
| 69 | } | ||
| 70 | |||
| 71 | #[test] | ||
| 72 | fn dma_index_dma_sync_updates_complete_count_properly_if_sync_takes_place_on_same_dma_cycle() { | ||
| 73 | let mut dma = TestCircularTransfer::new(CAP); | ||
| 74 | dma.setup(vec![ | ||
| 75 | TestCircularTransferRequest::PositionRequest(4), | ||
| 76 | TestCircularTransferRequest::ResetCompleteCount(2), | ||
| 77 | TestCircularTransferRequest::PositionRequest(7), | ||
| 78 | ]); | ||
| 79 | let mut index = DmaIndex::default(); | ||
| 80 | index.complete_count = 1; | ||
| 81 | index.dma_sync(CAP, &mut dma); | ||
| 82 | assert_eq!(index.complete_count, 3); | ||
| 83 | assert_eq!(index.pos, 7); | ||
| 84 | } | ||
| 85 | |||
| 86 | #[test] | ||
| 87 | fn dma_index_dma_sync_syncs_to_last_position_if_reads_occur_on_different_dma_cycles() { | ||
| 88 | let mut dma = TestCircularTransfer::new(CAP); | ||
| 89 | dma.setup(vec![ | ||
| 90 | TestCircularTransferRequest::PositionRequest(10), | ||
| 91 | TestCircularTransferRequest::ResetCompleteCount(1), | ||
| 92 | TestCircularTransferRequest::PositionRequest(5), | ||
| 93 | TestCircularTransferRequest::ResetCompleteCount(0), | ||
| 94 | ]); | ||
| 95 | let mut index = DmaIndex::default(); | ||
| 96 | index.dma_sync(CAP, &mut dma); | ||
| 97 | assert_eq!(index.complete_count, 1); | ||
| 98 | assert_eq!(index.pos, 5); | ||
| 99 | } | ||
| 100 | |||
| 101 | #[test] | ||
| 102 | fn dma_index_dma_sync_detects_new_cycle_if_later_position_is_less_than_first_and_first_complete_count_occurs_on_first_cycle( | ||
| 103 | ) { | ||
| 104 | let mut dma = TestCircularTransfer::new(CAP); | ||
| 105 | dma.setup(vec![ | ||
| 106 | TestCircularTransferRequest::PositionRequest(10), | ||
| 107 | TestCircularTransferRequest::ResetCompleteCount(1), | ||
| 108 | TestCircularTransferRequest::PositionRequest(5), | ||
| 109 | TestCircularTransferRequest::ResetCompleteCount(1), | ||
| 110 | ]); | ||
| 111 | let mut index = DmaIndex::default(); | ||
| 112 | index.complete_count = 1; | ||
| 113 | index.dma_sync(CAP, &mut dma); | ||
| 114 | assert_eq!(index.complete_count, 3); | ||
| 115 | assert_eq!(index.pos, 5); | ||
| 116 | } | ||
| 117 | |||
| 118 | #[test] | ||
| 119 | fn dma_index_dma_sync_detects_new_cycle_if_later_position_is_less_than_first_and_first_complete_count_occurs_on_later_cycle( | ||
| 120 | ) { | ||
| 121 | let mut dma = TestCircularTransfer::new(CAP); | ||
| 122 | dma.setup(vec![ | ||
| 123 | TestCircularTransferRequest::PositionRequest(10), | ||
| 124 | TestCircularTransferRequest::ResetCompleteCount(2), | ||
| 125 | TestCircularTransferRequest::PositionRequest(5), | ||
| 126 | TestCircularTransferRequest::ResetCompleteCount(0), | ||
| 127 | ]); | ||
| 128 | let mut index = DmaIndex::default(); | ||
| 129 | index.complete_count = 1; | ||
| 130 | index.dma_sync(CAP, &mut dma); | ||
| 131 | assert_eq!(index.complete_count, 3); | ||
| 132 | assert_eq!(index.pos, 5); | ||
| 133 | } | ||
| 134 | |||
| 135 | #[test] | ||
| 136 | fn dma_index_as_index_returns_index_mod_cap_by_default() { | 61 | fn dma_index_as_index_returns_index_mod_cap_by_default() { |
| 137 | let index = DmaIndex::default(); | 62 | let index = DmaIndex::default(); |
| 138 | assert_eq!(index.as_index(CAP, 0), 0); | 63 | assert_eq!(index.as_index(CAP, 0), 0); |
