working example.now step by step make init function generic

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

@ -25,6 +25,10 @@ extern "C" {
#include <stdint.h>
#include "hardwareDescription.h"
// TODO: when everything worksmove this into imp.spi.c
#define SPI_BASE ((SPI_TypeDef *)spiBase_Addr_List[spi_hw_ch])
typedef enum{
SPI_SLAVE,
SPI_MASTER
@ -70,7 +74,7 @@ typedef uint8_t (*readReg_t) (uint8_t);
* @brief This is the spi hardware channel class
* @param spi_hw_ch SPI hardware channel
*/
void spiInit(spiCH_t spi_ch,
void spiInitMaster(spiCH_t spi_hw_ch,
spi_mode_t mode,
spi_clkPol_t clockPolarity,
spi_phase_t phase,
@ -284,6 +288,13 @@ void spiSetComMode(spiCH_t spi_hw_ch, spi_comMode_t comMode);
*/
uint32_t spiGetClockPrescaler(spiCH_t spi_hw_ch);
#if 0
/**
* @brief Set software slave management
* @param logic 1 = enabled, 0 = dissabled
*/
void spiSetSoftwareSlaveManagement(spiCH_t spi_hw_ch, uint8_t logic);
#endif
/*!
* @brief Transmits and receives on byte of data

@ -1,6 +1,6 @@
#include "spi.h"
#define SPI_BASE ((SPI_TypeDef *)spiBase_Addr_List[spi_hw_ch])
#include "usart.h"
//#define SPI_BASE ((SPI_TypeDef *)spiBase_Addr_List[spi_hw_ch])
// https://controllerstech.com/spi-using-registers-in-stm32/
/*
@ -41,9 +41,12 @@ void spiReset(spiCH_t spi_hw_ch)
if(spiBus_No[spi_hw_ch] == 1) {
RCC->APB1RSTR |= (1 << spiBus_Rst_bitPos[spi_hw_ch]);
RCC->APB1RSTR &= ~(1 << spiBus_Rst_bitPos[spi_hw_ch]);
print_Usart(usart2, "BUS 1\n\r");
return;
}
print_Usart(usart2, "BUS 2\n\r");
RCC->APB2RSTR |= (1 << spiBus_Rst_bitPos[spi_hw_ch]);
RCC->APB2RSTR &= ~(1 << spiBus_Rst_bitPos[spi_hw_ch]);
}
@ -51,15 +54,22 @@ void spiReset(spiCH_t spi_hw_ch)
void spiEnableBus(spiCH_t spi_hw_ch)
{
if(spiBus_No[spi_hw_ch] == 1) {
print_Usart(usart2,"BUS EN 1\n\r");
RCC->APB1ENR |= (1 << spiBus_En_bitPos[spi_hw_ch]);
return;
}
print_Usart(usart2,"BUS EN 2\n\r");
RCC->APB2ENR |= (1 << spiBus_En_bitPos[spi_hw_ch]);
}
void spiEnable(spiCH_t spi_hw_ch)
{
//SPI1->CR1 |= (1<<8) | (1<<9); // SSM=1, SSi=1 -> Software Slave Management
//SPI_BASE->CR1 |= (1 << 8) | (1 << 9);
SPI_BASE->CR1 |= SPI_CR1_SPE;
}
@ -82,6 +92,14 @@ void spiSetPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol)
SPI_BASE->CR1 |= (clkPol << SPI_CR1_CPOL_Pos);
}
void spiSetPhase(spiCH_t spi_hw_ch, spi_phase_t phase)
{
// reset
SPI_BASE->CR1 &= ~(phase << SPI_CR1_CPHA_Pos);
// set
SPI_BASE->CR1 |= (phase << SPI_CR1_CPHA_Pos);
}
void spiSetFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef)
{
// reset

@ -2,17 +2,43 @@
// generic implementation of spi channel class
void spiInitMaster(spiCH_t spi_ch,
void spiInitMaster(spiCH_t spi_hw_ch,
spi_mode_t mode,
spi_clkPol_t clockPolarity,
spi_phase_t phase,
spi_framef_t frameFormat,
uint32_t prescaler)
{
RCC->APB2ENR |= (1<<12); // Enable SPI1 CLock
//SPI1->CR1 |= (1<<0)|(1<<1); // CPOL=1, CPHA=1
SPI_BASE->CR1 |= (1<<2) | (1 << 1);
//SPI1->CR1 |= (1<<2); // Master Mode
SPI_BASE->CR1 |= (1 << 2);
//SPI1->CR1 |= (3<<3); // BR[2:0] = 011: fPCLK/16, PCLK2 = 80MHz, SPI clk = 5MHz
SPI_BASE-> CR1 |= (3<<3);
//SPI1->CR1 &= ~(1<<7); // LSBFIRST = 0, MSB first
SPI_BASE->CR1 &= ~(1<<7);
//SPI1->CR1 |= (1<<8) | (1<<9); // SSM=1, SSi=1 -> Software Slave Management
SPI_BASE->CR1 |= (1 << 8) | (1 << 9);
//SPI1->CR1 &= ~(1<<10); // RXONLY = 0, full-duplex
SPI_BASE->CR1 &= ~(1<<10);
//SPI1->CR1 &= ~(1<<11); // DFF=0, 8 bit data
SPI_BASE->CR1 &= ~(1 << 11);
//SPI1->CR2 = 0;
SPI_BASE->CR2 = 0;
/*
spiReset(spi_ch);
spiEnableBus(spi_ch);
spiEnableBus(spi_ch);
spiSetPolarity(spi_ch, clockPolarity);
@ -26,6 +52,7 @@ void spiInitMaster(spiCH_t spi_ch,
spiSetClockPrescaler(spi_ch, prescaler);
spiDissable(spi_ch);
*/
}
void spiSetupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipselectPin)

@ -31,7 +31,7 @@ int main(int argc, char *argv[])
// making array with all available timers
delayInitMs(8000000, 1000); // Clock Freq and Divider for ARM library
pinConfig(pinB3, output, pushPull, def_res, def_speed);
pinConfig(pinB3, output, pushPull, output, def_speed);
pinConfig(pinA0, input, def_stage, pullDown, def_speed);
setupInit(); // This is the sescond call of System init the assebly start code is calling it before the main.
@ -57,17 +57,17 @@ int main(int argc, char *argv[])
}
pinWrite(pinB3,0);
#if 1
pinInit(pinA5);
pinInit(pinA6);
pinInit(pinA7);
pinInit(pinA4);
pinConfig(pinA5, alternate, pushPull, none, veryFast);
pinConfig(pinA6, alternate, floating, none , veryFast);
pinConfig(pinA7, alternate, pushPull, none, veryFast);
pinConfig(pinA5, alternate, pushPull, output, veryFast);
pinConfig(pinA6, alternate, floating, input , veryFast);
pinConfig(pinA7, alternate, pushPull, output, veryFast);
//pinConfig(pinA6, alternate, floating, pullDown, veryFast);
pinConfig(pinA4, output, pushPull, none, veryFast);
pinConfig(pinA4, output, pushPull, output, veryFast);
@ -78,7 +78,7 @@ int main(int argc, char *argv[])
spiInitMaster(SPI_CH_1,
SPI_MASTER,
NONINVERTED,
CAPTURE_ON_SECCOND_CLK_TRANSITION,
CAPTURE_ON_FIRST_CLK_TRANSITION,
MSB_FIRST,
0b111);
@ -95,18 +95,19 @@ int main(int argc, char *argv[])
* CLK pinA5 A4
* CS pinA4 A3
*/
#endif
for(i = 0 ; i < 10 ; i++) {
delayMs(50);
pinToggle(pinB3);
delayMs(50);
}
max7219_test_display(&spi_test_channel);
delayMs(10000);
while(1){
spiTrx(SPI_CH_1, 0xAE);
pinToggle(pinB3);
//max7219_test_display(&spi_test_channel);
}
for(i = 0 ; i < 100 ; i++) {
pinWrite(pinB3, 1);

@ -5,7 +5,6 @@ echo $dir
tmux new -s 'git' -d 'fish'
cd $dir/ked/
tmux new -s 'compile' -d 'fish'
@ -23,4 +22,6 @@ tmux new -s 'interface' -d 'fish'
cd $dir/ked/
tmux new -s 'KED submodule' -d 'fish'
tmux a
cd $dir/
tmux new -s 'miniCom' -d 'minicom'

Loading…
Cancel
Save