working on init function

pull/2/head
polymurph 3 years ago
parent d28e42f2c1
commit 411403e946

@ -25,16 +25,31 @@ extern "C" {
#include <stdint.h>
#include "hardwareDescription.h"
typedef enum{
SPI_SLAVE,
SPI_MASTER
} spi_mode_t;
typedef enum{
NONINVERTED,
INVERTED
}spi_clkPol_t;
typedef enum{
CAPTURE_ON_FIRST_CLK_TRANSITION,
CAPTURE_ON_SECCOND_CLK_TRANSITION
} spi_phase_t;
typedef enum{
MSB_FIRST,
LSB_FIRST
}spi_framef_t;
typedef enum{
SPI_DOUPLEX,
SPI_SIMPLEX
}spi_comMode_t;
/*! \brief SPI cannel class
* This class cpntains the pin and spi channel number
* select.
@ -50,6 +65,19 @@ typedef uint8_t (*readReg_t) (uint8_t);
// generic code
/** TODO: setup init with all attributes
* TODO: setup auto pin set alternate function
* @brief This is the spi hardware channel class
* @param spi_hw_ch SPI hardware channel
*/
void spiInit(spiCH_t spi_ch,
spi_mode_t mode,
spi_clkPol_t clockPolarity,
spi_phase_t phase,
spi_framef_t frameFormat,
uint32_t prescaler);
/**
* \brief Set up SPI channel
* Set up a SPI channel by passig a hardware SPI channel and a chipselect pin.
@ -157,43 +185,82 @@ void spiWrite32bit(spi_ch_t *spi_ch,
uint32_t spiReadWrite32bit(spi_ch_t *spi_ch,
uint8_t bits);
//implementation
/**
* @brief This is the spi hardware channel class
* @brief SPI hardware peripheral reset
* @param spi_hw_ch SPI hardware channel
*/
void spiInit(spiCH_t spi_hw_ch);
void spiReset(spiCH_t spi_hw_ch);
/**
* @brief SPI hardware peripheral reset
* @brief Enable Bus for SPI
* @param spi_hw_ch SPI hardware channel
*/
void spiReset(spiCH_t spi_hw_ch);
void spiEnableBus(spiCH_t spi_hw_ch);
/**
* @brief Enable spi hardware channel
* @brief Enable SPI hardware channel
* @param spi_hw_ch SPI hardware channel
*/
void spiEnable(spiCH_t spi_hw_ch);
/**
* @brief set SPI clock polarity
* @brief Dissable SPI hardware channel
* @param spi_hw_ch SPI hardware channel
*/
void spiDissable(spiCH_t spi_hw_ch);
/**
* @brief Set SPI operation mode (MASTER or SLAVE)
* @param spi_hw_ch SPI hardware channel
* @param mode
*/
void spiSetMode(spiCH_t spi_hw_ch, spi_mode_t mode);
/**
* @brief Set SPI clock polarity
* @param spi_hw_ch SPI hardware channel
* @param clkPol Clock polarity
*/
void spiSetPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol);
/**
* @breif Get SPI polarity
* @param spi_hw_ch SPI hardware channel
* @return polarity
*/
spi_clkPol_t spiGetPolarity(spiCH_t spi_hw_ch);
/**
* @brief Set SPI clock phase
* @param spi_hw_ch SPI hardware channel
* @param phase
*/
void spiSetPhase(spiCH_t spi_hw_ch, spi_phase_t phase);
/**
* @brief Get SPI clock phase
* @param spi_hw_ch SPI hardware channel
* @return phase
*/
spi_phase_t spiGetPhase(spiCH_t spi_hw_ch);
/**
* @brief Set SPI frame format
* @param spi_hw_ch SPI hardware cannel
* @param spi_hw_ch SPI hardware channel
* @param framef Frame format
*/
void spiSetFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef);
/**
* @brief Get SPI frame format
* @param spi_hw_ch SPI hardware channel
* @return Frame format
*/
spi_framef_t spiGetFrameFormat(spiCH_t spi_hw_ch);
/**
* @brief Set Clock Prescaler
* This is dependent on your target device. Please enter in the correct value.
@ -203,6 +270,21 @@ void spiSetFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef);
*/
void spiSetClockPrescaler(spiCH_t spi_hw_ch,uint32_t clkDiv);
/**
* @brief Set communication Mode
* @param spi_hw_ch SPI hardware channel
* @param comMode
*/
void spiSetComMode(spiCH_t spi_hw_ch, spi_comMode_t comMode);
/**
* @brief Get Clock Prescaler
* @param spi_hw_ch SPI hardware channel
* @return prescaler
*/
uint32_t spiGetClockPrescaler(spiCH_t spi_hw_ch);
/*!
* @brief Transmits and receives on byte of data
* @param spi_hw_ch SPI hardware channel

@ -3,7 +3,7 @@
#define SPI_BASE ((SPI_TypeDef *)spiBase_Addr_List[spi_hw_ch])
// https://controllerstech.com/spi-using-registers-in-stm32/
/*
void spiInit(spiCH_t spi_hw_ch)
{
spiReset(spi_hw_ch);
@ -35,7 +35,7 @@ void spiInit(spiCH_t spi_hw_ch)
//SPI1->CR2 = 0;
SPI_BASE->CR2 = 0;
}
*/
void spiReset(spiCH_t spi_hw_ch)
{
if(spiBus_No[spi_hw_ch] == 1) {
@ -48,20 +48,62 @@ void spiReset(spiCH_t spi_hw_ch)
RCC->APB2RSTR &= ~(1 << spiBus_Rst_bitPos[spi_hw_ch]);
}
void spiEnableBus(spiCH_t spi_hw_ch)
{
if(spiBus_No[spi_hw_ch] == 1) {
RCC->APB1ENR |= (1 << spiBus_En_bitPos[spi_hw_ch]);
return;
}
RCC->APB2ENR |= (1 << spiBus_En_bitPos[spi_hw_ch]);
}
void spiEnable(spiCH_t spi_hw_ch)
{
//TODO void spi_enable(spiCH_t spi_hw_ch);
SPI_BASE->CR1 |= SPI_CR1_SPE;
}
void spiDissable(spiCH_t spi_hw_ch)
{
SPI_BASE->CR1 &= ~SPI_CR1_SPE;
}
void spiSetMode(spiCH_t spi_hw_ch, spi_mode_t mode)
{
SPI_BASE->CR1 &= ~(mode << SPI_CR1_MSTR_Pos);
SPI_BASE->CR1 |= (mode << SPI_CR1_MSTR_Pos);
}
void spiSetPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol)
{
//TODO void spi_setPolarity(spi_clkPol_t clkPol);
// reset
SPI_BASE->CR1 &= ~SPI_CR1_CPOL;
// set
SPI_BASE->CR1 |= (clkPol << SPI_CR1_CPOL_Pos);
}
void spiSetFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef)
{
// reset
SPI_BASE->CR1 &= ~SPI_CR1_LSBFIRST;
// set
SPI_BASE->CR1 |= (framef << SPI_CR1_LSBFIRST_Pos);
}
void spiSetClockPrescaler(spiCH_t spi_hw_ch, uint32_t clkDiv)
{
// reset
SPI_BASE->CR1 &= ~SPI_CR1_BR;
// set
SPI_BASE->CR1 |= clkDiv & SPI_CR1_BR;
}
void spiSet_setFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef)
void spiSetComMode(spiCH_t spi_hw_ch, spi_comMode_t comMode)
{
// TODO void spi_set_setFrameFormat(spi_framef_t framef);
// reset
SPI_BASE->CR1 &= ~SPI_CR1_RXONLY;
// set
SPI_BASE->CR1 |= (comMode << SPI_CR1_RXONLY_Pos);
}
uint8_t spiTrx(spiCH_t spi_hw_ch, uint8_t tx_data)

