stm32f3_hal_seccan.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "stm32f3xx_hal_can.h"
  5. #include "stm32f3xx_hal_rcc.h"
  6. #include "stm32f3xx_hal_gpio.h"
  7. #include "stm32f3_hal_seccan.h"
  8. #include "stm32f3xx_hal_adc.h"
  9. #include "stm32f3xx_hal_tim.h"
  10. static CAN_HandleTypeDef myhcan;
  11. static CanTxMsgTypeDef msg;
  12. static CanRxMsgTypeDef RxMessage;
  13. typedef enum {
  14. CAN_RET_BAD_ADDRESS = -1,
  15. CAN_RET_TOO_MUCH_DATA = -2,
  16. CAN_RET_TIMEOUT = -3,
  17. CAN_RET_BUSY = -4,
  18. CAN_RET_ERROR = -5,
  19. CAN_RET_ERROR_UNKNOWN = -6
  20. } can_return_t;
  21. typedef enum {
  22. ADC_RET_OK = 0,
  23. ADC_RET_ADC_INIT = -1,
  24. ADC_RET_CHANNEL_INIT = -2,
  25. ADC_RET_ADC_START = -3,
  26. ADC_RET_ADC_TIMEOUT = -4,
  27. ADC_RET_ADC_STOP = -5,
  28. ADC_RET_PIN_INIT = -6
  29. } adc_return_t;
  30. //////////////////////////CAN/////////////////////////////////
  31. static can_return_t can_return_error(HAL_StatusTypeDef canError) {
  32. can_return_t r_error;
  33. switch (canError) {
  34. case HAL_TIMEOUT:
  35. r_error = CAN_RET_TIMEOUT;
  36. break;
  37. case HAL_BUSY:
  38. r_error = CAN_RET_BUSY;
  39. break;
  40. case HAL_ERROR:
  41. r_error = CAN_RET_ERROR;
  42. break;
  43. default:
  44. r_error = CAN_RET_ERROR_UNKNOWN;
  45. }
  46. return r_error;
  47. }
  48. int init_can(void)
  49. {
  50. HAL_StatusTypeDef rval;
  51. CAN_FilterConfTypeDef sFilterConfig;
  52. myhcan.Instance = CAN;
  53. myhcan.Init.Prescaler = 8;
  54. myhcan.Init.Mode = CAN_MODE_NORMAL;
  55. myhcan.Init.SJW = CAN_SJW_1TQ;
  56. myhcan.Init.BS1 = CAN_BS1_2TQ;
  57. myhcan.Init.BS2 = CAN_BS2_1TQ;
  58. myhcan.Init.TTCM = DISABLE;
  59. myhcan.Init.ABOM = DISABLE;
  60. myhcan.Init.AWUM = DISABLE;
  61. myhcan.Init.NART = DISABLE;
  62. myhcan.Init.RFLM = DISABLE;
  63. myhcan.Init.TXFP = DISABLE;
  64. GPIO_InitTypeDef GPIO_InitStruct;
  65. /* Peripheral clock enable */
  66. __HAL_RCC_CAN1_CLK_ENABLE()
  67. ;
  68. __HAL_RCC_GPIOB_CLK_ENABLE()
  69. ;
  70. /**CAN GPIO Configuration
  71. PB8 ------> CAN_RX
  72. PB9 ------> CAN_TX
  73. */
  74. GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
  75. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  76. GPIO_InitStruct.Pull = GPIO_NOPULL;
  77. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  78. GPIO_InitStruct.Alternate = GPIO_AF9_CAN;
  79. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  80. rval = HAL_CAN_Init(&myhcan);
  81. if (rval != HAL_OK)
  82. return -1;
  83. /*##-2- Configure the CAN Filter ###########################################*/
  84. sFilterConfig.FilterNumber = 0;
  85. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  86. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  87. sFilterConfig.FilterIdHigh = 0x0000;
  88. sFilterConfig.FilterIdLow = 0x0000;
  89. sFilterConfig.FilterMaskIdHigh = 0x0000;
  90. sFilterConfig.FilterMaskIdLow = 0x0000;
  91. sFilterConfig.FilterFIFOAssignment = 0;
  92. sFilterConfig.FilterActivation = ENABLE;
  93. sFilterConfig.BankNumber = 14;
  94. if (HAL_CAN_ConfigFilter(&myhcan, &sFilterConfig) != HAL_OK) {
  95. /* Filter configuration Error */
  96. //Error_Handler();
  97. return -1;
  98. }
  99. msg.StdId = 0x124; //1BC;
  100. msg.ExtId = 0x12345ABA;
  101. msg.IDE = CAN_ID_EXT;
  102. msg.RTR = CAN_RTR_DATA;
  103. myhcan.pTxMsg = &msg;
  104. myhcan.pRxMsg = &RxMessage;
  105. return 0;
  106. }
  107. int write_can(uint32_t addr, uint8_t *data, int len)
  108. {
  109. HAL_StatusTypeDef canError;
  110. int index;
  111. if (len > 8) {
  112. return (CAN_RET_TOO_MUCH_DATA);
  113. }
  114. msg.DLC = len;
  115. for (index = 0; index <= len; index++) {
  116. if (index >= 8) {
  117. break;
  118. }
  119. msg.Data[index] = *data++;
  120. }
  121. if (addr <= 0x7FF) {
  122. msg.StdId = addr;
  123. msg.IDE = CAN_ID_STD;
  124. } else if (addr <= 0x1FFFFFFF) {
  125. msg.ExtId = addr;
  126. msg.IDE = CAN_ID_EXT;
  127. } else {
  128. return (CAN_RET_BAD_ADDRESS);
  129. }
  130. // Transmit the data.
  131. canError = HAL_CAN_Transmit(&myhcan, 100);
  132. if (myhcan.State == HAL_CAN_STATE_TIMEOUT) {
  133. //restart bus?
  134. HAL_CAN_DeInit(&myhcan);
  135. init_can();
  136. }
  137. if (canError == HAL_OK) {
  138. return (index);
  139. }
  140. return (can_return_error(canError));
  141. }
  142. int read_can(uint8_t *data, uint32_t *addr, int len)
  143. {
  144. HAL_StatusTypeDef canError;
  145. if (len < 8) {
  146. return (CAN_RET_TOO_MUCH_DATA);
  147. }
  148. canError = HAL_CAN_Receive(&myhcan, CAN_FIFO0, 0);
  149. if (canError == HAL_OK) {
  150. memcpy(data, RxMessage.Data, 8);
  151. if (RxMessage.IDE == CAN_ID_EXT) {
  152. *addr = RxMessage.ExtId;
  153. } else {
  154. *addr = RxMessage.StdId;
  155. }
  156. return ((int) RxMessage.DLC);
  157. }
  158. return (can_return_error(canError));
  159. }
  160. /////////////////////////ADC/////////////////////////////
  161. #define ADC_READ_TIMEOUT 500
  162. static ADC_HandleTypeDef myadc;
  163. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  164. {
  165. /* Prevent unused argument(s) compilation warning */
  166. UNUSED(hadc);
  167. /* NOTE : This function should not be modified. When the callback is needed,
  168. function HAL_ADC_MspInit must be implemented in the user file.
  169. */
  170. }
  171. int init_adc(void)
  172. {
  173. GPIO_InitTypeDef gpio;
  174. gpio.Pin = GPIO_PIN_14;
  175. gpio.Mode = GPIO_MODE_ANALOG;
  176. gpio.Pull = GPIO_NOPULL;
  177. gpio.Speed = GPIO_SPEED_FREQ_HIGH;
  178. HAL_GPIO_Init(GPIOB, &gpio);
  179. __HAL_RCC_ADC34_CLK_ENABLE();
  180. __HAL_RCC_GPIOB_CLK_ENABLE();
  181. //init_ADC_pin();
  182. myadc.Instance = ADC4;
  183. myadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; //div by 4 rec for 12 and 10 bit
  184. myadc.Init.Resolution = ADC_RESOLUTION_12B;
  185. myadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; //lsb at bit 0
  186. myadc.Init.ScanConvMode = ADC_SCAN_ENABLE; //just one channel needed, so no scanning
  187. myadc.Init.EOCSelection = DISABLE;//ADC_EOC_SINGLE_CONV; //does this matter with one channel?
  188. myadc.Init.LowPowerAutoWait = DISABLE;
  189. myadc.Init.ContinuousConvMode = ENABLE; //single mode now
  190. myadc.Init.NbrOfConversion = 1; //does this matter for single channel?
  191. myadc.Init.DiscontinuousConvMode = DISABLE; //discarded with continuous mode
  192. myadc.Init.NbrOfDiscConversion = 1; //discarded without Discont mode
  193. myadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; //start ADC with software, not ext trigger
  194. myadc.Init.ExternalTrigConvEdge = 0; //discarded with software start
  195. myadc.Init.DMAContinuousRequests = DISABLE;
  196. myadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; //overwrite if data not read before next conversion is done
  197. if (HAL_ADC_Init(&myadc) != HAL_OK) {
  198. //send error message
  199. return ADC_RET_ADC_INIT;
  200. }
  201. ADC_ChannelConfTypeDef channel;
  202. channel.Channel = ADC_CHANNEL_4; //PB14
  203. channel.Rank = ADC_REGULAR_RANK_1; //single conversion, does this matter?
  204. channel.SamplingTime = ADC_SAMPLETIME_19CYCLES_5;
  205. channel.SingleDiff = ADC_SINGLE_ENDED;
  206. channel.OffsetNumber = ADC_OFFSET_NONE;
  207. channel.Offset = 0;
  208. if (HAL_ADC_ConfigChannel(&myadc, &channel) != HAL_OK) {
  209. //send error
  210. return ADC_RET_CHANNEL_INIT;
  211. }
  212. if (HAL_ADC_Start(&myadc) != HAL_OK) {
  213. return ADC_RET_ADC_START;
  214. }
  215. return 0;
  216. }
  217. int read_adc(uint16_t *val)
  218. {
  219. if (HAL_ADC_PollForConversion(&myadc, ADC_READ_TIMEOUT) == HAL_OK) {
  220. //good to read
  221. *val = HAL_ADC_GetValue(&myadc);
  222. } else {
  223. //error
  224. return ADC_RET_ADC_TIMEOUT;
  225. }
  226. return 0;
  227. }
  228. /////////////////////PWM//////////////////////////////
  229. static TIM_OC_InitTypeDef pwm;
  230. static TIM_HandleTypeDef tim;
  231. int init_pwm(void)
  232. {
  233. HAL_StatusTypeDef rtn;
  234. __HAL_RCC_TIM1_CLK_ENABLE();
  235. __HAL_RCC_GPIOA_CLK_ENABLE();
  236. GPIO_InitTypeDef GpioInit;
  237. GpioInit.Pin = GPIO_PIN_11;
  238. GpioInit.Mode = GPIO_MODE_AF_PP;
  239. GpioInit.Pull = GPIO_NOPULL;
  240. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  241. GpioInit.Alternate = GPIO_AF11_TIM1;
  242. HAL_GPIO_Init(GPIOA, &GpioInit);
  243. tim.Instance = TIM1;
  244. tim.Init.CounterMode = TIM_COUNTERMODE_DOWN;
  245. tim.Init.Prescaler = 0;
  246. tim.Init.Period = 0xFFFF;
  247. tim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  248. tim.Init.RepetitionCounter = 0x0;
  249. tim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  250. pwm.OCMode = TIM_OCMODE_PWM1;
  251. pwm.Pulse = 0;
  252. pwm.OCPolarity = TIM_OCPOLARITY_HIGH;
  253. pwm.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  254. pwm.OCFastMode = TIM_OCFAST_ENABLE; //s;nic
  255. pwm.OCIdleState = TIM_OCIDLESTATE_RESET;
  256. pwm.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  257. tim.Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  258. rtn = HAL_TIM_PWM_Init(&tim);
  259. rtn = HAL_TIM_PWM_ConfigChannel(&tim, &pwm, TIM_CHANNEL_4);
  260. rtn = HAL_TIM_PWM_Start(&tim, TIM_CHANNEL_4);
  261. if (rtn != HAL_OK) {
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. void change_pwm(uint16_t duty_cycle)
  267. {
  268. pwm.Pulse = duty_cycle;
  269. HAL_TIM_PWM_ConfigChannel(&tim, &pwm, TIM_CHANNEL_4);
  270. HAL_TIM_PWM_Start(&tim, TIM_CHANNEL_4);
  271. }