Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 31 → Rev 32

/branches/V0.1 killagreg/Navi-Ctrl.Uv2
35,6 → 35,7
File 1,1,<.\uart0.c><uart0.c>
File 1,1,<.\uart1.c><uart1.c>
File 1,1,<.\kml.c><kml.c>
File 1,1,<.\fifo.c><fifo.c>
File 2,5,<.\ramfunc.h><ramfunc.h>
File 2,5,<.\main.h><main.h>
File 2,5,<.\menu.h><menu.h>
56,6 → 57,7
File 2,5,<.\uart1.h><uart1.h>
File 2,5,<.\kml_header.h><kml_header.h>
File 2,5,<.\kml.h><kml.h>
File 2,5,<.\fifo.h><fifo.h>
File 3,2,<.\startup912.s><startup912.s>
File 4,1,<.\libstr91x\src\91x_scu.c><91x_scu.c>
File 4,1,<.\libstr91x\src\91x_gpio.c><91x_gpio.c>
/branches/V0.1 killagreg/fifo.c
0,0 → 1,44
#include "fifo.h"
 
u8 fifo_init (fifo_t *f, u8 *buffer, const u8 size)
{
if(f == NULL) return(0);
f->buffer = buffer;
f->count = 0;
f->pread = buffer;
f->pwrite = buffer;
f->size = size;
return(1);
}
 
u8 fifo_put (fifo_t *f, const u8 data)
{
if (f->count >= f->size) return(0); // return 0 in case of FIFO overflow.
 
u8 * pwrite = f->pwrite;
 
*(pwrite++) = data; // copy data byte to buffer
if(pwrite >= f->buffer + f->size) pwrite = f->buffer; // start at the begining after reaching the end
f->pwrite = pwrite;
f->count++;
return 1;
}
 
u8 fifo_get (fifo_t *f, u8 *pdata)
{
if(!f->count) return(0);
 
u8 *pread = f->pread;
*pdata = *(pread++);
if(pread >= f->buffer + f->size) pread = f->buffer; // start at the begining after reaching the end
f->pread = pread;
f->count--;
return(1);
}
 
u8 fifo_get_wait (fifo_t *f, u8 *pdata)
{
while (!f->count);
 
return fifo_get(f, pdata);
}
/branches/V0.1 killagreg/fifo.h
0,0 → 1,45
#ifndef _FIFO_H_
#define _FIFO_H_
 
#include <stdio.h>
#include "91x_lib.h"
 
// the fifo object
typedef struct
{
u8 *buffer; // pointrer to start of the ringbuffer
u8 count; // number of characters in FIFO
u8 size; // buffer size
u8 *pread; // read pointer
u8 *pwrite; // write pointer
} fifo_t;
 
/*
The initialization of the FIFO sets the read/write pointers etc..
The FIFO uses the buffer 'buf' which byte length must 'size'.
Returns 1 on success ans 0 in case of an error.
*/
u8 fifo_init (fifo_t* f, u8* buf, const u8 size);
 
/*
Puts a byte into the FIFO. Returns 1 on success and 0 in case of FIFO overflow.
*/
u8 fifo_put (fifo_t* f, const u8 data);
 
/*
Get the next byte from the FIFO as int. Returns 0 if the FIFO is empty.
*/
u8 fifo_get (fifo_t* f, u8* pdata);
 
/*
Get the next byte out of the FIFO. If the FIFO is empty the function blocks
until the next byte is put into the FIFO.
*/
u8 fifo_get_wait (fifo_t* f, u8* pdata);
 
 
 
 
 
 
#endif /* _FIFO_H_ */
/branches/V0.1 killagreg/i2c.c
55,6 → 55,7
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdlib.h>
#include <string.h>
#include "91x_lib.h"
#include "i2c.h"
#include "uart1.h"
61,6 → 62,7
#include "timer.h"
#include "main.h"
#include "led.h"
#include "spi_slave.h"
 
 
volatile I2C_State_t I2C_State;
86,9 → 88,9
volatile u8 I2C_ReadRequest = 0;
volatile u32 I2C1_Timeout = 0;
 
volatile u8 I2C_PrimRxBuffer[10];
 
 
 
//--------------------------------------------------------------
void I2C1_Init(void)
{
222,6 → 224,7
{
u8 data;
u16 status;
static u8 crc;
// detemine I2C State
status = I2C_GetLastEvent(I2C1);
 
237,140 → 240,158
}
else
{
switch (status)
{
// the start condition was initiated on the bus
case I2C_EVENT_MASTER_MODE_SELECT: // EV5
LED_GRN_ON;
// update current bus state variable
switch(I2C_Direction)
{
case I2C_MODE_TRANSMITTER:
I2C_State = I2C_TX_PROGRESS;
break;
 
case I2C_MODE_RECEIVER:
if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0))
{
switch (status)
{
// the start condition was initiated on the bus
case I2C_EVENT_MASTER_MODE_SELECT: // EV5
LED_GRN_ON;
// update current bus state variable
switch(I2C_Direction)
{
case I2C_MODE_TRANSMITTER:
I2C_State = I2C_TX_PROGRESS;
break;
case I2C_MODE_RECEIVER:
if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0))
{
I2C_GenerateSTOP (I2C1, ENABLE);
I2C_State = I2C_IDLE;
return;
}
else
{
I2C_State = I2C_RX_PROGRESS;
}
break;
default:
I2C_GenerateSTOP (I2C1, ENABLE);
I2C_State = I2C_IDLE;
LED_GRN_OFF;
return;
}
I2C_AcknowledgeConfig (I2C1, ENABLE);
// send address/direction byte on the bus
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
break;
// the address byte was send
case I2C_EVENT_MASTER_MODE_SELECTED: // EV6
// Clear EV6 by set again the PE bit
I2C1->CR |= 0x20;
crc = 0;
switch(I2C_State)
{
case I2C_TX_PROGRESS:
// send command 1st data byte (allways the command id)
I2C_SendData(I2C1, I2C_Command);
crc += I2C_Command;
Tx_Idx = 0;
// reset timeout
I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset
break;
case I2C_RX_PROGRESS:
// if only one byte should be received
if (I2C_RxBufferSize == 1)
{ // send no acknowledge after reception of the first byte
I2C_AcknowledgeConfig (I2C1, DISABLE);
}
else
{
I2C_State = I2C_RX_PROGRESS;
}
Rx_Idx = 0;
break;
 
default:
default:
// should never happen
I2C_GenerateSTOP (I2C1, ENABLE);
I2C_State = I2C_IDLE;
LED_GRN_OFF;
return;
}
I2C_AcknowledgeConfig (I2C1, ENABLE);
// send address/direction byte on the bus
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
break;
 
// the address byte was send
case I2C_EVENT_MASTER_MODE_SELECTED: // EV6
// Clear EV6 by set again the PE bit
I2C1->CR |= 0x20;
switch(I2C_State)
{
case I2C_TX_PROGRESS:
// send command 1st data byte (allways the command id)
I2C_SendData(I2C1, I2C_Command);
Tx_Idx = 0;
// reset timeout
I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset
break;
 
case I2C_RX_PROGRESS:
// if only one byte should be received
if (I2C_RxBufferSize == 1)
{ // send no acknowledge after reception of the first byte
I2C_AcknowledgeConfig (I2C1, DISABLE);
break;
}
Rx_Idx = 0;
break;
 
default:
// should never happen
I2C_GenerateSTOP (I2C1, ENABLE);
I2C_State = I2C_IDLE;
break;
}
break;
 
// the master has transmitted a byte and slave has been acknowledged
case I2C_EVENT_MASTER_BYTE_TRANSMITTED: // EV8
// if all bytes are transmitted
if ( (Tx_Idx >= I2C_TxBufferSize) || (I2C_TxBuffer == NULL) ) // all bytes transmitted
{
if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
// the master has transmitted a byte and slave has been acknowledged
case I2C_EVENT_MASTER_BYTE_TRANSMITTED: // EV8
// if all bytes incl. crc are transmitted
if ( (Tx_Idx >= I2C_TxBufferSize + 1) || (I2C_TxBuffer == NULL) ) // all bytes transmitted
{
I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus
if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
{
I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus
}
else
{ // stop communication
I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
I2C_State = I2C_IDLE; // ready for new actions
LED_GRN_OFF;
}
}
else
{ // stop communication
I2C_GenerateSTOP (I2C1, ENABLE); // generate stop condition to free the bus
I2C_State = I2C_IDLE; // ready for new actions
LED_GRN_OFF;
else // some bytes have to be transmitted
{
if(Tx_Idx < I2C_TxBufferSize)
{
if(I2C_TxBuffer != NULL) data = I2C_TxBuffer[Tx_Idx];
else data = 0x00;
crc += data;
}
else // the last tx buffer byte was send
{
data = crc;
}
I2C_SendData(I2C1, data); // send next byte
Tx_Idx++;
}
}
else // some bytes have to be transmitted
{
if(I2C_TxBuffer != NULL)
break;
// the master has received a byte from the slave
case I2C_EVENT_MASTER_BYTE_RECEIVED: // EV7
data = I2C_ReceiveData(I2C1);
DebugOut.Analog[16]++;
DebugOut.Analog[6] = Rx_Idx;
// as long as not all bytes are collected
if (Rx_Idx < I2C_RxBufferSize)
{ // copy received byte from the data register to the rx-buffer
I2C_PrimRxBuffer[Rx_Idx] = data;
crc += data;
}
Rx_Idx++;
// if the 2nd last byte was received disable acknowledge for the last one
if ( Rx_Idx == I2C_RxBufferSize )
{
I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx++]); // send next byte
I2C_AcknowledgeConfig (I2C1, DISABLE);
}
else
{ // send a dummy byte (should never happen)
I2C_SendData(I2C1, 0x00);
// if the last byte was received
if ( Rx_Idx == (I2C_RxBufferSize + 1))
{
if(data == crc)
{
if(I2C_RxBuffer != NULL)
{
memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize);
}
}
else // crc error
{
DebugOut.Analog[15]++;
}
// generate a STOP condition on the bus
I2C_GenerateSTOP(I2C1, ENABLE);
I2C_State = I2C_IDLE;
LED_GRN_OFF;
}
}
break;
 
// the master has received a byte from the slave
case I2C_EVENT_MASTER_BYTE_RECEIVED: // EV7
data = I2C_ReceiveData(I2C1);
DebugOut.Analog[16]++;
 
// as long as there is some space in the rx-buffer
if ((I2C_RxBuffer != NULL) && (Rx_Idx < I2C_RxBufferSize))
{ // copy received byte from the data register to the rx-buffer
I2C_RxBuffer[Rx_Idx] = data;
}
Rx_Idx++;
// if the 2nd last byte was received disable acknowledge for the last one
if ( (Rx_Idx+1) >= I2C_RxBufferSize )
{
I2C_AcknowledgeConfig (I2C1, DISABLE);
}
// if the last byte was received
if ( Rx_Idx >= I2C_RxBufferSize )
{
// generate a STOP condition on the bus
I2C_GenerateSTOP(I2C1, ENABLE);
I2C_State = I2C_IDLE;
LED_GRN_OFF;
}
break;
 
default:
break;
break;
default:
break;
}
}
}
}
//----------------------------------------------------------------
void I2C1_SendCommand(u8 command)
{
// If I2C transmission is in progress
if (I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress
while (I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress
// disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during
// update of that buffer pointers and length
I2C_ITConfig(I2C1, DISABLE);
388,7 → 409,8
case I2C_CMD_WRITE_CAL:
I2C_RxBuffer = NULL;
I2C_RxBufferSize = 0;
I2C_WriteCal.CalByte = CompassCalState;
fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
DebugOut.Analog[14] = I2C_WriteCal.CalByte;
I2C_TxBuffer = (u8 *)&I2C_WriteCal;
I2C_TxBufferSize = sizeof(I2C_WriteCal);
break;
399,7 → 421,7
I2C_TxBufferSize = 0;
break;
case I2C_CMD_READ_HEADING:
DebugOut.Analog[26] = I2C_Heading.Heading;
DebugOut.Analog[26] = I2C_Heading.Heading;
I2C_RxBuffer = (u8 *)&I2C_Heading;
I2C_RxBufferSize = sizeof(I2C_Heading);
I2C_TxBuffer = (u8 *)&I2C_WriteAttitude;
420,16 → 442,3
I2C_GenerateStart(I2C1, ENABLE);
// to be continued in the I2C1_IRQHandler() above
}
/*
void I2C1_ReadAnswer(void)
{
// only if the bus state is matching
if (I2C_State == I2C_RX_PENDING)
{
// set direction to master receiver
I2C_Direction = I2C_MODE_RECEIVER;
// initiale start condition on the bus
I2C_GenerateStart(I2C1, ENABLE);
// to be continued in the I2C1_IRQHandler() above
}
} */
/branches/V0.1 killagreg/interrupt.c
54,7 → 54,6
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//#include "main.h"
#include "91x_lib.h"
#include "usb_lib.h"
#include "fat16.h"
/branches/V0.1 killagreg/main.c
77,8 → 77,6
u32 TimerDebugDataDelay;
u32 TimerKmlAddPointDelay;
 
u8 CompassCalState = 0;
 
u16 BeepTime;
 
u8 ClearMKFlags = 0;
122,7 → 120,6
//----------------------------------------------------------------------------------------------------
int main(void)
{
u8 OldCompassCalState = 0;
u8 kml_state = 0;
u8 kml_count = 0;
KML_Document_t mydocument;
200,13 → 197,12
I2C1_Init();
}
else
{
if(OldCompassCalState != CompassCalState)
{ // check for incomming compass calibration request
if(CompassCalcStateFiFo.count)
{
I2C1_SendCommand(I2C_CMD_WRITE_CAL);
OldCompassCalState = CompassCalState;
}
else
else // request current heading
{
I2C1_SendCommand(I2C_CMD_READ_HEADING);
}
/branches/V0.1 killagreg/main.h
7,7 → 7,6
#define VERSION_PATCH 7
 
extern u32 TimerCompassUpdate;
extern u8 CompassCalState;
extern u16 BeepTime;
extern u8 ClearMKFlags;
void Interrupt_Init(void);
/branches/V0.1 killagreg/spi_slave.c
58,7 → 58,6
#include <string.h>
#include "91x_lib.h"
#include "led.h"
//#include "GPSUart.h"
#include "GPS.h"
#include "uart1.h"
#include "spi_slave.h"
65,7 → 64,9
#include "i2c.h"
#include "timer.h"
#include "main.h"
#include "fifo.h"
 
 
#define SPI_RXSYNCBYTE1 0xAA
#define SPI_RXSYNCBYTE2 0x83
#define SPI_TXSYNCBYTE1 0x81
100,6 → 101,12
u8 SPI_CommandCounter = 0;
 
 
 
u8 CompassCalStateQueue[10];
fifo_t CompassCalcStateFiFo;
 
u8 CompassCalState = 0;
 
//--------------------------------------------------------------
void SSP0_IRQHandler(void)
{
247,6 → 254,8
 
SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_TxFifo | SSP_IT_RxTimeOut, ENABLE);
 
fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue));
 
SSP_Cmd(SSP0, ENABLE);
// initialize the syncbytes in the tx buffer
SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
362,8 → 371,11
break;
 
case SPI_CMD_CAL_COMPASS:
CompassCalState = FromFlightCtrl.Param.Byte[0];
DebugOut.Analog[4] = CompassCalState;
if(CompassCalState != FromFlightCtrl.Param.Byte[0])
{ // put only new CompassCalState into queue to send via I2C
CompassCalState = FromFlightCtrl.Param.Byte[0];
fifo_put(&CompassCalcStateFiFo, CompassCalState);
}
break;
 
default:
/branches/V0.1 killagreg/spi_slave.h
1,6 → 1,9
#ifndef _SPI_SLAVE_H
#define _SPI_SLAVE_H
 
#include "fifo.h"
 
 
#define SS_PIN GPIO_ReadBit(GPIO2, GPIO_Pin_7)
 
#define SPI_PROTOCOL_COMP 1
58,8 → 61,11
 
extern volatile FromFlightCtrl_t FromFlightCtrl;
extern volatile ToFlightCtrl_t ToFlightCtrl;
extern fifo_t CompassCalcStateFiFo;
 
extern void SPI0_Init(void);
extern void UpdateSPI_Buffer(void);
void SPI0_Init(void);
void UpdateSPI_Buffer(void);
 
 
 
#endif //_SPI_SLAVE_H
/branches/V0.1 killagreg/uart0.c
54,7 → 54,6
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//#include <stdio.h>
#include "91x_lib.h"
#include "uart1.h"
#include "ubx.h"
/branches/V0.1 killagreg/uart1.c
120,8 → 120,8
" ",
"SPI Error ",
"SPI Okay ",
" ",
" ", //15
"CompassCalState ",
"I2C CRC Error ", //15
"I2C_ReadByte ",
"ACC_Speed_N ",
"ACC_Speed_E ",