Fixed AVRISP programmer demo -- can now connect to a target and read/write Sig/Lock...
authorDean Camera <dean@fourwalledcubicle.com>
Sun, 23 Aug 2009 08:37:11 +0000 (08:37 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Sun, 23 Aug 2009 08:37:11 +0000 (08:37 +0000)
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.

Demos/Device/ClassDriver/MassStorage/MassStorage.c
Demos/Device/LowLevel/MassStorage/MassStorage.c
LUFA/Drivers/Board/Dataflash.h
LUFA/Drivers/Peripheral/SPI.h
LUFA/ManPages/ChangeLog.txt
LUFA/ManPages/MigrationInformation.txt
Projects/Incomplete/AVRISP/Lib/V2Protocol.c
Projects/Incomplete/AVRISP/Lib/V2ProtocolParams.c
Projects/Incomplete/AVRISP/makefile

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