mirror of https://github.com/ARMmbed/mbed-os.git
MCUXpresso: Update SPI driver
Move the clock setup and peripheral reset to the init function Signed-off-by: Mahesh Mahadevan <mahesh.mahadevan@nxp.com>pull/10067/head
parent
1dc9a6760c
commit
9efcd955ce
|
@ -28,6 +28,7 @@
|
|||
|
||||
/* Array of SPI peripheral base address. */
|
||||
static SPI_Type *const spi_address[] = SPI_BASE_PTRS;
|
||||
static int baud_rate = 0;
|
||||
|
||||
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
|
||||
{
|
||||
|
@ -42,45 +43,6 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
obj->instance = pinmap_merge(spi_data, spi_cntl);
|
||||
MBED_ASSERT((int)obj->instance != NC);
|
||||
|
||||
// pin out the spi pins
|
||||
pinmap_pinout(mosi, PinMap_SPI_MOSI);
|
||||
pinmap_pinout(miso, PinMap_SPI_MISO);
|
||||
pinmap_pinout(sclk, PinMap_SPI_SCLK);
|
||||
if (ssel != NC) {
|
||||
pinmap_pinout(ssel, PinMap_SPI_SSEL);
|
||||
}
|
||||
}
|
||||
|
||||
void spi_free(spi_t *obj)
|
||||
{
|
||||
SPI_Deinit(spi_address[obj->instance]);
|
||||
}
|
||||
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave)
|
||||
{
|
||||
spi_master_config_t master_config;
|
||||
spi_slave_config_t slave_config;
|
||||
|
||||
/* Bits: values between 4 and 16 are valid */
|
||||
MBED_ASSERT(bits >= 4 && bits <= 16);
|
||||
obj->bits = bits;
|
||||
|
||||
if (slave) {
|
||||
/* Slave config */
|
||||
SPI_SlaveGetDefaultConfig(&slave_config);
|
||||
slave_config.dataWidth = (spi_data_width_t)(bits - 1);
|
||||
slave_config.polarity = (mode & 0x2) ? kSPI_ClockPolarityActiveLow : kSPI_ClockPolarityActiveHigh;
|
||||
slave_config.phase = (mode & 0x1) ? kSPI_ClockPhaseSecondEdge : kSPI_ClockPhaseFirstEdge;
|
||||
|
||||
SPI_SlaveInit(spi_address[obj->instance], &slave_config);
|
||||
} else {
|
||||
/* Master config */
|
||||
SPI_MasterGetDefaultConfig(&master_config);
|
||||
master_config.dataWidth = (spi_data_width_t)(bits - 1);
|
||||
master_config.polarity = (mode & 0x2) ? kSPI_ClockPolarityActiveLow : kSPI_ClockPolarityActiveHigh;
|
||||
master_config.phase = (mode & 0x1) ? kSPI_ClockPhaseSecondEdge : kSPI_ClockPhaseFirstEdge;
|
||||
master_config.direction = kSPI_MsbFirst;
|
||||
|
||||
switch (obj->instance) {
|
||||
case 0:
|
||||
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
|
||||
|
@ -126,15 +88,56 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)
|
|||
RESET_PeripheralReset(kFC9_RST_SHIFT_RSTn);
|
||||
break;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
// pin out the spi pins
|
||||
pinmap_pinout(mosi, PinMap_SPI_MOSI);
|
||||
pinmap_pinout(miso, PinMap_SPI_MISO);
|
||||
pinmap_pinout(sclk, PinMap_SPI_SCLK);
|
||||
if (ssel != NC) {
|
||||
pinmap_pinout(ssel, PinMap_SPI_SSEL);
|
||||
}
|
||||
}
|
||||
|
||||
void spi_free(spi_t *obj)
|
||||
{
|
||||
SPI_Deinit(spi_address[obj->instance]);
|
||||
}
|
||||
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave)
|
||||
{
|
||||
spi_master_config_t master_config;
|
||||
spi_slave_config_t slave_config;
|
||||
|
||||
/* Bits: values between 4 and 16 are valid */
|
||||
MBED_ASSERT(bits >= 4 && bits <= 16);
|
||||
obj->bits = bits;
|
||||
|
||||
if (slave) {
|
||||
/* Slave config */
|
||||
SPI_SlaveGetDefaultConfig(&slave_config);
|
||||
slave_config.dataWidth = (spi_data_width_t)(bits - 1);
|
||||
slave_config.polarity = (mode & 0x2) ? kSPI_ClockPolarityActiveLow : kSPI_ClockPolarityActiveHigh;
|
||||
slave_config.phase = (mode & 0x1) ? kSPI_ClockPhaseSecondEdge : kSPI_ClockPhaseFirstEdge;
|
||||
|
||||
SPI_SlaveInit(spi_address[obj->instance], &slave_config);
|
||||
} else {
|
||||
/* Master config */
|
||||
SPI_MasterGetDefaultConfig(&master_config);
|
||||
master_config.dataWidth = (spi_data_width_t)(bits - 1);
|
||||
master_config.polarity = (mode & 0x2) ? kSPI_ClockPolarityActiveLow : kSPI_ClockPolarityActiveHigh;
|
||||
master_config.phase = (mode & 0x1) ? kSPI_ClockPhaseSecondEdge : kSPI_ClockPhaseFirstEdge;
|
||||
master_config.direction = kSPI_MsbFirst;
|
||||
if (baud_rate > 0) {
|
||||
master_config.baudRate_Bps = baud_rate;
|
||||
}
|
||||
SPI_MasterInit(spi_address[obj->instance], &master_config, 12000000);
|
||||
}
|
||||
}
|
||||
|
||||
void spi_frequency(spi_t *obj, int hz)
|
||||
{
|
||||
baud_rate = hz;
|
||||
SPI_MasterSetBaud(spi_address[obj->instance], (uint32_t)hz, 12000000);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue