[tuxdroid-svn] r1101 - in firmware/fuxusb/branches/usb_cleanup: . bootloader lib_mcu modules module
Status: Beta
Brought to you by:
ks156
From: Paul_R <c2m...@c2...> - 2008-05-05 10:48:28
|
Author: Paul_R Date: 2008-05-05 12:48:26 +0200 (Mon, 05 May 2008) New Revision: 1101 Added: firmware/fuxusb/branches/usb_cleanup/bootloader/ firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.c firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.h firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.c firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.h firmware/fuxusb/branches/usb_cleanup/bootloader/twi.h firmware/fuxusb/branches/usb_cleanup/spi/ firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.c firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.h firmware/fuxusb/branches/usb_cleanup/spi/spi_task.c firmware/fuxusb/branches/usb_cleanup/spi/spi_task.h firmware/fuxusb/branches/usb_cleanup/usb/ firmware/fuxusb/branches/usb_cleanup/usb/usb_drv.c firmware/fuxusb/branches/usb_cleanup/usb/usb_drv.h firmware/fuxusb/branches/usb_cleanup/usb/usb_enum.c firmware/fuxusb/branches/usb_cleanup/usb/usb_enum.h firmware/fuxusb/branches/usb_cleanup/usb/usb_task.c firmware/fuxusb/branches/usb_cleanup/usb/usb_task.h Removed: firmware/fuxusb/branches/usb_cleanup/bootloading.c firmware/fuxusb/branches/usb_cleanup/bootloading.h firmware/fuxusb/branches/usb_cleanup/i2c.c firmware/fuxusb/branches/usb_cleanup/i2c.h firmware/fuxusb/branches/usb_cleanup/lib_mcu/spi/ firmware/fuxusb/branches/usb_cleanup/lib_mcu/twi/ firmware/fuxusb/branches/usb_cleanup/lib_mcu/usb/ firmware/fuxusb/branches/usb_cleanup/modules/spi/ firmware/fuxusb/branches/usb_cleanup/modules/usb/ firmware/fuxusb/branches/usb_cleanup/modules/usb_enum/ Modified: firmware/fuxusb/branches/usb_cleanup/fuxusb.Uv2 firmware/fuxusb/branches/usb_cleanup/main.c firmware/fuxusb/branches/usb_cleanup/modules/fifo/fifo_spk.c firmware/fuxusb/branches/usb_cleanup/modules/fifo_stt/fifo_stt.c Log: * Reorganised the project. Added: firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.c (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,61 @@ +/* KySoH iTux agent + * + * Behavioural source code + * created on 2005/10/19 David Bourgeois + * -------------------------------------------------------- + * $Id$ + */ + +#include "bootloading.h" +#include "config.h" + +uint8_t Command_FromUSB_bootload_cmd; +uint8_t cpu_address; +uint8_t page_size; +uint8_t page_address; +uint8_t packets_per_page; +uint8_t address_idx; +uint8_t packet_idx; + +blHeader_t blHeader; /* header for bootloading */ + +/* + * Initialize i2c interface + */ +void bl_init(void) +{ + /* Set TWI bitrate */ + i2cSetBitrate(); + /* Initialize TWI interface */ + i2cInit(); + /* Connect the bootloader pointer to the I2C buffer */ + blHeader.blData = &i2cSendData[2]; +} + +/* + * Sends the data stored in i2cSendData array. Only the + * 'dataLength' first bytes are sent. + * + * Prepare the i2c communication and store the page address in the + * i2c buffer. Status can be followed with i2cFlags.i2c_busy which + * is set when the communication is in progress and cleared when + * done. i2cFlags.s_val is set when the bootload has succeded. + */ +void bl_senddata(blHeader_t blHeader, uint8_t dataLength) +{ + i2cFlags.s_val = 0; + i2cDeviceAddrRW = blHeader.slave_address; + i2cSendData[0] = blHeader.page_address >> 8; /* the first 2 bytes of the i2c frame are the page address */ + i2cSendData[1] = blHeader.page_address; + i2cSendDataLength = dataLength + 2; +#ifdef BOOTLOAD_DEBUG + { + unsigned char i; + printf("I2C: Page 0x%BX%BX:\n", i2cSendData[0], i2cSendData[1]); + for (i=2; i<i2cSendDataLength; i++) + printf("%BX", i2cSendData[i]); + printf("\n"); + } +#endif + i2cMasterStart(); +} Property changes on: firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.c ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.h =================================================================== --- firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.h (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.h 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006, C2ME S.A. + * All rights reserved. + * + * $Id:$ + */ + +#ifndef _BOOTLOADING_H_ +#define _BOOTLOADING_H_ + +#include "lib_c/stdint.h" +#include "bootloader\i2c.h" +#include "config.h" + +/** USB bootloader commands */ +#define BOOT_INIT 1 +#define BOOT_FILLPAGE 2 +#define BOOT_EXIT 3 + +#define BOOTLOADER_CMD (uint8_t)0xF0 /* Bootloader status command */ + +/** + * \brief Bootloading header structure which holds all information to initiate + * a bootloading communication + */ +typedef struct blHeader_s { + /* 7 bits slave address aligned left + rw bit */ + uint8_t slave_address; + /* 16 bits address of the first byte of the page to write */ + uint16_t page_address; + /* page length in multiple of 64 bytes */ + uint8_t page_length; + /* pointer to the bootloader data */ + uint8_t xdata *blData; +} blHeader_t; + +extern blHeader_t blHeader; + +/* + * Extern declarations + */ + +void bl_init(void); + +void bl_senddata(blHeader_t blHeader, uint8_t dataLength); + +extern uint8_t Command_FromUSB_bootload_cmd; +extern uint8_t cpu_address; +extern uint8_t page_size; +extern uint8_t page_address; +extern uint8_t packets_per_page; +extern uint8_t address_idx; +extern uint8_t packet_idx; + +#endif /* _BOOTLOADING_H_ */ Property changes on: firmware/fuxusb/branches/usb_cleanup/bootloader/bootloading.h ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.c (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,301 @@ +#include "bootloader\i2c.h" + +#define I2C_DEBUG 0 + +/* + * DEBUG + */ +//#define __debug__ + +/* I2C status and address variables */ +uint8_t i2cDeviceAddrRW; +volatile I2C_FLAGS i2cFlags; +/* send/transmit buffer (outgoing data) */ +uint8_t xdata i2cSendData[I2C_SEND_DATA_BUFFER_SIZE]; +uint8_t *i2cDataToSend; +uint8_t i2cSendDataIndex; +uint8_t i2cSendDataLength; +/* receive buffer (incoming data) */ +uint8_t xdata i2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE]; +uint8_t i2cReceiveDataIndex; +uint8_t i2cReceiveDataLength; + +/* function pointer to i2c receive routine */ +/* I2cSlaveReceive is called when this processor is addressed as a slave for + * writing */ +static void (*i2cSlaveReceive)(uint8_t receiveDataLength, uint8_t* recieveData); +/* I2cSlaveTransmit is called when this processor is addressed as a slave for + * reading */ +static uint8_t (*i2cSlaveTransmit)(uint8_t transmitDataLengthMax, uint8_t* transmitData); + +/* functions */ + +/* + * Set the TWI transaction bitrate + */ +void i2cSetBitrate(void) +{ + /* Setting SSCON based on TWI_SCAL defined in i2c.h */ + SSCON = TWI_SCAL_VALUE; /* twi intialisation */ +} + +/* + * This function initializes the TWI interface for i2c communication. You + * should use the definitions in i2c.h to select the mode you want. + * + * Note that you should set the i2cSlaveReceive and i2cSlaveTransmit handlers + * after this initialization otherwise they'll be cleared. + */ +void i2cInit(void) +{ + /* Set up twi */ + SSCON = TWI_SSIE | TWI_SCAL_VALUE; /* enable TWI */ +#ifdef TWI_INT_ENABLED + Enable_twi_interrupt(); +#endif + +#ifdef TWI_SLA_ENABLED /* If Slave mode: */ + /* Set local device address (used in slave mode only) */ + SSADR = I2C_SLA_ADD; + + /* Enabling Slave mode */ + TWI_SET_AA(); /* enable TWI ACK */ + i2cSlaveReceive = 0; /* XXX do we need those 2 lines? clear SlaveReceive and SlaveTransmit handler to null */ + i2cSlaveTransmit = 0; +#endif + +#ifdef TWI_M_ENABLED +#endif +} + +/* Set the user function which handles receiving (incoming) data as a slave */ +void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(uint8_t receiveDataLength, uint8_t* recieveData)) +{ + i2cSlaveReceive = i2cSlaveRx_func; +} + +/* Set the user function which handles transmitting (outgoing) data as a slave */ +void i2cSetSlaveTransmitHandler(uint8_t (*i2cSlaveTx_func)(uint8_t transmitDataLengthMax, uint8_t* transmitData)) +{ + i2cSlaveTransmit = i2cSlaveTx_func; +} + +void i2cSendStart(void); +/* Start a Master transmission, i2cSendDataIndex is reset automatically here */ +void i2cMasterStart(void) +{ + i2cFlags.i2c_busy = 1; + i2cSendDataIndex = 0; /* don't forget to reset the index */ + i2cSendStart(); +} + +void i2cSendStart(void) +{ + TWI_SET_START(); +} + +void i2cSendStop(void) +{ + TWI_SET_STOP(); + i2cFlags.i2c_busy = 0; +} + +void i2cSendByte(uint8_t _data) +{ + SSDAT = _data; +} + +void i2cAckReceiveByte(void) +{ + TWI_SET_AA(); +} + +void i2cNackReceiveByte(void) +{ + TWI_CLEAR_AA(); +} + +#define TW_DATA SSDAT +uint8_t i2cGetReceivedByte(void) +{ + return SSDAT; +} + +uint8_t i2cGetStatus(void) +{ + return TW_STATUS; +} + +#ifdef __debug__ +uint8_t i2cStatus[20]; /* debug only */ +uint8_t i2cStatusIdx = 0; +#endif + +/* TWI interrupt service routine */ +#ifdef TWI_INT_ENABLED +void it_TWI(void) interrupt IRQ_TWI using 1 +{ +#ifdef __debug__ + i2cStatus[i2cStatusIdx++] = TW_STATUS; + if (i2cStatusIdx == 20) i2cStatusIdx = 0; +#endif + + + switch (TW_STATUS) + { + /* * * Master General * * */ + case TW_START: // 0x08: Sent start condition + case TW_REP_START: // 0x10: Sent repeated start condition +#if I2C_DEBUG +#endif + // send device address + TWI_CLEAR_START(); /* clear start condition */ + i2cSendByte(i2cDeviceAddrRW); + break; + + + /* * * Master Transmitter & Receiver status codes * * */ + + case TW_MT_SLA_ACK: // 0x18: Slave address acknowledged + case TW_MT_DATA_ACK: // 0x28: Data acknowledged +#if I2C_DEBUG +#endif + if(i2cSendDataIndex < i2cSendDataLength) + { + + i2cSendByte( i2cSendData[i2cSendDataIndex++] ); /* send data */ + } + else + { + i2cSendStop(); /* End of data stream */ + i2cFlags.s_val = 1; + } + break; + case TW_MR_DATA_NACK: // 0x58: Data received, NACK reply issued +#if I2C_DEBUG +#endif + i2cReceiveData[i2cReceiveDataIndex++] = TW_DATA; // store final received data byte + /* no break, continue to transmit STOP condition */ + case TW_MR_SLA_NACK: // 0x48: Slave address not acknowledged + case TW_MT_SLA_NACK: // 0x20: Slave address not acknowledged + case TW_MT_DATA_NACK: // 0x30: Data not acknowledged +#if I2C_DEBUG +#endif + i2cFlags.mt_nack = 1; + i2cSendStop(); + break; + case TW_MT_ARB_LOST: // 0x38: Bus arbitration lost + //case TW_MR_ARB_LOST: // 0x38: Bus arbitration lost +#if I2C_DEBUG +#endif + i2cInit(); + break; + case TW_MR_DATA_ACK: // 0x50: Data acknowledged +#if I2C_DEBUG +#endif + // store received data byte + i2cReceiveData[i2cReceiveDataIndex++] = TW_DATA; + // fall-through to see if more bytes will be received + case TW_MR_SLA_ACK: // 0x40: Slave address acknowledged +#if I2C_DEBUG +#endif + if(i2cReceiveDataIndex < (i2cReceiveDataLength-1)) + i2cAckReceiveByte(); /* receive more bytes */ + else + i2cNackReceiveByte(); /* receive the last byte */ + break; + + + /* * * Slave Receiver status codes * * */ + + case TW_SR_SLA_ACK: // 0x60: own SLA+W has been received, ACK has been returned + case TW_SR_ARB_LOST_SLA_ACK: // 0x68: own SLA+W has been received, ACK has been returned + case TW_SR_GCALL_ACK: // 0x70: GCA+W has been received, ACK has been returned + case TW_SR_ARB_LOST_GCALL_ACK: // 0x78: GCA+W has been received, ACK has been returned +#if I2C_DEBUG +#endif + /* we are being addressed as slave for writing (data will be received from master) */ + i2cFlags.i2c_busy = 1; + i2cReceiveDataIndex = 0; + i2cFlags.s_nack = 0; /* reset nack flag here, not in user app */ + i2cAckReceiveByte(); /* accept data */ + break; + case TW_SR_DATA_ACK: // 0x80: data byte has been received, ACK has been returned + case TW_SR_GCALL_DATA_ACK: // 0x90: data byte has been received, ACK has been returned +#if I2C_DEBUG +#endif + i2cReceiveData[i2cReceiveDataIndex++] = TW_DATA; /* get received data byte */ + /* check receive buffer status */ + if(i2cReceiveDataIndex < I2C_RECEIVE_DATA_BUFFER_SIZE) + { + i2cAckReceiveByte(); /* accept more data */ + } + else + { + i2cNackReceiveByte(); /* refuse more data */ + } + break; + case TW_SR_DATA_NACK: // 0x88: data byte has been received, NACK has been returned + case TW_SR_GCALL_DATA_NACK: // 0x98: data byte has been received, NACK has been returned +#if I2C_DEBUG +#endif + //i2cReceiveData[i2cReceiveDataIndex++] = TW_DATA; /* receive last byte XXX check if this is right */ + //i2cNackReceiveByte(); //XXX bug here? // receive data byte and return NACK + //i2cAckReceiveByte(); [> I should clear the interrupt and enable acknoledge for slave mode (disabled during previous nack) <] + i2cFlags.s_nack = 1; /* XXX check if this flag is reset in all possible conditions */ + //break; + /* pass along to restart slave mode */ + case TW_SR_STOP: // 0xA0: STOP or REPEATED START has been received while addressed as slave +#if I2C_DEBUG +#endif + i2cInit(); /* enable TWI ACK */ + if(i2cSlaveReceive) i2cSlaveReceive(i2cReceiveDataIndex, i2cReceiveData); /* i2c receive is complete, call i2cSlaveReceive */ + i2cFlags.i2c_busy = 0; /* XXX check if this flag is reset in all possible conditions */ + break; + + + /* * * Slave Transmitter * * */ + + case TW_ST_SLA_ACK: // 0xA8: own SLA+R has been received, ACK has been returned + case TW_ST_ARB_LOST_SLA_ACK: // 0xB0: GCA+R has been received, ACK has been returned +#if I2C_DEBUG +#endif + // we are being addressed as slave for reading (data must be transmitted back to master) + // request data from application + if(i2cSlaveTransmit) i2cSendDataLength = i2cSlaveTransmit(I2C_SEND_DATA_BUFFER_SIZE, i2cSendData); + i2cSendDataIndex = 0; /* reset data index */ + /* fall-through to transmit first data byte */ + case TW_ST_DATA_ACK: // 0xB8: data byte has been transmitted, ACK has been received +#if I2C_DEBUG +#endif + TW_DATA = i2cSendData[i2cSendDataIndex++]; /* transmit data byte */ + if(i2cSendDataIndex < i2cSendDataLength) + i2cAckReceiveByte(); /* expect ACK to data byte */ + else + i2cNackReceiveByte(); /* expect NACK to data byte */ + break; + case TW_ST_DATA_NACK: // 0xC0: data byte has been transmitted, NACK has been received + case TW_ST_LAST_DATA: // 0xC8: last data byte transmitted, ACK received +#if I2C_DEBUG +#endif + /* all done, switch to open slave */ + i2cInit(); /* enable TWI ACK */ + break; + + + /* * * Misc * * */ + + case TW_NO_INFO: // 0xF8: No relevant state information + /* do nothing */ +#if I2C_DEBUG +#endif + break; + case TW_BUS_ERROR: // 0x00: Bus error due to illegal start or stop condition +#if I2C_DEBUG +#endif + i2cSendStop(); /* reset internal hardware and release bus */ + break; + } + TWI_CLEAR_SI(); +} +#endif Property changes on: firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.c ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.h =================================================================== --- firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.h (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.h 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2006, C2ME S.A. + * All rights reserved. + * + * $Id:$ + */ + +#ifndef _I2C_H_ +#define _I2C_H_ + +#include "lib_c/stdint.h" +#include "bootloader/twi.h" +#include "config.h" + +#ifdef __debug__ +extern uint8_t i2cStatus[20]; /* XXX debug only */ +extern uint8_t i2cStatusIdx; +#endif + +/* * * include custom configuration here * * */ + +#define I2C_SEND_DATA_BUFFER_SIZE 130 +#define I2C_RECEIVE_DATA_BUFFER_SIZE 8 + +/* + * Choose operation mode, comment/uncomment lines + * For Slave only operation, choose Master and Slave + */ +#define TWI_INT_ENABLED /* comment if you don't want twi interrupts */ +#define TWI_M_ENABLED /* comment if you don't want twi master mode */ +//#define TWI_SLA_ENABLED /* comment if you don't want twi slave mode */ + +/* SSADR: TWI (Slave) Address Register + * Bits 7..1 : TWI (Slave) Address Register + * Bit 0 : TWI General Call Recognition Enable Bit */ +#define I2C_SLA_ADD ((0X55<<1) | 0x01) /* Only necessary in slave mode */ + +/* + * Initialise TWI clock + * + * Here we set SCL frequency in Master mode by defining TWI_SCAL: + * SCL_freq = F_CPU/TWI_SCAL + * + * TWI_SCAL value should be one of: 256 224 192 160 960 120 60 + */ + +//#define TWI_SCAL 160 /* TWI_SCAL value should be: 256 224 192 160 960 120 60 */ +#define TWI_SCAL 960 /* TWI_SCAL value should be: 256 224 192 160 960 120 60 */ + +/* * * END of code customisation * * */ + +#if TWI_SCAL == 256 +#define TWI_SCAL_VALUE TWI_RATIO_256 +#elif TWI_SCAL == 224 +#define TWI_SCAL_VALUE TWI_RATIO_224 +#elif TWI_SCAL == 192 +#define TWI_SCAL_VALUE TWI_RATIO_192 +#elif TWI_SCAL == 160 +#define TWI_SCAL_VALUE TWI_RATIO_160 +#elif TWI_SCAL == 960 +#define TWI_SCAL_VALUE TWI_RATIO_960 +#elif TWI_SCAL == 120 +#define TWI_SCAL_VALUE TWI_RATIO_120 +#elif TWI_SCAL == 60 +#define TWI_SCAL_VALUE TWI_RATIO_60 +#else +#error Incorrect TWI_SCAL value, should be: 256 224 192 160 960 120 60 +#define TWI_SCAL_VALUE +#endif + +/*! defines and constants */ +#define TWCR_CMD_MASK 0x0F + +/* + * Bits that are set inside interrupt routines, and watched outside in + * the program's main loop. + */ +typedef struct +{ + uint8_t i2c_busy: 1; /* set when twi hardware is busy, cleared after an i2c stop */ + uint8_t mt_nack: 1; /* error due to a nack received by the master transmitter */ + uint8_t s_nack: 1; /* error due to a nack replied by the slave */ + uint8_t sr_end: 1; /* set at the end of a receiver slave transmission if i2cSlaveReceive has not been defined */ + uint8_t st_end: 1; /* set at the end of a transmitter slave transmission if i2cSlaveTransmit has not been defined */ + uint8_t i2c_idx: 1; /* application side - can be used for data indexes status */ + uint8_t s_val: 1; /* application side - can be used for data validation */ +} +I2C_FLAGS; +extern volatile I2C_FLAGS i2cFlags; + +/*! types */ +typedef enum +{ + I2C_IDLE = 0, I2C_BUSY = 1, + I2C_MASTER_TX = 2, I2C_MASTER_RX = 3, + I2C_SLAVE_TX = 4, I2C_SLAVE_RX = 5 +} eI2cStateType; + +/* I2C state and address variables */ +extern uint8_t i2cDeviceAddrRW; +/* send/transmit buffer (outgoing data) */ +extern uint8_t xdata i2cSendData[]; +extern uint8_t i2cSendDataIndex; +extern uint8_t i2cSendDataLength; +/* receive buffer (incoming data) */ +extern uint8_t xdata i2cReceiveData[]; +extern uint8_t i2cReceiveDataIndex; +extern uint8_t i2cReceiveDataLength; + +/* Functions */ +void i2cSetBitrate(void); +void i2cInit(void); +void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(uint8_t receiveDataLength, uint8_t* recieveData)); +void i2cSetSlaveTransmitHandler(uint8_t (*i2cSlaveTx_func)(uint8_t transmitDataLengthMax, uint8_t* transmitData)); +void i2cMasterStart(void); + +#endif + Property changes on: firmware/fuxusb/branches/usb_cleanup/bootloader/i2c.h ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/fuxusb/branches/usb_cleanup/bootloader/twi.h =================================================================== --- firmware/fuxusb/branches/usb_cleanup/bootloader/twi.h (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/bootloader/twi.h 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,229 @@ +/* $Id:$ */ + +#ifndef _LIBMCU_TWI_H_ +#define _LIBMCU_TWI_H_ 1 + +/* define register information */ +#define TWI_RATIO_256 0x00 +#define TWI_RATIO_224 0x01 +#define TWI_RATIO_192 0x02 +#define TWI_RATIO_160 0x03 +#define TWI_RATIO_960 0x80 +#define TWI_RATIO_120 0x81 +#define TWI_RATIO_60 0x82 +#define TWI_RATIO_TIMER1 0x83 + +#define TWI_CR2 0x80 +#define TWI_SSIE 0x40 +#define TWI_STA 0x20 +#define TWI_STO 0x10 +#define TWI_SI 0x08 +#define TWI_AA 0x04 +#define TWI_CR1 0x02 +#define TWI_CR0 0x01 + +#define TWI_WAIT_EVENT() while ( !(SSCON & TWI_SI) ) +#define TWI_WAIT_HW_STOP() while ( SSCON & TWI_STO ) +#define TWI_SET_START() ( SSCON |= TWI_STA ) +#define TWI_CLEAR_START() ( SSCON &= ~TWI_STA ) +#define TWI_SET_STOP() ( SSCON |= TWI_STO ) +#define TWI_SET_AA() ( SSCON |= TWI_AA ) +#define TWI_CLEAR_AA() ( SSCON &= ~TWI_AA ) +#define TWI_CLEAR_SI() ( SSCON &= ~TWI_SI ) + +/** \defgroup libmcu_twi "lib_mcu/twi/twi.h": TWI bit mask definitions + \code #include "lib_mcu/twi/twi.h" \endcode + + This header file contains bit mask definitions for use with + the ATMEL TWI interface. + */ +/** \name TWSR values + +Mnemonics: +<br>TW_MT_xxx - master transmitter +<br>TW_MR_xxx - master receiver +<br>TW_ST_xxx - slave transmitter +<br>TW_SR_xxx - slave receiver +*/ + +/*@{*/ +/* Master */ +/** \ingroup libmcu_twi + \def TW_START + start condition transmitted */ +#define TW_START 0x08 + +/** \ingroup libmcu_twi + \def TW_REP_START + repeated start condition transmitted */ +#define TW_REP_START 0x10 + +/* Master Transmitter */ +/** \ingroup libmcu_twi + \def TW_MT_SLA_ACK + SLA+W transmitted, ACK received */ +#define TW_MT_SLA_ACK 0x18 + +/** \ingroup libmcu_twi + \def TW_MT_SLA_NACK + SLA+W transmitted, NACK received */ +#define TW_MT_SLA_NACK 0x20 + +/** \ingroup libmcu_twi + \def TW_MT_DATA_ACK + data transmitted, ACK received */ +#define TW_MT_DATA_ACK 0x28 + +/** \ingroup libmcu_twi + \def TW_MT_DATA_NACK + data transmitted, NACK received */ +#define TW_MT_DATA_NACK 0x30 + +/** \ingroup libmcu_twi + \def TW_MT_ARB_LOST + arbitration lost in SLA+W or data */ +#define TW_MT_ARB_LOST 0x38 + +/* Master Receiver */ +/** \ingroup libmcu_twi + \def TW_MR_ARB_LOST + arbitration lost in SLA+R or NACK */ +#define TW_MR_ARB_LOST 0x38 + +/** \ingroup libmcu_twi + \def TW_MR_SLA_ACK + SLA+R transmitted, ACK received */ +#define TW_MR_SLA_ACK 0x40 + +/** \ingroup libmcu_twi + \def TW_MR_SLA_NACK + SLA+R transmitted, NACK received */ +#define TW_MR_SLA_NACK 0x48 + +/** \ingroup libmcu_twi + \def TW_MR_DATA_ACK + data received, ACK returned */ +#define TW_MR_DATA_ACK 0x50 + +/** \ingroup libmcu_twi + \def TW_MR_DATA_NACK + data received, NACK returned */ +#define TW_MR_DATA_NACK 0x58 + +/* Slave Transmitter */ +/** \ingroup libmcu_twi + \def TW_ST_SLA_ACK + SLA+R received, ACK returned */ +#define TW_ST_SLA_ACK 0xA8 + +/** \ingroup libmcu_twi + \def TW_ST_ARB_LOST_SLA_ACK + arbitration lost in SLA+RW, SLA+R received, ACK returned */ +#define TW_ST_ARB_LOST_SLA_ACK 0xB0 + +/** \ingroup libmcu_twi + \def TW_ST_DATA_ACK + data transmitted, ACK received */ +#define TW_ST_DATA_ACK 0xB8 + +/** \ingroup libmcu_twi + \def TW_ST_DATA_NACK + data transmitted, NACK received */ +#define TW_ST_DATA_NACK 0xC0 + +/** \ingroup libmcu_twi + \def TW_ST_LAST_DATA + last data byte transmitted, ACK received */ +#define TW_ST_LAST_DATA 0xC8 + +/* Slave Receiver */ +/** \ingroup libmcu_twi + \def TW_SR_SLA_ACK + SLA+W received, ACK returned */ +#define TW_SR_SLA_ACK 0x60 + +/** \ingroup libmcu_twi + \def TW_SR_ARB_LOST_SLA_ACK + arbitration lost in SLA+RW, SLA+W received, ACK returned */ +#define TW_SR_ARB_LOST_SLA_ACK 0x68 + +/** \ingroup libmcu_twi + \def TW_SR_GCALL_ACK + general call received, ACK returned */ +#define TW_SR_GCALL_ACK 0x70 + +/** \ingroup libmcu_twi + \def TW_SR_ARB_LOST_GCALL_ACK + arbitration lost in SLA+RW, general call received, ACK returned */ +#define TW_SR_ARB_LOST_GCALL_ACK 0x78 + +/** \ingroup libmcu_twi + \def TW_SR_DATA_ACK + data received, ACK returned */ +#define TW_SR_DATA_ACK 0x80 + +/** \ingroup libmcu_twi + \def TW_SR_DATA_NACK + data received, NACK returned */ +#define TW_SR_DATA_NACK 0x88 + +/** \ingroup libmcu_twi + \def TW_SR_GCALL_DATA_ACK + general call data received, ACK returned */ +#define TW_SR_GCALL_DATA_ACK 0x90 + +/** \ingroup libmcu_twi + \def TW_SR_GCALL_DATA_NACK + general call data received, NACK returned */ +#define TW_SR_GCALL_DATA_NACK 0x98 + +/** \ingroup libmcu_twi + \def TW_SR_STOP + stop or repeated start condition received while selected */ +#define TW_SR_STOP 0xA0 + +/* Misc */ +/** \ingroup libmcu_twi + \def TW_NO_INFO + no state information available */ +#define TW_NO_INFO 0xF8 + +/** \ingroup libmcu_twi + \def TW_BUS_ERROR + illegal start or stop condition */ +#define TW_BUS_ERROR 0x00 + + +/** + * \ingroup libmcu_twi + * \def TW_STATUS_MASK + * The lower 3 bits of TWSR are reserved on the ATmega163. + * The 2 LSB carry the prescaler bits on the newer ATmegas. + */ +#define TW_STATUS_MASK 0xF8 +/** + * \ingroup libmcu_twi + * \def TW_STATUS + * + * TWSR, masked by TW_STATUS_MASK + */ +#define TW_STATUS (SSCS) +/*@}*/ + +/** + * \name R/~W bit in SLA+R/W address field. + */ + +/*@{*/ +/** \ingroup libmcu_twi + \def TW_READ + SLA+R address */ +#define TW_READ 1 + +/** \ingroup libmcu_twi + \def TW_WRITE + SLA+W address */ +#define TW_WRITE 0 +/*@}*/ + +#endif /* _LIBMCU_TWI_H_ */ Property changes on: firmware/fuxusb/branches/usb_cleanup/bootloader/twi.h ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Deleted: firmware/fuxusb/branches/usb_cleanup/bootloading.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/bootloading.c 2008-05-05 08:34:42 UTC (rev 1100) +++ firmware/fuxusb/branches/usb_cleanup/bootloading.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -1,61 +0,0 @@ -/* KySoH iTux agent - * - * Behavioural source code - * created on 2005/10/19 David Bourgeois - * -------------------------------------------------------- - * $Id: main.c 28 2006-01-12 16:51:01Z david $ - */ - -#include "bootloading.h" -#include "config.h" - -uint8_t Command_FromUSB_bootload_cmd; -uint8_t cpu_address; -uint8_t page_size; -uint8_t page_address; -uint8_t packets_per_page; -uint8_t address_idx; -uint8_t packet_idx; - -blHeader_t blHeader; /* header for bootloading */ - -/* - * Initialize i2c interface - */ -void bl_init(void) -{ - /* Set TWI bitrate */ - i2cSetBitrate(); - /* Initialize TWI interface */ - i2cInit(); - /* Connect the bootloader pointer to the I2C buffer */ - blHeader.blData = &i2cSendData[2]; -} - -/* - * Sends the data stored in i2cSendData array. Only the - * 'dataLength' first bytes are sent. - * - * Prepare the i2c communication and store the page address in the - * i2c buffer. Status can be followed with i2cFlags.i2c_busy which - * is set when the communication is in progress and cleared when - * done. i2cFlags.s_val is set when the bootload has succeded. - */ -void bl_senddata(blHeader_t blHeader, uint8_t dataLength) -{ - i2cFlags.s_val = 0; - i2cDeviceAddrRW = blHeader.slave_address; - i2cSendData[0] = blHeader.page_address >> 8; /* the first 2 bytes of the i2c frame are the page address */ - i2cSendData[1] = blHeader.page_address; - i2cSendDataLength = dataLength + 2; -#ifdef BOOTLOAD_DEBUG - { - unsigned char i; - printf("I2C: Page 0x%BX%BX:\n", i2cSendData[0], i2cSendData[1]); - for (i=2; i<i2cSendDataLength; i++) - printf("%BX", i2cSendData[i]); - printf("\n"); - } -#endif - i2cMasterStart(); -} Deleted: firmware/fuxusb/branches/usb_cleanup/bootloading.h =================================================================== --- firmware/fuxusb/branches/usb_cleanup/bootloading.h 2008-05-05 08:34:42 UTC (rev 1100) +++ firmware/fuxusb/branches/usb_cleanup/bootloading.h 2008-05-05 10:48:26 UTC (rev 1101) @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2006, C2ME S.A. - * All rights reserved. - * - * $Id:$ - */ - -#ifndef _BOOTLOADING_H_ -#define _BOOTLOADING_H_ - -#include "lib_c/stdint.h" -#include "i2c.h" -#include "config.h" - -/** USB bootloader commands */ -#define BOOT_INIT 1 -#define BOOT_FILLPAGE 2 -#define BOOT_EXIT 3 - -#define BOOTLOADER_CMD (uint8_t)0xF0 /* Bootloader status command */ - -/** - * \brief Bootloading header structure which holds all information to initiate - * a bootloading communication - */ -typedef struct blHeader_s { - /* 7 bits slave address aligned left + rw bit */ - uint8_t slave_address; - /* 16 bits address of the first byte of the page to write */ - uint16_t page_address; - /* page length in multiple of 64 bytes */ - uint8_t page_length; - /* pointer to the bootloader data */ - uint8_t xdata *blData; -} blHeader_t; - -extern blHeader_t blHeader; - -/* - * Extern declarations - */ - -void bl_init(void); - -void bl_senddata(blHeader_t blHeader, uint8_t dataLength); - -extern uint8_t Command_FromUSB_bootload_cmd; -extern uint8_t cpu_address; -extern uint8_t page_size; -extern uint8_t page_address; -extern uint8_t packets_per_page; -extern uint8_t address_idx; -extern uint8_t packet_idx; - -#endif /* _BOOTLOADING_H_ */ Modified: firmware/fuxusb/branches/usb_cleanup/fuxusb.Uv2 =================================================================== --- firmware/fuxusb/branches/usb_cleanup/fuxusb.Uv2 2008-05-05 08:34:42 UTC (rev 1100) +++ firmware/fuxusb/branches/usb_cleanup/fuxusb.Uv2 2008-05-05 10:48:26 UTC (rev 1101) @@ -4,35 +4,35 @@ Target (fuxusb), 0x0000 // Tools: 'MCS-51' Group (system) +Group (usb) +Group (spi) +Group (bootloader) Group (modules) Group (lib_mcu) Group (headers) File 1,1,<.\main.c><main.c> 0x0 File 1,1,<.\global.c><global.c> 0x0 -File 1,1,<.\modules\usb\usb_task.c><usb_task.c> 0x0 -File 1,1,<.\modules\spi\spi_task.c><spi_task.c> 0x0 -File 1,1,<.\bootloading.c><bootloading.c> 0x0 -File 1,1,<.\i2c.c><i2c.c> 0x0 -File 2,1,<.\modules\timer_soft\timer_soft.c><timer_soft.c> 0x0 -File 2,1,<.\modules\usb_enum\usb_enum.c><usb_enum.c> 0x0 -File 2,1,<.\modules\fifo\fifo_spk.c><fifo_spk.c> 0x0 -File 2,1,<.\modules\fifo\fifo_mic.c><fifo_mic.c> 0x0 -File 2,1,<.\modules\fifo\fifo.c><fifo.c> 0x0 -File 2,1,<.\modules\fifo_stt\fifo_stt.c><fifo_stt.c> 0x0 -File 3,1,<.\lib_mcu\usb\usb_drv.c><usb_drv.c> 0x0 -File 3,1,<.\lib_mcu\uart\uart_lib.c><uart_lib.c> 0x0 -File 3,1,<.\lib_mcu\spi\spi_lib.c><spi_lib.c> 0x0 -File 3,1,<.\lib_mcu\fa-usb\flash_api.c><flash_api.c> 0x0 -File 4,5,<.\config.h><config.h> 0x0 -File 4,5,<.\lib_mcu\reg_5131.h><reg_5131.h> 0x0 -File 4,5,<.\lib_mcu\ext_5131.h><ext_5131.h> 0x0 -File 4,5,<.\lib_mcu\5131_drv.h><5131_drv.h> 0x0 -File 4,5,<.\lib_mcu\usb\usb_drv.h><usb_drv.h> 0x0 -File 4,5,<.\global.h><global.h> 0x0 -File 4,5,<.\bootloading.h><bootloading.h> 0x0 -File 4,5,<.\i2c.h><i2c.h> 0x0 -File 4,5,<.\lib_mcu\twi\twi.h><twi.h> 0x0 +File 2,1,<.\usb\usb_drv.c><usb_drv.c> 0x0 +File 2,1,<.\usb\usb_enum.c><usb_enum.c> 0x0 +File 2,1,<.\usb\usb_task.c><usb_task.c> 0x0 +File 3,1,<.\spi\spi_task.c><spi_task.c> 0x0 +File 3,1,<.\spi\spi_lib.c><spi_lib.c> 0x0 +File 4,1,<.\bootloader\i2c.c><i2c.c> 0x0 +File 4,1,<.\bootloader\bootloading.c><bootloading.c> 0x0 +File 5,1,<.\modules\timer_soft\timer_soft.c><timer_soft.c> 0x0 +File 5,1,<.\modules\fifo\fifo_spk.c><fifo_spk.c> 0x0 +File 5,1,<.\modules\fifo\fifo_mic.c><fifo_mic.c> 0x0 +File 5,1,<.\modules\fifo\fifo.c><fifo.c> 0x0 +File 5,1,<.\modules\fifo_stt\fifo_stt.c><fifo_stt.c> 0x0 +File 6,1,<.\lib_mcu\uart\uart_lib.c><uart_lib.c> 0x0 +File 6,1,<.\lib_mcu\fa-usb\flash_api.c><flash_api.c> 0x0 +File 7,5,<.\config.h><config.h> 0x0 +File 7,5,<.\lib_mcu\reg_5131.h><reg_5131.h> 0x0 +File 7,5,<.\lib_mcu\ext_5131.h><ext_5131.h> 0x0 +File 7,5,<.\lib_mcu\5131_drv.h><5131_drv.h> 0x0 +File 7,5,<.\global.h><global.h> 0x0 +File 7,5,<.\bootloading.h><bootloading.h> 0x0 Options 1,0,0 // Target 'fuxusb' @@ -54,7 +54,7 @@ EnvLib () EnvReg () OrgReg () - TgStat=0 + TgStat=9 OutDir (.\obj\) OutName (fuxusb.aof) GenApp=1 Deleted: firmware/fuxusb/branches/usb_cleanup/i2c.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/i2c.c 2008-05-05 08:34:42 UTC (rev 1100) +++ firmware/fuxusb/branches/usb_cleanup/i2c.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -1,301 +0,0 @@ -#include "i2c.h" - -#define I2C_DEBUG 0 - -/* - * DEBUG - */ -//#define __debug__ - -/* I2C status and address variables */ -uint8_t i2cDeviceAddrRW; -volatile I2C_FLAGS i2cFlags; -/* send/transmit buffer (outgoing data) */ -uint8_t xdata i2cSendData[I2C_SEND_DATA_BUFFER_SIZE]; -uint8_t *i2cDataToSend; -uint8_t i2cSendDataIndex; -uint8_t i2cSendDataLength; -/* receive buffer (incoming data) */ -uint8_t xdata i2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE]; -uint8_t i2cReceiveDataIndex; -uint8_t i2cReceiveDataLength; - -/* function pointer to i2c receive routine */ -/* I2cSlaveReceive is called when this processor is addressed as a slave for - * writing */ -static void (*i2cSlaveReceive)(uint8_t receiveDataLength, uint8_t* recieveData); -/* I2cSlaveTransmit is called when this processor is addressed as a slave for - * reading */ -static uint8_t (*i2cSlaveTransmit)(uint8_t transmitDataLengthMax, uint8_t* transmitData); - -/* functions */ - -/* - * Set the TWI transaction bitrate - */ -void i2cSetBitrate(void) -{ - /* Setting SSCON based on TWI_SCAL defined in i2c.h */ - SSCON = TWI_SCAL_VALUE; /* twi intialisation */ -} - -/* - * This function initializes the TWI interface for i2c communication. You - * should use the definitions in i2c.h to select the mode you want. - * - * Note that you should set the i2cSlaveReceive and i2cSlaveTransmit handlers - * after this initialization otherwise they'll be cleared. - */ -void i2cInit(void) -{ - /* Set up twi */ - SSCON = TWI_SSIE | TWI_SCAL_VALUE; /* enable TWI */ -#ifdef TWI_INT_ENABLED - Enable_twi_interrupt(); -#endif - -#ifdef TWI_SLA_ENABLED /* If Slave mode: */ - /* Set local device address (used in slave mode only) */ - SSADR = I2C_SLA_ADD; - - /* Enabling Slave mode */ - TWI_SET_AA(); /* enable TWI ACK */ - i2cSlaveReceive = 0; /* XXX do we need those 2 lines? clear SlaveReceive and SlaveTransmit handler to null */ - i2cSlaveTransmit = 0; -#endif - -#ifdef TWI_M_ENABLED -#endif -} - -/* Set the user function which handles receiving (incoming) data as a slave */ -void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(uint8_t receiveDataLength, uint8_t* recieveData)) -{ - i2cSlaveReceive = i2cSlaveRx_func; -} - -/* Set the user function which handles transmitting (outgoing) data as a slave */ -void i2cSetSlaveTransmitHandler(uint8_t (*i2cSlaveTx_func)(uint8_t transmitDataLengthMax, uint8_t* transmitData)) -{ - i2cSlaveTransmit = i2cSlaveTx_func; -} - -void i2cSendStart(void); -/* Start a Master transmission, i2cSendDataIndex is reset automatically here */ -void i2cMasterStart(void) -{ - i2cFlags.i2c_busy = 1; - i2cSendDataIndex = 0; /* don't forget to reset the index */ - i2cSendStart(); -} - -void i2cSendStart(void) -{ - TWI_SET_START(); -} - -void i2cSendStop(void) -{ - TWI_SET_STOP(); - i2cFlags.i2c_busy = 0; -} - -void i2cSendByte(uint8_t _data) -{ - SSDAT = _data; -} - -void i2cAckReceiveByte(void) -{ - TWI_SET_AA(); -} - -void i2cNackReceiveByte(void) -{ - TWI_CLEAR_AA(); -} - -#define TW_DATA SSDAT -uint8_t i2cGetReceivedByte(void) -{ - return SSDAT; -} - -uint8_t i2cGetStatus(void) -{ - return TW_STATUS; -} - -#ifdef __debug__ -uint8_t i2cStatus[20]; /* debug only */ -uint8_t i2cStatusIdx = 0; -#endif - -/* TWI interrupt service routine */ -#ifdef TWI_INT_ENABLED -void it_TWI(void) interrupt IRQ_TWI using 1 -{ -#ifdef __debug__ - i2cStatus[i2cStatusIdx++] = TW_STATUS; - if (i2cStatusIdx == 20) i2cStatusIdx = 0; -#endif - - - switch (TW_STATUS) - { - /* * * Master General * * */ - case TW_START: // 0x08: Sent start condition - case TW_REP_START: // 0x10: Sent repeated start condition -#if I2C_DEBUG -#endif - // send device address - TWI_CLEAR_START(); /* clear start condition */ - i2cSendByte(i2cDeviceAddrRW); - break; - - - /* * * Master Transmitter & Receiver status codes * * */ - - case TW_MT_SLA_ACK: // 0x18: Slave address acknowledged - case TW_MT_DATA_ACK: // 0x28: Data acknowledged -#if I2C_DEBUG -#endif - if(i2cSendDataIndex < i2cSendDataLength) - { - - i2cSendByte( i2cSendData[i2cSendDataIndex++] ); /* send data */ - } - else - { - i2cSendStop(); /* End of data stream */ - i2cFlags.s_val = 1; - } - break; - case TW_MR_DATA_NACK: // 0x58: Data received, NACK reply issued -#if I2C_DEBUG -#endif - i2cReceiveData[i2cReceiveDataIndex++] = TW_DATA; // store final received data byte - /* no break, continue to transmit STOP condition */ - case TW_MR_SLA_NACK: // 0x48: Slave address not acknowledged - case TW_MT_SLA_NACK: // 0x20: Slave address not acknowledged - case TW_MT_DATA_NACK: // 0x30: Data not acknowledged -#if I2C_DEBUG -#endif - i2cFlags.mt_nack = 1; - i2cSendStop(); - break; - case TW_MT_ARB_LOST: // 0x38: Bus arbitration lost - //case TW_MR_ARB_LOST: // 0x38: Bus arbitration lost -#if I2C_DEBUG -#endif - i2cInit(); - break; - case TW_MR_DATA_ACK: // 0x50: Data acknowledged -#if I2C_DEBUG -#endif - // store received data byte - i2cReceiveData[i2cReceiveDataIndex++] = TW_DATA; - // fall-through to see if more bytes will be received - case TW_MR_SLA_ACK: // 0x40: Slave address acknowledged -#if I2C_DEBUG -#endif - if(i2cReceiveDataIndex < (i2cReceiveDataLength-1)) - i2cAckReceiveByte(); /* receive more bytes */ - else - i2cNackReceiveByte(); /* receive the last byte */ - break; - - - /* * * Slave Receiver status codes * * */ - - case TW_SR_SLA_ACK: // 0x60: own SLA+W has been received, ACK has been returned - case TW_SR_ARB_LOST_SLA_ACK: // 0x68: own SLA+W has been received, ACK has been returned - case TW_SR_GCALL_ACK: // 0x70: GCA+W has been received, ACK has been returned - case TW_SR_ARB_LOST_GCALL_ACK: // 0x78: GCA+W has been received, ACK has been returned -#if I2C_DEBUG -#endif - /* we are being addressed as slave for writing (data will be received from master) */ - i2cFlags.i2c_busy = 1; - i2cReceiveDataIndex = 0; - i2cFlags.s_nack = 0; /* reset nack flag here, not in user app */ - i2cAckReceiveByte(); /* accept data */ - break; - case TW_SR_DATA_ACK: // 0x80: data byte has been received, ACK has been returned - case TW_SR_GCALL_DATA_ACK: // 0x90: data byte has been received, ACK has been returned -#if I2C_DEBUG -#endif - i2cReceiveData[i2cReceiveDataIndex++] = TW_DATA; /* get received data byte */ - /* check receive buffer status */ - if(i2cReceiveDataIndex < I2C_RECEIVE_DATA_BUFFER_SIZE) - { - i2cAckReceiveByte(); /* accept more data */ - } - else - { - i2cNackReceiveByte(); /* refuse more data */ - } - break; - case TW_SR_DATA_NACK: // 0x88: data byte has been received, NACK has been returned - case TW_SR_GCALL_DATA_NACK: // 0x98: data byte has been received, NACK has been returned -#if I2C_DEBUG -#endif - //i2cReceiveData[i2cReceiveDataIndex++] = TW_DATA; /* receive last byte XXX check if this is right */ - //i2cNackReceiveByte(); //XXX bug here? // receive data byte and return NACK - //i2cAckReceiveByte(); [> I should clear the interrupt and enable acknoledge for slave mode (disabled during previous nack) <] - i2cFlags.s_nack = 1; /* XXX check if this flag is reset in all possible conditions */ - //break; - /* pass along to restart slave mode */ - case TW_SR_STOP: // 0xA0: STOP or REPEATED START has been received while addressed as slave -#if I2C_DEBUG -#endif - i2cInit(); /* enable TWI ACK */ - if(i2cSlaveReceive) i2cSlaveReceive(i2cReceiveDataIndex, i2cReceiveData); /* i2c receive is complete, call i2cSlaveReceive */ - i2cFlags.i2c_busy = 0; /* XXX check if this flag is reset in all possible conditions */ - break; - - - /* * * Slave Transmitter * * */ - - case TW_ST_SLA_ACK: // 0xA8: own SLA+R has been received, ACK has been returned - case TW_ST_ARB_LOST_SLA_ACK: // 0xB0: GCA+R has been received, ACK has been returned -#if I2C_DEBUG -#endif - // we are being addressed as slave for reading (data must be transmitted back to master) - // request data from application - if(i2cSlaveTransmit) i2cSendDataLength = i2cSlaveTransmit(I2C_SEND_DATA_BUFFER_SIZE, i2cSendData); - i2cSendDataIndex = 0; /* reset data index */ - /* fall-through to transmit first data byte */ - case TW_ST_DATA_ACK: // 0xB8: data byte has been transmitted, ACK has been received -#if I2C_DEBUG -#endif - TW_DATA = i2cSendData[i2cSendDataIndex++]; /* transmit data byte */ - if(i2cSendDataIndex < i2cSendDataLength) - i2cAckReceiveByte(); /* expect ACK to data byte */ - else - i2cNackReceiveByte(); /* expect NACK to data byte */ - break; - case TW_ST_DATA_NACK: // 0xC0: data byte has been transmitted, NACK has been received - case TW_ST_LAST_DATA: // 0xC8: last data byte transmitted, ACK received -#if I2C_DEBUG -#endif - /* all done, switch to open slave */ - i2cInit(); /* enable TWI ACK */ - break; - - - /* * * Misc * * */ - - case TW_NO_INFO: // 0xF8: No relevant state information - /* do nothing */ -#if I2C_DEBUG -#endif - break; - case TW_BUS_ERROR: // 0x00: Bus error due to illegal start or stop condition -#if I2C_DEBUG -#endif - i2cSendStop(); /* reset internal hardware and release bus */ - break; - } - TWI_CLEAR_SI(); -} -#endif Deleted: firmware/fuxusb/branches/usb_cleanup/i2c.h =================================================================== --- firmware/fuxusb/branches/usb_cleanup/i2c.h 2008-05-05 08:34:42 UTC (rev 1100) +++ firmware/fuxusb/branches/usb_cleanup/i2c.h 2008-05-05 10:48:26 UTC (rev 1101) @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2006, C2ME S.A. - * All rights reserved. - * - * $Id:$ - */ - -#ifndef _I2C_H_ -#define _I2C_H_ - -#include "lib_c/stdint.h" -#include "lib_mcu/twi/twi.h" -#include "config.h" - -#ifdef __debug__ -extern uint8_t i2cStatus[20]; /* XXX debug only */ -extern uint8_t i2cStatusIdx; -#endif - -/* * * include custom configuration here * * */ - -#define I2C_SEND_DATA_BUFFER_SIZE 130 -#define I2C_RECEIVE_DATA_BUFFER_SIZE 8 - -/* - * Choose operation mode, comment/uncomment lines - * For Slave only operation, choose Master and Slave - */ -#define TWI_INT_ENABLED /* comment if you don't want twi interrupts */ -#define TWI_M_ENABLED /* comment if you don't want twi master mode */ -//#define TWI_SLA_ENABLED /* comment if you don't want twi slave mode */ - -/* SSADR: TWI (Slave) Address Register - * Bits 7..1 : TWI (Slave) Address Register - * Bit 0 : TWI General Call Recognition Enable Bit */ -#define I2C_SLA_ADD ((0X55<<1) | 0x01) /* Only necessary in slave mode */ - -/* - * Initialise TWI clock - * - * Here we set SCL frequency in Master mode by defining TWI_SCAL: - * SCL_freq = F_CPU/TWI_SCAL - * - * TWI_SCAL value should be one of: 256 224 192 160 960 120 60 - */ - -//#define TWI_SCAL 160 /* TWI_SCAL value should be: 256 224 192 160 960 120 60 */ -#define TWI_SCAL 960 /* TWI_SCAL value should be: 256 224 192 160 960 120 60 */ - -/* * * END of code customisation * * */ - -#if TWI_SCAL == 256 -#define TWI_SCAL_VALUE TWI_RATIO_256 -#elif TWI_SCAL == 224 -#define TWI_SCAL_VALUE TWI_RATIO_224 -#elif TWI_SCAL == 192 -#define TWI_SCAL_VALUE TWI_RATIO_192 -#elif TWI_SCAL == 160 -#define TWI_SCAL_VALUE TWI_RATIO_160 -#elif TWI_SCAL == 960 -#define TWI_SCAL_VALUE TWI_RATIO_960 -#elif TWI_SCAL == 120 -#define TWI_SCAL_VALUE TWI_RATIO_120 -#elif TWI_SCAL == 60 -#define TWI_SCAL_VALUE TWI_RATIO_60 -#else -#error Incorrect TWI_SCAL value, should be: 256 224 192 160 960 120 60 -#define TWI_SCAL_VALUE -#endif - -/*! defines and constants */ -#define TWCR_CMD_MASK 0x0F - -/* - * Bits that are set inside interrupt routines, and watched outside in - * the program's main loop. - */ -typedef struct -{ - uint8_t i2c_busy: 1; /* set when twi hardware is busy, cleared after an i2c stop */ - uint8_t mt_nack: 1; /* error due to a nack received by the master transmitter */ - uint8_t s_nack: 1; /* error due to a nack replied by the slave */ - uint8_t sr_end: 1; /* set at the end of a receiver slave transmission if i2cSlaveReceive has not been defined */ - uint8_t st_end: 1; /* set at the end of a transmitter slave transmission if i2cSlaveTransmit has not been defined */ - uint8_t i2c_idx: 1; /* application side - can be used for data indexes status */ - uint8_t s_val: 1; /* application side - can be used for data validation */ -} -I2C_FLAGS; -extern volatile I2C_FLAGS i2cFlags; - -/*! types */ -typedef enum -{ - I2C_IDLE = 0, I2C_BUSY = 1, - I2C_MASTER_TX = 2, I2C_MASTER_RX = 3, - I2C_SLAVE_TX = 4, I2C_SLAVE_RX = 5 -} eI2cStateType; - -/* I2C state and address variables */ -extern uint8_t i2cDeviceAddrRW; -/* send/transmit buffer (outgoing data) */ -extern uint8_t xdata i2cSendData[]; -extern uint8_t i2cSendDataIndex; -extern uint8_t i2cSendDataLength; -/* receive buffer (incoming data) */ -extern uint8_t xdata i2cReceiveData[]; -extern uint8_t i2cReceiveDataIndex; -extern uint8_t i2cReceiveDataLength; - -/* Functions */ -void i2cSetBitrate(void); -void i2cInit(void); -void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(uint8_t receiveDataLength, uint8_t* recieveData)); -void i2cSetSlaveTransmitHandler(uint8_t (*i2cSlaveTx_func)(uint8_t transmitDataLengthMax, uint8_t* transmitData)); -void i2cMasterStart(void); - -#endif - Modified: firmware/fuxusb/branches/usb_cleanup/main.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/main.c 2008-05-05 08:34:42 UTC (rev 1100) +++ firmware/fuxusb/branches/usb_cleanup/main.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -15,7 +15,7 @@ #include "config.h" #include "lib_c/stdint.h" #include "global.h" -#include "modules\usb\usb_task.h" +#include "usb\usb_task.h" #ifdef MAIN_DEBUG #include "lib_mcu\uart\uart_lib.h" Modified: firmware/fuxusb/branches/usb_cleanup/modules/fifo/fifo_spk.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/modules/fifo/fifo_spk.c 2008-05-05 08:34:42 UTC (rev 1100) +++ firmware/fuxusb/branches/usb_cleanup/modules/fifo/fifo_spk.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -13,7 +13,7 @@ #include "config.h" #include "fifo_spk.h" -#include "lib_mcu\usb\usb_drv.h" +#include "usb\usb_drv.h" unsigned char FifoTbl_SPK[FIFOTBL_SPK_MAX]; Modified: firmware/fuxusb/branches/usb_cleanup/modules/fifo_stt/fifo_stt.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/modules/fifo_stt/fifo_stt.c 2008-05-05 08:34:42 UTC (rev 1100) +++ firmware/fuxusb/branches/usb_cleanup/modules/fifo_stt/fifo_stt.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -13,7 +13,7 @@ #include "config.h" #include "fifo_stt.h" -#include "lib_mcu\usb\usb_drv.h" +#include "usb\usb_drv.h" unsigned char FifoTbl_Stt[FIFOTBL_STT_MAX]; Added: firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.c (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,52 @@ +/*C************************************************************************** + * NAME: spi_lib.c + *---------------------------------------------------------------------------- + * Copyright (c) 2006 C2me. + *---------------------------------------------------------------------------- + * RELEASE: c5131-usb-RF Dongle + * REVISION: 1.3 + *---------------------------------------------------------------------------- + * PURPOSE: + * This file provides the function to initiate the SPI interface + *****************************************************************************/ + +/*_____ I N C L U D E S ____________________________________________________*/ +#include "config.h" +#include "spi\spi_lib.h" + + +/*_____ G L O B A L D E F I N I T I O N _________________________________*/ + + +/*_____ D E F I N I T I O N ________________________________________________*/ + + +/*_____ M A C R O S ________________________________________________________*/ + + +/*_____ D E C L A R A T I O N ______________________________________________*/ + +/*F************************************************************************** + * NAME: spi_init + *---------------------------------------------------------------------------- + * PARAMS: + * delay: none + * return: none + *---------------------------------------------------------------------------- + * PURPOSE: + * This function initializes the UART USB library. + *---------------------------------------------------------------------------- + * EXAMPLE: + *---------------------------------------------------------------------------- + * NOTE: + *---------------------------------------------------------------------------- + * REQUIREMENTS: + *****************************************************************************/ + + +unsigned char spi_SendByte(Uchar Data) +{ + SPDAT = Data; + while(!(SPSTA&0x80)); + return 1; +} Property changes on: firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.c ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.h =================================================================== --- firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.h (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.h 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,31 @@ +/*H************************************************************************** +* NAME: uart_lib.h +*---------------------------------------------------------------------------- +* Copyright (c) 2006 C2me s.a. +*---------------------------------------------------------------------------- +* RELEASE: +* REVISION: +*---------------------------------------------------------------------------- +* PURPOSE: +* SPI lib header file +*****************************************************************************/ + +#ifndef _SPI_LIB_H_ +#define _SPI_LIB_H_ + +/*_____ C O N F I G U R A T I O N _________________________________________*/ + +/*_____ D E F I N I T I O N S ______________________________________________*/ + +/*_____ D E C L A R A T I O N ______________________________________________*/ + +/*_____ M A C R O S ________________________________________________________*/ + +#define SPI_CSn P1_1 + +/*_____ P R O T O T Y P E S ____________________________________________________________*/ + +extern void spi_init(void); +extern unsigned char spi_SendByte(Uchar Data); + +#endif Property changes on: firmware/fuxusb/branches/usb_cleanup/spi/spi_lib.h ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/fuxusb/branches/usb_cleanup/spi/spi_task.c =================================================================== --- firmware/fuxusb/branches/usb_cleanup/spi/spi_task.c (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/spi/spi_task.c 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,146 @@ +/*C************************************************************************** + * NAME: spi_task.c + *---------------------------------------------------------------------------- + * Copyright (c) 2006 C2me. + *---------------------------------------------------------------------------- + * PURPOSE: + * This file controls + *---------------------------------------------------------------------------- + * $Id$ + *****************************************************************************/ +/*_____ I N C L U D E S ____________________________________________________*/ + +#include "global.h" +#include "config.h" +#include "spi\spi_lib.h" +#include "spi\spi_task.h" +#include "global.h" + +/*_____ M A C R O S ________________________________________________________*/ + +/*_____ D E F I N I T I O N ________________________________________________*/ + + +/*_____ D E C L A R A T I O N ______________________________________________*/ + + +data STM_SPI_MASTER_SLAVE_TYPE spi_slave; +data STM_SPI_SLAVE_MASTER_TYPE spi_master; +data unsigned char spi_slave_config; +data unsigned char spi_master_config; +data unsigned char spi_count; +data unsigned char spi_lenght_data; + +bit spi_enable = 1; +bit spi_ready = 0; +bit spi_Start_Flag = 0; + +unsigned char spi_TestCtr ; // Debug + + +/*F************************************************************************** + * NAME: spi_task_reset + *---------------------------------------------------------------------------- + * PARAMS: + * delay: none + * return: none + *---------------------------------------------------------------------------- + * PURPOSE: + * Reset the ASM of spi-task if watchdog or RFOFFLINE + *---------------------------------------------------------------------------- + * EXAMPLE: + *---------------------------------------------------------------------------- + * NOTE: + *---------------------------------------------------------------------------- + * REQUIREMENTS: + *****************************************************************************/ + +void spi_task_reset(void) +{ + spi_slave = HEADERS; // Set state machine + spi_master = HEADERM; + spi_enable = 1; // Communication Authorized + SPI_CSn = 1; // Chip select + spi_Start_Flag = 0; + spi_ready = 0; +} + +/*F************************************************************************** + * NAME: spi_task_init + *---------------------------------------------------------------------------- + * PARAMS: + * delay: none + * return: none + *---------------------------------------------------------------------------- + * PURPOSE: + * This function initializes the SPI controller and the associated variables. + *---------------------------------------------------------------------------- + * EXAMPLE: + *---------------------------------------------------------------------------- + * NOTE: + *---------------------------------------------------------------------------- + * REQUIREMENTS: + *****************************************************************************/ + + +void spi_task_init(void) +{ + +#ifdef X2_MODE + Set_spi_x2_mode(); +#endif + SPCON |= MSK_MSTR; /* Master mode */ + +#ifdef SPI_Clock_Div128 + SPCON |= 0x82; /* Fclk Periph/128 */ +#endif +#ifdef SPI_Clock_Div4 + SPCON |= 0x01; /* Fclk Periph/4 */ +#endif +#ifdef SPI_Clock_Div8 + SPCON |= 0x02; /* Fclk Periph/8 */ +#endif +#ifdef SPI_Clock_Div16 + SPCON |= 0x03; /* Fclk Periph/8 */ +#endif + SPCON &= ~MSK_CPOL; /* CPOL=0; transmit mode example */ + SPCON &= ~MSK_CPHA; /* CPHA=0; transmit mode example */ + SPCON |= MSK_SSDIS; + SPCON |= MSK_SPEN; /* run spi */ + + SPI_DREADY = 1; + SPI_START = 1; + SPI_CSn = 1; + IT0 = 1; // Falling Edge On Int0 + EX0 = 1; /* enable Int0 interrupt */ + + spi_slave = 0; // Reset State Machine of spi_task() + spi_master = 0; // Reset State Machine of spi_task() + + spi_task_on_Flag = 0; +} + + +/*F************************************************************************** + * NAME: it_INT0 + *---------------------------------------------------------------------------- + * PARAMS: + * delay: none + * return: none + *---------------------------------------------------------------------------- + * PURPOSE: + * Interrupt manages the DREADY Signal From RF Module + *---------------------------------------------------------------------------- + * EXAMPLE: + *---------------------------------------------------------------------------- + * NOTE: + *---------------------------------------------------------------------------- + * REQUIREMENTS: + *****************************************************************************/ + +void it_INT0(void) interrupt IRQ_INT0 +{ + spi_ready = 1; +} + + Property changes on: firmware/fuxusb/branches/usb_cleanup/spi/spi_task.c ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/fuxusb/branches/usb_cleanup/spi/spi_task.h =================================================================== --- firmware/fuxusb/branches/usb_cleanup/spi/spi_task.h (rev 0) +++ firmware/fuxusb/branches/usb_cleanup/spi/spi_task.h 2008-05-05 10:48:26 UTC (rev 1101) @@ -0,0 +1,50 @@ +/*C************************************************************************** +* NAME: spi_task.h +*---------------------------------------------------------------------------- +* Copyright (c) 2006 C2me. +*---------------------------------------------------------------------------- +* RELEASE: c5131-usb-RF Dongle +* REVISION: 1.3 +*---------------------------------------------------------------------------- +* PURPOSE: +* +*****************************************************************************/ + +#ifndef _SPITASK_H_ +#define _SPITASK_H_ + +/*_____ I N C L U D E S ____________________________________________________*/ +... [truncated message content] |