Skip to content

Commit 49f716f

Browse files
committed
lib/tiam1808: split SPIPinControl() into two functions
Split SPIPinControl() into SPIPinControlGet() and SPIPinControlSet() functions. Having to pass a pointer to flags to set a value was awkward and the read/write flag parameter made it difficult to know if we were reading or writing. And any function that does two completely different things based on a boolean flag is a code smell.
1 parent 0a9819d commit 49f716f

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

lib/pbio/drv/block_device/block_device_ev3.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ static void spi_bus_init(void) {
192192
SPIReset(SOC_SPI_0_REGS);
193193
SPIOutOfReset(SOC_SPI_0_REGS);
194194
SPIModeConfigure(SOC_SPI_0_REGS, SPI_MASTER_MODE);
195-
unsigned int spipc0 = SPI_SPIPC0_SOMIFUN | SPI_SPIPC0_SIMOFUN | SPI_SPIPC0_CLKFUN | SPI_SPIPC0_SCS0FUN0 | SPI_SPIPC0_SCS0FUN3;
196-
SPIPinControl(SOC_SPI_0_REGS, 0, 0, &spipc0);
195+
SPIPinControlSet(SOC_SPI_0_REGS, 0, SPI_SPIPC0_SOMIFUN | SPI_SPIPC0_SIMOFUN | SPI_SPIPC0_CLKFUN | SPI_SPIPC0_SCS0FUN0 | SPI_SPIPC0_SCS0FUN3);
197196
SPIDefaultCSSet(SOC_SPI_0_REGS, (1 << PBDRV_EV3_SPI0_FLASH_CS) | (1 << PBDRV_EV3_SPI0_ADC_CS));
198197

199198
// SPI module data formats

lib/pbio/drv/display/display_ev3.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,8 @@ static void pbdrv_display_ev3_spi_init(void) {
429429
SPIOutOfReset(SOC_SPI_1_REGS);
430430

431431
// Mode.
432-
uint32_t spipc0 = SPI_SPIPC0_SOMIFUN | SPI_SPIPC0_SIMOFUN | SPI_SPIPC0_CLKFUN | SPI_SPIPC0_ENAFUN;
433432
SPIModeConfigure(SOC_SPI_1_REGS, SPI_MASTER_MODE);
434-
SPIPinControl(SOC_SPI_1_REGS, 0, 0, (unsigned int *)&spipc0);
433+
SPIPinControlSet(SOC_SPI_1_REGS, 0, SPI_SPIPC0_SOMIFUN | SPI_SPIPC0_SIMOFUN | SPI_SPIPC0_CLKFUN | SPI_SPIPC0_ENAFUN);
435434

436435
// Config.
437436
SPIClkConfigure(SOC_SPI_1_REGS, SOC_SYSCLK_2_FREQ, 10000000, SPI_DATA_FORMAT0);

lib/tiam1808/drivers/spi.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,37 @@ void SPIModeConfigure(unsigned int baseAdd, unsigned int flag)
110110
}
111111

112112
/**
113-
* \brief - Configures SPI Pin Control Registers.\n
113+
* \brief - Get SPI Pin Control Register value.\n
114114
*
115115
* \param - baseAdd is the Memory Address of the SPI instance used.\n
116116
* \param - idx is the Pin Control register number.It can take
117117
* any integer value between 0 and 5.\n
118118
*
119-
* \param - flag is to indicate to whether to (1)read from Pin Control
120-
* Register or to (0)write to Pin Control Register.\n
119+
* \param - val the value read
121120
*
122-
* \param - val is a value/return argument which has the value to be written
123-
* in case of writes or the value read in case of reads
121+
* \return none.\n
122+
**/
123+
void SPIPinControlGet(unsigned int baseAdd, unsigned int idx, unsigned int *val)
124+
{
125+
*val = HWREG(baseAdd + SPI_SPIPC(idx));
126+
}
127+
128+
/**
129+
* \brief - Set SPI Pin Control Register value.\n
130+
*
131+
* \param - baseAdd is the Memory Address of the SPI instance used.\n
132+
* \param - idx is the Pin Control register number.It can take
133+
* any integer value between 0 and 5.\n
134+
*
135+
* \param - val the value to be written
124136
*
125137
* \return none.\n
126138
**/
127-
void SPIPinControl(unsigned int baseAdd, unsigned int idx,
128-
unsigned int flag, unsigned int *val)
139+
void SPIPinControlSet(unsigned int baseAdd, unsigned int idx, unsigned int val)
129140
{
130-
if (0 == flag)
131-
{
132-
HWREG(baseAdd + SPI_SPIPC(idx)) = *val;
133-
}
134-
else
135-
{
136-
*val = HWREG(baseAdd + SPI_SPIPC(idx));
137-
}
141+
HWREG(baseAdd + SPI_SPIPC(idx)) = val;
138142
}
143+
139144
/**
140145
* \brief - Configures SPI CS and ENA Delay in SPIDELAY Register.\n
141146
*

lib/tiam1808/tiam1808/spi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ void SPIOutOfReset(unsigned int baseAdd);
204204

205205
void SPIModeConfigure(unsigned int baseAdd, unsigned int flag);
206206

207-
void SPIPinControl(unsigned int baseAdd, unsigned int idx,
208-
unsigned int flag, unsigned int *val);
207+
void SPIPinControlGet(unsigned int baseAdd, unsigned int idx, unsigned int *val);
208+
void SPIPinControlSet(unsigned int baseAdd, unsigned int idx, unsigned int val);
209209

210210
void SPIDelayConfigure(unsigned int baseAdd, unsigned int c2edelay,
211211
unsigned int t2edelay, unsigned int t2cdelay,

0 commit comments

Comments
 (0)