20 void spi_setSpeed(
spi_periph periph, uint16_t divisor);
22 void spi1_put(uint16_t data);
23 void spi2_put(uint16_t data);
24 uint16_t spi1_get(
void);
25 uint16_t spi2_get(
void);
28 static void (*spi1_tx_handler)(void);
29 static void (*spi1_rx_handler)(uint16_t);
30 static void (*spi2_tx_handler)(void);
31 static void (*spi2_rx_handler)(uint16_t);
40 rcc_periph_clock_enable(RCC_SPI1);
53 spi_enable_software_slave_management(SPI1);
54 spi_set_nss_high(SPI1);
55 spi_set_master_mode(SPI1);
62 rcc_periph_clock_enable(RCC_SPI2);
75 spi_enable_software_slave_management(SPI2);
76 spi_set_nss_high(SPI2);
77 spi_set_master_mode(SPI2);
100 nvic_disable_irq(NVIC_SPI1_IRQ);
101 spi1_tx_handler = handler;
102 spi_enable_tx_buffer_empty_interrupt(SPI1);
103 nvic_enable_irq(NVIC_SPI1_IRQ);
108 nvic_disable_irq(NVIC_SPI2_IRQ);
109 spi2_tx_handler = handler;
110 spi_enable_tx_buffer_empty_interrupt(SPI2);
111 nvic_enable_irq(NVIC_SPI2_IRQ);
132 nvic_disable_irq(NVIC_SPI1_IRQ);
133 spi1_rx_handler = handler;
134 spi_enable_rx_buffer_not_empty_interrupt(SPI1);
135 nvic_enable_irq(NVIC_SPI1_IRQ);
140 nvic_disable_irq(NVIC_SPI2_IRQ);
141 spi2_rx_handler = handler;
142 spi_enable_rx_buffer_not_empty_interrupt(SPI2);
143 nvic_enable_irq(NVIC_SPI2_IRQ);
165 spi_disable_tx_buffer_empty_interrupt(SPI1);
168 if(!(SPI_CR2(SPI1) & SPI_CR2_RXNEIE)){
170 nvic_disable_irq(NVIC_SPI1_IRQ);
176 spi_disable_tx_buffer_empty_interrupt(SPI2);
179 if(!(SPI_CR2(SPI2) & SPI_CR2_RXNEIE)){
181 nvic_disable_irq(NVIC_SPI2_IRQ);
204 spi_disable_rx_buffer_not_empty_interrupt(SPI1);
207 if(!(SPI_CR2(SPI1) & SPI_CR2_TXEIE)){
209 nvic_disable_irq(NVIC_SPI1_IRQ);
215 spi_disable_rx_buffer_not_empty_interrupt(SPI2);
217 if(!(SPI_CR2(SPI1) & SPI_CR2_TXEIE)){
219 nvic_disable_irq(NVIC_SPI2_IRQ);
238 if(SPI_SR(SPI1) & SPI_SR_TXE){
242 if(SPI_SR(SPI1) & SPI_SR_RXNE){
244 spi1_rx_handler(SPI_DR(SPI1));
250 if(SPI_SR(SPI2) & SPI_SR_TXE){
254 if(SPI_SR(SPI2) & SPI_SR_RXNE){
256 spi2_rx_handler(SPI_DR(SPI2));
291 void spi_setSpeed(
spi_periph periph, uint16_t divisor){
297 scalar = SPI_CR1_BR_FPCLK_DIV_256;
299 else if(divisor > 64){
301 scalar = SPI_CR1_BR_FPCLK_DIV_128;
303 else if(divisor > 32){
305 scalar = SPI_CR1_BR_FPCLK_DIV_64;
307 else if(divisor > 16){
309 scalar = SPI_CR1_BR_FPCLK_DIV_32;
311 else if(divisor > 8){
313 scalar = SPI_CR1_BR_FPCLK_DIV_16;
315 else if(divisor > 4){
317 scalar = SPI_CR1_BR_FPCLK_DIV_8;
319 else if(divisor > 2){
321 scalar = SPI_CR1_BR_FPCLK_DIV_4;
325 scalar = SPI_CR1_BR_FPCLK_DIV_2;
332 spi_set_baudrate_prescaler(SPI1,scalar);
337 spi_set_baudrate_prescaler(SPI2,scalar);
352 spi_set_clock_polarity_1(SPI1);
357 spi_set_clock_polarity_0(SPI1);
366 spi_set_clock_polarity_1(SPI2);
371 spi_set_clock_polarity_0(SPI2);
396 spi_set_dff_8bit(SPI1);
401 spi_set_dff_16bit(SPI1);
409 spi_set_dff_8bit(SPI2);
414 spi_set_dff_16bit(SPI2);
439 spi_send_lsb_first(SPI1);
443 spi_send_msb_first(SPI1);
451 spi_send_lsb_first(SPI2);
455 spi_send_msb_first(SPI2);
480 spi_set_clock_phase_0(SPI1);
484 spi_set_clock_phase_1(SPI1);
492 spi_set_clock_phase_0(SPI2);
496 spi_set_clock_phase_1(SPI2);
598 void spi1_put(uint16_t data){
602 while(SPI_SR(SPI1) & SPI_SR_BSY);
606 void spi2_put(uint16_t data){
610 while(SPI_SR(SPI2) & SPI_SR_BSY);
616 while(SPI_SR(SPI1) & SPI_SR_BSY);
623 while(SPI_SR(SPI2) & SPI_SR_BSY);
628 spi_csPol polarity, uint16_t address, uint16_t data){
656 spi_csPol polarity, uint16_t address, uint16_t *data, uint16_t count){
675 for(uint16_t i = 0; i < count; i++){
684 for(uint16_t i = 0; i < count; i++){
759 spi_csPol polarity, uint16_t address, uint16_t *data, uint16_t count){
781 for(uint16_t i = 0; i < count; i++){
784 data[i] = spi1_get();
793 for(uint16_t i = 0; i < count; i++){
796 data[i] = spi2_get();
gpio_port
GPIO ports available for the MCU.
mcu_error SPI_setClockPolarity(spi_periph periph, spi_clock clock)
Sets the polarity of the clock during the idle state.
The second SPI peripheral.
mcu_error SPI_setDataFrame(spi_periph periph, spi_data data)
Sets the size of the data frame for one transmission.
#define SPI_DEFAULT_SPEED
Serial clock rate.
#define SPI_DEFAULT_ENDIAN
Data frame order.
mcu_error SPI_enableTxISR(spi_periph periph, void(*handler)(void))
Enable the peripheral's transmit interrupt.
spi_speed
Clock rates for the serial clock.
spi_u16_fp_v SPI_getGetAdd(spi_periph periph)
Get the function address of the get function for the port.
void pinLow(gpio_port port, gpio_pin pin)
Set a given gpio pin on a port to logic low.
spi_v_fp_u16 SPI_getPutAdd(spi_periph periph)
Get the function address of the put function for the port.
One data frame is 8 bits.
void SPI_write(spi_periph periph, gpio_port port, gpio_pin pin, spi_csPol polarity, uint16_t address, uint16_t data)
Write a data frame to a slave device using standard protocole.
Captured on first transition.
void MCU_printError(mcu_error errorNum)
Print a given error number as a character stream.
uint8_t MCU_debugIsEnabled(void)
Checks if debug is enabled.
Setup the port for output SPI.
void pinHigh(gpio_port port, gpio_pin pin)
Set a given gpio pin on a port to logic high.
spi_clock
The idle state the clock line is left with no activity.
uint32_t CLOCK_getSpeed(void)
Get the current clock speed of the device.
Error SPI: Port doesn't exist.
mcu_error SPI_setPhase(spi_periph periph, spi_phase phase)
Sets the phase of the clock.
#define SPI_DEFAULT_PHASE
Clock capture edge relative to clock.
void(* spi_v_fp_u16)(uint16_t)
Funtion point typedef for void(*fp)(uint8_t)
spi_data
The number of data bits send in one transmission.
gpio_pin
GPIO pins available for each port.
spi_csPol
The polarity of the chip select pin.
mcu_error SPI_setSpeed(spi_periph periph, spi_speed speed)
Sets the speed of the serial clock.
mcu_error SPI_enableRxISR(spi_periph periph, void(*handler)(uint16_t))
Enable the peripheral's receive interrupt.
#define SPI_DEFAULT_DATA
Data frame size.
mcu_error SPI_setEndian(spi_periph periph, spi_endian endian)
Sets order of the transmitted data frame.
mcu_error SPI_disableTxISR(spi_periph periph)
Disable the peripheral's transmit interrupt.
spi_endian
Byte order for send data.
uint32_t CLOCK_getSpeedAlternate(void)
Get the current clock speed of the alternate bus.
Chip select pin is active high.
uint16_t SPI_read(spi_periph periph, gpio_port port, gpio_pin pin, spi_csPol polarity, uint16_t address)
Read a data frame from the slave device using standard protocole.
void SPI_repeatRead(spi_periph periph, gpio_port port, gpio_pin pin, spi_csPol polarity, uint16_t address, uint16_t *data, uint16_t count)
Read multiple data frames from the slave device using standard protocole.
mcu_error SPI_setup(spi_periph periph)
Sets up the SPI peripheral with standard setting doneted with macro defines.
Header file for stm32f103cb SPI.
uint16_t SPI_get(spi_periph periph)
Recieve a byte from the recieve buffer. (blocking)
mcu_error pinSetup(gpio_mode mode, gpio_port port, gpio_pin pin)
Setup a GPIO pin for a given function.
The first SPI peripheral.
spi_phase
The phase at which the data is captured on the clock line.
#define SPI_DEFAULT_CLOCK
Clock reset state.
Setup the port for input SPI.
mcu_error SPI_disableRxISR(spi_periph periph)
Disable the peripheral's receive interrupt.
void SPI_repeatWrite(spi_periph periph, gpio_port port, gpio_pin pin, spi_csPol polarity, uint16_t address, uint16_t *data, uint16_t count)
Write multiple data frames to a slave device using standard protocole.
spi_periph
Peripheral SPI ports available.
uint16_t(* spi_u16_fp_v)(void)
Funtion point typedef for void(*fp)(void)
mcu_error SPI_put(spi_periph periph, uint16_t data)
Send one data frame on the data bus. (blocking)
mcu_error
Error enumerators for the Debug peripheral.