[sduino] Is it possible to mix C code into Arduino IDE to use other features?

Arduino on the STM8 (8 bit processor)
Post Reply
ElDominio
Posts: 12
Joined: Wed Feb 28, 2018 9:32 pm

[sduino] Is it possible to mix C code into Arduino IDE to use other features?

Post by ElDominio » Tue Dec 11, 2018 12:14 am

As the thread title explains, I want to use interrupts, and I know some about C in STVD, could I use the Std Periph EXTI to be able to use interrupts, while it isn't yet implemented in sduino?

Thanks beforehand

tenbaht
Posts: 14
Joined: Wed Oct 11, 2017 3:10 pm

Re: [sduino] Is it possible to mix C code into Arduino IDE to use other features?

Post by tenbaht » Tue Dec 11, 2018 10:21 am

Sure, no problem. The SPL is linked with the sketches anyway. Interrupts are a little special, though.

SDCC requires the interrupt function header to be in the same compile unit main() is defined. Most interrupt headers are activated in cores/sduino/stm8s_it.h and should work. But if your interrupt is not working this is a good place to start looking why.

It would be great to hear about your experiences on this! This might be a good topic for the project website https://tenbaht.github.io/sduino/

Michael
https://github.com/tenbaht/sduino - Programming the STM8 the Arduino way

ElDominio
Posts: 12
Joined: Wed Feb 28, 2018 9:32 pm

Re: [sduino] Is it possible to mix C code into Arduino IDE to use other features?

Post by ElDominio » Wed Dec 12, 2018 6:58 am

I got it working!

I needed to include stm8s_it.c from the Std Perih example section in my project, and modify the ISR handler there, and I declared my input as
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_PU_IT);
right alongside the typical pinModes for the other pins and such.

How can I help out in explaining and/or anyway I can return the favor please let me know!

now I only need to know how to modify a program variable from the ISR lol

**EDIT***

FIGURED IT OUT!
Variables to be modified have to be called as extern from stm8s_it.c, and byte is not an sdcc compatible typedef. Change to uint8_t and called it as an extern and viola! We have interrupts!

tenbaht
Posts: 14
Joined: Wed Oct 11, 2017 3:10 pm

Re: [sduino] Is it possible to mix C code into Arduino IDE to use other features?

Post by tenbaht » Wed Dec 12, 2018 9:53 am

I am glad to hear it worked!
Could you maybe send me the source code of your example? The sketch part and the SPL example part. This might be an interesting thing to show as an example.
https://github.com/tenbaht/sduino - Programming the STM8 the Arduino way

ElDominio
Posts: 12
Joined: Wed Feb 28, 2018 9:32 pm

Re: [sduino] Is it possible to mix C code into Arduino IDE to use other features?

Post by ElDominio » Wed Dec 12, 2018 5:05 pm

Here is the main, called in this case "testSTM8.ino":

Code: Select all

byte bitField = 0;
#define BIT_METH_STATE  0
#define BIT_LOW_LEVEL   1
#define BIT_PUMP_STATE  2
#define BIT_TPS_OR_MAP  3
#define BIT_WARNING     4
//program variables
uint8_t triggerCounter = 0;
uint16_t analogIn = 0;
#include 
#include "pins.h"
void setup() {
  // put your setup code here, to run once:
  pinMode(MAP_INPUT, INPUT);
  GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_PU_IT);
  pinMode(PUMP_OUT, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);
  EEPROM_write(1,0x00);
 Serial_begin(115200);
  EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOA, EXTI_SENSITIVITY_FALL_ONLY); 
  EXTI_SetTLISensitivity(EXTI_TLISENSITIVITY_FALL_ONLY);
  enableInterrupts();
}
void loop() {
  // put your main code here, to run repeatedly:;
    unsigned long currentMillis = millis();
  if((currentMillis % 100) == 0){
    analogIn = analogRead(MAP_INPUT);
    bitWrite(bitField, BIT_LOW_LEVEL, digitalRead(LOW_LVL_PIN));
            Serial_print_s("inside 100ms loop\n");
  } 
  if ((currentMillis % 500) == 0){
            Serial_print_s("Inside 500ms loop\n");
    if (bitRead(bitField, BIT_LOW_LEVEL)){
      bitWrite(bitField, BIT_WARNING, !(bitRead(bitField, BIT_WARNING)));
        //Serial_print_s("Inside 500ms loop\n");
    }
  }
  if (triggerCounter > 0){
    Serial_print_s("TRIGGER");
    triggerCounter = 0;
  }
  if((analogIn > 400) || digitalRead(TST_BTN_PIN)){
    bitSet(bitField, BIT_PUMP_STATE);
  }
  else{ 
    bitClear(bitField, BIT_PUMP_STATE);
    }
  digitalWrite(PUMP_OUT, bitRead(bitField, BIT_PUMP_STATE));
  if((bitRead(bitField, BIT_LOW_LEVEL) && !(bitRead(bitField, BIT_PUMP_STATE)))){
    digitalWrite(RED_LED_PIN, bitRead(bitField, BIT_WARNING));
    digitalWrite(BLU_LED_PIN, HIGH);
  }
  else if (!(bitRead(bitField, BIT_PUMP_STATE))){
    digitalWrite(RED_LED_PIN, LOW);
    digitalWrite(BLU_LED_PIN, HIGH);
  }
  else if(bitRead(bitField, BIT_PUMP_STATE)){
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(BLU_LED_PIN, LOW);
  }
  else{
    digitalWrite(RED_LED_PIN, LOW);
    digitalWrite(BLU_LED_PIN, LOW);
  }
}
Here is pins.h:

