xref: /aosp_15_r20/external/pigweed/pw_spi_mcuxpresso/flexio_spi_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_spi_mcuxpresso/flexio_spi.h"
16 
17 #include "board.h"
18 #include "pw_bytes/array.h"
19 #include "pw_status/status.h"
20 #include "pw_unit_test/framework.h"
21 
22 namespace pw::spi {
23 namespace {
24 
25 FLEXIO_SPI_Type flexio_spi_config = {
26     .flexioBase = reinterpret_cast<FLEXIO_Type*>(FLEXIO0),
27     .SDOPinIndex = 13,
28     .SDIPinIndex = 14,
29     .SCKPinIndex = 15,
30     .CSnPinIndex = 12,
31     .shifterIndex = {0, 2},
32     .timerIndex = {0, 1}};
33 constexpr uint32_t baud_rate_bps = 500000;
34 constexpr Config configuration{.polarity = ClockPolarity::kActiveLow,
35                                .phase = ClockPhase::kFallingEdge,
36                                .bits_per_word = BitsPerWord(8),
37                                .bit_order = BitOrder::kMsbFirst};
38 
TEST(Configure,ConfigurationSuccess)39 TEST(Configure, ConfigurationSuccess) {
40   McuxpressoFlexIoInitiator spi(
41       flexio_spi_config, CLOCK_GetFlexioClkFreq(), baud_rate_bps);
42   auto status = spi.Configure(configuration);
43 
44   EXPECT_EQ(status, OkStatus());
45 }
46 
TEST(Configure,RepeatedConfigurationSuccess)47 TEST(Configure, RepeatedConfigurationSuccess) {
48   McuxpressoFlexIoInitiator spi(
49       flexio_spi_config, CLOCK_GetFlexioClkFreq(), baud_rate_bps);
50   auto status = spi.Configure(configuration);
51   EXPECT_EQ(status, OkStatus());
52 
53   status = spi.Configure(configuration);
54   EXPECT_EQ(status, OkStatus());
55 }
56 
TEST(ReadWrite,PollingWriteSuccess)57 TEST(ReadWrite, PollingWriteSuccess) {
58   const auto blocking = true;
59   constexpr auto source = bytes::Array<0x01, 0x02, 0x03, 0x04, 0x05>();
60   static auto destination = bytes::Array<0xff, 0xff, 0xff, 0xff, 0xff>();
61 
62   McuxpressoFlexIoInitiator spi(
63       flexio_spi_config, CLOCK_GetFlexioClkFreq(), baud_rate_bps, blocking);
64   auto status = spi.Configure(configuration);
65   ASSERT_EQ(status, OkStatus());
66 
67   status = spi.WriteRead(source, destination);
68   EXPECT_EQ(status, OkStatus());
69 }
70 
TEST(ReadWrite,IRQWriteSuccess)71 TEST(ReadWrite, IRQWriteSuccess) {
72   const auto blocking = false;
73   constexpr auto source = bytes::Array<0x01, 0x02, 0x03, 0x04, 0x05>();
74   static auto destination = bytes::Array<0xff, 0xff, 0xff, 0xff, 0xff>();
75   McuxpressoFlexIoInitiator spi(
76       flexio_spi_config, CLOCK_GetFlexioClkFreq(), baud_rate_bps, blocking);
77   auto status = spi.Configure(configuration);
78   ASSERT_EQ(status, OkStatus());
79 
80   status = spi.WriteRead(source, destination);
81   EXPECT_EQ(status, OkStatus());
82 }
83 
TEST(ReadWrite,WriteOnlySuccess)84 TEST(ReadWrite, WriteOnlySuccess) {
85   const auto blocking = false;
86   constexpr auto source = bytes::Array<0x01, 0x02, 0x03, 0x04, 0x05>();
87   McuxpressoFlexIoInitiator spi(
88       flexio_spi_config, CLOCK_GetFlexioClkFreq(), baud_rate_bps, blocking);
89   auto status = spi.Configure(configuration);
90   ASSERT_EQ(status, OkStatus());
91 
92   status = spi.WriteRead(source, {});
93   EXPECT_EQ(status, OkStatus());
94 }
95 
96 }  // namespace
97 }  // namespace pw::spi
98