Inlined STM8S SPL (STM8S103) for use with SDCC

bootstrap.c 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Created by MightyPork on 2017/02/10.
  3. //
  4. #include <stm8s.h>
  5. #include "bootstrap.h"
  6. /** Global time base */
  7. volatile uint32_t time_ms;
  8. /**
  9. * Putchar for printf
  10. * @param c - char to print
  11. */
  12. void putchar(char c)
  13. {
  14. while ((UART1->SR & UART1_SR_TXE) == 0);
  15. UART1->DR = (u8)c;
  16. }
  17. /**
  18. * Init for the chinese STM8 board
  19. * - enable LED
  20. * - enable UART @ 115200
  21. * - set up UART rx irq
  22. */
  23. void SimpleInit(void)
  24. {
  25. // Disable default div/8 HSI prescaller
  26. CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  27. // LED
  28. GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
  29. // UART init & enable IRQ
  30. UART_SimpleInit(UART_BAUD_115200);
  31. UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
  32. // Timebase generation counter
  33. TIM4_UpdateRequestConfig(TIM4_UPDATESOURCE_REGULAR);
  34. TIM4_PrescalerConfig(TIM4_PRESCALER_128, TIM4_PSCRELOADMODE_IMMEDIATE);
  35. TIM4_SetAutoreload(0xFF);
  36. TIM4_ARRPreloadConfig(ENABLE);
  37. TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  38. TIM4_Cmd(ENABLE);
  39. enableInterrupts();
  40. }
  41. /**
  42. * @brief Timer4 Update/Overflow Interrupt routine.
  43. * @param None
  44. * @retval None
  45. */
  46. INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  47. {
  48. time_ms++;
  49. TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
  50. }
  51. /** Delay ms */
  52. void Delay(uint16_t ms)
  53. {
  54. uint32_t start = time_ms;
  55. while ((time_ms - start) < ms);
  56. }
  57. /**
  58. * UART1 RX Interrupt routine.
  59. */
  60. INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
  61. {
  62. if (UART1->SR & UART1_SR_RXNE) {
  63. UART_HandleRx(UART1->DR);
  64. }
  65. }
  66. // Comment out if custom rx handler is added
  67. #if 1
  68. void UART_HandleRx(char c)
  69. {
  70. // echo
  71. putchar(c);
  72. }
  73. #endif