Advertisement
Guest User

SparkCore proposal hw_config.c - dimmed LED

a guest
Jan 14th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 52.58 KB | None | 0 0
  1. /**
  2.  ******************************************************************************
  3.  * @file    hw_config.c
  4.  * @author  Satish Nair, Zachary Crockett and Mohit Bhoite
  5.  * @version V1.0.0
  6.  * @date    13-March-2013
  7.  * @brief   Hardware Configuration & Setup
  8.  ******************************************************************************
  9.   Copyright (c) 2013 Spark Labs, Inc.  All rights reserved.
  10.  
  11.   This program is free software; you can redistribute it and/or
  12.   modify it under the terms of the GNU Lesser General Public
  13.   License as published by the Free Software Foundation, either
  14.   version 3 of the License, or (at your option) any later version.
  15.  
  16.   This program is distributed in the hope that it will be useful,
  17.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19.   Lesser General Public License for more details.
  20.  
  21.   You should have received a copy of the GNU Lesser General Public
  22.   License along with this program; if not, see <http://www.gnu.org/licenses/>.
  23.   ******************************************************************************
  24.  */
  25.  
  26. /* Includes ------------------------------------------------------------------*/
  27. #include "hw_config.h"
  28. #include "usb_lib.h"
  29. #include "usb_pwr.h"
  30.  
  31. /* Private typedef -----------------------------------------------------------*/
  32.  
  33. /* Private define ------------------------------------------------------------*/
  34.  
  35. /* Private macro -------------------------------------------------------------*/
  36.  
  37. /* Private variables ---------------------------------------------------------*/
  38. uint8_t USE_SYSTEM_FLAGS = 0;   //0, 1
  39.  
  40. __IO uint32_t TimingDelay;
  41. __IO uint32_t TimingLED;
  42. __IO uint32_t TimingBUTTON;
  43. __IO uint32_t TimingIWDGReload;
  44.  
  45. __IO uint8_t IWDG_SYSTEM_RESET;
  46.  
  47. GPIO_TypeDef* DIO_PORT[] = {D0_GPIO_PORT, D1_GPIO_PORT, D2_GPIO_PORT, D3_GPIO_PORT,
  48.                                 D4_GPIO_PORT, D5_GPIO_PORT, D6_GPIO_PORT, D7_GPIO_PORT};
  49. const uint16_t DIO_PIN[] = {D0_PIN, D1_PIN, D2_PIN, D3_PIN,
  50.                                 D4_PIN, D5_PIN, D6_PIN, D7_PIN};
  51. const uint32_t DIO_CLK[] = {D0_GPIO_CLK, D1_GPIO_CLK, D2_GPIO_CLK, D3_GPIO_CLK,
  52.                                 D4_GPIO_CLK, D5_GPIO_CLK, D6_GPIO_CLK, D7_GPIO_CLK};
  53.  
  54. GPIO_TypeDef* LED_PORT[] = {LED1_GPIO_PORT, LED2_GPIO_PORT, LED3_GPIO_PORT, LED4_GPIO_PORT};
  55. const uint16_t LED_PIN[] = {LED1_PIN, LED2_PIN, LED3_PIN, LED4_PIN};
  56. const uint32_t LED_CLK[] = {LED1_GPIO_CLK, LED2_GPIO_CLK, LED3_GPIO_CLK, LED4_GPIO_CLK};
  57. __IO uint16_t LED_TIM_CCR[] = {0x0000, 0x0000, 0x0000, 0x0000};
  58. __IO uint16_t LED_TIM_CCR_SIGNAL[] = {0x0000, 0x0000, 0x0000, 0x0000};  //TIM CCR Signal Override
  59. int8_t delta1, delta2, delta3, delta4;
  60. uint8_t LED_RGB_OVERRIDE = 0;
  61.  
  62. GPIO_TypeDef* BUTTON_PORT[] = {BUTTON1_GPIO_PORT, BUTTON2_GPIO_PORT};
  63. const uint16_t BUTTON_PIN[] = {BUTTON1_PIN, BUTTON2_PIN};
  64. const uint32_t BUTTON_CLK[] = {BUTTON1_GPIO_CLK, BUTTON2_GPIO_CLK};
  65. GPIOMode_TypeDef BUTTON_GPIO_MODE[] = {BUTTON1_GPIO_MODE, BUTTON2_GPIO_MODE};
  66. __IO uint16_t BUTTON_DEBOUNCED_TIME[] = {0, 0};
  67.  
  68. const uint16_t BUTTON_EXTI_LINE[] = {BUTTON1_EXTI_LINE, BUTTON2_EXTI_LINE};
  69. const uint16_t BUTTON_PORT_SOURCE[] = {BUTTON1_EXTI_PORT_SOURCE, BUTTON2_EXTI_PORT_SOURCE};
  70. const uint16_t BUTTON_PIN_SOURCE[] = {BUTTON1_EXTI_PIN_SOURCE, BUTTON2_EXTI_PIN_SOURCE};
  71. const uint16_t BUTTON_IRQn[] = {BUTTON1_EXTI_IRQn, BUTTON2_EXTI_IRQn};
  72. EXTITrigger_TypeDef BUTTON_EXTI_TRIGGER[] = {BUTTON1_EXTI_TRIGGER, BUTTON2_EXTI_TRIGGER};
  73.  
  74. uint16_t CORE_FW_Version_SysFlag = 0xFFFF;
  75. uint16_t NVMEM_SPARK_Reset_SysFlag = 0xFFFF;
  76. uint16_t FLASH_OTA_Update_SysFlag = 0xFFFF;
  77. uint16_t OTA_FLASHED_Status_SysFlag = 0xFFFF;
  78.  
  79. uint32_t WRPR_Value = 0xFFFFFFFF;
  80. uint32_t Flash_Pages_Protected = 0x0;
  81. uint32_t Internal_Flash_Address = 0;
  82. uint32_t External_Flash_Address = 0;
  83. uint32_t Internal_Flash_Data = 0;
  84. uint8_t External_Flash_Data[4];
  85. uint32_t EraseCounter = 0;
  86. uint32_t NbrOfPage = 0;
  87. volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
  88.  
  89. __IO uint16_t CC3000_SPI_CR;
  90. __IO uint16_t sFLASH_SPI_CR;
  91.  
  92. /* Dim LED by Frido */
  93.  
  94. uint8_t LED_DIM = 8;
  95.  
  96. /* Extern variables ----------------------------------------------------------*/
  97.  
  98. /* Private function prototypes -----------------------------------------------*/
  99.  
  100. /* Private functions ---------------------------------------------------------*/
  101.  
  102. /**
  103.  * @brief  Initialise Data Watchpoint and Trace Register (DWT).
  104.  * @param  None
  105.  * @retval None
  106.  */
  107. static void DWT_Init(void)
  108. {
  109.     CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  110.     DWT->CYCCNT = 0;
  111.     DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  112. }
  113.  
  114. /**
  115.  * @brief  Configures Main system clocks & power.
  116.  * @param  None
  117.  * @retval None
  118.  */
  119. void Set_System(void)
  120. {
  121.     /*!< At this stage the microcontroller clock setting is already configured,
  122.      this is done through SystemInit() function which is called from startup
  123.      file (startup_stm32f10x_xx.S) before to branch to application main.
  124.      To reconfigure the default setting of SystemInit() function, refer to
  125.      system_stm32f10x.c file
  126.      */
  127.  
  128.     DWT_Init();
  129.  
  130.     /* Enable PWR and BKP clock */
  131.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  132.  
  133.     /* Enable write access to Backup domain */
  134.     PWR_BackupAccessCmd(ENABLE);
  135.  
  136.     /* NVIC configuration */
  137.     NVIC_Configuration();
  138.  
  139.     /* Enable AFIO clock */
  140.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  141.  
  142.     /* Configure DIOs */
  143.     int Dx;
  144.     for(Dx = 0; Dx < Dn; ++Dx)
  145.     {
  146.         DIO_Init(Dx);
  147.     }
  148.  
  149.     /* Configure TIM1 for LED-PWM and BUTTON-DEBOUNCE usage */
  150.     UI_Timer_Configure();
  151.  
  152.     /* Configure the LEDs and set the default states */
  153.     int LEDx;
  154.     for(LEDx = 0; LEDx < LEDn; ++LEDx)
  155.     {
  156.         LED_Init(LEDx);
  157.     }
  158.  
  159.     /* Configure the Button */
  160.     BUTTON_Init(BUTTON1, BUTTON_MODE_EXTI);
  161. }
  162.  
  163. /*******************************************************************************
  164.  * Function Name  : NVIC_Configuration
  165.  * Description    : Configures Vector Table base location.
  166.  * Input          : None
  167.  * Output         : None
  168.  * Return         : None
  169.  *******************************************************************************/
  170. void NVIC_Configuration(void)
  171. {
  172.     /* Configure the NVIC Preemption Priority Bits */
  173.     /* 4 bits for pre-emption priority(0-15 PreemptionPriority) and 0 bits for subpriority(0 SubPriority) */
  174.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  175.  
  176.     /* Configure the Priority Group to 2 bits */
  177.     /* 2 bits for pre-emption priority(0-3 PreemptionPriority) and 2 bits for subpriority(0-3 SubPriority) */
  178.     //OLD: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  179. }
  180.  
  181. /*******************************************************************************
  182.  * Function Name  : SysTick_Configuration
  183.  * Description    : Setup SysTick Timer and Configure its Interrupt Priority
  184.  * Input          : None
  185.  * Output         : None
  186.  * Return         : None
  187.  *******************************************************************************/
  188. void SysTick_Configuration(void)
  189. {
  190.     /* Setup SysTick Timer for 1 msec interrupts */
  191.     if (SysTick_Config(SystemCoreClock / 1000))
  192.     {
  193.         /* Capture error */
  194.         while (1)
  195.         {
  196.         }
  197.     }
  198.  
  199.     /* Configure the SysTick Handler Priority: Preemption priority and subpriority */
  200.     NVIC_SetPriority(SysTick_IRQn, SYSTICK_IRQ_PRIORITY);   //OLD: NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x03, 0x00)
  201. }
  202.  
  203. /*******************************************************************************
  204. * Function Name  : Delay
  205. * Description    : Inserts a delay time.
  206. * Input          : nTime: specifies the delay time length, in milliseconds.
  207. * Output         : None
  208. * Return         : None
  209. *******************************************************************************/
  210. void Delay(__IO uint32_t nTime)
  211. {
  212.     TimingDelay = nTime;
  213.  
  214.     while (TimingDelay != 0x00);
  215. }
  216.  
  217. /*******************************************************************************
  218.  * Function Name  : Delay_Microsecond
  219.  * Description    : Inserts a delay time in microseconds using DWT.
  220.  * Input          : uSec: specifies the delay time length, in microseconds.
  221.  * Output         : None
  222.  * Return         : None
  223.  *******************************************************************************/
  224. void Delay_Microsecond(__IO uint32_t uSec)
  225. {
  226.     uint32_t DWT_CYCCNT = (((SystemCoreClock / 1000000) * uSec) + DWT->CYCCNT);
  227.     while (DWT_CYCCNT > DWT->CYCCNT);
  228. }
  229.  
  230. #if defined (USE_SPARK_CORE_V02)
  231. void RTC_Configuration(void)
  232. {
  233.     EXTI_InitTypeDef EXTI_InitStructure;
  234.     NVIC_InitTypeDef NVIC_InitStructure;
  235.  
  236.     /* Configure EXTI Line17(RTC Alarm) to generate an interrupt on rising edge */
  237.     EXTI_ClearITPendingBit(EXTI_Line17);
  238.     EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  239.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  240.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  241.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  242.     EXTI_Init(&EXTI_InitStructure);
  243.  
  244.     /* Enable the RTC Interrupt */
  245.     NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  246.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = RTC_IRQ_PRIORITY;            //OLD: 0x01
  247.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                               //OLD: 0x01
  248.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  249.     NVIC_Init(&NVIC_InitStructure);
  250.  
  251.     /* Enable the RTC Alarm Interrupt */
  252.     NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
  253.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = RTCALARM_IRQ_PRIORITY;       //OLD: 0x01
  254.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                               //OLD: 0x02
  255.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  256.     NVIC_Init(&NVIC_InitStructure);
  257.  
  258.     /* Check if the StandBy flag is set */
  259.     if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET)
  260.     {
  261.         /* System resumed from STANDBY mode */
  262.  
  263.         /* Clear StandBy flag */
  264.         PWR_ClearFlag(PWR_FLAG_SB);
  265.  
  266.         /* Wait for RTC APB registers synchronisation */
  267.         RTC_WaitForSynchro();
  268.  
  269.         BKP_WriteBackupRegister(BKP_DR9, 0xFFFF);
  270.  
  271.         /* No need to configure the RTC as the RTC configuration(clock source, enable,
  272.            prescaler,...) is kept after wake-up from STANDBY */
  273.     }
  274.     else
  275.     {
  276.         /* StandBy flag is not set */
  277.  
  278.         if(BKP_ReadBackupRegister(BKP_DR9) == 0xA5A5)
  279.         {
  280.             /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
  281.             PWR_EnterSTANDBYMode();
  282.         }
  283.  
  284.         /* Enable LSE */
  285.         RCC_LSEConfig(RCC_LSE_ON);
  286.  
  287.         /* Wait till LSE is ready */
  288.         while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  289.         {
  290.             //Do nothing
  291.         }
  292.  
  293.         /* Select LSE as RTC Clock Source */
  294.         RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  295.  
  296.         /* Enable RTC Clock */
  297.         RCC_RTCCLKCmd(ENABLE);
  298.  
  299.         /* Wait for RTC registers synchronization */
  300.         RTC_WaitForSynchro();
  301.  
  302.         /* Wait until last write operation on RTC registers has finished */
  303.         RTC_WaitForLastTask();
  304.  
  305.         /* Enable the RTC Second and RTC Alarm interrupt */
  306.         RTC_ITConfig(RTC_IT_SEC | RTC_IT_ALR, ENABLE);
  307.  
  308.         /* Wait until last write operation on RTC registers has finished */
  309.         RTC_WaitForLastTask();
  310.  
  311.         /* Set RTC prescaler: set RTC period to 1sec */
  312.         RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
  313.  
  314.         /* Wait until last write operation on RTC registers has finished */
  315.         RTC_WaitForLastTask();
  316.     }
  317. }
  318.  
  319. void Enter_STANDBY_Mode(void)
  320. {
  321.     BKP_WriteBackupRegister(BKP_DR9, 0xA5A5);
  322.  
  323.     NVIC_SystemReset();
  324. }
  325. #endif
  326.  
  327. void IWDG_Reset_Enable(uint32_t msTimeout)
  328. {
  329.     uint16_t Reload_Value;
  330.  
  331.     if(msTimeout > 10000)
  332.         msTimeout = 10000;  //Max IWDG timeout that can be set is 10 sec
  333.  
  334.     /* Enable write access to IWDG_PR and IWDG_RLR registers */
  335.     IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  336.  
  337.     /* IWDG counter clock: LSI/256 */
  338.     IWDG_SetPrescaler(IWDG_Prescaler_256);
  339.  
  340.     /* IWDG timeout may vary due to LSI frequency dispersion */
  341.     Reload_Value = (uint16_t)((msTimeout * 40) / 256); //Assuming LSI Frequency = 40000
  342.  
  343.     IWDG_SetReload(Reload_Value);
  344.  
  345.     /* Reload IWDG counter */
  346.     IWDG_ReloadCounter();
  347.  
  348.     /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
  349.     IWDG_Enable();
  350. }
  351.  
  352. /**
  353.   * @brief  Configures Dx GPIO.
  354.   * @param  Dx: Specifies the Dx to be configured.
  355.   * @retval None
  356.   */
  357. void DIO_Init(DIO_TypeDef Dx)
  358. {
  359.     GPIO_InitTypeDef  GPIO_InitStructure;
  360.  
  361.     /* Enable the GPIO_Dx Clock */
  362.     RCC_APB2PeriphClockCmd(DIO_CLK[Dx], ENABLE);
  363.  
  364.     /* Configure the GPIO_Dx pin */
  365.     GPIO_InitStructure.GPIO_Pin = DIO_PIN[Dx];
  366.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  367.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  368.     GPIO_Init(DIO_PORT[Dx], &GPIO_InitStructure);
  369.  
  370.     /* Set to Off State */
  371.     DIO_SetState(Dx, LOW);
  372. }
  373.  
  374. /**
  375.   * @brief  Turns selected Dx On/Off.
  376.   * @param  Dx: Specifies the Dx.
  377.   * @param  State: Set On or Off.
  378.   * @retval None
  379.   */
  380. DIO_Error_TypeDef DIO_SetState(DIO_TypeDef Dx, DIO_State_TypeDef State)
  381. {
  382.     if(Dx < 0 || Dx > Dn)
  383.         return FAIL;
  384.     else if(State == HIGH)
  385.         DIO_PORT[Dx]->BSRR = DIO_PIN[Dx];
  386.     else if(State == LOW)
  387.         DIO_PORT[Dx]->BRR = DIO_PIN[Dx];
  388.  
  389.     return OK;
  390. }
  391.  
  392. void UI_Timer_Configure(void)
  393. {
  394.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  395.     TIM_OCInitTypeDef TIM_OCInitStructure;
  396.  
  397.     /* Enable TIM1 clock */
  398.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  399.  
  400.     /* TIM1 Update Frequency = 72000000/7200/100 = 100Hz = 10ms */
  401.     /* TIM1_Prescaler: 7199 */
  402.     /* TIM1_Autoreload: 99 -> 100Hz = 10ms */
  403.     uint16_t TIM1_Prescaler = (SystemCoreClock / 10000) - 1;
  404.     uint16_t TIM1_Autoreload = (10000 / UI_TIMER_FREQUENCY) - 1;
  405.  
  406.     TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  407.  
  408.     /* Time Base Configuration */
  409.     TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  410.     TIM_TimeBaseStructure.TIM_Period = TIM1_Autoreload;
  411.     TIM_TimeBaseStructure.TIM_Prescaler = TIM1_Prescaler;
  412.     TIM_TimeBaseStructure.TIM_ClockDivision = 0x0000;
  413.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  414.  
  415.     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  416.  
  417.     TIM_OCStructInit(&TIM_OCInitStructure);
  418.  
  419.     /* PWM1 Mode configuration: Channel 1, 2 and 3 */
  420.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  421.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  422.     TIM_OCInitStructure.TIM_Pulse = 0x0000;
  423. #if defined (USE_SPARK_CORE_V01)
  424.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  425. #elif defined (USE_SPARK_CORE_V02)
  426.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  427. #endif
  428.     TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
  429.  
  430.     TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  431.     TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
  432.  
  433.     TIM_OC2Init(TIM1, &TIM_OCInitStructure);
  434.     TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Disable);
  435.  
  436.     TIM_OC3Init(TIM1, &TIM_OCInitStructure);
  437.     TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Disable);
  438.  
  439.     /* Output Compare Timing Mode configuration: Channel 4 */
  440.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
  441.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  442.     TIM_OCInitStructure.TIM_Pulse = 0x0000;
  443.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  444.  
  445.     TIM_OC4Init(TIM1, &TIM_OCInitStructure);
  446.     TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Disable);
  447.  
  448.     TIM_ARRPreloadConfig(TIM1, ENABLE);
  449.  
  450.     /* TIM1 enable counter */
  451.     TIM_Cmd(TIM1, ENABLE);
  452.  
  453.     /* Main Output Enable */
  454.     TIM_CtrlPWMOutputs(TIM1, ENABLE);
  455. }
  456.  
  457. #if defined (USE_SPARK_CORE_V02)
  458. void LED_SetRGBColor(uint32_t RGB_Color)
  459. {
  460.    
  461.  
  462.     LED_TIM_CCR[2] = (uint16_t)(((RGB_Color & 0xFF000) >> 16) * (TIM1->ARR + 1) / 255 ) / LED_DIM;  //LED3 -> Red Led
  463.     LED_TIM_CCR[3] = (uint16_t)(((RGB_Color & 0xFF00) >> 8) *(TIM1->ARR + 1) / 255 ) / LED_DIM;     //LED4 -> Green Led
  464.     LED_TIM_CCR[1] = (uint16_t)((RGB_Color & 0xFF) * (TIM1->ARR + 1) / 255 ) / LED_DIM;             //LED2 -> Blue Led
  465. }
  466.  
  467. void LED_SetSignalingColor(uint32_t RGB_Color)
  468. {
  469.     LED_TIM_CCR_SIGNAL[2] = (uint16_t)(((RGB_Color & 0xFF0000) >> 16) * (TIM1->ARR + 1) / 255 ) / LED_DIM;  //LED3 -> Red Led
  470.     LED_TIM_CCR_SIGNAL[3] = (uint16_t)(((RGB_Color & 0xFF00) >> 8) * (TIM1->ARR + 1) / 255 ) / LED_DIM; //LED4 -> Green Led
  471.     LED_TIM_CCR_SIGNAL[1] = (uint16_t)((RGB_Color & 0xFF) * (TIM1->ARR + 1) / 255) / LED_DIM;               //LED2 -> Blue Led
  472. }
  473.  
  474. void LED_Signaling_Start(void)
  475. {
  476.     LED_RGB_OVERRIDE = 1;
  477.  
  478.     LED_Off(LED_RGB);
  479. }
  480.  
  481. void LED_Signaling_Stop(void)
  482. {
  483.     LED_RGB_OVERRIDE = 0;
  484.  
  485.     LED_On(LED_RGB);
  486. }
  487. #endif
  488.  
  489. /**
  490.   * @brief  Configures LED GPIO.
  491.   * @param  Led: Specifies the Led to be configured.
  492.   *   This parameter can be one of following parameters:
  493.   *     @arg LED1, LED2, LED3, LED4
  494.   * @retval None
  495.   */
  496. void LED_Init(Led_TypeDef Led)
  497. {
  498.     GPIO_InitTypeDef  GPIO_InitStructure;
  499.  
  500.     /* Enable the GPIO_LED Clock */
  501.     RCC_APB2PeriphClockCmd(LED_CLK[Led], ENABLE);
  502.  
  503.     /* Configure the GPIO_LED pin as alternate function push-pull */
  504.     GPIO_InitStructure.GPIO_Pin = LED_PIN[Led];
  505. #if defined (USE_SPARK_CORE_V01)
  506.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  507. #elif defined (USE_SPARK_CORE_V02)
  508.     if(Led == LED_USER)
  509.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  510.     else
  511.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  512. #endif
  513.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  514.  
  515.     GPIO_Init(LED_PORT[Led], &GPIO_InitStructure);
  516. }
  517.  
  518. /**
  519.   * @brief  Turns selected LED On.
  520.   * @param  Led: Specifies the Led to be set on.
  521.   *   This parameter can be one of following parameters:
  522.   *     @arg LED1, LED2, LED_USER, LED_RGB
  523.   * @retval None
  524.   */
  525. void LED_On(Led_TypeDef Led)
  526. {
  527. #if defined (USE_SPARK_CORE_V01)
  528.     switch(Led)
  529.     {
  530.     case LED1:
  531.         TIM1->CCR1 = TIM1->ARR + 1;
  532.         break;
  533.  
  534.     case LED2:
  535.         TIM1->CCR2 = TIM1->ARR + 1;
  536.         break;
  537.     }
  538. #elif defined (USE_SPARK_CORE_V02)
  539.     switch(Led)
  540.     {
  541.     case LED_USER:
  542.         LED_PORT[Led]->BSRR = LED_PIN[Led];
  543.         break;
  544.  
  545.     case LED_RGB:   //LED_SetRGBColor() should be called first for this Case
  546.         if(LED_RGB_OVERRIDE == 0)
  547.         {
  548.             TIM1->CCR2 = LED_TIM_CCR[2];
  549.             TIM1->CCR3 = LED_TIM_CCR[3];
  550.             TIM1->CCR1 = LED_TIM_CCR[1];
  551.         }
  552.         else
  553.         {
  554.             TIM1->CCR2 = LED_TIM_CCR_SIGNAL[2];
  555.             TIM1->CCR3 = LED_TIM_CCR_SIGNAL[3];
  556.             TIM1->CCR1 = LED_TIM_CCR_SIGNAL[1];
  557.         }
  558.         break;
  559.     }
  560. #endif
  561. }
  562.  
  563. /**
  564.   * @brief  Turns selected LED Off.
  565.   * @param  Led: Specifies the Led to be set off.
  566.   *   This parameter can be one of following parameters:
  567.   *     @arg LED1, LED2, LED_USER, LED_RGB
  568.   * @retval None
  569.   */
  570. void LED_Off(Led_TypeDef Led)
  571. {
  572. #if defined (USE_SPARK_CORE_V01)
  573.     switch(Led)
  574.     {
  575.     case LED1:
  576.         TIM1->CCR1 = 0;
  577.         break;
  578.  
  579.     case LED2:
  580.         TIM1->CCR2 = 0;
  581.         break;
  582.     }
  583. #elif defined (USE_SPARK_CORE_V02)
  584.     switch(Led)
  585.     {
  586.     case LED_USER:
  587.         LED_PORT[Led]->BRR = LED_PIN[Led];
  588.         break;
  589.  
  590.     case LED_RGB:
  591.         TIM1->CCR2 = 0;
  592.         TIM1->CCR3 = 0;
  593.         TIM1->CCR1 = 0;
  594.         break;
  595.     }
  596. #endif
  597. }
  598.  
  599. /**
  600.   * @brief  Toggles the selected LED.
  601.   * @param  Led: Specifies the Led to be toggled.
  602.   *   This parameter can be one of following parameters:
  603.   *     @arg LED1, LED2, LED_USER, LED_RGB
  604.   * @retval None
  605.   */
  606. void LED_Toggle(Led_TypeDef Led)
  607. {
  608. #if defined (USE_SPARK_CORE_V01)
  609.     switch(Led)
  610.     {
  611.     case LED1:
  612.         TIM1->CCR1 ^= TIM1->ARR + 1;
  613.         break;
  614.  
  615.     case LED2:
  616.         TIM1->CCR2 ^= TIM1->ARR + 1;
  617.         break;
  618.     }
  619. #elif defined (USE_SPARK_CORE_V02)
  620.     switch(Led)
  621.     {
  622.     case LED_USER:
  623.         LED_PORT[Led]->ODR ^= LED_PIN[Led];
  624.         break;
  625.  
  626.     case LED_RGB://LED_SetRGBColor() and LED_On() should be called first for this Case
  627.         if(LED_RGB_OVERRIDE == 0)
  628.         {
  629.             TIM1->CCR2 ^= LED_TIM_CCR[2];
  630.             TIM1->CCR3 ^= LED_TIM_CCR[3];
  631.             TIM1->CCR1 ^= LED_TIM_CCR[1];
  632.         }
  633.         else
  634.         {
  635.             TIM1->CCR2 ^= LED_TIM_CCR_SIGNAL[2];
  636.             TIM1->CCR3 ^= LED_TIM_CCR_SIGNAL[3];
  637.             TIM1->CCR1 ^= LED_TIM_CCR_SIGNAL[1];
  638.         }
  639.         break;
  640.     }
  641. #endif
  642. }
  643.  
  644. /**
  645.   * @brief  Fades selected LED.
  646.   * @param  Led: Specifies the Led to be set on.
  647.   *   This parameter can be one of following parameters:
  648.   *     @arg LED1, LED2, LED_RGB
  649.   * @retval None
  650.   */
  651. void LED_Fade(Led_TypeDef Led)
  652. {
  653. #if defined (USE_SPARK_CORE_V01)
  654.     switch(Led)
  655.     {
  656.     case LED1:
  657.         if(LED_TIM_CCR[1] == 0)
  658.             delta1 = 0;
  659.         else if(TIM1->CCR1 == 0)
  660.             delta1 = 1;
  661.         else if(TIM1->CCR1 == TIM1->ARR + 1)
  662.             delta1 = -1;
  663.  
  664.         TIM1->CCR1 += delta1;
  665.         break;
  666.  
  667.     case LED2:
  668.         if(LED_TIM_CCR[2] == 0)
  669.             delta2 = 0;
  670.         else if(TIM1->CCR2 == 0)
  671.             delta2 = 1;
  672.         else if(TIM1->CCR2 == TIM1->ARR + 1)
  673.             delta2 = -1;
  674.  
  675.         TIM1->CCR2 += delta2;
  676.         break;
  677.     }
  678. #elif defined (USE_SPARK_CORE_V02)
  679.  
  680.     if(Led == LED_RGB)
  681.     {
  682.         if(LED_RGB_OVERRIDE == 0)
  683.         {
  684.  
  685.             if(TIM1->CCR4 % LED_DIM ==0)
  686.             {
  687.                 if(LED_TIM_CCR[2] == 0)
  688.                     delta2 = 0;
  689.                 else if(TIM1->CCR2 == 0)
  690.                     delta2 = 1;
  691.                 else if(TIM1->CCR2 >= (TIM1->ARR / LED_DIM) )
  692.                     delta2 = -1;
  693.            
  694.                 if(LED_TIM_CCR[3] == 0)
  695.                     delta3 = 0;
  696.                 else if(TIM1->CCR3 == 0)
  697.                     delta3 = 1;
  698.                 else if(TIM1->CCR3 >= (TIM1->ARR / LED_DIM) )
  699.                     delta3 = -1;
  700.            
  701.                 if(LED_TIM_CCR[1] == 0)
  702.                     delta1 = 0;
  703.                 else if(TIM1->CCR1 == 0)
  704.                     delta1 = 1;
  705.                 else if(TIM1->CCR1 >= (TIM1->ARR / LED_DIM) )
  706.                     delta1 = -1;
  707.  
  708.                 TIM1->CCR2 += delta2;
  709.                 TIM1->CCR3 += delta3;
  710.                 TIM1->CCR1 += delta1;  
  711.  
  712.             }
  713.  
  714.             if(TIM1->CCR4 == 0)
  715.                     delta4 = 1;
  716.             else if(TIM1->CCR4 == TIM1->ARR + 1) delta4 = -1;
  717.         }
  718.         else
  719.         {
  720.             if(TIM1->CCR4 % LED_DIM ==0)
  721.             {
  722.                 if(LED_TIM_CCR_SIGNAL[2] == 0)
  723.                     delta2 = 0;
  724.                 else if(TIM1->CCR2 == 0)
  725.                     delta2 = 1;
  726.                 else if(TIM1->CCR2 == TIM1->ARR + 1)
  727.                     delta2 = -1;
  728.  
  729.                 if(LED_TIM_CCR_SIGNAL[3] == 0)
  730.                     delta3 = 0;
  731.                 else if(TIM1->CCR3 == 0)
  732.                     delta3 = 1;
  733.                 else if(TIM1->CCR3 == TIM1->ARR + 1)
  734.                     delta3 = -1;
  735.  
  736.                 if(LED_TIM_CCR_SIGNAL[1] == 0)
  737.                     delta1 = 0;
  738.                 else if(TIM1->CCR1 == 0)
  739.                     delta1 = 1;
  740.                 else if(TIM1->CCR1 == TIM1->ARR + 1)
  741.                     delta1 = -1;
  742.  
  743.                 TIM1->CCR2 += delta2;
  744.                 TIM1->CCR3 += delta3;
  745.                 TIM1->CCR1 += delta1;
  746.             }
  747.  
  748.             if(TIM1->CCR4 == 0)
  749.                     delta4 = 1;
  750.             else if(TIM1->CCR4 == TIM1->ARR + 1) delta4 = -1;
  751.         }
  752.  
  753.        
  754.         TIM1->CCR4 += delta4;
  755.     }
  756. #endif
  757. }
  758.  
  759. /**
  760.   * @brief  Configures Button GPIO, EXTI Line and DEBOUNCE Timer.
  761.   * @param  Button: Specifies the Button to be configured.
  762.   *   This parameter can be one of following parameters:
  763.   *     @arg BUTTON1: Button1
  764.   *     @arg BUTTON2: Button2
  765.   * @param  Button_Mode: Specifies Button mode.
  766.   *   This parameter can be one of following parameters:
  767.   *     @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  768.   *     @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  769.   *                     generation capability
  770.   * @retval None
  771.   */
  772. void BUTTON_Init(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode)
  773. {
  774.     GPIO_InitTypeDef GPIO_InitStructure;
  775.     NVIC_InitTypeDef NVIC_InitStructure;
  776.  
  777.     /* Enable the BUTTON Clock */
  778.     RCC_APB2PeriphClockCmd(BUTTON_CLK[Button] | RCC_APB2Periph_AFIO, ENABLE);
  779.  
  780.     /* Configure Button pin as input floating */
  781.     GPIO_InitStructure.GPIO_Mode = BUTTON_GPIO_MODE[Button];
  782.     GPIO_InitStructure.GPIO_Pin = BUTTON_PIN[Button];
  783.     GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStructure);
  784.  
  785.     if (Button_Mode == BUTTON_MODE_EXTI)
  786.     {
  787.         /* Disable TIM1 CC4 Interrupt */
  788.         TIM_ITConfig(TIM1, TIM_IT_CC4, DISABLE);
  789.  
  790.         /* Enable the TIM1 Interrupt */
  791.         NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
  792.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIM1_CC_IRQ_PRIORITY;    //OLD: 0x02
  793.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                           //OLD: 0x00
  794.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  795.  
  796.         NVIC_Init(&NVIC_InitStructure);
  797.  
  798.         /* Enable the Button EXTI Interrupt */
  799.         NVIC_InitStructure.NVIC_IRQChannel = BUTTON_IRQn[Button];
  800.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = EXTI2_IRQ_PRIORITY;      //OLD: 0x02
  801.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                           //OLD: 0x01
  802.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  803.  
  804.         NVIC_Init(&NVIC_InitStructure);
  805.  
  806.         BUTTON_EXTI_Config(Button, ENABLE);
  807.     }
  808. }
  809.  
  810. void BUTTON_EXTI_Config(Button_TypeDef Button, FunctionalState NewState)
  811. {
  812.     EXTI_InitTypeDef EXTI_InitStructure;
  813.  
  814.     /* Connect Button EXTI Line to Button GPIO Pin */
  815.     GPIO_EXTILineConfig(BUTTON_PORT_SOURCE[Button], BUTTON_PIN_SOURCE[Button]);
  816.  
  817.     /* Clear the EXTI line pending flag */
  818.     EXTI_ClearFlag(BUTTON_EXTI_LINE[Button]);
  819.  
  820.     /* Configure Button EXTI line */
  821.     EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE[Button];
  822.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  823.     EXTI_InitStructure.EXTI_Trigger = BUTTON_EXTI_TRIGGER[Button];
  824.     EXTI_InitStructure.EXTI_LineCmd = NewState;
  825.     EXTI_Init(&EXTI_InitStructure);
  826. }
  827.  
  828. /**
  829.   * @brief  Returns the selected Button non-filtered state.
  830.   * @param  Button: Specifies the Button to be checked.
  831.   *   This parameter can be one of following parameters:
  832.   *     @arg BUTTON1: Button1
  833.   *     @arg BUTTON2: Button2
  834.   * @retval Actual Button Pressed state.
  835.   */
  836. uint8_t BUTTON_GetState(Button_TypeDef Button)
  837. {
  838.     return GPIO_ReadInputDataBit(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  839. }
  840.  
  841. /**
  842.   * @brief  Returns the selected Button Debounced Time.
  843.   * @param  Button: Specifies the Button to be checked.
  844.   *   This parameter can be one of following parameters:
  845.   *     @arg BUTTON1: Button1
  846.   *     @arg BUTTON2: Button2
  847.   * @retval Button Debounced time in millisec.
  848.   */
  849. uint16_t BUTTON_GetDebouncedTime(Button_TypeDef Button)
  850. {
  851.     return BUTTON_DEBOUNCED_TIME[Button];
  852. }
  853.  
  854. void BUTTON_ResetDebouncedState(Button_TypeDef Button)
  855. {
  856.     BUTTON_DEBOUNCED_TIME[Button] = 0;
  857. }
  858.  
  859. /**
  860.  * @brief  Initialize the CC3000 - CS and ENABLE lines.
  861.  * @param  None
  862.  * @retval None
  863.  */
  864. void CC3000_WIFI_Init(void)
  865. {
  866.     GPIO_InitTypeDef GPIO_InitStructure;
  867.  
  868.     /* CC3000_WIFI_CS_GPIO and CC3000_WIFI_EN_GPIO Peripheral clock enable */
  869.     RCC_APB2PeriphClockCmd(CC3000_WIFI_CS_GPIO_CLK | CC3000_WIFI_EN_GPIO_CLK, ENABLE);
  870.  
  871.     /* Configure CC3000_WIFI pins: CS */
  872.     GPIO_InitStructure.GPIO_Pin = CC3000_WIFI_CS_PIN;
  873.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  874.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  875.     GPIO_Init(CC3000_WIFI_CS_GPIO_PORT, &GPIO_InitStructure);
  876.  
  877.     /* Deselect CC3000 */
  878.     CC3000_CS_HIGH();
  879.  
  880.     /* Configure CC3000_WIFI pins: Enable */
  881.     GPIO_InitStructure.GPIO_Pin = CC3000_WIFI_EN_PIN;
  882.     GPIO_Init(CC3000_WIFI_EN_GPIO_PORT, &GPIO_InitStructure);
  883.  
  884.     /* Disable CC3000 */
  885.     CC3000_Write_Enable_Pin(WLAN_DISABLE);
  886. }
  887.  
  888. /**
  889.  * @brief  Initialize and configure the SPI peripheral used by CC3000.
  890.  * @param  None
  891.  * @retval None
  892.  */
  893. void CC3000_SPI_Init(void)
  894. {
  895.     GPIO_InitTypeDef GPIO_InitStructure;
  896.     SPI_InitTypeDef SPI_InitStructure;
  897.  
  898.     /* CC3000_SPI_SCK_GPIO, CC3000_SPI_MOSI_GPIO and CC3000_SPI_MISO_GPIO Peripheral clock enable */
  899.     RCC_APB2PeriphClockCmd(CC3000_SPI_SCK_GPIO_CLK | CC3000_SPI_MOSI_GPIO_CLK | CC3000_SPI_MISO_GPIO_CLK, ENABLE);
  900.  
  901.     /* CC3000_SPI Peripheral clock enable */
  902.     CC3000_SPI_CLK_CMD(CC3000_SPI_CLK, ENABLE);
  903.  
  904.     /* Configure CC3000_SPI pins: SCK */
  905.     GPIO_InitStructure.GPIO_Pin = CC3000_SPI_SCK_PIN;
  906.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  907.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  908.     GPIO_Init(CC3000_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  909.  
  910.     /* Configure CC3000_SPI pins: MOSI */
  911.     GPIO_InitStructure.GPIO_Pin = CC3000_SPI_MOSI_PIN;
  912.     GPIO_Init(CC3000_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  913.  
  914.     /* Configure CC3000_SPI pins: MISO */
  915.     GPIO_InitStructure.GPIO_Pin = CC3000_SPI_MISO_PIN;
  916.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  917.     GPIO_Init(CC3000_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  918.  
  919.     /* CC3000_SPI Config */
  920.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  921.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  922.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  923.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  924.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  925.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  926.     SPI_InitStructure.SPI_BaudRatePrescaler = CC3000_SPI_BAUDRATE_PRESCALER;
  927.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  928.     SPI_InitStructure.SPI_CRCPolynomial = 7;
  929.     SPI_Init(CC3000_SPI, &SPI_InitStructure);
  930.  
  931.     CC3000_SPI_CR = CC3000_SPI->CR1;
  932. }
  933.  
  934. /**
  935.  * @brief  Configure the DMA Peripheral used to handle CC3000 communication via SPI.
  936.  * @param  None
  937.  * @retval None
  938.  */
  939. void CC3000_DMA_Config(CC3000_DMADirection_TypeDef Direction, uint8_t* buffer, uint16_t NumData)
  940. {
  941.     DMA_InitTypeDef DMA_InitStructure;
  942.  
  943.     RCC_AHBPeriphClockCmd(CC3000_SPI_DMA_CLK, ENABLE);
  944.  
  945.     DMA_InitStructure.DMA_PeripheralBaseAddr = CC3000_SPI_DR_BASE;
  946.     DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) buffer;
  947.     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  948.     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  949.     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  950.     DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  951.     DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  952.     DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  953.     DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  954.  
  955.     /* DMA used for Reception */
  956.     if (Direction == CC3000_DMA_RX)
  957.     {
  958.         DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  959.         DMA_InitStructure.DMA_BufferSize = NumData;
  960.         DMA_DeInit(CC3000_SPI_RX_DMA_CHANNEL );
  961.         DMA_Init(CC3000_SPI_RX_DMA_CHANNEL, &DMA_InitStructure);
  962.     }
  963.     /* DMA used for Transmission */
  964.     else if (Direction == CC3000_DMA_TX)
  965.     {
  966.         DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  967.         DMA_InitStructure.DMA_BufferSize = NumData;
  968.         DMA_DeInit(CC3000_SPI_TX_DMA_CHANNEL );
  969.         DMA_Init(CC3000_SPI_TX_DMA_CHANNEL, &DMA_InitStructure);
  970.     }
  971. }
  972.  
  973. void CC3000_SPI_DMA_Init(void)
  974. {
  975.     NVIC_InitTypeDef NVIC_InitStructure;
  976.  
  977.     /* Configure and enable SPI DMA TX Channel interrupt */
  978.     NVIC_InitStructure.NVIC_IRQChannel = CC3000_SPI_TX_DMA_IRQn;
  979.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = DMA1_CHANNEL5_IRQ_PRIORITY;  //OLD: 0x00
  980.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                               //OLD: 0x00
  981.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  982.     NVIC_Init(&NVIC_InitStructure);
  983.  
  984.     CC3000_SPI_Init();
  985.  
  986.     /* Configure DMA Peripheral but don't send data*/
  987.     CC3000_DMA_Config(CC3000_DMA_RX, (uint8_t*) wlan_rx_buffer, 0);
  988.     CC3000_DMA_Config(CC3000_DMA_TX, (uint8_t*) wlan_tx_buffer, 0);
  989.  
  990.     /* Enable SPI DMA TX Channel Transfer Complete Interrupt */
  991.     DMA_ITConfig(CC3000_SPI_TX_DMA_CHANNEL, DMA_IT_TC, ENABLE);
  992.  
  993.     /* Enable SPI DMA request */
  994.     SPI_I2S_DMACmd(CC3000_SPI, SPI_I2S_DMAReq_Rx, ENABLE);
  995.     SPI_I2S_DMACmd(CC3000_SPI, SPI_I2S_DMAReq_Tx, ENABLE);
  996.  
  997.     /* Enable CC3000_SPI */
  998.     SPI_Cmd(CC3000_SPI, ENABLE);
  999.  
  1000.     /* Enable DMA RX Channel */
  1001.     DMA_Cmd(CC3000_SPI_RX_DMA_CHANNEL, ENABLE);
  1002.     /* Enable DMA TX Channel */
  1003.     DMA_Cmd(CC3000_SPI_TX_DMA_CHANNEL, ENABLE);
  1004. }
  1005.  
  1006. void CC3000_SPI_DMA_Channels(FunctionalState NewState)
  1007. {
  1008.     /* Enable/Disable DMA RX Channel */
  1009.     DMA_Cmd(CC3000_SPI_RX_DMA_CHANNEL, NewState);
  1010.     /* Enable/Disable DMA TX Channel */
  1011.     DMA_Cmd(CC3000_SPI_TX_DMA_CHANNEL, NewState);
  1012. }
  1013.  
  1014. /* Select CC3000: ChipSelect pin low */
  1015. void CC3000_CS_LOW(void)
  1016. {
  1017.     CC3000_SPI->CR1 &= ((uint16_t)0xFFBF);
  1018.     sFLASH_CS_HIGH();
  1019.     CC3000_SPI->CR1 = CC3000_SPI_CR | ((uint16_t)0x0040);
  1020.     GPIO_ResetBits(CC3000_WIFI_CS_GPIO_PORT, CC3000_WIFI_CS_PIN);
  1021. }
  1022.  
  1023. /* Deselect CC3000: ChipSelect pin high */
  1024. void CC3000_CS_HIGH(void)
  1025. {
  1026.     GPIO_SetBits(CC3000_WIFI_CS_GPIO_PORT, CC3000_WIFI_CS_PIN);
  1027. }
  1028.  
  1029. /* CC3000 Hardware related callbacks passed to wlan_init */
  1030. long CC3000_Read_Interrupt_Pin(void)
  1031. {
  1032.     return GPIO_ReadInputDataBit(CC3000_WIFI_INT_GPIO_PORT, CC3000_WIFI_INT_PIN );
  1033. }
  1034.  
  1035. void CC3000_Interrupt_Enable(void)
  1036. {
  1037.     GPIO_InitTypeDef GPIO_InitStructure;
  1038.     EXTI_InitTypeDef EXTI_InitStructure;
  1039.     NVIC_InitTypeDef NVIC_InitStructure;
  1040.  
  1041.     /* CC3000_WIFI_INT_GPIO and AFIO Peripheral clock enable */
  1042.     RCC_APB2PeriphClockCmd(CC3000_WIFI_INT_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
  1043.  
  1044.     /* Configure CC3000_WIFI pins: Interrupt */
  1045.     GPIO_InitStructure.GPIO_Pin = CC3000_WIFI_INT_PIN;
  1046.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  1047.     GPIO_Init(CC3000_WIFI_INT_GPIO_PORT, &GPIO_InitStructure);
  1048.  
  1049.     /* Select the CC3000_WIFI_INT GPIO pin used as EXTI Line */
  1050.     GPIO_EXTILineConfig(CC3000_WIFI_INT_EXTI_PORT_SOURCE, CC3000_WIFI_INT_EXTI_PIN_SOURCE );
  1051.  
  1052.     /* Clear the EXTI line pending flag */
  1053.     EXTI_ClearFlag(CC3000_WIFI_INT_EXTI_LINE );
  1054.  
  1055.     /* Configure and Enable CC3000_WIFI_INT EXTI line */
  1056.     EXTI_InitStructure.EXTI_Line = CC3000_WIFI_INT_EXTI_LINE;
  1057.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  1058.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  1059.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  1060.     EXTI_Init(&EXTI_InitStructure);
  1061.  
  1062.     /* Enable and set CC3000_WIFI_INT EXTI Interrupt to the lowest priority */
  1063.     NVIC_InitStructure.NVIC_IRQChannel = CC3000_WIFI_INT_EXTI_IRQn;
  1064.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = EXTI15_10_IRQ_PRIORITY;      //OLD: 0x00
  1065.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                               //OLD: 0x01
  1066.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  1067.     NVIC_Init(&NVIC_InitStructure);
  1068. }
  1069.  
  1070. void CC3000_Interrupt_Disable(void)
  1071. {
  1072.     EXTI_InitTypeDef EXTI_InitStructure;
  1073.     NVIC_InitTypeDef NVIC_InitStructure;
  1074.  
  1075.     /* Disable CC3000_WIFI_INT EXTI Interrupt */
  1076.     NVIC_InitStructure.NVIC_IRQChannel = CC3000_WIFI_INT_EXTI_IRQn;
  1077.     NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  1078.     NVIC_Init(&NVIC_InitStructure);
  1079.  
  1080.     /* Disable CC3000_WIFI_INT EXTI line */
  1081.     EXTI_InitStructure.EXTI_Line = CC3000_WIFI_INT_EXTI_LINE;
  1082.     EXTI_InitStructure.EXTI_LineCmd = DISABLE;
  1083.     EXTI_Init(&EXTI_InitStructure);
  1084. }
  1085.  
  1086. void CC3000_Write_Enable_Pin(unsigned char val)
  1087. {
  1088.     /* Set WLAN Enable/Disable */
  1089.     if (val != WLAN_DISABLE)
  1090.     {
  1091.         GPIO_SetBits(CC3000_WIFI_EN_GPIO_PORT, CC3000_WIFI_EN_PIN );
  1092.     }
  1093.     else
  1094.     {
  1095.         GPIO_ResetBits(CC3000_WIFI_EN_GPIO_PORT, CC3000_WIFI_EN_PIN );
  1096.     }
  1097. }
  1098.  
  1099. /**
  1100.   * @brief  DeInitializes the peripherals used by the SPI FLASH driver.
  1101.   * @param  None
  1102.   * @retval None
  1103.   */
  1104. void sFLASH_SPI_DeInit(void)
  1105. {
  1106.     GPIO_InitTypeDef GPIO_InitStructure;
  1107.  
  1108.     /* Disable the sFLASH_SPI  */
  1109.     SPI_Cmd(sFLASH_SPI, DISABLE);
  1110.  
  1111.     /* DeInitializes the sFLASH_SPI */
  1112.     SPI_I2S_DeInit(sFLASH_SPI);
  1113.  
  1114.     /* sFLASH_SPI Peripheral clock disable */
  1115.     sFLASH_SPI_CLK_CMD(sFLASH_SPI_CLK, DISABLE);
  1116.  
  1117.     /* Configure sFLASH_SPI pins: SCK */
  1118.     GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_SCK_PIN;
  1119.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  1120.     GPIO_Init(sFLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  1121.  
  1122.     /* Configure sFLASH_SPI pins: MISO */
  1123.     GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MISO_PIN;
  1124.     GPIO_Init(sFLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  1125.  
  1126.     /* Configure sFLASH_SPI pins: MOSI */
  1127.     GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MOSI_PIN;
  1128.     GPIO_Init(sFLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  1129.  
  1130.     /* Configure sFLASH_MEM_CS_PIN pin: sFLASH CS pin */
  1131.     GPIO_InitStructure.GPIO_Pin = sFLASH_MEM_CS_PIN;
  1132.     GPIO_Init(sFLASH_MEM_CS_GPIO_PORT, &GPIO_InitStructure);
  1133. }
  1134.  
  1135. /**
  1136.   * @brief  Initializes the peripherals used by the SPI FLASH driver.
  1137.   * @param  None
  1138.   * @retval None
  1139.   */
  1140. void sFLASH_SPI_Init(void)
  1141. {
  1142.     GPIO_InitTypeDef GPIO_InitStructure;
  1143.     SPI_InitTypeDef  SPI_InitStructure;
  1144.  
  1145.     /* sFLASH_MEM_CS_GPIO, sFLASH_SPI_MOSI_GPIO, sFLASH_SPI_MISO_GPIO
  1146.        and sFLASH_SPI_SCK_GPIO Periph clock enable */
  1147.     RCC_APB2PeriphClockCmd(sFLASH_MEM_CS_GPIO_CLK | sFLASH_SPI_MOSI_GPIO_CLK | sFLASH_SPI_MISO_GPIO_CLK |
  1148.                          sFLASH_SPI_SCK_GPIO_CLK, ENABLE);
  1149.  
  1150.     /* sFLASH_SPI Periph clock enable */
  1151.     sFLASH_SPI_CLK_CMD(sFLASH_SPI_CLK, ENABLE);
  1152.  
  1153.     /* Configure sFLASH_SPI pins: SCK */
  1154.     GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_SCK_PIN;
  1155.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  1156.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  1157.     GPIO_Init(sFLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  1158.  
  1159.     /* Configure sFLASH_SPI pins: MOSI */
  1160.     GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MOSI_PIN;
  1161.     GPIO_Init(sFLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  1162.  
  1163.     /* Configure sFLASH_SPI pins: MISO */
  1164.     GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MISO_PIN;
  1165.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  1166.     GPIO_Init(sFLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  1167.  
  1168.     /* Configure sFLASH_MEM_CS_PIN pin: sFLASH CS pin */
  1169.     GPIO_InitStructure.GPIO_Pin = sFLASH_MEM_CS_PIN;
  1170.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  1171.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  1172.     GPIO_Init(sFLASH_MEM_CS_GPIO_PORT, &GPIO_InitStructure);
  1173.  
  1174.     /*!< Deselect the FLASH: Chip Select high */
  1175.     sFLASH_CS_HIGH();
  1176.  
  1177.     /*!< SPI configuration */
  1178.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  1179.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  1180.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  1181.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  1182.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  1183.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  1184.     SPI_InitStructure.SPI_BaudRatePrescaler = sFLASH_SPI_BAUDRATE_PRESCALER;
  1185.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  1186.     SPI_InitStructure.SPI_CRCPolynomial = 7;
  1187.     SPI_Init(sFLASH_SPI, &SPI_InitStructure);
  1188.  
  1189.     sFLASH_SPI_CR = sFLASH_SPI->CR1;
  1190.  
  1191.     /*!< Enable the sFLASH_SPI  */
  1192.     SPI_Cmd(sFLASH_SPI, ENABLE);
  1193. }
  1194.  
  1195. /* Select sFLASH: Chip Select pin low */
  1196. void sFLASH_CS_LOW(void)
  1197. {
  1198.     sFLASH_SPI->CR1 &= ((uint16_t)0xFFBF);
  1199.     CC3000_CS_HIGH();
  1200.     sFLASH_SPI->CR1 = sFLASH_SPI_CR | ((uint16_t)0x0040);
  1201.     GPIO_ResetBits(sFLASH_MEM_CS_GPIO_PORT, sFLASH_MEM_CS_PIN);
  1202. }
  1203.  
  1204. /* Deselect sFLASH: Chip Select pin high */
  1205. void sFLASH_CS_HIGH(void)
  1206. {
  1207.     GPIO_SetBits(sFLASH_MEM_CS_GPIO_PORT, sFLASH_MEM_CS_PIN);
  1208. }
  1209.  
  1210. /*******************************************************************************
  1211. * Function Name  : USB_Disconnect_Config
  1212. * Description    : Disconnect pin configuration
  1213. * Input          : None.
  1214. * Return         : None.
  1215. *******************************************************************************/
  1216. void USB_Disconnect_Config(void)
  1217. {
  1218.     GPIO_InitTypeDef GPIO_InitStructure;
  1219.  
  1220.     /* Enable USB_DISCONNECT GPIO clock */
  1221.     RCC_APB2PeriphClockCmd(USB_DISCONNECT_GPIO_CLK, ENABLE);
  1222.  
  1223.     /* USB_DISCONNECT_PIN used as USB pull-up */
  1224.     GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
  1225.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  1226.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  1227.     GPIO_Init(USB_DISCONNECT_GPIO_PORT, &GPIO_InitStructure);
  1228. }
  1229.  
  1230. /*******************************************************************************
  1231. * Function Name  : Set_USBClock
  1232. * Description    : Configures USB Clock input (48MHz)
  1233. * Input          : None.
  1234. * Return         : None.
  1235. *******************************************************************************/
  1236. void Set_USBClock(void)
  1237. {
  1238.     /* Select USBCLK source */
  1239.     RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
  1240.  
  1241.     /* Enable the USB clock */
  1242.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
  1243. }
  1244.  
  1245. /*******************************************************************************
  1246. * Function Name  : Enter_LowPowerMode
  1247. * Description    : Power-off system clocks and power while entering suspend mode
  1248. * Input          : None.
  1249. * Return         : None.
  1250. *******************************************************************************/
  1251. void Enter_LowPowerMode(void)
  1252. {
  1253.     /* Set the device state to suspend */
  1254.     bDeviceState = SUSPENDED;
  1255. }
  1256.  
  1257. /*******************************************************************************
  1258. * Function Name  : Leave_LowPowerMode
  1259. * Description    : Restores system clocks and power while exiting suspend mode
  1260. * Input          : None.
  1261. * Return         : None.
  1262. *******************************************************************************/
  1263. void Leave_LowPowerMode(void)
  1264. {
  1265.     DEVICE_INFO *pInfo = &Device_Info;
  1266.  
  1267.     /* Set the device state to the correct state */
  1268.     if (pInfo->Current_Configuration != 0)
  1269.     {
  1270.         /* Device configured */
  1271.         bDeviceState = CONFIGURED;
  1272.     }
  1273.     else
  1274.     {
  1275.         bDeviceState = ATTACHED;
  1276.     }
  1277. }
  1278.  
  1279. /*******************************************************************************
  1280. * Function Name  : USB_Interrupts_Config
  1281. * Description    : Configures the USB interrupts
  1282. * Input          : None.
  1283. * Return         : None.
  1284. *******************************************************************************/
  1285. void USB_Interrupts_Config(void)
  1286. {
  1287.     NVIC_InitTypeDef NVIC_InitStructure;
  1288.  
  1289.     /* Enable the USB interrupt */
  1290.     NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  1291.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USB_LP_IRQ_PRIORITY;         //OLD: 0x01
  1292.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                               //OLD: 0x00
  1293.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  1294.     NVIC_Init(&NVIC_InitStructure);
  1295. }
  1296.  
  1297. /*******************************************************************************
  1298. * Function Name  : USB_Cable_Config
  1299. * Description    : Software Connection/Disconnection of USB Cable
  1300. * Input          : None.
  1301. * Return         : Status
  1302. *******************************************************************************/
  1303. void USB_Cable_Config (FunctionalState NewState)
  1304. {
  1305.     if (NewState != DISABLE)
  1306.     {
  1307.         GPIO_ResetBits(USB_DISCONNECT_GPIO_PORT, USB_DISCONNECT_PIN);
  1308.     }
  1309.     else
  1310.     {
  1311.         GPIO_SetBits(USB_DISCONNECT_GPIO_PORT, USB_DISCONNECT_PIN);
  1312.     }
  1313. }
  1314.  
  1315. void Load_SystemFlags(void)
  1316. {
  1317.     uint32_t Address = SYSTEM_FLAGS_ADDRESS;
  1318.  
  1319.     if(!USE_SYSTEM_FLAGS)
  1320.         return;
  1321.  
  1322.     CORE_FW_Version_SysFlag = (*(__IO uint16_t*) Address);
  1323.     Address += 2;
  1324.  
  1325.     NVMEM_SPARK_Reset_SysFlag = (*(__IO uint16_t*) Address);
  1326.     Address += 2;
  1327.  
  1328.     FLASH_OTA_Update_SysFlag = (*(__IO uint16_t*) Address);
  1329.     Address += 2;
  1330.  
  1331.     OTA_FLASHED_Status_SysFlag = (*(__IO uint16_t*) Address);
  1332.     Address += 2;
  1333. }
  1334.  
  1335. void Save_SystemFlags(void)
  1336. {
  1337.     uint32_t Address = SYSTEM_FLAGS_ADDRESS;
  1338.     FLASH_Status FLASHStatus = FLASH_COMPLETE;
  1339.  
  1340.     if(!USE_SYSTEM_FLAGS)
  1341.         return;
  1342.  
  1343.     /* Unlock the Flash Program Erase Controller */
  1344.     FLASH_Unlock();
  1345.  
  1346.     /* Clear All pending flags */
  1347.     FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
  1348.  
  1349.     /* Erase the Internal Flash pages */
  1350.     FLASHStatus = FLASH_ErasePage(SYSTEM_FLAGS_ADDRESS);
  1351.     while(FLASHStatus != FLASH_COMPLETE);
  1352.  
  1353.     /* Program CORE_FW_Version_SysFlag */
  1354.     FLASHStatus = FLASH_ProgramHalfWord(Address, CORE_FW_Version_SysFlag);
  1355.     while(FLASHStatus != FLASH_COMPLETE);
  1356.     Address += 2;
  1357.  
  1358.     /* Program NVMEM_SPARK_Reset_SysFlag */
  1359.     FLASHStatus = FLASH_ProgramHalfWord(Address, NVMEM_SPARK_Reset_SysFlag);
  1360.     while(FLASHStatus != FLASH_COMPLETE);
  1361.     Address += 2;
  1362.  
  1363.     /* Program FLASH_OTA_Update_SysFlag */
  1364.     FLASHStatus = FLASH_ProgramHalfWord(Address, FLASH_OTA_Update_SysFlag);
  1365.     while(FLASHStatus != FLASH_COMPLETE);
  1366.     Address += 2;
  1367.  
  1368.     /* Program OTA_FLASHED_Status_SysFlag */
  1369.     FLASHStatus = FLASH_ProgramHalfWord(Address, OTA_FLASHED_Status_SysFlag);
  1370.     while(FLASHStatus != FLASH_COMPLETE);
  1371.     Address += 2;
  1372.  
  1373.     /* Locks the FLASH Program Erase Controller */
  1374.     FLASH_Lock();
  1375. }
  1376.  
  1377. void FLASH_WriteProtection_Enable(uint32_t FLASH_Pages)
  1378. {
  1379.     /* Get pages write protection status */
  1380.     WRPR_Value = FLASH_GetWriteProtectionOptionByte();
  1381.  
  1382.     /* Check if desired pages are not yet write protected */
  1383.     if(((~WRPR_Value) & FLASH_Pages ) != FLASH_Pages)
  1384.     {
  1385.         /* Get current write protected pages and the new pages to be protected */
  1386.         Flash_Pages_Protected =  (~WRPR_Value) | FLASH_Pages;
  1387.  
  1388.         /* Unlock the Flash Program Erase controller */
  1389.         FLASH_Unlock();
  1390.  
  1391.         /* Erase all the option Bytes because if a program operation is
  1392.           performed on a protected page, the Flash memory returns a
  1393.           protection error */
  1394.         FLASHStatus = FLASH_EraseOptionBytes();
  1395.  
  1396.         /* Enable the pages write protection */
  1397.         FLASHStatus = FLASH_EnableWriteProtection(Flash_Pages_Protected);
  1398.  
  1399.         /* Generate System Reset to load the new option byte values */
  1400.         NVIC_SystemReset();
  1401.     }
  1402. }
  1403.  
  1404. void FLASH_WriteProtection_Disable(uint32_t FLASH_Pages)
  1405. {
  1406.     /* Get pages write protection status */
  1407.     WRPR_Value = FLASH_GetWriteProtectionOptionByte();
  1408.  
  1409.     /* Check if desired pages are already write protected */
  1410.     if((WRPR_Value | (~FLASH_Pages)) != 0xFFFFFFFF)
  1411.     {
  1412.         /* Get pages already write protected */
  1413.         Flash_Pages_Protected = ~(WRPR_Value | FLASH_Pages);
  1414.  
  1415.         /* Unlock the Flash Program Erase controller */
  1416.         FLASH_Unlock();
  1417.  
  1418.         /* Erase all the option Bytes */
  1419.         FLASHStatus = FLASH_EraseOptionBytes();
  1420.  
  1421.         /* Check if there is write protected pages */
  1422.         if(Flash_Pages_Protected != 0x0)
  1423.         {
  1424.             /* Restore write protected pages */
  1425.             FLASHStatus = FLASH_EnableWriteProtection(Flash_Pages_Protected);
  1426.         }
  1427.  
  1428.         /* Generate System Reset to load the new option byte values */
  1429.         NVIC_SystemReset();
  1430.     }
  1431. }
  1432.  
  1433. void FLASH_Backup(uint32_t sFLASH_Address)
  1434. {
  1435. #ifdef SPARK_SFLASH_ENABLE
  1436.  
  1437.     /* Initialize SPI Flash */
  1438.     sFLASH_Init();
  1439.  
  1440.     /* Define the number of External Flash pages to be erased */
  1441.     NbrOfPage = EXTERNAL_FLASH_BLOCK_SIZE / sFLASH_PAGESIZE;
  1442.  
  1443.     /* Erase the SPI Flash pages */
  1444.     for (EraseCounter = 0; (EraseCounter < NbrOfPage); EraseCounter++)
  1445.     {
  1446.         sFLASH_EraseSector(sFLASH_Address + (sFLASH_PAGESIZE * EraseCounter));
  1447.     }
  1448.  
  1449.     Internal_Flash_Address = CORE_FW_ADDRESS;
  1450.     External_Flash_Address = sFLASH_Address;
  1451.  
  1452.     /* Program External Flash */
  1453.     while (Internal_Flash_Address < INTERNAL_FLASH_END_ADDRESS)
  1454.     {
  1455.         /* Read data from Internal Flash memory */
  1456.         Internal_Flash_Data = (*(__IO uint32_t*) Internal_Flash_Address);
  1457.         Internal_Flash_Address += 4;
  1458.  
  1459.         /* Program Word to SPI Flash memory */
  1460.         External_Flash_Data[0] = (uint8_t)(Internal_Flash_Data & 0xFF);
  1461.         External_Flash_Data[1] = (uint8_t)((Internal_Flash_Data & 0xFF00) >> 8);
  1462.         External_Flash_Data[2] = (uint8_t)((Internal_Flash_Data & 0xFF0000) >> 16);
  1463.         External_Flash_Data[3] = (uint8_t)((Internal_Flash_Data & 0xFF000000) >> 24);
  1464.         //OR
  1465.         //*((uint32_t *)External_Flash_Data) = Internal_Flash_Data;
  1466.         sFLASH_WriteBuffer(External_Flash_Data, External_Flash_Address, 4);
  1467.         External_Flash_Address += 4;
  1468.     }
  1469.  
  1470. #endif
  1471. }
  1472.  
  1473. void FLASH_Restore(uint32_t sFLASH_Address)
  1474. {
  1475. #ifdef SPARK_SFLASH_ENABLE
  1476.  
  1477.     /* Initialize SPI Flash */
  1478.     sFLASH_Init();
  1479.  
  1480.     FLASHStatus = FLASH_COMPLETE;
  1481.  
  1482.     /* Unlock the Flash Program Erase Controller */
  1483.     FLASH_Unlock();
  1484.  
  1485.     /* Define the number of Internal Flash pages to be erased */
  1486.     NbrOfPage = (INTERNAL_FLASH_END_ADDRESS - CORE_FW_ADDRESS) / INTERNAL_FLASH_PAGE_SIZE;
  1487.  
  1488.     /* Clear All pending flags */
  1489.     FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
  1490.  
  1491.     /* Erase the Internal Flash pages */
  1492.     for (EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
  1493.     {
  1494.         FLASHStatus = FLASH_ErasePage(CORE_FW_ADDRESS + (INTERNAL_FLASH_PAGE_SIZE * EraseCounter));
  1495.     }
  1496.  
  1497.     Internal_Flash_Address = CORE_FW_ADDRESS;
  1498.     External_Flash_Address = sFLASH_Address;
  1499.  
  1500.     /* Program Internal Flash Bank1 */
  1501.     while ((Internal_Flash_Address < INTERNAL_FLASH_END_ADDRESS) && (FLASHStatus == FLASH_COMPLETE))
  1502.     {
  1503.         /* Read data from SPI Flash memory */
  1504.         sFLASH_ReadBuffer(External_Flash_Data, External_Flash_Address, 4);
  1505.         External_Flash_Address += 4;
  1506.  
  1507.         /* Program Word to Internal Flash memory */
  1508.         Internal_Flash_Data = (uint32_t)(External_Flash_Data[0] | (External_Flash_Data[1] << 8) | (External_Flash_Data[2] << 16) | (External_Flash_Data[3] << 24));
  1509.         //OR
  1510.         //Internal_Flash_Data = *((uint32_t *)External_Flash_Data);
  1511.         FLASHStatus = FLASH_ProgramWord(Internal_Flash_Address, Internal_Flash_Data);
  1512.         Internal_Flash_Address += 4;
  1513.     }
  1514.  
  1515.     /* Locks the FLASH Program Erase Controller */
  1516.     FLASH_Lock();
  1517.  
  1518. #endif
  1519. }
  1520.  
  1521. void FLASH_Begin(uint32_t sFLASH_Address)
  1522. {
  1523. #ifdef SPARK_SFLASH_ENABLE
  1524.  
  1525. #if defined (USE_SPARK_CORE_V02)
  1526.     LED_SetRGBColor(RGB_COLOR_MAGENTA);
  1527.     LED_On(LED_RGB);
  1528. #endif
  1529.  
  1530.    OTA_FLASHED_Status_SysFlag = 0x0000;
  1531.     //FLASH_OTA_Update_SysFlag = 0x5555;
  1532.     Save_SystemFlags();
  1533.     //BKP_WriteBackupRegister(BKP_DR10, 0x5555);
  1534.  
  1535.     External_Flash_Address = sFLASH_Address;
  1536.  
  1537.     /* Define the number of External Flash pages to be erased */
  1538.     NbrOfPage = EXTERNAL_FLASH_BLOCK_SIZE / sFLASH_PAGESIZE;
  1539.  
  1540.     /* Erase the SPI Flash pages */
  1541.     for (EraseCounter = 0; (EraseCounter < NbrOfPage); EraseCounter++)
  1542.     {
  1543.         sFLASH_EraseSector(sFLASH_Address + (sFLASH_PAGESIZE * EraseCounter));
  1544.     }
  1545.  
  1546. #endif
  1547. }
  1548.  
  1549. void FLASH_Update(uint8_t *pBuffer, uint32_t bufferSize)
  1550. {
  1551. #ifdef SPARK_SFLASH_ENABLE
  1552.  
  1553.     uint32_t i = bufferSize >> 2;
  1554.  
  1555.     /* Program External Flash */
  1556.     while (i--)
  1557.     {
  1558.         Internal_Flash_Data = *((uint32_t *)pBuffer);
  1559.         pBuffer += 4;
  1560.  
  1561.         /* Program Word to SPI Flash memory */
  1562.         External_Flash_Data[0] = (uint8_t)(Internal_Flash_Data & 0xFF);
  1563.         External_Flash_Data[1] = (uint8_t)((Internal_Flash_Data & 0xFF00) >> 8);
  1564.         External_Flash_Data[2] = (uint8_t)((Internal_Flash_Data & 0xFF0000) >> 16);
  1565.         External_Flash_Data[3] = (uint8_t)((Internal_Flash_Data & 0xFF000000) >> 24);
  1566.         //OR
  1567.         //*((uint32_t *)External_Flash_Data) = Internal_Flash_Data;
  1568.         sFLASH_WriteBuffer(External_Flash_Data, External_Flash_Address, 4);
  1569.         External_Flash_Address += 4;
  1570.     }
  1571.  
  1572.     i = bufferSize & 3;
  1573.  
  1574.     /* Not an aligned data */
  1575.     if (i != 0)
  1576.     {
  1577.         /* Program the last word to SPI Flash memory */
  1578.         Internal_Flash_Data = *((uint32_t *)pBuffer);
  1579.  
  1580.         /* Program Word to SPI Flash memory */
  1581.         External_Flash_Data[0] = (uint8_t)(Internal_Flash_Data & 0xFF);
  1582.         External_Flash_Data[1] = (uint8_t)((Internal_Flash_Data & 0xFF00) >> 8);
  1583.         External_Flash_Data[2] = (uint8_t)((Internal_Flash_Data & 0xFF0000) >> 16);
  1584.         External_Flash_Data[3] = (uint8_t)((Internal_Flash_Data & 0xFF000000) >> 24);
  1585.         //OR
  1586.         //*((uint32_t *)External_Flash_Data) = Internal_Flash_Data;
  1587.         sFLASH_WriteBuffer(External_Flash_Data, External_Flash_Address, 4);
  1588.     }
  1589.  
  1590. #if defined (USE_SPARK_CORE_V01)
  1591.     LED_Toggle(LED2);
  1592. #elif defined (USE_SPARK_CORE_V02)
  1593.     LED_Toggle(LED_RGB);
  1594. #endif
  1595.  
  1596. #endif
  1597. }
  1598.  
  1599. void FLASH_End(void)
  1600. {
  1601. #ifdef SPARK_SFLASH_ENABLE
  1602.  
  1603.     FLASH_OTA_Update_SysFlag = 0x0005;
  1604.     Save_SystemFlags();
  1605.  
  1606.     BKP_WriteBackupRegister(BKP_DR10, 0x0005);
  1607.  
  1608.     USB_Cable_Config(DISABLE);
  1609.  
  1610.     NVIC_SystemReset();
  1611.  
  1612. #endif
  1613. }
  1614.  
  1615. // keyBuffer length must be at least EXTERNAL_FLASH_SERVER_PUBLIC_KEY_LENGTH
  1616. void FLASH_Read_ServerPublicKey(uint8_t *keyBuffer)
  1617. {
  1618. #ifdef SPARK_SFLASH_ENABLE
  1619.  
  1620.     sFLASH_ReadBuffer(keyBuffer,
  1621.             EXTERNAL_FLASH_SERVER_PUBLIC_KEY_ADDRESS,
  1622.             EXTERNAL_FLASH_SERVER_PUBLIC_KEY_LENGTH);
  1623.  
  1624. #endif
  1625. }
  1626.  
  1627. // keyBuffer length must be at least EXTERNAL_FLASH_CORE_PRIVATE_KEY_LENGTH
  1628. void FLASH_Read_CorePrivateKey(uint8_t *keyBuffer)
  1629. {
  1630. #ifdef SPARK_SFLASH_ENABLE
  1631.  
  1632.     sFLASH_ReadBuffer(keyBuffer,
  1633.             EXTERNAL_FLASH_CORE_PRIVATE_KEY_ADDRESS,
  1634.             EXTERNAL_FLASH_CORE_PRIVATE_KEY_LENGTH);
  1635.  
  1636. #endif
  1637. }
  1638.  
  1639. void Factory_Flash_Reset(void)
  1640. {
  1641.     //First take backup of the current application firmware to External Flash
  1642.     FLASH_Backup(EXTERNAL_FLASH_BKP_ADDRESS);
  1643.  
  1644.     //Restore the Factory programmed application firmware from External Flash
  1645.     FLASH_Restore(EXTERNAL_FLASH_FAC_ADDRESS);
  1646.  
  1647.     Finish_Update();
  1648. }
  1649.  
  1650. void OTA_Flash_Update(void)
  1651. {
  1652.     //First take backup of the current application firmware to External Flash
  1653.     FLASH_Backup(EXTERNAL_FLASH_BKP_ADDRESS);
  1654.  
  1655.    FLASH_OTA_Update_SysFlag = 0x5555;
  1656.     Save_SystemFlags();
  1657.     BKP_WriteBackupRegister(BKP_DR10, 0x5555);
  1658.  
  1659.     //Restore the OTA programmed application firmware from External Flash
  1660.     FLASH_Restore(EXTERNAL_FLASH_OTA_ADDRESS);
  1661.  
  1662.     OTA_FLASHED_Status_SysFlag = 0x0001;
  1663.  
  1664.     Finish_Update();
  1665. }
  1666.  
  1667. bool OTA_Flashed_GetStatus(void)
  1668. {
  1669.     if(OTA_FLASHED_Status_SysFlag == 0x0001)
  1670.         return true;
  1671.     else
  1672.         return false;
  1673. }
  1674.  
  1675. void OTA_Flashed_ResetStatus(void)
  1676. {
  1677.     OTA_FLASHED_Status_SysFlag = 0x0000;
  1678.     Save_SystemFlags();
  1679. }
  1680.  
  1681. /*******************************************************************************
  1682. * Function Name  : Finish_Update.
  1683. * Description    : Reset the device.
  1684. * Input          : None.
  1685. * Return         : None.
  1686. *******************************************************************************/
  1687. void Finish_Update(void)
  1688. {
  1689.     FLASH_OTA_Update_SysFlag = 0x5000;
  1690.     Save_SystemFlags();
  1691.  
  1692.     BKP_WriteBackupRegister(BKP_DR10, 0x5000);
  1693.  
  1694.     USB_Cable_Config(DISABLE);
  1695.  
  1696.     NVIC_SystemReset();
  1697. }
  1698.  
  1699. /**
  1700.   * @brief  Computes the 32-bit CRC of a given buffer of byte data.
  1701.   * @param  pBuffer: pointer to the buffer containing the data to be computed
  1702.   * @param  BufferSize: Size of the buffer to be computed
  1703.   * @retval 32-bit CRC
  1704.   */
  1705. uint32_t Compute_CRC32(uint8_t *pBuffer, uint32_t bufferSize)
  1706. {
  1707.     uint32_t i, j;
  1708.     uint32_t Data;
  1709.  
  1710.     CRC_ResetDR();
  1711.  
  1712.     i = bufferSize >> 2;
  1713.  
  1714.     while (i--)
  1715.     {
  1716.         Data = *((uint32_t *)pBuffer);
  1717.         pBuffer += 4;
  1718.  
  1719.         Data = __RBIT(Data);//reverse the bit order of input Data
  1720.         CRC->DR = Data;
  1721.     }
  1722.  
  1723.     Data = CRC->DR;
  1724.     Data = __RBIT(Data);//reverse the bit order of output Data
  1725.  
  1726.     i = bufferSize & 3;
  1727.  
  1728.     while (i--)
  1729.     {
  1730.         Data ^= (uint32_t)*pBuffer++;
  1731.  
  1732.         for (j = 0 ; j < 8 ; j++)
  1733.         {
  1734.             if (Data & 1)
  1735.                 Data = (Data >> 1) ^ 0xEDB88320;
  1736.             else
  1737.                 Data >>= 1;
  1738.         }
  1739.     }
  1740.  
  1741.     Data ^= 0xFFFFFFFF;
  1742.  
  1743.     return Data;
  1744. }
  1745.  
  1746. void Get_Unique_Device_ID(uint8_t *Device_ID)
  1747. {
  1748.   uint32_t Device_IDx;
  1749.  
  1750.   Device_IDx = *(uint32_t*)ID1;
  1751.   *Device_ID++ = (uint8_t)(Device_IDx & 0xFF);
  1752.   *Device_ID++ = (uint8_t)((Device_IDx & 0xFF00) >> 8);
  1753.   *Device_ID++ = (uint8_t)((Device_IDx & 0xFF0000) >> 16);
  1754.   *Device_ID++ = (uint8_t)((Device_IDx & 0xFF000000) >> 24);
  1755.  
  1756.   Device_IDx = *(uint32_t*)ID2;
  1757.   *Device_ID++ = (uint8_t)(Device_IDx & 0xFF);
  1758.   *Device_ID++ = (uint8_t)((Device_IDx & 0xFF00) >> 8);
  1759.   *Device_ID++ = (uint8_t)((Device_IDx & 0xFF0000) >> 16);
  1760.   *Device_ID++ = (uint8_t)((Device_IDx & 0xFF000000) >> 24);
  1761.  
  1762.   Device_IDx = *(uint32_t*)ID3;
  1763.   *Device_ID++ = (uint8_t)(Device_IDx & 0xFF);
  1764.   *Device_ID++ = (uint8_t)((Device_IDx & 0xFF00) >> 8);
  1765.   *Device_ID++ = (uint8_t)((Device_IDx & 0xFF0000) >> 16);
  1766.   *Device_ID = (uint8_t)((Device_IDx & 0xFF000000) >> 24);
  1767. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement