mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 18:55:13 +03:00
add SPI (not tested yet) to F0_testbrd
This commit is contained in:
parent
348d3f4dca
commit
83b3e56587
@ -23,6 +23,7 @@
|
||||
#include "hardware.h"
|
||||
#include "i2c.h"
|
||||
#include "proto.h"
|
||||
#include "spi.h"
|
||||
#include "usart.h"
|
||||
#include "usb.h"
|
||||
#include "usb_lib.h"
|
||||
@ -50,6 +51,7 @@ int main(void){
|
||||
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
||||
|
||||
USB_setup();
|
||||
spi_setup();
|
||||
iwdg_setup();
|
||||
|
||||
while (1){
|
||||
|
||||
Binary file not shown.
@ -19,10 +19,15 @@
|
||||
#include "adc.h"
|
||||
#include "i2c.h"
|
||||
#include "proto.h"
|
||||
#include "spi.h"
|
||||
#include "usart.h"
|
||||
#include "usb.h"
|
||||
#include "usb_lib.h"
|
||||
|
||||
#define LOCBUFFSZ (32)
|
||||
// local buffer for I2C and SPI data to send
|
||||
static uint8_t locBuffer[LOCBUFFSZ];
|
||||
|
||||
void USB_sendstr(const char *str){
|
||||
uint16_t l = 0;
|
||||
const char *b = str;
|
||||
@ -78,6 +83,43 @@ static inline char *USARTsend(char *buf){
|
||||
return "OK";
|
||||
}
|
||||
|
||||
// read N numbers from buf, @return 0 if wrong or none
|
||||
static uint16_t readNnumbers(char *buf){
|
||||
uint32_t D;
|
||||
char *nxt;
|
||||
uint16_t N = 0;
|
||||
while((nxt = getnum(buf, &D)) && nxt != buf && N < LOCBUFFSZ){
|
||||
buf = nxt;
|
||||
locBuffer[N++] = (uint8_t) D&0xff;
|
||||
USND("add byte: "); USB_sendstr(uhex2str(D&0xff)); USND("\n");
|
||||
}
|
||||
USND("Send "); USB_sendstr(u2str(N)); USND(" bytes\n");
|
||||
return N;
|
||||
}
|
||||
|
||||
// dump memory buffer
|
||||
static void hexdump(uint8_t *arr, uint16_t len){
|
||||
char buf[52], *bptr = buf;
|
||||
for(uint16_t l = 0; l < len; ++l, ++arr){
|
||||
for(int16_t j = 1; j > -1; --j){
|
||||
register uint8_t half = (*arr >> (4*j)) & 0x0f;
|
||||
if(half < 10) *bptr++ = half + '0';
|
||||
else *bptr++ = half - 10 + 'a';
|
||||
}
|
||||
if(l % 16 == 15){
|
||||
*bptr++ = '\n';
|
||||
*bptr = 0;
|
||||
USB_sendstr(buf);
|
||||
bptr = buf;
|
||||
}else *bptr++ = ' ';
|
||||
}
|
||||
if(bptr != buf){
|
||||
*bptr++ = '\n';
|
||||
*bptr = 0;
|
||||
USB_sendstr(buf);
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t i2cinited = 0;
|
||||
static inline char *setupI2C(char *buf){
|
||||
buf = omit_spaces(buf);
|
||||
@ -88,7 +130,6 @@ static inline char *setupI2C(char *buf){
|
||||
}
|
||||
|
||||
static uint8_t I2Caddress = 0;
|
||||
static uint8_t I2Cdata[12];
|
||||
static inline char *saI2C(char *buf){
|
||||
uint32_t addr;
|
||||
if(!getnum(buf, &addr) || addr > 0x7f) return "Wrong address";
|
||||
@ -106,33 +147,26 @@ static inline void rdI2C(char *buf){
|
||||
buf = nxt;
|
||||
uint8_t reg = N;
|
||||
nxt = getnum(buf, &N);
|
||||
if(!nxt || buf == nxt || N > 12){
|
||||
if(!nxt || buf == nxt || N > LOCBUFFSZ){
|
||||
USND("Bad length\n");
|
||||
return;
|
||||
}
|
||||
if(!read_i2c_reg(I2Caddress, reg, I2Cdata, N)){
|
||||
if(!read_i2c_reg(I2Caddress, reg, locBuffer, N)){
|
||||
USND("Error reading I2C\n");
|
||||
return;
|
||||
}
|
||||
if(N == 0){ USND("OK"); return; }
|
||||
USND("Register "); USB_sendstr(uhex2str(reg)); USND(":\n");
|
||||
for(uint32_t i = 0; i < N; ++i){
|
||||
hexdump(locBuffer, N);
|
||||
/*for(uint32_t i = 0; i < N; ++i){
|
||||
if(i < 10) USND(" ");
|
||||
USB_sendstr(u2str(i)); USND(": "); USB_sendstr(uhex2str(I2Cdata[i]));
|
||||
USB_sendstr(u2str(i)); USND(": "); USB_sendstr(uhex2str(locBuffer[i]));
|
||||
USND("\n");
|
||||
}
|
||||
}*/
|
||||
}
|
||||
static inline char *wrI2C(char *buf){
|
||||
uint8_t N = 0;
|
||||
uint32_t D;
|
||||
char *nxt;
|
||||
while((nxt = getnum(buf, &D)) && nxt != buf && N < 12){
|
||||
buf = nxt;
|
||||
I2Cdata[N++] = (uint8_t) D&0xff;
|
||||
USND("add byte: "); USB_sendstr(uhex2str(D&0xff)); USND("\n");
|
||||
}
|
||||
USND("Send "); USB_sendstr(u2str(N)); USND(" bytes\n");
|
||||
if(!write_i2c(I2Caddress, I2Cdata, N)) return "Error writing I2C";
|
||||
uint16_t N = readNnumbers(buf);
|
||||
if(!write_i2c(I2Caddress, locBuffer, N)) return "Error writing I2C";
|
||||
return "OK";
|
||||
}
|
||||
|
||||
@ -144,6 +178,43 @@ static inline char *DAC_chval(char *buf){
|
||||
return "OK";
|
||||
}
|
||||
|
||||
// write locBuffer to SPI
|
||||
static inline void wrSPI(int SPIidx, char *buf){
|
||||
uint16_t N = readNnumbers(buf);
|
||||
if(N < 1){
|
||||
USND("Enter at least 1 number (max: ");
|
||||
USB_sendstr(u2str(LOCBUFFSZ)); USND(")\n");
|
||||
return;
|
||||
}
|
||||
if(SPI_transmit(SPIidx, locBuffer, N)) USND("Error: busy?\n");
|
||||
else USND("done");
|
||||
}
|
||||
static inline void rdSPI(int SPIidx){
|
||||
if(SPI_isoverflow(SPIidx)) USND("SPI buffer overflow\n");
|
||||
uint8_t len = LOCBUFFSZ;
|
||||
if(SPI_getdata(SPIidx, locBuffer, &len)){
|
||||
USND("Error getting data: busy?\n");
|
||||
return;
|
||||
}
|
||||
if(len == 0){
|
||||
USND("Nothing to read\n");
|
||||
return;
|
||||
}
|
||||
if(len > LOCBUFFSZ) USND("Can't get full message: buffer too small\n");
|
||||
USND("SPI data:\n");
|
||||
hexdump(locBuffer, len);
|
||||
}
|
||||
|
||||
static inline char *procSPI(char *buf){
|
||||
int idx = 0;
|
||||
if(*buf == 'p') idx = 1;
|
||||
buf = omit_spaces(buf + 1);
|
||||
if(*buf == 'w') wrSPI(idx, buf + 1);
|
||||
else if(*buf == 'r') rdSPI(idx);
|
||||
else return "Enter `w` and data to write, `r` - to read";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *helpstring =
|
||||
"+/-[num] - increase/decrease TIM1ch1 PWM by 1 or `num`\n"
|
||||
"1..4'+'/'-'[num] - increase/decrease TIM3chN PWM by 1 or `num`\n"
|
||||
@ -157,6 +228,10 @@ const char *helpstring =
|
||||
"Is - scan I2C bus\n"
|
||||
"L - send long string over USB\n"
|
||||
"m - monitor ADC on/off\n"
|
||||
"Pw bytes - send bytes over SPI1\n"
|
||||
"pw bytes - send bytes over SPI2\n"
|
||||
"Pr - get data from SPI1\n"
|
||||
"pr - get data from SPI2\n"
|
||||
"R - software reset\n"
|
||||
"S - send short string over USB\n"
|
||||
"Ux str - send string to USARTx (1..3)\n"
|
||||
@ -202,6 +277,10 @@ const char *parse_cmd(char *buf){
|
||||
else if(*buf == 's') i2c_init_scan_mode();
|
||||
else return "Command should be 'Ia', 'Iw', 'Ir' or 'Is'\n";
|
||||
break;
|
||||
case 'p':
|
||||
case 'P':
|
||||
return procSPI(buf);
|
||||
break;
|
||||
case 'U':
|
||||
return USARTsend(buf + 1);
|
||||
break;
|
||||
|
||||
185
F0-nolib/F0_testbrd/spi.c
Normal file
185
F0-nolib/F0_testbrd/spi.c
Normal file
@ -0,0 +1,185 @@
|
||||
/*
|
||||
* This file is part of the F0testbrd project.
|
||||
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "hardware.h"
|
||||
#include "proto.h"
|
||||
#include "spi.h"
|
||||
#include "usb.h"
|
||||
|
||||
#include <string.h> // memcpy
|
||||
|
||||
// buffers for DMA rx/tx
|
||||
static uint8_t inbuff[SPInumber][SPIBUFSZ], outbuf[SPInumber][SPIBUFSZ];
|
||||
static uint8_t overfl[SPInumber] = {0, 0};
|
||||
spiStatus SPI_status[SPInumber] = {SPI_NOTREADY, SPI_NOTREADY};
|
||||
static DMA_Channel_TypeDef * const rxDMA[SPInumber] = {DMA1_Channel2, DMA1_Channel4};
|
||||
static DMA_Channel_TypeDef * const txDMA[SPInumber] = {DMA1_Channel3, DMA1_Channel5};
|
||||
static const uint32_t txDMAirqn[SPInumber] = {DMA1_Channel2_3_IRQn, DMA1_Channel4_5_6_7_IRQn};
|
||||
static SPI_TypeDef * const SPI[SPInumber] = {SPI1, SPI2};
|
||||
|
||||
// setup SPI by data from arrays above,
|
||||
// @param SPIidx - index in arrays
|
||||
// @param master - == 0 for slave
|
||||
static void spicommonsetup(uint8_t SPIidx, uint8_t master){
|
||||
if(SPIidx >= SPInumber) return;
|
||||
// Configure DMA SPI
|
||||
/* SPI_RX DMA config */
|
||||
/* (1) Peripheral address */
|
||||
/* (2) Memory address */
|
||||
/* (3) Data size */
|
||||
/* (4) Memory increment */
|
||||
/* Peripheral to memory */
|
||||
/* 8-bit transfer */
|
||||
/* Overflow IR */
|
||||
rxDMA[SPIidx]->CPAR = (uint32_t)&(SPI[SPIidx]->DR); /* (1) */
|
||||
rxDMA[SPIidx]->CMAR = (uint32_t)inbuff[SPIidx]; /* (2) */
|
||||
rxDMA[SPIidx]->CNDTR = SPIBUFSZ; /* (3) */
|
||||
rxDMA[SPIidx]->CCR |= DMA_CCR_MINC | DMA_CCR_TEIE | DMA_CCR_EN; /* (4) */
|
||||
/* SPI_TX DMA config */
|
||||
/* (5) Peripheral address */
|
||||
/* (6) Memory address */
|
||||
/* (7) Memory increment */
|
||||
/* Memory to peripheral*/
|
||||
/* 8-bit transfer */
|
||||
/* Transfer complete IT */
|
||||
txDMA[SPIidx]->CPAR = (uint32_t)&(SPI[SPIidx]->DR); /* (5) */
|
||||
txDMA[SPIidx]->CMAR = (uint32_t)outbuf[SPIidx]; /* (6) */
|
||||
txDMA[SPIidx]->CCR |= DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_DIR; /* (7) */
|
||||
/* Configure IT */
|
||||
/* (8) Set priority */
|
||||
/* (9) Enable DMA */
|
||||
NVIC_SetPriority(txDMAirqn[SPIidx], 0);
|
||||
NVIC_EnableIRQ(txDMAirqn[SPIidx]);
|
||||
/* Configure SPI */
|
||||
/* (1) Master selection, BR: Fpclk/256 CPOL and CPHA at zero (rising first edge) */
|
||||
/* (2) TX and RX with DMA, 8-bit Rx fifo */
|
||||
/* (3) Enable SPI1 */
|
||||
SPI[SPIidx]->CR1 = master ? (SPI_CR1_MSTR | SPI_CR1_BR) : (SPI_CR1_BR); /* (1) */
|
||||
SPI[SPIidx]->CR2 = SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN | SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0; /* (2) */
|
||||
SPI[SPIidx]->CR1 |= SPI_CR1_SPE; /* (3) */
|
||||
SPI_status[SPIidx] = SPI_READY;
|
||||
SPI_prep_receive(SPIidx);
|
||||
}
|
||||
|
||||
// SPI1 (AF0): PB3 - SCK, PB4 - MISO, PB5 - MOSI; RxDMA - ch2, TxDMA - ch3
|
||||
// SPI2 (AF0): PB13 - SCK, PB14 - MISO, PB15 - MOSI; RxDMA - ch4, TxDMA - ch5
|
||||
void spi_setup(){
|
||||
// RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // uncomment in common case
|
||||
/* (1) Select AF mode on pins */
|
||||
/* (2) AF0 for SPI1 signals */
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER4 | GPIO_MODER_MODER5|
|
||||
GPIO_MODER_MODER13 | GPIO_MODER_MODER14 | GPIO_MODER_MODER15)) |
|
||||
GPIO_MODER_MODER3_AF | GPIO_MODER_MODER4_AF | GPIO_MODER_MODER5_AF |
|
||||
GPIO_MODER_MODER13_AF | GPIO_MODER_MODER14_AF | GPIO_MODER_MODER15_AF; /* (1) */
|
||||
GPIOB->AFR[0] = (GPIOB->AFR[0] & ~(GPIO_AFRL_AFRL3 | GPIO_AFRL_AFRL4 | GPIO_AFRL_AFRL5)); /* (2) */
|
||||
GPIOB->AFR[1] = (GPIOB->AFR[1] & ~(GPIO_AFRH_AFRH5 | GPIO_AFRH_AFRH6 | GPIO_AFRH_AFRH7)); /* (2) */
|
||||
// enable clocking
|
||||
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
|
||||
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
|
||||
// SPI1 - master, SPI2 - slave
|
||||
spicommonsetup(0, 1);
|
||||
spicommonsetup(1, 0);
|
||||
}
|
||||
|
||||
// SPI1 Tx (channel 3)
|
||||
void dma1_channel2_3_isr(){
|
||||
if(DMA1->ISR & DMA_ISR_TCIF3){ // transfer done
|
||||
DMA1->IFCR |= DMA_IFCR_CTCIF3;
|
||||
SPI_status[0] = SPI_READY;
|
||||
USND("SPI1 tx done\n");
|
||||
}
|
||||
if(DMA1->ISR & DMA_ISR_TEIF2){ // receiver overflow
|
||||
DMA1->IFCR |= DMA_IFCR_CTEIF2;
|
||||
overfl[0] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// SPI2 Tx (channel 5)
|
||||
void dma1_channel4_5_isr(){
|
||||
if(DMA1->ISR & DMA_ISR_TCIF5){
|
||||
DMA1->IFCR |= DMA_IFCR_CTCIF5;
|
||||
SPI_status[1] = SPI_READY;
|
||||
USND("SPI2 tx done\n");
|
||||
}
|
||||
if(DMA1->ISR & DMA_ISR_TEIF4){
|
||||
DMA1->IFCR |= DMA_IFCR_CTEIF4;
|
||||
overfl[1] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI_transmit - transmit data over SPI DMA
|
||||
* @param N - SPI number (0 - SPI1, 1 - SPI2)
|
||||
* @param buf - data to transmit
|
||||
* @param len - its length
|
||||
* @return 0 if all OK
|
||||
*/
|
||||
uint8_t SPI_transmit(uint8_t N, const uint8_t *buf, uint8_t len){
|
||||
if(!buf || !len || len > SPIBUFSZ || N >= SPInumber) return 1; // bad data format
|
||||
if(SPI_status[N] != SPI_READY) return 2; // spi not ready to transmit data
|
||||
txDMA[N]->CCR &=~ DMA_CCR_EN;
|
||||
memcpy(outbuf[N], buf, len);
|
||||
txDMA[N]->CNDTR = len;
|
||||
SPI_status[N] = SPI_BUSY;
|
||||
SPI_prep_receive(N);
|
||||
txDMA[N]->CCR |= DMA_CCR_EN;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// prepare channel N to receive data, return 0 if all OK
|
||||
uint8_t SPI_prep_receive(uint8_t N){
|
||||
if(N >= SPInumber) return 1;
|
||||
if(SPI_status[N] != SPI_READY) return 2; // still transmitting data
|
||||
overfl[N] = 0;
|
||||
rxDMA[N]->CCR &= ~DMA_CCR_EN;
|
||||
rxDMA[N]->CNDTR = SPIBUFSZ;
|
||||
rxDMA[N]->CCR |= DMA_CCR_EN;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI_getdata - get data received by DMA & reload receiver
|
||||
* @param N - number of channel (0/1)
|
||||
* @param buf - buffer for data (with length maxlen) or NULL
|
||||
* @param maxlen - (I) - amount of received bytes (or 0 if buffer is empty),
|
||||
* (O) - amount of real bytes amount in buffer (could be > maxlen if maxlen < SPIBUFSZ)
|
||||
* @return 0 if all OK or error code
|
||||
*/
|
||||
uint8_t SPI_getdata(uint8_t N, uint8_t *buf, uint8_t *maxlen){
|
||||
if(N >= SPInumber) return 1;
|
||||
if(SPI_status[N] != SPI_READY) return 2; // still transmitting data
|
||||
rxDMA[N]->CCR &= ~DMA_CCR_EN;
|
||||
overfl[N] = 0;
|
||||
uint8_t remain = rxDMA[N]->CNDTR;
|
||||
if(maxlen){
|
||||
if(buf && *maxlen) memcpy(buf, inbuff[N], *maxlen);
|
||||
*maxlen = SPIBUFSZ - remain; // bytes received
|
||||
}
|
||||
rxDMA[N]->CNDTR = SPIBUFSZ;
|
||||
rxDMA[N]->CCR |= DMA_CCR_EN;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return 1 if given channel overflowed & clear overflow flag
|
||||
// should be called BEFORE SPI_prep_receive() or SPI_getdata()
|
||||
uint8_t SPI_isoverflow(uint8_t N){
|
||||
if(N >= SPInumber) return 1;
|
||||
register uint8_t o = overfl[N];
|
||||
overfl[N] = 0;
|
||||
return o;
|
||||
}
|
||||
43
F0-nolib/F0_testbrd/spi.h
Normal file
43
F0-nolib/F0_testbrd/spi.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* This file is part of the F0testbrd project.
|
||||
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#ifndef SPI_H__
|
||||
#define SPI_H__
|
||||
|
||||
#include "stm32f0.h"
|
||||
|
||||
#define SPInumber (2)
|
||||
|
||||
#define SPIBUFSZ 4
|
||||
|
||||
typedef enum{
|
||||
SPI_NOTREADY,
|
||||
SPI_READY,
|
||||
SPI_BUSY
|
||||
} spiStatus;
|
||||
|
||||
extern spiStatus SPI_status[];
|
||||
|
||||
void spi_setup();
|
||||
uint8_t SPI_transmit(uint8_t N, const uint8_t *buf, uint8_t len);
|
||||
uint8_t SPI_prep_receive(uint8_t N);
|
||||
uint8_t SPI_getdata(uint8_t N, uint8_t *buf, uint8_t *maxlen);
|
||||
uint8_t SPI_isoverflow(uint8_t N);
|
||||
|
||||
#endif // SPI_H__
|
||||
Loading…
x
Reference in New Issue
Block a user