Code: Select all

#ifndef PINS_H
#define PINS_H
#define MAP_INPUT   PD2
#define TPS_INPUT   PC4
#define PUMP_OUT    PD4
#define LOW_LVL_PIN PD3
#define RED_LED_PIN PB5
#define BLU_LED_PIN PB4
#define TST_BTN_PIN PC3
#endif // PINS_H
And here is the stm8s_it.c which i'm sure can be shrunk down quite a bit:

Code: Select all

/**
  ******************************************************************************
  * @file     stm8s_it.c
  * @author   MCD Application Team
  * @version  V2.2.0
  * @date     30-September-2014
  * @brief    Main Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * 

© COPYRIGHT 2014 STMicroelectronics

* * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); * You may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.st.com/software_license_agreement_liberty_v2 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "stm8s_it.h" //#include "main.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /* Public functions ----------------------------------------------------------*/ /** @addtogroup EXTI_InterruptPriority * @{ */ #ifdef _COSMIC_ /** * @brief Dummy interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(NonHandledInterrupt, 25) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #endif /*_COSMIC_*/ /** * @brief TRAP interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER_TRAP(TRAP_IRQHandler) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief Top Level Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TLI_IRQHandler, 0) { // GPIO_WriteReverse(LEDS_PORT, (GPIO_Pin_TypeDef)(LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN)); } /** * @brief Auto Wake Up Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(AWU_IRQHandler, 1) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief Clock Controller Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(CLK_IRQHandler, 2) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief External Interrupt PORTA Interrupt routine * @param None * @retval None */ extern uint8_t triggerCounter; INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ GPIO_WriteReverse(GPIOB, GPIO_PIN_5); triggerCounter = triggerCounter + 1; //Serial_print_s("trigger"); } /** * @brief External Interrupt PORTB Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4) {/* if ((GPIO_ReadInputData(JOYSTICK_PORT) & JOYSTICK_LEFT_PIN) == 0x00) { GPIO_WriteReverse(LEDS_PORT, LED1_PIN); } if ((GPIO_ReadInputData(JOYSTICK_PORT) & JOYSTICK_RIGHT_PIN) == 0x00) { GPIO_WriteReverse(LEDS_PORT, LED2_PIN); } if ((GPIO_ReadInputData(JOYSTICK_PORT) & JOYSTICK_UP_PIN) == 0x00) { GPIO_WriteReverse(LEDS_PORT, LED3_PIN); } if ((GPIO_ReadInputData(JOYSTICK_PORT) & JOYSTICK_DOWN_PIN) == 0x00) { GPIO_WriteReverse(LEDS_PORT, LED4_PIN); } */ } /** * @brief External Interrupt PORTC Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief External Interrupt PORTD Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief External Interrupt PORTE Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #ifdef STM8S903 /** * @brief External Interrupt PORTF Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #endif /*STM8S903*/ #if defined (STM8S208) || defined (STM8AF52Ax) /** * @brief CAN RX Interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief CAN TX Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #endif /*STM8S208 || STM8AF52Ax */ /** * @brief SPI Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(SPI_IRQHandler, 10) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief Timer1 Update/Overflow/Trigger/Break Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief Timer1 Capture/Compare Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #ifdef STM8S903 /** * @brief Timer5 Update/Overflow/Break/Trigger Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief Timer5 Capture/Compare Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */ /** * @brief Timer2 Update/Overflow/Break Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief Timer2 Capture/Compare Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #endif /*STM8S903*/ #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \ defined(STM8S005) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x) /** * @brief Timer3 Update/Overflow/Break Interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief Timer3 Capture/Compare Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #endif /*STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */ #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \ defined(STM8S003) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903) /** * @brief UART1 TX Interrupt routine * @param None * @retval None */ //INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17) //{ /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ //} /** * @brief UART1 RX Interrupt routine * @param None * @retval None */ //INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18) //{ /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ //} #endif /*STM8S105*/ /** * @brief I2C Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(I2C_IRQHandler, 19) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x) /** * @brief UART2 TX interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief UART2 RX interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #endif /* STM8S105*/ #if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax) /** * @brief UART3 TX interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @brief UART3 RX interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */ #if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax) /** * @brief ADC2 interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(ADC2_IRQHandler, 22) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ return; } #else /*STM8S105, STM8S103 or STM8S903 or STM8AF626x */ /** * @brief ADC1 interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(ADC1_IRQHandler, 22) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ return; } #endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */ #ifdef STM8S903 /** * @brief Timer6 Update/Overflow/Trigger Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } #else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */ /** * @brief Timer4 Update/Overflow Interrupt routine * @param None * @retval None */ // INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23) //{ /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ //} #endif /*STM8S903*/ /** * @brief Eeprom EEC Interrupt routine * @param None * @retval None */ INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ } /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
This made the light turn on and trigger counter to increase.
BTW it is code for n automotive methanol injection system I am making hehe

ElDominio
Posts: 12
Joined: Wed Feb 28, 2018 9:32 pm

Re: [sduino] Is it possible to mix C code into Arduino IDE to use other features?

Post by ElDominio » Mon Dec 17, 2018 6:56 pm

Question, how would I go about setting PWM frequency on an analogWrite? I suppose I have to use STD_Periph stuff too? Or is there an easy way out? lol

Post Reply