#include <stdint.h>
#include <stm32l031xx.h>
void pinMode(GPIO_TypeDef *Port, uint32_t BitNumber, uint32_t Mode);
extern void asm_delay(uint32_t dly);
extern void asm_setPortBit(GPIO_TypeDef *Port, uint32_t BitNumber);
extern void asm_clearPortBit(GPIO_TypeDef *Port, uint32_t BitNumber);
extern void asm_blinkOnce(void);
extern void asm_blinkLoop(void);
//NOTE: If you're using WOKWI to complete this lab, make sure rename "sketch.ino" to "main.c"
// And add a new file for the assembly called "asm_library.S"
// Otherwise, the assembly code won't link when you compile
void setup()
{
RCC->IOPENR |= (1 << 1);
pinMode(GPIOB, 3, 1); // PB3 as output
pinMode(GPIOB, 4, 0); // PB4 as input
// Enable internal pull-up on PB4
GPIOB->PUPDR &= ~(3U << 8);
GPIOB->PUPDR |= (1U << 8);
}
int main(void)
{
setup();
asm_blinkLoop(); // Replaced while(1) loop with assembly loop
}
void pinMode(GPIO_TypeDef *Port, uint32_t BitNumber, uint32_t Mode)
{
// This function writes the given Mode bits to the appropriate location for
// the given BitNumber in the Port specified. It leaves other bits unchanged
// Mode values:
// 0 : digital input
// 1 : digital output
// 2 : Alternative function
// 3 : Analog input
uint32_t mode_value = Port->MODER; // read current value of Mode register
Mode = Mode << (2 * BitNumber); // There are two Mode bits per port bit
//so need to shift
// the mask for Mode up to the proper location
mode_value = mode_value & ~(3u << (BitNumber * 2)); // Clear out old mode bits
mode_value = mode_value | Mode; // set new bits
Port->MODER = mode_value; // write back to port mode register
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6