From f229502d9addacfbefe955a47931b980bfbb1a10 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Sun, 23 Aug 2009 08:37:11 +0000 Subject: [PATCH] Fixed AVRISP programmer demo -- can now connect to a target and read/write Sig/Lock/Fuse/OSCCAL bytes successfully. Changed SPI_Init() to allow for the clock polarity and data sample modes to be set. Changed Dataflash_Init() to no longer call SPI_Init() automatically. --- .../ClassDriver/MassStorage/MassStorage.c | 3 +- .../Device/LowLevel/MassStorage/MassStorage.c | 3 +- LUFA/Drivers/Board/Dataflash.h | 10 +--- LUFA/Drivers/Peripheral/SPI.h | 31 ++++++++--- LUFA/ManPages/ChangeLog.txt | 1 + LUFA/ManPages/MigrationInformation.txt | 4 ++ Projects/Incomplete/AVRISP/Lib/V2Protocol.c | 55 +++++-------------- .../Incomplete/AVRISP/Lib/V2ProtocolParams.c | 6 +- Projects/Incomplete/AVRISP/makefile | 8 +-- 9 files changed, 58 insertions(+), 63 deletions(-) diff --git a/Demos/Device/ClassDriver/MassStorage/MassStorage.c b/Demos/Device/ClassDriver/MassStorage/MassStorage.c index 52ed2d9383..4092680c48 100644 --- a/Demos/Device/ClassDriver/MassStorage/MassStorage.c +++ b/Demos/Device/ClassDriver/MassStorage/MassStorage.c @@ -84,7 +84,8 @@ void SetupHardware(void) /* Hardware Initialization */ LEDs_Init(); - Dataflash_Init(SPI_SPEED_FCPU_DIV_2); + SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER); + Dataflash_Init(); USB_Init(); /* Clear Dataflash sector protections, if enabled */ diff --git a/Demos/Device/LowLevel/MassStorage/MassStorage.c b/Demos/Device/LowLevel/MassStorage/MassStorage.c index 1a2d8d468a..cf276fd81d 100644 --- a/Demos/Device/LowLevel/MassStorage/MassStorage.c +++ b/Demos/Device/LowLevel/MassStorage/MassStorage.c @@ -75,7 +75,8 @@ void SetupHardware(void) /* Hardware Initialization */ LEDs_Init(); - Dataflash_Init(SPI_SPEED_FCPU_DIV_2); + SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER); + Dataflash_Init(); USB_Init(); /* Clear Dataflash sector protections, if enabled */ diff --git a/LUFA/Drivers/Board/Dataflash.h b/LUFA/Drivers/Board/Dataflash.h index acdcc4c401..8c2b69d991 100644 --- a/LUFA/Drivers/Board/Dataflash.h +++ b/LUFA/Drivers/Board/Dataflash.h @@ -164,17 +164,13 @@ #endif /* Inline Functions: */ - /** Initializes the dataflash driver (including the SPI driver) so that commands and data may be - * sent to an attached dataflash IC. - * - * \param[in] PrescalerMask SPI prescaler mask, see SPI.h documentation + /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC. + * The AVR's SPI driver MUST be initialized before any of the dataflash commands are used. */ - static inline void Dataflash_Init(const uint8_t PrescalerMask) + static inline void Dataflash_Init(void) { DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK; DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK; - - SPI_Init(PrescalerMask, true); } /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive diff --git a/LUFA/Drivers/Peripheral/SPI.h b/LUFA/Drivers/Peripheral/SPI.h index df741d7b87..dcc58ce494 100644 --- a/LUFA/Drivers/Peripheral/SPI.h +++ b/LUFA/Drivers/Peripheral/SPI.h @@ -61,7 +61,7 @@ /* Private Interface - For use in library only: */ #if !defined(__DOXYGEN__) /* Macros: */ - #define SPI_USE_DOUBLESPEED (1 << 7) + #define SPI_USE_DOUBLESPEED (1 << SPE) #endif /* Public Interface - May be used in end-application: */ @@ -86,23 +86,40 @@ /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 128. */ #define SPI_SPEED_FCPU_DIV_128 ((1 << SPR1) | (1 << SPR0)) + + /** SPI clock polarity mask for SPI_Init(). Indicates that the SCK should lead on the rising edge. */ + #define SPI_SCK_LEAD_RISING (0 << CPOL) + + /** SPI clock polarity mask for SPI_Init(). Indicates that the SCK should lead on the falling edge. */ + #define SPI_SCK_LEAD_FALLING (1 << CPOL) + + /** SPI data sample mode mask for SPI_Init(). Indicates that the data should sampled on the leading edge. */ + #define SPI_SAMPLE_LEADING (0 << CPHA) + + /** SPI data sample mode mask for SPI_Init(). Indicates that the data should be sampled on the trailing edge. */ + #define SPI_SAMPLE_TRAILING (1 << CPHA) + + /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */ + #define SPI_MODE_SLAVE (0 << MSTR) + + /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */ + #define SPI_MODE_MASTER (1 << MSTR) /* Inline Functions: */ /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other * SPI routines. * - * \param[in] PrescalerMask Prescaler mask to set the SPI clock speed - * \param[in] Master If true, sets the SPI system to use master mode, slave if false + * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the SPI_SPEED_*, + * SPI_SCK_*, SPI_SAMPLE_* and SPI_MODE_* masks */ - static inline void SPI_Init(const uint8_t PrescalerMask, const bool Master) + static inline void SPI_Init(const uint8_t SPIOptions) { DDRB |= ((1 << 1) | (1 << 2)); PORTB |= ((1 << 0) | (1 << 3)); - SPCR = ((1 << SPE) | (Master << MSTR) | (1 << CPOL) | (1 << CPHA) | - (PrescalerMask & ~SPI_USE_DOUBLESPEED)); + SPCR = ((1 << SPE) | SPIOptions); - if (PrescalerMask & SPI_USE_DOUBLESPEED) + if (SPIOptions & SPI_USE_DOUBLESPEED) SPSR |= (1 << SPI2X); else SPSR &= ~(1 << SPI2X); diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt index 058d55c5cf..da4c007f03 100644 --- a/LUFA/ManPages/ChangeLog.txt +++ b/LUFA/ManPages/ChangeLog.txt @@ -23,6 +23,7 @@ * - Added new CDC_Device_Flush() command to the device mode CDC Class driver * - Added explicit attribute masks to the device mode demos' descriptors * - Added return values to the CDC and MIDI class driver transmit functions + * - Added extra masks to the SPI driver, changed SPI_Init() so that the clock polarity and sample modes can be set * * Fixed: * - Fixed possible lockup in the CDC device class driver, when the host sends data that is a multiple of the diff --git a/LUFA/ManPages/MigrationInformation.txt b/LUFA/ManPages/MigrationInformation.txt index f65539d563..d83043182b 100644 --- a/LUFA/ManPages/MigrationInformation.txt +++ b/LUFA/ManPages/MigrationInformation.txt @@ -14,6 +14,10 @@ * * Non-USB Library Components * - The ADC_Off() function has been renamed to \ref ADC_ShutDown() to be consistent with the rest of the library. + * - The Dataflash_Init() routine no longer initializes the SPI bus - the SPI bus should be initialized manually via a + * call to SPI_Init() before using the Dataflash driver + * - The SPI_Init() routine's parameters have changed, so that the clock polarity and data sampling modes can be set. See + * the SPI_Init() function documentation for more details * * \section Sec_Migration090810 Migrating from 090605 to 090810 * diff --git a/Projects/Incomplete/AVRISP/Lib/V2Protocol.c b/Projects/Incomplete/AVRISP/Lib/V2Protocol.c index c31fd0eb5c..dc6d3c6683 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2Protocol.c +++ b/Projects/Incomplete/AVRISP/Lib/V2Protocol.c @@ -150,10 +150,8 @@ void V2Protocol_ProcessCommand(void) break; } - printf("COMMAND 0x%02x\r\n", V2Command); - - Endpoint_WaitUntilReady(); - Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT); + Endpoint_WaitUntilReady(); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT); } static void V2Protocol_Command_Unknown(uint8_t V2Command) @@ -167,7 +165,6 @@ static void V2Protocol_Command_Unknown(uint8_t V2Command) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); Endpoint_Write_Byte(V2Command); Endpoint_Write_Byte(STATUS_CMD_UNKNOWN); @@ -178,7 +175,6 @@ static void V2Protocol_Command_SignOn(void) { Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); Endpoint_Write_Byte(CMD_SIGN_ON); Endpoint_Write_Byte(STATUS_CMD_OK); @@ -189,31 +185,28 @@ static void V2Protocol_Command_SignOn(void) static void V2Protocol_Command_GetSetParam(uint8_t V2Command) { - struct - { - uint8_t ParamID; - uint8_t ParamValue; - } Get_Set_Param_Params; + uint8_t ParamID = Endpoint_Read_Byte(); + uint8_t ParamValue; - Endpoint_Read_Stream_LE(&Get_Set_Param_Params, sizeof(Get_Set_Param_Params)); + if (V2Command == CMD_SET_PARAMETER) + ParamValue = Endpoint_Read_Byte(); Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); - - uint8_t ParamPrivs = V2Params_GetParameterPrivellages(Get_Set_Param_Params.ParamID); Endpoint_Write_Byte(V2Command); + uint8_t ParamPrivs = V2Params_GetParameterPrivellages(ParamID); + if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE)) { Endpoint_Write_Byte(STATUS_CMD_OK); - V2Params_SetParameterValue(Get_Set_Param_Params.ParamID, Get_Set_Param_Params.ParamValue); + V2Params_SetParameterValue(ParamID, ParamValue); } else if ((V2Command == CMD_GET_PARAMETER) && (ParamPrivs & PARAM_PRIV_READ)) { Endpoint_Write_Byte(STATUS_CMD_OK); - Endpoint_Write_Byte(V2Params_GetParameterValue(Get_Set_Param_Params.ParamID)); + Endpoint_Write_Byte(V2Params_GetParameterValue(ParamID)); } else { @@ -229,7 +222,6 @@ static void V2Protocol_Command_LoadAddress(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); // TODO: Check for extended address @@ -256,40 +248,28 @@ static void V2Protocol_Command_EnterISPMode(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); - uint8_t SCKDuration = V2Params_GetParameterValue(PARAM_SCK_DURATION); uint8_t ResponseStatus = STATUS_CMD_FAILED; - - Enter_ISP_Params.TimeoutMS -= Enter_ISP_Params.ExecutionDelayMS + - Enter_ISP_Params.PinStabDelayMS; CurrentAddress = 0; - if (SCKDuration >= sizeof(SPIMaskFromSCKDuration)) - SCKDuration = (sizeof(SPIMaskFromSCKDuration) - 1); - V2Protocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS); - SPI_Init(SPIMaskFromSCKDuration[SCKDuration], true); + SPI_Init(V2Protocol_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER); V2Protocol_ChangeTargetResetLine(true); V2Protocol_DelayMS(Enter_ISP_Params.PinStabDelayMS); while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED)) { uint8_t ResponseBytes[4]; - + for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++) { - ResponseBytes[RByte] = SPI_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]); V2Protocol_DelayMS(Enter_ISP_Params.ByteDelay); - - if (Enter_ISP_Params.TimeoutMS >= Enter_ISP_Params.ByteDelay) - Enter_ISP_Params.TimeoutMS -= Enter_ISP_Params.ByteDelay; - else - ResponseStatus = STATUS_CMD_TOUT; + ResponseBytes[RByte] = SPI_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]); } - if (ResponseBytes[Enter_ISP_Params.PollIndex] == Enter_ISP_Params.PollValue) + /* Check if polling disabled, or if the polled value matches the expected value */ + if (!Enter_ISP_Params.PollIndex || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue)) ResponseStatus = STATUS_CMD_OK; } @@ -310,7 +290,6 @@ static void V2Protocol_Command_LeaveISPMode(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); V2Protocol_DelayMS(Leave_ISP_Params.PreDelayMS); V2Protocol_ChangeTargetResetLine(false); @@ -335,7 +314,6 @@ static void V2Protocol_Command_ChipErase(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); uint8_t ResponseStatus = STATUS_CMD_OK; @@ -364,7 +342,6 @@ static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); uint8_t ResponseBytes[4]; @@ -389,7 +366,6 @@ static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++) SPI_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]); @@ -416,7 +392,6 @@ static void V2Protocol_Command_SPIMulti(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - Endpoint_WaitUntilReady(); Endpoint_Write_Byte(CMD_SPI_MULTI); Endpoint_Write_Byte(STATUS_CMD_OK); diff --git a/Projects/Incomplete/AVRISP/Lib/V2ProtocolParams.c b/Projects/Incomplete/AVRISP/Lib/V2ProtocolParams.c index 3a42ba32de..9e6467244d 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2ProtocolParams.c +++ b/Projects/Incomplete/AVRISP/Lib/V2ProtocolParams.c @@ -51,7 +51,7 @@ static ParameterItem_t ParameterTable[] = .ParamPrivellages = PARAM_PRIV_READ }, { .ParamID = PARAM_HW_VER, - .ParamValue = 0x01, + .ParamValue = 0x00, .ParamPrivellages = PARAM_PRIV_READ }, { .ParamID = PARAM_SW_MAJOR, @@ -59,7 +59,7 @@ static ParameterItem_t ParameterTable[] = .ParamPrivellages = PARAM_PRIV_READ }, { .ParamID = PARAM_SW_MINOR, - .ParamValue = 0x00, + .ParamValue = 0x0C, .ParamPrivellages = PARAM_PRIV_READ }, { .ParamID = PARAM_VTARGET, @@ -67,7 +67,7 @@ static ParameterItem_t ParameterTable[] = .ParamPrivellages = PARAM_PRIV_READ }, { .ParamID = PARAM_SCK_DURATION, - .ParamValue = 0, + .ParamValue = 0xFF, .ParamPrivellages = PARAM_PRIV_READ | PARAM_PRIV_WRITE }, { .ParamID = PARAM_RESET_POLARITY, diff --git a/Projects/Incomplete/AVRISP/makefile b/Projects/Incomplete/AVRISP/makefile index 958d9abae8..b9aee4848c 100644 --- a/Projects/Incomplete/AVRISP/makefile +++ b/Projects/Incomplete/AVRISP/makefile @@ -60,7 +60,7 @@ # MCU name -MCU = at90usb1287 +MCU = at90usb647 # Target board (see library "Board Types" documentation, USER or blank for projects not requiring @@ -194,9 +194,9 @@ CSTANDARD = -std=gnu99 # Place -D or -U options here for C sources CDEFS = -DF_CPU=$(F_CPU)UL -DF_CLOCK=$(F_CLOCK)UL -DBOARD=BOARD_$(BOARD) $(LUFA_OPTS) -CDEFS += -DRESET_LINE_PORT=PORTA -CDEFS += -DRESET_LINE_DDR=DDRA -CDEFS += -DRESET_LINE_MASK="(1 << 0)" +CDEFS += -DRESET_LINE_PORT=PORTB +CDEFS += -DRESET_LINE_DDR=DDRB +CDEFS += -DRESET_LINE_MASK="(1 << 4)" # Place -D or -U options here for ASM sources