main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /**
  2. ******************************************************************************
  3. * @file main.c
  4. * @author MCU Application Team
  5. * @brief Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "main.h"
  32. #include "py32f002bxx_ll_Start_Kit.h"
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private variables ---------------------------------------------------------*/
  35. unsigned int ADC_RE[4];
  36. unsigned int ADC_CH1_CUSUM;
  37. unsigned int ADC_CH2_CUSUM;
  38. unsigned int ADC_CH3_CUSUM;
  39. unsigned int ADC_CH4_CUSUM;
  40. unsigned int ADC_CH1_RE;
  41. unsigned int ADC_CH2_RE;
  42. unsigned int ADC_CH3_RE;
  43. unsigned int ADC_CH4_RE;
  44. // uint8_t SPI_TxBuff[32] = { \
  45. // 1,0xaa,2,0xaa,3,0xaa,4,0xaa,5,0xaa,\
  46. // 1,0xaa,2,0xaa,3,0xaa,4,0xaa,5,0xaa,\
  47. // 1,0xaa,2,0xaa,3,0xaa,4,0xaa,5,0xaa,\
  48. // 1,0xaa
  49. // };
  50. uint8_t SPI_TxBuff[32];
  51. uint8_t SPI_TxIndex = 0;
  52. uint8_t SPI_RxBuff[32];
  53. uint8_t SPI_RxIndex = 0;
  54. typedef struct
  55. {
  56. uint8_t HEAD;
  57. uint8_t len;
  58. uint8_t res[2];
  59. uint32_t key;
  60. uint16_t lx;
  61. uint16_t ly;
  62. uint16_t rx;
  63. uint16_t ry;
  64. }ZKM_Keybuf_t;
  65. ZKM_Keybuf_t Send_buf;
  66. /* Private user code ---------------------------------------------------------*/
  67. /* Private macro -------------------------------------------------------------*/
  68. /* Private function prototypes -----------------------------------------------*/
  69. static void APP_SystemClockConfig(void);
  70. static void APP_AdcEnable(void);
  71. static void APP_AdcCalibrate(void);
  72. static void APP_AdcConfig(void);
  73. static void APP_TimerInit(void);
  74. static void APP_Key_Init(void);
  75. static uint32_t APP_Read_Key_val(void);
  76. static void APP_FlashOBProgram(void);
  77. static void APP_ConfigSpi(void);
  78. static void APP_ConfigureEXTI(void);
  79. void *App_Memcpy(void *dst,const void *src,size_t size);
  80. uint16_t calculate_check_sum(void *buf, uint16_t len);
  81. /**
  82. * @brief Main program.
  83. * @param None
  84. * @retval int
  85. */
  86. int main(void)
  87. {
  88. static uint32_t cont = 0;
  89. static uint32_t adc_cont = 0;
  90. /* Configure Systemclock */
  91. APP_SystemClockConfig();
  92. /* Configure ADC parameters */
  93. APP_AdcConfig();
  94. /* Initialize USART(for printf use) */
  95. BSP_USART_Config();
  96. printf("Delay:1000 mS \n\r");
  97. // LL_mDelay(1000);
  98. printf("Delay End, program start \n\r");
  99. APP_ConfigureEXTI();
  100. /* Initialize TIM1 */
  101. APP_TimerInit();
  102. APP_Key_Init();
  103. APP_ConfigSpi();
  104. // memset(&Send_buf, 0, sizeof(ZKM_Keybuf_t));
  105. printf("sizeof(ZKM_Keybuf_t):%d\n\r",sizeof(ZKM_Keybuf_t));
  106. // LL_SPI_Enable(SPI1);
  107. while(1)
  108. {
  109. if(LL_ADC_IsActiveFlag_EOS(ADC1)==1) //4路ADC数据采集
  110. {
  111. LL_ADC_ClearFlag_EOS(ADC1);
  112. ADC_CH1_CUSUM = ADC_CH1_CUSUM - (ADC_CH1_CUSUM>>3) +ADC_RE[0];
  113. ADC_CH1_RE=ADC_CH1_CUSUM>>3;
  114. ADC_CH2_CUSUM = ADC_CH2_CUSUM - (ADC_CH2_CUSUM>>3) +ADC_RE[1];
  115. ADC_CH2_RE=ADC_CH2_CUSUM>>3;
  116. ADC_CH3_CUSUM = ADC_CH3_CUSUM - (ADC_CH3_CUSUM>>3) +ADC_RE[2];
  117. ADC_CH3_RE=ADC_CH3_CUSUM>>3;
  118. ADC_CH4_CUSUM = ADC_CH4_CUSUM - (ADC_CH4_CUSUM>>3) +ADC_RE[3];
  119. ADC_CH4_RE=ADC_CH4_CUSUM>>3;
  120. // printf("adc: %d, %d, %d, %d ,%x\n\r", ADC_CH1_RE, ADC_CH2_RE, ADC_CH3_RE, ADC_CH4_RE, APP_Read_Key_val());
  121. adc_cont++;
  122. Send_buf.HEAD = 0xA5;
  123. Send_buf.len = 16;
  124. Send_buf.res[0] = 0;
  125. Send_buf.res[1] = 0;
  126. Send_buf.key = APP_Read_Key_val();
  127. Send_buf.lx = ADC_CH1_RE;
  128. Send_buf.ly = ADC_CH2_RE;
  129. Send_buf.rx = ADC_CH3_RE;
  130. Send_buf.ry = ADC_CH4_RE;
  131. App_Memcpy(SPI_TxBuff, &Send_buf, 16);
  132. SPI_TxBuff[16] = calculate_check_sum(SPI_TxBuff, 16);
  133. }
  134. if(cont >= 100000){
  135. printf("adc_cont:%d, %x\n\r",adc_cont, APP_Read_Key_val());
  136. cont = 0;
  137. adc_cont = 0;
  138. }else{
  139. cont++;
  140. }
  141. }
  142. }
  143. /**
  144. * @brief Configure ADC parameters
  145. * @param None
  146. * @retval None
  147. */
  148. static void APP_AdcConfig(void)
  149. {
  150. /* Enable ADC1 clock */
  151. LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1);
  152. /* Enable GPIOA clock */
  153. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
  154. /* Configure PA7 pin in analog input mode */
  155. LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_7, LL_GPIO_MODE_ANALOG);
  156. /* Configure PA6 pin in analog input mode */
  157. LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_6, LL_GPIO_MODE_ANALOG);
  158. /* Configure PA6 pin in analog input mode */
  159. LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_4, LL_GPIO_MODE_ANALOG);
  160. /* Configure PA6 pin in analog input mode */
  161. LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_3, LL_GPIO_MODE_ANALOG);
  162. /* Set ADC clock to pclk/4 */
  163. LL_ADC_SetClock(ADC1, LL_ADC_CLOCK_SYNC_PCLK_DIV4);
  164. /* Set ADC resolution to 12 bit */
  165. LL_ADC_SetResolution(ADC1, LL_ADC_RESOLUTION_12B);
  166. /* ADC conversion data alignment: right aligned */
  167. LL_ADC_SetDataAlignment(ADC1, LL_ADC_DATA_ALIGN_RIGHT);
  168. /* No ADC low power mode activated */
  169. LL_ADC_SetLowPowerMode(ADC1, LL_ADC_LP_MODE_NONE);
  170. /* Sampling time 239.5 ADC clock cycles */
  171. LL_ADC_SetSamplingTimeCommonChannels(ADC1, LL_ADC_SAMPLINGTIME_239CYCLES_5);
  172. /* ADC regular group conversion trigger from external IP: TIM1 TRGO. */
  173. LL_ADC_REG_SetTriggerSource(ADC1, LL_ADC_REG_TRIG_EXT_TIM1_TRGO);
  174. /* Set Trigger edge to rising edge */
  175. LL_ADC_REG_SetTriggerEdge(ADC1, LL_ADC_REG_TRIG_EXT_RISING);
  176. /* Set ADC conversion mode to single mode: one conversion per trigger */
  177. LL_ADC_REG_SetContinuousMode(ADC1, LL_ADC_REG_CONV_SINGLE);
  178. /* ADC regular group behavior in case of overrun: data overwritten */
  179. LL_ADC_REG_SetOverrun(ADC1, LL_ADC_REG_OVR_DATA_OVERWRITTEN);
  180. /* Disable ADC regular group sequencer discontinuous mode */
  181. LL_ADC_REG_SetSequencerDiscont(ADC1, LL_ADC_REG_SEQ_DISCONT_DISABLE);
  182. /* Set channel 1/2/3/4/vrefint as conversion channel */
  183. LL_ADC_REG_SetSequencerChannels(ADC1, LL_ADC_CHANNEL_4);
  184. LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_3);
  185. LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_2);
  186. LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_1);
  187. // LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_VREFINT);
  188. /* Dose not enable internal conversion channel */
  189. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(ADC1), LL_ADC_PATH_INTERNAL_VREFINT );
  190. /* Enable EOC IT */
  191. LL_ADC_EnableIT_EOC(ADC1);
  192. NVIC_SetPriority(ADC_COMP_IRQn,1);
  193. NVIC_EnableIRQ(ADC_COMP_IRQn);
  194. /* ADC automatic self-calibration */
  195. APP_AdcCalibrate();
  196. /* Enable ADC */
  197. APP_AdcEnable();
  198. /* Start ADC conversion (if it is software triggered then start conversion directly) */
  199. LL_ADC_REG_StartConversion(ADC1);
  200. }
  201. /**
  202. * @brief ADC calibration program.
  203. * @param None
  204. * @retval None
  205. */
  206. static void APP_AdcCalibrate(void)
  207. {
  208. #if (USE_TIMEOUT == 1)
  209. uint32_t Timeout = 0;
  210. #endif
  211. if (LL_ADC_IsEnabled(ADC1) == 0)
  212. {
  213. /* Enable ADC calibration */
  214. LL_ADC_StartCalibration(ADC1);
  215. #if (USE_TIMEOUT == 1)
  216. Timeout = ADC_CALIBRATION_TIMEOUT_MS;
  217. #endif
  218. while (LL_ADC_IsCalibrationOnGoing(ADC1) != 0)
  219. {
  220. #if (USE_TIMEOUT == 1)
  221. /* Detects if the calibration has timed out */
  222. if (LL_SYSTICK_IsActiveCounterFlag())
  223. {
  224. if(Timeout-- == 0)
  225. {
  226. }
  227. }
  228. #endif
  229. }
  230. /* The delay between the end of ADC calibration and ADC enablement is at least 4 ADC clocks */
  231. LL_mDelay(1);
  232. }
  233. }
  234. /**
  235. * @brief Enable ADC.
  236. * @param None
  237. * @retval None
  238. */
  239. static void APP_AdcEnable(void)
  240. {
  241. /* Enable ADC */
  242. LL_ADC_Enable(ADC1);
  243. /* The delay between ADC enablement and ADC stabilization is at least 8 ADC clocks */
  244. LL_mDelay(1);
  245. }
  246. /**
  247. * @brief TIM1 configuration program.
  248. * @param None
  249. * @retval None
  250. */
  251. static void APP_TimerInit()
  252. {
  253. /* Enable TIM1 clock */
  254. LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_TIM1);
  255. /* Set TIM1 prescale */
  256. LL_TIM_SetPrescaler(TIM1,6);
  257. /* Set TIM1 auto-reload value */
  258. LL_TIM_SetAutoReload(TIM1, 3000);
  259. /* TIM1 Update event is used as trigger output */
  260. LL_TIM_SetTriggerOutput(TIM1,LL_TIM_TRGO_UPDATE);
  261. /* Enable TIM1 */
  262. LL_TIM_EnableCounter(TIM1);
  263. }
  264. /**
  265. * @brief Configure Systemclock
  266. * @param None
  267. * @retval None
  268. */
  269. static void APP_SystemClockConfig(void)
  270. {
  271. /* Enable SYSCFG clock and PWR clock */
  272. LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
  273. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
  274. /* Enable HSI */
  275. LL_RCC_HSI_Enable();
  276. while(LL_RCC_HSI_IsReady() != 1)
  277. {
  278. }
  279. /* Set AHB divider: HCLK = SYSCLK */
  280. LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  281. /* HSISYS used as SYSCLK clock source */
  282. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSISYS);
  283. while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSISYS)
  284. {
  285. }
  286. /* Set APB1 divider */
  287. LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  288. LL_Init1msTick(24000000);
  289. /* Update CMSIS variable (which can be updated also through SystemCoreClockUpdate function) */
  290. LL_SetSystemCoreClock(24000000);
  291. }
  292. /**
  293. * @brief Option byte program
  294. * @param None
  295. * @retval None
  296. */
  297. static void APP_FlashOBProgram(void)
  298. {
  299. FLASH_OBProgramInitTypeDef OBInitCfg;
  300. /* Unlock the Flash to enable the flash control register access */
  301. HAL_FLASH_Unlock();
  302. /* Unlock the Flash to enable access to the part of flash control register that is about option byte */
  303. HAL_FLASH_OB_Unlock();
  304. /* Initialize FLASH Option Bytes PROGRAM structure */
  305. OBInitCfg.OptionType = OPTIONBYTE_USER;
  306. OBInitCfg.USERType = OB_USER_BOR_EN | OB_USER_BOR_LEV | OB_USER_IWDG_SW | OB_USER_IWDG_STOP | OB_USER_SWD_NRST_MODE;
  307. OBInitCfg.USERConfig = OB_BOR_DISABLE | OB_BOR_LEVEL_3p1_3p2 | OB_IWDG_SW | OB_IWDG_STOP_ACTIVE | OB_SWD_PB6_GPIO_PC0 ;
  308. /* Execute option byte program */
  309. HAL_FLASH_OBProgram(&OBInitCfg);
  310. /* Lock the Flash to disable the flash control register access */
  311. HAL_FLASH_Lock();
  312. /* Lock the Flash to disable access to the part of flash control register that is about option byte */
  313. HAL_FLASH_OB_Lock();
  314. /* Generate a reset and let option byte reload */
  315. HAL_FLASH_OB_Launch();
  316. }
  317. void APP_GPIO_Init(GPIO_TypeDef *GPIOx, uint32_t PinMask)
  318. {
  319. // LL_GPIO_SetPinMode(GPIOx, PinMask, LL_GPIO_MODE_INPUT);
  320. // LL_GPIO_SetPinPull(GPIOx, PinMask, LL_GPIO_PULL_UP);
  321. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  322. GPIO_InitStruct.Pin = PinMask;
  323. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  324. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  325. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  326. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  327. GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
  328. LL_GPIO_Init(GPIOx, &GPIO_InitStruct);
  329. }
  330. void APP_Key_Init(void)
  331. {
  332. if(READ_BIT(FLASH->OPTR, OB_USER_SWD_NRST_MODE)!= OB_SWD_PB6_GPIO_PC0 )
  333. {
  334. /* Option byte program */
  335. APP_FlashOBProgram();
  336. }
  337. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
  338. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  339. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
  340. APP_GPIO_Init(GPIOA, LL_GPIO_PIN_5);
  341. // APP_GPIO_Init(GPIOA, LL_GPIO_PIN_2); //TODO SWDIO 调试使用
  342. // APP_GPIO_Init(GPIOB, LL_GPIO_PIN_6);
  343. APP_GPIO_Init(GPIOB, LL_GPIO_PIN_7);
  344. APP_GPIO_Init(GPIOB, LL_GPIO_PIN_5);
  345. // APP_GPIO_Init(GPIOB, LL_GPIO_PIN_4);
  346. APP_GPIO_Init(GPIOB, LL_GPIO_PIN_3);
  347. APP_GPIO_Init(GPIOB, LL_GPIO_PIN_2);
  348. APP_GPIO_Init(GPIOC, LL_GPIO_PIN_0);
  349. APP_GPIO_Init(GPIOC, LL_GPIO_PIN_1);
  350. }
  351. uint32_t APP_Read_Key_val(void)
  352. {
  353. uint32_t key_value = 0;
  354. if(LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_5) == 0) key_value |= HW_KEY_R3;
  355. // if(LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_2) == 0) key_value |= HW_KEY_L3;
  356. // if(LL_GPIO_IsInputPinSet(GPIOB, LL_GPIO_PIN_6) == 0) key_value |= HW_KEY_B;
  357. if(LL_GPIO_IsInputPinSet(GPIOB, LL_GPIO_PIN_7) == 0) key_value |= HW_KEY_A;
  358. if(LL_GPIO_IsInputPinSet(GPIOB, LL_GPIO_PIN_5) == 0) key_value |= HW_KEY_M2;
  359. // if(LL_GPIO_IsInputPinSet(GPIOB, LL_GPIO_PIN_4) == 0) key_value |= HW_KEY_M1;
  360. if(LL_GPIO_IsInputPinSet(GPIOB, LL_GPIO_PIN_3) == 0) key_value |= HW_KEY_START;
  361. if(LL_GPIO_IsInputPinSet(GPIOB, LL_GPIO_PIN_2) == 0) key_value |= HW_KEY_SELECT;
  362. if(LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_0) == 0) key_value |= HW_KEY_Y;
  363. if(LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_1) == 0) key_value |= HW_KEY_X;
  364. return key_value;
  365. }
  366. static void APP_ConfigureEXTI(void)
  367. {
  368. /* Enable GPIOA clock */
  369. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  370. /* Configure PA0 as input mode */
  371. LL_GPIO_InitTypeDef GPIO_InitStruct;
  372. GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
  373. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  374. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  375. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  376. /* Set EXTI0 to connected to PA0 pin */
  377. LL_EXTI_SetEXTISource(LL_EXTI_CONFIG_PORTB,LL_EXTI_CONFIG_LINE1);
  378. /* Set EXTI0 to event patterns, falling edge trigger */
  379. LL_EXTI_InitTypeDef EXTI_InitStruct;
  380. EXTI_InitStruct.Line = LL_EXTI_LINE_1;
  381. EXTI_InitStruct.LineCommand = ENABLE;
  382. EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
  383. EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING_FALLING;
  384. LL_EXTI_Init(&EXTI_InitStruct);
  385. /* Enable and set EXTI line0 Interrupt to the lowest priority */
  386. NVIC_SetPriority(EXTI0_1_IRQn, 1);
  387. NVIC_EnableIRQ(EXTI0_1_IRQn);
  388. }
  389. /**
  390. * @brief Set SPI1 fearures
  391. * @param None
  392. * @retval None
  393. */
  394. static void APP_ConfigSpi(void)
  395. {
  396. LL_SPI_InitTypeDef SPI_InitStruct = {0};
  397. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  398. /* Enable SPI1 periphreal clock */
  399. LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SPI1);
  400. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  401. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
  402. /**SPI1 I/O configuration
  403. PB2 ------> SPI1_SCK
  404. PA1 ------> SPI1_MISO
  405. PA7 ------> SPI1_MOSI
  406. */
  407. GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  408. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  409. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  410. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  411. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  412. GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
  413. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  414. GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
  415. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  416. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  417. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  418. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  419. GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
  420. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  421. GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  422. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  423. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  424. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  425. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  426. GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
  427. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  428. /* Enable SPI1 interrupt request */
  429. NVIC_SetPriority(SPI1_IRQn, 0);
  430. NVIC_EnableIRQ(SPI1_IRQn);
  431. /* Set SPI1 features */
  432. SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
  433. SPI_InitStruct.Mode = LL_SPI_MODE_SLAVE;
  434. SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
  435. SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_HIGH;
  436. SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
  437. SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
  438. SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV128;
  439. SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
  440. LL_SPI_Init(SPI1, &SPI_InitStruct);
  441. /* Configure SPI1 Transmission Interrupts */
  442. /* Enable TXE interrupt request */
  443. LL_SPI_EnableIT_TXE(SPI1);
  444. /* Enable RxNE interrupt request */
  445. LL_SPI_EnableIT_RXNE(SPI1);
  446. /* Enable SPI1 Error interrupt request */
  447. LL_SPI_EnableIT_ERR(SPI1);
  448. }
  449. /**
  450. * @brief SPI1 receive callback
  451. * @param None
  452. * @retval None
  453. */
  454. void APP_SpiRxCallback(void)
  455. {
  456. /* Read data register to clear RxNE flag */
  457. SPI_RxBuff[SPI_RxIndex++] = LL_SPI_ReceiveData8(SPI1);
  458. }
  459. /**
  460. * @brief SPI1 send callback
  461. * @param None
  462. * @retval None
  463. */
  464. void APP_SpiTxCallback(void)
  465. {
  466. /* Write data register to clear TxE flag */
  467. LL_SPI_TransmitData8(SPI1, SPI_TxBuff[SPI_TxIndex++]);
  468. // printf("t\n\r");
  469. }
  470. /**
  471. * @brief SPI transfer error treatment callback
  472. * @param None
  473. * @retval None
  474. */
  475. void APP_SpiTransferErrorCallback(void)
  476. {
  477. /* Disable RxNE interrupt request */
  478. LL_SPI_DisableIT_RXNE(SPI1);
  479. /* Disable TXE interrupt request */
  480. LL_SPI_DisableIT_TXE(SPI1);
  481. /* printf error notice */
  482. printf("SPI Transfer Error\n\r");
  483. }
  484. void APP_Spi_CS_EN(uint8_t en)
  485. {
  486. SPI_RxIndex = 0;
  487. SPI_TxIndex = 0;
  488. }
  489. /**
  490. * @brief Error handling function
  491. * @param None
  492. * @retval None
  493. */
  494. void APP_ErrorHandler(void)
  495. {
  496. /* Infinite loop */
  497. while (1)
  498. {
  499. }
  500. }
  501. void *App_Memcpy(void *dst,const void *src,size_t size)
  502. {
  503. char *psrc; //源地址
  504. char *pdst; //目标地址
  505. if(NULL == dst || NULL == src)
  506. {
  507. return NULL;
  508. }
  509. //源地址在前,对应上述情况2,需要自后向前拷贝
  510. if((src < dst)&&(char *)src+size > (char *)dst)
  511. {
  512. psrc = (char *)src + size - 1;
  513. pdst = (char *)dst + size - 1;
  514. while(size--)
  515. {
  516. *pdst-- = *psrc--;
  517. }
  518. }
  519. else //源地址在后,对应上述第一种情况,直接逐个拷贝*pdst++=*psrc++即可
  520. {
  521. psrc = (char *)src;
  522. pdst = (char *)dst;
  523. while(size--)
  524. {
  525. *pdst++ = *psrc++;
  526. }
  527. }
  528. return pdst;
  529. }
  530. uint16_t calculate_check_sum(void *buf, uint16_t len)
  531. {
  532. uint16_t i;
  533. uint16_t sum = 0;
  534. uint8_t *p;
  535. p = (uint8_t*)buf;
  536. //HEAD inglne , 100 -SUM
  537. for(i=0; i<len; i++){
  538. sum += *(p+i);
  539. }
  540. return sum;
  541. }
  542. #ifdef USE_FULL_ASSERT
  543. /**
  544. * @brief Reports the name of the source file and the source line number
  545. * where the assert_param error has occurred.
  546. * @param file:Pointer to the source file name
  547. * @param line:assert_param error line source number
  548. * @retval None
  549. */
  550. void assert_failed(uint8_t *file, uint32_t line)
  551. {
  552. /* User can add His own implementation to report the file name and line number,
  553. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  554. /* Infinite loop */
  555. while (1)
  556. {
  557. }
  558. }
  559. #endif /* USE_FULL_ASSERT */
  560. /************************ (C) COPYRIGHT Puya *****END OF FILE******************/