mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-28 11:54:30 +03:00
SPI works
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
#include <stm32f3.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "BMP280.h"
|
||||
#include "i2c.h"
|
||||
#include "spi.h"
|
||||
#include "strfunc.h"
|
||||
#include "usb_dev.h"
|
||||
@@ -29,11 +31,19 @@
|
||||
static uint8_t locBuffer[LOCBUFFSZ];
|
||||
static const char *ERR = "ERR\n", *OK = "OK\n";
|
||||
extern volatile uint32_t Tms;
|
||||
int contmeas = 0; // continuous measurements each 1s
|
||||
|
||||
const char *helpstring =
|
||||
"https://github.com/eddyem/stm32samples/tree/master/F3:F303/xxx build#" BUILD_NUMBER " @ " BUILD_DATE "\n"
|
||||
"i - [re]init SPI1\n"
|
||||
"s - send up to 32 bytes of data and read answer\n"
|
||||
"c - set/reset continuous measurements\n"
|
||||
"s - send up to 32 bytes of data over SPI and read answer\n"
|
||||
"Axi - init BME280 with address x (0/1) and interface i (I/S - I2C/SPI)\n"
|
||||
"Fx- set filter to x (0..4)\n"
|
||||
"I - [re]init I2C\n"
|
||||
"Is - scan I2C bus\n"
|
||||
"M - start measurement\n"
|
||||
"Ovx - set oversampling of v(t, h or p) to x(0..5)\n"
|
||||
"S - [re]init SPI1\n"
|
||||
"T - print current Tms\n"
|
||||
;
|
||||
|
||||
@@ -46,9 +56,9 @@ TRUE_INLINE uint16_t readNnumbers(const char *buf){
|
||||
if(D > 0xff){ USND("Each number should be uint8_t"); return 0; }
|
||||
buf = nxt;
|
||||
locBuffer[N++] = (uint8_t) D&0xff;
|
||||
USND("add byte: "); USND(uhex2str(D&0xff)); USND("\n");
|
||||
//USND("add byte: "); USND(uhex2str(D&0xff)); USND("\n");
|
||||
}
|
||||
USND("Send "); USND(u2str(N)); USND(" bytes\n");
|
||||
//USND("Send "); USND(u2str(N)); USND(" bytes\n");
|
||||
return N;
|
||||
}
|
||||
|
||||
@@ -62,23 +72,106 @@ TRUE_INLINE const char* spirdwr(const char *buf){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TRUE_INLINE const char* bmeinint(const char *buf){
|
||||
buf = omit_spaces(buf);
|
||||
char c = *buf;
|
||||
if(c != '0' && c != '1') return "Wrong address\n";
|
||||
uint8_t addr = c - '0';
|
||||
buf = omit_spaces(buf + 1);
|
||||
c = *buf;
|
||||
uint8_t isI2C = 0;
|
||||
switch(c){
|
||||
case 'i':
|
||||
case 'I':
|
||||
isI2C = 1;
|
||||
case 's':
|
||||
case 'S':
|
||||
break;
|
||||
default:
|
||||
return "Wrong interface\n";
|
||||
}
|
||||
BMP280_setup(addr, isI2C);
|
||||
if(BMP280_init()){
|
||||
if(BMP280_read_ID(&addr)){
|
||||
if(addr == BMP280_CHIP_ID) return "found BMP280\n";
|
||||
else return "found BME280\n";
|
||||
}else return "Failed read ID\n";
|
||||
}
|
||||
return ERR;
|
||||
}
|
||||
|
||||
TRUE_INLINE const char* setos(const char *buf){
|
||||
buf = omit_spaces(buf);
|
||||
uint32_t U32;
|
||||
void (*osfunc)(BMP280_Oversampling) = NULL;
|
||||
switch(*buf){
|
||||
case 'h': // h_os
|
||||
case 'H':
|
||||
osfunc = BMP280_setOSh;
|
||||
break;
|
||||
case 'p': // p_os
|
||||
case 'P':
|
||||
osfunc = BMP280_setOSp;
|
||||
break;
|
||||
case 't': // t_os
|
||||
case 'T':
|
||||
osfunc = BMP280_setOSt;
|
||||
break;
|
||||
default:
|
||||
return "Wrong OS function\n";
|
||||
}
|
||||
buf = omit_spaces(buf+1);
|
||||
if(!*buf) return "Need OS number\n";
|
||||
if(buf != getnum(buf, &U32) && U32 < BMP280_OVERSMAX){
|
||||
osfunc((BMP280_Oversampling)U32);
|
||||
return OK;
|
||||
}
|
||||
return "Wrong OS number\n";
|
||||
}
|
||||
|
||||
const char *parse_cmd(const char *buf){
|
||||
uint32_t U32;
|
||||
// "long" commands
|
||||
if(buf[1]){
|
||||
char c = *buf++;
|
||||
switch(c){
|
||||
case 's':
|
||||
return spirdwr(buf);
|
||||
case 'A':
|
||||
return bmeinint(buf);
|
||||
case 'F':
|
||||
buf = omit_spaces(buf);
|
||||
if(buf != getnum(buf, &U32) && U32 < BMP280_FILTERMAX){
|
||||
BMP280_setfilter((BMP280_Filter)U32);
|
||||
return OK;
|
||||
}else return ERR;
|
||||
case 'I':
|
||||
if(*buf == 's'){
|
||||
i2c_init_scan_mode();
|
||||
return "Start scan\n";
|
||||
}else return ERR;
|
||||
case 'O':
|
||||
return setos(buf);
|
||||
default: return buf; // echo input
|
||||
}
|
||||
}
|
||||
/* switch(*buf){
|
||||
case '':
|
||||
break;
|
||||
}*/
|
||||
// "short" commands
|
||||
switch(*buf){
|
||||
case 'i':
|
||||
case 'c':
|
||||
contmeas = !contmeas;
|
||||
if(contmeas){
|
||||
if(BMP280_start()) return OK;
|
||||
return ERR;
|
||||
}
|
||||
return OK;
|
||||
case 'I':
|
||||
i2c_setup(I2C_SPEED_400K);
|
||||
return OK;
|
||||
case 'M':
|
||||
if(!BMP280_start()) return ERR;
|
||||
else return OK;
|
||||
break;
|
||||
case 'S':
|
||||
spi_setup();
|
||||
return OK;
|
||||
case 'T':
|
||||
|
||||
Reference in New Issue
Block a user