fixed a few spi weaknesses, like timing in spi_mst_start_packet

master
Dario Ernst 15 years ago
parent d8e0702729
commit 10530a2666

@ -2,6 +2,7 @@
#include <stdint.h>
#include <math.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "main.h"
@ -16,7 +17,6 @@
uint8_t readbuf[5];
uint8_t writebuf[5];
uint8_t writebuf2[5] = {2, 4, 6, 0x12, 5};
uint8_t pos;
@ -84,12 +84,15 @@ void spi_init(){
void spi_mst_start_packet(){
#ifdef SPI_MASTER
SPI_SSOUT &= ~(_BV(SPI_SS_PIN));
_delay_us(1e6/SPI_BAUDRATE);
#endif
}
void spi_mst_end_packet(){
#ifdef SPI_MASTER
_delay_us(1e6/SPI_BAUDRATE);
SPI_SSOUT |= _BV(SPI_SS_PIN);
_delay_us(1e6/SPI_BAUDRATE);
#endif
}
@ -125,7 +128,7 @@ void spi_mst_write_read(uint8_t len, uint8_t *data){
void spi_sla_handle_packet(){
// TODO: make slave not hangup in case of partial read
uint8_t opcode, addr, do_write;
uint8_t opcode, addr;
uint16_t data, newdata;
writebuf[0] = 0; // generate invalid replies, so no inconsistent packets
// are sent.

@ -7,7 +7,7 @@ uint8_t mst_buf[5];
uint16_t spi_master_transceive(uint8_t opcode, uint8_t addr, uint16_t value)
{
do{
// for(uint8_t j=0; j<10; ++j){
mst_buf[0] = opcode;
mst_buf[1] = addr;
mst_buf[2] = value >> 8;
@ -16,11 +16,22 @@ uint16_t spi_master_transceive(uint8_t opcode, uint8_t addr, uint16_t value)
spi_mst_start_packet();
spi_mst_write_read(5,mst_buf);
spi_mst_end_packet();
} while(mst_buf[1] != opcode || mst_buf[2] != addr); // wait for echoed opcode
//TODO: validate opcode and addr
// bytes from slave are received shifted right by one
return ((mst_buf[3] << 8) | mst_buf[4]);
for(uint8_t i=0; i<5; ++i){
mst_buf[0] = 0;
mst_buf[1] = 0;
mst_buf[2] = 0;
mst_buf[3] = 0;
mst_buf[4] = 0;
spi_mst_start_packet();
spi_mst_write_read(5,mst_buf);
spi_mst_end_packet();
// bytes from slave are received shifted right by one
if(mst_buf[1] == opcode && mst_buf[2] == addr){ // valid answer
return ((mst_buf[3] << 8) | mst_buf[4]);
}
}
// }
return 0xffff; // error-code
}

@ -1,11 +1,11 @@
#include "main.h"
#include "spi.h"
uint16_t foo;
uint8_t bar;
uint8_t foo;
uint16_t bar;
void baz() {
foo++;
}
@ -30,8 +30,8 @@ void softinit() {
int __attribute__((noreturn)) main(void) {
hardinit();
softinit();
foo= 0xfafa;
bar = 0x87;
foo = 0x87;
bar= 0xfafa;
for(;;){

@ -13,8 +13,8 @@
#define SOFTTIMERNUMS 4
#include "softtimer.h"
extern uint16_t foo;
extern uint8_t bar;
extern uint8_t foo;
extern uint16_t bar;
void baz();

@ -2,7 +2,7 @@
#include "spi_pointers.h"
#include "main.h"
uint16_t *spi_proto_globals8[] = {
uint8_t *spi_proto_globals8[] = {
&foo,
&foo,
&foo,
@ -11,7 +11,7 @@ uint16_t *spi_proto_globals8[] = {
&foo,
&foo
};
uint8_t *spi_proto_globals16[] = {
uint16_t *spi_proto_globals16[] = {
&bar,
&bar,
&bar,

@ -5,8 +5,8 @@
typedef void(*funptr_t)();
extern uint16_t *spi_proto_globals8[];
extern uint8_t *spi_proto_globals16[];
extern uint8_t *spi_proto_globals8[];
extern uint16_t *spi_proto_globals16[];
extern funptr_t spi_proto_funcs[];

Loading…
Cancel
Save