@ -2,6 +2,32 @@
// generic implementation of spi channel class
void spiInitMaster(spiCH_t spi_ch,
spi_mode_t mode,
spi_clkPol_t clockPolarity,
spi_phase_t phase,
spi_framef_t frameFormat,
uint32_t prescaler)
{
spiReset(spi_ch);
spiEnableBus(spi_ch);
spiSetPolarity(spi_ch, clockPolarity);
spiSetMode(spi_ch, mode);
spiSetPhase(spi_ch, phase);
spiSetFrameFormat(spi_ch, frameFormat);
spiSetClockPrescaler(spi_ch, prescaler);
spiDissable(spi_ch);
}
void spiSetupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipselectPin)
{
ch->pin = chipselectPin;

@ -7,6 +7,21 @@
#include "spi.h"
void max7219_test_display(spi_ch_t* spi_ch)
{
// set mode to display test
spiWrite16bit(spi_ch, 0xFFFF);
delayMs(10000);
// set mode to shutdown
spiWrite16bit(spi_ch, 0x0C00);
}
void max7219_setIntensity(spi_ch_t* spi_ch, uint8_t intensity)
{
spiWrite16bit(spi_ch, 0x0A00 | intensity);
}
int main(int argc, char *argv[])
{
uint8_t i = 0;
@ -35,15 +50,14 @@ int main(int argc, char *argv[])
print_Usart(usart2, "HEllooo to our KED project\n\r");
//blinks 10 times to indicate the sicsessfull init if the device
for(i = 0 ; i < 10 ; i++) {
for(i = 0 ; i < 5 ; i++) {
delayMs(100);
pinToggle(pinB3);
delayMs(100);
}
pinWrite(pinB3,0);
#if 1
pinInit(pinA5);
pinInit(pinA6);
pinInit(pinA7);
@ -61,29 +75,39 @@ int main(int argc, char *argv[])
pinSetAlternate(pinA6, 0); // SPI1_MISO
pinSetAlternate(pinA7, 0); // SPI1_MOSI
spiInit(SPI_CH_1);
spiInitMaster(SPI_CH_1,
SPI_MASTER,
NONINVERTED,
CAPTURE_ON_SECCOND_CLK_TRANSITION,
MSB_FIRST,
0b111);
spiSetupCH(&spi_test_channel, SPI_CH_1, pinA4);
spi_test_channel.format = MSB_FIRST;
spiEnable(SPI_CH_1);
for(i = 0 ; i < 20 ; i++) {
/*
* MAX7219 hoockup for this example
*
* Signal pin nucleo
* --------------------------
* MOSI pinA7 A6
* MISO pinA6 A5
* CLK pinA5 A4
* CS pinA4 A3
*/
#endif
for(i = 0 ; i < 10 ; i++) {
delayMs(50);
pinToggle(pinB3);
delayMs(50);
}
for(i = 100; i > 0; i--) {
//spiWriteReg(&spi_test_channel,0xAE,0xAE);
spiTrx(SPI_CH_1, 0xAE);
}
spiWrite16bit(&spi_test_channel, 0x00F0);
max7219_test_display(&spi_test_channel);
delayMs(10000);
for(i = 0 ; i < 100 ; i++) {
pinWrite(pinB3, 1);
//pinWrite(pinA4,1);

Loading…
Cancel
Save