I’m trying to use CubeProgrammer_API.dll in python to program STM32 microcontroller memory over USART interface.
In general, following operations shall be performed at the beginning:
- Get list of available serial ports using getUsartList() call (it returns array of usartConnectParameters structures).
- Start connection to target through USART interface using connectUsartBootloader() call (passing selected usartConnectParameters structure)
CubeProgrammer_API.h
typedef struct usartConnectParameters
{
char portName[100]; /**< Interface identifier: COM1, COM2, /dev/ttyS0...*/
unsigned int baudrate; /**< Speed transmission: 115200, 9600... */
usartParity parity; /**< Parity bit: value in usartParity. */
unsigned char dataBits; /**< Data bit: value in {6, 7, 8}. */
float stopBits; /**< Stop bit: value in {1, 1.5, 2}. */
usartFlowControl flowControl; /**< Flow control: value in usartFlowControl. */
int statusRTS; /**< RTS: Value in {0,1}. */
int statusDTR; /**< DTR: Value in {0,1}. */
unsigned char noinitBits; /**< Set No Init bits: value in {0,1}. */
char rdu; /**< request a read unprotect: value in {0,1}.*/
char tzenreg; /**< request a TZEN regression: value in {0,1}.*/
}usartConnectParameters;
int getUsartList(usartConnectParameters** usartList);
int connectUsartBootloader(usartConnectParameters usartParameters);
So far I’m able to invoke getUsartList() function and receive correct array of usartConnectParameters structures.
script.py
from ctypes import *
dll = cdll.LoadLibrary(r'C:Program FilesSTMicroelectronicsSTM32CubeSTM32CubeProgrammerapilibCubeProgrammer_API.dll')
class usartConnectParameters(Structure):
_fields_ = [("portName", c_byte * 100), # or c_char * 100
("baudrate", c_uint),
("parity", c_int), # usartParity
("dataBits", c_ubyte),
("stopBits", c_float),
("flowControl", c_int), # usartFlowControl
("statusRTS", c_int),
("statusDTR", c_int),
("noinitBits", c_ubyte),
("rdu", c_byte),
("tzenreg", c_byte)]
getUsartList = dll.getUsartList
getUsartList.argtypes = (POINTER(POINTER(usartConnectParameters)),)
getUsartList.restype = c_int
uartParam = POINTER(usartConnectParameters)()
uartCnt = getUsartList(byref(uartParam))
Unfortunately when I’m trying to invoke connectUsartBootloader() function with one usartConnectParameters structure from the list,
connectUsartBootloader = dll.connectUsartBootloader
connectUsartBootloader.argtypes = (usartConnectParameters,)
connectUsartBootloader.restype = c_int
connectUsartBootloader(uartParam[0])
I’m getting an exception:
OSError: exception: access violation writing 0x0000000000000000
Can anyone point me in the right direction or provide some more info on this kind of errors?
tma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.